-
Notifications
You must be signed in to change notification settings - Fork 0
/
nm_nodejs.js
52 lines (47 loc) · 1.38 KB
/
nm_nodejs.js
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
#!/usr/bin/env -S ./node --max-old-space-size=6 --jitless --expose-gc --v8-pool-size=1 --experimental-default-type=module
// Node.js Native Messaging host
// guest271314, 10-9-2022
import { open } from "node:fs/promises";
process.env.UV_THREADPOOL_SIZE = 1;
// https://github.com/denoland/deno/discussions/17236#discussioncomment-4566134
// https://github.com/saghul/txiki.js/blob/master/src/js/core/tjs/eval-stdin.js
async function readFullAsync(length, buffer = new Uint8Array(65536)) {
const data = [];
while (data.length < length) {
const input = await open("/dev/stdin");
let { bytesRead } = await input.read({
buffer
});
await input.close();
if (bytesRead === 0) {
break;
}
data.push(...buffer.subarray(0, bytesRead));
}
return new Uint8Array(data);
}
async function getMessage() {
const header = new Uint32Array(1);
await readFullAsync(1, header);
const content = await readFullAsync(header[0]);
return content;
}
async function sendMessage(message) {
const header = new Uint32Array([message.length]);
const stdout = await open("/proc/self/fd/1", "w");
await stdout.write(header);
await stdout.write(message);
await stdout.close();
global.gc();
}
async function main() {
while (true) {
try {
const message = await getMessage();
await sendMessage(message);
} catch (e) {
process.exit();
}
}
}
main();