|
1 |
| -import * as net from 'net'; |
2 |
| -import * as fs from 'fs'; |
| 1 | +import { isIP } from 'net'; |
| 2 | +import { promises as fs } from 'fs'; |
3 | 3 | import { BaseProtocol } from './protocol/base-protocol';
|
4 | 4 | import BaseConnection from './connection/base-connection';
|
5 | 5 | import { JsonProtocol } from './protocol/json-protocol';
|
@@ -33,27 +33,35 @@ export default class JunoModule {
|
33 | 33 | public static async default(socketPath: string) {
|
34 | 34 | const [ host, port ] = socketPath.split(':');
|
35 | 35 |
|
36 |
| - if (net.isIP(host) && typeof Number(port) === 'number') { |
| 36 | + if (isIP(host) && !isNaN(Number(port))) { |
37 | 37 | return this.fromInetSocket(host, Number(port));
|
38 | 38 | }
|
39 |
| - if ( (await fs.promises.lstat(socketPath)).isSocket() ) { |
| 39 | + if ( (await fs.lstat(socketPath)).isSocket() ) { |
40 | 40 | return this.fromUnixSocket(socketPath);
|
41 | 41 | }
|
42 | 42 |
|
43 |
| - throw new Error('Invalid socket object'); |
| 43 | + throw new Error('Invalid socket object. Only unix domain sockets and Inet sockets are allowed'); |
44 | 44 |
|
45 | 45 | }
|
46 | 46 |
|
47 |
| - public static fromUnixSocket(path: string) { |
| 47 | + public static async fromUnixSocket(path: string) { |
48 | 48 | // Return Error if invoked from windows
|
49 | 49 | if (process.platform == 'win32') {
|
50 | 50 | throw new Error('Unix sockets are not supported on windows');
|
51 | 51 | }
|
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'); |
53 | 57 | }
|
54 | 58 |
|
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}`') |
57 | 65 | }
|
58 | 66 |
|
59 | 67 | public async initialize(
|
|
0 commit comments