native-messaging-nodejs 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/background.js +0 -1
- package/deno.json +6 -0
- package/manifest.json +1 -1
- package/nm_nodejs.js +60 -0
- package/package.json +2 -2
package/background.js
CHANGED
package/deno.json
ADDED
package/manifest.json
CHANGED
package/nm_nodejs.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env -S /home/user/bin/node --optimize-for-size --zero-unused-memory --memory-saver-mode --double-string-cache-size=1 --experimental-flush-embedded-blob-icache --jitless --expose-gc --v8-pool-size=1
|
|
2
|
+
// Node.js Native Messaging host
|
|
3
|
+
// guest271314, 10-9-2022
|
|
4
|
+
|
|
5
|
+
// try {port.postMessage(Array((209715*65)))} catch (e) {console.log(e)}
|
|
6
|
+
// Error: Message exceeded maximum allowed size of 64MiB.
|
|
7
|
+
// import fs from "node:fs";
|
|
8
|
+
const maxMessageLengthFromHost = 209715;
|
|
9
|
+
let currentMessageLength = 0;
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
const decoder = new TextDecoder();
|
|
12
|
+
const data = Array();
|
|
13
|
+
// https://github.com/nodejs/node/issues/11568#issuecomment-282765300
|
|
14
|
+
process.stdout._handle.setBlocking(false);
|
|
15
|
+
|
|
16
|
+
for await (const nativeMessage of process.stdin) {
|
|
17
|
+
if (currentMessageLength === 0 && data.length === 0) {
|
|
18
|
+
const u8 = new Uint8Array(nativeMessage);
|
|
19
|
+
currentMessageLength = new DataView(u8.subarray(0, 4).buffer).getUint32(
|
|
20
|
+
0,
|
|
21
|
+
true,
|
|
22
|
+
);
|
|
23
|
+
// fs.writeFileSync("log.txt", `${currentMessageLength}`);
|
|
24
|
+
data.push(...u8.subarray(4));
|
|
25
|
+
} else {
|
|
26
|
+
if (data.length < currentMessageLength) {
|
|
27
|
+
const u8 = new Uint8Array(nativeMessage);
|
|
28
|
+
data.push(...u8);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (data.length && data.length === currentMessageLength) {
|
|
32
|
+
const json = JSON.parse(decoder.decode(new Uint8Array(data)));
|
|
33
|
+
if (Array.isArray(json) && json.length) {
|
|
34
|
+
for (let i = 0; i < json.length; i += maxMessageLengthFromHost) {
|
|
35
|
+
const message = encoder.encode(
|
|
36
|
+
JSON.stringify(json.slice(i, i + maxMessageLengthFromHost)),
|
|
37
|
+
);
|
|
38
|
+
process.stdout.write(new Uint32Array([message.length]));
|
|
39
|
+
process.stdout.write(message);
|
|
40
|
+
}
|
|
41
|
+
await new Promise((resolve) => {
|
|
42
|
+
process.stdout.once("drain", resolve);
|
|
43
|
+
});
|
|
44
|
+
currentMessageLength = 0;
|
|
45
|
+
data.length = 0;
|
|
46
|
+
gc();
|
|
47
|
+
continue;
|
|
48
|
+
} else {
|
|
49
|
+
const message = encoder.encode(
|
|
50
|
+
JSON.stringify(json),
|
|
51
|
+
);
|
|
52
|
+
process.stdout.write(new Uint32Array([message.length]));
|
|
53
|
+
process.stdout.write(message);
|
|
54
|
+
}
|
|
55
|
+
currentMessageLength = 0;
|
|
56
|
+
data.length = 0;
|
|
57
|
+
gc();
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "native-messaging-nodejs",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Node.js Native Messaging host",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Native Messaging",
|
|
@@ -18,4 +18,4 @@
|
|
|
18
18
|
"author": "guest271314",
|
|
19
19
|
"type": "esm",
|
|
20
20
|
"main": "nm_nodejs.js"
|
|
21
|
-
}
|
|
21
|
+
}
|