UNPKG

histoire

Version:

A fast story book powered by Vite

103 lines • 5.19 kB
import type { ViteDevServer, Plugin as VitePlugin } from 'vite'; /** * Dev-server path the browser runtimes POST their mock-resolution requests to. * * Why HTTP and not a Vite HMR event (which is what both `@vitest/mocker`'s own * `vitest:mocks:*` events and Histoire's first attempt used): Vite's HMR client * funnels **every** server → client payload through one strictly serial queue * (`createHMRHandler`), and handling an `update` payload is awaited until every * module that update re-executes has settled. A story calling `vi.mock` is * re-executed by exactly such an update and needs a resolution from the server, * so a reply travelling as an HMR payload queued behind the very update that * was waiting for it. Measured against a real dev server: the server answered * in 1 ms, the browser only processed the reply 5000 ms later — one millisecond * after the RPC timeout fired and unblocked the update. An HTTP round trip is * answered on its own connection and cannot be blocked by HMR processing. * * HTTP also makes correlation structural: a response can only ever settle the * request it answers, so replies cannot leak between the several mocker-enabled * browsing contexts sharing one dev server (preview iframe, controls sandbox * iframe, "open in new tab" sandbox, extra app tabs) — which is exactly what * `@vitest/mocker`'s uncorrelated `server.ws.send` broadcasts did. * * This suffix is mounted beneath the configured project base. configureServer * runs before Vite's base rewriting, so both browser URL and owned route must * include that base, including when a host owns several middleware projects. */ export declare const VITEST_MOCK_RPC_ENDPOINT = "/__histoire_vitest_mock_rpc"; /** RPC methods the browser runtimes may call, as sent in the request body. */ export declare const VITEST_MOCK_RPC_METHODS: { readonly resolveId: 'resolveId'; readonly resolveMock: 'resolveMock'; readonly invalidate: 'invalidate'; readonly awaitBarrier: 'awaitBarrier'; }; /** * `@vitest/mocker`'s own dev-server events registering what is mocked. Only its * plugin owns the registry they write to, so Histoire keeps sending them — * outbound HMR messages are never queued — and only replaces how the browser * waits for them to be applied (see {@link VITEST_MOCK_RPC_BARRIER_EVENT}). */ export declare const VITEST_MOCK_INTERCEPTOR_EVENTS: { readonly register: 'vitest:interceptor:register'; readonly delete: 'vitest:interceptor:delete'; readonly invalidate: 'vitest:interceptor:invalidate'; }; /** * Event a browser runtime sends right after an interceptor event, and then * waits for over {@link VITEST_MOCK_RPC_ENDPOINT}. * * Vite dispatches one socket's messages synchronously and in order, and the * mocker's interceptor handlers are synchronous, so this event arriving proves * that the interceptor event sent just before it has already been applied. That * is what lets the browser wait over HTTP instead of on the `<event>:result` * HMR payload `ModuleMockerServerInterceptor` waits for — which deadlocks, and * which the mocker broadcasts uncorrelated to every frame anyway. */ export declare const VITEST_MOCK_RPC_BARRIER_EVENT = "histoire:mocks:barrier"; /** * Structural subset of `@vitest/mocker/node`'s `ServerMockResolver` that * Histoire uses. */ interface ServerMockResolverLike { resolveId: (id: string, importer?: string) => unknown; resolveMock: (id: string, importer: string, options: { mock: 'spy' | 'factory' | 'auto'; }) => unknown; invalidate: (ids: string[]) => void; } /** * The only `@vitest/mocker` internal Histoire depends on here: * `ServerMockResolver` is a **named export of `@vitest/mocker/node`** (typed in * its `node.d.ts`) in Vitest 4 and 5. `mockerPlugin` builds the * exact same object (`new ServerMockResolver(server)`); Histoire only replaces * the transport around it. */ export interface VitestMockerNodeModule { ServerMockResolver?: new (server: ViteDevServer, options?: { moduleDirectories?: string[]; }) => ServerMockResolverLike; } /** * Pairs the barrier events arriving over the websocket with the HTTP requests * waiting for them. Either can come first: the two travel on independent * connections. * @param timeout How long an unreached barrier is waited for. */ export declare function createMockBarriers(timeout?: number): { /** Records that the browser's preceding interceptor event was applied. */ reach(barrierId: string): void; /** Resolves once `barrierId` is reached; rejects rather than hanging. */ wait(barrierId: string): Promise<void>; }; /** * Builds the Vite plugin answering Histoire's mock-resolution endpoint by * delegating to `@vitest/mocker`'s own server resolver. * @param mockerNode The imported `@vitest/mocker/node` module. * @throws If the module no longer exposes `ServerMockResolver`, so a Vitest * upgrade breaks loudly at dev-server start instead of silently serving * unmocked modules. */ export declare function createVitestMockRpcPlugin(mockerNode: VitestMockerNodeModule): VitePlugin; export {}; //# sourceMappingURL=vitest-mock-rpc.d.ts.map