Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nm_nodejs.js does not work on linux with firefox #3

Open
epragalakis opened this issue Oct 28, 2024 · 11 comments
Open

nm_nodejs.js does not work on linux with firefox #3

epragalakis opened this issue Oct 28, 2024 · 11 comments

Comments

@epragalakis
Copy link

I posted a similar issue on: mdn/content#36513

although the examples differ from each other.

If this is under active maintenance and you are interested in it I can post more debug info (and also a walthrough of my attempts) later.

@epragalakis epragalakis changed the title nm_nodejs.js does not work on linux nm_nodejs.js does not work on linux with firefox Oct 28, 2024
@guest271314
Copy link
Owner

First make sure the file is marked executable.

Then remove this

process.env.UV_THREADPOOL_SIZE = 1;

If you have deno available you can test the nm_nodejs.js outside of a Native Messaging host context with the code here #2 (comment).

Test on Chromium or Chrome to make sure the host works there.

If that doesn't work try using this https://github.com/guest271314/NativeMessagingHosts/blob/main/nm_host.js, which works using node, deno, and/or bun .

@guest271314
Copy link
Owner

What versions of Firefox and Node.js are you using?

Can you kindly create a gist including all of the code you are using?

BTW, I updated nm_nodejs.js here https://github.com/guest271314/native-messaging-nodejs/blob/main/nm_nodejs.js, though the code on MDN should still work.

@Josh-Cena One of your team mates over in MDN world contacted me by email, we exchanged idea, and they saw fit to ban me from MDN, so if we need to coordinate to publish working code - if we conclude it's the code and not something else that's not working - kindly visit this issue so we have working code published on MDN. Thanks.

@epragalakis
Copy link
Author

node 20.17
fedora 40 (sway spin)

BTW, I updated nm_nodejs.js here https://github.com/guest271314/native-messaging-nodejs/blob/main/nm_nodejs.js,

it worked!! (although I dont understand why and also the code looks like its doing way more than using the stdin and stdout as the docs mention)

I will post a follow up message with more debug info on the MDN example when I get some time in the coming days

@guest271314
Copy link
Owner

@epragalakis

nm_nodejs.js does not work on linux with firefox #3

Cannot reproduce.

Works for me on Linux 5.15.0-43-generic x86_64 x86_64 x86_64 GNU/Linux.

manifest.json

{
  "name": "tab",
  "version": "1.0",
  "manifest_version": 3,
  "chrome_url_overrides": {
    "newtab": "./tab.html"
  },
  "browser_specific_settings": {
    "gecko": {
      "id": "newtab@guest271314"
    }
  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": ["nativeMessaging"]
}

nm_nodejs.mjs

#!/usr/bin/env -S /home/user/bin/node

import fs from "node:fs/promises";

async function getMessage() {
  const header = new Uint32Array(1);
  await readFullAsync(1, header);
  const message = await readFullAsync(header[0]);
  return message;
}

async function readFullAsync(length, buffer = new Uint8Array(65536)) {
  const data = [];
  while (data.length < length) {
    const input = await fs.open("/dev/stdin");
    const { bytesRead } = await input.read({ buffer });
    await input.close();
    if (bytesRead === 0) {
      break;
    }
    data.push(...buffer.subarray(0, bytesRead));
  }
  return new Uint8Array(data);
}

async function sendMessage(message) {
  const header = new Uint32Array([message.length]);
  const stdout = await fs.open(`/proc/${process.pid}/fd/1`, "w");
  await stdout.write(header);
  await stdout.write(message);
  await stdout.close();
}

while (true) {
  try {
    const message = await getMessage();
    await sendMessage(message);
  } catch (e) {
    console.error(e);
    process.exit(1);
  }
}

From App side.

node version v24.0.0-nightly202410257ddd2c2282.

Make sure the file is executable.

chmod +x nm_nodejs.mjs

nm_nodejs.json

{
  "name": "nm_nodejs",
  "description": "Node.js Native Messaging host",
  "path": "/home/xubuntu/tab-firefox/nm_nodejs.mjs",
  "type": "stdio",
  "allowed_extensions": [ "newtab@guest271314" ]
}

nm_nodejs.json is written to the local filesystem at ~/.mozilla/native-messaging-hosts/nm_nodejs.json, see Manifest location at hLinux.

Firefox Nightly 133.0a1 (2024-10-27) (64-bit).

Code run in DevTools on the Wex extension page

globalThis.port = browser.runtime.connectNative("nm_nodejs");
port.onMessage.addListener((message) => console.log(message));
port.onDisconnect.addListener((p) => console.log(chrome.runtime.lastError));
port.postMessage(new Array(209715));

Message from host

Array(209715) [ null, null, null, null, null, null, null, null, null, null, … ]

Firefox Nightly screenshot

Screenshot_2024-10-28_17-39-58

Task Manager screen shot

Screenshot_2024-10-28_17-55-39

@guest271314
Copy link
Owner

@Josh-Cena Cannot reproduce the code on MDN not working. See above. Need more details.

@Josh-Cena
Copy link

I know nothing about this area so sorry I can't provide any help in debugging. @guest271314 fully appreciate your effort here. I'm sorry to hear about your situation with MDN, but if someone else has made that decision it's beyond my capability to revert it. I'm happy to credit you on any fix we may reach for the MDN example, though.

@guest271314
Copy link
Owner

@Josh-Cena I wasn't asking for anything re MDN. I'm just notifying you I'm banned from over there. Any help I can provide from here for Native Messaging that helps the documentation and working code examples I will.

This script, nm_standalone_test.js (nm_standalone_test.js.tar.gz), here How to test the different hosts #2 tests Native Messaging hosts, outside of the browser. So there's really no getting around the question of the protocol and programming language or runtime not working as expected, leaving user or architecture as causes fof issues. C, C++, Rust, WASM, Python, JavaScript (QuickJS, txiki.js, Node.js, Deno, Bun) should all read and write the same output back to the parent process, again, on the commandline. Thanks.

@guest271314
Copy link
Owner

@epragalakis Re

hey @Josh-Cena
long shot, but do you remember on why you had to do the above (lines 266-271)
instead of doing a console.log or process.stdout.write ?

console.log() doesn't work in JavaScript runtimes for Native Messaging hosts to send a message back to the browser.

@guest271314
Copy link
Owner

@epragalakis The reason that console lof doesn't work for sending messages from the runtime to the browser is 1. ECMA-262 doesn't specify I/O for "JavaScript", therefore console is not part of ECMA-262, and the implementation thereof, if implemented at all (could be print() in a given JavaScript runtime; I/O might not be possible, e.g., SerentityOS's LibJS js), and console is, in general, expected to be a TTY; the browser is not a TTY.

@guest271314
Copy link
Owner

@Josh-Cena AN independent Native Messaging hacker verified this https://github.com/guest271314/native-messaging-nodejs/blob/main/nm_nodejs.js works on Apple devices, using node version 18. I'm running node version 24.

Would you mind updating the example on MDN to use the above code?

@guest271314
Copy link
Owner

@Josh-Cena Re mdn/content#36626 (comment)

Could you make minimal changes required to make it work, rather than copying entire source code over?

I don't understand. Don't you want code that works on Linux, Microsoft products, and Apple products on MDN.

The PR changes one part of one file. What's the blocker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants