Skip to content

Commit 96fb1fc

Browse files
author
Takashi Kato
committed
Update @mswjs/interceptors to v0.37.5
1 parent 0a07fd7 commit 96fb1fc

File tree

2 files changed

+178
-47
lines changed

2 files changed

+178
-47
lines changed

vendor/javascripts/@mswjs--interceptors--presets--browser.js

Lines changed: 100 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -837,23 +837,6 @@ function isPropertyAccessible(obj, key) {
837837
}
838838

839839
// src/utils/responseUtils.ts
840-
var RESPONSE_STATUS_CODES_WITHOUT_BODY = /* @__PURE__ */ new Set([
841-
101,
842-
103,
843-
204,
844-
205,
845-
304
846-
]);
847-
var RESPONSE_STATUS_CODES_WITH_REDIRECT = /* @__PURE__ */ new Set([
848-
301,
849-
302,
850-
303,
851-
307,
852-
308
853-
]);
854-
function isResponseWithoutBody(status) {
855-
return RESPONSE_STATUS_CODES_WITHOUT_BODY.has(status);
856-
}
857840
function createServerErrorResponse(body) {
858841
return new Response(
859842
JSON.stringify(
@@ -922,13 +905,17 @@ async function handleRequest(options) {
922905
});
923906
const requestAbortPromise = new DeferredPromise();
924907
if (options.request.signal) {
925-
options.request.signal.addEventListener(
926-
"abort",
927-
() => {
928-
requestAbortPromise.reject(options.request.signal.reason);
929-
},
930-
{ once: true }
931-
);
908+
if (options.request.signal.aborted) {
909+
requestAbortPromise.reject(options.request.signal.reason);
910+
} else {
911+
options.request.signal.addEventListener(
912+
"abort",
913+
() => {
914+
requestAbortPromise.reject(options.request.signal.reason);
915+
},
916+
{ once: true }
917+
);
918+
}
932919
}
933920
const result = await until(async () => {
934921
const requestListtenersPromise = emitAsync(options.emitter, "request", {
@@ -1147,6 +1134,12 @@ function hasConfigurableGlobal(propertyName) {
11471134
if (typeof descriptor === "undefined") {
11481135
return false;
11491136
}
1137+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") {
1138+
return false;
1139+
}
1140+
if (typeof descriptor.get === "undefined" && descriptor.value == null) {
1141+
return false;
1142+
}
11501143
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
11511144
console.error(
11521145
`[MSW] Failed to apply interceptor: the global \`${propertyName}\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`
@@ -1156,6 +1149,83 @@ function hasConfigurableGlobal(propertyName) {
11561149
return true;
11571150
}
11581151

1152+
// src/utils/fetchUtils.ts
1153+
var FetchResponse = class _FetchResponse extends Response {
1154+
static {
1155+
/**
1156+
* Response status codes for responses that cannot have body.
1157+
* @see https://fetch.spec.whatwg.org/#statuses
1158+
*/
1159+
this.STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304];
1160+
}
1161+
static {
1162+
this.STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308];
1163+
}
1164+
static isConfigurableStatusCode(status) {
1165+
return status >= 200 && status <= 599;
1166+
}
1167+
static isRedirectResponse(status) {
1168+
return _FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status);
1169+
}
1170+
/**
1171+
* Returns a boolean indicating whether the given response status
1172+
* code represents a response that can have a body.
1173+
*/
1174+
static isResponseWithBody(status) {
1175+
return !_FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status);
1176+
}
1177+
static setUrl(url, response) {
1178+
if (!url) {
1179+
return;
1180+
}
1181+
if (response.url != "") {
1182+
return;
1183+
}
1184+
Object.defineProperty(response, "url", {
1185+
value: url,
1186+
enumerable: true,
1187+
configurable: true,
1188+
writable: false
1189+
});
1190+
}
1191+
/**
1192+
* Parses the given raw HTTP headers into a Fetch API `Headers` instance.
1193+
*/
1194+
static parseRawHeaders(rawHeaders) {
1195+
const headers = new Headers();
1196+
for (let line = 0; line < rawHeaders.length; line += 2) {
1197+
headers.append(rawHeaders[line], rawHeaders[line + 1]);
1198+
}
1199+
return headers;
1200+
}
1201+
constructor(body, init = {}) {
1202+
const status = init.status ?? 200;
1203+
const safeStatus = _FetchResponse.isConfigurableStatusCode(status) ? status : 200;
1204+
const finalBody = _FetchResponse.isResponseWithBody(status) ? body : null;
1205+
super(finalBody, {
1206+
...init,
1207+
status: safeStatus
1208+
});
1209+
if (status !== safeStatus) {
1210+
const stateSymbol = Object.getOwnPropertySymbols(this).find(
1211+
(symbol) => symbol.description === "state"
1212+
);
1213+
if (stateSymbol) {
1214+
const state = Reflect.get(this, stateSymbol);
1215+
Reflect.set(state, "status", status);
1216+
} else {
1217+
Object.defineProperty(this, "status", {
1218+
value: status,
1219+
enumerable: true,
1220+
configurable: true,
1221+
writable: false
1222+
});
1223+
}
1224+
}
1225+
_FetchResponse.setUrl(init.url, this);
1226+
}
1227+
};
1228+
11591229
// src/interceptors/fetch/index.ts
11601230
var FetchInterceptor = class _FetchInterceptor extends Interceptor {
11611231
static {
@@ -1195,8 +1265,9 @@ var FetchInterceptor = class _FetchInterceptor extends Interceptor {
11951265
rawResponse
11961266
});
11971267
const decompressedStream = decompressResponse(rawResponse);
1198-
const response = decompressedStream === null ? rawResponse : new Response(decompressedStream, rawResponse);
1199-
if (RESPONSE_STATUS_CODES_WITH_REDIRECT.has(response.status)) {
1268+
const response = decompressedStream === null ? rawResponse : new FetchResponse(decompressedStream, rawResponse);
1269+
FetchResponse.setUrl(request.url, response);
1270+
if (FetchResponse.isRedirectResponse(response.status)) {
12001271
if (request.redirect === "error") {
12011272
responsePromise.reject(createNetworkError("unexpected redirect"));
12021273
return;
@@ -1213,12 +1284,6 @@ var FetchInterceptor = class _FetchInterceptor extends Interceptor {
12131284
return;
12141285
}
12151286
}
1216-
Object.defineProperty(response, "url", {
1217-
writable: false,
1218-
enumerable: true,
1219-
configurable: false,
1220-
value: request.url
1221-
});
12221287
if (this.emitter.listenerCount("response") > 0) {
12231288
this.logger.info('emitting the "response" event...');
12241289
await emitAsync(this.emitter, "response", {
@@ -1477,8 +1542,9 @@ function parseJson(data) {
14771542

14781543
// src/interceptors/XMLHttpRequest/utils/createResponse.ts
14791544
function createResponse(request, body) {
1480-
const responseBodyOrNull = isResponseWithoutBody(request.status) ? null : body;
1481-
return new Response(responseBodyOrNull, {
1545+
const responseBodyOrNull = FetchResponse.isResponseWithBody(request.status) ? body : null;
1546+
return new FetchResponse(responseBodyOrNull, {
1547+
url: request.responseURL,
14821548
status: request.status,
14831549
statusText: request.statusText,
14841550
headers: createHeadersFromXMLHttpReqestHeaders(

vendor/javascripts/@mswjs--interceptors.js

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -752,19 +752,85 @@ function decodeBuffer(buffer, encoding) {
752752
return decoder.decode(buffer);
753753
}
754754

755-
// src/utils/responseUtils.ts
756-
var RESPONSE_STATUS_CODES_WITHOUT_BODY = /* @__PURE__ */ new Set([
757-
101,
758-
103,
759-
204,
760-
205,
761-
304
762-
]);
763-
function isResponseWithoutBody(status) {
764-
return RESPONSE_STATUS_CODES_WITHOUT_BODY.has(status);
765-
}
755+
// src/utils/fetchUtils.ts
756+
var FetchResponse = class _FetchResponse extends Response {
757+
static {
758+
/**
759+
* Response status codes for responses that cannot have body.
760+
* @see https://fetch.spec.whatwg.org/#statuses
761+
*/
762+
this.STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304];
763+
}
764+
static {
765+
this.STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308];
766+
}
767+
static isConfigurableStatusCode(status) {
768+
return status >= 200 && status <= 599;
769+
}
770+
static isRedirectResponse(status) {
771+
return _FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status);
772+
}
773+
/**
774+
* Returns a boolean indicating whether the given response status
775+
* code represents a response that can have a body.
776+
*/
777+
static isResponseWithBody(status) {
778+
return !_FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status);
779+
}
780+
static setUrl(url, response) {
781+
if (!url) {
782+
return;
783+
}
784+
if (response.url != "") {
785+
return;
786+
}
787+
Object.defineProperty(response, "url", {
788+
value: url,
789+
enumerable: true,
790+
configurable: true,
791+
writable: false
792+
});
793+
}
794+
/**
795+
* Parses the given raw HTTP headers into a Fetch API `Headers` instance.
796+
*/
797+
static parseRawHeaders(rawHeaders) {
798+
const headers = new Headers();
799+
for (let line = 0; line < rawHeaders.length; line += 2) {
800+
headers.append(rawHeaders[line], rawHeaders[line + 1]);
801+
}
802+
return headers;
803+
}
804+
constructor(body, init = {}) {
805+
const status = init.status ?? 200;
806+
const safeStatus = _FetchResponse.isConfigurableStatusCode(status) ? status : 200;
807+
const finalBody = _FetchResponse.isResponseWithBody(status) ? body : null;
808+
super(finalBody, {
809+
...init,
810+
status: safeStatus
811+
});
812+
if (status !== safeStatus) {
813+
const stateSymbol = Object.getOwnPropertySymbols(this).find(
814+
(symbol) => symbol.description === "state"
815+
);
816+
if (stateSymbol) {
817+
const state = Reflect.get(this, stateSymbol);
818+
Reflect.set(state, "status", status);
819+
} else {
820+
Object.defineProperty(this, "status", {
821+
value: status,
822+
enumerable: true,
823+
configurable: true,
824+
writable: false
825+
});
826+
}
827+
}
828+
_FetchResponse.setUrl(init.url, this);
829+
}
830+
};
766831
export {
767832
BatchInterceptor,
833+
FetchResponse,
768834
INTERNAL_REQUEST_ID_HEADER_NAME,
769835
IS_PATCHED_MODULE,
770836
Interceptor,
@@ -774,6 +840,5 @@ export {
774840
deleteGlobalSymbol,
775841
encodeBuffer,
776842
getCleanUrl,
777-
getGlobalSymbol,
778-
isResponseWithoutBody
843+
getGlobalSymbol
779844
};

0 commit comments

Comments
 (0)