Skip to content

Commit f5d6ca3

Browse files
feat #4: Error handling for socket path
1 parent 6a8a925 commit f5d6ca3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/juno-node.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as net from 'net';
2+
import * as fs from 'fs';
23
import { BaseProtocol } from './protocol/base-protocol';
34
import BaseConnection from './connection/base-connection';
45
import { JsonProtocol } from './protocol/json-protocol';
@@ -30,12 +31,17 @@ export default class JunoModule {
3031
}
3132

3233
public static async default(socketPath: string) {
33-
if (net.isIP(socketPath.split(':')[0])) {
34-
const [ host, port ] = socketPath.split(':');
34+
const [ host, port ] = socketPath.split(':');
35+
36+
if (net.isIP(host) && typeof Number(port) === 'number') {
3537
return this.fromInetSocket(host, Number(port));
36-
} else {
38+
}
39+
if ( (await fs.promises.lstat(socketPath)).isSocket() ) {
3740
return this.fromUnixSocket(socketPath);
3841
}
42+
43+
throw new Error('Invalid socket object');
44+
3945
}
4046

4147
public static fromUnixSocket(path: string) {

0 commit comments

Comments
 (0)