Skip to content

Commit

Permalink
refactor(child-process): IPC via stdio
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 14, 2024
1 parent 6d6bd7c commit 53f816d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
32 changes: 31 additions & 1 deletion examples/child-process/src/lib/vite/bridge-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// @ts-check

import assert from "node:assert";
import fs from "node:fs";
import readline from "node:readline";
import { Writable } from "node:stream";
import { ESModulesEvaluator, ModuleRunner } from "vite/module-runner";

/**
Expand All @@ -22,12 +25,39 @@ export function createBridgeClient(options) {
return result;
}

// TODO: birpc
const childIn = readline.createInterface(process.stdin);
const childOut = new Writable({
write(chunk, _encoding, callback) {
fs.write(3, chunk, callback);
},
});

const runner = new ModuleRunner(
{
root: options.root,
sourcemapInterceptor: "prepareStackTrace",
transport: {
fetchModule: (...args) => rpc("fetchModule", ...args),
fetchModule: async (...args) => {
const id = Math.random().toString(36).slice(2);
const promise = new Promise((resolve, _reject) => {
/**
* @param {string} line
*/
function handler(line) {
const event = JSON.parse(line);
if (event.id === id) {
childIn.off("line", handler);
resolve(event.result);
}
}
childIn.on("line", handler);
});
childOut.write(
JSON.stringify({ type: "fetchModule", id, args }) + "\n",
);
return promise;
},
},
hmr: false,
},
Expand Down
14 changes: 12 additions & 2 deletions examples/child-process/src/lib/vite/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import childProcess from "node:child_process";
import http from "node:http";
import { join } from "node:path";
import readline from "node:readline";
import { Readable } from "node:stream";
import { Readable, Writable } from "node:stream";
import { webToNodeHandler } from "@hiogawa/utils-node";
import { DevEnvironment, type DevEnvironmentOptions } from "vite";
import type { BridgeClientOptions } from "./types";
Expand Down Expand Up @@ -101,11 +101,13 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
// 4th stdio to ease startup communication
// TODO: use 1st stdio to make bidirection?
// https://github.com/cloudflare/workers-sdk/blob/e5037b92ac13b1b8a94434e1f9bfa70d4abf791a/packages/miniflare/src/runtime/index.ts#L141
stdio: ["ignore", "inherit", "inherit", "pipe"],
stdio: ["pipe", "inherit", "inherit", "pipe"],
},
);
this.child = child;
assert(child.stdio[0] instanceof Writable);
assert(child.stdio[3] instanceof Readable);
const childIn = child.stdio[0];
this.childIO = readline.createInterface(child.stdio[3]);
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(
Expand All @@ -128,6 +130,14 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
}
});
});
this.childIO.on("line", async (line) => {
const event = JSON.parse(line);
if (event.type === "fetchModule") {
const { id, args } = event;
const result = await this.fetchModule(...(args as [any]));
childIn.write(JSON.stringify({ id, result }) + "\n");
}
});
console.log("[environment.init]", {
bridgeUrl: this.bridgeUrl,
childUrl: this.childUrl,
Expand Down

0 comments on commit 53f816d

Please sign in to comment.