Wraps the three additional entry points where user server code runs outside fetchHandler's own context wrap: - the server-function RPC path (/__wrnexus/rpc) intercepted before handlers.fetch in the dev server (index.ts) - what server.fn() travels - the service RPC path (isRpcPath) inside fetchHandler, which runs implement()/implementStream() service code before ctx existed - the HMR-sync handler, which runs real load blocks/actions via dispatch() Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { getRequestContext } from "@wrnexus/core";
|
|
import { withServerFnRequestContext } from "../src/index.ts";
|
|
|
|
// The server-function RPC path ("/__wrnexus/rpc", what server.fn() calls from
|
|
// the browser) is intercepted before fetchHandler in the dev server and would
|
|
// otherwise run entirely outside the AsyncLocalStorage wrap. This is the
|
|
// property that was broken: a handler invoked through that path must see the
|
|
// request context, not `undefined`.
|
|
test("a handler invoked through the server-function RPC path can read the request context", async () => {
|
|
let seen: unknown;
|
|
const innerHandler = async (request: Request): Promise<Response> => {
|
|
seen = getRequestContext();
|
|
return new Response("ok");
|
|
};
|
|
|
|
const wrapped = withServerFnRequestContext(innerHandler);
|
|
const request = new Request("http://localhost/__wrnexus/rpc", { method: "POST" });
|
|
await wrapped(request);
|
|
|
|
expect(seen).toBeDefined();
|
|
expect((seen as { req: Request }).req).toBe(request);
|
|
});
|
|
|
|
test("there is no context outside the wrap", () => {
|
|
expect(getRequestContext()).toBeUndefined();
|
|
});
|