diff --git a/packages/remote-runner/README.md b/packages/remote-runner/README.md new file mode 100644 index 00000000..990abb85 --- /dev/null +++ b/packages/remote-runner/README.md @@ -0,0 +1,11 @@ +# remote-runner + +Simplified ["magic proxy"](https://github.com/cloudflare/miniflare/pull/639) +for Vite environment remote module runner? + +## todo + +- https://github.com/cloudflare/miniflare/pull/639 +- https://github.com/jacob-ebey/turbo-stream +- https://github.com/lxsmnsyc/seroval +- https://github.com/GoogleChromeLabs/comlink diff --git a/packages/remote-runner/package.json b/packages/remote-runner/package.json new file mode 100644 index 00000000..3d909d65 --- /dev/null +++ b/packages/remote-runner/package.json @@ -0,0 +1,18 @@ +{ + "name": "@hiogawa/remote-runner", + "private": true, + "type": "module", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + }, + "scripts": { + "dev": "tsup --watch", + "build": "tsup" + }, + "volta": { + "extends": "../../package.json" + } +} diff --git a/packages/remote-runner/src/client.ts b/packages/remote-runner/src/client.ts new file mode 100644 index 00000000..5ac5f93b --- /dev/null +++ b/packages/remote-runner/src/client.ts @@ -0,0 +1,76 @@ +// https://github.com/cloudflare/workers-sdk/blame/ad1d056cb2710b790ff5d4532e9f694e099c27e2/packages/miniflare/src/plugins/core/proxy/client.ts + +// proxy only get/apply? +// everything async + +// special names +// global +// import +// primitives +// function + +// const client = new ProxyClient({ post }); +// const runnerProxy = client.getRootProxy(); +// const mod = await (await runnerProxy.import)("/src/entry"); +// const res = await (await mod.default)(req); + +// [with sync getter] +// const runnerProxy = client.getRootProxy(); +// const mod = await runnerProxy.import("/src/entry"); +// const res = await mod.default(req); + +// const server = new ProxyServer({ on, root: { runner } }); + +export type ProxyTarget = { + address: number; +}; + +// direct value to return +export type ProxyResult = + | { + type: "value"; + value: unknown; + } + | { + type: "proxy"; + address: number; + }; + +export class ProxyClient { + constructor() {} + + async invokeCall(t: ProxyTarget, args: unknown[]) { + t; + args; + } + + // sync fetch...? + async invokeGet(t: ProxyTarget, p: keyof any) { + t; + p; + } + + createProxy(t: ProxyTarget) { + const client = this; + return new Proxy(() => {}, { + apply(_target, _thisArg, argArray) { + return client.invokeCall(t, argArray); + }, + get(_target, p, _receiver) { + return client.invokeGet(t, p); + }, + }); + } +} + +// puppeteer/playwright style "handle" + "eval" system? +// https://playwright.dev/docs/evaluating + +// const runner = createRemoteRunner(); +// const modHandle = await runner.import("/some-entry.ts"); +// const result = await runner.eval((mod) => mod.default("hello"), [modHandle]); + +// const result = await runner.import("/some-entry.ts", (mod, args) => mod.default(...args), ["hello"]); + +// const result = await remoteRunner.$eval("/some-module.ts", (mod, args) => ..., ["hello"]); +// const moduleHandle = remoteRunner. diff --git a/packages/remote-runner/src/index.ts b/packages/remote-runner/src/index.ts new file mode 100644 index 00000000..cc576d71 --- /dev/null +++ b/packages/remote-runner/src/index.ts @@ -0,0 +1,2 @@ +export * from "./client"; +export * from "./server"; diff --git a/packages/remote-runner/src/server.ts b/packages/remote-runner/src/server.ts new file mode 100644 index 00000000..d6c48392 --- /dev/null +++ b/packages/remote-runner/src/server.ts @@ -0,0 +1,23 @@ +// https://github.com/cloudflare/workers-sdk/blame/ad1d056cb2710b790ff5d4532e9f694e099c27e2/packages/miniflare/src/workers/core/proxy.worker.ts# + +import type { ProxyTarget } from "./client"; + +export class ProxyServer { + heap = new Map(); + heapInv = new WeakMap(); + + constructor() {} + + async handleCall(t: ProxyTarget, args: unknown[]) { + t; + args; + t.address; + this.heap.get(t.address); + } + + async handleGet(t: ProxyTarget, p: keyof any) { + this.heap.get(t.address); + t; + p; + } +} diff --git a/packages/remote-runner/src/types.ts b/packages/remote-runner/src/types.ts new file mode 100644 index 00000000..92e8002e --- /dev/null +++ b/packages/remote-runner/src/types.ts @@ -0,0 +1 @@ +export type RemoteProxy = T; diff --git a/packages/remote-runner/tsconfig.json b/packages/remote-runner/tsconfig.json new file mode 100644 index 00000000..ee568703 --- /dev/null +++ b/packages/remote-runner/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@tsconfig/strictest/tsconfig.json", + "include": ["src"], + "compilerOptions": { + "checkJs": false, + "exactOptionalPropertyTypes": false, + "verbatimModuleSyntax": true, + "noEmit": true, + "moduleResolution": "Bundler", + "module": "ESNext", + "target": "ESNext", + "lib": ["ESNext", "DOM", "DOM.Iterable"] + } +} diff --git a/packages/remote-runner/tsup.config.ts b/packages/remote-runner/tsup.config.ts new file mode 100644 index 00000000..ace7083a --- /dev/null +++ b/packages/remote-runner/tsup.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "tsup"; + +export default defineConfig({ + entry: ["src/index.ts"], + format: ["esm"], + platform: "browser", + dts: true, +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 700f2d1f..9fffd828 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -127,6 +127,8 @@ importers: specifier: 18.2.22 version: 18.2.22 + examples/remote-runner: {} + packages/workerd: dependencies: miniflare: