@mswjs/interceptors 0.17.6 → 0.18.0

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 (114) hide show
  1. package/README.md +49 -47
  2. package/lib/Interceptor.js +4 -4
  3. package/lib/Interceptor.js.map +1 -1
  4. package/lib/RemoteHttpInterceptor.d.ts +15 -0
  5. package/lib/RemoteHttpInterceptor.js +86 -56
  6. package/lib/RemoteHttpInterceptor.js.map +1 -1
  7. package/lib/glossary.d.ts +3 -14
  8. package/lib/glossary.js.map +1 -1
  9. package/lib/index.d.ts +0 -2
  10. package/lib/index.js +0 -2
  11. package/lib/index.js.map +1 -1
  12. package/lib/interceptors/ClientRequest/NodeClientRequest.d.ts +13 -5
  13. package/lib/interceptors/ClientRequest/NodeClientRequest.js +179 -166
  14. package/lib/interceptors/ClientRequest/NodeClientRequest.js.map +1 -1
  15. package/lib/interceptors/ClientRequest/http.get.js +9 -5
  16. package/lib/interceptors/ClientRequest/http.get.js.map +1 -1
  17. package/lib/interceptors/ClientRequest/http.request.js +10 -6
  18. package/lib/interceptors/ClientRequest/http.request.js.map +1 -1
  19. package/lib/interceptors/ClientRequest/index.d.ts +2 -5
  20. package/lib/interceptors/ClientRequest/index.js +2 -13
  21. package/lib/interceptors/ClientRequest/index.js.map +1 -1
  22. package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.js +9 -5
  23. package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.js.map +1 -1
  24. package/lib/interceptors/ClientRequest/utils/createRequest.d.ts +6 -0
  25. package/lib/interceptors/ClientRequest/utils/createRequest.js +52 -0
  26. package/lib/interceptors/ClientRequest/utils/createRequest.js.map +1 -0
  27. package/lib/interceptors/ClientRequest/utils/createResponse.d.ts +8 -0
  28. package/lib/interceptors/ClientRequest/utils/createResponse.js +24 -0
  29. package/lib/interceptors/ClientRequest/utils/createResponse.js.map +1 -0
  30. package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.js +1 -1
  31. package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.js.map +1 -1
  32. package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js +8 -8
  33. package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js.map +1 -1
  34. package/lib/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.d.ts +1 -1
  35. package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.js +1 -1
  36. package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.js.map +1 -1
  37. package/lib/interceptors/XMLHttpRequest/XMLHttpRequestOverride.d.ts +6 -10
  38. package/lib/interceptors/XMLHttpRequest/XMLHttpRequestOverride.js +203 -143
  39. package/lib/interceptors/XMLHttpRequest/XMLHttpRequestOverride.js.map +1 -1
  40. package/lib/interceptors/XMLHttpRequest/index.d.ts +2 -2
  41. package/lib/interceptors/XMLHttpRequest/index.js +2 -2
  42. package/lib/interceptors/XMLHttpRequest/index.js.map +1 -1
  43. package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.d.ts +4 -0
  44. package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.js +14 -0
  45. package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.js.map +1 -0
  46. package/lib/interceptors/XMLHttpRequest/utils/createResponse.d.ts +2 -0
  47. package/lib/interceptors/XMLHttpRequest/utils/createResponse.js +14 -0
  48. package/lib/interceptors/XMLHttpRequest/utils/createResponse.js.map +1 -0
  49. package/lib/interceptors/fetch/index.js +24 -81
  50. package/lib/interceptors/fetch/index.js.map +1 -1
  51. package/lib/utils/AsyncEventEmitter.js +12 -8
  52. package/lib/utils/AsyncEventEmitter.js.map +1 -1
  53. package/lib/utils/RequestWithCredentials.d.ts +7 -0
  54. package/lib/utils/RequestWithCredentials.js +20 -0
  55. package/lib/utils/RequestWithCredentials.js.map +1 -0
  56. package/lib/utils/bufferUtils.d.ts +7 -2
  57. package/lib/utils/bufferUtils.js +10 -6
  58. package/lib/utils/bufferUtils.js.map +1 -1
  59. package/lib/utils/parseJson.d.ts +1 -1
  60. package/lib/utils/toInteractiveRequest.d.ts +7 -0
  61. package/lib/utils/toInteractiveRequest.js +20 -0
  62. package/lib/utils/toInteractiveRequest.js.map +1 -0
  63. package/package.json +3 -2
  64. package/src/RemoteHttpInterceptor.ts +84 -34
  65. package/src/glossary.ts +5 -18
  66. package/src/index.ts +0 -2
  67. package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +17 -23
  68. package/src/interceptors/ClientRequest/NodeClientRequest.ts +177 -153
  69. package/src/interceptors/ClientRequest/index.test.ts +5 -3
  70. package/src/interceptors/ClientRequest/index.ts +2 -26
  71. package/src/interceptors/ClientRequest/utils/createRequest.test.ts +61 -0
  72. package/src/interceptors/ClientRequest/utils/createRequest.ts +32 -0
  73. package/src/interceptors/ClientRequest/utils/createResponse.test.ts +24 -0
  74. package/src/interceptors/ClientRequest/utils/createResponse.ts +22 -0
  75. package/src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts +1 -1
  76. package/src/interceptors/XMLHttpRequest/XMLHttpRequestOverride.ts +234 -174
  77. package/src/interceptors/XMLHttpRequest/index.ts +3 -2
  78. package/src/interceptors/XMLHttpRequest/utils/concatArrayBuffer.ts +12 -0
  79. package/src/interceptors/XMLHttpRequest/utils/concateArrayBuffer.test.ts +14 -0
  80. package/src/interceptors/XMLHttpRequest/utils/createResponse.ts +13 -0
  81. package/src/interceptors/fetch/index.ts +30 -69
  82. package/src/utils/RequestWithCredentials.ts +21 -0
  83. package/src/utils/bufferUtils.ts +10 -5
  84. package/src/utils/parseJson.ts +1 -1
  85. package/src/utils/toInteractiveRequest.ts +29 -0
  86. package/lib/InteractiveIsomorphicRequest.d.ts +0 -7
  87. package/lib/InteractiveIsomorphicRequest.js +0 -37
  88. package/lib/InteractiveIsomorphicRequest.js.map +0 -1
  89. package/lib/IsomorphicRequest.d.ts +0 -24
  90. package/lib/IsomorphicRequest.js +0 -107
  91. package/lib/IsomorphicRequest.js.map +0 -1
  92. package/lib/interceptors/ClientRequest/utils/bodyBufferToString.d.ts +0 -2
  93. package/lib/interceptors/ClientRequest/utils/bodyBufferToString.js +0 -11
  94. package/lib/interceptors/ClientRequest/utils/bodyBufferToString.js.map +0 -1
  95. package/lib/interceptors/ClientRequest/utils/concatChunkToBuffer.d.ts +0 -2
  96. package/lib/interceptors/ClientRequest/utils/concatChunkToBuffer.js +0 -11
  97. package/lib/interceptors/ClientRequest/utils/concatChunkToBuffer.js.map +0 -1
  98. package/lib/interceptors/XMLHttpRequest/utils/bufferFrom.d.ts +0 -5
  99. package/lib/interceptors/XMLHttpRequest/utils/bufferFrom.js +0 -20
  100. package/lib/interceptors/XMLHttpRequest/utils/bufferFrom.js.map +0 -1
  101. package/lib/utils/toIsoResponse.d.ts +0 -5
  102. package/lib/utils/toIsoResponse.js +0 -18
  103. package/lib/utils/toIsoResponse.js.map +0 -1
  104. package/src/InteractiveIsomorphicRequest.ts +0 -24
  105. package/src/IsomorphicRequest.test.ts +0 -106
  106. package/src/IsomorphicRequest.ts +0 -86
  107. package/src/interceptors/ClientRequest/utils/bodyBufferToString.test.ts +0 -16
  108. package/src/interceptors/ClientRequest/utils/bodyBufferToString.ts +0 -7
  109. package/src/interceptors/ClientRequest/utils/concatChunkToBuffer.test.ts +0 -13
  110. package/src/interceptors/ClientRequest/utils/concatChunkToBuffer.ts +0 -10
  111. package/src/interceptors/XMLHttpRequest/utils/bufferFrom.test.ts +0 -11
  112. package/src/interceptors/XMLHttpRequest/utils/bufferFrom.ts +0 -16
  113. package/src/utils/toIsoResponse.test.ts +0 -39
  114. package/src/utils/toIsoResponse.ts +0 -14
@@ -1,19 +1,9 @@
1
- import {
2
- Headers,
3
- flattenHeadersObject,
4
- objectToHeaders,
5
- headersToObject,
6
- } from 'headers-polyfill'
7
1
  import { invariant } from 'outvariant'
8
- import { IsomorphicRequest } from '../../IsomorphicRequest'
9
- import {
10
- HttpRequestEventMap,
11
- IsomorphicResponse,
12
- IS_PATCHED_MODULE,
13
- } from '../../glossary'
2
+ import type { Response as ResponsePolyfill } from '@remix-run/web-fetch'
3
+ import { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'
14
4
  import { Interceptor } from '../../Interceptor'
15
- import { toIsoResponse } from '../../utils/toIsoResponse'
16
- import { InteractiveIsomorphicRequest } from '../../InteractiveIsomorphicRequest'
5
+ import { uuidv4 } from '../../utils/uuid'
6
+ import { toInteractiveRequest } from '../../utils/toInteractiveRequest'
17
7
 
18
8
  export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
19
9
  static symbol = Symbol('fetch')
@@ -38,73 +28,51 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
38
28
  )
39
29
 
40
30
  globalThis.fetch = async (input, init) => {
31
+ const requestId = uuidv4()
41
32
  const request = new Request(input, init)
42
33
 
43
- const url = typeof input === 'string' ? input : input.url
44
- const method = request.method
34
+ this.log('[%s] %s', request.method, request.url)
45
35
 
46
- this.log('[%s] %s', method, url)
47
-
48
- const body = await request.clone().arrayBuffer()
49
- const isomorphicRequest = new IsomorphicRequest(
50
- new URL(url, location.origin),
51
- {
52
- body,
53
- method,
54
- headers: new Headers(request.headers),
55
- credentials: request.credentials,
56
- }
57
- )
58
-
59
- const interactiveIsomorphicRequest = new InteractiveIsomorphicRequest(
60
- isomorphicRequest
61
- )
62
-
63
- this.log('isomorphic request', interactiveIsomorphicRequest)
36
+ const interactiveRequest = toInteractiveRequest(request)
64
37
 
65
38
  this.log(
66
39
  'emitting the "request" event for %d listener(s)...',
67
40
  this.emitter.listenerCount('request')
68
41
  )
69
- this.emitter.emit('request', interactiveIsomorphicRequest)
42
+ this.emitter.emit('request', interactiveRequest, requestId)
70
43
 
71
44
  this.log('awaiting for the mocked response...')
72
45
 
73
- await this.emitter.untilIdle('request', ({ args: [request] }) => {
74
- return request.id === interactiveIsomorphicRequest.id
75
- })
46
+ await this.emitter.untilIdle(
47
+ 'request',
48
+ ({ args: [, pendingRequestId] }) => {
49
+ return pendingRequestId === requestId
50
+ }
51
+ )
76
52
  this.log('all request listeners have been resolved!')
77
53
 
78
- const [mockedResponse] =
79
- await interactiveIsomorphicRequest.respondWith.invoked()
54
+ const [mockedResponse] = await interactiveRequest.respondWith.invoked()
80
55
  this.log('event.respondWith called with:', mockedResponse)
81
56
 
82
57
  if (mockedResponse) {
83
58
  this.log('received mocked response:', mockedResponse)
84
-
85
- const isomorphicResponse = toIsoResponse(mockedResponse)
86
- this.log('derived isomorphic response:', isomorphicResponse)
59
+ const responseCloine = mockedResponse.clone()
87
60
 
88
61
  this.emitter.emit(
89
62
  'response',
90
- interactiveIsomorphicRequest,
91
- isomorphicResponse
63
+ responseCloine,
64
+ interactiveRequest,
65
+ requestId
92
66
  )
93
67
 
94
- const response = new Response(mockedResponse.body, {
95
- ...isomorphicResponse,
96
- // `Response.headers` cannot be instantiated with the `Headers` polyfill.
97
- // Apparently, it halts if the `Headers` class contains unknown properties
98
- // (i.e. the internal `Headers.map`).
99
- headers: flattenHeadersObject(mockedResponse.headers || {}),
100
- })
68
+ const response = new Response(mockedResponse.body, mockedResponse)
101
69
 
102
70
  // Set the "response.url" property to equal the intercepted request URL.
103
71
  Object.defineProperty(response, 'url', {
104
72
  writable: false,
105
73
  enumerable: true,
106
74
  configurable: false,
107
- value: interactiveIsomorphicRequest.url.href,
75
+ value: request.url,
108
76
  })
109
77
 
110
78
  return response
@@ -112,15 +80,19 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
112
80
 
113
81
  this.log('no mocked response received!')
114
82
 
115
- return pureFetch(request).then(async (response) => {
116
- const cloneResponse = response.clone()
117
- this.log('original fetch performed', cloneResponse)
83
+ console.log('or req headers', Array.from(request.headers.entries()))
84
+
85
+ return pureFetch(request).then((response) => {
86
+ const responseClone = response.clone() as ResponsePolyfill
87
+ this.log('original fetch performed', responseClone)
118
88
 
119
89
  this.emitter.emit(
120
90
  'response',
121
- interactiveIsomorphicRequest,
122
- await normalizeFetchResponse(cloneResponse)
91
+ responseClone,
92
+ interactiveRequest,
93
+ requestId
123
94
  )
95
+
124
96
  return response
125
97
  })
126
98
  }
@@ -142,14 +114,3 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
142
114
  })
143
115
  }
144
116
  }
145
-
146
- async function normalizeFetchResponse(
147
- response: Response
148
- ): Promise<IsomorphicResponse> {
149
- return {
150
- status: response.status,
151
- statusText: response.statusText,
152
- headers: objectToHeaders(headersToObject(response.headers)),
153
- body: await response.text(),
154
- }
155
- }
@@ -0,0 +1,21 @@
1
+ import { Request } from '@remix-run/web-fetch'
2
+
3
+ /**
4
+ * Custom wrapper around Remix's "Request" until it
5
+ * supports "credentials" correctly.
6
+ * @see https://github.com/remix-run/web-std-io/pull/21
7
+ */
8
+ export function createRequestWithCredentials(
9
+ input: string | URL | Request,
10
+ init?: RequestInit
11
+ ): Request {
12
+ const request = new Request(input, init)
13
+
14
+ Object.defineProperty(request, 'credentials', {
15
+ enumerable: true,
16
+ writable: false,
17
+ value: init?.credentials || 'include',
18
+ })
19
+
20
+ return request
21
+ }
@@ -1,9 +1,9 @@
1
1
  import { TextDecoder, TextEncoder } from 'web-encoding'
2
2
 
3
- export function encodeBuffer(text: string): ArrayBuffer {
4
- const encoder = new TextEncoder()
5
- const encoded = encoder.encode(text)
6
- return getArrayBuffer(encoded)
3
+ const encoder = new TextEncoder()
4
+
5
+ export function encodeBuffer(text: string): Uint8Array {
6
+ return encoder.encode(text)
7
7
  }
8
8
 
9
9
  export function decodeBuffer(buffer: ArrayBuffer, encoding?: string): string {
@@ -11,7 +11,12 @@ export function decodeBuffer(buffer: ArrayBuffer, encoding?: string): string {
11
11
  return decoder.decode(buffer)
12
12
  }
13
13
 
14
- export function getArrayBuffer(array: Uint8Array): ArrayBuffer {
14
+ /**
15
+ * Create an `ArrayBuffer` from the given `Uint8Array`.
16
+ * Takes the byte offset into account to produce the right buffer
17
+ * in the case when the buffer is bigger than the data view.
18
+ */
19
+ export function toArrayBuffer(array: Uint8Array): ArrayBuffer {
15
20
  return array.buffer.slice(
16
21
  array.byteOffset,
17
22
  array.byteOffset + array.byteLength
@@ -2,7 +2,7 @@
2
2
  * Parses a given string into JSON.
3
3
  * Gracefully handles invalid JSON by returning `null`.
4
4
  */
5
- export function parseJson(data: string): Record<string, any> | null {
5
+ export function parseJson(data: string): Record<string, unknown> | null {
6
6
  try {
7
7
  const json = JSON.parse(data)
8
8
  return json
@@ -0,0 +1,29 @@
1
+ import { format } from 'outvariant'
2
+ import { createLazyCallback, LazyCallback } from './createLazyCallback'
3
+
4
+ type LazyResponseCallback = (response: Response) => void
5
+
6
+ export type InteractiveRequest = globalThis.Request & {
7
+ respondWith: LazyCallback<LazyResponseCallback>
8
+ }
9
+
10
+ export function toInteractiveRequest(request: Request): InteractiveRequest {
11
+ Object.defineProperty(request, 'respondWith', {
12
+ writable: false,
13
+ enumerable: true,
14
+ value: createLazyCallback<LazyResponseCallback>({
15
+ maxCalls: 1,
16
+ maxCallsCallback() {
17
+ throw new Error(
18
+ format(
19
+ 'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
20
+ request.method,
21
+ request.url
22
+ )
23
+ )
24
+ },
25
+ }),
26
+ })
27
+
28
+ return request as InteractiveRequest
29
+ }
@@ -1,7 +0,0 @@
1
- import { MockedResponse } from './glossary';
2
- import { IsomorphicRequest } from './IsomorphicRequest';
3
- import { LazyCallback } from './utils/createLazyCallback';
4
- export declare class InteractiveIsomorphicRequest extends IsomorphicRequest {
5
- respondWith: LazyCallback<(response: MockedResponse) => void>;
6
- constructor(request: IsomorphicRequest);
7
- }
@@ -1,37 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.InteractiveIsomorphicRequest = void 0;
19
- var outvariant_1 = require("outvariant");
20
- var IsomorphicRequest_1 = require("./IsomorphicRequest");
21
- var createLazyCallback_1 = require("./utils/createLazyCallback");
22
- var InteractiveIsomorphicRequest = /** @class */ (function (_super) {
23
- __extends(InteractiveIsomorphicRequest, _super);
24
- function InteractiveIsomorphicRequest(request) {
25
- var _this = _super.call(this, request) || this;
26
- _this.respondWith = createLazyCallback_1.createLazyCallback({
27
- maxCalls: 1,
28
- maxCallsCallback: function () {
29
- outvariant_1.invariant(false, 'Failed to respond to "%s %s" request: the "request" event has already been responded to.', _this.method, _this.url.href);
30
- },
31
- });
32
- return _this;
33
- }
34
- return InteractiveIsomorphicRequest;
35
- }(IsomorphicRequest_1.IsomorphicRequest));
36
- exports.InteractiveIsomorphicRequest = InteractiveIsomorphicRequest;
37
- //# sourceMappingURL=InteractiveIsomorphicRequest.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"InteractiveIsomorphicRequest.js","sourceRoot":"","sources":["../src/InteractiveIsomorphicRequest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,yCAAsC;AAEtC,yDAAuD;AACvD,iEAA6E;AAE7E;IAAkD,gDAAiB;IAGjE,sCAAY,OAA0B;QAAtC,YACE,kBAAM,OAAO,CAAC,SAaf;QAXC,KAAI,CAAC,WAAW,GAAG,uCAAkB,CAAC;YACpC,QAAQ,EAAE,CAAC;YACX,gBAAgB,EAAE;gBAChB,sBAAS,CACP,KAAK,EACL,0FAA0F,EAC1F,KAAI,CAAC,MAAM,EACX,KAAI,CAAC,GAAG,CAAC,IAAI,CACd,CAAA;YACH,CAAC;SACF,CAAC,CAAA;;IACJ,CAAC;IACH,mCAAC;AAAD,CAAC,AAlBD,CAAkD,qCAAiB,GAkBlE;AAlBY,oEAA4B"}
@@ -1,24 +0,0 @@
1
- import { Headers } from 'headers-polyfill';
2
- export interface RequestInit {
3
- method?: string;
4
- headers?: Record<string, string | string[]> | Headers;
5
- credentials?: RequestCredentials;
6
- body?: ArrayBuffer;
7
- }
8
- export declare class IsomorphicRequest {
9
- id: string;
10
- readonly url: URL;
11
- readonly method: string;
12
- readonly headers: Headers;
13
- readonly credentials: RequestCredentials;
14
- private readonly _body;
15
- private _bodyUsed;
16
- constructor(url: URL);
17
- constructor(url: URL, init: RequestInit);
18
- constructor(request: IsomorphicRequest);
19
- get bodyUsed(): boolean;
20
- text(): Promise<string>;
21
- json<T = any>(): Promise<T>;
22
- arrayBuffer(): Promise<ArrayBuffer>;
23
- clone(): IsomorphicRequest;
24
- }
@@ -1,107 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.IsomorphicRequest = void 0;
40
- var headers_polyfill_1 = require("headers-polyfill");
41
- var outvariant_1 = require("outvariant");
42
- var bufferUtils_1 = require("./utils/bufferUtils");
43
- var uuid_1 = require("./utils/uuid");
44
- var IsomorphicRequest = /** @class */ (function () {
45
- function IsomorphicRequest(input, init) {
46
- if (init === void 0) { init = {}; }
47
- var defaultBody = new ArrayBuffer(0);
48
- this._bodyUsed = false;
49
- if (input instanceof IsomorphicRequest) {
50
- this.id = input.id;
51
- this.url = input.url;
52
- this.method = input.method;
53
- this.headers = input.headers;
54
- this.credentials = input.credentials;
55
- this._body = input._body || defaultBody;
56
- return;
57
- }
58
- this.id = uuid_1.uuidv4();
59
- this.url = input;
60
- this.method = init.method || 'GET';
61
- this.headers = new headers_polyfill_1.Headers(init.headers);
62
- this.credentials = init.credentials || 'same-origin';
63
- this._body = init.body || defaultBody;
64
- }
65
- Object.defineProperty(IsomorphicRequest.prototype, "bodyUsed", {
66
- get: function () {
67
- return this._bodyUsed;
68
- },
69
- enumerable: false,
70
- configurable: true
71
- });
72
- IsomorphicRequest.prototype.text = function () {
73
- return __awaiter(this, void 0, void 0, function () {
74
- return __generator(this, function (_a) {
75
- outvariant_1.invariant(!this.bodyUsed, 'Failed to execute "text" on "IsomorphicRequest": body buffer already read');
76
- this._bodyUsed = true;
77
- return [2 /*return*/, bufferUtils_1.decodeBuffer(this._body)];
78
- });
79
- });
80
- };
81
- IsomorphicRequest.prototype.json = function () {
82
- return __awaiter(this, void 0, void 0, function () {
83
- var text;
84
- return __generator(this, function (_a) {
85
- outvariant_1.invariant(!this.bodyUsed, 'Failed to execute "json" on "IsomorphicRequest": body buffer already read');
86
- this._bodyUsed = true;
87
- text = bufferUtils_1.decodeBuffer(this._body);
88
- return [2 /*return*/, JSON.parse(text)];
89
- });
90
- });
91
- };
92
- IsomorphicRequest.prototype.arrayBuffer = function () {
93
- return __awaiter(this, void 0, void 0, function () {
94
- return __generator(this, function (_a) {
95
- outvariant_1.invariant(!this.bodyUsed, 'Failed to execute "arrayBuffer" on "IsomorphicRequest": body buffer already read');
96
- this._bodyUsed = true;
97
- return [2 /*return*/, this._body];
98
- });
99
- });
100
- };
101
- IsomorphicRequest.prototype.clone = function () {
102
- return new IsomorphicRequest(this);
103
- };
104
- return IsomorphicRequest;
105
- }());
106
- exports.IsomorphicRequest = IsomorphicRequest;
107
- //# sourceMappingURL=IsomorphicRequest.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"IsomorphicRequest.js","sourceRoot":"","sources":["../src/IsomorphicRequest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAA0C;AAC1C,yCAAsC;AACtC,mDAAkD;AAClD,qCAAqC;AASrC;IAaE,2BAAY,KAA8B,EAAE,IAAsB;QAAtB,qBAAA,EAAA,SAAsB;QAChE,IAAM,WAAW,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAA;QACtC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;QAEtB,IAAI,KAAK,YAAY,iBAAiB,EAAE;YACtC,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC,EAAE,CAAA;YAClB,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAA;YACpB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAA;YAC1B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAA;YAC5B,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW,CAAA;YACpC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,IAAI,WAAW,CAAA;YACvC,OAAM;SACP;QAED,IAAI,CAAC,EAAE,GAAG,aAAM,EAAE,CAAA;QAClB,IAAI,CAAC,GAAG,GAAG,KAAK,CAAA;QAChB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK,CAAA;QAClC,IAAI,CAAC,OAAO,GAAG,IAAI,0BAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACxC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,aAAa,CAAA;QACpD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,IAAI,WAAW,CAAA;IACvC,CAAC;IAED,sBAAW,uCAAQ;aAAnB;YACE,OAAO,IAAI,CAAC,SAAS,CAAA;QACvB,CAAC;;;OAAA;IAEY,gCAAI,GAAjB;;;gBACE,sBAAS,CACP,CAAC,IAAI,CAAC,QAAQ,EACd,2EAA2E,CAC5E,CAAA;gBAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;gBACrB,sBAAO,0BAAY,CAAC,IAAI,CAAC,KAAK,CAAC,EAAA;;;KAChC;IAEY,gCAAI,GAAjB;;;;gBACE,sBAAS,CACP,CAAC,IAAI,CAAC,QAAQ,EACd,2EAA2E,CAC5E,CAAA;gBAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;gBACf,IAAI,GAAG,0BAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;gBACrC,sBAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAA;;;KACxB;IAEY,uCAAW,GAAxB;;;gBACE,sBAAS,CACP,CAAC,IAAI,CAAC,QAAQ,EACd,kFAAkF,CACnF,CAAA;gBAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;gBACrB,sBAAO,IAAI,CAAC,KAAK,EAAA;;;KAClB;IAEM,iCAAK,GAAZ;QACE,OAAO,IAAI,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACpC,CAAC;IACH,wBAAC;AAAD,CAAC,AAzED,IAyEC;AAzEY,8CAAiB"}
@@ -1,2 +0,0 @@
1
- /// <reference types="node" />
2
- export declare function bodyBufferToString(buffer: Buffer): string;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bodyBufferToString = void 0;
4
- function bodyBufferToString(buffer) {
5
- var utfEncodedBuffer = buffer.toString('utf8');
6
- var bufferCopy = Buffer.from(utfEncodedBuffer);
7
- var isUtf8 = bufferCopy.equals(buffer);
8
- return isUtf8 ? utfEncodedBuffer : buffer.toString('hex');
9
- }
10
- exports.bodyBufferToString = bodyBufferToString;
11
- //# sourceMappingURL=bodyBufferToString.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bodyBufferToString.js","sourceRoot":"","sources":["../../../../src/interceptors/ClientRequest/utils/bodyBufferToString.ts"],"names":[],"mappings":";;;AAAA,SAAgB,kBAAkB,CAAC,MAAc;IAC/C,IAAM,gBAAgB,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IAChD,IAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAA;IAChD,IAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IAExC,OAAO,MAAM,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAC3D,CAAC;AAND,gDAMC"}
@@ -1,2 +0,0 @@
1
- /// <reference types="node" />
2
- export declare function concatChunkToBuffer(chunk: string | Buffer, buffer: Buffer[]): Buffer[];
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.concatChunkToBuffer = void 0;
4
- function concatChunkToBuffer(chunk, buffer) {
5
- if (!Buffer.isBuffer(chunk)) {
6
- chunk = Buffer.from(chunk);
7
- }
8
- return buffer.concat(chunk);
9
- }
10
- exports.concatChunkToBuffer = concatChunkToBuffer;
11
- //# sourceMappingURL=concatChunkToBuffer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"concatChunkToBuffer.js","sourceRoot":"","sources":["../../../../src/interceptors/ClientRequest/utils/concatChunkToBuffer.ts"],"names":[],"mappings":";;;AAAA,SAAgB,mBAAmB,CACjC,KAAsB,EACtB,MAAgB;IAEhB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;QAC3B,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;KAC3B;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC7B,CAAC;AATD,kDASC"}
@@ -1,5 +0,0 @@
1
- /**
2
- * Convert a given string into a `Uint8Array`.
3
- * We don't use `TextEncoder` because it's unavailable in some environments.
4
- */
5
- export declare function bufferFrom(init: string): Uint8Array;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bufferFrom = void 0;
4
- /**
5
- * Convert a given string into a `Uint8Array`.
6
- * We don't use `TextEncoder` because it's unavailable in some environments.
7
- */
8
- function bufferFrom(init) {
9
- var encodedString = encodeURIComponent(init);
10
- var binaryString = encodedString.replace(/%([0-9A-F]{2})/g, function (_, char) {
11
- return String.fromCharCode(('0x' + char));
12
- });
13
- var buffer = new Uint8Array(binaryString.length);
14
- Array.prototype.forEach.call(binaryString, function (char, index) {
15
- buffer[index] = char.charCodeAt(0);
16
- });
17
- return buffer;
18
- }
19
- exports.bufferFrom = bufferFrom;
20
- //# sourceMappingURL=bufferFrom.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bufferFrom.js","sourceRoot":"","sources":["../../../../src/interceptors/XMLHttpRequest/utils/bufferFrom.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,SAAgB,UAAU,CAAC,IAAY;IACrC,IAAM,aAAa,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAA;IAC9C,IAAM,YAAY,GAAG,aAAa,CAAC,OAAO,CAAC,iBAAiB,EAAE,UAAC,CAAC,EAAE,IAAI;QACpE,OAAO,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,GAAG,IAAI,CAAQ,CAAC,CAAA;IAClD,CAAC,CAAC,CAAA;IACF,IAAM,MAAM,GAAG,IAAI,UAAU,CAAC,YAAY,CAAC,MAAM,CAAC,CAAA;IAClD,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,UAAC,IAAI,EAAE,KAAK;QACrD,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;IACpC,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC;AAXD,gCAWC"}
@@ -1,5 +0,0 @@
1
- import { IsomorphicResponse, MockedResponse } from '../glossary';
2
- /**
3
- * Converts a given mocked response object into an isomorphic response.
4
- */
5
- export declare function toIsoResponse(response: MockedResponse): IsomorphicResponse;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toIsoResponse = void 0;
4
- var headers_polyfill_1 = require("headers-polyfill");
5
- /**
6
- * Converts a given mocked response object into an isomorphic response.
7
- */
8
- function toIsoResponse(response) {
9
- var _a;
10
- return {
11
- status: (_a = response.status) !== null && _a !== void 0 ? _a : 200,
12
- statusText: response.statusText || 'OK',
13
- headers: headers_polyfill_1.objectToHeaders(response.headers || {}),
14
- body: response.body,
15
- };
16
- }
17
- exports.toIsoResponse = toIsoResponse;
18
- //# sourceMappingURL=toIsoResponse.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"toIsoResponse.js","sourceRoot":"","sources":["../../src/utils/toIsoResponse.ts"],"names":[],"mappings":";;;AAAA,qDAAkD;AAGlD;;GAEG;AACH,SAAgB,aAAa,CAAC,QAAwB;;IACpD,OAAO;QACL,MAAM,EAAE,MAAA,QAAQ,CAAC,MAAM,mCAAI,GAAG;QAC9B,UAAU,EAAE,QAAQ,CAAC,UAAU,IAAI,IAAI;QACvC,OAAO,EAAE,kCAAe,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC;QAChD,IAAI,EAAE,QAAQ,CAAC,IAAI;KACpB,CAAA;AACH,CAAC;AAPD,sCAOC"}
@@ -1,24 +0,0 @@
1
- import { invariant } from 'outvariant'
2
- import { MockedResponse } from './glossary'
3
- import { IsomorphicRequest } from './IsomorphicRequest'
4
- import { createLazyCallback, LazyCallback } from './utils/createLazyCallback'
5
-
6
- export class InteractiveIsomorphicRequest extends IsomorphicRequest {
7
- public respondWith: LazyCallback<(response: MockedResponse) => void>
8
-
9
- constructor(request: IsomorphicRequest) {
10
- super(request)
11
-
12
- this.respondWith = createLazyCallback({
13
- maxCalls: 1,
14
- maxCallsCallback: () => {
15
- invariant(
16
- false,
17
- 'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
18
- this.method,
19
- this.url.href
20
- )
21
- },
22
- })
23
- }
24
- }