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

chore: remove eval api #142

Merged
merged 1 commit into from
Oct 27, 2024
Merged
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
34 changes: 1 addition & 33 deletions packages/workerd/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ import {
type ResolvedConfig,
} from "vite";
import type { SourcelessWorkerOptions } from "wrangler";
import {
ANY_URL,
type EvalApi,
type EvalMetadata,
type FetchMetadata,
RUNNER_EVAL_PATH,
RUNNER_INIT_PATH,
} from "./shared";
import { ANY_URL, type FetchMetadata, RUNNER_INIT_PATH } from "./shared";

interface WorkerdPluginOptions extends WorkerdEnvironmentOptions {
entry?: string;
Expand Down Expand Up @@ -84,7 +77,6 @@ export type WorkerdDevEnvironment = DevEnvironment & {

type WorkerdDevApi = {
dispatchFetch: (entry: string, request: Request) => Promise<Response>;
eval: EvalApi;
};

export async function createWorkerdDevEnvironment(
Expand Down Expand Up @@ -210,30 +202,6 @@ export async function createWorkerdDevEnvironment(
headers: res.headers,
});
},

// playwright-like eval interface https://playwright.dev/docs/evaluating
eval: async (ctx) => {
const headers = new Headers();
headers.set(
"x-vite-eval",
JSON.stringify({
entry: ctx.entry,
fnString: ctx.fn.toString(),
} satisfies EvalMetadata),
);
const body = JSON.stringify(ctx.data ?? (null as any));
const fetch_ = runnerObject.fetch as any as typeof fetch; // fix web/undici types
const response = await fetch_(ANY_URL + RUNNER_EVAL_PATH, {
method: "POST",
headers,
body,
// @ts-ignore undici
duplex: "half",
});
tinyassert(response.ok);
const result = await response.json();
return result as any;
},
};

return Object.assign(devEnv, { api }) as WorkerdDevEnvironment;
Expand Down
21 changes: 0 additions & 21 deletions packages/workerd/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type { ModuleRunner } from "vite/module-runner";

export const RUNNER_INIT_PATH = "/__viteInit";
export const RUNNER_EVAL_PATH = "/__viteEval";
export const ANY_URL = "https://any.local";

export type RunnerEnv = {
Expand All @@ -18,21 +15,3 @@ export type RunnerEnv = {
export type FetchMetadata = {
entry: string;
};

export type EvalFn<In = any, Out = any> = (ctx: {
mod: any;
data?: In;
env: any;
runner: ModuleRunner;
}) => Promise<Out> | Out;

export type EvalApi = <In = any, Out = any>(request: {
entry: string;
data?: In;
fn: EvalFn<In, Out>;
}) => Promise<Awaited<Out>>;

export type EvalMetadata = {
entry: string;
fnString: string;
};
19 changes: 0 additions & 19 deletions packages/workerd/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import {
} from "vite/module-runner";
import {
ANY_URL,
type EvalFn,
type EvalMetadata,
type FetchMetadata,
RUNNER_EVAL_PATH,
RUNNER_INIT_PATH,
type RunnerEnv,
} from "./shared";
Expand Down Expand Up @@ -46,22 +43,6 @@ export class RunnerObject implements DurableObject {
return new Response(null, { status: 101, webSocket: pair[1] });
}

if (url.pathname === RUNNER_EVAL_PATH) {
tinyassert(this.#runner);
const meta = JSON.parse(
request.headers.get("x-vite-eval")!,
) as EvalMetadata;
const mod = await this.#runner.import(meta.entry);
const data = await request.json();
const env = objectPickBy(this.#env, (_v, k) => !k.startsWith("__vite"));
const fn: EvalFn = this.#env.__viteUnsafeEval.eval(
`() => ${meta.fnString}`,
)();
const result = await fn({ mod, data, env, runner: this.#runner });
const body = JSON.stringify(result ?? null);
return new Response(body);
}

tinyassert(this.#runner);
const options = JSON.parse(
request.headers.get("x-vite-fetch")!,
Expand Down