A file system experiment I've thought about for a while. Somehow creating a FIFO or Unix Domain Socket that is a bi-directional connection with streaming capabilities between arbitrary Web pages and whatever program I launch locally - without using Native Messaging, which is IPC over a JSON-like format, at least on Chromium.
Reading this Principle of Unix Domain Socket. How does it work? I thinkit's kind of a mix of both.
The difference between FIFOs and Unix sockets, is that FIFO use file sys calls, while Unix sockets use socket calls.
Is this structure closure to a FIFO or Unix Domain Socket? Or something else? Not that I need a name or box for the communication channel. I just care that it does what I tell it to do. Just curious if the system fits into one of those standard Unix boxes? Something else...?
repl.js
for await (const e of Deno.watchFs("/home/user/Documents")) {
for (const [index, path] of Object.entries(e.paths)) {
if (path.split("/").pop() === "sock") {
const input = Deno.readTextFileSync(path);
console.log(e.kind, path);
if (input.length > 0) {
console.log(input);
const command = new Deno.Command(Deno.execPath(), {
args: [
"eval",
input,
],
});
const { code, stdout, stderr } = command.outputSync();
await Deno.stdout.write(stdout);
Deno.writeFileSync(path, stdout);
} else {
continue;
}
}
}
}
repl-sock.js
startIn: "documents",
suggestedName: "sock",
});
async function repl(script, fs) {
let readWrites = 0;
const { resolve, promise } = Promise.withResolvers();
const fso = new FileSystemObserver(async ([{
changedHandle,
root,
type,
}], record) => {
try {
if (++readWrites === 2) {
readWrites = 0;
const text = await (await changedHandle.getFile()).text();
fso.unobserve(fs);
fso.disconnect();
const currentHandle = await changedHandle.createWritable();
await currentHandle.truncate(0);
await currentHandle.close();
resolve(text);
}
} catch (e) {
console.log(e);
}
});
fso.observe(fs);
return new Response(script).body.pipeTo(await handle.createWritable())
.then(() => promise).then(console.log).then(() =>
console.log(`Done writing to and reading from ${handle.name}`)
).catch((e) => console.log(e));
}
Usage, from DevTools console
or Snippets on arbitrary Web page
repl(`console.log(Deno.version)`, handle).then(console.log)
// { deno: "2.2.6+b880087", v8: "13.5.212.4-rusty", typescript: "5.7.3" }
// Done writing to and reading from sock