Spaces:
Sleeping
Sleeping
File size: 6,287 Bytes
cd6720a | 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 | let _open_draft_logger = require("@open-draft/logger");
let strict_event_emitter = require("strict-event-emitter");
//#region src/Interceptor.ts
/**
* Request header name to detect when a single request
* is being handled by nested interceptors (XHR -> ClientRequest).
* Obscure by design to prevent collisions with user-defined headers.
* Ideally, come up with the Interceptor-level mechanism for this.
* @see https://github.com/mswjs/interceptors/issues/378
*/
const INTERNAL_REQUEST_ID_HEADER_NAME = "x-interceptors-internal-request-id";
function getGlobalSymbol(symbol) {
return globalThis[symbol] || void 0;
}
function setGlobalSymbol(symbol, value) {
globalThis[symbol] = value;
}
function deleteGlobalSymbol(symbol) {
delete globalThis[symbol];
}
let InterceptorReadyState = /* @__PURE__ */ function(InterceptorReadyState$1) {
InterceptorReadyState$1["INACTIVE"] = "INACTIVE";
InterceptorReadyState$1["APPLYING"] = "APPLYING";
InterceptorReadyState$1["APPLIED"] = "APPLIED";
InterceptorReadyState$1["DISPOSING"] = "DISPOSING";
InterceptorReadyState$1["DISPOSED"] = "DISPOSED";
return InterceptorReadyState$1;
}({});
var Interceptor = class {
constructor(symbol) {
this.symbol = symbol;
this.readyState = InterceptorReadyState.INACTIVE;
this.emitter = new strict_event_emitter.Emitter();
this.subscriptions = [];
this.logger = new _open_draft_logger.Logger(symbol.description);
this.emitter.setMaxListeners(0);
this.logger.info("constructing the interceptor...");
}
/**
* Determine if this interceptor can be applied
* in the current environment.
*/
checkEnvironment() {
return true;
}
/**
* Apply this interceptor to the current process.
* Returns an already running interceptor instance if it's present.
*/
apply() {
const logger = this.logger.extend("apply");
logger.info("applying the interceptor...");
if (this.readyState === InterceptorReadyState.APPLIED) {
logger.info("intercepted already applied!");
return;
}
if (!this.checkEnvironment()) {
logger.info("the interceptor cannot be applied in this environment!");
return;
}
this.readyState = InterceptorReadyState.APPLYING;
const runningInstance = this.getInstance();
if (runningInstance) {
logger.info("found a running instance, reusing...");
this.on = (event, listener) => {
logger.info("proxying the \"%s\" listener", event);
runningInstance.emitter.addListener(event, listener);
this.subscriptions.push(() => {
runningInstance.emitter.removeListener(event, listener);
logger.info("removed proxied \"%s\" listener!", event);
});
return this;
};
this.readyState = InterceptorReadyState.APPLIED;
return;
}
logger.info("no running instance found, setting up a new instance...");
this.setup();
this.setInstance();
this.readyState = InterceptorReadyState.APPLIED;
}
/**
* Setup the module augments and stubs necessary for this interceptor.
* This method is not run if there's a running interceptor instance
* to prevent instantiating an interceptor multiple times.
*/
setup() {}
/**
* Listen to the interceptor's public events.
*/
on(event, listener) {
const logger = this.logger.extend("on");
if (this.readyState === InterceptorReadyState.DISPOSING || this.readyState === InterceptorReadyState.DISPOSED) {
logger.info("cannot listen to events, already disposed!");
return this;
}
logger.info("adding \"%s\" event listener:", event, listener);
this.emitter.on(event, listener);
return this;
}
once(event, listener) {
this.emitter.once(event, listener);
return this;
}
off(event, listener) {
this.emitter.off(event, listener);
return this;
}
removeAllListeners(event) {
this.emitter.removeAllListeners(event);
return this;
}
/**
* Disposes of any side-effects this interceptor has introduced.
*/
dispose() {
const logger = this.logger.extend("dispose");
if (this.readyState === InterceptorReadyState.DISPOSED) {
logger.info("cannot dispose, already disposed!");
return;
}
logger.info("disposing the interceptor...");
this.readyState = InterceptorReadyState.DISPOSING;
if (!this.getInstance()) {
logger.info("no interceptors running, skipping dispose...");
return;
}
this.clearInstance();
logger.info("global symbol deleted:", getGlobalSymbol(this.symbol));
if (this.subscriptions.length > 0) {
logger.info("disposing of %d subscriptions...", this.subscriptions.length);
for (const dispose of this.subscriptions) dispose();
this.subscriptions = [];
logger.info("disposed of all subscriptions!", this.subscriptions.length);
}
this.emitter.removeAllListeners();
logger.info("destroyed the listener!");
this.readyState = InterceptorReadyState.DISPOSED;
}
getInstance() {
const instance = getGlobalSymbol(this.symbol);
this.logger.info("retrieved global instance:", instance?.constructor?.name);
return instance;
}
setInstance() {
setGlobalSymbol(this.symbol, this);
this.logger.info("set global instance!", this.symbol.description);
}
clearInstance() {
deleteGlobalSymbol(this.symbol);
this.logger.info("cleared global instance!", this.symbol.description);
}
};
//#endregion
//#region src/createRequestId.ts
/**
* Generate a random ID string to represent a request.
* @example
* createRequestId()
* // "f774b6c9c600f"
*/
function createRequestId() {
return Math.random().toString(16).slice(2);
}
//#endregion
Object.defineProperty(exports, 'INTERNAL_REQUEST_ID_HEADER_NAME', {
enumerable: true,
get: function () {
return INTERNAL_REQUEST_ID_HEADER_NAME;
}
});
Object.defineProperty(exports, 'Interceptor', {
enumerable: true,
get: function () {
return Interceptor;
}
});
Object.defineProperty(exports, 'InterceptorReadyState', {
enumerable: true,
get: function () {
return InterceptorReadyState;
}
});
Object.defineProperty(exports, 'createRequestId', {
enumerable: true,
get: function () {
return createRequestId;
}
});
Object.defineProperty(exports, 'deleteGlobalSymbol', {
enumerable: true,
get: function () {
return deleteGlobalSymbol;
}
});
Object.defineProperty(exports, 'getGlobalSymbol', {
enumerable: true,
get: function () {
return getGlobalSymbol;
}
});
//# sourceMappingURL=createRequestId-DOf8Ktjs.cjs.map |