@mswjs/interceptors 0.21.0 → 0.21.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.
@@ -3,7 +3,7 @@
3
3
  var _chunkKZJG2UW7js = require('./chunk-KZJG2UW7.js');
4
4
 
5
5
 
6
- var _chunkVKKFXSTLjs = require('./chunk-VKKFXSTL.js');
6
+ var _chunkUVNTVJHDjs = require('./chunk-UVNTVJHD.js');
7
7
 
8
8
 
9
9
  var _chunkHDUJCCWFjs = require('./chunk-HDUJCCWF.js');
@@ -27,7 +27,7 @@ var RemoteHttpInterceptor = class extends _chunkKZJG2UW7js.BatchInterceptor {
27
27
  super({
28
28
  name: "remote-interceptor",
29
29
  interceptors: [
30
- new (0, _chunkVKKFXSTLjs.ClientRequestInterceptor)(),
30
+ new (0, _chunkUVNTVJHDjs.ClientRequestInterceptor)(),
31
31
  new (0, _chunkHDUJCCWFjs.XMLHttpRequestInterceptor)()
32
32
  ]
33
33
  });
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-JISWS3Y3.mjs";
4
4
  import {
5
5
  ClientRequestInterceptor
6
- } from "./chunk-FCIAOT7W.mjs";
6
+ } from "./chunk-Z5JAVEOB.mjs";
7
7
  import {
8
8
  XMLHttpRequestInterceptor
9
9
  } from "./chunk-ZWCZGO3W.mjs";
@@ -141,6 +141,7 @@ function createRequest(clientRequest) {
141
141
  }
142
142
 
143
143
  // src/interceptors/ClientRequest/NodeClientRequest.ts
144
+ var _deferredpromise = require('@open-draft/deferred-promise');
144
145
  var _NodeClientRequest = class extends _http.ClientRequest {
145
146
  constructor([url, requestOptions, callback], options) {
146
147
  super(requestOptions, callback);
@@ -324,9 +325,13 @@ var _NodeClientRequest = class extends _http.ClientRequest {
324
325
  });
325
326
  }
326
327
  this.log("mocked response headers ready:", headers);
328
+ const isResponseStreamRead = new (0, _deferredpromise.DeferredPromise)();
327
329
  const closeResponseStream = () => {
330
+ this.log("closing response stream...");
328
331
  this.response.push(null);
329
332
  this.response.complete = true;
333
+ isResponseStreamRead.resolve();
334
+ this.log("closed response stream!");
330
335
  };
331
336
  if (body) {
332
337
  const bodyReader = body.getReader();
@@ -343,14 +348,16 @@ var _NodeClientRequest = class extends _http.ClientRequest {
343
348
  } else {
344
349
  closeResponseStream();
345
350
  }
346
- this.res = this.response;
347
- this.finished = true;
348
- Object.defineProperty(this, "writableEnded", {
349
- value: true
351
+ isResponseStreamRead.then(() => {
352
+ this.res = this.response;
353
+ this.finished = true;
354
+ Object.defineProperty(this, "writableEnded", {
355
+ value: true
356
+ });
357
+ this.emit("finish");
358
+ this.emit("response", this.response);
359
+ this.terminate();
350
360
  });
351
- this.emit("finish");
352
- this.emit("response", this.response);
353
- this.terminate();
354
361
  }
355
362
  terminate() {
356
363
  var _a;
@@ -141,6 +141,7 @@ function createRequest(clientRequest) {
141
141
  }
142
142
 
143
143
  // src/interceptors/ClientRequest/NodeClientRequest.ts
144
+ import { DeferredPromise } from "@open-draft/deferred-promise";
144
145
  var _NodeClientRequest = class extends ClientRequest {
145
146
  constructor([url, requestOptions, callback], options) {
146
147
  super(requestOptions, callback);
@@ -324,9 +325,13 @@ var _NodeClientRequest = class extends ClientRequest {
324
325
  });
325
326
  }
326
327
  this.log("mocked response headers ready:", headers);
328
+ const isResponseStreamRead = new DeferredPromise();
327
329
  const closeResponseStream = () => {
330
+ this.log("closing response stream...");
328
331
  this.response.push(null);
329
332
  this.response.complete = true;
333
+ isResponseStreamRead.resolve();
334
+ this.log("closed response stream!");
330
335
  };
331
336
  if (body) {
332
337
  const bodyReader = body.getReader();
@@ -343,14 +348,16 @@ var _NodeClientRequest = class extends ClientRequest {
343
348
  } else {
344
349
  closeResponseStream();
345
350
  }
346
- this.res = this.response;
347
- this.finished = true;
348
- Object.defineProperty(this, "writableEnded", {
349
- value: true
351
+ isResponseStreamRead.then(() => {
352
+ this.res = this.response;
353
+ this.finished = true;
354
+ Object.defineProperty(this, "writableEnded", {
355
+ value: true
356
+ });
357
+ this.emit("finish");
358
+ this.emit("response", this.response);
359
+ this.terminate();
350
360
  });
351
- this.emit("finish");
352
- this.emit("response", this.response);
353
- this.terminate();
354
361
  }
355
362
  terminate() {
356
363
  var _a;
@@ -1,9 +1,9 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkVKKFXSTLjs = require('../../chunk-VKKFXSTL.js');
3
+ var _chunkUVNTVJHDjs = require('../../chunk-UVNTVJHD.js');
4
4
  require('../../chunk-6V3JXLBF.js');
5
5
  require('../../chunk-ZJOF5MEZ.js');
6
6
  require('../../chunk-QMIXLBOU.js');
7
7
 
8
8
 
9
- exports.ClientRequestInterceptor = _chunkVKKFXSTLjs.ClientRequestInterceptor;
9
+ exports.ClientRequestInterceptor = _chunkUVNTVJHDjs.ClientRequestInterceptor;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ClientRequestInterceptor
3
- } from "../../chunk-FCIAOT7W.mjs";
3
+ } from "../../chunk-Z5JAVEOB.mjs";
4
4
  import "../../chunk-NNVTJLQA.mjs";
5
5
  import "../../chunk-STA6QBYM.mjs";
6
6
  import "../../chunk-ZSI7MX3V.mjs";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mswjs/interceptors",
3
3
  "description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
4
- "version": "0.21.0",
4
+ "version": "0.21.1",
5
5
  "main": "./lib/node/index.js",
6
6
  "module": "./lib/node/index.mjs",
7
7
  "types": "./lib/node/index.d.ts",
@@ -35,8 +35,8 @@
35
35
  "browser": {
36
36
  "types": "./lib/browser/interceptors/fetch/index.d.ts",
37
37
  "require": "./lib/browser/interceptors/fetch/index.js",
38
- "default": "./lib/browser/interceptors/fetch/index.js",
39
- "import": "./lib/browser/interceptors/fetch/index.mjs"
38
+ "import": "./lib/browser/interceptors/fetch/index.mjs",
39
+ "default": "./lib/browser/interceptors/fetch/index.js"
40
40
  },
41
41
  "types": "./lib/node/interceptors/fetch/index.d.ts",
42
42
  "require": "./lib/node/interceptors/fetch/index.js",
@@ -104,7 +104,7 @@
104
104
  "got": "^11.8.3",
105
105
  "jest": "^27.4.3",
106
106
  "node-fetch": "2.6.7",
107
- "page-with": "^0.5.1",
107
+ "page-with": "^0.6.0",
108
108
  "rimraf": "^3.0.2",
109
109
  "simple-git-hooks": "^2.7.0",
110
110
  "superagent": "^6.1.0",
@@ -116,6 +116,7 @@
116
116
  "web-encoding": "^1.1.5"
117
117
  },
118
118
  "dependencies": {
119
+ "@open-draft/deferred-promise": "^2.1.0",
119
120
  "@open-draft/until": "^1.0.3",
120
121
  "@types/debug": "^4.1.7",
121
122
  "debug": "^4.3.3",
@@ -123,6 +124,9 @@
123
124
  "outvariant": "^1.2.1",
124
125
  "strict-event-emitter": "^0.4.6"
125
126
  },
127
+ "resolutions": {
128
+ "memfs": "^3.4.13"
129
+ },
126
130
  "keywords": [
127
131
  "request",
128
132
  "intercept",
@@ -153,7 +153,7 @@ test('emits the ENOTFOUND error connecting to a non-existing hostname given no m
153
153
  test('emits the ECONNREFUSED error connecting to an inactive server given no mocked response', (done) => {
154
154
  const emitter = new AsyncEventEmitter<HttpRequestEventMap>()
155
155
  const request = new NodeClientRequest(
156
- normalizeClientRequestArgs('http:', 'http://localhost:12345'),
156
+ normalizeClientRequestArgs('http:', 'http://127.0.0.1:12345'),
157
157
  {
158
158
  emitter,
159
159
  log,
@@ -17,6 +17,7 @@ import { createResponse } from './utils/createResponse'
17
17
  import { createRequest } from './utils/createRequest'
18
18
  import { toInteractiveRequest } from '../../utils/toInteractiveRequest'
19
19
  import { uuidv4 } from '../../utils/uuid'
20
+ import { DeferredPromise } from '@open-draft/deferred-promise'
20
21
 
21
22
  export type Protocol = 'http' | 'https'
22
23
 
@@ -372,11 +373,18 @@ export class NodeClientRequest extends ClientRequest {
372
373
  }
373
374
  this.log('mocked response headers ready:', headers)
374
375
 
376
+ const isResponseStreamRead = new DeferredPromise<void>()
377
+
375
378
  const closeResponseStream = () => {
379
+ this.log('closing response stream...')
380
+
376
381
  // Push "null" to indicate that the response body is complete
377
382
  // and shouldn't be written to anymore.
378
383
  this.response.push(null)
379
384
  this.response.complete = true
385
+
386
+ isResponseStreamRead.resolve()
387
+ this.log('closed response stream!')
380
388
  }
381
389
 
382
390
  if (body) {
@@ -400,24 +408,26 @@ export class NodeClientRequest extends ClientRequest {
400
408
  closeResponseStream()
401
409
  }
402
410
 
403
- /**
404
- * Set the internal "res" property to the mocked "OutgoingMessage"
405
- * to make the "ClientRequest" instance think there's data received
406
- * from the socket.
407
- * @see https://github.com/nodejs/node/blob/9c405f2591f5833d0247ed0fafdcd68c5b14ce7a/lib/_http_client.js#L501
408
- */
409
- // @ts-ignore
410
- this.res = this.response
411
-
412
- this.finished = true
413
- Object.defineProperty(this, 'writableEnded', {
414
- value: true,
415
- })
411
+ isResponseStreamRead.then(() => {
412
+ /**
413
+ * Set the internal "res" property to the mocked "OutgoingMessage"
414
+ * to make the "ClientRequest" instance think there's data received
415
+ * from the socket.
416
+ * @see https://github.com/nodejs/node/blob/9c405f2591f5833d0247ed0fafdcd68c5b14ce7a/lib/_http_client.js#L501
417
+ */
418
+ // @ts-ignore
419
+ this.res = this.response
420
+
421
+ this.finished = true
422
+ Object.defineProperty(this, 'writableEnded', {
423
+ value: true,
424
+ })
416
425
 
417
- this.emit('finish')
418
- this.emit('response', this.response)
426
+ this.emit('finish')
427
+ this.emit('response', this.response)
419
428
 
420
- this.terminate()
429
+ this.terminate()
430
+ })
421
431
  }
422
432
 
423
433
  /**