For anyone who happens to have interest in this domain, what I wound up doing is getting the PID of the current d8 process, then reading the STDIN to d8 using dd.
#!/usr/bin/env -S /home/user/.jsvu/engines/v8/v8 --enable-os-system
// d8 Native Messaging host
// guest271314 7-7-2023, 6-20-2024
function encodeMessage(str) {
return new Uint8Array([...str].map((s) => s.codePointAt()));
}
function getMessage(pid) {
try {
// readline() doesn't read past the Uint32Array equivalent message length
// V8 authors are not interested in reading STDIN to an ArrayBuffer in d8
//
https://groups.google.com/g/v8-users/c/NsnStT6bx3Y/m/Yr_Z1FwgAQAJ // Use dd to get message length and return message from Bash script
const stdin = (os.system("./getNativeMessage.sh", [pid])).trim();
return encodeMessage(stdin);
} catch (e) {
writeFile("getMessageError.txt", encodeMessage(e.message));
}
}
function sendMessage(message) {
const header = new Uint32Array([message.length]);
writeFile("/proc/self/fd/1", header);
writeFile("/proc/self/fd/1", message);
}
function main() {
// Get PID of current process
const pid = (os.system("pgrep", ["-n", "-f", os.d8Path])).replace(/\D+/g, "");
while (true) {
const message = getMessage(pid);
sendMessage(message);
}
}
try {
main();
} catch (e) {
writeFile("mainError.txt", encodeMessage(e.message));
quit();
}
#!/bin/bash
# Bash Native Messaging host
# Read STDIN for V8's d8 shell, return message to d8
# guest271314 2024
set -x
set -o posix
getNativeMessage() {
#
https://lists.gnu.org/archive/html/help-bash/2023-06/msg00036.html length=$(dd iflag=fullblock bs=4 count=1 if=/proc/$@/fd/0 | od -An -td4)
message=$(dd iflag=fullblock bs=$((length)) count=1 if=/proc/$@/fd/0)
echo "$message"
}
getNativeMessage "$1"
in an MV3 browser extension ServiceWorker (background.js); or via "web_accessible_resources" and "externally_connectable" from any arbitrary Web page; or an offscreen document