@mswjs/interceptors 0.22.2 → 0.22.4

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.
@@ -121,12 +121,28 @@ function optionsToProxyHandler(options) {
121
121
  return constructorCall.call(newTarget, args, next);
122
122
  };
123
123
  }
124
- if (typeof setProperty !== "undefined") {
125
- handler.set = function(target, propertyName, nextValue, receiver) {
126
- const next = () => Reflect.set(target, propertyName, nextValue, receiver);
127
- return setProperty.call(target, [propertyName, nextValue], next);
124
+ handler.set = function(target, propertyName, nextValue, receiver) {
125
+ const next = () => {
126
+ const ownDescriptors = Reflect.getOwnPropertyDescriptor(
127
+ target,
128
+ propertyName
129
+ );
130
+ if (typeof (ownDescriptors == null ? void 0 : ownDescriptors.set) !== "undefined") {
131
+ ownDescriptors.set.apply(target, [nextValue]);
132
+ return true;
133
+ }
134
+ return Reflect.defineProperty(target, propertyName, {
135
+ writable: true,
136
+ enumerable: true,
137
+ configurable: true,
138
+ value: nextValue
139
+ });
128
140
  };
129
- }
141
+ if (typeof setProperty !== "undefined") {
142
+ return setProperty.call(target, [propertyName, nextValue], next);
143
+ }
144
+ return next();
145
+ };
130
146
  handler.get = function(target, propertyName, receiver) {
131
147
  const next = () => target[propertyName];
132
148
  const value = typeof getProperty !== "undefined" ? getProperty.call(target, [propertyName, receiver], next) : next();
@@ -191,11 +207,25 @@ var XMLHttpRequestController = class {
191
207
  this.responseBuffer = new Uint8Array();
192
208
  this.request = createProxy(initialRequest, {
193
209
  setProperty: ([propertyName, nextValue], invoke) => {
194
- if (propertyName === "withCredentials") {
195
- define(this.request, "withCredentials", nextValue);
196
- return true;
210
+ switch (propertyName) {
211
+ case "onabort":
212
+ case "onerror":
213
+ case "onload":
214
+ case "onloadend":
215
+ case "onloadstart":
216
+ case "onprogress":
217
+ case "ontimeout":
218
+ case "onreadystatechange": {
219
+ const eventName = propertyName.slice(
220
+ 2
221
+ );
222
+ this.request.addEventListener(eventName, nextValue);
223
+ return invoke();
224
+ }
225
+ default: {
226
+ return invoke();
227
+ }
197
228
  }
198
- return invoke();
199
229
  },
200
230
  methodCall: ([methodName, args], invoke) => {
201
231
  var _a;
@@ -259,21 +289,6 @@ var XMLHttpRequestController = class {
259
289
  });
260
290
  break;
261
291
  }
262
- case "onabort":
263
- case "onerror":
264
- case "onload":
265
- case "onloadend":
266
- case "onloadstart":
267
- case "onprogress":
268
- case "ontimeout":
269
- case "onreadystatechange": {
270
- const [listener] = args;
271
- this.registerEvent(
272
- methodName,
273
- listener
274
- );
275
- return invoke();
276
- }
277
292
  default: {
278
293
  return invoke();
279
294
  }
@@ -326,14 +341,17 @@ var XMLHttpRequestController = class {
326
341
  Object.defineProperties(this.request, {
327
342
  response: {
328
343
  enumerable: true,
344
+ configurable: false,
329
345
  get: () => this.response
330
346
  },
331
347
  responseText: {
332
348
  enumerable: true,
349
+ configurable: false,
333
350
  get: () => this.responseText
334
351
  },
335
352
  responseXML: {
336
353
  enumerable: true,
354
+ configurable: false,
337
355
  get: () => this.responseXML
338
356
  }
339
357
  });
@@ -547,9 +565,19 @@ function createXMLHttpRequestProxy({
547
565
  log
548
566
  }) {
549
567
  const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {
550
- construct(target, args) {
568
+ construct(target, args, newTarget) {
551
569
  log("constructed new XMLHttpRequest");
552
- const originalRequest = Reflect.construct(target, args);
570
+ const originalRequest = Reflect.construct(target, args, newTarget);
571
+ const prototypeDescriptors = Object.getOwnPropertyDescriptors(
572
+ target.prototype
573
+ );
574
+ for (const propertyName in prototypeDescriptors) {
575
+ Reflect.defineProperty(
576
+ originalRequest,
577
+ propertyName,
578
+ prototypeDescriptors[propertyName]
579
+ );
580
+ }
553
581
  const requestController = new XMLHttpRequestController(
554
582
  originalRequest,
555
583
  log
@@ -121,12 +121,28 @@ function optionsToProxyHandler(options) {
121
121
  return constructorCall.call(newTarget, args, next);
122
122
  };
123
123
  }
124
- if (typeof setProperty !== "undefined") {
125
- handler.set = function(target, propertyName, nextValue, receiver) {
126
- const next = () => Reflect.set(target, propertyName, nextValue, receiver);
127
- return setProperty.call(target, [propertyName, nextValue], next);
124
+ handler.set = function(target, propertyName, nextValue, receiver) {
125
+ const next = () => {
126
+ const ownDescriptors = Reflect.getOwnPropertyDescriptor(
127
+ target,
128
+ propertyName
129
+ );
130
+ if (typeof (ownDescriptors == null ? void 0 : ownDescriptors.set) !== "undefined") {
131
+ ownDescriptors.set.apply(target, [nextValue]);
132
+ return true;
133
+ }
134
+ return Reflect.defineProperty(target, propertyName, {
135
+ writable: true,
136
+ enumerable: true,
137
+ configurable: true,
138
+ value: nextValue
139
+ });
128
140
  };
129
- }
141
+ if (typeof setProperty !== "undefined") {
142
+ return setProperty.call(target, [propertyName, nextValue], next);
143
+ }
144
+ return next();
145
+ };
130
146
  handler.get = function(target, propertyName, receiver) {
131
147
  const next = () => target[propertyName];
132
148
  const value = typeof getProperty !== "undefined" ? getProperty.call(target, [propertyName, receiver], next) : next();
@@ -191,11 +207,25 @@ var XMLHttpRequestController = class {
191
207
  this.responseBuffer = new Uint8Array();
192
208
  this.request = createProxy(initialRequest, {
193
209
  setProperty: ([propertyName, nextValue], invoke) => {
194
- if (propertyName === "withCredentials") {
195
- define(this.request, "withCredentials", nextValue);
196
- return true;
210
+ switch (propertyName) {
211
+ case "onabort":
212
+ case "onerror":
213
+ case "onload":
214
+ case "onloadend":
215
+ case "onloadstart":
216
+ case "onprogress":
217
+ case "ontimeout":
218
+ case "onreadystatechange": {
219
+ const eventName = propertyName.slice(
220
+ 2
221
+ );
222
+ this.request.addEventListener(eventName, nextValue);
223
+ return invoke();
224
+ }
225
+ default: {
226
+ return invoke();
227
+ }
197
228
  }
198
- return invoke();
199
229
  },
200
230
  methodCall: ([methodName, args], invoke) => {
201
231
  var _a;
@@ -259,21 +289,6 @@ var XMLHttpRequestController = class {
259
289
  });
260
290
  break;
261
291
  }
262
- case "onabort":
263
- case "onerror":
264
- case "onload":
265
- case "onloadend":
266
- case "onloadstart":
267
- case "onprogress":
268
- case "ontimeout":
269
- case "onreadystatechange": {
270
- const [listener] = args;
271
- this.registerEvent(
272
- methodName,
273
- listener
274
- );
275
- return invoke();
276
- }
277
292
  default: {
278
293
  return invoke();
279
294
  }
@@ -326,14 +341,17 @@ var XMLHttpRequestController = class {
326
341
  Object.defineProperties(this.request, {
327
342
  response: {
328
343
  enumerable: true,
344
+ configurable: false,
329
345
  get: () => this.response
330
346
  },
331
347
  responseText: {
332
348
  enumerable: true,
349
+ configurable: false,
333
350
  get: () => this.responseText
334
351
  },
335
352
  responseXML: {
336
353
  enumerable: true,
354
+ configurable: false,
337
355
  get: () => this.responseXML
338
356
  }
339
357
  });
@@ -547,9 +565,19 @@ function createXMLHttpRequestProxy({
547
565
  log
548
566
  }) {
549
567
  const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {
550
- construct(target, args) {
568
+ construct(target, args, newTarget) {
551
569
  log("constructed new XMLHttpRequest");
552
- const originalRequest = Reflect.construct(target, args);
570
+ const originalRequest = Reflect.construct(target, args, newTarget);
571
+ const prototypeDescriptors = Object.getOwnPropertyDescriptors(
572
+ target.prototype
573
+ );
574
+ for (const propertyName in prototypeDescriptors) {
575
+ Reflect.defineProperty(
576
+ originalRequest,
577
+ propertyName,
578
+ prototypeDescriptors[propertyName]
579
+ );
580
+ }
553
581
  const requestController = new XMLHttpRequestController(
554
582
  originalRequest,
555
583
  log
@@ -9,6 +9,7 @@ var _chunkQWL3EOEYjs = require('../../chunk-QWL3EOEY.js');
9
9
 
10
10
  // src/interceptors/fetch/index.ts
11
11
  var _outvariant = require('outvariant');
12
+ var _until = require('@open-draft/until');
12
13
  var _FetchInterceptor = class extends _chunkQWL3EOEYjs.Interceptor {
13
14
  constructor() {
14
15
  super(_FetchInterceptor.symbol);
@@ -33,15 +34,25 @@ var _FetchInterceptor = class extends _chunkQWL3EOEYjs.Interceptor {
33
34
  );
34
35
  this.emitter.emit("request", interactiveRequest, requestId);
35
36
  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);
37
+ const [middlewareException, mockedResponse] = await _until.until.call(void 0, async () => {
38
+ await this.emitter.untilIdle(
39
+ "request",
40
+ ({ args: [, pendingRequestId] }) => {
41
+ return pendingRequestId === requestId;
42
+ }
43
+ );
44
+ this.log("all request listeners have been resolved!");
45
+ const [mockedResponse2] = await interactiveRequest.respondWith.invoked();
46
+ this.log("event.respondWith called with:", mockedResponse2);
47
+ return mockedResponse2;
48
+ });
49
+ if (middlewareException) {
50
+ console.error(`${request.method} ${request.url} net::ERR_FAILED`);
51
+ const error = Object.assign(new TypeError("Failed to fetch"), {
52
+ cause: middlewareException
53
+ });
54
+ return Promise.reject(error);
55
+ }
45
56
  if (mockedResponse) {
46
57
  this.log("received mocked response:", mockedResponse);
47
58
  const responseCloine = mockedResponse.clone();
@@ -9,6 +9,7 @@ import {
9
9
 
10
10
  // src/interceptors/fetch/index.ts
11
11
  import { invariant } from "outvariant";
12
+ import { until } from "@open-draft/until";
12
13
  var _FetchInterceptor = class extends Interceptor {
13
14
  constructor() {
14
15
  super(_FetchInterceptor.symbol);
@@ -33,15 +34,25 @@ var _FetchInterceptor = class extends Interceptor {
33
34
  );
34
35
  this.emitter.emit("request", interactiveRequest, requestId);
35
36
  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);
37
+ const [middlewareException, mockedResponse] = await until(async () => {
38
+ await this.emitter.untilIdle(
39
+ "request",
40
+ ({ args: [, pendingRequestId] }) => {
41
+ return pendingRequestId === requestId;
42
+ }
43
+ );
44
+ this.log("all request listeners have been resolved!");
45
+ const [mockedResponse2] = await interactiveRequest.respondWith.invoked();
46
+ this.log("event.respondWith called with:", mockedResponse2);
47
+ return mockedResponse2;
48
+ });
49
+ if (middlewareException) {
50
+ console.error(`${request.method} ${request.url} net::ERR_FAILED`);
51
+ const error = Object.assign(new TypeError("Failed to fetch"), {
52
+ cause: middlewareException
53
+ });
54
+ return Promise.reject(error);
55
+ }
45
56
  if (mockedResponse) {
46
57
  this.log("received mocked response:", mockedResponse);
47
58
  const responseCloine = mockedResponse.clone();
@@ -3,10 +3,10 @@
3
3
  var _chunkQ56TMOP5js = require('./chunk-Q56TMOP5.js');
4
4
 
5
5
 
6
- var _chunkPRX3F52Mjs = require('./chunk-PRX3F52M.js');
6
+ var _chunkXLZJAPVSjs = require('./chunk-XLZJAPVS.js');
7
7
 
8
8
 
9
- var _chunkGGD5JOGBjs = require('./chunk-GGD5JOGB.js');
9
+ var _chunkRVLLS44Wjs = require('./chunk-RVLLS44W.js');
10
10
  require('./chunk-SNNL2EXF.js');
11
11
  require('./chunk-VQ4DZOBB.js');
12
12
 
@@ -23,8 +23,8 @@ var RemoteHttpInterceptor = class extends _chunkQ56TMOP5js.BatchInterceptor {
23
23
  super({
24
24
  name: "remote-interceptor",
25
25
  interceptors: [
26
- new (0, _chunkPRX3F52Mjs.ClientRequestInterceptor)(),
27
- new (0, _chunkGGD5JOGBjs.XMLHttpRequestInterceptor)()
26
+ new (0, _chunkXLZJAPVSjs.ClientRequestInterceptor)(),
27
+ new (0, _chunkRVLLS44Wjs.XMLHttpRequestInterceptor)()
28
28
  ]
29
29
  });
30
30
  }
@@ -3,10 +3,10 @@ import {
3
3
  } from "./chunk-KZEQH4YW.mjs";
4
4
  import {
5
5
  ClientRequestInterceptor
6
- } from "./chunk-SWJ33XIS.mjs";
6
+ } from "./chunk-FGPCRIW6.mjs";
7
7
  import {
8
8
  XMLHttpRequestInterceptor
9
- } from "./chunk-CYWTKHFI.mjs";
9
+ } from "./chunk-P7NKDCFD.mjs";
10
10
  import "./chunk-G6ZTHYZQ.mjs";
11
11
  import "./chunk-GFH37L5D.mjs";
12
12
  import {
@@ -404,7 +404,6 @@ var debug3 = __require("debug")("utils getUrlByRequestOptions");
404
404
  var DEFAULT_PATH = "/";
405
405
  var DEFAULT_PROTOCOL = "http:";
406
406
  var DEFAULT_HOST = "localhost";
407
- var DEFAULT_PORT = 80;
408
407
  var SSL_PORT = 443;
409
408
  function getAgent(options) {
410
409
  return options.agent instanceof Agent ? options.agent : void 0;
@@ -424,16 +423,30 @@ function getProtocolByRequestOptions(options) {
424
423
  return isSecureRequest ? "https:" : ((_a = options.uri) == null ? void 0 : _a.protocol) || DEFAULT_PROTOCOL;
425
424
  }
426
425
  function getPortByRequestOptions(options) {
426
+ if (options.port) {
427
+ return Number(options.port);
428
+ }
429
+ if (options.hostname != null) {
430
+ const [, extractedPort] = options.hostname.match(/:(\d+)$/) || [];
431
+ if (extractedPort != null) {
432
+ return Number(extractedPort);
433
+ }
434
+ }
427
435
  const agent = getAgent(options);
428
- const agentPort = (agent == null ? void 0 : agent.options.port) || (agent == null ? void 0 : agent.defaultPort);
429
- const optionsPort = options.port;
430
- if (optionsPort || agentPort) {
431
- const explicitPort = optionsPort || agentPort || DEFAULT_PORT;
432
- return Number(explicitPort);
436
+ if (agent == null ? void 0 : agent.options.port) {
437
+ return Number(agent.options.port);
438
+ }
439
+ if (agent == null ? void 0 : agent.defaultPort) {
440
+ return Number(agent.defaultPort);
433
441
  }
442
+ return void 0;
434
443
  }
435
444
  function getHostByRequestOptions(options) {
436
- return options.hostname || options.host || DEFAULT_HOST;
445
+ const { hostname, host } = options;
446
+ if (hostname != null) {
447
+ return hostname.replace(/:\d+$/, "");
448
+ }
449
+ return host || DEFAULT_HOST;
437
450
  }
438
451
  function getAuthByRequestOptions(options) {
439
452
  if (options.auth) {
@@ -444,30 +457,42 @@ function getAuthByRequestOptions(options) {
444
457
  function isRawIPv6Address(host) {
445
458
  return host.includes(":") && !host.startsWith("[") && !host.endsWith("]");
446
459
  }
460
+ function getHostname(host, port) {
461
+ const portString = typeof port !== "undefined" ? `:${port}` : "";
462
+ if (isRawIPv6Address(host)) {
463
+ return `[${host}]${portString}`;
464
+ }
465
+ if (typeof port === "undefined") {
466
+ return host;
467
+ }
468
+ return `${host}${portString}`;
469
+ }
447
470
  function getUrlByRequestOptions(options) {
448
471
  debug3("request options", options);
472
+ if (options.uri) {
473
+ debug3(
474
+ 'constructing url from explicitly provided "options.uri": %s',
475
+ options.uri
476
+ );
477
+ return new URL(options.uri.href);
478
+ }
479
+ debug3("figuring out url from request options...");
449
480
  const protocol = getProtocolByRequestOptions(options);
450
- const host = getHostByRequestOptions(options);
451
- const port = getPortByRequestOptions(options);
452
- const path = options.path || DEFAULT_PATH;
453
- const auth = getAuthByRequestOptions(options);
454
481
  debug3("protocol", protocol);
482
+ const host = getHostByRequestOptions(options);
455
483
  debug3("host", host);
484
+ const port = getPortByRequestOptions(options);
456
485
  debug3("port", port);
486
+ const hostname = getHostname(host, port);
487
+ debug3("hostname", hostname);
488
+ const path = options.path || DEFAULT_PATH;
457
489
  debug3("path", path);
458
- const baseUrl = `${protocol}//${isRawIPv6Address(host) ? `[${host}]` : host}`;
459
- debug3("base URL:", baseUrl);
460
- const url = options.uri ? new URL(options.uri.href) : new URL(path, baseUrl);
461
- if (port) {
462
- debug3("detected explicit port", port);
463
- url.port = port.toString();
464
- }
465
- if (auth) {
466
- debug3("resolved auth", auth);
467
- url.username = auth.username;
468
- url.password = auth.password;
469
- }
470
- debug3("created URL:", url);
490
+ const credentials = getAuthByRequestOptions(options);
491
+ debug3("credentials", credentials);
492
+ const authString = credentials ? `${credentials.username}:${credentials.password}@` : "";
493
+ debug3("auth string:", authString);
494
+ const url = new URL(`${protocol}//${authString}${hostname}${path}`);
495
+ debug3("created url:", url);
471
496
  return url;
472
497
  }
473
498
 
@@ -123,12 +123,28 @@ function optionsToProxyHandler(options) {
123
123
  return constructorCall.call(newTarget, args, next);
124
124
  };
125
125
  }
126
- if (typeof setProperty !== "undefined") {
127
- handler.set = function(target, propertyName, nextValue, receiver) {
128
- const next = () => Reflect.set(target, propertyName, nextValue, receiver);
129
- return setProperty.call(target, [propertyName, nextValue], next);
126
+ handler.set = function(target, propertyName, nextValue, receiver) {
127
+ const next = () => {
128
+ const ownDescriptors = Reflect.getOwnPropertyDescriptor(
129
+ target,
130
+ propertyName
131
+ );
132
+ if (typeof (ownDescriptors == null ? void 0 : ownDescriptors.set) !== "undefined") {
133
+ ownDescriptors.set.apply(target, [nextValue]);
134
+ return true;
135
+ }
136
+ return Reflect.defineProperty(target, propertyName, {
137
+ writable: true,
138
+ enumerable: true,
139
+ configurable: true,
140
+ value: nextValue
141
+ });
130
142
  };
131
- }
143
+ if (typeof setProperty !== "undefined") {
144
+ return setProperty.call(target, [propertyName, nextValue], next);
145
+ }
146
+ return next();
147
+ };
132
148
  handler.get = function(target, propertyName, receiver) {
133
149
  const next = () => target[propertyName];
134
150
  const value = typeof getProperty !== "undefined" ? getProperty.call(target, [propertyName, receiver], next) : next();
@@ -193,11 +209,25 @@ var XMLHttpRequestController = class {
193
209
  this.responseBuffer = new Uint8Array();
194
210
  this.request = createProxy(initialRequest, {
195
211
  setProperty: ([propertyName, nextValue], invoke) => {
196
- if (propertyName === "withCredentials") {
197
- define(this.request, "withCredentials", nextValue);
198
- return true;
212
+ switch (propertyName) {
213
+ case "onabort":
214
+ case "onerror":
215
+ case "onload":
216
+ case "onloadend":
217
+ case "onloadstart":
218
+ case "onprogress":
219
+ case "ontimeout":
220
+ case "onreadystatechange": {
221
+ const eventName = propertyName.slice(
222
+ 2
223
+ );
224
+ this.request.addEventListener(eventName, nextValue);
225
+ return invoke();
226
+ }
227
+ default: {
228
+ return invoke();
229
+ }
199
230
  }
200
- return invoke();
201
231
  },
202
232
  methodCall: ([methodName, args], invoke) => {
203
233
  var _a;
@@ -261,21 +291,6 @@ var XMLHttpRequestController = class {
261
291
  });
262
292
  break;
263
293
  }
264
- case "onabort":
265
- case "onerror":
266
- case "onload":
267
- case "onloadend":
268
- case "onloadstart":
269
- case "onprogress":
270
- case "ontimeout":
271
- case "onreadystatechange": {
272
- const [listener] = args;
273
- this.registerEvent(
274
- methodName,
275
- listener
276
- );
277
- return invoke();
278
- }
279
294
  default: {
280
295
  return invoke();
281
296
  }
@@ -328,14 +343,17 @@ var XMLHttpRequestController = class {
328
343
  Object.defineProperties(this.request, {
329
344
  response: {
330
345
  enumerable: true,
346
+ configurable: false,
331
347
  get: () => this.response
332
348
  },
333
349
  responseText: {
334
350
  enumerable: true,
351
+ configurable: false,
335
352
  get: () => this.responseText
336
353
  },
337
354
  responseXML: {
338
355
  enumerable: true,
356
+ configurable: false,
339
357
  get: () => this.responseXML
340
358
  }
341
359
  });
@@ -549,9 +567,19 @@ function createXMLHttpRequestProxy({
549
567
  log
550
568
  }) {
551
569
  const XMLHttpRequestProxy = new Proxy(globalThis.XMLHttpRequest, {
552
- construct(target, args) {
570
+ construct(target, args, newTarget) {
553
571
  log("constructed new XMLHttpRequest");
554
- const originalRequest = Reflect.construct(target, args);
572
+ const originalRequest = Reflect.construct(target, args, newTarget);
573
+ const prototypeDescriptors = Object.getOwnPropertyDescriptors(
574
+ target.prototype
575
+ );
576
+ for (const propertyName in prototypeDescriptors) {
577
+ Reflect.defineProperty(
578
+ originalRequest,
579
+ propertyName,
580
+ prototypeDescriptors[propertyName]
581
+ );
582
+ }
555
583
  const requestController = new XMLHttpRequestController(
556
584
  originalRequest,
557
585
  log