Spaces:
Paused
Paused
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 |