@mswjs/interceptors 0.25.1 → 0.25.3
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/lib/browser/Interceptor-8d5fd4c6.d.ts +86 -0
- package/lib/browser/{chunk-ANLPTCZ5.mjs → chunk-AN3YI76R.mjs} +33 -26
- package/lib/browser/{chunk-4CFMDU7Z.js → chunk-CWVY2E3W.js} +20 -134
- package/lib/browser/{chunk-OSIUQA4X.js → chunk-FFBQOFWV.js} +38 -31
- package/lib/browser/{chunk-VMXB5F2J.mjs → chunk-HXJPKJY3.mjs} +25 -15
- package/lib/browser/{chunk-DBFLI5DJ.js → chunk-KITNLK66.js} +30 -20
- package/lib/browser/chunk-KK6APRON.mjs +58 -0
- package/lib/browser/{chunk-GXJLJMOT.mjs → chunk-QPMXOLDO.mjs} +21 -135
- package/lib/browser/chunk-X3NRJIZW.js +58 -0
- package/lib/browser/index.d.ts +7 -3
- package/lib/browser/index.js +24 -5
- package/lib/browser/index.mjs +22 -3
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +4 -3
- package/lib/browser/interceptors/XMLHttpRequest/index.js +4 -4
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/browser/interceptors/fetch/index.d.ts +2 -1
- package/lib/browser/interceptors/fetch/index.js +4 -4
- package/lib/browser/interceptors/fetch/index.mjs +3 -3
- package/lib/browser/presets/browser.d.ts +2 -1
- package/lib/browser/presets/browser.js +6 -6
- package/lib/browser/presets/browser.mjs +4 -4
- package/lib/node/{BatchInterceptor-c841b068.d.ts → BatchInterceptor-9785c567.d.ts} +5 -2
- package/lib/node/Interceptor-7a701c1f.d.ts +86 -0
- package/lib/node/RemoteHttpInterceptor.d.ts +3 -2
- package/lib/node/RemoteHttpInterceptor.js +19 -18
- package/lib/node/RemoteHttpInterceptor.mjs +15 -14
- package/lib/node/chunk-3IYIKC3X.mjs +6 -0
- package/lib/node/{chunk-XYZRP5S2.js → chunk-3XFLRXRY.js} +20 -134
- package/lib/node/chunk-5PTPJLB7.js +58 -0
- package/lib/node/{chunk-HSCXCLVT.mjs → chunk-FB53TMYN.mjs} +33 -26
- package/lib/node/{chunk-RGYCLCLK.mjs → chunk-GM3YBSM3.mjs} +21 -135
- package/lib/node/{chunk-E6YC337Q.js → chunk-JCWVLTP7.js} +35 -28
- package/lib/node/{chunk-OL7OR4RL.mjs → chunk-MCO3RLQC.mjs} +48 -26
- package/lib/node/{chunk-3MYUI4B2.js → chunk-NCHFM2TB.js} +50 -28
- package/lib/node/chunk-OGN3ZR35.js +6 -0
- package/lib/node/{chunk-VS3GJPUE.mjs → chunk-UBEFEZXT.mjs} +22 -3
- package/lib/node/{chunk-MVPEJK4V.js → chunk-UF7QIAQ5.js} +23 -4
- package/lib/node/chunk-YQGTMMOZ.mjs +58 -0
- package/lib/node/index.d.ts +3 -2
- package/lib/node/index.js +3 -3
- package/lib/node/index.mjs +2 -2
- package/lib/node/interceptors/ClientRequest/index.d.ts +4 -3
- package/lib/node/interceptors/ClientRequest/index.js +5 -4
- package/lib/node/interceptors/ClientRequest/index.mjs +4 -3
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +4 -3
- package/lib/node/interceptors/XMLHttpRequest/index.js +5 -4
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +4 -3
- package/lib/node/interceptors/fetch/index.d.ts +2 -1
- package/lib/node/interceptors/fetch/index.js +27 -17
- package/lib/node/interceptors/fetch/index.mjs +25 -15
- package/lib/node/presets/node.d.ts +3 -2
- package/lib/node/presets/node.js +7 -6
- package/lib/node/presets/node.mjs +5 -4
- package/package.json +2 -2
- package/src/BatchInterceptor.test.ts +141 -0
- package/src/BatchInterceptor.ts +38 -4
- package/src/Interceptor.test.ts +46 -0
- package/src/Interceptor.ts +35 -16
- package/src/RemoteHttpInterceptor.ts +11 -9
- package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +10 -10
- package/src/interceptors/ClientRequest/NodeClientRequest.ts +80 -43
- package/src/interceptors/ClientRequest/http.get.ts +3 -1
- package/src/interceptors/ClientRequest/http.request.ts +3 -1
- package/src/interceptors/ClientRequest/index.test.ts +2 -3
- package/src/interceptors/ClientRequest/index.ts +2 -2
- package/src/interceptors/ClientRequest/utils/createRequest.test.ts +2 -2
- package/src/interceptors/ClientRequest/utils/createResponse.test.ts +23 -0
- package/src/interceptors/ClientRequest/utils/createResponse.ts +18 -13
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +29 -25
- package/src/interceptors/XMLHttpRequest/index.ts +2 -2
- package/src/interceptors/XMLHttpRequest/utils/createResponse.ts +4 -2
- package/src/interceptors/fetch/index.ts +26 -13
- package/src/utils/RequestController.ts +21 -0
- package/src/utils/emitAsync.ts +25 -0
- package/src/utils/responseUtils.ts +5 -0
- package/src/utils/toInteractiveRequest.ts +17 -23
- package/lib/browser/Interceptor-0a020bc4.d.ts +0 -116
- package/lib/browser/chunk-PCFJD76X.js +0 -64
- package/lib/browser/chunk-RT3ATOJH.mjs +0 -64
- package/lib/node/Interceptor-738f79c5.d.ts +0 -116
- package/lib/node/chunk-STA6QBYM.mjs +0 -64
- package/lib/node/chunk-ZJOF5MEZ.js +0 -64
- package/src/utils/AsyncEventEmitter.test.ts +0 -102
- package/src/utils/AsyncEventEmitter.ts +0 -193
- package/src/utils/createLazyCallback.ts +0 -49
|
@@ -1,29 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createLazyCallback, LazyCallback } from './createLazyCallback'
|
|
3
|
-
|
|
4
|
-
type LazyResponseCallback = (response: Response) => void
|
|
1
|
+
import { RequestController } from './RequestController'
|
|
5
2
|
|
|
6
3
|
export type InteractiveRequest = globalThis.Request & {
|
|
7
|
-
respondWith:
|
|
4
|
+
respondWith: RequestController['respondWith']
|
|
8
5
|
}
|
|
9
6
|
|
|
10
|
-
export function toInteractiveRequest(request: Request):
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
request.url
|
|
22
|
-
)
|
|
23
|
-
)
|
|
24
|
-
},
|
|
25
|
-
}),
|
|
26
|
-
})
|
|
7
|
+
export function toInteractiveRequest(request: Request): {
|
|
8
|
+
interactiveRequest: InteractiveRequest
|
|
9
|
+
requestController: RequestController
|
|
10
|
+
} {
|
|
11
|
+
const requestController = new RequestController(request)
|
|
12
|
+
|
|
13
|
+
Reflect.set(
|
|
14
|
+
request,
|
|
15
|
+
'respondWith',
|
|
16
|
+
requestController.respondWith.bind(requestController)
|
|
17
|
+
)
|
|
27
18
|
|
|
28
|
-
return
|
|
19
|
+
return {
|
|
20
|
+
interactiveRequest: request as InteractiveRequest,
|
|
21
|
+
requestController,
|
|
22
|
+
}
|
|
29
23
|
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@open-draft/logger';
|
|
2
|
-
import { EventMap, Emitter, Listener } from 'strict-event-emitter';
|
|
3
|
-
|
|
4
|
-
type AnyFunction = (...args: any[]) => any;
|
|
5
|
-
type LazyCallbackReturnType<FnType extends AnyFunction> = Parameters<FnType> | [];
|
|
6
|
-
interface LazyCallback<FnType extends AnyFunction> {
|
|
7
|
-
(...args: Parameters<FnType>): void;
|
|
8
|
-
invoked(): Promise<LazyCallbackReturnType<FnType>>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
type LazyResponseCallback = (response: Response) => void;
|
|
12
|
-
type InteractiveRequest = globalThis.Request & {
|
|
13
|
-
respondWith: LazyCallback<LazyResponseCallback>;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
declare const IS_PATCHED_MODULE: unique symbol;
|
|
17
|
-
type RequestCredentials = 'omit' | 'include' | 'same-origin';
|
|
18
|
-
type HttpRequestEventMap = {
|
|
19
|
-
request: [
|
|
20
|
-
args: {
|
|
21
|
-
request: InteractiveRequest;
|
|
22
|
-
requestId: string;
|
|
23
|
-
}
|
|
24
|
-
];
|
|
25
|
-
response: [
|
|
26
|
-
args: {
|
|
27
|
-
response: Response;
|
|
28
|
-
isMockedResponse: boolean;
|
|
29
|
-
request: Request;
|
|
30
|
-
requestId: string;
|
|
31
|
-
}
|
|
32
|
-
];
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
interface QueueItem<Args extends Array<unknown>> {
|
|
36
|
-
args: Args;
|
|
37
|
-
done: Promise<void>;
|
|
38
|
-
}
|
|
39
|
-
declare enum AsyncEventEmitterReadyState {
|
|
40
|
-
ACTIVE = "ACTIVE",
|
|
41
|
-
DEACTIVATED = "DEACTIVATED"
|
|
42
|
-
}
|
|
43
|
-
declare class AsyncEventEmitter<Events extends EventMap> extends Emitter<Events> {
|
|
44
|
-
readyState: AsyncEventEmitterReadyState;
|
|
45
|
-
private logger;
|
|
46
|
-
protected queue: Map<keyof Events, Array<QueueItem<Events[any]>>>;
|
|
47
|
-
constructor();
|
|
48
|
-
on<EventName extends keyof Events>(eventName: EventName, listener: Listener<any>): this;
|
|
49
|
-
emit<EventName extends keyof Events>(eventName: EventName, ...data: Events[EventName]): boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Returns a promise that resolves when all the listeners for the given event
|
|
52
|
-
* has been called. Awaits asynchronous listeners.
|
|
53
|
-
* If the event has no listeners, resolves immediately.
|
|
54
|
-
*/
|
|
55
|
-
untilIdle<EventName extends keyof Events>(eventName: EventName, filter?: (item: QueueItem<Events[EventName]>) => boolean): Promise<void>;
|
|
56
|
-
private openListenerQueue;
|
|
57
|
-
removeAllListeners<EventName extends keyof Events>(eventName?: EventName): this;
|
|
58
|
-
activate(): void;
|
|
59
|
-
/**
|
|
60
|
-
* Deactivate this event emitter.
|
|
61
|
-
* Deactivated emitter can no longer emit and listen to events
|
|
62
|
-
* and needs to be activated again in order to do so.
|
|
63
|
-
*/
|
|
64
|
-
deactivate(): void;
|
|
65
|
-
private isInternalEventName;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type InterceptorEventMap = Record<string, any>;
|
|
69
|
-
type InterceptorSubscription = () => void;
|
|
70
|
-
declare function getGlobalSymbol<V>(symbol: Symbol): V | undefined;
|
|
71
|
-
declare function deleteGlobalSymbol(symbol: Symbol): void;
|
|
72
|
-
declare enum InterceptorReadyState {
|
|
73
|
-
INACTIVE = "INACTIVE",
|
|
74
|
-
APPLYING = "APPLYING",
|
|
75
|
-
APPLIED = "APPLIED",
|
|
76
|
-
DISPOSING = "DISPOSING",
|
|
77
|
-
DISPOSED = "DISPOSED"
|
|
78
|
-
}
|
|
79
|
-
type ExtractEventNames<Events extends Record<string, any>> = Events extends Record<infer EventName, any> ? EventName : never;
|
|
80
|
-
declare class Interceptor<Events extends InterceptorEventMap> {
|
|
81
|
-
private readonly symbol;
|
|
82
|
-
protected emitter: AsyncEventEmitter<Events>;
|
|
83
|
-
protected subscriptions: Array<InterceptorSubscription>;
|
|
84
|
-
protected logger: Logger;
|
|
85
|
-
readyState: InterceptorReadyState;
|
|
86
|
-
constructor(symbol: symbol);
|
|
87
|
-
/**
|
|
88
|
-
* Determine if this interceptor can be applied
|
|
89
|
-
* in the current environment.
|
|
90
|
-
*/
|
|
91
|
-
protected checkEnvironment(): boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Apply this interceptor to the current process.
|
|
94
|
-
* Returns an already running interceptor instance if it's present.
|
|
95
|
-
*/
|
|
96
|
-
apply(): void;
|
|
97
|
-
/**
|
|
98
|
-
* Setup the module augments and stubs necessary for this interceptor.
|
|
99
|
-
* This method is not run if there's a running interceptor instance
|
|
100
|
-
* to prevent instantiating an interceptor multiple times.
|
|
101
|
-
*/
|
|
102
|
-
protected setup(): void;
|
|
103
|
-
/**
|
|
104
|
-
* Listen to the interceptor's public events.
|
|
105
|
-
*/
|
|
106
|
-
on<EventName extends ExtractEventNames<Events>>(eventName: EventName, listener: Listener<Events[EventName]>): void;
|
|
107
|
-
/**
|
|
108
|
-
* Disposes of any side-effects this interceptor has introduced.
|
|
109
|
-
*/
|
|
110
|
-
dispose(): void;
|
|
111
|
-
private getInstance;
|
|
112
|
-
private setInstance;
|
|
113
|
-
private clearInstance;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export { AsyncEventEmitter as A, ExtractEventNames as E, HttpRequestEventMap as H, Interceptor as I, RequestCredentials as R, IS_PATCHED_MODULE as a, InterceptorEventMap as b, InterceptorSubscription as c, deleteGlobalSymbol as d, InterceptorReadyState as e, InteractiveRequest as f, getGlobalSymbol as g };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/utils/uuid.ts
|
|
2
|
-
function uuidv4() {
|
|
3
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
4
|
-
const r = Math.random() * 16 | 0;
|
|
5
|
-
const v = c == "x" ? r : r & 3 | 8;
|
|
6
|
-
return v.toString(16);
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// src/utils/toInteractiveRequest.ts
|
|
11
|
-
var _outvariant = require('outvariant');
|
|
12
|
-
|
|
13
|
-
// src/utils/createLazyCallback.ts
|
|
14
|
-
function createLazyCallback(options = {}) {
|
|
15
|
-
let calledTimes = 0;
|
|
16
|
-
let autoResolveTimeout;
|
|
17
|
-
let remoteResolve;
|
|
18
|
-
const callPromise = new Promise((resolve) => {
|
|
19
|
-
remoteResolve = resolve;
|
|
20
|
-
}).finally(() => {
|
|
21
|
-
clearTimeout(autoResolveTimeout);
|
|
22
|
-
});
|
|
23
|
-
const fn = function(...args) {
|
|
24
|
-
var _a;
|
|
25
|
-
if (options.maxCalls && calledTimes >= options.maxCalls) {
|
|
26
|
-
(_a = options.maxCallsCallback) == null ? void 0 : _a.call(options);
|
|
27
|
-
}
|
|
28
|
-
remoteResolve(args);
|
|
29
|
-
calledTimes++;
|
|
30
|
-
};
|
|
31
|
-
fn.invoked = async () => {
|
|
32
|
-
autoResolveTimeout = setTimeout(() => {
|
|
33
|
-
remoteResolve([]);
|
|
34
|
-
}, 0);
|
|
35
|
-
return callPromise;
|
|
36
|
-
};
|
|
37
|
-
return fn;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// src/utils/toInteractiveRequest.ts
|
|
41
|
-
function toInteractiveRequest(request) {
|
|
42
|
-
Object.defineProperty(request, "respondWith", {
|
|
43
|
-
writable: false,
|
|
44
|
-
enumerable: true,
|
|
45
|
-
value: createLazyCallback({
|
|
46
|
-
maxCalls: 1,
|
|
47
|
-
maxCallsCallback() {
|
|
48
|
-
throw new Error(
|
|
49
|
-
_outvariant.format.call(void 0,
|
|
50
|
-
'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
|
|
51
|
-
request.method,
|
|
52
|
-
request.url
|
|
53
|
-
)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
});
|
|
58
|
-
return request;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
exports.uuidv4 = uuidv4; exports.toInteractiveRequest = toInteractiveRequest;
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// src/utils/uuid.ts
|
|
2
|
-
function uuidv4() {
|
|
3
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
4
|
-
const r = Math.random() * 16 | 0;
|
|
5
|
-
const v = c == "x" ? r : r & 3 | 8;
|
|
6
|
-
return v.toString(16);
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// src/utils/toInteractiveRequest.ts
|
|
11
|
-
import { format } from "outvariant";
|
|
12
|
-
|
|
13
|
-
// src/utils/createLazyCallback.ts
|
|
14
|
-
function createLazyCallback(options = {}) {
|
|
15
|
-
let calledTimes = 0;
|
|
16
|
-
let autoResolveTimeout;
|
|
17
|
-
let remoteResolve;
|
|
18
|
-
const callPromise = new Promise((resolve) => {
|
|
19
|
-
remoteResolve = resolve;
|
|
20
|
-
}).finally(() => {
|
|
21
|
-
clearTimeout(autoResolveTimeout);
|
|
22
|
-
});
|
|
23
|
-
const fn = function(...args) {
|
|
24
|
-
var _a;
|
|
25
|
-
if (options.maxCalls && calledTimes >= options.maxCalls) {
|
|
26
|
-
(_a = options.maxCallsCallback) == null ? void 0 : _a.call(options);
|
|
27
|
-
}
|
|
28
|
-
remoteResolve(args);
|
|
29
|
-
calledTimes++;
|
|
30
|
-
};
|
|
31
|
-
fn.invoked = async () => {
|
|
32
|
-
autoResolveTimeout = setTimeout(() => {
|
|
33
|
-
remoteResolve([]);
|
|
34
|
-
}, 0);
|
|
35
|
-
return callPromise;
|
|
36
|
-
};
|
|
37
|
-
return fn;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// src/utils/toInteractiveRequest.ts
|
|
41
|
-
function toInteractiveRequest(request) {
|
|
42
|
-
Object.defineProperty(request, "respondWith", {
|
|
43
|
-
writable: false,
|
|
44
|
-
enumerable: true,
|
|
45
|
-
value: createLazyCallback({
|
|
46
|
-
maxCalls: 1,
|
|
47
|
-
maxCallsCallback() {
|
|
48
|
-
throw new Error(
|
|
49
|
-
format(
|
|
50
|
-
'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
|
|
51
|
-
request.method,
|
|
52
|
-
request.url
|
|
53
|
-
)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
});
|
|
58
|
-
return request;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export {
|
|
62
|
-
uuidv4,
|
|
63
|
-
toInteractiveRequest
|
|
64
|
-
};
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@open-draft/logger';
|
|
2
|
-
import { EventMap, Emitter, Listener } from 'strict-event-emitter';
|
|
3
|
-
|
|
4
|
-
type AnyFunction = (...args: any[]) => any;
|
|
5
|
-
type LazyCallbackReturnType<FnType extends AnyFunction> = Parameters<FnType> | [];
|
|
6
|
-
interface LazyCallback<FnType extends AnyFunction> {
|
|
7
|
-
(...args: Parameters<FnType>): void;
|
|
8
|
-
invoked(): Promise<LazyCallbackReturnType<FnType>>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
type LazyResponseCallback = (response: Response) => void;
|
|
12
|
-
type InteractiveRequest = globalThis.Request & {
|
|
13
|
-
respondWith: LazyCallback<LazyResponseCallback>;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
declare const IS_PATCHED_MODULE: unique symbol;
|
|
17
|
-
type RequestCredentials = 'omit' | 'include' | 'same-origin';
|
|
18
|
-
type HttpRequestEventMap = {
|
|
19
|
-
request: [
|
|
20
|
-
args: {
|
|
21
|
-
request: InteractiveRequest;
|
|
22
|
-
requestId: string;
|
|
23
|
-
}
|
|
24
|
-
];
|
|
25
|
-
response: [
|
|
26
|
-
args: {
|
|
27
|
-
response: Response;
|
|
28
|
-
isMockedResponse: boolean;
|
|
29
|
-
request: Request;
|
|
30
|
-
requestId: string;
|
|
31
|
-
}
|
|
32
|
-
];
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
interface QueueItem<Args extends Array<unknown>> {
|
|
36
|
-
args: Args;
|
|
37
|
-
done: Promise<void>;
|
|
38
|
-
}
|
|
39
|
-
declare enum AsyncEventEmitterReadyState {
|
|
40
|
-
ACTIVE = "ACTIVE",
|
|
41
|
-
DEACTIVATED = "DEACTIVATED"
|
|
42
|
-
}
|
|
43
|
-
declare class AsyncEventEmitter<Events extends EventMap> extends Emitter<Events> {
|
|
44
|
-
readyState: AsyncEventEmitterReadyState;
|
|
45
|
-
private logger;
|
|
46
|
-
protected queue: Map<keyof Events, Array<QueueItem<Events[any]>>>;
|
|
47
|
-
constructor();
|
|
48
|
-
on<EventName extends keyof Events>(eventName: EventName, listener: Listener<any>): this;
|
|
49
|
-
emit<EventName extends keyof Events>(eventName: EventName, ...data: Events[EventName]): boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Returns a promise that resolves when all the listeners for the given event
|
|
52
|
-
* has been called. Awaits asynchronous listeners.
|
|
53
|
-
* If the event has no listeners, resolves immediately.
|
|
54
|
-
*/
|
|
55
|
-
untilIdle<EventName extends keyof Events>(eventName: EventName, filter?: (item: QueueItem<Events[EventName]>) => boolean): Promise<void>;
|
|
56
|
-
private openListenerQueue;
|
|
57
|
-
removeAllListeners<EventName extends keyof Events>(eventName?: EventName): this;
|
|
58
|
-
activate(): void;
|
|
59
|
-
/**
|
|
60
|
-
* Deactivate this event emitter.
|
|
61
|
-
* Deactivated emitter can no longer emit and listen to events
|
|
62
|
-
* and needs to be activated again in order to do so.
|
|
63
|
-
*/
|
|
64
|
-
deactivate(): void;
|
|
65
|
-
private isInternalEventName;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type InterceptorEventMap = Record<string, any>;
|
|
69
|
-
type InterceptorSubscription = () => void;
|
|
70
|
-
declare function getGlobalSymbol<V>(symbol: Symbol): V | undefined;
|
|
71
|
-
declare function deleteGlobalSymbol(symbol: Symbol): void;
|
|
72
|
-
declare enum InterceptorReadyState {
|
|
73
|
-
INACTIVE = "INACTIVE",
|
|
74
|
-
APPLYING = "APPLYING",
|
|
75
|
-
APPLIED = "APPLIED",
|
|
76
|
-
DISPOSING = "DISPOSING",
|
|
77
|
-
DISPOSED = "DISPOSED"
|
|
78
|
-
}
|
|
79
|
-
type ExtractEventNames<Events extends Record<string, any>> = Events extends Record<infer EventName, any> ? EventName : never;
|
|
80
|
-
declare class Interceptor<Events extends InterceptorEventMap> {
|
|
81
|
-
private readonly symbol;
|
|
82
|
-
protected emitter: AsyncEventEmitter<Events>;
|
|
83
|
-
protected subscriptions: Array<InterceptorSubscription>;
|
|
84
|
-
protected logger: Logger;
|
|
85
|
-
readyState: InterceptorReadyState;
|
|
86
|
-
constructor(symbol: symbol);
|
|
87
|
-
/**
|
|
88
|
-
* Determine if this interceptor can be applied
|
|
89
|
-
* in the current environment.
|
|
90
|
-
*/
|
|
91
|
-
protected checkEnvironment(): boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Apply this interceptor to the current process.
|
|
94
|
-
* Returns an already running interceptor instance if it's present.
|
|
95
|
-
*/
|
|
96
|
-
apply(): void;
|
|
97
|
-
/**
|
|
98
|
-
* Setup the module augments and stubs necessary for this interceptor.
|
|
99
|
-
* This method is not run if there's a running interceptor instance
|
|
100
|
-
* to prevent instantiating an interceptor multiple times.
|
|
101
|
-
*/
|
|
102
|
-
protected setup(): void;
|
|
103
|
-
/**
|
|
104
|
-
* Listen to the interceptor's public events.
|
|
105
|
-
*/
|
|
106
|
-
on<EventName extends ExtractEventNames<Events>>(eventName: EventName, listener: Listener<Events[EventName]>): void;
|
|
107
|
-
/**
|
|
108
|
-
* Disposes of any side-effects this interceptor has introduced.
|
|
109
|
-
*/
|
|
110
|
-
dispose(): void;
|
|
111
|
-
private getInstance;
|
|
112
|
-
private setInstance;
|
|
113
|
-
private clearInstance;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export { AsyncEventEmitter as A, ExtractEventNames as E, HttpRequestEventMap as H, IS_PATCHED_MODULE as I, RequestCredentials as R, InterceptorEventMap as a, InterceptorSubscription as b, InterceptorReadyState as c, deleteGlobalSymbol as d, Interceptor as e, InteractiveRequest as f, getGlobalSymbol as g };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// src/utils/toInteractiveRequest.ts
|
|
2
|
-
import { format } from "outvariant";
|
|
3
|
-
|
|
4
|
-
// src/utils/createLazyCallback.ts
|
|
5
|
-
function createLazyCallback(options = {}) {
|
|
6
|
-
let calledTimes = 0;
|
|
7
|
-
let autoResolveTimeout;
|
|
8
|
-
let remoteResolve;
|
|
9
|
-
const callPromise = new Promise((resolve) => {
|
|
10
|
-
remoteResolve = resolve;
|
|
11
|
-
}).finally(() => {
|
|
12
|
-
clearTimeout(autoResolveTimeout);
|
|
13
|
-
});
|
|
14
|
-
const fn = function(...args) {
|
|
15
|
-
var _a;
|
|
16
|
-
if (options.maxCalls && calledTimes >= options.maxCalls) {
|
|
17
|
-
(_a = options.maxCallsCallback) == null ? void 0 : _a.call(options);
|
|
18
|
-
}
|
|
19
|
-
remoteResolve(args);
|
|
20
|
-
calledTimes++;
|
|
21
|
-
};
|
|
22
|
-
fn.invoked = async () => {
|
|
23
|
-
autoResolveTimeout = setTimeout(() => {
|
|
24
|
-
remoteResolve([]);
|
|
25
|
-
}, 0);
|
|
26
|
-
return callPromise;
|
|
27
|
-
};
|
|
28
|
-
return fn;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// src/utils/toInteractiveRequest.ts
|
|
32
|
-
function toInteractiveRequest(request) {
|
|
33
|
-
Object.defineProperty(request, "respondWith", {
|
|
34
|
-
writable: false,
|
|
35
|
-
enumerable: true,
|
|
36
|
-
value: createLazyCallback({
|
|
37
|
-
maxCalls: 1,
|
|
38
|
-
maxCallsCallback() {
|
|
39
|
-
throw new Error(
|
|
40
|
-
format(
|
|
41
|
-
'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
|
|
42
|
-
request.method,
|
|
43
|
-
request.url
|
|
44
|
-
)
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
});
|
|
49
|
-
return request;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/utils/uuid.ts
|
|
53
|
-
function uuidv4() {
|
|
54
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
55
|
-
const r = Math.random() * 16 | 0;
|
|
56
|
-
const v = c == "x" ? r : r & 3 | 8;
|
|
57
|
-
return v.toString(16);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export {
|
|
62
|
-
toInteractiveRequest,
|
|
63
|
-
uuidv4
|
|
64
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/utils/toInteractiveRequest.ts
|
|
2
|
-
var _outvariant = require('outvariant');
|
|
3
|
-
|
|
4
|
-
// src/utils/createLazyCallback.ts
|
|
5
|
-
function createLazyCallback(options = {}) {
|
|
6
|
-
let calledTimes = 0;
|
|
7
|
-
let autoResolveTimeout;
|
|
8
|
-
let remoteResolve;
|
|
9
|
-
const callPromise = new Promise((resolve) => {
|
|
10
|
-
remoteResolve = resolve;
|
|
11
|
-
}).finally(() => {
|
|
12
|
-
clearTimeout(autoResolveTimeout);
|
|
13
|
-
});
|
|
14
|
-
const fn = function(...args) {
|
|
15
|
-
var _a;
|
|
16
|
-
if (options.maxCalls && calledTimes >= options.maxCalls) {
|
|
17
|
-
(_a = options.maxCallsCallback) == null ? void 0 : _a.call(options);
|
|
18
|
-
}
|
|
19
|
-
remoteResolve(args);
|
|
20
|
-
calledTimes++;
|
|
21
|
-
};
|
|
22
|
-
fn.invoked = async () => {
|
|
23
|
-
autoResolveTimeout = setTimeout(() => {
|
|
24
|
-
remoteResolve([]);
|
|
25
|
-
}, 0);
|
|
26
|
-
return callPromise;
|
|
27
|
-
};
|
|
28
|
-
return fn;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// src/utils/toInteractiveRequest.ts
|
|
32
|
-
function toInteractiveRequest(request) {
|
|
33
|
-
Object.defineProperty(request, "respondWith", {
|
|
34
|
-
writable: false,
|
|
35
|
-
enumerable: true,
|
|
36
|
-
value: createLazyCallback({
|
|
37
|
-
maxCalls: 1,
|
|
38
|
-
maxCallsCallback() {
|
|
39
|
-
throw new Error(
|
|
40
|
-
_outvariant.format.call(void 0,
|
|
41
|
-
'Failed to respond to "%s %s" request: the "request" event has already been responded to.',
|
|
42
|
-
request.method,
|
|
43
|
-
request.url
|
|
44
|
-
)
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
});
|
|
49
|
-
return request;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/utils/uuid.ts
|
|
53
|
-
function uuidv4() {
|
|
54
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
55
|
-
const r = Math.random() * 16 | 0;
|
|
56
|
-
const v = c == "x" ? r : r & 3 | 8;
|
|
57
|
-
return v.toString(16);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
exports.toInteractiveRequest = toInteractiveRequest; exports.uuidv4 = uuidv4;
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { vi, it, expect, afterEach } from 'vitest'
|
|
2
|
-
import { AsyncEventEmitter } from './AsyncEventEmitter'
|
|
3
|
-
import { sleep } from '../../test/helpers'
|
|
4
|
-
|
|
5
|
-
afterEach(() => {
|
|
6
|
-
vi.useRealTimers()
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
it('emits and listens to events', () => {
|
|
10
|
-
const emitter = new AsyncEventEmitter<{ hello: [string] }>()
|
|
11
|
-
const listener = vi.fn()
|
|
12
|
-
emitter.on('hello', listener)
|
|
13
|
-
emitter.emit('hello', 'John')
|
|
14
|
-
|
|
15
|
-
expect(listener).toHaveBeenCalledTimes(1)
|
|
16
|
-
expect(listener).toHaveBeenCalledWith('John')
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('resolves "untilIdle" when all the event listeners are done', async () => {
|
|
20
|
-
const emitter = new AsyncEventEmitter<{ speak: [string] }>()
|
|
21
|
-
|
|
22
|
-
const results: string[] = []
|
|
23
|
-
const firstListener = vi.fn(() => results.push('first'))
|
|
24
|
-
emitter.on('speak', firstListener)
|
|
25
|
-
|
|
26
|
-
const secondListener = vi.fn(async () => {
|
|
27
|
-
await sleep(150)
|
|
28
|
-
results.push('second')
|
|
29
|
-
})
|
|
30
|
-
emitter.on('speak', secondListener)
|
|
31
|
-
|
|
32
|
-
emitter.emit('speak', 'hi')
|
|
33
|
-
await emitter.untilIdle('speak')
|
|
34
|
-
|
|
35
|
-
// All listeners must be called.
|
|
36
|
-
expect(firstListener).toHaveBeenCalledTimes(1)
|
|
37
|
-
expect(secondListener).toHaveBeenCalledTimes(1)
|
|
38
|
-
|
|
39
|
-
// All promise listeners must be awaited.
|
|
40
|
-
expect(results).toEqual(['first', 'second'])
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('resolves "untilIdle" only for the relevant listeners', async () => {
|
|
44
|
-
const emitter = new AsyncEventEmitter<{ signal: [number] }>()
|
|
45
|
-
|
|
46
|
-
const results: number[] = []
|
|
47
|
-
const listener = vi.fn(async (code: number) => {
|
|
48
|
-
if (code !== 1) {
|
|
49
|
-
// Delay listener based on the signal code.
|
|
50
|
-
await sleep(150)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
results.push(code)
|
|
54
|
-
})
|
|
55
|
-
emitter.on('signal', listener)
|
|
56
|
-
|
|
57
|
-
emitter.emit('signal', 1)
|
|
58
|
-
emitter.emit('signal', 2)
|
|
59
|
-
|
|
60
|
-
const resultsAfterIdle = await emitter
|
|
61
|
-
.untilIdle('signal', ({ args: [code] }) => {
|
|
62
|
-
return code === 1
|
|
63
|
-
})
|
|
64
|
-
.then(() => results)
|
|
65
|
-
|
|
66
|
-
await emitter.untilIdle('signal')
|
|
67
|
-
|
|
68
|
-
expect(listener).toHaveBeenCalled()
|
|
69
|
-
expect(resultsAfterIdle).toEqual([1])
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('resolves "untilIdle" immediately if there are no pending listeners', async () => {
|
|
73
|
-
const emitter = new AsyncEventEmitter<{ ping: never }>()
|
|
74
|
-
emitter.emit('ping')
|
|
75
|
-
|
|
76
|
-
await expect(emitter.untilIdle('ping')).resolves.toBeUndefined()
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
it('propagates listener exceptions to "untilIdle" promise', async () => {
|
|
80
|
-
const emitter = new AsyncEventEmitter<{ ping: never }>()
|
|
81
|
-
|
|
82
|
-
const error = new Error('oops')
|
|
83
|
-
const listener = vi.fn(() => {
|
|
84
|
-
throw error
|
|
85
|
-
})
|
|
86
|
-
emitter.on('ping', listener)
|
|
87
|
-
|
|
88
|
-
emitter.emit('ping')
|
|
89
|
-
await expect(emitter.untilIdle('ping')).rejects.toBe(error)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('does not emit events once the emitter was deactivated', () => {
|
|
93
|
-
const emitter = new AsyncEventEmitter<{ ping: never }>()
|
|
94
|
-
|
|
95
|
-
const listener = vi.fn()
|
|
96
|
-
emitter.on('ping', listener)
|
|
97
|
-
emitter.deactivate()
|
|
98
|
-
|
|
99
|
-
emitter.emit('ping')
|
|
100
|
-
|
|
101
|
-
expect(listener).not.toHaveBeenCalled()
|
|
102
|
-
})
|