CodeVetter · Repo Unpack · Data flow
mswjs/msw
Sources normalize intercepted traffic into protocol frames before shared resolution. HTTP handlers parse matching inputs and invoke resolvers in order, with handler state owning one-time and generator behavior. Browser response transport serializes across worker ports, whereas Node responds through interceptor controllers. Cookies and WebSocket client metadata have distinct persistence paths, and SSE sends encoded frames through a ReadableStream.
- The worker serializes request metadata and an ArrayBuffer body, sends REQUEST with a transferred port/body, and the page reconstructs a FetchRequest before queuing a ServiceWorkerHttpNetworkFrame.
- InterceptorSource receives request/controller pairs, stores HTTP frames in a WeakMap keyed by Request identity, and queues them; defineNetwork forwards frame events and supplies the current handler controller.
- HttpHandler.parse gathers cookies and URL params or invokes a custom predicate; predicate combines URL matching with the HTTP method, and extendResolverArgs exposes params and cookies.
- GraphQL input comes from GET query parameters, POST JSON, or multipart operations/map/files; parsing selects the requested document operation, and GraphQLHandler caches parsed results per Request.
- executeHandlers awaits handlers sequentially and stops on a response; RequestHandler owns isUsed, generator iterator/results, and lazy finalize cleanup, including returning a thrown Response as the resolver result.
- Browser mock responses cross the port as MOCK_RESPONSE using a transferable stream or buffered ArrayBuffer; the worker reconstructs Response, special-cases status zero, and buffers streamed navigation responses before committing navigation.
- Mock response cookies are forwarded to document.cookie and stored against request.url before respondWith; later credentialed requests merge document cookies, jar cookies, and explicit Cookie headers, with headers taking precedence.
- WebSocket storage persists only client id/url and reconstructs remote handles that send BroadcastChannel commands; SSE client.send instead serializes payloads into UTF-8 event frames enqueued on a response stream.