Skip to content

Commit 6a8a925

Browse files
feat #4: TCP sockets support added
1 parent 8f093ee commit 6a8a925

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as net from 'net';
2+
import BaseConnection from './base-connection';
3+
4+
export default class InetSocketConnection extends BaseConnection {
5+
client?: net.Socket;
6+
host: string;
7+
port: number;
8+
9+
constructor(host: string, port: number) {
10+
super();
11+
this.host = host;
12+
this.port = port;
13+
}
14+
15+
setupConnection(): Promise<void> {
16+
return new Promise(resolve => {
17+
this.client = net.createConnection(this.port, this.host);
18+
this.client.on('data', (data) => {
19+
const dataLines = data.toString().split(/\r?\n/);
20+
dataLines.map((data) => {
21+
if (data) {
22+
this.onData(Buffer.from(data))
23+
}
24+
});
25+
});
26+
this.client.on('connect', () => {
27+
resolve();
28+
});
29+
});
30+
}
31+
32+
async closeConnection() {
33+
this.client?.destroy();
34+
}
35+
36+
send(message: Buffer): Promise<void> {
37+
return new Promise(resolve => {
38+
this.client?.write(message, () => {
39+
resolve();
40+
});
41+
});
42+
}
43+
}

src/connection/unix-socket-connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as net from 'net';
22
import BaseConnection from './base-connection';
33

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

src/juno-node.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as net from 'net';
12
import { BaseProtocol } from './protocol/base-protocol';
23
import BaseConnection from './connection/base-connection';
34
import { JsonProtocol } from './protocol/json-protocol';
@@ -8,7 +9,8 @@ import {
89
TriggerHookRequest,
910
JunoMessage
1011
} from './models/messages';
11-
import SocketConnection from './connection/unix-socket-connection';
12+
import UnixSocketConnection from './connection/unix-socket-connection';
13+
import InetSocketConnection from './connection/inet-socket-connection';
1214

1315
export default class JunoModule {
1416

@@ -27,8 +29,25 @@ export default class JunoModule {
2729
// this.connection.setOnDataListener(this.onDataHandler);
2830
}
2931

30-
public static default(socketPath: string): JunoModule {
31-
return new JunoModule(new SocketConnection(socketPath), new JsonProtocol());
32+
public static async default(socketPath: string) {
33+
if (net.isIP(socketPath.split(':')[0])) {
34+
const [ host, port ] = socketPath.split(':');
35+
return this.fromInetSocket(host, Number(port));
36+
} else {
37+
return this.fromUnixSocket(socketPath);
38+
}
39+
}
40+
41+
public static fromUnixSocket(path: string) {
42+
// Return Error if invoked from windows
43+
if (process.platform == 'win32') {
44+
throw new Error('Unix sockets are not supported on windows');
45+
}
46+
return new JunoModule(new UnixSocketConnection(path), new JsonProtocol());
47+
}
48+
49+
public static fromInetSocket(host: string, port: number) {
50+
return new JunoModule(new InetSocketConnection(host, port), new JsonProtocol());
3251
}
3352

3453
public async initialize(

0 commit comments

Comments
 (0)