@mswjs/interceptors
Advanced tools
Comparing version 0.30.1 to 0.31.0
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); | ||
var _chunkIJFRWSLWjs = require('../../chunk-IJFRWSLW.js'); | ||
var _chunkETCNJNDTjs = require('../../chunk-ETCNJNDT.js'); | ||
require('../../chunk-MQJ3JOOK.js'); | ||
@@ -8,3 +8,3 @@ require('../../chunk-E4AC7YAC.js'); | ||
exports.ClientRequestInterceptor = _chunkIJFRWSLWjs.ClientRequestInterceptor; | ||
exports.ClientRequestInterceptor = _chunkETCNJNDTjs.ClientRequestInterceptor; | ||
//# sourceMappingURL=index.js.map |
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); | ||
var _chunkIJFRWSLWjs = require('../chunk-IJFRWSLW.js'); | ||
var _chunkETCNJNDTjs = require('../chunk-ETCNJNDT.js'); | ||
@@ -14,3 +14,3 @@ | ||
var node_default = [ | ||
new (0, _chunkIJFRWSLWjs.ClientRequestInterceptor)(), | ||
new (0, _chunkETCNJNDTjs.ClientRequestInterceptor)(), | ||
new (0, _chunkAPT7KA3Bjs.XMLHttpRequestInterceptor)() | ||
@@ -17,0 +17,0 @@ ]; |
@@ -6,3 +6,3 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); | ||
var _chunkIJFRWSLWjs = require('./chunk-IJFRWSLW.js'); | ||
var _chunkETCNJNDTjs = require('./chunk-ETCNJNDT.js'); | ||
@@ -27,3 +27,3 @@ | ||
interceptors: [ | ||
new (0, _chunkIJFRWSLWjs.ClientRequestInterceptor)(), | ||
new (0, _chunkETCNJNDTjs.ClientRequestInterceptor)(), | ||
new (0, _chunkAPT7KA3Bjs.XMLHttpRequestInterceptor)() | ||
@@ -30,0 +30,0 @@ ] |
{ | ||
"name": "@mswjs/interceptors", | ||
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.", | ||
"version": "0.30.1", | ||
"version": "0.31.0", | ||
"main": "./lib/node/index.js", | ||
@@ -6,0 +6,0 @@ "module": "./lib/node/index.mjs", |
@@ -370,3 +370,4 @@ import { ClientRequest, IncomingMessage, STATUS_CODES } from 'node:http' | ||
this.logger.info('emitting the custom "response" event...') | ||
this.emitter.emit('response', { | ||
const responseListenersPromise = emitAsync(this.emitter, 'response', { | ||
response: responseClone, | ||
@@ -378,4 +379,10 @@ isMockedResponse: true, | ||
this.logger.info('request (mock) is completed') | ||
responseListenersPromise.then(() => { | ||
this.logger.info('request (mock) is completed') | ||
}) | ||
// Defer the end of the response until all the response | ||
// event listeners are done (those can be async). | ||
this.deferResponseEndUntil(responseListenersPromise, this.response) | ||
return this | ||
@@ -386,15 +393,23 @@ } | ||
this.once('response-internal', (message: IncomingMessage) => { | ||
this.logger.info(message.statusCode, message.statusMessage) | ||
this.logger.info('original response headers:', message.headers) | ||
this.once( | ||
'response-internal', | ||
(message: IncomingMessage, originalMessage: IncomingMessage) => { | ||
this.logger.info(message.statusCode, message.statusMessage) | ||
this.logger.info('original response headers:', message.headers) | ||
this.logger.info('emitting the custom "response" event...') | ||
this.emitter.emit('response', { | ||
response: createResponse(message), | ||
isMockedResponse: false, | ||
request: capturedRequest, | ||
requestId, | ||
}) | ||
}) | ||
this.logger.info('emitting the custom "response" event...') | ||
const responseListenersPromise = emitAsync(this.emitter, 'response', { | ||
response: createResponse(message), | ||
isMockedResponse: false, | ||
request: capturedRequest, | ||
requestId, | ||
}) | ||
// Defer the end of the response until all the response | ||
// event listeners are done (those can be async). | ||
this.deferResponseEndUntil(responseListenersPromise, originalMessage) | ||
} | ||
) | ||
return this.passthrough(chunk, encoding, callback) | ||
@@ -425,3 +440,3 @@ }) | ||
this.emit('response-internal', secondClone) | ||
this.emit('response-internal', secondClone, firstClone) | ||
@@ -650,2 +665,21 @@ this.logger.info( | ||
} | ||
private deferResponseEndUntil( | ||
promise: Promise<unknown>, | ||
response: IncomingMessage | ||
): void { | ||
response.emit = new Proxy(response.emit, { | ||
apply(target, thisArg, args) { | ||
const [event] = args | ||
const callEmit = () => Reflect.apply(target, thisArg, args) | ||
if (event === 'end') { | ||
promise.finally(() => callEmit()) | ||
return | ||
} | ||
return callEmit() | ||
}, | ||
}) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1174382
15567