forked from bytesonus/juno-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix-socket-connection.ts
More file actions
41 lines (36 loc) · 856 Bytes
/
unix-socket-connection.ts
File metadata and controls
41 lines (36 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as net from 'net';
import BaseConnection from './base-connection';
export default class UnixSocketConnection extends BaseConnection {
client?: net.Socket;
sockPath: string;
constructor(sockPath: string) {
super();
this.sockPath = sockPath;
}
setupConnection(): Promise<void> {
return new Promise(resolve => {
this.client = net.createConnection(this.sockPath);
this.client.on('data', (data) => {
const dataLines = data.toString().split(/\r?\n/);
dataLines.map((data) => {
if (data) {
this.onData(Buffer.from(data))
}
});
});
this.client.on('connect', () => {
resolve();
});
});
}
async closeConnection() {
this.client?.destroy();
}
send(message: Buffer): Promise<void> {
return new Promise(resolve => {
this.client?.write(message, () => {
resolve();
});
});
}
}