Skip to content

Commit 9d6eb53

Browse files
fix #7: Import destructuring and minor fixes
1 parent f5d6ca3 commit 9d6eb53

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/connection/inet-socket-connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as net from 'net';
1+
import { Socket, createConnection } from 'net';
22
import BaseConnection from './base-connection';
33

44
export default class InetSocketConnection extends BaseConnection {
5-
client?: net.Socket;
5+
client?: Socket;
66
host: string;
77
port: number;
88

@@ -14,7 +14,7 @@ export default class InetSocketConnection extends BaseConnection {
1414

1515
setupConnection(): Promise<void> {
1616
return new Promise(resolve => {
17-
this.client = net.createConnection(this.port, this.host);
17+
this.client = createConnection(this.port, this.host);
1818
this.client.on('data', (data) => {
1919
const dataLines = data.toString().split(/\r?\n/);
2020
dataLines.map((data) => {

src/connection/unix-socket-connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as net from 'net';
1+
import { Socket, createConnection } from 'net';
22
import BaseConnection from './base-connection';
33

44
export default class UnixSocketConnection extends BaseConnection {
5-
client?: net.Socket;
5+
client?: Socket;
66
sockPath: string;
77

88
constructor(sockPath: string) {
@@ -12,7 +12,7 @@ export default class UnixSocketConnection extends BaseConnection {
1212

1313
setupConnection(): Promise<void> {
1414
return new Promise(resolve => {
15-
this.client = net.createConnection(this.sockPath);
15+
this.client = createConnection(this.sockPath);
1616
this.client.on('data', (data) => {
1717
const dataLines = data.toString().split(/\r?\n/);
1818
dataLines.map((data) => {

src/juno-node.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as net from 'net';
2-
import * as fs from 'fs';
1+
import { isIP } from 'net';
2+
import { promises as fs } from 'fs';
33
import { BaseProtocol } from './protocol/base-protocol';
44
import BaseConnection from './connection/base-connection';
55
import { JsonProtocol } from './protocol/json-protocol';
@@ -33,27 +33,35 @@ export default class JunoModule {
3333
public static async default(socketPath: string) {
3434
const [ host, port ] = socketPath.split(':');
3535

36-
if (net.isIP(host) && typeof Number(port) === 'number') {
36+
if (isIP(host) && !isNaN(Number(port))) {
3737
return this.fromInetSocket(host, Number(port));
3838
}
39-
if ( (await fs.promises.lstat(socketPath)).isSocket() ) {
39+
if ( (await fs.lstat(socketPath)).isSocket() ) {
4040
return this.fromUnixSocket(socketPath);
4141
}
4242

43-
throw new Error('Invalid socket object');
43+
throw new Error('Invalid socket object. Only unix domain sockets and Inet sockets are allowed');
4444

4545
}
4646

47-
public static fromUnixSocket(path: string) {
47+
public static async fromUnixSocket(path: string) {
4848
// Return Error if invoked from windows
4949
if (process.platform == 'win32') {
5050
throw new Error('Unix sockets are not supported on windows');
5151
}
52-
return new JunoModule(new UnixSocketConnection(path), new JsonProtocol());
52+
if ( (await fs.lstat(path)).isSocket() ) {
53+
return new JunoModule(new UnixSocketConnection(path), new JsonProtocol());
54+
}
55+
56+
throw new Error('Invalid unix socket path');
5357
}
5458

55-
public static fromInetSocket(host: string, port: number) {
56-
return new JunoModule(new InetSocketConnection(host, port), new JsonProtocol());
59+
public static async fromInetSocket(host: string, port: number) {
60+
if (isIP(host) && !isNaN(Number(port))) {
61+
return new JunoModule(new InetSocketConnection(host, port), new JsonProtocol());
62+
}
63+
64+
throw new Error('Invalid Inet socket address. Use the format `{host}:{port}`')
5765
}
5866

5967
public async initialize(

0 commit comments

Comments
 (0)