d8 Native Messaging host

20 views
Skip to first unread message

guest271314

unread,
Oct 14, 2022, 2:43:41 AM10/14/22
to v8-dev
I'm trying to use d8 as a Native Messaging host, using the same or similar algorithm I do for QuickJS, Deno, and Node.js hosts.

d8 is getting stuck somewhere in the process. I suspect trying to read and write Uint32Array from stdin with readline() and write Uint32Array with write().

Is there any way to make this work with d8 alone?

#!d8
function getMessage() {
  const input = readline();
  const uint8 = new Uint8Array(
      [...input].map((s) =>
        s.charCodeAt()
      )
    );
  const view = new DataView(uint8.buffer);
  const length = view.getUint32(0, true);
  const content = new Uint8Array(length);
  content.set(input.subarray(4));
  return uint8.subarray();
}

function sendMessage(json) {

  let header = Uint32Array.from(
    {
      length: 4,
    },
    (_, index) => (json.length >> (index * 8)) & 0xff
  );
  let output = new Uint8Array(header.length + json.length);
  output.set(header, 0);
  output.set(json, 4);
  write(output);
}

function main() {
  while (true) {
    try {
      const message = getMessage();
      sendMessage(message);
      break;
    } catch (e) {
      quit();
    }
  }
}

main();
Reply all
Reply to author
Forward
0 new messages