@mswjs/interceptors 0.31.1 → 0.32.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -39
- package/lib/node/RemoteHttpInterceptor.d.ts +1 -2
- package/lib/node/RemoteHttpInterceptor.js +11 -11
- package/lib/node/RemoteHttpInterceptor.mjs +5 -5
- package/lib/node/{chunk-LTEXDYJ6.js → chunk-2COJKQQB.js} +3 -3
- package/lib/node/chunk-3OJLYEWA.mjs +963 -0
- package/lib/node/chunk-3OJLYEWA.mjs.map +1 -0
- package/lib/node/chunk-5JMJ55U7.js +963 -0
- package/lib/node/chunk-5JMJ55U7.js.map +1 -0
- package/lib/node/{chunk-E4AC7YAC.js → chunk-BFLYGQ6D.js} +4 -2
- package/lib/node/{chunk-KSHIDGUL.mjs → chunk-DV4PBH4D.mjs} +3 -3
- package/lib/node/{chunk-OUWBQF3Z.mjs → chunk-KWV3JXSI.mjs} +14 -14
- package/lib/node/chunk-KWV3JXSI.mjs.map +1 -0
- package/lib/node/{chunk-6FRASLM3.mjs → chunk-PNWPIDEL.mjs} +2 -2
- package/lib/node/{chunk-APT7KA3B.js → chunk-PYD4E2EJ.js} +13 -13
- package/lib/node/{chunk-Q7POAM5N.mjs → chunk-TGTPXCLF.mjs} +3 -1
- package/lib/node/{chunk-MQJ3JOOK.js → chunk-UXCYRE4F.js} +14 -14
- package/lib/node/chunk-UXCYRE4F.js.map +1 -0
- package/lib/node/index.js +3 -3
- package/lib/node/index.mjs +2 -2
- package/lib/node/interceptors/ClientRequest/index.d.ts +83 -14
- package/lib/node/interceptors/ClientRequest/index.js +4 -4
- package/lib/node/interceptors/ClientRequest/index.mjs +3 -3
- package/lib/node/interceptors/XMLHttpRequest/index.js +4 -4
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/node/interceptors/fetch/index.js +10 -10
- package/lib/node/interceptors/fetch/index.mjs +2 -2
- package/lib/node/presets/node.d.ts +2 -3
- package/lib/node/presets/node.js +6 -6
- package/lib/node/presets/node.mjs +4 -4
- package/package.json +2 -2
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +595 -0
- package/src/interceptors/ClientRequest/agents.ts +78 -0
- package/src/interceptors/ClientRequest/index.test.ts +14 -12
- package/src/interceptors/ClientRequest/index.ts +200 -41
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +78 -98
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +40 -22
- package/src/interceptors/Socket/MockSocket.test.ts +264 -0
- package/src/interceptors/Socket/MockSocket.ts +59 -0
- package/src/interceptors/Socket/utils/baseUrlFromConnectionOptions.ts +26 -0
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.test.ts +52 -0
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.ts +33 -0
- package/src/interceptors/Socket/utils/parseRawHeaders.ts +10 -0
- package/lib/node/chunk-IS3CIGXU.js +0 -909
- package/lib/node/chunk-IS3CIGXU.js.map +0 -1
- package/lib/node/chunk-MQJ3JOOK.js.map +0 -1
- package/lib/node/chunk-OMOWHUE6.mjs +0 -909
- package/lib/node/chunk-OMOWHUE6.mjs.map +0 -1
- package/lib/node/chunk-OUWBQF3Z.mjs.map +0 -1
- package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +0 -206
- package/src/interceptors/ClientRequest/NodeClientRequest.ts +0 -680
- package/src/interceptors/ClientRequest/http.get.ts +0 -30
- package/src/interceptors/ClientRequest/http.request.ts +0 -27
- package/src/interceptors/ClientRequest/utils/cloneIncomingMessage.test.ts +0 -26
- package/src/interceptors/ClientRequest/utils/cloneIncomingMessage.ts +0 -74
- package/src/interceptors/ClientRequest/utils/createRequest.test.ts +0 -144
- package/src/interceptors/ClientRequest/utils/createRequest.ts +0 -51
- package/src/interceptors/ClientRequest/utils/createResponse.test.ts +0 -53
- package/src/interceptors/ClientRequest/utils/createResponse.ts +0 -55
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.test.ts +0 -41
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts +0 -53
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.test.ts +0 -36
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.ts +0 -39
- /package/lib/node/{chunk-LTEXDYJ6.js.map → chunk-2COJKQQB.js.map} +0 -0
- /package/lib/node/{chunk-E4AC7YAC.js.map → chunk-BFLYGQ6D.js.map} +0 -0
- /package/lib/node/{chunk-KSHIDGUL.mjs.map → chunk-DV4PBH4D.mjs.map} +0 -0
- /package/lib/node/{chunk-6FRASLM3.mjs.map → chunk-PNWPIDEL.mjs.map} +0 -0
- /package/lib/node/{chunk-APT7KA3B.js.map → chunk-PYD4E2EJ.js.map} +0 -0
- /package/lib/node/{chunk-Q7POAM5N.mjs.map → chunk-TGTPXCLF.mjs.map} +0 -0
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/interceptors/ClientRequest/index.ts","../../src/interceptors/ClientRequest/NodeClientRequest.ts","../../src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts","../../src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.ts","../../src/interceptors/ClientRequest/utils/cloneIncomingMessage.ts","../../src/interceptors/ClientRequest/utils/createResponse.ts","../../src/interceptors/ClientRequest/utils/createRequest.ts","../../src/utils/getValueBySymbol.ts","../../src/utils/isObject.ts","../../src/utils/getRawFetchHeaders.ts","../../src/utils/isNodeLikeError.ts","../../src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts","../../src/utils/getRequestOptionsByUrl.ts","../../src/utils/getUrlByRequestOptions.ts","../../src/utils/cloneObject.ts","../../src/interceptors/ClientRequest/http.get.ts","../../src/interceptors/ClientRequest/http.request.ts"],"sourcesContent":["import http from 'http'\nimport https from 'https'\nimport type { Emitter } from 'strict-event-emitter'\nimport { HttpRequestEventMap } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { get } from './http.get'\nimport { request } from './http.request'\nimport { NodeClientOptions, Protocol } from './NodeClientRequest'\n\nexport type ClientRequestEmitter = Emitter<HttpRequestEventMap>\n\nexport type ClientRequestModules = Map<Protocol, typeof http | typeof https>\n\n/**\n * Intercept requests made via the `ClientRequest` class.\n * Such requests include `http.get`, `https.request`, etc.\n */\nexport class ClientRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static interceptorSymbol = Symbol('http')\n private modules: ClientRequestModules\n\n constructor() {\n super(ClientRequestInterceptor.interceptorSymbol)\n\n this.modules = new Map()\n this.modules.set('http', http)\n this.modules.set('https', https)\n }\n\n protected setup(): void {\n const logger = this.logger.extend('setup')\n\n for (const [protocol, requestModule] of this.modules) {\n const { request: pureRequest, get: pureGet } = requestModule\n\n this.subscriptions.push(() => {\n requestModule.request = pureRequest\n requestModule.get = pureGet\n\n logger.info('native \"%s\" module restored!', protocol)\n })\n\n const options: NodeClientOptions = {\n emitter: this.emitter,\n logger: this.logger,\n }\n\n // @ts-ignore\n requestModule.request =\n // Force a line break.\n request(protocol, options)\n\n // @ts-ignore\n requestModule.get =\n // Force a line break.\n get(protocol, options)\n\n logger.info('native \"%s\" module patched!', protocol)\n }\n }\n}\n","import { ClientRequest, IncomingMessage, STATUS_CODES } from 'node:http'\nimport type { Logger } from '@open-draft/logger'\nimport { until } from '@open-draft/until'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport type { ClientRequestEmitter } from '.'\nimport {\n ClientRequestEndCallback,\n ClientRequestEndChunk,\n normalizeClientRequestEndArgs,\n} from './utils/normalizeClientRequestEndArgs'\nimport { NormalizedClientRequestArgs } from './utils/normalizeClientRequestArgs'\nimport {\n ClientRequestWriteArgs,\n normalizeClientRequestWriteArgs,\n} from './utils/normalizeClientRequestWriteArgs'\nimport { cloneIncomingMessage } from './utils/cloneIncomingMessage'\nimport { createResponse } from './utils/createResponse'\nimport { createRequest } from './utils/createRequest'\nimport { toInteractiveRequest } from '../../utils/toInteractiveRequest'\nimport { emitAsync } from '../../utils/emitAsync'\nimport { getRawFetchHeaders } from '../../utils/getRawFetchHeaders'\nimport { isNodeLikeError } from '../../utils/isNodeLikeError'\nimport { INTERNAL_REQUEST_ID_HEADER_NAME } from '../../Interceptor'\nimport { createRequestId } from '../../createRequestId'\nimport {\n createServerErrorResponse,\n isResponseError,\n} from '../../utils/responseUtils'\n\nexport type Protocol = 'http' | 'https'\n\nenum HttpClientInternalState {\n // Have the concept of an idle request because different\n // request methods can kick off request sending\n // (e.g. \".end()\" or \".flushHeaders()\").\n Idle,\n Sending,\n Sent,\n MockLookupStart,\n MockLookupEnd,\n ResponseReceived,\n}\n\nexport interface NodeClientOptions {\n emitter: ClientRequestEmitter\n logger: Logger\n}\n\nexport class NodeClientRequest extends ClientRequest {\n /**\n * The list of internal Node.js errors to suppress while\n * using the \"mock\" response source.\n */\n static suppressErrorCodes = [\n 'ENOTFOUND',\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'EAI_AGAIN',\n 'ENETUNREACH',\n 'EHOSTUNREACH',\n ]\n\n /**\n * Internal state of the request.\n */\n private state: HttpClientInternalState\n private responseType?: 'mock' | 'passthrough'\n private response: IncomingMessage\n private emitter: ClientRequestEmitter\n private logger: Logger\n private chunks: Array<{\n chunk?: string | Buffer\n encoding?: BufferEncoding\n }> = []\n private capturedError?: NodeJS.ErrnoException\n\n public url: URL\n public requestBuffer: Buffer | null\n\n constructor(\n [url, requestOptions, callback]: NormalizedClientRequestArgs,\n options: NodeClientOptions\n ) {\n super(requestOptions, callback)\n\n this.logger = options.logger.extend(\n `request ${requestOptions.method} ${url.href}`\n )\n\n this.logger.info('constructing ClientRequest using options:', {\n url,\n requestOptions,\n callback,\n })\n\n this.state = HttpClientInternalState.Idle\n this.url = url\n this.emitter = options.emitter\n\n // Set request buffer to null by default so that GET/HEAD requests\n // without a body wouldn't suddenly get one.\n this.requestBuffer = null\n\n // Construct a mocked response message.\n this.response = new IncomingMessage(this.socket!)\n }\n\n private writeRequestBodyChunk(\n chunk: string | Buffer | null,\n encoding?: BufferEncoding\n ): void {\n if (chunk == null) {\n return\n }\n\n if (this.requestBuffer == null) {\n this.requestBuffer = Buffer.from([])\n }\n\n const resolvedChunk = Buffer.isBuffer(chunk)\n ? chunk\n : Buffer.from(chunk, encoding)\n\n this.requestBuffer = Buffer.concat([this.requestBuffer, resolvedChunk])\n }\n\n write(...args: ClientRequestWriteArgs): boolean {\n const [chunk, encoding, callback] = normalizeClientRequestWriteArgs(args)\n this.logger.info('write:', { chunk, encoding, callback })\n this.chunks.push({ chunk, encoding })\n\n // Write each request body chunk to the internal buffer.\n this.writeRequestBodyChunk(chunk, encoding)\n\n this.logger.info(\n 'chunk successfully stored!',\n this.requestBuffer?.byteLength\n )\n\n /**\n * Prevent invoking the callback if the written chunk is empty.\n * @see https://nodejs.org/api/http.html#requestwritechunk-encoding-callback\n */\n if (!chunk || chunk.length === 0) {\n this.logger.info('written chunk is empty, skipping callback...')\n } else {\n callback?.()\n }\n\n // Do not write the request body chunks to prevent\n // the Socket from sending data to a potentially existing\n // server when there is a mocked response defined.\n return true\n }\n\n end(...args: any): this {\n this.logger.info('end', args)\n\n const requestId = createRequestId()\n\n const [chunk, encoding, callback] = normalizeClientRequestEndArgs(...args)\n this.logger.info('normalized arguments:', { chunk, encoding, callback })\n\n // Write the last request body chunk passed to the \"end()\" method.\n this.writeRequestBodyChunk(chunk, encoding || undefined)\n\n /**\n * @note Mark the request as sent immediately when invoking \".end()\".\n * In Node.js, calling \".end()\" will flush the remaining request body\n * and mark the request as \"finished\" immediately (\"end\" is synchronous)\n * but we delegate that property update to:\n *\n * - respondWith(), in the case of mocked responses;\n * - super.end(), in the case of bypassed responses.\n *\n * For that reason, we have to keep an internal flag for a finished request.\n */\n this.state = HttpClientInternalState.Sent\n\n const capturedRequest = createRequest(this)\n const { interactiveRequest, requestController } =\n toInteractiveRequest(capturedRequest)\n\n /**\n * @todo Remove this modification of the original request\n * and expose the controller alongside it in the \"request\"\n * listener argument.\n */\n Object.defineProperty(capturedRequest, 'respondWith', {\n value: requestController.respondWith.bind(requestController),\n })\n\n // Prevent handling this request if it has already been handled\n // in another (parent) interceptor (like XMLHttpRequest -> ClientRequest).\n // That means some interceptor up the chain has concluded that\n // this request must be performed as-is.\n if (this.hasHeader(INTERNAL_REQUEST_ID_HEADER_NAME)) {\n this.removeHeader(INTERNAL_REQUEST_ID_HEADER_NAME)\n return this.passthrough(chunk, encoding, callback)\n }\n\n // Add the last \"request\" listener that always resolves\n // the pending response Promise. This way if the consumer\n // hasn't handled the request themselves, we will prevent\n // the response Promise from pending indefinitely.\n this.emitter.once('request', ({ requestId: pendingRequestId }) => {\n /**\n * @note Ignore request events emitted by irrelevant\n * requests. This happens when response patching.\n */\n if (pendingRequestId !== requestId) {\n return\n }\n\n if (requestController.responsePromise.state === 'pending') {\n this.logger.info(\n 'request has not been handled in listeners, executing fail-safe listener...'\n )\n\n requestController.responsePromise.resolve(undefined)\n }\n })\n\n // Execute the resolver Promise like a side-effect.\n // Node.js 16 forces \"ClientRequest.end\" to be synchronous and return \"this\".\n until<unknown, Response | undefined>(async () => {\n // Notify the interceptor about the request.\n // This will call any \"request\" listeners the users have.\n this.logger.info(\n 'emitting the \"request\" event for %d listener(s)...',\n this.emitter.listenerCount('request')\n )\n\n this.state = HttpClientInternalState.MockLookupStart\n\n await emitAsync(this.emitter, 'request', {\n request: interactiveRequest,\n requestId,\n })\n\n this.logger.info('all \"request\" listeners done!')\n\n const mockedResponse = await requestController.responsePromise\n this.logger.info('event.respondWith called with:', mockedResponse)\n\n return mockedResponse\n }).then((resolverResult) => {\n this.logger.info('the listeners promise awaited!')\n\n this.state = HttpClientInternalState.MockLookupEnd\n\n /**\n * @fixme We are in the \"end()\" method that still executes in parallel\n * to our mocking logic here. This can be solved by migrating to the\n * Proxy-based approach and deferring the passthrough \"end()\" properly.\n * @see https://github.com/mswjs/interceptors/issues/346\n */\n if (!this.headersSent) {\n // Forward any request headers that the \"request\" listener\n // may have modified before proceeding with this request.\n for (const [headerName, headerValue] of capturedRequest.headers) {\n this.setHeader(headerName, headerValue)\n }\n }\n\n if (resolverResult.error) {\n this.logger.info(\n 'unhandled resolver exception, coercing to an error response...',\n resolverResult.error\n )\n\n // Handle thrown Response instances.\n if (resolverResult.error instanceof Response) {\n // Treat thrown Response.error() as a request error.\n if (isResponseError(resolverResult.error)) {\n this.logger.info(\n 'received network error response, erroring request...'\n )\n\n this.errorWith(new TypeError('Network error'))\n } else {\n // Handle a thrown Response as a mocked response.\n this.respondWith(resolverResult.error)\n }\n\n return\n }\n\n // Allow throwing Node.js-like errors, like connection rejection errors.\n // Treat them as request errors.\n if (isNodeLikeError(resolverResult.error)) {\n this.errorWith(resolverResult.error)\n return this\n }\n\n until(async () => {\n if (this.emitter.listenerCount('unhandledException') > 0) {\n // Emit the \"unhandledException\" event to allow the client\n // to opt-out from the default handling of exceptions\n // as 500 error responses.\n await emitAsync(this.emitter, 'unhandledException', {\n error: resolverResult.error,\n request: capturedRequest,\n requestId,\n controller: {\n respondWith: this.respondWith.bind(this),\n errorWith: this.errorWith.bind(this),\n },\n })\n\n // If after the \"unhandledException\" listeners are done,\n // the request is either not writable (was mocked) or\n // destroyed (has errored), do nothing.\n if (this.writableEnded || this.destroyed) {\n return\n }\n }\n\n // Unhandled exceptions in the request listeners are\n // synonymous to unhandled exceptions on the server.\n // Those are represented as 500 error responses.\n this.respondWith(createServerErrorResponse(resolverResult.error))\n })\n\n return this\n }\n\n const mockedResponse = resolverResult.data\n\n if (mockedResponse) {\n this.logger.info(\n 'received mocked response:',\n mockedResponse.status,\n mockedResponse.statusText\n )\n\n /**\n * @note Ignore this request being destroyed by TLS in Node.js\n * due to connection errors.\n */\n this.destroyed = false\n\n // Handle mocked \"Response.error\" network error responses.\n if (isResponseError(mockedResponse)) {\n this.logger.info(\n 'received network error response, erroring request...'\n )\n\n /**\n * There is no standardized error format for network errors\n * in Node.js. Instead, emit a generic TypeError.\n */\n this.errorWith(new TypeError('Network error'))\n\n return this\n }\n\n const responseClone = mockedResponse.clone()\n\n this.respondWith(mockedResponse)\n this.logger.info(\n mockedResponse.status,\n mockedResponse.statusText,\n '(MOCKED)'\n )\n\n callback?.()\n\n this.logger.info('emitting the custom \"response\" event...')\n\n const responseListenersPromise = emitAsync(this.emitter, 'response', {\n response: responseClone,\n isMockedResponse: true,\n request: capturedRequest,\n requestId,\n })\n\n responseListenersPromise.then(() => {\n this.logger.info('request (mock) is completed')\n })\n\n // Defer the end of the response until all the response\n // event listeners are done (those can be async).\n this.deferResponseEndUntil(responseListenersPromise, this.response)\n\n return this\n }\n\n this.logger.info('no mocked response received!')\n\n this.once(\n 'response-internal',\n (message: IncomingMessage, originalMessage: IncomingMessage) => {\n this.logger.info(message.statusCode, message.statusMessage)\n this.logger.info('original response headers:', message.headers)\n\n this.logger.info('emitting the custom \"response\" event...')\n\n const responseListenersPromise = emitAsync(this.emitter, 'response', {\n response: createResponse(message),\n isMockedResponse: false,\n request: capturedRequest,\n requestId,\n })\n\n // Defer the end of the response until all the response\n // event listeners are done (those can be async).\n this.deferResponseEndUntil(responseListenersPromise, originalMessage)\n }\n )\n\n return this.passthrough(chunk, encoding, callback)\n })\n\n return this\n }\n\n emit(event: string, ...data: any[]) {\n this.logger.info('emit: %s', event)\n\n if (event === 'response') {\n this.logger.info('found \"response\" event, cloning the response...')\n\n try {\n /**\n * Clone the response object when emitting the \"response\" event.\n * This prevents the response body stream from locking\n * and allows reading it twice:\n * 1. Internal \"response\" event from the observer.\n * 2. Any external response body listeners.\n * @see https://github.com/mswjs/interceptors/issues/161\n */\n const response = data[0] as IncomingMessage\n const firstClone = cloneIncomingMessage(response)\n const secondClone = cloneIncomingMessage(response)\n\n this.emit('response-internal', secondClone, firstClone)\n\n this.logger.info(\n 'response successfully cloned, emitting \"response\" event...'\n )\n return super.emit(event, firstClone, ...data.slice(1))\n } catch (error) {\n this.logger.info('error when cloning response:', error)\n return super.emit(event, ...data)\n }\n }\n\n if (event === 'error') {\n const error = data[0] as NodeJS.ErrnoException\n const errorCode = error.code || ''\n\n this.logger.info('error:\\n', error)\n\n // Suppress only specific Node.js connection errors.\n if (NodeClientRequest.suppressErrorCodes.includes(errorCode)) {\n // Until we aren't sure whether the request will be\n // passthrough, capture the first emitted connection\n // error in case we have to replay it for this request.\n if (this.state < HttpClientInternalState.MockLookupEnd) {\n if (!this.capturedError) {\n this.capturedError = error\n this.logger.info('captured the first error:', this.capturedError)\n }\n return false\n }\n\n // Ignore any connection errors once we know the request\n // has been resolved with a mocked response. Don't capture\n // them as they won't ever be replayed.\n if (\n this.state === HttpClientInternalState.ResponseReceived &&\n this.responseType === 'mock'\n ) {\n return false\n }\n }\n }\n\n return super.emit(event, ...data)\n }\n\n /**\n * Performs the intercepted request as-is.\n * Replays the captured request body chunks,\n * still emits the internal events, and wraps\n * up the request with `super.end()`.\n */\n private passthrough(\n chunk: ClientRequestEndChunk | null,\n encoding?: BufferEncoding | null,\n callback?: ClientRequestEndCallback | null\n ): this {\n this.state = HttpClientInternalState.ResponseReceived\n this.responseType = 'passthrough'\n\n // Propagate previously captured errors.\n // For example, a ECONNREFUSED error when connecting to a non-existing host.\n if (this.capturedError) {\n this.emit('error', this.capturedError)\n return this\n }\n\n this.logger.info('writing request chunks...', this.chunks)\n\n // Write the request body chunks in the order of \".write()\" calls.\n // Note that no request body has been written prior to this point\n // in order to prevent the Socket to communicate with a potentially\n // existing server.\n for (const { chunk, encoding } of this.chunks) {\n if (encoding) {\n super.write(chunk, encoding)\n } else {\n super.write(chunk)\n }\n }\n\n this.once('error', (error) => {\n this.logger.info('original request error:', error)\n })\n\n this.once('abort', () => {\n this.logger.info('original request aborted!')\n })\n\n this.once('response-internal', (message: IncomingMessage) => {\n this.logger.info(message.statusCode, message.statusMessage)\n this.logger.info('original response headers:', message.headers)\n })\n\n this.logger.info('performing original request...')\n\n // This call signature is way too dynamic.\n return super.end(...[chunk, encoding as any, callback].filter(Boolean))\n }\n\n /**\n * Responds to this request instance using a mocked response.\n */\n private respondWith(mockedResponse: Response): void {\n this.logger.info('responding with a mocked response...', mockedResponse)\n\n this.state = HttpClientInternalState.ResponseReceived\n this.responseType = 'mock'\n\n /**\n * Mark the request as finished right before streaming back the response.\n * This is not entirely conventional but this will allow the consumer to\n * modify the outoging request in the interceptor.\n *\n * The request is finished when its headers and bodies have been sent.\n * @see https://nodejs.org/api/http.html#event-finish\n */\n Object.defineProperties(this, {\n writableFinished: { value: true },\n writableEnded: { value: true },\n })\n this.emit('finish')\n\n const { status, statusText, headers, body } = mockedResponse\n this.response.statusCode = status\n this.response.statusMessage = statusText || STATUS_CODES[status]\n\n // Try extracting the raw headers from the headers instance.\n // If not possible, fallback to the headers instance as-is.\n const rawHeaders = getRawFetchHeaders(headers) || headers\n\n if (rawHeaders) {\n this.response.headers = {}\n\n rawHeaders.forEach((headerValue, headerName) => {\n /**\n * @note Make sure that multi-value headers are appended correctly.\n */\n this.response.rawHeaders.push(headerName, headerValue)\n\n const insensitiveHeaderName = headerName.toLowerCase()\n const prevHeaders = this.response.headers[insensitiveHeaderName]\n this.response.headers[insensitiveHeaderName] = prevHeaders\n ? Array.prototype.concat([], prevHeaders, headerValue)\n : headerValue\n })\n }\n this.logger.info('mocked response headers ready:', headers)\n\n /**\n * Set the internal \"res\" property to the mocked \"OutgoingMessage\"\n * to make the \"ClientRequest\" instance think there's data received\n * from the socket.\n * @see https://github.com/nodejs/node/blob/9c405f2591f5833d0247ed0fafdcd68c5b14ce7a/lib/_http_client.js#L501\n *\n * Set the response immediately so the interceptor could stream data\n * chunks to the request client as they come in.\n */\n // @ts-ignore\n this.res = this.response\n this.emit('response', this.response)\n\n const isResponseStreamFinished = new DeferredPromise<void>()\n\n const finishResponseStream = () => {\n this.logger.info('finished response stream!')\n\n // Push \"null\" to indicate that the response body is complete\n // and shouldn't be written to anymore.\n this.response.push(null)\n this.response.complete = true\n\n isResponseStreamFinished.resolve()\n }\n\n if (body) {\n const bodyReader = body.getReader()\n const readNextChunk = async (): Promise<void> => {\n const { done, value } = await bodyReader.read()\n\n if (done) {\n finishResponseStream()\n return\n }\n\n this.response.emit('data', value)\n\n return readNextChunk()\n }\n\n readNextChunk()\n } else {\n finishResponseStream()\n }\n\n isResponseStreamFinished.then(() => {\n this.logger.info('finalizing response...')\n this.response.emit('end')\n this.terminate()\n\n this.logger.info('request complete!')\n })\n }\n\n private errorWith(error: Error): void {\n this.destroyed = true\n this.emit('error', error)\n this.terminate()\n }\n\n /**\n * Terminates a pending request.\n */\n private terminate(): void {\n /**\n * @note Some request clients (e.g. Octokit, or proxy providers like\n * `global-agent`) create a ClientRequest in a way that it has no Agent set,\n * or does not have a destroy method on it. Now, whether that's correct is\n * debatable, but we should still handle this case gracefully.\n * @see https://github.com/mswjs/interceptors/issues/304\n */\n // @ts-ignore \"agent\" is a private property.\n this.agent?.destroy?.()\n }\n\n private deferResponseEndUntil(\n promise: Promise<unknown>,\n response: IncomingMessage\n ): void {\n response.emit = new Proxy(response.emit, {\n apply: (target, thisArg, args) => {\n const [event] = args\n const callEmit = () => Reflect.apply(target, thisArg, args)\n\n if (event === 'end') {\n promise.finally(() => callEmit())\n return this.listenerCount('end') > 0\n }\n\n return callEmit()\n },\n })\n }\n}\n","import { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('utils getUrlByRequestOptions')\n\nexport type ClientRequestEndChunk = string | Buffer\nexport type ClientRequestEndCallback = () => void\n\ntype HttpRequestEndArgs =\n | []\n | [ClientRequestEndCallback]\n | [ClientRequestEndChunk, ClientRequestEndCallback?]\n | [ClientRequestEndChunk, BufferEncoding, ClientRequestEndCallback?]\n\ntype NormalizedHttpRequestEndParams = [\n ClientRequestEndChunk | null,\n BufferEncoding | null,\n ClientRequestEndCallback | null\n]\n\n/**\n * Normalizes a list of arguments given to the `ClientRequest.end()`\n * method to always include `chunk`, `encoding`, and `callback`.\n */\nexport function normalizeClientRequestEndArgs(\n ...args: HttpRequestEndArgs\n): NormalizedHttpRequestEndParams {\n logger.info('arguments', args)\n const normalizedArgs = new Array(3)\n .fill(null)\n .map((value, index) => args[index] || value)\n\n normalizedArgs.sort((a, b) => {\n // If first element is a function, move it rightwards.\n if (typeof a === 'function') {\n return 1\n }\n\n // If second element is a function, move the first leftwards.\n if (typeof b === 'function') {\n return -1\n }\n\n // If both elements are strings, preserve their original index.\n if (typeof a === 'string' && typeof b === 'string') {\n return normalizedArgs.indexOf(a) - normalizedArgs.indexOf(b)\n }\n\n return 0\n })\n\n logger.info('normalized args', normalizedArgs)\n return normalizedArgs as NormalizedHttpRequestEndParams\n}\n","import { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('http normalizeWriteArgs')\n\nexport type ClientRequestWriteCallback = (error?: Error | null) => void\nexport type ClientRequestWriteArgs = [\n chunk: string | Buffer,\n encoding?: BufferEncoding | ClientRequestWriteCallback,\n callback?: ClientRequestWriteCallback\n]\n\nexport type NormalizedClientRequestWriteArgs = [\n chunk: string | Buffer,\n encoding?: BufferEncoding,\n callback?: ClientRequestWriteCallback\n]\n\nexport function normalizeClientRequestWriteArgs(\n args: ClientRequestWriteArgs\n): NormalizedClientRequestWriteArgs {\n logger.info('normalizing ClientRequest.write arguments...', args)\n\n const chunk = args[0]\n const encoding =\n typeof args[1] === 'string' ? (args[1] as BufferEncoding) : undefined\n const callback = typeof args[1] === 'function' ? args[1] : args[2]\n\n const writeArgs: NormalizedClientRequestWriteArgs = [\n chunk,\n encoding,\n callback,\n ]\n logger.info(\n 'successfully normalized ClientRequest.write arguments:',\n writeArgs\n )\n\n return writeArgs\n}\n","import { IncomingMessage } from 'http'\nimport { PassThrough } from 'stream'\n\nexport const IS_CLONE = Symbol('isClone')\n\nexport interface ClonedIncomingMessage extends IncomingMessage {\n [IS_CLONE]: boolean\n}\n\n/**\n * Clones a given `http.IncomingMessage` instance.\n */\nexport function cloneIncomingMessage(\n message: IncomingMessage\n): ClonedIncomingMessage {\n const clone = message.pipe(new PassThrough())\n\n // Inherit all direct \"IncomingMessage\" properties.\n inheritProperties(message, clone)\n\n // Deeply inherit the message prototypes (Readable, Stream, EventEmitter, etc.).\n const clonedPrototype = Object.create(IncomingMessage.prototype)\n getPrototypes(clone).forEach((prototype) => {\n inheritProperties(prototype, clonedPrototype)\n })\n Object.setPrototypeOf(clone, clonedPrototype)\n\n Object.defineProperty(clone, IS_CLONE, {\n enumerable: true,\n value: true,\n })\n\n return clone as unknown as ClonedIncomingMessage\n}\n\n/**\n * Returns a list of all prototypes the given object extends.\n */\nfunction getPrototypes(source: object): object[] {\n const prototypes: object[] = []\n let current = source\n\n while ((current = Object.getPrototypeOf(current))) {\n prototypes.push(current)\n }\n\n return prototypes\n}\n\n/**\n * Inherits a given target object properties and symbols\n * onto the given source object.\n * @param source Object which should acquire properties.\n * @param target Object to inherit the properties from.\n */\nfunction inheritProperties(source: object, target: object): void {\n const properties = [\n ...Object.getOwnPropertyNames(source),\n ...Object.getOwnPropertySymbols(source),\n ]\n\n for (const property of properties) {\n if (target.hasOwnProperty(property)) {\n continue\n }\n\n const descriptor = Object.getOwnPropertyDescriptor(source, property)\n if (!descriptor) {\n continue\n }\n\n Object.defineProperty(target, property, descriptor)\n }\n}\n","import type { IncomingHttpHeaders, IncomingMessage } from 'http'\nimport { isResponseWithoutBody } from '../../../utils/responseUtils'\n\n/**\n * Creates a Fetch API `Response` instance from the given\n * `http.IncomingMessage` instance.\n */\nexport function createResponse(message: IncomingMessage): Response {\n const responseBodyOrNull = isResponseWithoutBody(message.statusCode || 200)\n ? null\n : new ReadableStream({\n start(controller) {\n message.on('data', (chunk) => controller.enqueue(chunk))\n message.on('end', () => controller.close())\n\n /**\n * @todo Should also listen to the \"error\" on the message\n * and forward it to the controller. Otherwise the stream\n * will pend indefinitely.\n */\n },\n })\n\n return new Response(responseBodyOrNull, {\n status: message.statusCode,\n statusText: message.statusMessage,\n headers: createHeadersFromIncomingHttpHeaders(message.headers),\n })\n}\n\nfunction createHeadersFromIncomingHttpHeaders(\n httpHeaders: IncomingHttpHeaders\n): Headers {\n const headers = new Headers()\n\n for (const headerName in httpHeaders) {\n const headerValues = httpHeaders[headerName]\n\n if (typeof headerValues === 'undefined') {\n continue\n }\n\n if (Array.isArray(headerValues)) {\n headerValues.forEach((headerValue) => {\n headers.append(headerName, headerValue)\n })\n\n continue\n }\n\n headers.set(headerName, headerValues)\n }\n\n return headers\n}\n","import type { NodeClientRequest } from '../NodeClientRequest'\n\n/**\n * Creates a Fetch API `Request` instance from the given `http.ClientRequest`.\n */\nexport function createRequest(clientRequest: NodeClientRequest): Request {\n const headers = new Headers()\n\n const outgoingHeaders = clientRequest.getHeaders()\n for (const headerName in outgoingHeaders) {\n const headerValue = outgoingHeaders[headerName]\n\n if (typeof headerValue === 'undefined') {\n continue\n }\n\n const valuesList = Array.prototype.concat([], headerValue)\n for (const value of valuesList) {\n headers.append(headerName, value.toString())\n }\n }\n\n /**\n * Translate the authentication from the request URL to\n * the request \"Authorization\" header.\n * @see https://github.com/mswjs/interceptors/issues/438\n */\n if (clientRequest.url.username || clientRequest.url.password) {\n const username = decodeURIComponent(clientRequest.url.username || '')\n const password = decodeURIComponent(clientRequest.url.password || '')\n const auth = `${username}:${password}`\n headers.set('Authorization', `Basic ${btoa(auth)}`)\n\n // Remove the credentials from the URL since you cannot\n // construct a Request instance with such a URL.\n clientRequest.url.username = ''\n clientRequest.url.password = ''\n }\n\n const method = clientRequest.method || 'GET'\n\n return new Request(clientRequest.url, {\n method,\n headers,\n credentials: 'same-origin',\n body:\n method === 'HEAD' || method === 'GET'\n ? null\n : clientRequest.requestBuffer,\n })\n}\n","/**\n * Returns the value behind the symbol with the given name.\n */\nexport function getValueBySymbol<T>(\n symbolName: string,\n source: object\n): T | undefined {\n const ownSymbols = Object.getOwnPropertySymbols(source)\n\n const symbol = ownSymbols.find((symbol) => {\n return symbol.description === symbolName\n })\n\n if (symbol) {\n return Reflect.get(source, symbol)\n }\n\n return\n}\n","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","import { getValueBySymbol } from './getValueBySymbol'\nimport { isObject } from './isObject'\n\ntype RawHeadersMap = Map<string, string>\ntype HeadersMapHeader = { name: string; value: string }\n\n/**\n * Returns raw headers from the given `Headers` instance.\n * @example\n * const headers = new Headers([\n * ['X-HeadeR-NamE', 'Value']\n * ])\n * getRawFetchHeaders(headers)\n * // { 'X-HeadeR-NamE': 'Value' }\n */\nexport function getRawFetchHeaders(\n headers: Headers\n): RawHeadersMap | undefined {\n const headersList = getValueBySymbol<object>('headers list', headers)\n\n if (!headersList) {\n return\n }\n\n const headersMap = getValueBySymbol<\n Map<string, string> | Map<string, HeadersMapHeader>\n >('headers map', headersList)\n\n /**\n * @note Older versions of Node.js (e.g. 18.8.0) keep headers map\n * as Map<normalizedHeaderName, value> without any means to tap\n * into raw header values. Detect that and return undefined.\n */\n if (!headersMap || !isHeadersMapWithRawHeaderNames(headersMap)) {\n return\n }\n\n // Raw headers is a map of { rawHeaderName: rawHeaderValue }\n const rawHeaders: RawHeadersMap = new Map<string, string>()\n\n headersMap.forEach(({ name, value }) => {\n rawHeaders.set(name, value)\n })\n\n return rawHeaders\n}\n\nfunction isHeadersMapWithRawHeaderNames(\n headersMap: Map<string, string> | Map<string, HeadersMapHeader>\n): headersMap is Map<string, HeadersMapHeader> {\n return Array.from(\n headersMap.values() as Iterable<string | HeadersMapHeader>\n ).every((value) => {\n return isObject<HeadersMapHeader>(value) && 'name' in value\n })\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n","import {\n Agent as HttpAgent,\n globalAgent as httpGlobalAgent,\n IncomingMessage,\n} from 'http'\nimport {\n RequestOptions,\n Agent as HttpsAgent,\n globalAgent as httpsGlobalAgent,\n} from 'https'\nimport { Url as LegacyURL, parse as parseUrl } from 'url'\nimport { Logger } from '@open-draft/logger'\nimport { getRequestOptionsByUrl } from '../../../utils/getRequestOptionsByUrl'\nimport {\n ResolvedRequestOptions,\n getUrlByRequestOptions,\n} from '../../../utils/getUrlByRequestOptions'\nimport { cloneObject } from '../../../utils/cloneObject'\nimport { isObject } from '../../../utils/isObject'\n\nconst logger = new Logger('http normalizeClientRequestArgs')\n\nexport type HttpRequestCallback = (response: IncomingMessage) => void\n\nexport type ClientRequestArgs =\n // Request without any arguments is also possible.\n | []\n | [string | URL | LegacyURL, HttpRequestCallback?]\n | [string | URL | LegacyURL, RequestOptions, HttpRequestCallback?]\n | [RequestOptions, HttpRequestCallback?]\n\nfunction resolveRequestOptions(\n args: ClientRequestArgs,\n url: URL\n): RequestOptions {\n // Calling `fetch` provides only URL to `ClientRequest`\n // without any `RequestOptions` or callback.\n if (typeof args[1] === 'undefined' || typeof args[1] === 'function') {\n logger.info('request options not provided, deriving from the url', url)\n return getRequestOptionsByUrl(url)\n }\n\n if (args[1]) {\n logger.info('has custom RequestOptions!', args[1])\n const requestOptionsFromUrl = getRequestOptionsByUrl(url)\n\n logger.info('derived RequestOptions from the URL:', requestOptionsFromUrl)\n\n /**\n * Clone the request options to lock their state\n * at the moment they are provided to `ClientRequest`.\n * @see https://github.com/mswjs/interceptors/issues/86\n */\n logger.info('cloning RequestOptions...')\n const clonedRequestOptions = cloneObject(args[1])\n logger.info('successfully cloned RequestOptions!', clonedRequestOptions)\n\n return {\n ...requestOptionsFromUrl,\n ...clonedRequestOptions,\n }\n }\n\n logger.info('using an empty object as request options')\n return {} as RequestOptions\n}\n\n/**\n * Overrides the given `URL` instance with the explicit properties provided\n * on the `RequestOptions` object. The options object takes precedence,\n * and will replace URL properties like \"host\", \"path\", and \"port\", if specified.\n */\nfunction overrideUrlByRequestOptions(url: URL, options: RequestOptions): URL {\n url.host = options.host || url.host\n url.hostname = options.hostname || url.hostname\n url.port = options.port ? options.port.toString() : url.port\n\n if (options.path) {\n const parsedOptionsPath = parseUrl(options.path, false)\n url.pathname = parsedOptionsPath.pathname || ''\n url.search = parsedOptionsPath.search || ''\n }\n\n return url\n}\n\nfunction resolveCallback(\n args: ClientRequestArgs\n): HttpRequestCallback | undefined {\n return typeof args[1] === 'function' ? args[1] : args[2]\n}\n\nexport type NormalizedClientRequestArgs = [\n url: URL,\n options: ResolvedRequestOptions,\n callback?: HttpRequestCallback\n]\n\n/**\n * Normalizes parameters given to a `http.request` call\n * so it always has a `URL` and `RequestOptions`.\n */\nexport function normalizeClientRequestArgs(\n defaultProtocol: string,\n ...args: ClientRequestArgs\n): NormalizedClientRequestArgs {\n let url: URL\n let options: ResolvedRequestOptions\n let callback: HttpRequestCallback | undefined\n\n logger.info('arguments', args)\n logger.info('using default protocol:', defaultProtocol)\n\n // Support \"http.request()\" calls without any arguments.\n // That call results in a \"GET http://localhost\" request.\n if (args.length === 0) {\n const url = new URL('http://localhost')\n const options = resolveRequestOptions(args, url)\n return [url, options]\n }\n\n // Convert a url string into a URL instance\n // and derive request options from it.\n if (typeof args[0] === 'string') {\n logger.info('first argument is a location string:', args[0])\n\n url = new URL(args[0])\n logger.info('created a url:', url)\n\n const requestOptionsFromUrl = getRequestOptionsByUrl(url)\n logger.info('request options from url:', requestOptionsFromUrl)\n\n options = resolveRequestOptions(args, url)\n logger.info('resolved request options:', options)\n\n callback = resolveCallback(args)\n }\n // Handle a given URL instance as-is\n // and derive request options from it.\n else if (args[0] instanceof URL) {\n url = args[0]\n logger.info('first argument is a URL:', url)\n\n // Check if the second provided argument is RequestOptions.\n // If it is, check if \"options.path\" was set and rewrite it\n // on the input URL.\n // Do this before resolving options from the URL below\n // to prevent query string from being duplicated in the path.\n if (typeof args[1] !== 'undefined' && isObject<RequestOptions>(args[1])) {\n url = overrideUrlByRequestOptions(url, args[1])\n }\n\n options = resolveRequestOptions(args, url)\n logger.info('derived request options:', options)\n\n callback = resolveCallback(args)\n }\n // Handle a legacy URL instance and re-normalize from either a RequestOptions object\n // or a WHATWG URL.\n else if ('hash' in args[0] && !('method' in args[0])) {\n const [legacyUrl] = args\n logger.info('first argument is a legacy URL:', legacyUrl)\n\n if (legacyUrl.hostname === null) {\n /**\n * We are dealing with a relative url, so use the path as an \"option\" and\n * merge in any existing options, giving priority to exising options -- i.e. a path in any\n * existing options will take precedence over the one contained in the url. This is consistent\n * with the behaviour in ClientRequest.\n * @see https://github.com/nodejs/node/blob/d84f1312915fe45fe0febe888db692c74894c382/lib/_http_client.js#L122\n */\n logger.info('given legacy URL is relative (no hostname)')\n\n return isObject(args[1])\n ? normalizeClientRequestArgs(\n defaultProtocol,\n { path: legacyUrl.path, ...args[1] },\n args[2]\n )\n : normalizeClientRequestArgs(\n defaultProtocol,\n { path: legacyUrl.path },\n args[1] as HttpRequestCallback\n )\n }\n\n logger.info('given legacy url is absolute')\n\n // We are dealing with an absolute URL, so convert to WHATWG and try again.\n const resolvedUrl = new URL(legacyUrl.href)\n\n return args[1] === undefined\n ? normalizeClientRequestArgs(defaultProtocol, resolvedUrl)\n : typeof args[1] === 'function'\n ? normalizeClientRequestArgs(defaultProtocol, resolvedUrl, args[1])\n : normalizeClientRequestArgs(\n defaultProtocol,\n resolvedUrl,\n args[1],\n args[2]\n )\n }\n // Handle a given \"RequestOptions\" object as-is\n // and derive the URL instance from it.\n else if (isObject(args[0])) {\n options = args[0] as any\n logger.info('first argument is RequestOptions:', options)\n\n // When handling a \"RequestOptions\" object without an explicit \"protocol\",\n // infer the protocol from the request issuing module (http/https).\n options.protocol = options.protocol || defaultProtocol\n logger.info('normalized request options:', options)\n\n url = getUrlByRequestOptions(options)\n logger.info('created a URL from RequestOptions:', url.href)\n\n callback = resolveCallback(args)\n } else {\n throw new Error(\n `Failed to construct ClientRequest with these parameters: ${args}`\n )\n }\n\n options.protocol = options.protocol || url.protocol\n options.method = options.method || 'GET'\n\n /**\n * Infer a fallback agent from the URL protocol.\n * The interception is done on the \"ClientRequest\" level (\"NodeClientRequest\")\n * and it may miss the correct agent. Always align the agent\n * with the URL protocol, if not provided.\n *\n * @note Respect the \"agent: false\" value.\n */\n if (typeof options.agent === 'undefined') {\n const agent =\n options.protocol === 'https:'\n ? new HttpsAgent({\n rejectUnauthorized: options.rejectUnauthorized,\n })\n : new HttpAgent()\n\n options.agent = agent\n logger.info('resolved fallback agent:', agent)\n }\n\n /**\n * Ensure that the default Agent is always set.\n * This prevents the protocol mismatch for requests with { agent: false },\n * where the global Agent is inferred.\n * @see https://github.com/mswjs/msw/issues/1150\n * @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L130\n * @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L157-L159\n */\n if (!options._defaultAgent) {\n logger.info(\n 'has no default agent, setting the default agent for \"%s\"',\n options.protocol\n )\n\n options._defaultAgent =\n options.protocol === 'https:' ? httpsGlobalAgent : httpGlobalAgent\n }\n\n logger.info('successfully resolved url:', url.href)\n logger.info('successfully resolved options:', options)\n logger.info('successfully resolved callback:', callback)\n\n return [url, options, callback]\n}\n","import { RequestOptions } from 'http'\n\n/**\n * Converts a URL instance into the RequestOptions object expected by\n * the `ClientRequest` class.\n * @see https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/internal/url.js#L1257\n */\nexport function getRequestOptionsByUrl(url: URL): RequestOptions {\n const options: RequestOptions = {\n method: 'GET',\n protocol: url.protocol,\n hostname:\n typeof url.hostname === 'string' && url.hostname.startsWith('[')\n ? url.hostname.slice(1, -1)\n : url.hostname,\n host: url.host,\n path: `${url.pathname}${url.search || ''}`,\n }\n\n if (!!url.port) {\n options.port = Number(url.port)\n }\n\n if (url.username || url.password) {\n options.auth = `${url.username}:${url.password}`\n }\n\n return options\n}\n","import { Agent } from 'http'\nimport { RequestOptions, Agent as HttpsAgent } from 'https'\nimport { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('utils getUrlByRequestOptions')\n\n// Request instance constructed by the \"request\" library\n// has a \"self\" property that has a \"uri\" field. This is\n// reproducible by performing a \"XMLHttpRequest\" request in JSDOM.\nexport interface RequestSelf {\n uri?: URL\n}\n\nexport type ResolvedRequestOptions = RequestOptions & RequestSelf\n\nexport const DEFAULT_PATH = '/'\nconst DEFAULT_PROTOCOL = 'http:'\nconst DEFAULT_HOSTNAME = 'localhost'\nconst SSL_PORT = 443\n\nfunction getAgent(\n options: ResolvedRequestOptions\n): Agent | HttpsAgent | undefined {\n return options.agent instanceof Agent ? options.agent : undefined\n}\n\nfunction getProtocolByRequestOptions(options: ResolvedRequestOptions): string {\n if (options.protocol) {\n return options.protocol\n }\n\n const agent = getAgent(options)\n const agentProtocol = (agent as RequestOptions)?.protocol\n\n if (agentProtocol) {\n return agentProtocol\n }\n\n const port = getPortByRequestOptions(options)\n const isSecureRequest = options.cert || port === SSL_PORT\n\n return isSecureRequest ? 'https:' : options.uri?.protocol || DEFAULT_PROTOCOL\n}\n\nfunction getPortByRequestOptions(\n options: ResolvedRequestOptions\n): number | undefined {\n // Use the explicitly provided port.\n if (options.port) {\n return Number(options.port)\n }\n\n // Otherwise, try to resolve port from the agent.\n const agent = getAgent(options)\n\n if ((agent as HttpsAgent)?.options.port) {\n return Number((agent as HttpsAgent).options.port)\n }\n\n if ((agent as RequestOptions)?.defaultPort) {\n return Number((agent as RequestOptions).defaultPort)\n }\n\n // Lastly, return undefined indicating that the port\n // must inferred from the protocol. Do not infer it here.\n return undefined\n}\n\ninterface RequestAuth {\n username: string\n password: string\n}\n\nfunction getAuthByRequestOptions(\n options: ResolvedRequestOptions\n): RequestAuth | undefined {\n if (options.auth) {\n const [username, password] = options.auth.split(':')\n return { username, password }\n }\n}\n\n/**\n * Returns true if host looks like an IPv6 address without surrounding brackets\n * It assumes any host containing `:` is definitely not IPv4 and probably IPv6,\n * but note that this could include invalid IPv6 addresses as well.\n */\nfunction isRawIPv6Address(host: string): boolean {\n return host.includes(':') && !host.startsWith('[') && !host.endsWith(']')\n}\n\nfunction getHostname(options: ResolvedRequestOptions): string | undefined {\n let host = options.hostname || options.host\n\n if (host) {\n if (isRawIPv6Address(host)) {\n host = `[${host}]`\n }\n\n // Check the presence of the port, and if it's present,\n // remove it from the host, returning a hostname.\n return new URL(`http://${host}`).hostname\n }\n\n return DEFAULT_HOSTNAME\n}\n\n/**\n * Creates a `URL` instance from a given `RequestOptions` object.\n */\nexport function getUrlByRequestOptions(options: ResolvedRequestOptions): URL {\n logger.info('request options', options)\n\n if (options.uri) {\n logger.info(\n 'constructing url from explicitly provided \"options.uri\": %s',\n options.uri\n )\n return new URL(options.uri.href)\n }\n\n logger.info('figuring out url from request options...')\n\n const protocol = getProtocolByRequestOptions(options)\n logger.info('protocol', protocol)\n\n const port = getPortByRequestOptions(options)\n logger.info('port', port)\n\n const hostname = getHostname(options)\n logger.info('hostname', hostname)\n\n const path = options.path || DEFAULT_PATH\n logger.info('path', path)\n\n const credentials = getAuthByRequestOptions(options)\n logger.info('credentials', credentials)\n\n const authString = credentials\n ? `${credentials.username}:${credentials.password}@`\n : ''\n logger.info('auth string:', authString)\n\n const portString = typeof port !== 'undefined' ? `:${port}` : ''\n const url = new URL(`${protocol}//${hostname}${portString}${path}`)\n url.username = credentials?.username || ''\n url.password = credentials?.password || ''\n\n logger.info('created url:', url)\n\n return url\n}\n","import { Logger } from '@open-draft/logger'\n\nconst logger = new Logger('cloneObject')\n\nfunction isPlainObject(obj?: Record<string, any>): boolean {\n logger.info('is plain object?', obj)\n\n if (obj == null || !obj.constructor?.name) {\n logger.info('given object is undefined, not a plain object...')\n return false\n }\n\n logger.info('checking the object constructor:', obj.constructor.name)\n return obj.constructor.name === 'Object'\n}\n\nexport function cloneObject<ObjectType extends Record<string, any>>(\n obj: ObjectType\n): ObjectType {\n logger.info('cloning object:', obj)\n\n const enumerableProperties = Object.entries(obj).reduce<Record<string, any>>(\n (acc, [key, value]) => {\n logger.info('analyzing key-value pair:', key, value)\n\n // Recursively clone only plain objects, omitting class instances.\n acc[key] = isPlainObject(value) ? cloneObject(value) : value\n return acc\n },\n {}\n )\n\n return isPlainObject(obj)\n ? enumerableProperties\n : Object.assign(Object.getPrototypeOf(obj), enumerableProperties)\n}\n","import { ClientRequest } from 'node:http'\nimport {\n NodeClientOptions,\n NodeClientRequest,\n Protocol,\n} from './NodeClientRequest'\nimport {\n ClientRequestArgs,\n normalizeClientRequestArgs,\n} from './utils/normalizeClientRequestArgs'\n\nexport function get(protocol: Protocol, options: NodeClientOptions) {\n return function interceptorsHttpGet(\n ...args: ClientRequestArgs\n ): ClientRequest {\n const clientRequestArgs = normalizeClientRequestArgs(\n `${protocol}:`,\n ...args\n )\n const request = new NodeClientRequest(clientRequestArgs, options)\n\n /**\n * @note https://nodejs.org/api/http.html#httpgetoptions-callback\n * \"http.get\" sets the method to \"GET\" and calls \"req.end()\" automatically.\n */\n request.end()\n\n return request\n }\n}\n","import { ClientRequest } from 'http'\nimport { Logger } from '@open-draft/logger'\nimport {\n NodeClientOptions,\n NodeClientRequest,\n Protocol,\n} from './NodeClientRequest'\nimport {\n normalizeClientRequestArgs,\n ClientRequestArgs,\n} from './utils/normalizeClientRequestArgs'\n\nconst logger = new Logger('http request')\n\nexport function request(protocol: Protocol, options: NodeClientOptions) {\n return function interceptorsHttpRequest(\n ...args: ClientRequestArgs\n ): ClientRequest {\n logger.info('request call (protocol \"%s\"):', protocol, args)\n\n const clientRequestArgs = normalizeClientRequestArgs(\n `${protocol}:`,\n ...args\n )\n return new NodeClientRequest(clientRequestArgs, options)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,OAAO,UAAU;AACjB,OAAO,WAAW;;;ACDlB,SAAS,eAAe,mBAAAA,kBAAiB,oBAAoB;AAE7D,SAAS,aAAa;AACtB,SAAS,uBAAuB;;;ACHhC,SAAS,cAAc;AAEvB,IAAM,SAAS,IAAI,OAAO,8BAA8B;AAqBjD,SAAS,iCACX,MAC6B;AAChC,SAAO,KAAK,aAAa,IAAI;AAC7B,QAAM,iBAAiB,IAAI,MAAM,CAAC,E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encoding","Logger","Logger","logger","Logger","logger","logger","Logger","url","options","request","Logger","logger","Logger","logger"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/RequestController.ts","../../src/utils/toInteractiveRequest.ts","../../src/utils/emitAsync.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\n\nexport class RequestController {\n public responsePromise: DeferredPromise<Response | undefined>\n\n constructor(protected request: Request) {\n this.responsePromise = new DeferredPromise()\n }\n\n public respondWith(response?: Response): void {\n invariant(\n this.responsePromise.state === 'pending',\n 'Failed to respond to \"%s %s\" request: the \"request\" event has already been responded to.',\n this.request.method,\n this.request.url\n )\n\n this.responsePromise.resolve(response)\n }\n}\n","import { RequestController } from './RequestController'\n\nexport type InteractiveRequest = globalThis.Request & {\n respondWith: RequestController['respondWith']\n}\n\nexport function toInteractiveRequest(request: Request): {\n interactiveRequest: InteractiveRequest\n requestController: RequestController\n} {\n const requestController = new RequestController(request)\n\n Reflect.set(\n request,\n 'respondWith',\n requestController.respondWith.bind(requestController)\n )\n\n return {\n interactiveRequest: request as InteractiveRequest,\n requestController,\n }\n}\n","import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listners = emitter.listeners(eventName)\n\n if (listners.length === 0) {\n return\n }\n\n for (const listener of listners) {\n await listener.apply(emitter, data)\n }\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAEzB,IAAM,oBAAN,MAAwB;AAAA,EAG7B,YAAsB,SAAkB;AAAlB;AACpB,SAAK,kBAAkB,IAAI,gBAAgB;AAAA,EAC7C;AAAA,EAEO,YAAY,UAA2B;AAC5C;AAAA,MACE,KAAK,gBAAgB,UAAU;AAAA,MAC/B;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,gBAAgB,QAAQ,QAAQ;AAAA,EACvC;AACF;;;ACdO,SAAS,qBAAqB,SAGnC;AACA,QAAM,oBAAoB,IAAI,kBAAkB,OAAO;AAEvD,UAAQ;AAAA,IACN;AAAA,IACA;AAAA,IACA,kBAAkB,YAAY,KAAK,iBAAiB;AAAA,EACtD;AAEA,SAAO;AAAA,IACL,oBAAoB;AAAA,IACpB;AAAA,EACF;AACF;;;ACfA,eAAsB,UAIpB,SACA,cACG,MACY;AACf,QAAM,WAAW,QAAQ,UAAU,SAAS;AAE5C,MAAI,SAAS,WAAW,GAAG;AACzB;AAAA,EACF;AAEA,aAAW,YAAY,UAAU;AAC/B,UAAM,SAAS,MAAM,SAAS,IAAI;AAAA,EACpC;AACF;","names":[]}
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { vi, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
|
-
import express from 'express'
|
|
3
|
-
import { IncomingMessage } from 'http'
|
|
4
|
-
import { Emitter } from 'strict-event-emitter'
|
|
5
|
-
import { Logger } from '@open-draft/logger'
|
|
6
|
-
import { HttpServer } from '@open-draft/test-server/http'
|
|
7
|
-
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
8
|
-
import { NodeClientRequest } from './NodeClientRequest'
|
|
9
|
-
import { getIncomingMessageBody } from './utils/getIncomingMessageBody'
|
|
10
|
-
import { normalizeClientRequestArgs } from './utils/normalizeClientRequestArgs'
|
|
11
|
-
import { sleep } from '../../../test/helpers'
|
|
12
|
-
import { HttpRequestEventMap } from '../../glossary'
|
|
13
|
-
|
|
14
|
-
const httpServer = new HttpServer((app) => {
|
|
15
|
-
app.post('/comment', (_req, res) => {
|
|
16
|
-
res.status(200).send('original-response')
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
app.post('/write', express.text(), (req, res) => {
|
|
20
|
-
res.status(200).send(req.body)
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const logger = new Logger('test')
|
|
25
|
-
|
|
26
|
-
beforeAll(async () => {
|
|
27
|
-
await httpServer.listen()
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
afterAll(async () => {
|
|
31
|
-
await httpServer.close()
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('gracefully finishes the request when it has a mocked response', async () => {
|
|
35
|
-
const emitter = new Emitter<HttpRequestEventMap>()
|
|
36
|
-
const request = new NodeClientRequest(
|
|
37
|
-
normalizeClientRequestArgs('http:', 'http://any.thing', {
|
|
38
|
-
method: 'PUT',
|
|
39
|
-
}),
|
|
40
|
-
{
|
|
41
|
-
emitter,
|
|
42
|
-
logger,
|
|
43
|
-
}
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
emitter.on('request', ({ request }) => {
|
|
47
|
-
request.respondWith(
|
|
48
|
-
new Response('mocked-response', {
|
|
49
|
-
status: 301,
|
|
50
|
-
headers: {
|
|
51
|
-
'x-custom-header': 'yes',
|
|
52
|
-
},
|
|
53
|
-
})
|
|
54
|
-
)
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
request.end()
|
|
58
|
-
|
|
59
|
-
const responseReceived = new DeferredPromise<IncomingMessage>()
|
|
60
|
-
|
|
61
|
-
request.on('response', async (response) => {
|
|
62
|
-
responseReceived.resolve(response)
|
|
63
|
-
})
|
|
64
|
-
const response = await responseReceived
|
|
65
|
-
|
|
66
|
-
// Request must be marked as finished as soon as it's sent.
|
|
67
|
-
expect(request.writableEnded).toBe(true)
|
|
68
|
-
expect(request.writableFinished).toBe(true)
|
|
69
|
-
expect(request.writableCorked).toBe(0)
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Consume the response body, which will handle the "data" and "end"
|
|
73
|
-
* events of the incoming message. After this point, the response is finished.
|
|
74
|
-
*/
|
|
75
|
-
const text = await getIncomingMessageBody(response)
|
|
76
|
-
|
|
77
|
-
// Response must be marked as finished as soon as its done.
|
|
78
|
-
expect(request['response'].complete).toBe(true)
|
|
79
|
-
|
|
80
|
-
expect(response.statusCode).toBe(301)
|
|
81
|
-
expect(response.headers).toHaveProperty('x-custom-header', 'yes')
|
|
82
|
-
expect(text).toBe('mocked-response')
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
it('responds with a mocked response when requesting an existing hostname', async () => {
|
|
86
|
-
const emitter = new Emitter<HttpRequestEventMap>()
|
|
87
|
-
const request = new NodeClientRequest(
|
|
88
|
-
normalizeClientRequestArgs('http:', httpServer.http.url('/comment')),
|
|
89
|
-
{
|
|
90
|
-
emitter,
|
|
91
|
-
logger,
|
|
92
|
-
}
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
emitter.on('request', ({ request }) => {
|
|
96
|
-
request.respondWith(new Response('mocked-response', { status: 201 }))
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
request.end()
|
|
100
|
-
|
|
101
|
-
const responseReceived = new DeferredPromise<IncomingMessage>()
|
|
102
|
-
request.on('response', async (response) => {
|
|
103
|
-
responseReceived.resolve(response)
|
|
104
|
-
})
|
|
105
|
-
const response = await responseReceived
|
|
106
|
-
|
|
107
|
-
expect(response.statusCode).toBe(201)
|
|
108
|
-
|
|
109
|
-
const text = await getIncomingMessageBody(response)
|
|
110
|
-
expect(text).toBe('mocked-response')
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
it('performs the request as-is given resolver returned no mocked response', async () => {
|
|
114
|
-
const emitter = new Emitter<HttpRequestEventMap>()
|
|
115
|
-
const request = new NodeClientRequest(
|
|
116
|
-
normalizeClientRequestArgs('http:', httpServer.http.url('/comment'), {
|
|
117
|
-
method: 'POST',
|
|
118
|
-
}),
|
|
119
|
-
{
|
|
120
|
-
emitter,
|
|
121
|
-
logger,
|
|
122
|
-
}
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
request.end()
|
|
126
|
-
|
|
127
|
-
const responseReceived = new DeferredPromise<IncomingMessage>()
|
|
128
|
-
request.on('response', async (response) => {
|
|
129
|
-
responseReceived.resolve(response)
|
|
130
|
-
})
|
|
131
|
-
const response = await responseReceived
|
|
132
|
-
|
|
133
|
-
expect(request.finished).toBe(true)
|
|
134
|
-
expect(request.writableEnded).toBe(true)
|
|
135
|
-
|
|
136
|
-
expect(response.statusCode).toBe(200)
|
|
137
|
-
expect(response.statusMessage).toBe('OK')
|
|
138
|
-
expect(response.headers).toHaveProperty('x-powered-by', 'Express')
|
|
139
|
-
|
|
140
|
-
const text = await getIncomingMessageBody(response)
|
|
141
|
-
expect(text).toBe('original-response')
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
it('sends the request body to the server given no mocked response', async () => {
|
|
145
|
-
const emitter = new Emitter<HttpRequestEventMap>()
|
|
146
|
-
const request = new NodeClientRequest(
|
|
147
|
-
normalizeClientRequestArgs('http:', httpServer.http.url('/write'), {
|
|
148
|
-
method: 'POST',
|
|
149
|
-
headers: {
|
|
150
|
-
'Content-Type': 'text/plain',
|
|
151
|
-
},
|
|
152
|
-
}),
|
|
153
|
-
{
|
|
154
|
-
emitter,
|
|
155
|
-
logger,
|
|
156
|
-
}
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
request.write('one')
|
|
160
|
-
request.write('two')
|
|
161
|
-
request.end('three')
|
|
162
|
-
|
|
163
|
-
const responseReceived = new DeferredPromise<IncomingMessage>()
|
|
164
|
-
request.on('response', (response) => {
|
|
165
|
-
responseReceived.resolve(response)
|
|
166
|
-
})
|
|
167
|
-
const response = await responseReceived
|
|
168
|
-
|
|
169
|
-
expect(response.statusCode).toBe(200)
|
|
170
|
-
|
|
171
|
-
const text = await getIncomingMessageBody(response)
|
|
172
|
-
expect(text).toBe('onetwothree')
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
it('does not send request body to the original server given mocked response', async () => {
|
|
176
|
-
const emitter = new Emitter<HttpRequestEventMap>()
|
|
177
|
-
const request = new NodeClientRequest(
|
|
178
|
-
normalizeClientRequestArgs('http:', httpServer.http.url('/write'), {
|
|
179
|
-
method: 'POST',
|
|
180
|
-
}),
|
|
181
|
-
{
|
|
182
|
-
emitter,
|
|
183
|
-
logger,
|
|
184
|
-
}
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
emitter.on('request', async ({ request }) => {
|
|
188
|
-
await sleep(200)
|
|
189
|
-
request.respondWith(new Response('mock created!', { status: 301 }))
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
request.write('one')
|
|
193
|
-
request.write('two')
|
|
194
|
-
request.end()
|
|
195
|
-
|
|
196
|
-
const responseReceived = new DeferredPromise<IncomingMessage>()
|
|
197
|
-
request.on('response', (response) => {
|
|
198
|
-
responseReceived.resolve(response)
|
|
199
|
-
})
|
|
200
|
-
const response = await responseReceived
|
|
201
|
-
|
|
202
|
-
expect(response.statusCode).toBe(301)
|
|
203
|
-
|
|
204
|
-
const text = await getIncomingMessageBody(response)
|
|
205
|
-
expect(text).toBe('mock created!')
|
|
206
|
-
})
|