@mswjs/interceptors 0.39.7 → 0.40.0
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/{chunk-PFGO5BSM.js → chunk-2HUMWGRD.js} +15 -3
- package/lib/browser/chunk-2HUMWGRD.js.map +1 -0
- package/lib/browser/{chunk-ZXAL3FMU.js → chunk-2MCNQOY3.js} +56 -51
- package/lib/browser/chunk-2MCNQOY3.js.map +1 -0
- package/lib/browser/chunk-57RIRQUY.js +218 -0
- package/lib/browser/chunk-57RIRQUY.js.map +1 -0
- package/lib/browser/chunk-FW45TRCB.js +178 -0
- package/lib/browser/chunk-FW45TRCB.js.map +1 -0
- package/lib/browser/{chunk-TIPR373R.js → chunk-JQ2S7G56.js} +19 -3
- package/lib/browser/chunk-JQ2S7G56.js.map +1 -0
- package/lib/browser/{chunk-3RXCRGL2.mjs → chunk-LIKZF2VU.mjs} +102 -1
- package/lib/browser/chunk-LIKZF2VU.mjs.map +1 -0
- package/lib/browser/{chunk-F2NPA2FP.js → chunk-MNT2FUCH.js} +61 -56
- package/lib/browser/chunk-MNT2FUCH.js.map +1 -0
- package/lib/browser/chunk-VOUOVDAW.mjs +178 -0
- package/lib/browser/chunk-VOUOVDAW.mjs.map +1 -0
- package/lib/browser/{chunk-TX5GBTFY.mjs → chunk-VYSDLBSS.mjs} +13 -1
- package/lib/browser/chunk-VYSDLBSS.mjs.map +1 -0
- package/lib/browser/{chunk-ZDGZFWQH.mjs → chunk-WADP6VHN.mjs} +49 -44
- package/lib/browser/chunk-WADP6VHN.mjs.map +1 -0
- package/lib/browser/{chunk-MDMPOBGY.mjs → chunk-WOWPV4GR.mjs} +52 -47
- package/lib/browser/chunk-WOWPV4GR.mjs.map +1 -0
- package/lib/browser/{chunk-QED3Q6Z2.mjs → chunk-Z5TSB3T6.mjs} +17 -1
- package/lib/browser/{chunk-QED3Q6Z2.mjs.map → chunk-Z5TSB3T6.mjs.map} +1 -1
- package/lib/browser/{glossary-7152281e.d.ts → glossary-f7ee1c9d.d.ts} +22 -17
- package/lib/browser/index.d.ts +1 -2
- package/lib/browser/index.js +6 -4
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +4 -2
- package/lib/browser/index.mjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.js +9 -7
- package/lib/browser/interceptors/WebSocket/index.js.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.mjs +6 -4
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +1 -2
- package/lib/browser/interceptors/XMLHttpRequest/index.js +6 -6
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +5 -5
- package/lib/browser/interceptors/fetch/index.d.ts +1 -2
- package/lib/browser/interceptors/fetch/index.js +6 -6
- package/lib/browser/interceptors/fetch/index.mjs +5 -5
- package/lib/browser/presets/browser.d.ts +1 -2
- package/lib/browser/presets/browser.js +8 -8
- package/lib/browser/presets/browser.mjs +6 -6
- package/lib/node/{BatchInterceptor-5b72232f.d.ts → BatchInterceptor-cb9a2eee.d.ts} +1 -1
- package/lib/node/{Interceptor-bc5a9d8e.d.ts → Interceptor-dc0a39b5.d.ts} +22 -16
- package/lib/node/RemoteHttpInterceptor.d.ts +2 -3
- package/lib/node/RemoteHttpInterceptor.js +31 -27
- package/lib/node/RemoteHttpInterceptor.js.map +1 -1
- package/lib/node/RemoteHttpInterceptor.mjs +28 -24
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/{chunk-EKNRB5ZS.mjs → chunk-5UGIB6OX.mjs} +40 -29
- package/lib/node/chunk-5UGIB6OX.mjs.map +1 -0
- package/lib/node/{chunk-4NEYTVWD.mjs → chunk-5V3SIIW2.mjs} +48 -43
- package/lib/node/chunk-5V3SIIW2.mjs.map +1 -0
- package/lib/node/{chunk-VV2LUF5K.js → chunk-6B3ZQOO2.js} +51 -46
- package/lib/node/chunk-6B3ZQOO2.js.map +1 -0
- package/lib/node/chunk-7Q53NNPV.js +189 -0
- package/lib/node/chunk-7Q53NNPV.js.map +1 -0
- package/lib/node/{chunk-A7U44ARP.js → chunk-DOWWQYXZ.js} +104 -3
- package/lib/node/chunk-DOWWQYXZ.js.map +1 -0
- package/lib/node/{chunk-Z5LWCBZS.js → chunk-FRZQJNBO.js} +56 -51
- package/lib/node/chunk-FRZQJNBO.js.map +1 -0
- package/lib/node/{chunk-TJDMZZXE.mjs → chunk-GKN5RBVR.mjs} +2 -2
- package/lib/node/{chunk-R6JVCM7X.js → chunk-J5MULIHT.js} +3 -3
- package/lib/node/{chunk-IHJSPMYM.mjs → chunk-JXGB54LE.mjs} +102 -1
- package/lib/node/chunk-JXGB54LE.mjs.map +1 -0
- package/lib/node/{chunk-3CNGDJFB.mjs → chunk-OFW5L5ET.mjs} +50 -45
- package/lib/node/chunk-OFW5L5ET.mjs.map +1 -0
- package/lib/node/{chunk-A7Q4RTDJ.mjs → chunk-R6T7CL5E.mjs} +55 -115
- package/lib/node/chunk-R6T7CL5E.mjs.map +1 -0
- package/lib/node/{chunk-RC2XPCC4.mjs → chunk-SQ6RHTJR.mjs} +2 -2
- package/lib/node/chunk-SRMAQGPM.js +30 -0
- package/lib/node/chunk-SRMAQGPM.js.map +1 -0
- package/lib/node/{chunk-4YBV77DG.js → chunk-T3TW4P64.js} +3 -3
- package/lib/node/{chunk-N4ZZFE24.js → chunk-VYO5XDY2.js} +56 -45
- package/lib/node/chunk-VYO5XDY2.js.map +1 -0
- package/lib/node/chunk-YWNGXXUQ.mjs +30 -0
- package/lib/node/{chunk-3GJB4JDF.mjs.map → chunk-YWNGXXUQ.mjs.map} +1 -1
- package/lib/node/index.d.ts +2 -3
- package/lib/node/index.js +6 -4
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +5 -3
- package/lib/node/index.mjs.map +1 -1
- package/lib/node/interceptors/ClientRequest/index.d.ts +1 -2
- package/lib/node/interceptors/ClientRequest/index.js +6 -6
- package/lib/node/interceptors/ClientRequest/index.mjs +5 -5
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +1 -2
- package/lib/node/interceptors/XMLHttpRequest/index.js +5 -5
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +4 -4
- package/lib/node/interceptors/fetch/index.d.ts +1 -2
- package/lib/node/interceptors/fetch/index.js +5 -5
- package/lib/node/interceptors/fetch/index.mjs +4 -4
- package/lib/node/presets/node.d.ts +1 -2
- package/lib/node/presets/node.js +10 -10
- package/lib/node/presets/node.mjs +7 -7
- package/lib/node/utils/node/index.js +3 -3
- package/lib/node/utils/node/index.mjs +2 -2
- package/package.json +2 -1
- package/src/RemoteHttpInterceptor.ts +18 -13
- package/src/RequestController.test.ts +78 -31
- package/src/RequestController.ts +63 -39
- package/src/index.ts +4 -0
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +24 -3
- package/src/interceptors/ClientRequest/index.ts +14 -18
- package/src/interceptors/WebSocket/index.ts +6 -2
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +45 -35
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestProxy.ts +24 -21
- package/src/interceptors/fetch/index.ts +61 -50
- package/src/utils/handleRequest.ts +65 -95
- package/lib/browser/chunk-3RXCRGL2.mjs.map +0 -1
- package/lib/browser/chunk-F2NPA2FP.js.map +0 -1
- package/lib/browser/chunk-MDMPOBGY.mjs.map +0 -1
- package/lib/browser/chunk-MMKGBEJO.mjs +0 -246
- package/lib/browser/chunk-MMKGBEJO.mjs.map +0 -1
- package/lib/browser/chunk-PFGO5BSM.js.map +0 -1
- package/lib/browser/chunk-T7TBRNJZ.js +0 -117
- package/lib/browser/chunk-T7TBRNJZ.js.map +0 -1
- package/lib/browser/chunk-TIPR373R.js.map +0 -1
- package/lib/browser/chunk-TX5GBTFY.mjs.map +0 -1
- package/lib/browser/chunk-XX6WKANU.js +0 -246
- package/lib/browser/chunk-XX6WKANU.js.map +0 -1
- package/lib/browser/chunk-ZDGZFWQH.mjs.map +0 -1
- package/lib/browser/chunk-ZXAL3FMU.js.map +0 -1
- package/lib/node/chunk-3CNGDJFB.mjs.map +0 -1
- package/lib/node/chunk-3GJB4JDF.mjs +0 -14
- package/lib/node/chunk-4NEYTVWD.mjs.map +0 -1
- package/lib/node/chunk-72ZIHMEB.js +0 -249
- package/lib/node/chunk-72ZIHMEB.js.map +0 -1
- package/lib/node/chunk-A7Q4RTDJ.mjs.map +0 -1
- package/lib/node/chunk-A7U44ARP.js.map +0 -1
- package/lib/node/chunk-EKNRB5ZS.mjs.map +0 -1
- package/lib/node/chunk-IHJSPMYM.mjs.map +0 -1
- package/lib/node/chunk-N4ZZFE24.js.map +0 -1
- package/lib/node/chunk-SMXZPJEA.js +0 -14
- package/lib/node/chunk-SMXZPJEA.js.map +0 -1
- package/lib/node/chunk-VV2LUF5K.js.map +0 -1
- package/lib/node/chunk-Z5LWCBZS.js.map +0 -1
- package/src/utils/RequestController.ts +0 -21
- /package/lib/node/{chunk-TJDMZZXE.mjs.map → chunk-GKN5RBVR.mjs.map} +0 -0
- /package/lib/node/{chunk-R6JVCM7X.js.map → chunk-J5MULIHT.js.map} +0 -0
- /package/lib/node/{chunk-RC2XPCC4.mjs.map → chunk-SQ6RHTJR.mjs.map} +0 -0
- /package/lib/node/{chunk-4YBV77DG.js.map → chunk-T3TW4P64.js.map} +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('../../chunk-
|
|
5
|
-
require('../../chunk-
|
|
6
|
-
require('../../chunk-
|
|
7
|
-
require('../../chunk-
|
|
3
|
+
var _chunkMNT2FUCHjs = require('../../chunk-MNT2FUCH.js');
|
|
4
|
+
require('../../chunk-FW45TRCB.js');
|
|
5
|
+
require('../../chunk-57RIRQUY.js');
|
|
6
|
+
require('../../chunk-2HUMWGRD.js');
|
|
7
|
+
require('../../chunk-JQ2S7G56.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
exports.FetchInterceptor =
|
|
10
|
+
exports.FetchInterceptor = _chunkMNT2FUCHjs.FetchInterceptor;
|
|
11
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
FetchInterceptor
|
|
3
|
-
} from "../../chunk-
|
|
4
|
-
import "../../chunk-
|
|
5
|
-
import "../../chunk-
|
|
6
|
-
import "../../chunk-
|
|
7
|
-
import "../../chunk-
|
|
3
|
+
} from "../../chunk-WOWPV4GR.mjs";
|
|
4
|
+
import "../../chunk-VOUOVDAW.mjs";
|
|
5
|
+
import "../../chunk-LIKZF2VU.mjs";
|
|
6
|
+
import "../../chunk-VYSDLBSS.mjs";
|
|
7
|
+
import "../../chunk-Z5TSB3T6.mjs";
|
|
8
8
|
export {
|
|
9
9
|
FetchInterceptor
|
|
10
10
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { FetchInterceptor } from '../interceptors/fetch/index.js';
|
|
2
2
|
import { XMLHttpRequestInterceptor } from '../interceptors/XMLHttpRequest/index.js';
|
|
3
|
-
import '../glossary-
|
|
4
|
-
import '@open-draft/deferred-promise';
|
|
3
|
+
import '../glossary-f7ee1c9d.js';
|
|
5
4
|
import '../Interceptor-af98b768.js';
|
|
6
5
|
import '@open-draft/logger';
|
|
7
6
|
import 'strict-event-emitter';
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunk2MCNQOY3js = require('../chunk-2MCNQOY3.js');
|
|
4
4
|
require('../chunk-LK6DILFK.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
8
|
-
require('../chunk-
|
|
9
|
-
require('../chunk-
|
|
10
|
-
require('../chunk-
|
|
11
|
-
require('../chunk-
|
|
7
|
+
var _chunkMNT2FUCHjs = require('../chunk-MNT2FUCH.js');
|
|
8
|
+
require('../chunk-FW45TRCB.js');
|
|
9
|
+
require('../chunk-57RIRQUY.js');
|
|
10
|
+
require('../chunk-2HUMWGRD.js');
|
|
11
|
+
require('../chunk-JQ2S7G56.js');
|
|
12
12
|
|
|
13
13
|
// src/presets/browser.ts
|
|
14
14
|
var browser_default = [
|
|
15
|
-
new (0,
|
|
16
|
-
new (0,
|
|
15
|
+
new (0, _chunkMNT2FUCHjs.FetchInterceptor)(),
|
|
16
|
+
new (0, _chunk2MCNQOY3js.XMLHttpRequestInterceptor)()
|
|
17
17
|
];
|
|
18
18
|
|
|
19
19
|
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
XMLHttpRequestInterceptor
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-WADP6VHN.mjs";
|
|
4
4
|
import "../chunk-6HYIRFX2.mjs";
|
|
5
5
|
import {
|
|
6
6
|
FetchInterceptor
|
|
7
|
-
} from "../chunk-
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
7
|
+
} from "../chunk-WOWPV4GR.mjs";
|
|
8
|
+
import "../chunk-VOUOVDAW.mjs";
|
|
9
|
+
import "../chunk-LIKZF2VU.mjs";
|
|
10
|
+
import "../chunk-VYSDLBSS.mjs";
|
|
11
|
+
import "../chunk-Z5TSB3T6.mjs";
|
|
12
12
|
|
|
13
13
|
// src/presets/browser.ts
|
|
14
14
|
var browser_default = [
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventMap, Listener } from 'strict-event-emitter';
|
|
2
|
-
import {
|
|
2
|
+
import { i as Interceptor, E as ExtractEventNames } from './Interceptor-dc0a39b5.js';
|
|
3
3
|
|
|
4
4
|
interface BatchInterceptorOptions<InterceptorList extends ReadonlyArray<Interceptor<any>>> {
|
|
5
5
|
name: string;
|
|
@@ -1,27 +1,33 @@
|
|
|
1
|
-
import { DeferredPromise } from '@open-draft/deferred-promise';
|
|
2
1
|
import { Logger } from '@open-draft/logger';
|
|
3
2
|
import { Emitter, Listener } from 'strict-event-emitter';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
interface RequestControllerSource {
|
|
5
|
+
passthrough(): void;
|
|
6
|
+
respondWith(response: Response): void;
|
|
7
|
+
errorWith(reason?: unknown): void;
|
|
8
|
+
}
|
|
7
9
|
declare class RequestController {
|
|
8
|
-
private
|
|
10
|
+
#private;
|
|
11
|
+
protected readonly request: Request;
|
|
12
|
+
protected readonly source: RequestControllerSource;
|
|
13
|
+
static PENDING: 0;
|
|
14
|
+
static PASSTHROUGH: 1;
|
|
15
|
+
static RESPONSE: 2;
|
|
16
|
+
static ERROR: 3;
|
|
17
|
+
readyState: number;
|
|
9
18
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* response instance provided by the developer.
|
|
13
|
-
* @note This promise cannot be rejected. It's either infinitely
|
|
14
|
-
* pending or resolved with whichever Response was passed to `respondWith()`.
|
|
19
|
+
* A Promise that resolves when this controller handles a request.
|
|
20
|
+
* See `controller.readyState` for more information on the handling result.
|
|
15
21
|
*/
|
|
16
|
-
|
|
22
|
+
handled: Promise<void>;
|
|
23
|
+
constructor(request: Request, source: RequestControllerSource);
|
|
17
24
|
/**
|
|
18
|
-
*
|
|
19
|
-
* @note The response promise becomes "fulfilled" on the next tick.
|
|
25
|
+
* Perform this request as-is.
|
|
20
26
|
*/
|
|
21
|
-
|
|
22
|
-
constructor(request: Request);
|
|
27
|
+
passthrough(): Promise<void>;
|
|
23
28
|
/**
|
|
24
29
|
* Respond to this request with the given `Response` instance.
|
|
30
|
+
*
|
|
25
31
|
* @example
|
|
26
32
|
* controller.respondWith(new Response())
|
|
27
33
|
* controller.respondWith(Response.json({ id }))
|
|
@@ -36,7 +42,7 @@ declare class RequestController {
|
|
|
36
42
|
* controller.errorWith(new Error('Oops!'))
|
|
37
43
|
* controller.errorWith({ message: 'Oops!'})
|
|
38
44
|
*/
|
|
39
|
-
errorWith(reason?:
|
|
45
|
+
errorWith(reason?: unknown): void;
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
declare const IS_PATCHED_MODULE: unique symbol;
|
|
@@ -127,4 +133,4 @@ declare class Interceptor<Events extends InterceptorEventMap> {
|
|
|
127
133
|
private clearInstance;
|
|
128
134
|
}
|
|
129
135
|
|
|
130
|
-
export { ExtractEventNames as E, HttpRequestEventMap as H, IS_PATCHED_MODULE as I, RequestController as R,
|
|
136
|
+
export { ExtractEventNames as E, HttpRequestEventMap as H, IS_PATCHED_MODULE as I, RequestController as R, RequestControllerSource as a, RequestCredentials as b, InterceptorEventMap as c, InterceptorSubscription as d, INTERNAL_REQUEST_ID_HEADER_NAME as e, deleteGlobalSymbol as f, getGlobalSymbol as g, InterceptorReadyState as h, Interceptor as i };
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { ChildProcess } from 'child_process';
|
|
2
|
-
import {
|
|
3
|
-
import { a as BatchInterceptor } from './BatchInterceptor-
|
|
2
|
+
import { i as Interceptor, H as HttpRequestEventMap } from './Interceptor-dc0a39b5.js';
|
|
3
|
+
import { a as BatchInterceptor } from './BatchInterceptor-cb9a2eee.js';
|
|
4
4
|
import { ClientRequestInterceptor } from './interceptors/ClientRequest/index.js';
|
|
5
5
|
import { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest/index.js';
|
|
6
6
|
import { FetchInterceptor } from './interceptors/fetch/index.js';
|
|
7
|
-
import '@open-draft/deferred-promise';
|
|
8
7
|
import '@open-draft/logger';
|
|
9
8
|
import 'strict-event-emitter';
|
|
10
9
|
import 'node:net';
|
|
@@ -1,38 +1,39 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkJ5MULIHTjs = require('./chunk-J5MULIHT.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
7
|
-
require('./chunk-
|
|
6
|
+
var _chunkVYO5XDY2js = require('./chunk-VYO5XDY2.js');
|
|
7
|
+
require('./chunk-T3TW4P64.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _chunk6B3ZQOO2js = require('./chunk-6B3ZQOO2.js');
|
|
11
11
|
require('./chunk-LK6DILFK.js');
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _chunkFRZQJNBOjs = require('./chunk-FRZQJNBO.js');
|
|
15
15
|
require('./chunk-PFGO5BSM.js');
|
|
16
16
|
require('./chunk-73NOP3T5.js');
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
var
|
|
20
|
+
var _chunk7Q53NNPVjs = require('./chunk-7Q53NNPV.js');
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
require('./chunk-
|
|
24
|
+
|
|
25
|
+
var _chunkDOWWQYXZjs = require('./chunk-DOWWQYXZ.js');
|
|
26
|
+
require('./chunk-SRMAQGPM.js');
|
|
26
27
|
|
|
27
28
|
// src/RemoteHttpInterceptor.ts
|
|
28
|
-
var RemoteHttpInterceptor = class extends
|
|
29
|
+
var RemoteHttpInterceptor = class extends _chunkJ5MULIHTjs.BatchInterceptor {
|
|
29
30
|
constructor() {
|
|
30
31
|
super({
|
|
31
32
|
name: "remote-interceptor",
|
|
32
33
|
interceptors: [
|
|
33
|
-
new (0,
|
|
34
|
-
new (0,
|
|
35
|
-
new (0,
|
|
34
|
+
new (0, _chunkVYO5XDY2js.ClientRequestInterceptor)(),
|
|
35
|
+
new (0, _chunk6B3ZQOO2js.XMLHttpRequestInterceptor)(),
|
|
36
|
+
new (0, _chunkFRZQJNBOjs.FetchInterceptor)()
|
|
36
37
|
]
|
|
37
38
|
});
|
|
38
39
|
}
|
|
@@ -67,7 +68,7 @@ var RemoteHttpInterceptor = class extends _chunkR6JVCM7Xjs.BatchInterceptor {
|
|
|
67
68
|
const responseInit = JSON.parse(
|
|
68
69
|
serializedResponse
|
|
69
70
|
);
|
|
70
|
-
const mockedResponse = new (0,
|
|
71
|
+
const mockedResponse = new (0, _chunkDOWWQYXZjs.FetchResponse)(responseInit.body, {
|
|
71
72
|
url: request.url,
|
|
72
73
|
status: responseInit.status,
|
|
73
74
|
statusText: responseInit.statusText,
|
|
@@ -100,7 +101,7 @@ function requestReviver(key, value) {
|
|
|
100
101
|
return value;
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
|
-
var _RemoteHttpResolver = class extends
|
|
104
|
+
var _RemoteHttpResolver = class extends _chunkDOWWQYXZjs.Interceptor {
|
|
104
105
|
constructor(options) {
|
|
105
106
|
super(_RemoteHttpResolver.symbol);
|
|
106
107
|
this.process = options.process;
|
|
@@ -128,13 +129,14 @@ var _RemoteHttpResolver = class extends _chunkA7U44ARPjs.Interceptor {
|
|
|
128
129
|
credentials: requestJson.credentials,
|
|
129
130
|
body: requestJson.body
|
|
130
131
|
});
|
|
131
|
-
const controller = new (0,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
132
|
+
const controller = new (0, _chunkDOWWQYXZjs.RequestController)(request, {
|
|
133
|
+
passthrough: () => {
|
|
134
|
+
},
|
|
135
|
+
respondWith: async (response) => {
|
|
136
|
+
if (_chunk7Q53NNPVjs.isResponseError.call(void 0, response)) {
|
|
137
|
+
this.logger.info("received a network error!", { response });
|
|
138
|
+
throw new Error("Not implemented");
|
|
139
|
+
}
|
|
138
140
|
this.logger.info("received mocked response!", { response });
|
|
139
141
|
const responseClone = response.clone();
|
|
140
142
|
const responseText = await responseClone.text();
|
|
@@ -163,15 +165,17 @@ var _RemoteHttpResolver = class extends _chunkA7U44ARPjs.Interceptor {
|
|
|
163
165
|
serializedResponse
|
|
164
166
|
);
|
|
165
167
|
},
|
|
166
|
-
|
|
167
|
-
this.logger.info("
|
|
168
|
-
throw new Error("Not implemented");
|
|
169
|
-
},
|
|
170
|
-
onError: (error) => {
|
|
171
|
-
this.logger.info("request has errored!", { error });
|
|
168
|
+
errorWith: (reason) => {
|
|
169
|
+
this.logger.info("request has errored!", { error: reason });
|
|
172
170
|
throw new Error("Not implemented");
|
|
173
171
|
}
|
|
174
172
|
});
|
|
173
|
+
await _chunk7Q53NNPVjs.handleRequest.call(void 0, {
|
|
174
|
+
request,
|
|
175
|
+
requestId: requestJson.id,
|
|
176
|
+
controller,
|
|
177
|
+
emitter: this.emitter
|
|
178
|
+
});
|
|
175
179
|
};
|
|
176
180
|
this.subscriptions.push(() => {
|
|
177
181
|
this.process.removeListener("message", handleChildMessage);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/RemoteHttpInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCO,IAAM,wBAAN,cAAoC,iBAEzC;AAAA,EACA,cAAc;AACZ,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,cAAc;AAAA,QACZ,IAAI,yBAAyB;AAAA,QAC7B,IAAI,0BAA0B;AAAA,QAC9B,IAAI,iBAAiB;AAAA,MACvB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEU,QAAQ;AAChB,UAAM,MAAM;AAEZ,QAAI;AAEJ,SAAK,GAAG,WAAW,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AAnDrE;AAsDM,YAAM,oBAAoB,KAAK,UAAU;AAAA,QACvC,IAAI;AAAA,QACJ,QAAQ,QAAQ;AAAA,QAChB,KAAK,QAAQ;AAAA,QACb,SAAS,MAAM,KAAK,QAAQ,QAAQ,QAAQ,CAAC;AAAA,QAC7C,aAAa,QAAQ;AAAA,QACrB,MAAM,CAAC,OAAO,MAAM,EAAE,SAAS,QAAQ,MAAM,IACzC,OACA,MAAM,QAAQ,KAAK;AAAA,MACzB,CAAsB;AAEtB,WAAK,OAAO;AAAA,QACV;AAAA,QACA;AAAA,MACF;AAEA,oBAAQ,SAAR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YACH;AAAA,UACF;AAEA,iBAAO;AAAA,YACL;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,QACA,gBAAgB,CAAC,aAAa;AAC5B,eAAK,OAAO,KAAK,6BAA6B,EAAE,SAAS,CAAC;AAC1D,gBAAM,IAAI,MAAM,iBAAiB;AAAA,QACnC;AAAA,QACA,SAAS,CAAC,UAAU;AAClB,eAAK,OAAO,KAAK,wBAAwB,EAAE,MAAM,CAAC;AAClD,gBAAM,IAAI,MAAM,iBAAiB;AAAA,QACnC;AAAA,MACF,CAAC;AAAA,IACH;AAEA,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,QAAQ,eAAe,WAAW,kBAAkB;AACzD,aAAO,KAAK,wDAAwD;AAAA,IACtE,CAAC;AAED,WAAO,KAAK,kDAAkD;AAC9D,SAAK,QAAQ,YAAY,WAAW,kBAAkB;AAEtD,SAAK,QAAQ,KAAK,SAAS,MAAM,KAAK,QAAQ,CAAC;AAC/C,SAAK,QAAQ,KAAK,QAAQ,MAAM,KAAK,QAAQ,CAAC;AAAA,EAChD;AACF;AAxGO,IAAM,qBAAN;AAAM,mBACJ,SAAS,OAAO,iBAAiB","sourcesContent":["import { ChildProcess } from 'child_process'\nimport { HttpRequestEventMap } from './glossary'\nimport { Interceptor } from './Interceptor'\nimport { BatchInterceptor } from './BatchInterceptor'\nimport { ClientRequestInterceptor } from './interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from './interceptors/fetch'\nimport { handleRequest } from './utils/handleRequest'\nimport { RequestController } from './RequestController'\nimport { FetchResponse } from './utils/fetchUtils'\n\nexport interface SerializedRequest {\n id: string\n url: string\n method: string\n headers: Array<[string, string]>\n credentials: RequestCredentials\n body: string\n}\n\ninterface RevivedRequest extends Omit<SerializedRequest, 'url' | 'headers'> {\n url: URL\n headers: Headers\n}\n\nexport interface SerializedResponse {\n status: number\n statusText: string\n headers: Array<[string, string]>\n body: string\n}\n\nexport class RemoteHttpInterceptor extends BatchInterceptor<\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor]\n> {\n constructor() {\n super({\n name: 'remote-interceptor',\n interceptors: [\n new ClientRequestInterceptor(),\n new XMLHttpRequestInterceptor(),\n new FetchInterceptor(),\n ],\n })\n }\n\n protected setup() {\n super.setup()\n\n let handleParentMessage: NodeJS.MessageListener\n\n this.on('request', async ({ request, requestId, controller }) => {\n // Send the stringified intercepted request to\n // the parent process where the remote resolver is established.\n const serializedRequest = JSON.stringify({\n id: requestId,\n method: request.method,\n url: request.url,\n headers: Array.from(request.headers.entries()),\n credentials: request.credentials,\n body: ['GET', 'HEAD'].includes(request.method)\n ? null\n : await request.text(),\n } as SerializedRequest)\n\n this.logger.info(\n 'sent serialized request to the child:',\n serializedRequest\n )\n\n process.send?.(`request:${serializedRequest}`)\n\n const responsePromise = new Promise<void>((resolve) => {\n handleParentMessage = (message) => {\n if (typeof message !== 'string') {\n return resolve()\n }\n\n if (message.startsWith(`response:${requestId}`)) {\n const [, serializedResponse] =\n message.match(/^response:.+?:(.+)$/) || []\n\n if (!serializedResponse) {\n return resolve()\n }\n\n const responseInit = JSON.parse(\n serializedResponse\n ) as SerializedResponse\n\n const mockedResponse = new FetchResponse(responseInit.body, {\n url: request.url,\n status: responseInit.status,\n statusText: responseInit.statusText,\n headers: responseInit.headers,\n })\n\n /**\n * @todo Support \"errorWith\" as well.\n * This response handling from the child is incomplete.\n */\n\n controller.respondWith(mockedResponse)\n return resolve()\n }\n }\n })\n\n // Listen for the mocked response message from the parent.\n this.logger.info(\n 'add \"message\" listener to the parent process',\n handleParentMessage\n )\n process.addListener('message', handleParentMessage)\n\n return responsePromise\n })\n\n this.subscriptions.push(() => {\n process.removeListener('message', handleParentMessage)\n })\n }\n}\n\nexport function requestReviver(key: string, value: any) {\n switch (key) {\n case 'url':\n return new URL(value)\n\n case 'headers':\n return new Headers(value)\n\n default:\n return value\n }\n}\n\nexport interface RemoveResolverOptions {\n process: ChildProcess\n}\n\nexport class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('remote-resolver')\n private process: ChildProcess\n\n constructor(options: RemoveResolverOptions) {\n super(RemoteHttpResolver.symbol)\n this.process = options.process\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n const handleChildMessage: NodeJS.MessageListener = async (message) => {\n logger.info('received message from child!', message)\n\n if (typeof message !== 'string' || !message.startsWith('request:')) {\n logger.info('unknown message, ignoring...')\n return\n }\n\n const [, serializedRequest] = message.match(/^request:(.+)$/) || []\n if (!serializedRequest) {\n return\n }\n\n const requestJson = JSON.parse(\n serializedRequest,\n requestReviver\n ) as RevivedRequest\n\n logger.info('parsed intercepted request', requestJson)\n\n const request = new Request(requestJson.url, {\n method: requestJson.method,\n headers: new Headers(requestJson.headers),\n credentials: requestJson.credentials,\n body: requestJson.body,\n })\n\n const controller = new RequestController(request)\n await handleRequest({\n request,\n requestId: requestJson.id,\n controller,\n emitter: this.emitter,\n onResponse: async (response) => {\n this.logger.info('received mocked response!', { response })\n\n const responseClone = response.clone()\n const responseText = await responseClone.text()\n\n // // Send the mocked response to the child process.\n const serializedResponse = JSON.stringify({\n status: response.status,\n statusText: response.statusText,\n headers: Array.from(response.headers.entries()),\n body: responseText,\n } as SerializedResponse)\n\n this.process.send(\n `response:${requestJson.id}:${serializedResponse}`,\n (error) => {\n if (error) {\n return\n }\n\n // Emit an optimistic \"response\" event at this point,\n // not to rely on the back-and-forth signaling for the sake of the event.\n this.emitter.emit('response', {\n request,\n requestId: requestJson.id,\n response: responseClone,\n isMockedResponse: true,\n })\n }\n )\n\n logger.info(\n 'sent serialized mocked response to the parent:',\n serializedResponse\n )\n },\n onRequestError: (response) => {\n this.logger.info('received a network error!', { response })\n throw new Error('Not implemented')\n },\n onError: (error) => {\n this.logger.info('request has errored!', { error })\n throw new Error('Not implemented')\n },\n })\n }\n\n this.subscriptions.push(() => {\n this.process.removeListener('message', handleChildMessage)\n logger.info('removed the \"message\" listener from the child process!')\n })\n\n logger.info('adding a \"message\" listener to the child process')\n this.process.addListener('message', handleChildMessage)\n\n this.process.once('error', () => this.dispose())\n this.process.once('exit', () => this.dispose())\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/RemoteHttpInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCO,IAAM,wBAAN,cAAoC,iBAEzC;AAAA,EACA,cAAc;AACZ,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,cAAc;AAAA,QACZ,IAAI,yBAAyB;AAAA,QAC7B,IAAI,0BAA0B;AAAA,QAC9B,IAAI,iBAAiB;AAAA,MACvB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEU,QAAQ;AAChB,UAAM,MAAM;AAEZ,QAAI;AAEJ,SAAK,GAAG,WAAW,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AApDrE;AAuDM,YAAM,oBAAoB,KAAK,UAAU;AAAA,QACvC,IAAI;AAAA,QACJ,QAAQ,QAAQ;AAAA,QAChB,KAAK,QAAQ;AAAA,QACb,SAAS,MAAM,KAAK,QAAQ,QAAQ,QAAQ,CAAC;AAAA,QAC7C,aAAa,QAAQ;AAAA,QACrB,MAAM,CAAC,OAAO,MAAM,EAAE,SAAS,QAAQ,MAAM,IACzC,OACA,MAAM,QAAQ,KAAK;AAAA,MACzB,CAAsB;AAEtB,WAAK,OAAO;AAAA,QACV;AAAA,QACA;AAAA,MACF;AAEA,oBAAQ,SAAR,iCAAe,WAAW;AAE1B,YAAM,kBAAkB,IAAI,QAAc,CAAC,YAAY;AACrD,8BAAsB,CAAC,YAAY;AACjC,cAAI,OAAO,YAAY,UAAU;AAC/B,mBAAO,QAAQ;AAAA,UACjB;AAEA,cAAI,QAAQ,WAAW,YAAY,WAAW,GAAG;AAC/C,kBAAM,CAAC,EAAE,kBAAkB,IACzB,QAAQ,MAAM,qBAAqB,KAAK,CAAC;AAE3C,gBAAI,CAAC,oBA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,MACF,CAAC;AAED,YAAM,cAAc;AAAA,QAClB;AAAA,QACA,WAAW,YAAY;AAAA,QACvB;AAAA,QACA,SAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH;AAEA,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,QAAQ,eAAe,WAAW,kBAAkB;AACzD,aAAO,KAAK,wDAAwD;AAAA,IACtE,CAAC;AAED,WAAO,KAAK,kDAAkD;AAC9D,SAAK,QAAQ,YAAY,WAAW,kBAAkB;AAEtD,SAAK,QAAQ,KAAK,SAAS,MAAM,KAAK,QAAQ,CAAC;AAC/C,SAAK,QAAQ,KAAK,QAAQ,MAAM,KAAK,QAAQ,CAAC;AAAA,EAChD;AACF;AA5GO,IAAM,qBAAN;AAAM,mBACJ,SAAS,OAAO,iBAAiB","sourcesContent":["import { ChildProcess } from 'child_process'\nimport { HttpRequestEventMap } from './glossary'\nimport { Interceptor } from './Interceptor'\nimport { BatchInterceptor } from './BatchInterceptor'\nimport { ClientRequestInterceptor } from './interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from './interceptors/fetch'\nimport { handleRequest } from './utils/handleRequest'\nimport { RequestController } from './RequestController'\nimport { FetchResponse } from './utils/fetchUtils'\nimport { isResponseError } from './utils/responseUtils'\n\nexport interface SerializedRequest {\n id: string\n url: string\n method: string\n headers: Array<[string, string]>\n credentials: RequestCredentials\n body: string\n}\n\ninterface RevivedRequest extends Omit<SerializedRequest, 'url' | 'headers'> {\n url: URL\n headers: Headers\n}\n\nexport interface SerializedResponse {\n status: number\n statusText: string\n headers: Array<[string, string]>\n body: string\n}\n\nexport class RemoteHttpInterceptor extends BatchInterceptor<\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor]\n> {\n constructor() {\n super({\n name: 'remote-interceptor',\n interceptors: [\n new ClientRequestInterceptor(),\n new XMLHttpRequestInterceptor(),\n new FetchInterceptor(),\n ],\n })\n }\n\n protected setup() {\n super.setup()\n\n let handleParentMessage: NodeJS.MessageListener\n\n this.on('request', async ({ request, requestId, controller }) => {\n // Send the stringified intercepted request to\n // the parent process where the remote resolver is established.\n const serializedRequest = JSON.stringify({\n id: requestId,\n method: request.method,\n url: request.url,\n headers: Array.from(request.headers.entries()),\n credentials: request.credentials,\n body: ['GET', 'HEAD'].includes(request.method)\n ? null\n : await request.text(),\n } as SerializedRequest)\n\n this.logger.info(\n 'sent serialized request to the child:',\n serializedRequest\n )\n\n process.send?.(`request:${serializedRequest}`)\n\n const responsePromise = new Promise<void>((resolve) => {\n handleParentMessage = (message) => {\n if (typeof message !== 'string') {\n return resolve()\n }\n\n if (message.startsWith(`response:${requestId}`)) {\n const [, serializedResponse] =\n message.match(/^response:.+?:(.+)$/) || []\n\n if (!serializedResponse) {\n return resolve()\n }\n\n const responseInit = JSON.parse(\n serializedResponse\n ) as SerializedResponse\n\n const mockedResponse = new FetchResponse(responseInit.body, {\n url: request.url,\n status: responseInit.status,\n statusText: responseInit.statusText,\n headers: responseInit.headers,\n })\n\n /**\n * @todo Support \"errorWith\" as well.\n * This response handling from the child is incomplete.\n */\n\n controller.respondWith(mockedResponse)\n return resolve()\n }\n }\n })\n\n // Listen for the mocked response message from the parent.\n this.logger.info(\n 'add \"message\" listener to the parent process',\n handleParentMessage\n )\n process.addListener('message', handleParentMessage)\n\n return responsePromise\n })\n\n this.subscriptions.push(() => {\n process.removeListener('message', handleParentMessage)\n })\n }\n}\n\nexport function requestReviver(key: string, value: any) {\n switch (key) {\n case 'url':\n return new URL(value)\n\n case 'headers':\n return new Headers(value)\n\n default:\n return value\n }\n}\n\nexport interface RemoveResolverOptions {\n process: ChildProcess\n}\n\nexport class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('remote-resolver')\n private process: ChildProcess\n\n constructor(options: RemoveResolverOptions) {\n super(RemoteHttpResolver.symbol)\n this.process = options.process\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n const handleChildMessage: NodeJS.MessageListener = async (message) => {\n logger.info('received message from child!', message)\n\n if (typeof message !== 'string' || !message.startsWith('request:')) {\n logger.info('unknown message, ignoring...')\n return\n }\n\n const [, serializedRequest] = message.match(/^request:(.+)$/) || []\n if (!serializedRequest) {\n return\n }\n\n const requestJson = JSON.parse(\n serializedRequest,\n requestReviver\n ) as RevivedRequest\n\n logger.info('parsed intercepted request', requestJson)\n\n const request = new Request(requestJson.url, {\n method: requestJson.method,\n headers: new Headers(requestJson.headers),\n credentials: requestJson.credentials,\n body: requestJson.body,\n })\n\n const controller = new RequestController(request, {\n passthrough: () => {},\n respondWith: async (response) => {\n if (isResponseError(response)) {\n this.logger.info('received a network error!', { response })\n throw new Error('Not implemented')\n }\n\n this.logger.info('received mocked response!', { response })\n\n const responseClone = response.clone()\n const responseText = await responseClone.text()\n\n // // Send the mocked response to the child process.\n const serializedResponse = JSON.stringify({\n status: response.status,\n statusText: response.statusText,\n headers: Array.from(response.headers.entries()),\n body: responseText,\n } as SerializedResponse)\n\n this.process.send(\n `response:${requestJson.id}:${serializedResponse}`,\n (error) => {\n if (error) {\n return\n }\n\n // Emit an optimistic \"response\" event at this point,\n // not to rely on the back-and-forth signaling for the sake of the event.\n this.emitter.emit('response', {\n request,\n requestId: requestJson.id,\n response: responseClone,\n isMockedResponse: true,\n })\n }\n )\n\n logger.info(\n 'sent serialized mocked response to the parent:',\n serializedResponse\n )\n },\n errorWith: (reason) => {\n this.logger.info('request has errored!', { error: reason })\n throw new Error('Not implemented')\n },\n })\n\n await handleRequest({\n request,\n requestId: requestJson.id,\n controller,\n emitter: this.emitter,\n })\n }\n\n this.subscriptions.push(() => {\n this.process.removeListener('message', handleChildMessage)\n logger.info('removed the \"message\" listener from the child process!')\n })\n\n logger.info('adding a \"message\" listener to the child process')\n this.process.addListener('message', handleChildMessage)\n\n this.process.once('error', () => this.dispose())\n this.process.once('exit', () => this.dispose())\n }\n}\n"]}
|
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BatchInterceptor
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-SQ6RHTJR.mjs";
|
|
4
4
|
import {
|
|
5
5
|
ClientRequestInterceptor
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import "./chunk-
|
|
6
|
+
} from "./chunk-5UGIB6OX.mjs";
|
|
7
|
+
import "./chunk-GKN5RBVR.mjs";
|
|
8
8
|
import {
|
|
9
9
|
XMLHttpRequestInterceptor
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-5V3SIIW2.mjs";
|
|
11
11
|
import "./chunk-6HYIRFX2.mjs";
|
|
12
12
|
import {
|
|
13
13
|
FetchInterceptor
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-OFW5L5ET.mjs";
|
|
15
15
|
import "./chunk-TX5GBTFY.mjs";
|
|
16
16
|
import "./chunk-6YM4PLBI.mjs";
|
|
17
17
|
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from "./chunk-
|
|
18
|
+
handleRequest,
|
|
19
|
+
isResponseError
|
|
20
|
+
} from "./chunk-R6T7CL5E.mjs";
|
|
21
21
|
import {
|
|
22
22
|
FetchResponse,
|
|
23
|
-
Interceptor
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
Interceptor,
|
|
24
|
+
RequestController
|
|
25
|
+
} from "./chunk-JXGB54LE.mjs";
|
|
26
|
+
import "./chunk-YWNGXXUQ.mjs";
|
|
26
27
|
|
|
27
28
|
// src/RemoteHttpInterceptor.ts
|
|
28
29
|
var RemoteHttpInterceptor = class extends BatchInterceptor {
|
|
@@ -128,13 +129,14 @@ var _RemoteHttpResolver = class extends Interceptor {
|
|
|
128
129
|
credentials: requestJson.credentials,
|
|
129
130
|
body: requestJson.body
|
|
130
131
|
});
|
|
131
|
-
const controller = new RequestController(request
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
132
|
+
const controller = new RequestController(request, {
|
|
133
|
+
passthrough: () => {
|
|
134
|
+
},
|
|
135
|
+
respondWith: async (response) => {
|
|
136
|
+
if (isResponseError(response)) {
|
|
137
|
+
this.logger.info("received a network error!", { response });
|
|
138
|
+
throw new Error("Not implemented");
|
|
139
|
+
}
|
|
138
140
|
this.logger.info("received mocked response!", { response });
|
|
139
141
|
const responseClone = response.clone();
|
|
140
142
|
const responseText = await responseClone.text();
|
|
@@ -163,15 +165,17 @@ var _RemoteHttpResolver = class extends Interceptor {
|
|
|
163
165
|
serializedResponse
|
|
164
166
|
);
|
|
165
167
|
},
|
|
166
|
-
|
|
167
|
-
this.logger.info("
|
|
168
|
-
throw new Error("Not implemented");
|
|
169
|
-
},
|
|
170
|
-
onError: (error) => {
|
|
171
|
-
this.logger.info("request has errored!", { error });
|
|
168
|
+
errorWith: (reason) => {
|
|
169
|
+
this.logger.info("request has errored!", { error: reason });
|
|
172
170
|
throw new Error("Not implemented");
|
|
173
171
|
}
|
|
174
172
|
});
|
|
173
|
+
await handleRequest({
|
|
174
|
+
request,
|
|
175
|
+
requestId: requestJson.id,
|
|
176
|
+
controller,
|
|
177
|
+
emitter: this.emitter
|
|
178
|
+
});
|
|
175
179
|
};
|
|
176
180
|
this.subscriptions.push(() => {
|
|
177
181
|
this.process.removeListener("message", handleChildMessage);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/RemoteHttpInterceptor.ts"],"sourcesContent":["import { ChildProcess } from 'child_process'\nimport { HttpRequestEventMap } from './glossary'\nimport { Interceptor } from './Interceptor'\nimport { BatchInterceptor } from './BatchInterceptor'\nimport { ClientRequestInterceptor } from './interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from './interceptors/fetch'\nimport { handleRequest } from './utils/handleRequest'\nimport { RequestController } from './RequestController'\nimport { FetchResponse } from './utils/fetchUtils'\n\nexport interface SerializedRequest {\n id: string\n url: string\n method: string\n headers: Array<[string, string]>\n credentials: RequestCredentials\n body: string\n}\n\ninterface RevivedRequest extends Omit<SerializedRequest, 'url' | 'headers'> {\n url: URL\n headers: Headers\n}\n\nexport interface SerializedResponse {\n status: number\n statusText: string\n headers: Array<[string, string]>\n body: string\n}\n\nexport class RemoteHttpInterceptor extends BatchInterceptor<\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor]\n> {\n constructor() {\n super({\n name: 'remote-interceptor',\n interceptors: [\n new ClientRequestInterceptor(),\n new XMLHttpRequestInterceptor(),\n new FetchInterceptor(),\n ],\n })\n }\n\n protected setup() {\n super.setup()\n\n let handleParentMessage: NodeJS.MessageListener\n\n this.on('request', async ({ request, requestId, controller }) => {\n // Send the stringified intercepted request to\n // the parent process where the remote resolver is established.\n const serializedRequest = JSON.stringify({\n id: requestId,\n method: request.method,\n url: request.url,\n headers: Array.from(request.headers.entries()),\n credentials: request.credentials,\n body: ['GET', 'HEAD'].includes(request.method)\n ? null\n : await request.text(),\n } as SerializedRequest)\n\n this.logger.info(\n 'sent serialized request to the child:',\n serializedRequest\n )\n\n process.send?.(`request:${serializedRequest}`)\n\n const responsePromise = new Promise<void>((resolve) => {\n handleParentMessage = (message) => {\n if (typeof message !== 'string') {\n return resolve()\n }\n\n if (message.startsWith(`response:${requestId}`)) {\n const [, serializedResponse] =\n message.match(/^response:.+?:(.+)$/) || []\n\n if (!serializedResponse) {\n return resolve()\n }\n\n const responseInit = JSON.parse(\n serializedResponse\n ) as SerializedResponse\n\n const mockedResponse = new FetchResponse(responseInit.body, {\n url: request.url,\n status: responseInit.status,\n statusText: responseInit.statusText,\n headers: responseInit.headers,\n })\n\n /**\n * @todo Support \"errorWith\" as well.\n * This response handling from the child is incomplete.\n */\n\n controller.respondWith(mockedResponse)\n return resolve()\n }\n }\n })\n\n // Listen for the mocked response message from the parent.\n this.logger.info(\n 'add \"message\" listener to the parent process',\n handleParentMessage\n )\n process.addListener('message', handleParentMessage)\n\n return responsePromise\n })\n\n this.subscriptions.push(() => {\n process.removeListener('message', handleParentMessage)\n })\n }\n}\n\nexport function requestReviver(key: string, value: any) {\n switch (key) {\n case 'url':\n return new URL(value)\n\n case 'headers':\n return new Headers(value)\n\n default:\n return value\n }\n}\n\nexport interface RemoveResolverOptions {\n process: ChildProcess\n}\n\nexport class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('remote-resolver')\n private process: ChildProcess\n\n constructor(options: RemoveResolverOptions) {\n super(RemoteHttpResolver.symbol)\n this.process = options.process\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n const handleChildMessage: NodeJS.MessageListener = async (message) => {\n logger.info('received message from child!', message)\n\n if (typeof message !== 'string' || !message.startsWith('request:')) {\n logger.info('unknown message, ignoring...')\n return\n }\n\n const [, serializedRequest] = message.match(/^request:(.+)$/) || []\n if (!serializedRequest) {\n return\n }\n\n const requestJson = JSON.parse(\n serializedRequest,\n requestReviver\n ) as RevivedRequest\n\n logger.info('parsed intercepted request', requestJson)\n\n const request = new Request(requestJson.url, {\n method: requestJson.method,\n headers: new Headers(requestJson.headers),\n credentials: requestJson.credentials,\n body: requestJson.body,\n })\n\n const controller = new RequestController(request)\n await handleRequest({\n request,\n requestId: requestJson.id,\n controller,\n emitter: this.emitter,\n onResponse: async (response) => {\n this.logger.info('received mocked response!', { response })\n\n const responseClone = response.clone()\n const responseText = await responseClone.text()\n\n // // Send the mocked response to the child process.\n const serializedResponse = JSON.stringify({\n status: response.status,\n statusText: response.statusText,\n headers: Array.from(response.headers.entries()),\n body: responseText,\n } as SerializedResponse)\n\n this.process.send(\n `response:${requestJson.id}:${serializedResponse}`,\n (error) => {\n if (error) {\n return\n }\n\n // Emit an optimistic \"response\" event at this point,\n // not to rely on the back-and-forth signaling for the sake of the event.\n this.emitter.emit('response', {\n request,\n requestId: requestJson.id,\n response: responseClone,\n isMockedResponse: true,\n })\n }\n )\n\n logger.info(\n 'sent serialized mocked response to the parent:',\n serializedResponse\n )\n },\n onRequestError: (response) => {\n this.logger.info('received a network error!', { response })\n throw new Error('Not implemented')\n },\n onError: (error) => {\n this.logger.info('request has errored!', { error })\n throw new Error('Not implemented')\n },\n })\n }\n\n this.subscriptions.push(() => {\n this.process.removeListener('message', handleChildMessage)\n logger.info('removed the \"message\" listener from the child process!')\n })\n\n logger.info('adding a \"message\" listener to the child process')\n this.process.addListener('message', handleChildMessage)\n\n this.process.once('error', () => this.dispose())\n this.process.once('exit', () => this.dispose())\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCO,IAAM,wBAAN,cAAoC,iBAEzC;AAAA,EACA,cAAc;AACZ,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,cAAc;AAAA,QACZ,IAAI,yBAAyB;AAAA,QAC7B,IAAI,0BAA0B;AAAA,QAC9B,IAAI,iBAAiB;AAAA,MACvB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEU,QAAQ;AAChB,UAAM,MAAM;AAEZ,QAAI;AAEJ,SAAK,GAAG,WAAW,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AAnDrE;AAsDM,YAAM,oBAAoB,KAAK,UAAU;AAAA,QACvC,IAAI;AAAA,QACJ,QAAQ,QAAQ;AAAA,QAChB,KAAK,QAAQ;AAAA,QACb,SAAS,MAAM,KAAK,QAAQ,QAAQ,QAAQ,CAAC;AAAA,QAC7C,aAAa,QAAQ;AAAA,QACrB,MAAM,CAAC,OAAO,MAAM,EAAE,SAAS,QAAQ,MAAM,IACzC,OACA,MAAM,QAAQ,KAAK;AAAA,MACzB,CAAsB;AAEtB,WAAK,OAAO;AAAA,QACV;AAAA,QACA;AAAA,MACF;AAEA,oBAAQ,SAAR,iCAAe,WAAW;AAE1B,YAAM,kBAAkB,IAAI,QAAc,CAAC,YAAY;AACrD,8BAAsB,CAAC,YAAY;AACjC,cAAI,OAAO,YAAY,UAAU;AAC/B,mBAAO,QAAQ;AAAA,UACjB;AAEA,cAAI,QAAQ,WAAW,YA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AM,iBAAiB;AAAA,QACnC;AAAA,QACA,SAAS,CAAC,UAAU;AAClB,eAAK,OAAO,KAAK,wBAAwB,EAAE,MAAM,CAAC;AAClD,gBAAM,IAAI,MAAM,iBAAiB;AAAA,QACnC;AAAA,MACF,CAAC;AAAA,IACH;AAEA,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,QAAQ,eAAe,WAAW,kBAAkB;AACzD,aAAO,KAAK,wDAAwD;AAAA,IACtE,CAAC;AAED,WAAO,KAAK,kDAAkD;AAC9D,SAAK,QAAQ,YAAY,WAAW,kBAAkB;AAEtD,SAAK,QAAQ,KAAK,SAAS,MAAM,KAAK,QAAQ,CAAC;AAC/C,SAAK,QAAQ,KAAK,QAAQ,MAAM,KAAK,QAAQ,CAAC;AAAA,EAChD;AACF;AAxGO,IAAM,qBAAN;AAAM,mBACJ,SAAS,OAAO,iBAAiB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/RemoteHttpInterceptor.ts"],"sourcesContent":["import { ChildProcess } from 'child_process'\nimport { HttpRequestEventMap } from './glossary'\nimport { Interceptor } from './Interceptor'\nimport { BatchInterceptor } from './BatchInterceptor'\nimport { ClientRequestInterceptor } from './interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from './interceptors/fetch'\nimport { handleRequest } from './utils/handleRequest'\nimport { RequestController } from './RequestController'\nimport { FetchResponse } from './utils/fetchUtils'\nimport { isResponseError } from './utils/responseUtils'\n\nexport interface SerializedRequest {\n id: string\n url: string\n method: string\n headers: Array<[string, string]>\n credentials: RequestCredentials\n body: string\n}\n\ninterface RevivedRequest extends Omit<SerializedRequest, 'url' | 'headers'> {\n url: URL\n headers: Headers\n}\n\nexport interface SerializedResponse {\n status: number\n statusText: string\n headers: Array<[string, string]>\n body: string\n}\n\nexport class RemoteHttpInterceptor extends BatchInterceptor<\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor]\n> {\n constructor() {\n super({\n name: 'remote-interceptor',\n interceptors: [\n new ClientRequestInterceptor(),\n new XMLHttpRequestInterceptor(),\n new FetchInterceptor(),\n ],\n })\n }\n\n protected setup() {\n super.setup()\n\n let handleParentMessage: NodeJS.MessageListener\n\n this.on('request', async ({ request, requestId, controller }) => {\n // Send the stringified intercepted request to\n // the parent process where the remote resolver is established.\n const serializedRequest = JSON.stringify({\n id: requestId,\n method: request.method,\n url: request.url,\n headers: Array.from(request.headers.entries()),\n credentials: request.credentials,\n body: ['GET', 'HEAD'].includes(request.method)\n ? null\n : await request.text(),\n } as SerializedRequest)\n\n this.logger.info(\n 'sent serialized request to the child:',\n serializedRequest\n )\n\n process.send?.(`request:${serializedRequest}`)\n\n const responsePromise = new Promise<void>((resolve) => {\n handleParentMessage = (message) => {\n if (typeof message !== 'string') {\n return resolve()\n }\n\n if (message.startsWith(`response:${requestId}`)) {\n const [, serializedResponse] =\n message.match(/^response:.+?:(.+)$/) || []\n\n if (!serializedResponse) {\n return resolve()\n }\n\n const responseInit = JSON.parse(\n serializedResponse\n ) as SerializedResponse\n\n const mockedResponse = new FetchResponse(responseInit.body, {\n url: request.url,\n status: responseInit.status,\n statusText: responseInit.statusText,\n headers: responseInit.headers,\n })\n\n /**\n * @todo Support \"errorWith\" as well.\n * This response handling from the child is incomplete.\n */\n\n controller.respondWith(mockedResponse)\n return resolve()\n }\n }\n })\n\n // Listen for the mocked response message from the parent.\n this.logger.info(\n 'add \"message\" listener to the parent process',\n handleParentMessage\n )\n process.addListener('message', handleParentMessage)\n\n return responsePromise\n })\n\n this.subscriptions.push(() => {\n process.removeListener('message', handleParentMessage)\n })\n }\n}\n\nexport function requestReviver(key: string, value: any) {\n switch (key) {\n case 'url':\n return new URL(value)\n\n case 'headers':\n return new Headers(value)\n\n default:\n return value\n }\n}\n\nexport interface RemoveResolverOptions {\n process: ChildProcess\n}\n\nexport class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('remote-resolver')\n private process: ChildProcess\n\n constructor(options: RemoveResolverOptions) {\n super(RemoteHttpResolver.symbol)\n this.process = options.process\n }\n\n protected setup() {\n const logger = this.logger.extend('setup')\n\n const handleChildMessage: NodeJS.MessageListener = async (message) => {\n logger.info('received message from child!', message)\n\n if (typeof message !== 'string' || !message.startsWith('request:')) {\n logger.info('unknown message, ignoring...')\n return\n }\n\n const [, serializedRequest] = message.match(/^request:(.+)$/) || []\n if (!serializedRequest) {\n return\n }\n\n const requestJson = JSON.parse(\n serializedRequest,\n requestReviver\n ) as RevivedRequest\n\n logger.info('parsed intercepted request', requestJson)\n\n const request = new Request(requestJson.url, {\n method: requestJson.method,\n headers: new Headers(requestJson.headers),\n credentials: requestJson.credentials,\n body: requestJson.body,\n })\n\n const controller = new RequestController(request, {\n passthrough: () => {},\n respondWith: async (response) => {\n if (isResponseError(response)) {\n this.logger.info('received a network error!', { response })\n throw new Error('Not implemented')\n }\n\n this.logger.info('received mocked response!', { response })\n\n const responseClone = response.clone()\n const responseText = await responseClone.text()\n\n // // Send the mocked response to the child process.\n const serializedResponse = JSON.stringify({\n status: response.status,\n statusText: response.statusText,\n headers: Array.from(response.headers.entries()),\n body: responseText,\n } as SerializedResponse)\n\n this.process.send(\n `response:${requestJson.id}:${serializedResponse}`,\n (error) => {\n if (error) {\n return\n }\n\n // Emit an optimistic \"response\" event at this point,\n // not to rely on the back-and-forth signaling for the sake of the event.\n this.emitter.emit('response', {\n request,\n requestId: requestJson.id,\n response: responseClone,\n isMockedResponse: true,\n })\n }\n )\n\n logger.info(\n 'sent serialized mocked response to the parent:',\n serializedResponse\n )\n },\n errorWith: (reason) => {\n this.logger.info('request has errored!', { error: reason })\n throw new Error('Not implemented')\n },\n })\n\n await handleRequest({\n request,\n requestId: requestJson.id,\n controller,\n emitter: this.emitter,\n })\n }\n\n this.subscriptions.push(() => {\n this.process.removeListener('message', handleChildMessage)\n logger.info('removed the \"message\" listener from the child process!')\n })\n\n logger.info('adding a \"message\" listener to the child process')\n this.process.addListener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AA,IACH;AAEA,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,QAAQ,eAAe,WAAW,kBAAkB;AACzD,aAAO,KAAK,wDAAwD;AAAA,IACtE,CAAC;AAED,WAAO,KAAK,kDAAkD;AAC9D,SAAK,QAAQ,YAAY,WAAW,kBAAkB;AAEtD,SAAK,QAAQ,KAAK,SAAS,MAAM,KAAK,QAAQ,CAAC;AAC/C,SAAK,QAAQ,KAAK,QAAQ,MAAM,KAAK,QAAQ,CAAC;AAAA,EAChD;AACF;AA5GO,IAAM,qBAAN;AAAM,mBACJ,SAAS,OAAO,iBAAiB;","names":[]}
|
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
setRawRequestBodyStream
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-GKN5RBVR.mjs";
|
|
4
4
|
import {
|
|
5
|
-
RequestController,
|
|
6
|
-
createServerErrorResponse,
|
|
7
5
|
emitAsync,
|
|
8
6
|
handleRequest,
|
|
9
7
|
isObject,
|
|
10
8
|
isPropertyAccessible
|
|
11
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-R6T7CL5E.mjs";
|
|
12
10
|
import {
|
|
13
11
|
FetchResponse,
|
|
14
12
|
INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
15
13
|
Interceptor,
|
|
14
|
+
RequestController,
|
|
16
15
|
createRequestId
|
|
17
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-JXGB54LE.mjs";
|
|
18
17
|
import {
|
|
19
18
|
setRawRequest
|
|
20
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-YWNGXXUQ.mjs";
|
|
21
20
|
|
|
22
21
|
// src/interceptors/ClientRequest/index.ts
|
|
23
22
|
import http2 from "http";
|
|
@@ -443,6 +442,7 @@ var MockHttpSocket = class extends MockSocket {
|
|
|
443
442
|
Reflect.set(this, "getProtocol", () => "TLSv1.3");
|
|
444
443
|
Reflect.set(this, "getSession", () => void 0);
|
|
445
444
|
Reflect.set(this, "isSessionReused", () => false);
|
|
445
|
+
Reflect.set(this, "getCipher", () => ({ name: "AES256-SHA", standardName: "TLS_RSA_WITH_AES_256_CBC_SHA", version: "TLSv1.3" }));
|
|
446
446
|
}
|
|
447
447
|
}
|
|
448
448
|
emit(event, ...args) {
|
|
@@ -514,7 +514,8 @@ var MockHttpSocket = class extends MockSocket {
|
|
|
514
514
|
"authorized",
|
|
515
515
|
"getProtocol",
|
|
516
516
|
"getSession",
|
|
517
|
-
"isSessionReused"
|
|
517
|
+
"isSessionReused",
|
|
518
|
+
"getCipher"
|
|
518
519
|
];
|
|
519
520
|
tlsProperties.forEach((propertyName) => {
|
|
520
521
|
Object.defineProperty(this, propertyName, {
|
|
@@ -541,10 +542,18 @@ var MockHttpSocket = class extends MockSocket {
|
|
|
541
542
|
* HTTP message and push it to the socket.
|
|
542
543
|
*/
|
|
543
544
|
async respondWith(response) {
|
|
544
|
-
var _a;
|
|
545
|
+
var _a, _b, _c;
|
|
545
546
|
if (this.destroyed) {
|
|
546
547
|
return;
|
|
547
548
|
}
|
|
549
|
+
invariant(
|
|
550
|
+
this.socketState !== "mock",
|
|
551
|
+
'[MockHttpSocket] Failed to respond to the "%s %s" request with "%s %s": the request has already been handled',
|
|
552
|
+
(_a = this.request) == null ? void 0 : _a.method,
|
|
553
|
+
(_b = this.request) == null ? void 0 : _b.url,
|
|
554
|
+
response.status,
|
|
555
|
+
response.statusText
|
|
556
|
+
);
|
|
548
557
|
if (isPropertyAccessible(response, "type") && response.type === "error") {
|
|
549
558
|
this.errorWith(new TypeError("Network error"));
|
|
550
559
|
return;
|
|
@@ -586,15 +595,20 @@ var MockHttpSocket = class extends MockSocket {
|
|
|
586
595
|
serverResponse.write(value);
|
|
587
596
|
}
|
|
588
597
|
} catch (error) {
|
|
589
|
-
|
|
590
|
-
|
|
598
|
+
if (error instanceof Error) {
|
|
599
|
+
serverResponse.destroy();
|
|
600
|
+
this.destroy();
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
serverResponse.destroy();
|
|
604
|
+
throw error;
|
|
591
605
|
}
|
|
592
606
|
} else {
|
|
593
607
|
serverResponse.end();
|
|
594
608
|
}
|
|
595
609
|
if (!this.shouldKeepAlive) {
|
|
596
610
|
this.emit("readable");
|
|
597
|
-
(
|
|
611
|
+
(_c = this.responseStream) == null ? void 0 : _c.push(null);
|
|
598
612
|
this.push(null);
|
|
599
613
|
}
|
|
600
614
|
}
|
|
@@ -973,28 +987,25 @@ var _ClientRequestInterceptor = class extends Interceptor {
|
|
|
973
987
|
request,
|
|
974
988
|
socket
|
|
975
989
|
}) => {
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
request,
|
|
980
|
-
requestId,
|
|
981
|
-
controller,
|
|
982
|
-
emitter: this.emitter,
|
|
983
|
-
onResponse: (response) => {
|
|
984
|
-
socket.respondWith(response);
|
|
990
|
+
const controller = new RequestController(request, {
|
|
991
|
+
passthrough() {
|
|
992
|
+
socket.passthrough();
|
|
985
993
|
},
|
|
986
|
-
|
|
987
|
-
socket.respondWith(response);
|
|
994
|
+
async respondWith(response) {
|
|
995
|
+
await socket.respondWith(response);
|
|
988
996
|
},
|
|
989
|
-
|
|
990
|
-
if (
|
|
991
|
-
socket.errorWith(
|
|
997
|
+
errorWith(reason) {
|
|
998
|
+
if (reason instanceof Error) {
|
|
999
|
+
socket.errorWith(reason);
|
|
992
1000
|
}
|
|
993
1001
|
}
|
|
994
1002
|
});
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1003
|
+
await handleRequest({
|
|
1004
|
+
request,
|
|
1005
|
+
requestId: Reflect.get(request, kRequestId),
|
|
1006
|
+
controller,
|
|
1007
|
+
emitter: this.emitter
|
|
1008
|
+
});
|
|
998
1009
|
};
|
|
999
1010
|
this.onResponse = async ({
|
|
1000
1011
|
requestId,
|
|
@@ -1112,4 +1123,4 @@ ClientRequestInterceptor.symbol = Symbol("client-request-interceptor");
|
|
|
1112
1123
|
export {
|
|
1113
1124
|
ClientRequestInterceptor
|
|
1114
1125
|
};
|
|
1115
|
-
//# sourceMappingURL=chunk-
|
|
1126
|
+
//# sourceMappingURL=chunk-5UGIB6OX.mjs.map
|