-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathRemoteHttpInterceptor.js
More file actions
193 lines (175 loc) · 6.27 KB
/
RemoteHttpInterceptor.js
File metadata and controls
193 lines (175 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkR6JVCM7Xjs = require('./chunk-R6JVCM7X.js');
var _chunkF6CVST3Sjs = require('./chunk-F6CVST3S.js');
require('./chunk-4YBV77DG.js');
var _chunk4WG2AM2Tjs = require('./chunk-4WG2AM2T.js');
require('./chunk-LK6DILFK.js');
var _chunkYAIEISARjs = require('./chunk-YAIEISAR.js');
require('./chunk-PFGO5BSM.js');
require('./chunk-73NOP3T5.js');
var _chunkC2JSMMHYjs = require('./chunk-C2JSMMHY.js');
var _chunkA7U44ARPjs = require('./chunk-A7U44ARP.js');
require('./chunk-SMXZPJEA.js');
// src/RemoteHttpInterceptor.ts
var RemoteHttpInterceptor = class extends _chunkR6JVCM7Xjs.BatchInterceptor {
constructor() {
super({
name: "remote-interceptor",
interceptors: [
new (0, _chunkF6CVST3Sjs.ClientRequestInterceptor)(),
new (0, _chunk4WG2AM2Tjs.XMLHttpRequestInterceptor)(),
new (0, _chunkYAIEISARjs.FetchInterceptor)()
]
});
}
setup() {
super.setup();
let handleParentMessage;
this.on("request", async ({ request, requestId, controller }) => {
var _a;
const serializedRequest = JSON.stringify({
id: requestId,
method: request.method,
url: request.url,
headers: Array.from(request.headers.entries()),
credentials: request.credentials,
body: ["GET", "HEAD"].includes(request.method) ? null : await request.text()
});
this.logger.info(
"sent serialized request to the child:",
serializedRequest
);
(_a = process.send) == null ? void 0 : _a.call(process, `request:${serializedRequest}`);
const responsePromise = new Promise((resolve) => {
handleParentMessage = (message) => {
if (typeof message !== "string") {
return resolve();
}
if (message.startsWith(`response:${requestId}`)) {
const [, serializedResponse] = message.match(/^response:.+?:(.+)$/) || [];
if (!serializedResponse) {
return resolve();
}
const responseInit = JSON.parse(
serializedResponse
);
const mockedResponse = new (0, _chunkA7U44ARPjs.FetchResponse)(responseInit.body, {
url: request.url,
status: responseInit.status,
statusText: responseInit.statusText,
headers: responseInit.headers
});
controller.respondWith(mockedResponse);
return resolve();
}
};
});
this.logger.info(
'add "message" listener to the parent process',
handleParentMessage
);
process.addListener("message", handleParentMessage);
return responsePromise;
});
this.subscriptions.push(() => {
process.removeListener("message", handleParentMessage);
});
}
};
function requestReviver(key, value) {
switch (key) {
case "url":
return new URL(value);
case "headers":
return new Headers(value);
default:
return value;
}
}
var _RemoteHttpResolver = class extends _chunkA7U44ARPjs.Interceptor {
constructor(options) {
super(_RemoteHttpResolver.symbol);
this.process = options.process;
}
setup() {
const logger = this.logger.extend("setup");
const handleChildMessage = async (message) => {
logger.info("received message from child!", message);
if (typeof message !== "string" || !message.startsWith("request:")) {
logger.info("unknown message, ignoring...");
return;
}
const [, serializedRequest] = message.match(/^request:(.+)$/) || [];
if (!serializedRequest) {
return;
}
const requestJson = JSON.parse(
serializedRequest,
requestReviver
);
logger.info("parsed intercepted request", requestJson);
const request = new Request(requestJson.url, {
method: requestJson.method,
headers: new Headers(requestJson.headers),
credentials: requestJson.credentials,
body: requestJson.body
});
const controller = new (0, _chunkC2JSMMHYjs.RequestController)(request);
await _chunkC2JSMMHYjs.handleRequest.call(void 0, {
request,
requestId: requestJson.id,
controller,
emitter: this.emitter,
onResponse: async (response) => {
this.logger.info("received mocked response!", { response });
const responseClone = response.clone();
const responseText = await responseClone.text();
const serializedResponse = JSON.stringify({
status: response.status,
statusText: response.statusText,
headers: Array.from(response.headers.entries()),
body: responseText
});
this.process.send(
`response:${requestJson.id}:${serializedResponse}`,
(error) => {
if (error) {
return;
}
this.emitter.emit("response", {
request,
requestId: requestJson.id,
response: responseClone,
isMockedResponse: true
});
}
);
logger.info(
"sent serialized mocked response to the parent:",
serializedResponse
);
},
onRequestError: (response) => {
this.logger.info("received a network error!", { response });
throw new Error("Not implemented");
},
onError: (error) => {
this.logger.info("request has errored!", { error });
throw new Error("Not implemented");
}
});
};
this.subscriptions.push(() => {
this.process.removeListener("message", handleChildMessage);
logger.info('removed the "message" listener from the child process!');
});
logger.info('adding a "message" listener to the child process');
this.process.addListener("message", handleChildMessage);
this.process.once("error", () => this.dispose());
this.process.once("exit", () => this.dispose());
}
};
var RemoteHttpResolver = _RemoteHttpResolver;
RemoteHttpResolver.symbol = Symbol("remote-resolver");
exports.RemoteHttpInterceptor = RemoteHttpInterceptor; exports.RemoteHttpResolver = RemoteHttpResolver; exports.requestReviver = requestReviver;
//# sourceMappingURL=RemoteHttpInterceptor.js.map