native-messaging icon indicating copy to clipboard operation
native-messaging copied to clipboard

Does not send messages from host to browser equal or to 65536 length

Open guest271314 opened this issue 2 years ago • 2 comments

Modify the following:

example.js

function handleMessage (req) {
  // if (req.message === 'ping') {
    sendMessage(req)
    // {message: 'pong', body: 'hello from nodejs app'}
  // }
}

background.js

port.postMessage(new Array(13107))

function handleMessage (req) {
  // if (req.message === 'pong') {
  //  console.log(req)
  // }
  console.log(req);
}

Notice the message is not sent to the browser.

See

  • https://github.com/nodejs/node/issues/6456
  • https://github.com/nodejs/node/issues/11568#issuecomment-282765300
  • https://github.com/denoland/deno/discussions/17236#discussioncomment-4566134

guest271314 avatar May 12 '23 06:05 guest271314

I'm not sure how to fix this in protocol.js. process.stdout._handle.setBlocking(true) alone doesn't change anything. I wound up writing this https://github.com/guest271314/native-messaging-nodejs/blob/main/nm_nodejs.mjs which does send the expected 1MB (new Array(209715) sent from the client) message back to the browser.

guest271314 avatar May 18 '23 03:05 guest271314

https://github.com/guest271314/native-messaging/commit/8e99d2a345ae94426a502d05aa5d57b966f6bc78 fixes sending 1 MB to client.

Note, the fix applies to when only 1 MB is sent to the host. Since 4 GB can be sent to the host additional measures will need to be taken to parse and build JSON greater than 1048576 length which can break JSON parsing if/when the JSON is attempted to be sent in the middle of an object or array.

We can fix the case of sending over 1 MB to the host and parsing that JSON as discrete 1 MB parts in a future PR.

guest271314 avatar Jun 04 '23 00:06 guest271314