File: /var/www/design.system/node_modules/msw/lib/core/ws/handleWebSocketEvent.js.map
{"version":3,"sources":["../../../src/core/ws/handleWebSocketEvent.ts"],"sourcesContent":["import type { WebSocketConnectionData } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket'\nimport { RequestHandler } from '../handlers/RequestHandler'\nimport { WebSocketHandler, kDispatchEvent } from '../handlers/WebSocketHandler'\nimport { webSocketInterceptor } from './webSocketInterceptor'\nimport {\n onUnhandledRequest,\n UnhandledRequestStrategy,\n} from '../utils/request/onUnhandledRequest'\nimport { isHandlerKind } from '../utils/internal/isHandlerKind'\n\ninterface HandleWebSocketEventOptions {\n getUnhandledRequestStrategy: () => UnhandledRequestStrategy\n getHandlers: () => Array<RequestHandler | WebSocketHandler>\n onMockedConnection: (connection: WebSocketConnectionData) => void\n onPassthroughConnection: (onnection: WebSocketConnectionData) => void\n}\n\nexport function handleWebSocketEvent(options: HandleWebSocketEventOptions) {\n webSocketInterceptor.on('connection', async (connection) => {\n const handlers = options.getHandlers()\n\n const connectionEvent = new MessageEvent('connection', {\n data: connection,\n })\n\n // First, filter only those WebSocket handlers that\n // match the \"ws.link()\" endpoint predicate. Don't dispatch\n // anything yet so the logger can be attached to the connection\n // before it potentially sends events.\n const matchingHandlers: Array<WebSocketHandler> = []\n\n for (const handler of handlers) {\n if (\n isHandlerKind('EventHandler')(handler) &&\n handler.predicate({\n event: connectionEvent,\n parsedResult: handler.parse({\n event: connectionEvent,\n }),\n })\n ) {\n matchingHandlers.push(handler)\n }\n }\n\n if (matchingHandlers.length > 0) {\n options?.onMockedConnection(connection)\n\n // Iterate over the handlers and forward the connection\n // event to WebSocket event handlers. This is equivalent\n // to dispatching that event onto multiple listeners.\n for (const handler of matchingHandlers) {\n handler[kDispatchEvent](connectionEvent)\n }\n } else {\n // Construct a request representing this WebSocket connection.\n const request = new Request(connection.client.url, {\n headers: {\n upgrade: 'websocket',\n connection: 'upgrade',\n },\n })\n await onUnhandledRequest(\n request,\n options.getUnhandledRequestStrategy(),\n ).catch((error) => {\n const errorEvent = new Event('error')\n Object.defineProperty(errorEvent, 'cause', {\n enumerable: true,\n configurable: false,\n value: error,\n })\n connection.client.socket.dispatchEvent(errorEvent)\n })\n\n options?.onPassthroughConnection(connection)\n\n // If none of the \"ws\" handlers matched,\n // establish the WebSocket connection as-is.\n connection.server.connect()\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,8BAAiD;AACjD,kCAAqC;AACrC,gCAGO;AACP,2BAA8B;AASvB,SAAS,qBAAqB,SAAsC;AACzE,mDAAqB,GAAG,cAAc,OAAO,eAAe;AAC1D,UAAM,WAAW,QAAQ,YAAY;AAErC,UAAM,kBAAkB,IAAI,aAAa,cAAc;AAAA,MACrD,MAAM;AAAA,IACR,CAAC;AAMD,UAAM,mBAA4C,CAAC;AAEnD,eAAW,WAAW,UAAU;AAC9B,cACE,oCAAc,cAAc,EAAE,OAAO,KACrC,QAAQ,UAAU;AAAA,QAChB,OAAO;AAAA,QACP,cAAc,QAAQ,MAAM;AAAA,UAC1B,OAAO;AAAA,QACT,CAAC;AAAA,MACH,CAAC,GACD;AACA,yBAAiB,KAAK,OAAO;AAAA,MAC/B;AAAA,IACF;AAEA,QAAI,iBAAiB,SAAS,GAAG;AAC/B,eAAS,mBAAmB,UAAU;AAKtC,iBAAW,WAAW,kBAAkB;AACtC,gBAAQ,sCAAc,EAAE,eAAe;AAAA,MACzC;AAAA,IACF,OAAO;AAEL,YAAM,UAAU,IAAI,QAAQ,WAAW,OAAO,KAAK;AAAA,QACjD,SAAS;AAAA,UACP,SAAS;AAAA,UACT,YAAY;AAAA,QACd;AAAA,MACF,CAAC;AACD,gBAAM;AAAA,QACJ;AAAA,QACA,QAAQ,4BAA4B;AAAA,MACtC,EAAE,MAAM,CAAC,UAAU;AACjB,cAAM,aAAa,IAAI,MAAM,OAAO;AACpC,eAAO,eAAe,YAAY,SAAS;AAAA,UACzC,YAAY;AAAA,UACZ,cAAc;AAAA,UACd,OAAO;AAAA,QACT,CAAC;AACD,mBAAW,OAAO,OAAO,cAAc,UAAU;AAAA,MACnD,CAAC;AAED,eAAS,wBAAwB,UAAU;AAI3C,iBAAW,OAAO,QAAQ;AAAA,IAC5B;AAAA,EACF,CAAC;AACH;","names":[]}