Skip to content

Commit

Permalink
refactor: add RunnerEvalContext
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 14, 2024
1 parent e6853ff commit 3e13bae
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/workerd-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ async function main() {
const entry = "virtual:repl/" + encodeURI(entrySource);
await devEnv.api.eval(
entry,
async function (this: any, mod: any) {
const result = await mod.default({ env: this.env });
async function (ctx: any, mod: any) {
const result = await mod.default(ctx);
if (typeof result !== "undefined") {
console.log(result);
}
Expand Down
7 changes: 4 additions & 3 deletions packages/workerd/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
setRunnerFetchOptions,
type RunnerEvalOptions,
RUNNER_EVAL_PATH,
type RunnerEvalContext,
} from "./shared";
import {
DevEnvironment,
Expand Down Expand Up @@ -192,8 +193,8 @@ export async function createWorkerdDevEnvironment(
// (de)serialization can be customized (currently JSON.stringify/parse)
async eval<T>(
entry: string,
fn: (mod: unknown, ...args: unknown[]) => T,
...args: unknown[]
fn: (ctx: RunnerEvalContext, mod: any, ...args: any[]) => T,
...args: any[]
): Promise<Awaited<T>> {
const res = await runnerObject.fetch(ANY_URL + RUNNER_EVAL_PATH, {
method: "POST",
Expand All @@ -210,7 +211,7 @@ export async function createWorkerdDevEnvironment(
// not-so-magical proxy
// - proxy only shallow exports using above `eval`
// - all async call
async importProxy(entry: string): Promise<unknown> {
async importProxy(entry: string): Promise<any> {
return new Proxy(
{},
{
Expand Down
6 changes: 6 additions & 0 deletions packages/workerd/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { tinyassert } from "@hiogawa/utils";
import type { ModuleRunner } from "vite/module-runner";

export const RUNNER_INIT_PATH = "/__viteInit";
export const RUNNER_EVAL_PATH = "/__viteEval";
Expand Down Expand Up @@ -39,3 +40,8 @@ export type RunnerEvalOptions = {
fnString: string;
args: unknown[];
};

export type RunnerEvalContext = {
env: any;
runner: ModuleRunner;
};
4 changes: 3 additions & 1 deletion packages/workerd/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type RunnerEnv,
RUNNER_EVAL_PATH,
type RunnerEvalOptions,
type RunnerEvalContext,
} from "./shared";
import { ModuleRunner } from "vite/module-runner";

Expand Down Expand Up @@ -46,7 +47,8 @@ export class RunnerObject implements DurableObject {
const options = await request.json<RunnerEvalOptions>();
const fn = this.#env.__viteUnsafeEval.eval(`() => ${options.fnString}`)();
const mod = await this.#runner.import(options.entry);
const result = await fn.apply({ env: this.#env }, [mod, ...options.args]);
const ctx: RunnerEvalContext = { env: this.#env, runner: this.#runner };
const result = await fn(ctx, mod, ...options.args);
return new Response(JSON.stringify(result ?? null));
}

Expand Down

0 comments on commit 3e13bae

Please sign in to comment.