Repository navigation
Expand file tree
/
Copy pathasync.js
More file actions
79 lines (68 loc) · 2.3 KB
/
Copy pathasync.js
File metadata and controls
79 lines (68 loc) · 2.3 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const net = require('net');
const { StringDecoder } = require('string_decoder');
const { Loot, SetErrorLanguageEN, SetLogLevel } = require('./build/Release/node-loot');
let send = (args) => {};
process.on('uncaughtException', error => {
console.error(error.message);
// the caller is listening on the pipe, not on stderr, so the reason goes there too
try {
send({ error: error.message, extraArgs: JSON.stringify(error) });
} catch (err) {
// the pipe is the thing that just broke; stderr already has it
}
process.exit(1);
});
let currentLogLevel = 2; // default: info (matches previous hardcoded filter)
const client = net.connect(process.argv[2], (arg) => {
let instance;
let dataBuffer = '';
// a read can end mid-character; the decoder holds the incomplete sequence back until the
// rest arrives
const decoder = new StringDecoder('utf8');
send = (args) => client.write(JSON.stringify(args) + '\uFFFF');
function handleEvent(event) {
let result;
try {
if (event.type === 'init') {
SetErrorLanguageEN();
instance = new Loot(...event.args, logCallback);
} else if (event.type === 'setLogLevel') {
currentLogLevel = event.args[0];
SetLogLevel(event.args[0]);
} else if (event.type === 'terminate') {
send({});
process.exit(0);
} else {
result = instance[event.type](...event.args);
}
send({ result });
} catch (error) {
send({ error: error.message, extraArgs: JSON.stringify(error) });
}
}
function logCallback(level, message) {
if (level >= currentLogLevel) {
send({ log: { level, message } });
}
}
client.on('data', buffer => {
dataBuffer += decoder.write(buffer);
const messages = dataBuffer.split('\uFFFF');
// Keep incomplete chunk (last element after split if no trailing delimiter)
if (!dataBuffer.endsWith('\uFFFF')) {
dataBuffer = messages.pop();
} else {
dataBuffer = '';
}
// Process each complete message
for (const msg of messages) {
if (msg.length > 0) {
handleEvent(JSON.parse(msg));
}
}
});
// signal readiness to process messages
send({ result: null });
});
// the host is gone (closed or crashed), nobody can reach this worker anymore
client.on('close', () => process.exit(0));