Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remote module runner proxy #22

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/remote-runner/README.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions packages/remote-runner/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
76 changes: 76 additions & 0 deletions packages/remote-runner/src/client.ts
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions packages/remote-runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./client";
export * from "./server";
23 changes: 23 additions & 0 deletions packages/remote-runner/src/server.ts
Original file line number Diff line number Diff line change
@@ -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<number, unknown>();
heapInv = new WeakMap<object, number>();

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;
}
}
1 change: 1 addition & 0 deletions packages/remote-runner/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type RemoteProxy<T> = T;
14 changes: 14 additions & 0 deletions packages/remote-runner/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
8 changes: 8 additions & 0 deletions packages/remote-runner/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
format: ["esm"],
platform: "browser",
dts: true,
});
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.