CodeVetter · Repo Unpack · System map
mswjs/msw
The package exposes separate environment and protocol entrypoints around one shared network engine. Browser HTTP interception uses a Service Worker when available, with an interceptor fallback; WebSocket interception remains a separate source. Node combines four interceptor types and adds asynchronous handler contexts. Distribution consists of ESM modules, declarations, an IIFE bundle, and a generated worker asset, with release automation targeting npm.
- package.json declares ESM, Node >=22.12.0, pnpm 11.27.1, and conditional exports that reject msw/browser in Node and msw/node in browsers.
- The root entry exports HTTP, WebSocket, SSE, and utilities; GraphQL is exposed through its separate package subpath rather than the root barrel.
- defineNetwork owns readyState, resolved options, the handler controller, and lifecycle event forwarding; each source queues frames that select handlers and resolve themselves.
- setupWorker configures ServiceWorkerSource or FallbackHttpSource for HTTP and an InterceptorSource containing WebSocketInterceptor for WebSocket connections.
- setupServer combines ClientRequest, XMLHttpRequest, Fetch, and WebSocket interceptors with AsyncHandlersController, then wraps the network in the common listen/close API.
- Browser state includes a tough-cookie jar persisted under __msw-cookie-store__ and WebSocket client metadata stored in IndexedDB; Node leaves implicit cookie management to the client and uses memory when IndexedDB is unavailable.
- tsdown builds ESM entries with declarations and source maps, a Node-platform Vite integration, and the MockServiceWorker IIFE; the worker build injects package version and checksum placeholders.
- Scripts provide lint, build, Vitest projects, Playwright, module compatibility, typings, publint, and knip; scheduled/manual releases build and test before publishing through the provenance-enabled release profile.