Spaces:
Paused
Paused
File size: 7,463 Bytes
9ada4bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkMQJ3JOOKjs = require('./chunk-MQJ3JOOK.js');
var _chunk2CRB3JAQjs = require('./chunk-2CRB3JAQ.js');
var _chunkTIPR373Rjs = require('./chunk-TIPR373R.js');
// src/interceptors/fetch/index.ts
var _outvariant = require('outvariant');
var _deferredpromise = require('@open-draft/deferred-promise');
var _until = require('@open-draft/until');
// src/utils/canParseUrl.ts
function canParseUrl(url) {
try {
new URL(url);
return true;
} catch (_error) {
return false;
}
}
// src/interceptors/fetch/index.ts
var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
constructor() {
super(_FetchInterceptor.symbol);
}
checkEnvironment() {
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
}
async setup() {
const pureFetch = globalThis.fetch;
_outvariant.invariant.call(void 0,
!pureFetch[_chunk2CRB3JAQjs.IS_PATCHED_MODULE],
'Failed to patch the "fetch" module: already patched.'
);
globalThis.fetch = async (input, init) => {
var _a;
const requestId = _chunkTIPR373Rjs.createRequestId.call(void 0, );
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
const request = new Request(resolvedInput, init);
this.logger.info("[%s] %s", request.method, request.url);
const { interactiveRequest, requestController } = _chunkMQJ3JOOKjs.toInteractiveRequest.call(void 0, request);
this.logger.info(
'emitting the "request" event for %d listener(s)...',
this.emitter.listenerCount("request")
);
this.emitter.once("request", ({ requestId: pendingRequestId }) => {
if (pendingRequestId !== requestId) {
return;
}
if (requestController.responsePromise.state === "pending") {
requestController.responsePromise.resolve(void 0);
}
});
this.logger.info("awaiting for the mocked response...");
const signal = interactiveRequest.signal;
const requestAborted = new (0, _deferredpromise.DeferredPromise)();
if (signal) {
signal.addEventListener(
"abort",
() => {
requestAborted.reject(signal.reason);
},
{ once: true }
);
}
const responsePromise = new (0, _deferredpromise.DeferredPromise)();
const respondWith = (response) => {
this.logger.info("responding with a mock response:", response);
if (this.emitter.listenerCount("response") > 0) {
this.logger.info('emitting the "response" event...');
const responseClone = response.clone();
this.emitter.emit("response", {
response: responseClone,
isMockedResponse: true,
request: interactiveRequest,
requestId
});
}
Object.defineProperty(response, "url", {
writable: false,
enumerable: true,
configurable: false,
value: request.url
});
responsePromise.resolve(response);
};
const errorWith = (reason) => {
responsePromise.reject(reason);
};
const resolverResult = await _until.until.call(void 0,
async () => {
const listenersFinished = _chunkMQJ3JOOKjs.emitAsync.call(void 0, this.emitter, "request", {
request: interactiveRequest,
requestId
});
await Promise.race([
requestAborted,
// Put the listeners invocation Promise in the same race condition
// with the request abort Promise because otherwise awaiting the listeners
// would always yield some response (or undefined).
listenersFinished,
requestController.responsePromise
]);
this.logger.info("all request listeners have been resolved!");
const mockedResponse2 = await requestController.responsePromise;
this.logger.info("event.respondWith called with:", mockedResponse2);
return mockedResponse2;
}
);
if (requestAborted.state === "rejected") {
this.logger.info(
"request has been aborted:",
requestAborted.rejectionReason
);
responsePromise.reject(requestAborted.rejectionReason);
return responsePromise;
}
if (resolverResult.error) {
this.logger.info(
"request listerner threw an error:",
resolverResult.error
);
if (resolverResult.error instanceof Response) {
if (_chunk2CRB3JAQjs.isResponseError.call(void 0, resolverResult.error)) {
errorWith(createNetworkError(resolverResult.error));
} else {
respondWith(resolverResult.error);
}
}
if (this.emitter.listenerCount("unhandledException") > 0) {
await _chunkMQJ3JOOKjs.emitAsync.call(void 0, this.emitter, "unhandledException", {
error: resolverResult.error,
request,
requestId,
controller: {
respondWith,
errorWith
}
});
if (responsePromise.state !== "pending") {
return responsePromise;
}
}
respondWith(_chunk2CRB3JAQjs.createServerErrorResponse.call(void 0, resolverResult.error));
return responsePromise;
}
const mockedResponse = resolverResult.data;
if (mockedResponse && !((_a = request.signal) == null ? void 0 : _a.aborted)) {
this.logger.info("received mocked response:", mockedResponse);
if (_chunk2CRB3JAQjs.isResponseError.call(void 0, mockedResponse)) {
this.logger.info(
"received a network error response, rejecting the request promise..."
);
errorWith(createNetworkError(mockedResponse));
} else {
respondWith(mockedResponse);
}
return responsePromise;
}
this.logger.info("no mocked response received!");
return pureFetch(request).then((response) => {
this.logger.info("original fetch performed", response);
if (this.emitter.listenerCount("response") > 0) {
this.logger.info('emitting the "response" event...');
const responseClone = response.clone();
this.emitter.emit("response", {
response: responseClone,
isMockedResponse: false,
request: interactiveRequest,
requestId
});
}
return response;
});
};
Object.defineProperty(globalThis.fetch, _chunk2CRB3JAQjs.IS_PATCHED_MODULE, {
enumerable: true,
configurable: true,
value: true
});
this.subscriptions.push(() => {
Object.defineProperty(globalThis.fetch, _chunk2CRB3JAQjs.IS_PATCHED_MODULE, {
value: void 0
});
globalThis.fetch = pureFetch;
this.logger.info(
'restored native "globalThis.fetch"!',
globalThis.fetch.name
);
});
}
};
var FetchInterceptor = _FetchInterceptor;
FetchInterceptor.symbol = Symbol("fetch");
function createNetworkError(cause) {
return Object.assign(new TypeError("Failed to fetch"), {
cause
});
}
exports.FetchInterceptor = FetchInterceptor;
//# sourceMappingURL=chunk-WBHIW62P.js.map |