@mswjs/interceptors 0.19.4 → 0.19.5
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/BatchInterceptor.d.ts +5 -5
- package/lib/BatchInterceptor.js +14 -59
- package/lib/BatchInterceptor.js.map +1 -1
- package/lib/Interceptor.d.ts +7 -6
- package/lib/Interceptor.js +39 -64
- package/lib/Interceptor.js.map +1 -1
- package/lib/RemoteHttpInterceptor.js +115 -209
- package/lib/RemoteHttpInterceptor.js.map +1 -1
- package/lib/glossary.d.ts +2 -2
- package/lib/interceptors/ClientRequest/NodeClientRequest.js +141 -278
- package/lib/interceptors/ClientRequest/NodeClientRequest.js.map +1 -1
- package/lib/interceptors/ClientRequest/http.get.js +5 -34
- package/lib/interceptors/ClientRequest/http.get.js.map +1 -1
- package/lib/interceptors/ClientRequest/http.request.js +6 -35
- package/lib/interceptors/ClientRequest/http.request.js.map +1 -1
- package/lib/interceptors/ClientRequest/index.js +22 -82
- package/lib/interceptors/ClientRequest/index.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.js +18 -62
- package/lib/interceptors/ClientRequest/utils/cloneIncomingMessage.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/createRequest.js +10 -32
- package/lib/interceptors/ClientRequest/utils/createRequest.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/createResponse.js +6 -6
- package/lib/interceptors/ClientRequest/utils/createResponse.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.js +12 -12
- package/lib/interceptors/ClientRequest/utils/getIncomingMessageBody.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js +21 -52
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.js +5 -9
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.js +6 -6
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestOverride.js +213 -338
- package/lib/interceptors/XMLHttpRequest/XMLHttpRequestOverride.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/index.js +16 -33
- package/lib/interceptors/XMLHttpRequest/index.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/polyfills/EventPolyfill.js +11 -12
- package/lib/interceptors/XMLHttpRequest/polyfills/EventPolyfill.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.js +9 -27
- package/lib/interceptors/XMLHttpRequest/polyfills/ProgressEventPolyfill.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.js +1 -1
- package/lib/interceptors/XMLHttpRequest/utils/concatArrayBuffer.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/utils/createEvent.js +7 -7
- package/lib/interceptors/XMLHttpRequest/utils/createEvent.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/utils/createResponse.js +2 -2
- package/lib/interceptors/XMLHttpRequest/utils/createResponse.js.map +1 -1
- package/lib/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.js +2 -2
- package/lib/interceptors/XMLHttpRequest/utils/isDomParserSupportedType.js.map +1 -1
- package/lib/interceptors/fetch/index.js +52 -125
- package/lib/interceptors/fetch/index.js.map +1 -1
- package/lib/presets/browser.js +2 -2
- package/lib/presets/browser.js.map +1 -1
- package/lib/presets/node.js +2 -2
- package/lib/presets/node.js.map +1 -1
- package/lib/utils/AsyncEventEmitter.d.ts +9 -8
- package/lib/utils/AsyncEventEmitter.js +84 -190
- package/lib/utils/AsyncEventEmitter.js.map +1 -1
- package/lib/utils/bufferUtils.js +3 -3
- package/lib/utils/bufferUtils.js.map +1 -1
- package/lib/utils/cloneObject.js +2 -19
- package/lib/utils/cloneObject.js.map +1 -1
- package/lib/utils/createLazyCallback.js +14 -49
- package/lib/utils/createLazyCallback.js.map +1 -1
- package/lib/utils/getCleanUrl.js +1 -2
- package/lib/utils/getCleanUrl.js.map +1 -1
- package/lib/utils/getRequestOptionsByUrl.js +3 -3
- package/lib/utils/getRequestOptionsByUrl.js.map +1 -1
- package/lib/utils/getUrlByRequestOptions.js +23 -39
- package/lib/utils/getUrlByRequestOptions.js.map +1 -1
- package/lib/utils/nextTick.js +2 -2
- package/lib/utils/nextTick.js.map +1 -1
- package/lib/utils/parseJson.js +1 -1
- package/lib/utils/parseJson.js.map +1 -1
- package/lib/utils/toInteractiveRequest.js +3 -3
- package/lib/utils/toInteractiveRequest.js.map +1 -1
- package/lib/utils/uuid.js +2 -2
- package/lib/utils/uuid.js.map +1 -1
- package/package.json +2 -2
- package/src/BatchInterceptor.test.ts +2 -2
- package/src/BatchInterceptor.ts +14 -13
- package/src/Interceptor.ts +12 -11
- package/src/glossary.ts +2 -6
- package/src/utils/AsyncEventEmitter.test.ts +6 -6
- package/src/utils/AsyncEventEmitter.ts +60 -50
|
@@ -1,29 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.getUrlByRequestOptions = exports.DEFAULT_PATH = void 0;
|
|
20
|
-
|
|
21
|
-
|
|
4
|
+
const http_1 = require("http");
|
|
5
|
+
const debug = require('debug')('utils getUrlByRequestOptions');
|
|
22
6
|
exports.DEFAULT_PATH = '/';
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
const DEFAULT_PROTOCOL = 'http:';
|
|
8
|
+
const DEFAULT_HOST = 'localhost';
|
|
9
|
+
const DEFAULT_PORT = 80;
|
|
10
|
+
const SSL_PORT = 443;
|
|
27
11
|
function getAgent(options) {
|
|
28
12
|
return options.agent instanceof http_1.Agent ? options.agent : undefined;
|
|
29
13
|
}
|
|
@@ -32,23 +16,23 @@ function getProtocolByRequestOptions(options) {
|
|
|
32
16
|
if (options.protocol) {
|
|
33
17
|
return options.protocol;
|
|
34
18
|
}
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
const agent = getAgent(options);
|
|
20
|
+
const agentProtocol = (_a = agent) === null || _a === void 0 ? void 0 : _a.protocol;
|
|
37
21
|
if (agentProtocol) {
|
|
38
22
|
return agentProtocol;
|
|
39
23
|
}
|
|
40
|
-
|
|
41
|
-
|
|
24
|
+
const port = getPortByRequestOptions(options);
|
|
25
|
+
const isSecureRequest = options.cert || port === SSL_PORT;
|
|
42
26
|
return isSecureRequest ? 'https:' : ((_b = options.uri) === null || _b === void 0 ? void 0 : _b.protocol) || DEFAULT_PROTOCOL;
|
|
43
27
|
}
|
|
44
28
|
function getPortByRequestOptions(options) {
|
|
45
29
|
var _a, _b;
|
|
46
|
-
|
|
47
|
-
|
|
30
|
+
const agent = getAgent(options);
|
|
31
|
+
const agentPort = ((_a = agent) === null || _a === void 0 ? void 0 : _a.options.port) ||
|
|
48
32
|
((_b = agent) === null || _b === void 0 ? void 0 : _b.defaultPort);
|
|
49
|
-
|
|
33
|
+
const optionsPort = options.port;
|
|
50
34
|
if (optionsPort || agentPort) {
|
|
51
|
-
|
|
35
|
+
const explicitPort = optionsPort || agentPort || DEFAULT_PORT;
|
|
52
36
|
return Number(explicitPort);
|
|
53
37
|
}
|
|
54
38
|
}
|
|
@@ -57,8 +41,8 @@ function getHostByRequestOptions(options) {
|
|
|
57
41
|
}
|
|
58
42
|
function getAuthByRequestOptions(options) {
|
|
59
43
|
if (options.auth) {
|
|
60
|
-
|
|
61
|
-
return { username
|
|
44
|
+
const [username, password] = options.auth.split(':');
|
|
45
|
+
return { username, password };
|
|
62
46
|
}
|
|
63
47
|
}
|
|
64
48
|
/**
|
|
@@ -66,18 +50,18 @@ function getAuthByRequestOptions(options) {
|
|
|
66
50
|
*/
|
|
67
51
|
function getUrlByRequestOptions(options) {
|
|
68
52
|
debug('request options', options);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
53
|
+
const protocol = getProtocolByRequestOptions(options);
|
|
54
|
+
const host = getHostByRequestOptions(options);
|
|
55
|
+
const port = getPortByRequestOptions(options);
|
|
56
|
+
const path = options.path || exports.DEFAULT_PATH;
|
|
57
|
+
const auth = getAuthByRequestOptions(options);
|
|
74
58
|
debug('protocol', protocol);
|
|
75
59
|
debug('host', host);
|
|
76
60
|
debug('port', port);
|
|
77
61
|
debug('path', path);
|
|
78
|
-
|
|
62
|
+
const baseUrl = `${protocol}//${host}`;
|
|
79
63
|
debug('base URL:', baseUrl);
|
|
80
|
-
|
|
64
|
+
const url = options.uri ? new URL(options.uri.href) : new URL(path, baseUrl);
|
|
81
65
|
if (port) {
|
|
82
66
|
debug('detected explicit port', port);
|
|
83
67
|
url.port = port.toString();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getUrlByRequestOptions.js","sourceRoot":"","sources":["../../src/utils/getUrlByRequestOptions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getUrlByRequestOptions.js","sourceRoot":"","sources":["../../src/utils/getUrlByRequestOptions.ts"],"names":[],"mappings":";;;AAAA,+BAA4B;AAG5B,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,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CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAE5E,IAAI,IAAI,EAAE;QACR,KAAK,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAA;QACrC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAA;KAC3B;IAED,IAAI,IAAI,EAAE;QACR,KAAK,CAAC,eAAe,EAAE,IAAI,CAAC,CAAA;QAE5B,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;QAC5B,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;KAC7B;IAED,KAAK,CAAC,cAAc,EAAE,GAAG,CAAC,CAAA;IAE1B,OAAO,GAAG,CAAA;AACZ,CAAC;AAlCD,wDAkCC"}
|
package/lib/utils/nextTick.js
CHANGED
|
@@ -6,8 +6,8 @@ function nextTick(callback) {
|
|
|
6
6
|
}
|
|
7
7
|
exports.nextTick = nextTick;
|
|
8
8
|
function nextTickAsync(callback) {
|
|
9
|
-
return new Promise(
|
|
10
|
-
setTimeout(
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
setTimeout(() => {
|
|
11
11
|
resolve(callback());
|
|
12
12
|
}, 0);
|
|
13
13
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nextTick.js","sourceRoot":"","sources":["../../src/utils/nextTick.ts"],"names":[],"mappings":";;;AAAA,SAAgB,QAAQ,CAAC,QAAoB;IAC3C,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;AACzB,CAAC;AAFD,4BAEC;AAED,SAAgB,aAAa,CAAC,QAAoB;IAChD,OAAO,IAAI,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"nextTick.js","sourceRoot":"","sources":["../../src/utils/nextTick.ts"],"names":[],"mappings":";;;AAAA,SAAgB,QAAQ,CAAC,QAAoB;IAC3C,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;AACzB,CAAC;AAFD,4BAEC;AAED,SAAgB,aAAa,CAAC,QAAoB;IAChD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,UAAU,CAAC,GAAG,EAAE;YACd,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAA;QACrB,CAAC,EAAE,CAAC,CAAC,CAAA;IACP,CAAC,CAAC,CAAA;AACJ,CAAC;AAND,sCAMC"}
|
package/lib/utils/parseJson.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseJson.js","sourceRoot":"","sources":["../../src/utils/parseJson.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,SAAgB,SAAS,CAAC,IAAY;IACpC,IAAI;QACF,
|
|
1
|
+
{"version":3,"file":"parseJson.js","sourceRoot":"","sources":["../../src/utils/parseJson.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,SAAgB,SAAS,CAAC,IAAY;IACpC,IAAI;QACF,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QAC7B,OAAO,IAAI,CAAA;KACZ;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,IAAI,CAAA;KACZ;AACH,CAAC;AAPD,8BAOC"}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toInteractiveRequest = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const outvariant_1 = require("outvariant");
|
|
5
|
+
const createLazyCallback_1 = require("./createLazyCallback");
|
|
6
6
|
function toInteractiveRequest(request) {
|
|
7
7
|
Object.defineProperty(request, 'respondWith', {
|
|
8
8
|
writable: false,
|
|
9
9
|
enumerable: true,
|
|
10
10
|
value: (0, createLazyCallback_1.createLazyCallback)({
|
|
11
11
|
maxCalls: 1,
|
|
12
|
-
maxCallsCallback
|
|
12
|
+
maxCallsCallback() {
|
|
13
13
|
throw new Error((0, outvariant_1.format)('Failed to respond to "%s %s" request: the "request" event has already been responded to.', request.method, request.url));
|
|
14
14
|
},
|
|
15
15
|
}),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toInteractiveRequest.js","sourceRoot":"","sources":["../../src/utils/toInteractiveRequest.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"toInteractiveRequest.js","sourceRoot":"","sources":["../../src/utils/toInteractiveRequest.ts"],"names":[],"mappings":";;;AAAA,2CAAmC;AACnC,6DAAuE;AAQvE,SAAgB,oBAAoB,CAAC,OAAgB;IACnD,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE;QAC5C,QAAQ,EAAE,KAAK;QACf,UAAU,EAAE,IAAI;QAChB,KAAK,EAAE,IAAA,uCAAkB,EAAuB;YAC9C,QAAQ,EAAE,CAAC;YACX,gBAAgB;gBACd,MAAM,IAAI,KAAK,CACb,IAAA,mBAAM,EACJ,0FAA0F,EAC1F,OAAO,CAAC,MAAM,EACd,OAAO,CAAC,GAAG,CACZ,CACF,CAAA;YACH,CAAC;SACF,CAAC;KACH,CAAC,CAAA;IAEF,OAAO,OAA6B,CAAA;AACtC,CAAC;AAnBD,oDAmBC"}
|
package/lib/utils/uuid.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.uuidv4 = void 0;
|
|
4
4
|
function uuidv4() {
|
|
5
5
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const r = (Math.random() * 16) | 0;
|
|
7
|
+
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
8
8
|
return v.toString(16);
|
|
9
9
|
});
|
|
10
10
|
}
|
package/lib/utils/uuid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../src/utils/uuid.ts"],"names":[],"mappings":";;;AAAA,SAAgB,MAAM;IACpB,OAAO,sCAAsC,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC;QACxE,
|
|
1
|
+
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../src/utils/uuid.ts"],"names":[],"mappings":";;;AAAA,SAAgB,MAAM;IACpB,OAAO,sCAAsC,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC;QACxE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;QAClC,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;QACxC,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;IACvB,CAAC,CAAC,CAAA;AACJ,CAAC;AAND,wBAMC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mswjs/interceptors",
|
|
3
3
|
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
|
|
4
|
-
"version": "0.19.
|
|
4
|
+
"version": "0.19.5",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"author": "Artem Zakharchenko",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"debug": "^4.3.3",
|
|
72
72
|
"headers-polyfill": "^3.1.0",
|
|
73
73
|
"outvariant": "^1.2.1",
|
|
74
|
-
"strict-event-emitter": "^0.
|
|
74
|
+
"strict-event-emitter": "^0.4.6",
|
|
75
75
|
"web-encoding": "^1.1.5"
|
|
76
76
|
},
|
|
77
77
|
"keywords": [
|
|
@@ -38,14 +38,14 @@ it('applies child interceptors', () => {
|
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
it('proxies event listeners to the interceptors', () => {
|
|
41
|
-
class PrimaryInterceptor extends Interceptor<{ hello
|
|
41
|
+
class PrimaryInterceptor extends Interceptor<{ hello: [string] }> {
|
|
42
42
|
constructor() {
|
|
43
43
|
super(Symbol('primary'))
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
class SecondaryInterceptor extends Interceptor<{
|
|
48
|
-
goodbye
|
|
48
|
+
goodbye: [string]
|
|
49
49
|
}> {
|
|
50
50
|
constructor() {
|
|
51
51
|
super(Symbol('secondary'))
|
package/src/BatchInterceptor.ts
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EventMap, Listener } from 'strict-event-emitter'
|
|
2
2
|
import { ExtractEventNames, Interceptor } from './Interceptor'
|
|
3
3
|
|
|
4
4
|
export interface BatchInterceptorOptions<
|
|
5
|
-
InterceptorList extends Interceptor<any
|
|
5
|
+
InterceptorList extends Array<Interceptor<any>>
|
|
6
6
|
> {
|
|
7
7
|
name: string
|
|
8
8
|
interceptors: InterceptorList
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export type ExtractEventMapType<
|
|
12
|
-
InterceptorList extends Array<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
export type ExtractEventMapType<
|
|
12
|
+
InterceptorList extends Array<Interceptor<any>>
|
|
13
|
+
> = InterceptorList extends Array<infer InterceptorType>
|
|
14
|
+
? InterceptorType extends Interceptor<infer EventMap>
|
|
15
|
+
? EventMap
|
|
16
16
|
: never
|
|
17
|
+
: never
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* A batch interceptor that exposes a single interface
|
|
20
21
|
* to apply and operate with multiple interceptors at once.
|
|
21
22
|
*/
|
|
22
23
|
export class BatchInterceptor<
|
|
23
|
-
InterceptorList extends Interceptor<any
|
|
24
|
-
|
|
25
|
-
> extends Interceptor<
|
|
24
|
+
InterceptorList extends Array<Interceptor<any>>,
|
|
25
|
+
Events extends EventMap = ExtractEventMapType<InterceptorList>
|
|
26
|
+
> extends Interceptor<Events> {
|
|
26
27
|
static symbol: Symbol
|
|
27
28
|
|
|
28
29
|
private interceptors: InterceptorList
|
|
@@ -47,9 +48,9 @@ export class BatchInterceptor<
|
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
public on<
|
|
51
|
-
event:
|
|
52
|
-
listener:
|
|
51
|
+
public on<EventName extends ExtractEventNames<Events>>(
|
|
52
|
+
event: EventName,
|
|
53
|
+
listener: Listener<Events[EventName]>
|
|
53
54
|
) {
|
|
54
55
|
// Instead of adding a listener to the batch interceptor,
|
|
55
56
|
// propagate the listener to each of the individual interceptors.
|
package/src/Interceptor.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Debugger, debug } from 'debug'
|
|
2
|
+
import { Listener } from 'strict-event-emitter'
|
|
2
3
|
import { AsyncEventEmitter } from './utils/AsyncEventEmitter'
|
|
3
4
|
import { nextTick } from './utils/nextTick'
|
|
4
5
|
|
|
5
|
-
export type InterceptorEventMap = Record<string,
|
|
6
|
+
export type InterceptorEventMap = Record<string, any>
|
|
6
7
|
export type InterceptorSubscription = () => void
|
|
7
8
|
|
|
8
9
|
export function getGlobalSymbol<V>(symbol: Symbol): V | undefined {
|
|
@@ -30,12 +31,12 @@ export enum InterceptorReadyState {
|
|
|
30
31
|
DISPOSED = 'DISPOSED',
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
export type ExtractEventNames<
|
|
34
|
-
|
|
34
|
+
export type ExtractEventNames<Events extends Record<string, any>> =
|
|
35
|
+
Events extends Record<infer EventName, any> ? EventName : never
|
|
35
36
|
|
|
36
|
-
export class Interceptor<
|
|
37
|
-
protected emitter: AsyncEventEmitter<
|
|
38
|
-
protected subscriptions: InterceptorSubscription
|
|
37
|
+
export class Interceptor<Events extends InterceptorEventMap> {
|
|
38
|
+
protected emitter: AsyncEventEmitter<Events>
|
|
39
|
+
protected subscriptions: Array<InterceptorSubscription>
|
|
39
40
|
protected log: Debugger
|
|
40
41
|
|
|
41
42
|
public readyState: InterceptorReadyState
|
|
@@ -144,9 +145,9 @@ export class Interceptor<EventMap extends InterceptorEventMap> {
|
|
|
144
145
|
/**
|
|
145
146
|
* Listen to the interceptor's public events.
|
|
146
147
|
*/
|
|
147
|
-
public on<
|
|
148
|
-
|
|
149
|
-
listener:
|
|
148
|
+
public on<EventName extends ExtractEventNames<Events>>(
|
|
149
|
+
eventName: EventName,
|
|
150
|
+
listener: Listener<Events[EventName]>
|
|
150
151
|
): void {
|
|
151
152
|
const log = this.log.extend('on')
|
|
152
153
|
|
|
@@ -158,9 +159,9 @@ export class Interceptor<EventMap extends InterceptorEventMap> {
|
|
|
158
159
|
return
|
|
159
160
|
}
|
|
160
161
|
|
|
161
|
-
log('adding "%s" event listener:',
|
|
162
|
+
log('adding "%s" event listener:', eventName, listener.name)
|
|
162
163
|
|
|
163
|
-
this.emitter.on(
|
|
164
|
+
this.emitter.on(eventName, listener)
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
/**
|
package/src/glossary.ts
CHANGED
|
@@ -5,10 +5,6 @@ export const IS_PATCHED_MODULE: unique symbol = Symbol('isPatchedModule')
|
|
|
5
5
|
export type RequestCredentials = 'omit' | 'include' | 'same-origin'
|
|
6
6
|
|
|
7
7
|
export type HttpRequestEventMap = {
|
|
8
|
-
request
|
|
9
|
-
response
|
|
10
|
-
response: Response,
|
|
11
|
-
request: Request,
|
|
12
|
-
requestId: string
|
|
13
|
-
): Promise<void> | void
|
|
8
|
+
request: [request: InteractiveRequest, requestId: string]
|
|
9
|
+
response: [response: Response, request: Request, requestId: string]
|
|
14
10
|
}
|
|
@@ -6,7 +6,7 @@ afterEach(() => {
|
|
|
6
6
|
})
|
|
7
7
|
|
|
8
8
|
it('emits and listens to events', () => {
|
|
9
|
-
const emitter = new AsyncEventEmitter<{ hello
|
|
9
|
+
const emitter = new AsyncEventEmitter<{ hello: [string] }>()
|
|
10
10
|
const listener = jest.fn()
|
|
11
11
|
emitter.on('hello', listener)
|
|
12
12
|
emitter.emit('hello', 'John')
|
|
@@ -16,7 +16,7 @@ it('emits and listens to events', () => {
|
|
|
16
16
|
})
|
|
17
17
|
|
|
18
18
|
it('resolves "untilIdle" when all the event listeners are done', async () => {
|
|
19
|
-
const emitter = new AsyncEventEmitter<{ speak
|
|
19
|
+
const emitter = new AsyncEventEmitter<{ speak: [string] }>()
|
|
20
20
|
|
|
21
21
|
const results: string[] = []
|
|
22
22
|
const firstListener = jest.fn(() => results.push('first'))
|
|
@@ -40,7 +40,7 @@ it('resolves "untilIdle" when all the event listeners are done', async () => {
|
|
|
40
40
|
})
|
|
41
41
|
|
|
42
42
|
it('resolves "untilIdle" only for the relevant listeners', async () => {
|
|
43
|
-
const emitter = new AsyncEventEmitter<{ signal
|
|
43
|
+
const emitter = new AsyncEventEmitter<{ signal: [number] }>()
|
|
44
44
|
|
|
45
45
|
const results: number[] = []
|
|
46
46
|
const listener = jest.fn(async (code: number) => {
|
|
@@ -69,14 +69,14 @@ it('resolves "untilIdle" only for the relevant listeners', async () => {
|
|
|
69
69
|
})
|
|
70
70
|
|
|
71
71
|
it('resolves "untilIdle" immediately if there are no pending listeners', async () => {
|
|
72
|
-
const emitter = new AsyncEventEmitter<{ ping
|
|
72
|
+
const emitter = new AsyncEventEmitter<{ ping: never }>()
|
|
73
73
|
emitter.emit('ping')
|
|
74
74
|
|
|
75
75
|
await expect(emitter.untilIdle('ping')).resolves.toBeUndefined()
|
|
76
76
|
})
|
|
77
77
|
|
|
78
78
|
it('propagates listener exceptions to "untilIdle" promise', async () => {
|
|
79
|
-
const emitter = new AsyncEventEmitter<{ ping
|
|
79
|
+
const emitter = new AsyncEventEmitter<{ ping: never }>()
|
|
80
80
|
|
|
81
81
|
const error = new Error('oops')
|
|
82
82
|
const listener = jest.fn(() => {
|
|
@@ -89,7 +89,7 @@ it('propagates listener exceptions to "untilIdle" promise', async () => {
|
|
|
89
89
|
})
|
|
90
90
|
|
|
91
91
|
it('does not emit events once the emitter was deactivated', () => {
|
|
92
|
-
const emitter = new AsyncEventEmitter<{ ping
|
|
92
|
+
const emitter = new AsyncEventEmitter<{ ping: never }>()
|
|
93
93
|
|
|
94
94
|
const listener = jest.fn()
|
|
95
95
|
emitter.on('ping', listener)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Debugger, debug } from 'debug'
|
|
2
|
-
import {
|
|
2
|
+
import { Emitter, EventMap, Listener } from 'strict-event-emitter'
|
|
3
3
|
import { nextTick } from './nextTick'
|
|
4
4
|
|
|
5
|
-
export interface QueueItem<Args extends
|
|
5
|
+
export interface QueueItem<Args extends Array<unknown>> {
|
|
6
6
|
args: Args
|
|
7
7
|
done: Promise<void>
|
|
8
8
|
}
|
|
@@ -13,15 +13,12 @@ export enum AsyncEventEmitterReadyState {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export class AsyncEventEmitter<
|
|
16
|
-
|
|
17
|
-
> extends
|
|
16
|
+
Events extends EventMap
|
|
17
|
+
> extends Emitter<Events> {
|
|
18
18
|
public readyState: AsyncEventEmitterReadyState
|
|
19
19
|
|
|
20
20
|
private log: Debugger
|
|
21
|
-
protected queue: Map<
|
|
22
|
-
keyof EventMap,
|
|
23
|
-
QueueItem<Parameters<EventMap[keyof EventMap]>>[]
|
|
24
|
-
>
|
|
21
|
+
protected queue: Map<keyof Events, Array<QueueItem<Events[any]>>>
|
|
25
22
|
|
|
26
23
|
constructor() {
|
|
27
24
|
super()
|
|
@@ -32,24 +29,24 @@ export class AsyncEventEmitter<
|
|
|
32
29
|
this.readyState = AsyncEventEmitterReadyState.ACTIVE
|
|
33
30
|
}
|
|
34
31
|
|
|
35
|
-
public on<
|
|
36
|
-
|
|
37
|
-
listener:
|
|
32
|
+
public on<EventName extends keyof Events>(
|
|
33
|
+
eventName: EventName,
|
|
34
|
+
listener: Listener<any>
|
|
38
35
|
) {
|
|
39
36
|
const log = this.log.extend('on')
|
|
40
37
|
|
|
41
|
-
log('adding "%s" listener...',
|
|
38
|
+
log('adding "%s" listener...', eventName)
|
|
42
39
|
|
|
43
40
|
if (this.readyState === AsyncEventEmitterReadyState.DEACTIVATED) {
|
|
44
41
|
log('the emitter is destroyed, skipping!')
|
|
45
42
|
return this
|
|
46
43
|
}
|
|
47
44
|
|
|
48
|
-
return super.on(
|
|
45
|
+
return super.on(eventName, async (...args) => {
|
|
49
46
|
// Event queue is always established when calling ".emit()".
|
|
50
|
-
const queue = this.openListenerQueue(
|
|
47
|
+
const queue = this.openListenerQueue(eventName)
|
|
51
48
|
|
|
52
|
-
log('awaiting the "%s" listener...',
|
|
49
|
+
log('awaiting the "%s" listener...', eventName)
|
|
53
50
|
|
|
54
51
|
// Whenever a listener is called, create a new Promise
|
|
55
52
|
// that resolves when that listener function completes its execution.
|
|
@@ -62,46 +59,52 @@ export class AsyncEventEmitter<
|
|
|
62
59
|
await listener(...args)
|
|
63
60
|
resolve()
|
|
64
61
|
|
|
65
|
-
log('"%s" listener has resolved!',
|
|
62
|
+
log('"%s" listener has resolved!', eventName)
|
|
66
63
|
} catch (error) {
|
|
67
64
|
log('"%s" listener has rejected!', error)
|
|
68
65
|
reject(error)
|
|
69
66
|
}
|
|
70
67
|
}),
|
|
71
68
|
})
|
|
72
|
-
})
|
|
69
|
+
})
|
|
73
70
|
}
|
|
74
71
|
|
|
75
|
-
public emit<
|
|
76
|
-
|
|
77
|
-
...
|
|
72
|
+
public emit<EventName extends keyof Events>(
|
|
73
|
+
eventName: EventName,
|
|
74
|
+
...data: Events[EventName]
|
|
78
75
|
): boolean {
|
|
79
76
|
const log = this.log.extend('emit')
|
|
80
77
|
|
|
81
|
-
log('emitting "%s" event...',
|
|
78
|
+
log('emitting "%s" event...', eventName)
|
|
82
79
|
|
|
83
80
|
if (this.readyState === AsyncEventEmitterReadyState.DEACTIVATED) {
|
|
84
81
|
log('the emitter is destroyed, skipping!')
|
|
85
82
|
return false
|
|
86
83
|
}
|
|
87
84
|
|
|
85
|
+
// Skip establishing event queues for internal listeners.
|
|
86
|
+
// Those are not meant to be awaited.
|
|
87
|
+
if (this.isInternalEventName(eventName)) {
|
|
88
|
+
return super.emit(eventName, ...data)
|
|
89
|
+
}
|
|
90
|
+
|
|
88
91
|
// Establish the Promise queue for this particular event.
|
|
89
|
-
this.openListenerQueue(
|
|
92
|
+
this.openListenerQueue(eventName)
|
|
90
93
|
|
|
91
|
-
log('appending a one-time cleanup "%s" listener...',
|
|
94
|
+
log('appending a one-time cleanup "%s" listener...', eventName)
|
|
92
95
|
|
|
93
96
|
// Append a one-time clean up listener.
|
|
94
|
-
this.once(
|
|
97
|
+
this.once(eventName, () => {
|
|
95
98
|
// Clear the Promise queue for this particular event
|
|
96
99
|
// in the next tick so the Promise in "untilIdle" has
|
|
97
|
-
// time to properly resolve.
|
|
100
|
+
// the time to properly resolve.
|
|
98
101
|
nextTick(() => {
|
|
99
|
-
this.queue.delete(
|
|
100
|
-
log('cleaned up "%s" listeners queue!',
|
|
102
|
+
this.queue.delete(eventName)
|
|
103
|
+
log('cleaned up "%s" listeners queue!', eventName)
|
|
101
104
|
})
|
|
102
|
-
})
|
|
105
|
+
})
|
|
103
106
|
|
|
104
|
-
return super.emit(
|
|
107
|
+
return super.emit(eventName, ...data)
|
|
105
108
|
}
|
|
106
109
|
|
|
107
110
|
/**
|
|
@@ -109,35 +112,32 @@ export class AsyncEventEmitter<
|
|
|
109
112
|
* has been called. Awaits asynchronous listeners.
|
|
110
113
|
* If the event has no listeners, resolves immediately.
|
|
111
114
|
*/
|
|
112
|
-
public async untilIdle<
|
|
113
|
-
|
|
114
|
-
filter: (item: QueueItem<
|
|
115
|
-
true
|
|
115
|
+
public async untilIdle<EventName extends keyof Events>(
|
|
116
|
+
eventName: EventName,
|
|
117
|
+
filter: (item: QueueItem<Events[EventName]>) => boolean = () => true
|
|
116
118
|
): Promise<void> {
|
|
117
|
-
const listenersQueue = this.queue.get(
|
|
119
|
+
const listenersQueue = this.queue.get(eventName) || []
|
|
118
120
|
|
|
119
121
|
await Promise.all(
|
|
120
122
|
listenersQueue.filter(filter).map(({ done }) => done)
|
|
121
123
|
).finally(() => {
|
|
122
124
|
// Clear the queue one the promise settles
|
|
123
125
|
// so that different events don't share the same queue.
|
|
124
|
-
this.queue.delete(
|
|
126
|
+
this.queue.delete(eventName)
|
|
125
127
|
})
|
|
126
128
|
}
|
|
127
129
|
|
|
128
|
-
private openListenerQueue<
|
|
129
|
-
|
|
130
|
-
): QueueItem<
|
|
130
|
+
private openListenerQueue<EventName extends keyof Events>(
|
|
131
|
+
eventName: EventName
|
|
132
|
+
): Array<QueueItem<Events[EventName]>> {
|
|
131
133
|
const log = this.log.extend('openListenerQueue')
|
|
134
|
+
log('opening "%s" listeners queue...', eventName)
|
|
132
135
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const queue = this.queue.get(event)
|
|
136
|
+
const queue = this.queue.get(eventName)
|
|
136
137
|
|
|
137
138
|
if (!queue) {
|
|
138
139
|
log('no queue found, creating one...')
|
|
139
|
-
|
|
140
|
-
this.queue.set(event, [])
|
|
140
|
+
this.queue.set(eventName, [])
|
|
141
141
|
return []
|
|
142
142
|
}
|
|
143
143
|
|
|
@@ -145,19 +145,25 @@ export class AsyncEventEmitter<
|
|
|
145
145
|
return queue
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
public removeAllListeners<
|
|
148
|
+
public removeAllListeners<EventName extends keyof Events>(
|
|
149
|
+
eventName?: EventName
|
|
150
|
+
) {
|
|
149
151
|
const log = this.log.extend('removeAllListeners')
|
|
150
|
-
log('event:',
|
|
151
|
-
|
|
152
|
-
if (
|
|
153
|
-
this.queue.delete(
|
|
154
|
-
log(
|
|
152
|
+
log('event:', eventName)
|
|
153
|
+
|
|
154
|
+
if (eventName) {
|
|
155
|
+
this.queue.delete(eventName)
|
|
156
|
+
log(
|
|
157
|
+
'cleared the "%s" listeners queue!',
|
|
158
|
+
eventName,
|
|
159
|
+
this.queue.get(eventName)
|
|
160
|
+
)
|
|
155
161
|
} else {
|
|
156
162
|
this.queue.clear()
|
|
157
163
|
log('cleared the listeners queue!', this.queue)
|
|
158
164
|
}
|
|
159
165
|
|
|
160
|
-
return super.removeAllListeners(
|
|
166
|
+
return super.removeAllListeners(eventName)
|
|
161
167
|
}
|
|
162
168
|
|
|
163
169
|
public activate(): void {
|
|
@@ -180,4 +186,8 @@ export class AsyncEventEmitter<
|
|
|
180
186
|
this.readyState = AsyncEventEmitterReadyState.DEACTIVATED
|
|
181
187
|
log('set state to:', this.readyState)
|
|
182
188
|
}
|
|
189
|
+
|
|
190
|
+
private isInternalEventName(eventName: string | number | symbol): boolean {
|
|
191
|
+
return eventName === 'newListener' || eventName === 'removeListener'
|
|
192
|
+
}
|
|
183
193
|
}
|