forked from actualbudget/node-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-socket.js
More file actions
39 lines (32 loc) · 745 Bytes
/
get-socket.js
File metadata and controls
39 lines (32 loc) · 745 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
const net = require('net');
const os = require('os');
const { join } = require('path');
const ipc = require('node-ipc');
function connect(name) {
return new Promise((resolve, reject) => {
ipc.connectTo(name, () => {
ipc.of[name].on('error', () => {
ipc.disconnect(name);
resolve(false);
});
ipc.of[name].on('connect', () => {
resolve(ipc.of[name]);
});
});
});
}
async function getSocket(name) {
if (name) {
return connect(name);
}
let currentSocket = 1;
let client = null;
while (!(client = await connect('actual' + currentSocket))) {
currentSocket++;
if (currentSocket >= 10) {
return null;
}
}
return client;
}
module.exports = getSocket;