native-messaging-nodejs 0.0.1 → 0.0.3

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/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ Node.js Native Messaging host
2
+
3
+ Installation and usage on Chrome and Chromium
4
+
5
+ 1. Navigate to `chrome://extensions`.
6
+ 2. Toggle `Developer mode`.
7
+ 3. Click `Load unpacked`.
8
+ 4. Select `native-messaging-nodejs` folder.
9
+ 5. Note the generated extension ID.
10
+ 6. Open `nm_nodejs.json` in a text editor, set `"path"` to absolute path of `nm_nodejs.js` and `chrome-extension://<ID>/` using ID from 5 in `"allowed_origins"` array.
11
+ 7. Copy the file to Chrome or Chromium configuration folder, e.g., Chromium on \*nix `~/.config/chromium/NativeMessagingHosts`; Chrome dev channel on \*nix `~/.config/google-chrome-unstable/NativeMessagingHosts`.
12
+ 8. Make sure `node` executable and `nm_nodejs.js` are executable.
13
+ 9. To test click `service worker` link in panel of unpacked extension which is DevTools for `background.js` in MV3 `ServiceWorker`, observe echo'ed message from Node.js Native Messaging host. To disconnect run `port.disconnect()`.
14
+
15
+ The Native Messaging host echoes back the message passed.
16
+
17
+ For differences between OS and browser implementations see [Chrome incompatibilities](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#native_messaging).
18
+
19
+ # License
20
+ Do What the Fuck You Want to Public License [WTFPLv2](http://www.wtfpl.net/about/)
package/nm_nodejs.js ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env -S UV_THREADPOOL_SIZE=1 /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.1",
3
+ "version": "0.0.3",
4
4
  "description": "Node.js Native Messaging host",
5
5
  "keywords": [
6
6
  "Native Messaging",
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "git+https://github.com/guest271314/native-messaging-nodejs.git"
15
+ "url": "git+https://38112114github.com/guest271314/native-messaging-nodejs.git"
16
16
  },
17
17
  "license": "SEE LICENSE IN LICENSE",
18
18
  "author": "guest271314",