Skip to content

Commit

Permalink
feat(workerd): use new transport
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 27, 2024
1 parent 580f54d commit bacd465
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 51 deletions.
45 changes: 12 additions & 33 deletions packages/workerd/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,9 @@ export async function createWorkerdDevEnvironment(
},
unsafeEvalBinding: "__viteUnsafeEval",
serviceBindings: {
__viteFetchModule: async (request) => {
const args = await request.json();
try {
const result = await devEnv.fetchModule(...(args as [any, any]));
return MiniflareResponse.json(result);
} catch (error) {
console.error("[fetchModule]", args, error);
throw error;
}
},
__viteRunnerSend: async (request) => {
const payload = (await request.json()) as HotPayload;
hotListener.dispatch(payload);
hotListener.dispatch(payload, { send: runnerObject.__viteServerSend });
return MiniflareResponse.json(null);
},
},
Expand Down Expand Up @@ -176,10 +166,7 @@ export async function createWorkerdDevEnvironment(
close: () => {},
on: hotListener.on,
off: hotListener.off,
send(...args: any[]) {
const payload = normalizeServerSendPayload(...args);
runnerObject.__viteServerSend(payload);
},
send: runnerObject.__viteServerSend,
};

// TODO: move initialization code to `init`?
Expand All @@ -191,7 +178,10 @@ export async function createWorkerdDevEnvironment(
}
}

const devEnv = new WorkerdDevEnvironmentImpl(name, config, { hot });
const devEnv = new WorkerdDevEnvironmentImpl(name, config, {
transport: hot,
hot: true,
});

// custom environment api
const api: WorkerdDevApi = {
Expand Down Expand Up @@ -223,7 +213,10 @@ export async function createWorkerdDevEnvironment(

// wrapper to simplify listener management
function createHotListenerManager(): Pick<HotChannel, "on" | "off"> & {
dispatch: (payload: HotPayload) => void;
dispatch: (
payload: HotPayload,
client: { send: (payload: HotPayload) => void },
) => void;
} {
const listerMap: Record<string, Set<Function>> = {};
const getListerMap = (e: string) => (listerMap[e] ??= new Set());
Expand All @@ -235,26 +228,12 @@ function createHotListenerManager(): Pick<HotChannel, "on" | "off"> & {
off(event, listener: any) {
getListerMap(event).delete(listener);
},
dispatch(payload) {
dispatch(payload, client) {
if (payload.type === "custom") {
for (const lister of getListerMap(payload.event)) {
lister(payload.data);
lister(payload.data, client, payload.invoke);
}
}
},
};
}

function normalizeServerSendPayload(...args: any[]) {
let payload: HotPayload;
if (typeof args[0] === "string") {
payload = {
type: "custom",
event: args[0],
data: args[1],
};
} else {
payload = args[0];
}
return payload;
}
25 changes: 7 additions & 18 deletions packages/workerd/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,17 @@ export class RunnerObject extends DurableObject implements RunnerRpc {
root: env.__viteRoot,
sourcemapInterceptor: "prepareStackTrace",
transport: {
fetchModule: async (...args) => {
const response = await env.__viteFetchModule.fetch(
requestJson(args),
connect: async (handlers) => {
this.#viteServerSendHandler = handlers.onMessage;
},
send: async (payload) => {
const response = await env.__viteRunnerSend.fetch(
requestJson(payload),
);
tinyassert(response.ok);
return response.json();
},
},
hmr: {
connection: {
isReady: () => true,
onUpdate: (callback) => {
this.#viteServerSendHandler = callback;
},
send: async (payload) => {
const response = await env.__viteRunnerSend.fetch(
requestJson(payload),
);
tinyassert(response.ok);
},
},
},
hmr: true,
},
{
runInlinedModule: async (context, transformed) => {
Expand Down

0 comments on commit bacd465

Please sign in to comment.