-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathasync.js
More file actions
78 lines (68 loc) · 2.2 KB
/
async.js
File metadata and controls
78 lines (68 loc) · 2.2 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
const net = require('net');
const { Loot, SetErrorLanguageEN, SetLogLevel } = require('./build/Release/node-loot');
process.on('uncaughtException', error => {
console.error(error.message);
process.exit(1);
});
const CHUNK_SIZE = 32 * 1024;
let currentLogLevel = 2; // default: info (matches previous hardcoded filter)
const client = net.connect(`\\\\?\\pipe\\loot-ipc-${process.argv[2]}`, (arg) => {
let instance;
let dataBuffer = '';
function send(args) {
const message = JSON.stringify(args) + '\uFFFF';
// Chunk large messages to avoid Windows named pipe size limits
for (let i = 0; i < message.length; i += CHUNK_SIZE) {
client.write(message.slice(i, i + CHUNK_SIZE));
}
}
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 {
if (event.type === 'loadPlugins') {
SetLogLevel(4); // suppress BSA hash collision warnings during plugin loading
result = instance[event.type](...event.args);
SetLogLevel(currentLogLevel);
} 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 += buffer.toString();
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 });
});