@mswjs/interceptors 0.22.3 → 0.22.5

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.
Files changed (78) hide show
  1. package/README.md +3 -3
  2. package/lib/browser/{Interceptor-959f650e.d.ts → Interceptor-c794917d.d.ts} +2 -2
  3. package/lib/browser/{chunk-IIY3SHU3.js → chunk-2EIH6L6D.js} +25 -25
  4. package/lib/browser/{chunk-BQYA7ER5.mjs → chunk-2GVXYEMC.mjs} +5 -5
  5. package/lib/browser/chunk-2Z7B3HL5.js +106 -0
  6. package/lib/browser/{chunk-5B525MKF.mjs → chunk-65HGG3CV.mjs} +1 -1
  7. package/lib/browser/chunk-CNZUUPXM.mjs +682 -0
  8. package/lib/browser/chunk-GL6Y4E24.mjs +106 -0
  9. package/lib/browser/chunk-KDXWM46S.js +682 -0
  10. package/lib/browser/{chunk-QWL3EOEY.js → chunk-OK5YCL7L.js} +7 -7
  11. package/lib/browser/{chunk-ZJOF5MEZ.js → chunk-PCFJD76X.js} +11 -11
  12. package/lib/browser/{chunk-STA6QBYM.mjs → chunk-RT3ATOJH.mjs} +11 -11
  13. package/lib/browser/index.d.ts +6 -6
  14. package/lib/browser/index.js +4 -4
  15. package/lib/browser/index.mjs +2 -2
  16. package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +3 -3
  17. package/lib/browser/interceptors/XMLHttpRequest/index.js +5 -649
  18. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +5 -649
  19. package/lib/browser/interceptors/fetch/index.d.ts +2 -2
  20. package/lib/browser/interceptors/fetch/index.js +4 -90
  21. package/lib/browser/interceptors/fetch/index.mjs +4 -90
  22. package/lib/browser/presets/browser.d.ts +13 -0
  23. package/lib/browser/presets/browser.js +18 -0
  24. package/lib/browser/presets/browser.mjs +18 -0
  25. package/lib/node/{BatchInterceptor-a7261b26.d.ts → BatchInterceptor-5c1e5de3.d.ts} +5 -5
  26. package/lib/node/{Interceptor-997045eb.d.ts → Interceptor-b3a4098c.d.ts} +2 -2
  27. package/lib/node/RemoteHttpInterceptor.d.ts +3 -3
  28. package/lib/node/RemoteHttpInterceptor.js +9 -9
  29. package/lib/node/RemoteHttpInterceptor.mjs +5 -5
  30. package/lib/node/{chunk-FGPCRIW6.mjs → chunk-53LM4S3X.mjs} +59 -62
  31. package/lib/node/{chunk-WWHITCCI.js → chunk-62KFIM4W.js} +9 -8
  32. package/lib/node/{chunk-37CATPNG.mjs → chunk-6CRMKMDL.mjs} +6 -5
  33. package/lib/node/{chunk-XLZJAPVS.js → chunk-D7MOETL2.js} +59 -62
  34. package/lib/node/{chunk-CYWTKHFI.mjs → chunk-HGKO2BOA.mjs} +58 -30
  35. package/lib/node/{chunk-GGD5JOGB.js → chunk-HTLVOEKP.js} +62 -34
  36. package/lib/node/{chunk-KZEQH4YW.mjs → chunk-IC6Y7RGW.mjs} +1 -1
  37. package/lib/node/{chunk-SNNL2EXF.js → chunk-MUUQLKVJ.js} +3 -3
  38. package/lib/node/{chunk-G6ZTHYZQ.mjs → chunk-RFVEKRYP.mjs} +1 -1
  39. package/lib/node/{chunk-Q56TMOP5.js → chunk-SFLY7F52.js} +2 -2
  40. package/lib/node/index.d.ts +3 -3
  41. package/lib/node/index.js +4 -4
  42. package/lib/node/index.mjs +3 -3
  43. package/lib/node/interceptors/ClientRequest/index.d.ts +3 -3
  44. package/lib/node/interceptors/ClientRequest/index.js +3 -3
  45. package/lib/node/interceptors/ClientRequest/index.mjs +2 -2
  46. package/lib/node/interceptors/XMLHttpRequest/index.d.ts +3 -3
  47. package/lib/node/interceptors/XMLHttpRequest/index.js +4 -4
  48. package/lib/node/interceptors/XMLHttpRequest/index.mjs +3 -3
  49. package/lib/node/interceptors/fetch/index.d.ts +2 -2
  50. package/lib/node/interceptors/fetch/index.js +22 -11
  51. package/lib/node/interceptors/fetch/index.mjs +21 -10
  52. package/lib/node/presets/node.d.ts +15 -0
  53. package/lib/node/presets/node.js +19 -0
  54. package/lib/node/presets/node.mjs +19 -0
  55. package/package.json +17 -2
  56. package/src/BatchInterceptor.ts +5 -5
  57. package/src/Interceptor.ts +2 -2
  58. package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +1 -1
  59. package/src/interceptors/ClientRequest/http.request.ts +1 -1
  60. package/src/interceptors/ClientRequest/index.ts +2 -2
  61. package/src/interceptors/ClientRequest/utils/createRequest.test.ts +1 -1
  62. package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +1 -1
  63. package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +1 -1
  64. package/src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts +5 -3
  65. package/src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.ts +1 -1
  66. package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +30 -29
  67. package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +20 -2
  68. package/src/interceptors/XMLHttpRequest/index.ts +2 -2
  69. package/src/interceptors/XMLHttpRequest/utils/createResponse.ts +1 -1
  70. package/src/interceptors/fetch/index.ts +22 -9
  71. package/src/presets/browser.ts +4 -1
  72. package/src/presets/node.ts +4 -1
  73. package/src/utils/AsyncEventEmitter.ts +1 -1
  74. package/src/utils/cloneObject.ts +8 -6
  75. package/src/utils/createProxy.test.ts +149 -0
  76. package/src/utils/createProxy.ts +27 -6
  77. package/src/utils/debug.ts +4 -0
  78. package/src/utils/getUrlByRequestOptions.ts +13 -12
@@ -1,94 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
+ var _chunk2Z7B3HL5js = require('../../chunk-2Z7B3HL5.js');
4
+ require('../../chunk-PCFJD76X.js');
5
+ require('../../chunk-OK5YCL7L.js');
3
6
 
4
- var _chunkZJOF5MEZjs = require('../../chunk-ZJOF5MEZ.js');
5
7
 
6
-
7
-
8
- var _chunkQWL3EOEYjs = require('../../chunk-QWL3EOEY.js');
9
-
10
- // src/interceptors/fetch/index.ts
11
- var _outvariant = require('outvariant');
12
- var _FetchInterceptor = class extends _chunkQWL3EOEYjs.Interceptor {
13
- constructor() {
14
- super(_FetchInterceptor.symbol);
15
- }
16
- checkEnvironment() {
17
- return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
18
- }
19
- setup() {
20
- const pureFetch = globalThis.fetch;
21
- _outvariant.invariant.call(void 0,
22
- !pureFetch[_chunkQWL3EOEYjs.IS_PATCHED_MODULE],
23
- 'Failed to patch the "fetch" module: already patched.'
24
- );
25
- globalThis.fetch = async (input, init) => {
26
- const requestId = _chunkZJOF5MEZjs.uuidv4.call(void 0, );
27
- const request = new Request(input, init);
28
- this.log("[%s] %s", request.method, request.url);
29
- const interactiveRequest = _chunkZJOF5MEZjs.toInteractiveRequest.call(void 0, request);
30
- this.log(
31
- 'emitting the "request" event for %d listener(s)...',
32
- this.emitter.listenerCount("request")
33
- );
34
- this.emitter.emit("request", interactiveRequest, requestId);
35
- this.log("awaiting for the mocked response...");
36
- await this.emitter.untilIdle(
37
- "request",
38
- ({ args: [, pendingRequestId] }) => {
39
- return pendingRequestId === requestId;
40
- }
41
- );
42
- this.log("all request listeners have been resolved!");
43
- const [mockedResponse] = await interactiveRequest.respondWith.invoked();
44
- this.log("event.respondWith called with:", mockedResponse);
45
- if (mockedResponse) {
46
- this.log("received mocked response:", mockedResponse);
47
- const responseCloine = mockedResponse.clone();
48
- this.emitter.emit(
49
- "response",
50
- responseCloine,
51
- interactiveRequest,
52
- requestId
53
- );
54
- const response = new Response(mockedResponse.body, mockedResponse);
55
- Object.defineProperty(response, "url", {
56
- writable: false,
57
- enumerable: true,
58
- configurable: false,
59
- value: request.url
60
- });
61
- return response;
62
- }
63
- this.log("no mocked response received!");
64
- return pureFetch(request).then((response) => {
65
- const responseClone = response.clone();
66
- this.log("original fetch performed", responseClone);
67
- this.emitter.emit(
68
- "response",
69
- responseClone,
70
- interactiveRequest,
71
- requestId
72
- );
73
- return response;
74
- });
75
- };
76
- Object.defineProperty(globalThis.fetch, _chunkQWL3EOEYjs.IS_PATCHED_MODULE, {
77
- enumerable: true,
78
- configurable: true,
79
- value: true
80
- });
81
- this.subscriptions.push(() => {
82
- Object.defineProperty(globalThis.fetch, _chunkQWL3EOEYjs.IS_PATCHED_MODULE, {
83
- value: void 0
84
- });
85
- globalThis.fetch = pureFetch;
86
- this.log('restored native "globalThis.fetch"!', globalThis.fetch.name);
87
- });
88
- }
89
- };
90
- var FetchInterceptor = _FetchInterceptor;
91
- FetchInterceptor.symbol = Symbol("fetch");
92
-
93
-
94
- exports.FetchInterceptor = FetchInterceptor;
8
+ exports.FetchInterceptor = _chunk2Z7B3HL5js.FetchInterceptor;
@@ -1,94 +1,8 @@
1
1
  import {
2
- toInteractiveRequest,
3
- uuidv4
4
- } from "../../chunk-STA6QBYM.mjs";
5
- import {
6
- IS_PATCHED_MODULE,
7
- Interceptor
8
- } from "../../chunk-BQYA7ER5.mjs";
9
-
10
- // src/interceptors/fetch/index.ts
11
- import { invariant } from "outvariant";
12
- var _FetchInterceptor = class extends Interceptor {
13
- constructor() {
14
- super(_FetchInterceptor.symbol);
15
- }
16
- checkEnvironment() {
17
- return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
18
- }
19
- setup() {
20
- const pureFetch = globalThis.fetch;
21
- invariant(
22
- !pureFetch[IS_PATCHED_MODULE],
23
- 'Failed to patch the "fetch" module: already patched.'
24
- );
25
- globalThis.fetch = async (input, init) => {
26
- const requestId = uuidv4();
27
- const request = new Request(input, init);
28
- this.log("[%s] %s", request.method, request.url);
29
- const interactiveRequest = toInteractiveRequest(request);
30
- this.log(
31
- 'emitting the "request" event for %d listener(s)...',
32
- this.emitter.listenerCount("request")
33
- );
34
- this.emitter.emit("request", interactiveRequest, requestId);
35
- this.log("awaiting for the mocked response...");
36
- await this.emitter.untilIdle(
37
- "request",
38
- ({ args: [, pendingRequestId] }) => {
39
- return pendingRequestId === requestId;
40
- }
41
- );
42
- this.log("all request listeners have been resolved!");
43
- const [mockedResponse] = await interactiveRequest.respondWith.invoked();
44
- this.log("event.respondWith called with:", mockedResponse);
45
- if (mockedResponse) {
46
- this.log("received mocked response:", mockedResponse);
47
- const responseCloine = mockedResponse.clone();
48
- this.emitter.emit(
49
- "response",
50
- responseCloine,
51
- interactiveRequest,
52
- requestId
53
- );
54
- const response = new Response(mockedResponse.body, mockedResponse);
55
- Object.defineProperty(response, "url", {
56
- writable: false,
57
- enumerable: true,
58
- configurable: false,
59
- value: request.url
60
- });
61
- return response;
62
- }
63
- this.log("no mocked response received!");
64
- return pureFetch(request).then((response) => {
65
- const responseClone = response.clone();
66
- this.log("original fetch performed", responseClone);
67
- this.emitter.emit(
68
- "response",
69
- responseClone,
70
- interactiveRequest,
71
- requestId
72
- );
73
- return response;
74
- });
75
- };
76
- Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
77
- enumerable: true,
78
- configurable: true,
79
- value: true
80
- });
81
- this.subscriptions.push(() => {
82
- Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
83
- value: void 0
84
- });
85
- globalThis.fetch = pureFetch;
86
- this.log('restored native "globalThis.fetch"!', globalThis.fetch.name);
87
- });
88
- }
89
- };
90
- var FetchInterceptor = _FetchInterceptor;
91
- FetchInterceptor.symbol = Symbol("fetch");
2
+ FetchInterceptor
3
+ } from "../../chunk-GL6Y4E24.mjs";
4
+ import "../../chunk-RT3ATOJH.mjs";
5
+ import "../../chunk-2GVXYEMC.mjs";
92
6
  export {
93
7
  FetchInterceptor
94
8
  };
@@ -0,0 +1,13 @@
1
+ import { FetchInterceptor } from '../interceptors/fetch/index.js';
2
+ import { XMLHttpRequestInterceptor } from '../interceptors/XMLHttpRequest/index.js';
3
+ import '../Interceptor-c794917d.js';
4
+ import 'strict-event-emitter';
5
+ import 'debug';
6
+
7
+ /**
8
+ * The default preset provisions the interception of requests
9
+ * regardless of their type (fetch/XMLHttpRequest).
10
+ */
11
+ declare const _default: readonly [FetchInterceptor, XMLHttpRequestInterceptor];
12
+
13
+ export { _default as default };
@@ -0,0 +1,18 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkKDXWM46Sjs = require('../chunk-KDXWM46S.js');
4
+ require('../chunk-2EIH6L6D.js');
5
+
6
+
7
+ var _chunk2Z7B3HL5js = require('../chunk-2Z7B3HL5.js');
8
+ require('../chunk-PCFJD76X.js');
9
+ require('../chunk-OK5YCL7L.js');
10
+
11
+ // src/presets/browser.ts
12
+ var browser_default = [
13
+ new (0, _chunk2Z7B3HL5js.FetchInterceptor)(),
14
+ new (0, _chunkKDXWM46Sjs.XMLHttpRequestInterceptor)()
15
+ ];
16
+
17
+
18
+ exports.default = browser_default;
@@ -0,0 +1,18 @@
1
+ import {
2
+ XMLHttpRequestInterceptor
3
+ } from "../chunk-CNZUUPXM.mjs";
4
+ import "../chunk-65HGG3CV.mjs";
5
+ import {
6
+ FetchInterceptor
7
+ } from "../chunk-GL6Y4E24.mjs";
8
+ import "../chunk-RT3ATOJH.mjs";
9
+ import "../chunk-2GVXYEMC.mjs";
10
+
11
+ // src/presets/browser.ts
12
+ var browser_default = [
13
+ new FetchInterceptor(),
14
+ new XMLHttpRequestInterceptor()
15
+ ];
16
+ export {
17
+ browser_default as default
18
+ };
@@ -1,17 +1,17 @@
1
1
  import { EventMap, Listener } from 'strict-event-emitter';
2
- import { e as Interceptor, E as ExtractEventNames } from './Interceptor-997045eb.js';
2
+ import { e as Interceptor, E as ExtractEventNames } from './Interceptor-b3a4098c.js';
3
3
 
4
- interface BatchInterceptorOptions<InterceptorList extends Array<Interceptor<any>>> {
4
+ interface BatchInterceptorOptions<InterceptorList extends ReadonlyArray<Interceptor<any>>> {
5
5
  name: string;
6
6
  interceptors: InterceptorList;
7
7
  }
8
- type ExtractEventMapType<InterceptorList extends Array<Interceptor<any>>> = InterceptorList extends Array<infer InterceptorType> ? InterceptorType extends Interceptor<infer EventMap> ? EventMap : never : never;
8
+ type ExtractEventMapType<InterceptorList extends ReadonlyArray<Interceptor<any>>> = InterceptorList extends ReadonlyArray<infer InterceptorType> ? InterceptorType extends Interceptor<infer EventMap> ? EventMap : never : never;
9
9
  /**
10
10
  * A batch interceptor that exposes a single interface
11
11
  * to apply and operate with multiple interceptors at once.
12
12
  */
13
- declare class BatchInterceptor<InterceptorList extends Array<Interceptor<any>>, Events extends EventMap = ExtractEventMapType<InterceptorList>> extends Interceptor<Events> {
14
- static symbol: Symbol;
13
+ declare class BatchInterceptor<InterceptorList extends ReadonlyArray<Interceptor<any>>, Events extends EventMap = ExtractEventMapType<InterceptorList>> extends Interceptor<Events> {
14
+ static symbol: symbol;
15
15
  private interceptors;
16
16
  constructor(options: BatchInterceptorOptions<InterceptorList>);
17
17
  protected setup(): void;
@@ -1,5 +1,5 @@
1
- import { Debugger } from 'debug';
2
1
  import { EventMap, Emitter, Listener } from 'strict-event-emitter';
2
+ import { Debugger } from 'debug';
3
3
 
4
4
  type AnyFunction = (...args: any[]) => any;
5
5
  type LazyCallbackReturnType<FnType extends AnyFunction> = Parameters<FnType> | [];
@@ -71,7 +71,7 @@ declare class Interceptor<Events extends InterceptorEventMap> {
71
71
  protected subscriptions: Array<InterceptorSubscription>;
72
72
  protected log: Debugger;
73
73
  readyState: InterceptorReadyState;
74
- constructor(symbol: Symbol);
74
+ constructor(symbol: symbol);
75
75
  /**
76
76
  * Determine if this interceptor can be applied
77
77
  * in the current environment.
@@ -1,11 +1,11 @@
1
1
  import { ChildProcess } from 'child_process';
2
2
  import { HeadersObject } from 'headers-polyfill';
3
- import { e as Interceptor, H as HttpRequestEventMap } from './Interceptor-997045eb.js';
4
- import { a as BatchInterceptor } from './BatchInterceptor-a7261b26.js';
3
+ import { e as Interceptor, H as HttpRequestEventMap } from './Interceptor-b3a4098c.js';
4
+ import { a as BatchInterceptor } from './BatchInterceptor-5c1e5de3.js';
5
5
  import { ClientRequestInterceptor } from './interceptors/ClientRequest/index.js';
6
6
  import { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest/index.js';
7
- import 'debug';
8
7
  import 'strict-event-emitter';
8
+ import 'debug';
9
9
  import 'http';
10
10
  import 'https';
11
11
 
@@ -1,30 +1,30 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkQ56TMOP5js = require('./chunk-Q56TMOP5.js');
3
+ var _chunkSFLY7F52js = require('./chunk-SFLY7F52.js');
4
4
 
5
5
 
6
- var _chunkXLZJAPVSjs = require('./chunk-XLZJAPVS.js');
6
+ var _chunkD7MOETL2js = require('./chunk-D7MOETL2.js');
7
7
 
8
8
 
9
- var _chunkGGD5JOGBjs = require('./chunk-GGD5JOGB.js');
10
- require('./chunk-SNNL2EXF.js');
9
+ var _chunkHTLVOEKPjs = require('./chunk-HTLVOEKP.js');
10
+ require('./chunk-MUUQLKVJ.js');
11
11
  require('./chunk-VQ4DZOBB.js');
12
12
 
13
13
 
14
14
  var _chunkZJOF5MEZjs = require('./chunk-ZJOF5MEZ.js');
15
15
 
16
16
 
17
- var _chunkWWHITCCIjs = require('./chunk-WWHITCCI.js');
17
+ var _chunk62KFIM4Wjs = require('./chunk-62KFIM4W.js');
18
18
 
19
19
  // src/RemoteHttpInterceptor.ts
20
20
  var _headerspolyfill = require('headers-polyfill');
21
- var RemoteHttpInterceptor = class extends _chunkQ56TMOP5js.BatchInterceptor {
21
+ var RemoteHttpInterceptor = class extends _chunkSFLY7F52js.BatchInterceptor {
22
22
  constructor() {
23
23
  super({
24
24
  name: "remote-interceptor",
25
25
  interceptors: [
26
- new (0, _chunkXLZJAPVSjs.ClientRequestInterceptor)(),
27
- new (0, _chunkGGD5JOGBjs.XMLHttpRequestInterceptor)()
26
+ new (0, _chunkD7MOETL2js.ClientRequestInterceptor)(),
27
+ new (0, _chunkHTLVOEKPjs.XMLHttpRequestInterceptor)()
28
28
  ]
29
29
  });
30
30
  }
@@ -88,7 +88,7 @@ function requestReviver(key, value) {
88
88
  return value;
89
89
  }
90
90
  }
91
- var _RemoteHttpResolver = class extends _chunkWWHITCCIjs.Interceptor {
91
+ var _RemoteHttpResolver = class extends _chunk62KFIM4Wjs.Interceptor {
92
92
  constructor(options) {
93
93
  super(_RemoteHttpResolver.symbol);
94
94
  this.process = options.process;
@@ -1,20 +1,20 @@
1
1
  import {
2
2
  BatchInterceptor
3
- } from "./chunk-KZEQH4YW.mjs";
3
+ } from "./chunk-IC6Y7RGW.mjs";
4
4
  import {
5
5
  ClientRequestInterceptor
6
- } from "./chunk-FGPCRIW6.mjs";
6
+ } from "./chunk-53LM4S3X.mjs";
7
7
  import {
8
8
  XMLHttpRequestInterceptor
9
- } from "./chunk-CYWTKHFI.mjs";
10
- import "./chunk-G6ZTHYZQ.mjs";
9
+ } from "./chunk-HGKO2BOA.mjs";
10
+ import "./chunk-RFVEKRYP.mjs";
11
11
  import "./chunk-GFH37L5D.mjs";
12
12
  import {
13
13
  toInteractiveRequest
14
14
  } from "./chunk-STA6QBYM.mjs";
15
15
  import {
16
16
  Interceptor
17
- } from "./chunk-37CATPNG.mjs";
17
+ } from "./chunk-6CRMKMDL.mjs";
18
18
 
19
19
  // src/RemoteHttpInterceptor.ts
20
20
  import { Headers, headersToObject } from "headers-polyfill";