@zimic/interceptor 1.2.7-canary.2 → 1.3.0-canary.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.
- package/dist/{chunk-6GEP6R3L.mjs → chunk-4L2JH2L4.mjs} +80 -23
- package/dist/chunk-4L2JH2L4.mjs.map +1 -0
- package/dist/{chunk-PB4TJVK3.js → chunk-F5OGZSHS.js} +80 -23
- package/dist/chunk-F5OGZSHS.js.map +1 -0
- package/dist/cli.js +17 -19
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2 -4
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +95 -6
- package/dist/http.js +253 -186
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +254 -187
- package/dist/http.mjs.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.mjs +1 -1
- package/package.json +2 -2
- package/src/cli/server/start.ts +7 -2
- package/src/http/interceptor/HttpInterceptorClient.ts +8 -10
- package/src/http/interceptor/types/options.ts +11 -5
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +73 -15
- package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +36 -11
- package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +34 -13
- package/src/http/interceptorWorker/types/http.ts +6 -1
- package/src/http/interceptorWorker/types/msw.ts +0 -9
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +2 -4
- package/src/http/requestHandler/errors/TimesCheckError.ts +1 -1
- package/src/http/requestHandler/types/requests.ts +16 -2
- package/src/server/InterceptorServer.ts +13 -5
- package/src/server/constants.ts +1 -1
- package/src/server/errors/UnsupportedResponseBypassError.ts +11 -0
- package/src/utils/crypto.ts +1 -1
- package/src/utils/fetch.ts +25 -6
- package/src/utils/files.ts +1 -1
- package/src/utils/logging.ts +2 -2
- package/src/webSocket/WebSocketClient.ts +1 -1
- package/dist/chunk-6GEP6R3L.mjs.map +0 -1
- package/dist/chunk-PB4TJVK3.js.map +0 -1
package/dist/http.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InvalidFormDataError as InvalidFormDataError$1, InvalidJSONError as InvalidJSONError$1, HTTP_METHODS, HttpHeaders, HttpSearchParams, HttpFormData, parseHttpBody } from '@zimic/http';
|
|
2
2
|
import color2 from 'picocolors';
|
|
3
|
-
import { http, passthrough } from 'msw';
|
|
3
|
+
import { http, bypass, passthrough } from 'msw';
|
|
4
4
|
import * as mswBrowser from 'msw/browser';
|
|
5
5
|
import * as mswNode from 'msw/node';
|
|
6
6
|
import ClientSocket from 'isomorphic-ws';
|
|
@@ -96,19 +96,144 @@ var DisabledRequestSavingError = class extends TypeError {
|
|
|
96
96
|
};
|
|
97
97
|
var DisabledRequestSavingError_default = DisabledRequestSavingError;
|
|
98
98
|
|
|
99
|
-
// ../zimic-utils/dist/
|
|
99
|
+
// ../zimic-utils/dist/data.mjs
|
|
100
100
|
function isDefined(value) {
|
|
101
101
|
return value !== void 0 && value !== null;
|
|
102
102
|
}
|
|
103
103
|
var isDefined_default = isDefined;
|
|
104
|
-
|
|
105
|
-
// ../zimic-utils/dist/data/isNonEmpty.mjs
|
|
106
104
|
function isNonEmpty(value) {
|
|
107
105
|
return isDefined_default(value) && value !== "";
|
|
108
106
|
}
|
|
109
107
|
var isNonEmpty_default = isNonEmpty;
|
|
108
|
+
async function blobEquals(blob, otherBlob) {
|
|
109
|
+
if (blob.type !== otherBlob.type || blob.size !== otherBlob.size) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const reader = blob.stream().getReader();
|
|
113
|
+
const otherReader = otherBlob.stream().getReader();
|
|
114
|
+
let buffer = new Uint8Array(0);
|
|
115
|
+
let otherBuffer = new Uint8Array(0);
|
|
116
|
+
try {
|
|
117
|
+
while (true) {
|
|
118
|
+
const bufferReadPromises = [];
|
|
119
|
+
if (buffer.length === 0) {
|
|
120
|
+
bufferReadPromises.push(
|
|
121
|
+
reader.read().then((result) => {
|
|
122
|
+
if (!result.done) {
|
|
123
|
+
buffer = result.value;
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (otherBuffer.length === 0) {
|
|
129
|
+
bufferReadPromises.push(
|
|
130
|
+
otherReader.read().then((result) => {
|
|
131
|
+
if (!result.done) {
|
|
132
|
+
otherBuffer = result.value;
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
await Promise.all(bufferReadPromises);
|
|
138
|
+
const haveStreamsEndedTogether = buffer.length === 0 && otherBuffer.length === 0;
|
|
139
|
+
if (haveStreamsEndedTogether) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
const hasOneStreamEndedBeforeTheOther = buffer.length === 0 && otherBuffer.length > 0 || buffer.length > 0 && otherBuffer.length === 0;
|
|
143
|
+
if (hasOneStreamEndedBeforeTheOther) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const minimumByteLength = Math.min(buffer.length, otherBuffer.length);
|
|
147
|
+
for (let byteIndex = 0; byteIndex < minimumByteLength; byteIndex++) {
|
|
148
|
+
if (buffer[byteIndex] !== otherBuffer[byteIndex]) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
buffer = buffer.slice(minimumByteLength);
|
|
153
|
+
otherBuffer = otherBuffer.slice(minimumByteLength);
|
|
154
|
+
}
|
|
155
|
+
} finally {
|
|
156
|
+
reader.releaseLock();
|
|
157
|
+
otherReader.releaseLock();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
var blobEquals_default = blobEquals;
|
|
161
|
+
function isPrimitiveJSONValue(value) {
|
|
162
|
+
return typeof value !== "object" || value === null;
|
|
163
|
+
}
|
|
164
|
+
function jsonContains(value, otherValue) {
|
|
165
|
+
if (isPrimitiveJSONValue(value)) {
|
|
166
|
+
return value === otherValue;
|
|
167
|
+
}
|
|
168
|
+
if (isPrimitiveJSONValue(otherValue)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
if (Array.isArray(value)) {
|
|
172
|
+
if (!Array.isArray(otherValue)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (value.length < otherValue.length) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
let lastMatchedIndex = -1;
|
|
179
|
+
return otherValue.every((otherItem) => {
|
|
180
|
+
for (let index = lastMatchedIndex + 1; index < value.length; index++) {
|
|
181
|
+
if (jsonContains(value[index], otherItem)) {
|
|
182
|
+
lastMatchedIndex = index;
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if (Array.isArray(otherValue)) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
const valueKeys = Object.keys(value);
|
|
193
|
+
const otherValueKeys = Object.keys(otherValue);
|
|
194
|
+
if (valueKeys.length < otherValueKeys.length) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
return otherValueKeys.every((key) => {
|
|
198
|
+
const subValue = value[key];
|
|
199
|
+
const subOtherValue = otherValue[key];
|
|
200
|
+
return jsonContains(subValue, subOtherValue);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
var jsonContains_default = jsonContains;
|
|
204
|
+
function jsonEquals(value, otherValue) {
|
|
205
|
+
if (isPrimitiveJSONValue(value)) {
|
|
206
|
+
return value === otherValue;
|
|
207
|
+
}
|
|
208
|
+
if (isPrimitiveJSONValue(otherValue)) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
if (Array.isArray(value)) {
|
|
212
|
+
if (!Array.isArray(otherValue)) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (value.length !== otherValue.length) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
return value.every((item, index) => jsonEquals(item, otherValue[index]));
|
|
219
|
+
}
|
|
220
|
+
if (Array.isArray(otherValue)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
const valueKeys = Object.keys(value);
|
|
224
|
+
const otherValueKeys = Object.keys(otherValue);
|
|
225
|
+
if (valueKeys.length !== otherValueKeys.length) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
return valueKeys.every((key) => {
|
|
229
|
+
const subValue = value[key];
|
|
230
|
+
const subOtherValue = otherValue[key];
|
|
231
|
+
return jsonEquals(subValue, subOtherValue);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
var jsonEquals_default = jsonEquals;
|
|
110
235
|
|
|
111
|
-
// ../zimic-utils/dist/
|
|
236
|
+
// ../zimic-utils/dist/chunk-W66SQPEM.mjs
|
|
112
237
|
function createCachedDynamicImport(importModuleDynamically) {
|
|
113
238
|
let cachedImportResult;
|
|
114
239
|
return async function importModuleDynamicallyWithCache() {
|
|
@@ -118,7 +243,7 @@ function createCachedDynamicImport(importModuleDynamically) {
|
|
|
118
243
|
}
|
|
119
244
|
var createCachedDynamicImport_default = createCachedDynamicImport;
|
|
120
245
|
|
|
121
|
-
// ../zimic-utils/dist/logging
|
|
246
|
+
// ../zimic-utils/dist/logging.mjs
|
|
122
247
|
var Logger = class _Logger {
|
|
123
248
|
prefix;
|
|
124
249
|
raw;
|
|
@@ -348,142 +473,7 @@ var TimesCheckError = class extends TypeError {
|
|
|
348
473
|
};
|
|
349
474
|
var TimesCheckError_default = TimesCheckError;
|
|
350
475
|
|
|
351
|
-
// ../zimic-utils/dist/
|
|
352
|
-
async function blobEquals(blob, otherBlob) {
|
|
353
|
-
if (blob.type !== otherBlob.type || blob.size !== otherBlob.size) {
|
|
354
|
-
return false;
|
|
355
|
-
}
|
|
356
|
-
const reader = blob.stream().getReader();
|
|
357
|
-
const otherReader = otherBlob.stream().getReader();
|
|
358
|
-
let buffer = new Uint8Array(0);
|
|
359
|
-
let otherBuffer = new Uint8Array(0);
|
|
360
|
-
try {
|
|
361
|
-
while (true) {
|
|
362
|
-
const bufferReadPromises = [];
|
|
363
|
-
if (buffer.length === 0) {
|
|
364
|
-
bufferReadPromises.push(
|
|
365
|
-
reader.read().then((result) => {
|
|
366
|
-
if (!result.done) {
|
|
367
|
-
buffer = result.value;
|
|
368
|
-
}
|
|
369
|
-
})
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
if (otherBuffer.length === 0) {
|
|
373
|
-
bufferReadPromises.push(
|
|
374
|
-
otherReader.read().then((result) => {
|
|
375
|
-
if (!result.done) {
|
|
376
|
-
otherBuffer = result.value;
|
|
377
|
-
}
|
|
378
|
-
})
|
|
379
|
-
);
|
|
380
|
-
}
|
|
381
|
-
await Promise.all(bufferReadPromises);
|
|
382
|
-
const haveStreamsEndedTogether = buffer.length === 0 && otherBuffer.length === 0;
|
|
383
|
-
if (haveStreamsEndedTogether) {
|
|
384
|
-
return true;
|
|
385
|
-
}
|
|
386
|
-
const hasOneStreamEndedBeforeTheOther = buffer.length === 0 && otherBuffer.length > 0 || buffer.length > 0 && otherBuffer.length === 0;
|
|
387
|
-
if (hasOneStreamEndedBeforeTheOther) {
|
|
388
|
-
return false;
|
|
389
|
-
}
|
|
390
|
-
const minimumByteLength = Math.min(buffer.length, otherBuffer.length);
|
|
391
|
-
for (let byteIndex = 0; byteIndex < minimumByteLength; byteIndex++) {
|
|
392
|
-
if (buffer[byteIndex] !== otherBuffer[byteIndex]) {
|
|
393
|
-
return false;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
buffer = buffer.slice(minimumByteLength);
|
|
397
|
-
otherBuffer = otherBuffer.slice(minimumByteLength);
|
|
398
|
-
}
|
|
399
|
-
} finally {
|
|
400
|
-
reader.releaseLock();
|
|
401
|
-
otherReader.releaseLock();
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
var blobEquals_default = blobEquals;
|
|
405
|
-
|
|
406
|
-
// ../zimic-utils/dist/chunk-LBZAJMTR.mjs
|
|
407
|
-
function isPrimitiveJSONValue(value) {
|
|
408
|
-
return typeof value !== "object" || value === null;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// ../zimic-utils/dist/data/jsonContains.mjs
|
|
412
|
-
function jsonContains(value, otherValue) {
|
|
413
|
-
if (isPrimitiveJSONValue(value)) {
|
|
414
|
-
return value === otherValue;
|
|
415
|
-
}
|
|
416
|
-
if (isPrimitiveJSONValue(otherValue)) {
|
|
417
|
-
return false;
|
|
418
|
-
}
|
|
419
|
-
if (Array.isArray(value)) {
|
|
420
|
-
if (!Array.isArray(otherValue)) {
|
|
421
|
-
return false;
|
|
422
|
-
}
|
|
423
|
-
if (value.length < otherValue.length) {
|
|
424
|
-
return false;
|
|
425
|
-
}
|
|
426
|
-
let lastMatchedIndex = -1;
|
|
427
|
-
return otherValue.every((otherItem) => {
|
|
428
|
-
for (let index = lastMatchedIndex + 1; index < value.length; index++) {
|
|
429
|
-
if (jsonContains(value[index], otherItem)) {
|
|
430
|
-
lastMatchedIndex = index;
|
|
431
|
-
return true;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
return false;
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
if (Array.isArray(otherValue)) {
|
|
438
|
-
return false;
|
|
439
|
-
}
|
|
440
|
-
const valueKeys = Object.keys(value);
|
|
441
|
-
const otherValueKeys = Object.keys(otherValue);
|
|
442
|
-
if (valueKeys.length < otherValueKeys.length) {
|
|
443
|
-
return false;
|
|
444
|
-
}
|
|
445
|
-
return otherValueKeys.every((key) => {
|
|
446
|
-
const subValue = value[key];
|
|
447
|
-
const subOtherValue = otherValue[key];
|
|
448
|
-
return jsonContains(subValue, subOtherValue);
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
var jsonContains_default = jsonContains;
|
|
452
|
-
|
|
453
|
-
// ../zimic-utils/dist/data/jsonEquals.mjs
|
|
454
|
-
function jsonEquals(value, otherValue) {
|
|
455
|
-
if (isPrimitiveJSONValue(value)) {
|
|
456
|
-
return value === otherValue;
|
|
457
|
-
}
|
|
458
|
-
if (isPrimitiveJSONValue(otherValue)) {
|
|
459
|
-
return false;
|
|
460
|
-
}
|
|
461
|
-
if (Array.isArray(value)) {
|
|
462
|
-
if (!Array.isArray(otherValue)) {
|
|
463
|
-
return false;
|
|
464
|
-
}
|
|
465
|
-
if (value.length !== otherValue.length) {
|
|
466
|
-
return false;
|
|
467
|
-
}
|
|
468
|
-
return value.every((item, index) => jsonEquals(item, otherValue[index]));
|
|
469
|
-
}
|
|
470
|
-
if (Array.isArray(otherValue)) {
|
|
471
|
-
return false;
|
|
472
|
-
}
|
|
473
|
-
const valueKeys = Object.keys(value);
|
|
474
|
-
const otherValueKeys = Object.keys(otherValue);
|
|
475
|
-
if (valueKeys.length !== otherValueKeys.length) {
|
|
476
|
-
return false;
|
|
477
|
-
}
|
|
478
|
-
return valueKeys.every((key) => {
|
|
479
|
-
const subValue = value[key];
|
|
480
|
-
const subOtherValue = otherValue[key];
|
|
481
|
-
return jsonEquals(subValue, subOtherValue);
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
var jsonEquals_default = jsonEquals;
|
|
485
|
-
|
|
486
|
-
// ../zimic-utils/dist/chunk-QISSVK3C.mjs
|
|
476
|
+
// ../zimic-utils/dist/time.mjs
|
|
487
477
|
function waitForDelay(milliseconds) {
|
|
488
478
|
return new Promise((resolve) => {
|
|
489
479
|
setTimeout(resolve, milliseconds);
|
|
@@ -887,7 +877,7 @@ var LocalHttpRequestHandler = class {
|
|
|
887
877
|
};
|
|
888
878
|
var LocalHttpRequestHandler_default = LocalHttpRequestHandler;
|
|
889
879
|
|
|
890
|
-
// ../zimic-utils/dist/
|
|
880
|
+
// ../zimic-utils/dist/url.mjs
|
|
891
881
|
function createPathCharactersToEscapeRegex() {
|
|
892
882
|
return /([.(){}+$])/g;
|
|
893
883
|
}
|
|
@@ -958,8 +948,6 @@ function createRegexFromPath(path) {
|
|
|
958
948
|
return new RegExp(`^/?${pathRegexContent}/?$`);
|
|
959
949
|
}
|
|
960
950
|
var createRegexFromPath_default = createRegexFromPath;
|
|
961
|
-
|
|
962
|
-
// ../zimic-utils/dist/url/excludeNonPathParams.mjs
|
|
963
951
|
function excludeNonPathParams(url) {
|
|
964
952
|
url.hash = "";
|
|
965
953
|
url.search = "";
|
|
@@ -968,8 +956,29 @@ function excludeNonPathParams(url) {
|
|
|
968
956
|
return url;
|
|
969
957
|
}
|
|
970
958
|
var excludeNonPathParams_default = excludeNonPathParams;
|
|
971
|
-
|
|
972
|
-
|
|
959
|
+
var DuplicatedPathParamError = class extends Error {
|
|
960
|
+
constructor(path, paramName) {
|
|
961
|
+
super(
|
|
962
|
+
`The path parameter '${paramName}' appears more than once in '${path}'. This is not supported. Please make sure that each parameter is unique.`
|
|
963
|
+
);
|
|
964
|
+
this.name = "DuplicatedPathParamError";
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
function validatePathParams(path) {
|
|
968
|
+
const pathParamMatches = path.toString().matchAll(createPathParamRegex());
|
|
969
|
+
const uniqueParamNames = /* @__PURE__ */ new Set();
|
|
970
|
+
for (const paramMatch of pathParamMatches) {
|
|
971
|
+
const paramName = paramMatch.groups?.identifier;
|
|
972
|
+
if (!paramName) {
|
|
973
|
+
continue;
|
|
974
|
+
}
|
|
975
|
+
if (uniqueParamNames.has(paramName)) {
|
|
976
|
+
throw new DuplicatedPathParamError(path, paramName);
|
|
977
|
+
}
|
|
978
|
+
uniqueParamNames.add(paramName);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
var validatePathParams_default = validatePathParams;
|
|
973
982
|
var UnsupportedURLProtocolError = class extends TypeError {
|
|
974
983
|
constructor(protocol, availableProtocols) {
|
|
975
984
|
super(
|
|
@@ -1042,6 +1051,7 @@ var DEFAULT_UNHANDLED_REQUEST_STRATEGY = Object.freeze({
|
|
|
1042
1051
|
});
|
|
1043
1052
|
|
|
1044
1053
|
// src/http/interceptorWorker/HttpInterceptorWorker.ts
|
|
1054
|
+
var RESPONSE_ACTION_SYMBOL = Symbol.for("HttpResponse.action");
|
|
1045
1055
|
var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
1046
1056
|
platform = null;
|
|
1047
1057
|
isRunning = false;
|
|
@@ -1138,17 +1148,57 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
|
1138
1148
|
}
|
|
1139
1149
|
return interceptor.onUnhandledRequest;
|
|
1140
1150
|
}
|
|
1141
|
-
static
|
|
1151
|
+
static setResponseAction(response, action) {
|
|
1152
|
+
Object.defineProperty(response, RESPONSE_ACTION_SYMBOL, {
|
|
1153
|
+
value: action,
|
|
1154
|
+
enumerable: false,
|
|
1155
|
+
configurable: false,
|
|
1156
|
+
writable: false
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
static getResponseAction(response) {
|
|
1160
|
+
if (!(RESPONSE_ACTION_SYMBOL in response)) {
|
|
1161
|
+
return void 0;
|
|
1162
|
+
}
|
|
1163
|
+
const action = response[RESPONSE_ACTION_SYMBOL];
|
|
1164
|
+
if (action !== "bypass" && action !== "reject") {
|
|
1165
|
+
return void 0;
|
|
1166
|
+
}
|
|
1167
|
+
return action;
|
|
1168
|
+
}
|
|
1169
|
+
createBypassedResponse() {
|
|
1170
|
+
const response = Response.redirect("about:blank", 302);
|
|
1171
|
+
_HttpInterceptorWorker.setResponseAction(response, "bypass");
|
|
1172
|
+
return response;
|
|
1173
|
+
}
|
|
1174
|
+
static isBypassedResponse(response) {
|
|
1175
|
+
return this.getResponseAction(response) === "bypass";
|
|
1176
|
+
}
|
|
1177
|
+
createRejectedResponse() {
|
|
1178
|
+
const response = Response.error();
|
|
1179
|
+
_HttpInterceptorWorker.setResponseAction(response, "reject");
|
|
1180
|
+
return response;
|
|
1181
|
+
}
|
|
1182
|
+
static isRejectedResponse(response) {
|
|
1183
|
+
return this.getResponseAction(response) === "reject";
|
|
1184
|
+
}
|
|
1185
|
+
createResponseFromDeclaration(request, declaration) {
|
|
1186
|
+
if ("action" in declaration) {
|
|
1187
|
+
if (declaration.action === "bypass") {
|
|
1188
|
+
return this.createBypassedResponse();
|
|
1189
|
+
} else {
|
|
1190
|
+
return this.createRejectedResponse();
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1142
1193
|
const headers = new HttpHeaders(declaration.headers);
|
|
1143
|
-
const
|
|
1144
|
-
const canHaveBody = methodCanHaveResponseBody(request.method) && status !== 204;
|
|
1194
|
+
const canHaveBody = methodCanHaveResponseBody(request.method) && declaration.status !== 204;
|
|
1145
1195
|
if (!canHaveBody) {
|
|
1146
|
-
return new Response(null, { headers, status });
|
|
1196
|
+
return new Response(null, { headers, status: declaration.status });
|
|
1147
1197
|
}
|
|
1148
1198
|
if (typeof declaration.body === "string" || declaration.body === null || declaration.body === void 0 || declaration.body instanceof FormData || declaration.body instanceof URLSearchParams || declaration.body instanceof Blob || declaration.body instanceof ArrayBuffer || declaration.body instanceof ReadableStream) {
|
|
1149
|
-
return new Response(declaration.body ?? null, { headers, status });
|
|
1199
|
+
return new Response(declaration.body ?? null, { headers, status: declaration.status });
|
|
1150
1200
|
}
|
|
1151
|
-
return Response.json(declaration.body, { headers, status });
|
|
1201
|
+
return Response.json(declaration.body, { headers, status: declaration.status });
|
|
1152
1202
|
}
|
|
1153
1203
|
static async parseRawUnhandledRequest(request) {
|
|
1154
1204
|
return this.parseRawRequest(
|
|
@@ -1577,8 +1627,9 @@ var HttpInterceptorClient = class {
|
|
|
1577
1627
|
if (!responseDeclaration) {
|
|
1578
1628
|
return null;
|
|
1579
1629
|
}
|
|
1580
|
-
const response =
|
|
1581
|
-
|
|
1630
|
+
const response = await this.workerOrThrow.createResponseFromDeclaration(request, responseDeclaration);
|
|
1631
|
+
const shouldSaveInterceptedRequest = this.requestSaving.enabled && response && !HttpInterceptorWorker_default.isRejectedResponse(response);
|
|
1632
|
+
if (shouldSaveInterceptedRequest) {
|
|
1582
1633
|
const responseClone = response.clone();
|
|
1583
1634
|
const parsedResponse = await HttpInterceptorWorker_default.parseRawResponse(responseClone);
|
|
1584
1635
|
matchedHandler.saveInterceptedRequest(parsedRequest, parsedResponse);
|
|
@@ -1653,31 +1704,6 @@ var HttpInterceptorClient = class {
|
|
|
1653
1704
|
}
|
|
1654
1705
|
};
|
|
1655
1706
|
var HttpInterceptorClient_default = HttpInterceptorClient;
|
|
1656
|
-
|
|
1657
|
-
// ../zimic-utils/dist/url/validatePathParams.mjs
|
|
1658
|
-
var DuplicatedPathParamError = class extends Error {
|
|
1659
|
-
constructor(path, paramName) {
|
|
1660
|
-
super(
|
|
1661
|
-
`The path parameter '${paramName}' appears more than once in '${path}'. This is not supported. Please make sure that each parameter is unique.`
|
|
1662
|
-
);
|
|
1663
|
-
this.name = "DuplicatedPathParamError";
|
|
1664
|
-
}
|
|
1665
|
-
};
|
|
1666
|
-
function validatePathParams(path) {
|
|
1667
|
-
const pathParamMatches = path.toString().matchAll(createPathParamRegex());
|
|
1668
|
-
const uniqueParamNames = /* @__PURE__ */ new Set();
|
|
1669
|
-
for (const paramMatch of pathParamMatches) {
|
|
1670
|
-
const paramName = paramMatch.groups?.identifier;
|
|
1671
|
-
if (!paramName) {
|
|
1672
|
-
continue;
|
|
1673
|
-
}
|
|
1674
|
-
if (uniqueParamNames.has(paramName)) {
|
|
1675
|
-
throw new DuplicatedPathParamError(path, paramName);
|
|
1676
|
-
}
|
|
1677
|
-
uniqueParamNames.add(paramName);
|
|
1678
|
-
}
|
|
1679
|
-
}
|
|
1680
|
-
var validatePathParams_default = validatePathParams;
|
|
1681
1707
|
var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
1682
1708
|
internalWorker;
|
|
1683
1709
|
httpHandlersByMethod = {
|
|
@@ -1795,7 +1821,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
1795
1821
|
const requestClone = request.clone();
|
|
1796
1822
|
let response = null;
|
|
1797
1823
|
try {
|
|
1798
|
-
response = await createResponse({
|
|
1824
|
+
response = await createResponse({ request });
|
|
1799
1825
|
} catch (error) {
|
|
1800
1826
|
console.error(error);
|
|
1801
1827
|
}
|
|
@@ -1814,6 +1840,23 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
1814
1840
|
};
|
|
1815
1841
|
methodHandlers.push(handler);
|
|
1816
1842
|
}
|
|
1843
|
+
async createResponseFromDeclaration(request, declaration) {
|
|
1844
|
+
const requestClone = request.clone();
|
|
1845
|
+
const response = await super.createResponseFromDeclaration(request, declaration);
|
|
1846
|
+
if (response && HttpInterceptorWorker_default.isBypassedResponse(response)) {
|
|
1847
|
+
try {
|
|
1848
|
+
const response2 = await fetch(bypass(requestClone));
|
|
1849
|
+
return response2;
|
|
1850
|
+
} catch (error) {
|
|
1851
|
+
console.error(error);
|
|
1852
|
+
return null;
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
if (response && HttpInterceptorWorker_default.isRejectedResponse(response)) {
|
|
1856
|
+
return response;
|
|
1857
|
+
}
|
|
1858
|
+
return response;
|
|
1859
|
+
}
|
|
1817
1860
|
async createResponseForRequest(request) {
|
|
1818
1861
|
const methodHandlers = this.httpHandlersByMethod[request.method];
|
|
1819
1862
|
const requestURL = excludeNonPathParams_default(new URL(request.url));
|
|
@@ -1871,6 +1914,17 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
1871
1914
|
};
|
|
1872
1915
|
var LocalHttpInterceptorWorker_default = LocalHttpInterceptorWorker;
|
|
1873
1916
|
|
|
1917
|
+
// src/server/errors/UnsupportedResponseBypassError.ts
|
|
1918
|
+
var UnsupportedResponseBypassError = class extends Error {
|
|
1919
|
+
constructor() {
|
|
1920
|
+
super(
|
|
1921
|
+
"Remote interceptors cannot bypass responses. Use `{ action: 'reject' }` instead.\n\nLearn more: https://zimic.dev/docs/interceptor/api/http-request-handler#handlerrespond"
|
|
1922
|
+
);
|
|
1923
|
+
this.name = "UnsupportedResponseBypassError";
|
|
1924
|
+
}
|
|
1925
|
+
};
|
|
1926
|
+
var UnsupportedResponseBypassError_default = UnsupportedResponseBypassError;
|
|
1927
|
+
|
|
1874
1928
|
// src/utils/crypto.ts
|
|
1875
1929
|
var importCrypto = createCachedDynamicImport_default(async () => {
|
|
1876
1930
|
const globalCrypto = globalThis.crypto;
|
|
@@ -1898,6 +1952,8 @@ async function serializeResponse(response) {
|
|
|
1898
1952
|
const responseClone = response.clone();
|
|
1899
1953
|
const serializedBody = responseClone.body ? convertArrayBufferToBase64(await responseClone.arrayBuffer()) : null;
|
|
1900
1954
|
return {
|
|
1955
|
+
type: response.type,
|
|
1956
|
+
action: HttpInterceptorWorker_default.getResponseAction(response),
|
|
1901
1957
|
status: response.status,
|
|
1902
1958
|
statusText: response.statusText,
|
|
1903
1959
|
headers: Object.fromEntries(response.headers),
|
|
@@ -2374,8 +2430,8 @@ var RemoteHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
2374
2430
|
const request = deserializeRequest(serializedRequest);
|
|
2375
2431
|
try {
|
|
2376
2432
|
const rawResponse = await handler?.createResponse({ request }) ?? null;
|
|
2377
|
-
|
|
2378
|
-
|
|
2433
|
+
if (rawResponse) {
|
|
2434
|
+
const response = methodCanHaveResponseBody(request.method) ? rawResponse : new Response(null, rawResponse);
|
|
2379
2435
|
return { response: await serializeResponse(response) };
|
|
2380
2436
|
}
|
|
2381
2437
|
} catch (error) {
|
|
@@ -2422,10 +2478,7 @@ var RemoteHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
2422
2478
|
method,
|
|
2423
2479
|
path,
|
|
2424
2480
|
interceptor,
|
|
2425
|
-
|
|
2426
|
-
const response = await createResponse(context);
|
|
2427
|
-
return response;
|
|
2428
|
-
}
|
|
2481
|
+
createResponse
|
|
2429
2482
|
};
|
|
2430
2483
|
this.httpHandlers.set(handler.id, handler);
|
|
2431
2484
|
await this.webSocketClient.request("interceptors/workers/commit", {
|
|
@@ -2435,6 +2488,16 @@ var RemoteHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
|
|
|
2435
2488
|
path: handler.path
|
|
2436
2489
|
});
|
|
2437
2490
|
}
|
|
2491
|
+
async createResponseFromDeclaration(request, declaration) {
|
|
2492
|
+
const response = await super.createResponseFromDeclaration(request, declaration);
|
|
2493
|
+
if (response && HttpInterceptorWorker_default.isBypassedResponse(response)) {
|
|
2494
|
+
throw new UnsupportedResponseBypassError_default();
|
|
2495
|
+
}
|
|
2496
|
+
if (response && HttpInterceptorWorker_default.isRejectedResponse(response)) {
|
|
2497
|
+
return response;
|
|
2498
|
+
}
|
|
2499
|
+
return response;
|
|
2500
|
+
}
|
|
2438
2501
|
async clearHandlers(options = {}) {
|
|
2439
2502
|
if (!this.isRunning) {
|
|
2440
2503
|
throw new NotRunningHttpInterceptorError_default();
|
|
@@ -2794,6 +2857,10 @@ var InvalidFormDataError = class extends InvalidFormDataError$1 {
|
|
|
2794
2857
|
};
|
|
2795
2858
|
var InvalidJSONError = class extends InvalidJSONError$1 {
|
|
2796
2859
|
};
|
|
2860
|
+
/* istanbul ignore else -- @preserve
|
|
2861
|
+
* The else is a fallback for when the error is not an instance of Error. */
|
|
2862
|
+
/* istanbul ignore if -- @preserve
|
|
2863
|
+
* This is just a type guard to ensure the value is valid. In practice, this condition should never be true. */
|
|
2797
2864
|
/* istanbul ignore next -- @preserve
|
|
2798
2865
|
* Ignoring because there will always be a handler for the given method and path at this point. */
|
|
2799
2866
|
/* istanbul ignore if -- @preserve
|