CodeVetter · Repo Unpack · Behavior traces
mswjs/msw
The following traces follow concrete startup, response, persistence, isolation, and reload paths through the inspected source. Browser startup waits for worker activation and a client confirmation before returning a registration. HTTP resolution distinguishes explicit bypass, unhandled traffic, fallthrough, and mocked responses. Node boundaries and browser persistence preserve different kinds of state, while Vite serializes network changes around hot updates.
- Browser startup: setupWorker → start → ServiceWorkerSource.from → network.configure → network.enable → source.enable → #startWorker/getWorkerInstance → integrity check → activation wait → MOCK_ACTIVATE/MOCKING_ENABLED → return registration; unsupported Service Worker environments take FallbackHttpSource.
- Browser mock request: worker fetch handler → handleRequest/getResponse → REQUEST → #handleRequest/deserializeRequest → queue → HttpNetworkFrame.resolve/executeHandlers → RequestHandler.run → respondWith/MOCK_RESPONSE → respondWithMock; RESPONSE subsequently drives page lifecycle events.
- Unhandled HTTP flow: resolve emits request:start, checks bypass, calls executeHandlers, then executes onUnhandledFrame when no handler matches; unmatched CONNECT bypasses silently, and matched handlers returning nothing or passthrough bypass without the unhandled path.
- Node startup/request flow: setupServer creates AsyncHandlersController and defineNetwork → listen configures onUnhandledFrame and enables sources → InterceptorSource.apply/listeners → #handleRequest/queue → frame resolution → controller.respondWith; close initiates network.disable synchronously.
- Scoped override flow: server.boundary returns a wrapper → snapshots current handler buckets → AsyncLocalStorage.run creates a local context → use prepends per-kind overrides → frame lookup reads that context; outside a boundary, #getContext falls back to the server context.
- Cookie persistence flow: storeResponseCookies extracts raw Set-Cookie → CookieStore.setCookie updates tough-cookie → persist serializes jar entries to localStorage; the next browser CookieStore constructor restores entries, and getAllRequestCookies includes them only for eligible credentials.
- WebSocket flow: InterceptorSource queues a WebSocket frame → frame selects websocket handlers → handler.run matches and applies extensions → logger attaches → kConnect emits connection → link listener registers the client; broadcastExcept sends through local or remote client handles.
- Vite flow: resolveId maps virtual:msw to runtime → virtual options select browser/server defaults → runtime defines a disabled network → user configure/enable starts mocking; beforeUpdate disables, afterUpdate resumes if previously enabled, and dispose retires the instance and removes HMR listeners.