@zimic/interceptor 0.16.0-canary.0 → 0.16.0-canary.10
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/README.md +4 -5
- package/dist/{chunk-6TSSHQW5.mjs → chunk-NTRC2S4I.mjs} +253 -312
- package/dist/chunk-NTRC2S4I.mjs.map +1 -0
- package/dist/{chunk-R2ROSKU4.js → chunk-O6ZIPCUJ.js} +254 -313
- package/dist/chunk-O6ZIPCUJ.js.map +1 -0
- package/dist/cli.js +9 -9
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5 -5
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +156 -176
- package/dist/http.js +265 -324
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +263 -323
- package/dist/http.mjs.map +1 -1
- package/dist/server.d.ts +20 -46
- package/dist/server.js +7 -7
- package/dist/server.mjs +1 -1
- package/package.json +4 -4
- package/src/cli/browser/init.ts +2 -2
- package/src/cli/server/start.ts +2 -2
- package/src/http/index.ts +6 -13
- package/src/http/interceptor/HttpInterceptorClient.ts +30 -15
- package/src/http/interceptor/LocalHttpInterceptor.ts +8 -8
- package/src/http/interceptor/RemoteHttpInterceptor.ts +8 -8
- package/src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts +22 -0
- package/src/http/interceptor/factory.ts +1 -1
- package/src/http/interceptor/types/options.ts +4 -11
- package/src/http/interceptor/types/public.ts +44 -12
- package/src/http/interceptor/types/schema.ts +2 -2
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +6 -31
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +15 -5
- package/src/http/requestHandler/errors/DisabledRequestSavingError.ts +2 -2
- package/src/http/requestHandler/errors/TimesCheckError.ts +15 -14
- package/src/http/requestHandler/types/public.ts +16 -8
- package/src/server/index.ts +1 -11
- package/src/utils/console.ts +2 -2
- package/dist/chunk-6TSSHQW5.mjs.map +0 -1
- package/dist/chunk-R2ROSKU4.js.map +0 -1
- package/src/http/interceptorWorker/HttpInterceptorWorkerStore.ts +0 -34
- package/src/http/namespace/HttpInterceptorNamespace.ts +0 -81
- package/src/server/namespace/InterceptorServerNamespace.ts +0 -21
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
</h1>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
TypeScript-first HTTP intercepting and mocking
|
|
10
|
+
Next-gen, TypeScript-first HTTP intercepting and mocking
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
@@ -141,15 +141,14 @@ Check our [getting started guide](https://github.com/zimicjs/zimic/wiki/getting
|
|
|
141
141
|
generate types for your HTTP schema.
|
|
142
142
|
|
|
143
143
|
2. Create your
|
|
144
|
-
[interceptor](https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#
|
|
144
|
+
[interceptor](https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#createhttpinterceptoroptions):
|
|
145
145
|
|
|
146
146
|
```ts
|
|
147
|
-
import {
|
|
147
|
+
import { createHttpInterceptor } from '@zimic/interceptor/http';
|
|
148
148
|
|
|
149
|
-
const interceptor =
|
|
149
|
+
const interceptor = createHttpInterceptor<Schema>({
|
|
150
150
|
type: 'local',
|
|
151
151
|
baseURL: 'http://localhost:3000',
|
|
152
|
-
saveRequests: true, // Allow access to `handler.requests`
|
|
153
152
|
});
|
|
154
153
|
```
|
|
155
154
|
|
|
@@ -1,10 +1,251 @@
|
|
|
1
1
|
import { __name } from './chunk-CGILA3WO.mjs';
|
|
2
|
+
import { HTTP_METHODS, HttpHeaders, HttpSearchParams, HttpFormData, HTTP_METHODS_WITH_RESPONSE_BODY } from '@zimic/http';
|
|
2
3
|
import { normalizeNodeRequest, sendNodeResponse } from '@whatwg-node/server';
|
|
3
4
|
import { createServer } from 'http';
|
|
4
|
-
import
|
|
5
|
-
import chalk2 from 'chalk';
|
|
5
|
+
import color2 from 'picocolors';
|
|
6
6
|
import ClientSocket from 'isomorphic-ws';
|
|
7
7
|
|
|
8
|
+
// src/server/errors/RunningInterceptorServerError.ts
|
|
9
|
+
var RunningInterceptorServerError = class extends Error {
|
|
10
|
+
static {
|
|
11
|
+
__name(this, "RunningInterceptorServerError");
|
|
12
|
+
}
|
|
13
|
+
constructor(additionalMessage) {
|
|
14
|
+
super(`The interceptor server is running.${additionalMessage}`);
|
|
15
|
+
this.name = "RunningInterceptorServerError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var RunningInterceptorServerError_default = RunningInterceptorServerError;
|
|
19
|
+
|
|
20
|
+
// src/server/errors/NotRunningInterceptorServerError.ts
|
|
21
|
+
var NotRunningInterceptorServerError = class extends Error {
|
|
22
|
+
static {
|
|
23
|
+
__name(this, "NotRunningInterceptorServerError");
|
|
24
|
+
}
|
|
25
|
+
constructor() {
|
|
26
|
+
super("The interceptor server is not running. Did you forget to start it?");
|
|
27
|
+
this.name = "NotRunningInterceptorServerError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var NotRunningInterceptorServerError_default = NotRunningInterceptorServerError;
|
|
31
|
+
var HttpServerTimeoutError = class extends Error {
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "HttpServerTimeoutError");
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var HttpServerStartTimeoutError = class extends HttpServerTimeoutError {
|
|
37
|
+
static {
|
|
38
|
+
__name(this, "HttpServerStartTimeoutError");
|
|
39
|
+
}
|
|
40
|
+
constructor(reachedTimeout) {
|
|
41
|
+
super(`HTTP server start timed out after ${reachedTimeout}ms.`);
|
|
42
|
+
this.name = "HttpServerStartTimeout";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var HttpServerStopTimeoutError = class extends HttpServerTimeoutError {
|
|
46
|
+
static {
|
|
47
|
+
__name(this, "HttpServerStopTimeoutError");
|
|
48
|
+
}
|
|
49
|
+
constructor(reachedTimeout) {
|
|
50
|
+
super(`HTTP server stop timed out after ${reachedTimeout}ms.`);
|
|
51
|
+
this.name = "HttpServerStopTimeout";
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var DEFAULT_HTTP_SERVER_LIFECYCLE_TIMEOUT = 60 * 1e3;
|
|
55
|
+
async function startHttpServer(server, options = {}) {
|
|
56
|
+
const { hostname, port, timeout: timeoutDuration = DEFAULT_HTTP_SERVER_LIFECYCLE_TIMEOUT } = options;
|
|
57
|
+
await new Promise((resolve, reject) => {
|
|
58
|
+
function handleStartError(error) {
|
|
59
|
+
server.off("listening", handleStartSuccess);
|
|
60
|
+
reject(error);
|
|
61
|
+
}
|
|
62
|
+
__name(handleStartError, "handleStartError");
|
|
63
|
+
const startTimeout = setTimeout(() => {
|
|
64
|
+
const timeoutError = new HttpServerStartTimeoutError(timeoutDuration);
|
|
65
|
+
handleStartError(timeoutError);
|
|
66
|
+
}, timeoutDuration);
|
|
67
|
+
function handleStartSuccess() {
|
|
68
|
+
server.off("error", handleStartError);
|
|
69
|
+
clearTimeout(startTimeout);
|
|
70
|
+
resolve();
|
|
71
|
+
}
|
|
72
|
+
__name(handleStartSuccess, "handleStartSuccess");
|
|
73
|
+
server.once("error", handleStartError);
|
|
74
|
+
server.listen(port, hostname, handleStartSuccess);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
__name(startHttpServer, "startHttpServer");
|
|
78
|
+
async function stopHttpServer(server, options = {}) {
|
|
79
|
+
const { timeout: timeoutDuration = DEFAULT_HTTP_SERVER_LIFECYCLE_TIMEOUT } = options;
|
|
80
|
+
if (!server.listening) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
await new Promise((resolve, reject) => {
|
|
84
|
+
const stopTimeout = setTimeout(() => {
|
|
85
|
+
const timeoutError = new HttpServerStopTimeoutError(timeoutDuration);
|
|
86
|
+
reject(timeoutError);
|
|
87
|
+
}, timeoutDuration);
|
|
88
|
+
server.close((error) => {
|
|
89
|
+
clearTimeout(stopTimeout);
|
|
90
|
+
if (error) {
|
|
91
|
+
reject(error);
|
|
92
|
+
} else {
|
|
93
|
+
resolve();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
server.closeAllConnections();
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
__name(stopHttpServer, "stopHttpServer");
|
|
100
|
+
function getHttpServerPort(server) {
|
|
101
|
+
const address = server.address();
|
|
102
|
+
if (typeof address === "string") {
|
|
103
|
+
return void 0;
|
|
104
|
+
} else {
|
|
105
|
+
return address?.port;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
__name(getHttpServerPort, "getHttpServerPort");
|
|
109
|
+
function methodCanHaveResponseBody(method) {
|
|
110
|
+
const methodsToCompare = HTTP_METHODS_WITH_RESPONSE_BODY;
|
|
111
|
+
return methodsToCompare.includes(method);
|
|
112
|
+
}
|
|
113
|
+
__name(methodCanHaveResponseBody, "methodCanHaveResponseBody");
|
|
114
|
+
|
|
115
|
+
// src/utils/webSocket.ts
|
|
116
|
+
var WebSocketTimeoutError = class extends Error {
|
|
117
|
+
static {
|
|
118
|
+
__name(this, "WebSocketTimeoutError");
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
var WebSocketOpenTimeoutError = class extends WebSocketTimeoutError {
|
|
122
|
+
static {
|
|
123
|
+
__name(this, "WebSocketOpenTimeoutError");
|
|
124
|
+
}
|
|
125
|
+
constructor(reachedTimeout) {
|
|
126
|
+
super(`Web socket open timed out after ${reachedTimeout}ms.`);
|
|
127
|
+
this.name = "WebSocketOpenTimeout";
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
var WebSocketMessageTimeoutError = class extends WebSocketTimeoutError {
|
|
131
|
+
static {
|
|
132
|
+
__name(this, "WebSocketMessageTimeoutError");
|
|
133
|
+
}
|
|
134
|
+
constructor(reachedTimeout) {
|
|
135
|
+
super(`Web socket message timed out after ${reachedTimeout}ms.`);
|
|
136
|
+
this.name = "WebSocketMessageTimeout";
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
var WebSocketMessageAbortError = class extends WebSocketTimeoutError {
|
|
140
|
+
static {
|
|
141
|
+
__name(this, "WebSocketMessageAbortError");
|
|
142
|
+
}
|
|
143
|
+
constructor() {
|
|
144
|
+
super("Web socket message was aborted.");
|
|
145
|
+
this.name = "WebSocketMessageAbortError";
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var WebSocketCloseTimeoutError = class extends WebSocketTimeoutError {
|
|
149
|
+
static {
|
|
150
|
+
__name(this, "WebSocketCloseTimeoutError");
|
|
151
|
+
}
|
|
152
|
+
constructor(reachedTimeout) {
|
|
153
|
+
super(`Web socket close timed out after ${reachedTimeout}ms.`);
|
|
154
|
+
this.name = "WebSocketCloseTimeout";
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
var DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT = 60 * 1e3;
|
|
158
|
+
var DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT = 3 * 60 * 1e3;
|
|
159
|
+
async function waitForOpenClientSocket(socket, options = {}) {
|
|
160
|
+
const { timeout: timeoutDuration = DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT } = options;
|
|
161
|
+
const isAlreadyOpen = socket.readyState === socket.OPEN;
|
|
162
|
+
if (isAlreadyOpen) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
await new Promise((resolve, reject) => {
|
|
166
|
+
function handleOpenError(error) {
|
|
167
|
+
socket.removeEventListener("open", handleOpenSuccess);
|
|
168
|
+
reject(error);
|
|
169
|
+
}
|
|
170
|
+
__name(handleOpenError, "handleOpenError");
|
|
171
|
+
const openTimeout = setTimeout(() => {
|
|
172
|
+
const timeoutError = new WebSocketOpenTimeoutError(timeoutDuration);
|
|
173
|
+
handleOpenError(timeoutError);
|
|
174
|
+
}, timeoutDuration);
|
|
175
|
+
function handleOpenSuccess() {
|
|
176
|
+
socket.removeEventListener("error", handleOpenError);
|
|
177
|
+
clearTimeout(openTimeout);
|
|
178
|
+
resolve();
|
|
179
|
+
}
|
|
180
|
+
__name(handleOpenSuccess, "handleOpenSuccess");
|
|
181
|
+
socket.addEventListener("open", handleOpenSuccess);
|
|
182
|
+
socket.addEventListener("error", handleOpenError);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
__name(waitForOpenClientSocket, "waitForOpenClientSocket");
|
|
186
|
+
async function closeClientSocket(socket, options = {}) {
|
|
187
|
+
const { timeout: timeoutDuration = DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT } = options;
|
|
188
|
+
const isAlreadyClosed = socket.readyState === socket.CLOSED;
|
|
189
|
+
if (isAlreadyClosed) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
await new Promise((resolve, reject) => {
|
|
193
|
+
function handleCloseError(error) {
|
|
194
|
+
socket.removeEventListener("close", handleCloseSuccess);
|
|
195
|
+
reject(error);
|
|
196
|
+
}
|
|
197
|
+
__name(handleCloseError, "handleCloseError");
|
|
198
|
+
const closeTimeout = setTimeout(() => {
|
|
199
|
+
const timeoutError = new WebSocketCloseTimeoutError(timeoutDuration);
|
|
200
|
+
handleCloseError(timeoutError);
|
|
201
|
+
}, timeoutDuration);
|
|
202
|
+
function handleCloseSuccess() {
|
|
203
|
+
socket.removeEventListener("error", handleCloseError);
|
|
204
|
+
clearTimeout(closeTimeout);
|
|
205
|
+
resolve();
|
|
206
|
+
}
|
|
207
|
+
__name(handleCloseSuccess, "handleCloseSuccess");
|
|
208
|
+
socket.addEventListener("error", handleCloseError);
|
|
209
|
+
socket.addEventListener("close", handleCloseSuccess);
|
|
210
|
+
socket.close();
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
__name(closeClientSocket, "closeClientSocket");
|
|
214
|
+
async function closeServerSocket(socket, options = {}) {
|
|
215
|
+
const { timeout: timeoutDuration = DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT } = options;
|
|
216
|
+
await new Promise((resolve, reject) => {
|
|
217
|
+
const closeTimeout = setTimeout(() => {
|
|
218
|
+
const timeoutError = new WebSocketCloseTimeoutError(timeoutDuration);
|
|
219
|
+
reject(timeoutError);
|
|
220
|
+
}, timeoutDuration);
|
|
221
|
+
for (const client of socket.clients) {
|
|
222
|
+
client.terminate();
|
|
223
|
+
}
|
|
224
|
+
socket.close((error) => {
|
|
225
|
+
clearTimeout(closeTimeout);
|
|
226
|
+
if (error) {
|
|
227
|
+
reject(error);
|
|
228
|
+
} else {
|
|
229
|
+
resolve();
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
__name(closeServerSocket, "closeServerSocket");
|
|
235
|
+
|
|
236
|
+
// src/server/constants.ts
|
|
237
|
+
var ALLOWED_ACCESS_CONTROL_HTTP_METHODS = HTTP_METHODS.join(",");
|
|
238
|
+
var DEFAULT_ACCESS_CONTROL_HEADERS = Object.freeze({
|
|
239
|
+
"access-control-allow-origin": "*",
|
|
240
|
+
"access-control-allow-methods": ALLOWED_ACCESS_CONTROL_HTTP_METHODS,
|
|
241
|
+
"access-control-allow-headers": "*",
|
|
242
|
+
"access-control-expose-headers": "*",
|
|
243
|
+
"access-control-max-age": ""
|
|
244
|
+
});
|
|
245
|
+
var DEFAULT_PREFLIGHT_STATUS_CODE = 204;
|
|
246
|
+
var DEFAULT_HOSTNAME = "localhost";
|
|
247
|
+
var DEFAULT_LOG_UNHANDLED_REQUESTS = true;
|
|
248
|
+
|
|
8
249
|
// ../zimic-utils/dist/chunk-PAWJFY3S.mjs
|
|
9
250
|
var __defProp = Object.defineProperty;
|
|
10
251
|
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp(target, "name", { value, configurable: true }), "__name");
|
|
@@ -104,92 +345,9 @@ __name(formatValueToLog, "formatValueToLog");
|
|
|
104
345
|
function logWithPrefix(messageOrMessages, options = {}) {
|
|
105
346
|
const { method = "log" } = options;
|
|
106
347
|
const messages = Array.isArray(messageOrMessages) ? messageOrMessages : [messageOrMessages];
|
|
107
|
-
console[method](
|
|
348
|
+
console[method](color2.cyan("[@zimic/interceptor]"), ...messages);
|
|
108
349
|
}
|
|
109
350
|
__name(logWithPrefix, "logWithPrefix");
|
|
110
|
-
var HttpServerTimeoutError = class extends Error {
|
|
111
|
-
static {
|
|
112
|
-
__name(this, "HttpServerTimeoutError");
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
var HttpServerStartTimeoutError = class extends HttpServerTimeoutError {
|
|
116
|
-
static {
|
|
117
|
-
__name(this, "HttpServerStartTimeoutError");
|
|
118
|
-
}
|
|
119
|
-
constructor(reachedTimeout) {
|
|
120
|
-
super(`HTTP server start timed out after ${reachedTimeout}ms.`);
|
|
121
|
-
this.name = "HttpServerStartTimeout";
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
var HttpServerStopTimeoutError = class extends HttpServerTimeoutError {
|
|
125
|
-
static {
|
|
126
|
-
__name(this, "HttpServerStopTimeoutError");
|
|
127
|
-
}
|
|
128
|
-
constructor(reachedTimeout) {
|
|
129
|
-
super(`HTTP server stop timed out after ${reachedTimeout}ms.`);
|
|
130
|
-
this.name = "HttpServerStopTimeout";
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
var DEFAULT_HTTP_SERVER_LIFECYCLE_TIMEOUT = 60 * 1e3;
|
|
134
|
-
async function startHttpServer(server, options = {}) {
|
|
135
|
-
const { hostname, port, timeout: timeoutDuration = DEFAULT_HTTP_SERVER_LIFECYCLE_TIMEOUT } = options;
|
|
136
|
-
await new Promise((resolve, reject) => {
|
|
137
|
-
function handleStartError(error) {
|
|
138
|
-
server.off("listening", handleStartSuccess);
|
|
139
|
-
reject(error);
|
|
140
|
-
}
|
|
141
|
-
__name(handleStartError, "handleStartError");
|
|
142
|
-
const startTimeout = setTimeout(() => {
|
|
143
|
-
const timeoutError = new HttpServerStartTimeoutError(timeoutDuration);
|
|
144
|
-
handleStartError(timeoutError);
|
|
145
|
-
}, timeoutDuration);
|
|
146
|
-
function handleStartSuccess() {
|
|
147
|
-
server.off("error", handleStartError);
|
|
148
|
-
clearTimeout(startTimeout);
|
|
149
|
-
resolve();
|
|
150
|
-
}
|
|
151
|
-
__name(handleStartSuccess, "handleStartSuccess");
|
|
152
|
-
server.once("error", handleStartError);
|
|
153
|
-
server.listen(port, hostname, handleStartSuccess);
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
__name(startHttpServer, "startHttpServer");
|
|
157
|
-
async function stopHttpServer(server, options = {}) {
|
|
158
|
-
const { timeout: timeoutDuration = DEFAULT_HTTP_SERVER_LIFECYCLE_TIMEOUT } = options;
|
|
159
|
-
if (!server.listening) {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
await new Promise((resolve, reject) => {
|
|
163
|
-
const stopTimeout = setTimeout(() => {
|
|
164
|
-
const timeoutError = new HttpServerStopTimeoutError(timeoutDuration);
|
|
165
|
-
reject(timeoutError);
|
|
166
|
-
}, timeoutDuration);
|
|
167
|
-
server.close((error) => {
|
|
168
|
-
clearTimeout(stopTimeout);
|
|
169
|
-
if (error) {
|
|
170
|
-
reject(error);
|
|
171
|
-
} else {
|
|
172
|
-
resolve();
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
server.closeAllConnections();
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
__name(stopHttpServer, "stopHttpServer");
|
|
179
|
-
function getHttpServerPort(server) {
|
|
180
|
-
const address = server.address();
|
|
181
|
-
if (typeof address === "string") {
|
|
182
|
-
return void 0;
|
|
183
|
-
} else {
|
|
184
|
-
return address?.port;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
__name(getHttpServerPort, "getHttpServerPort");
|
|
188
|
-
function methodCanHaveResponseBody(method) {
|
|
189
|
-
const methodsToCompare = HTTP_METHODS_WITH_RESPONSE_BODY;
|
|
190
|
-
return methodsToCompare.includes(method);
|
|
191
|
-
}
|
|
192
|
-
__name(methodCanHaveResponseBody, "methodCanHaveResponseBody");
|
|
193
351
|
|
|
194
352
|
// src/http/requestHandler/types/requests.ts
|
|
195
353
|
var HTTP_INTERCEPTOR_REQUEST_HIDDEN_PROPERTIES = Object.freeze(
|
|
@@ -245,29 +403,6 @@ var InvalidJSONError = class extends SyntaxError {
|
|
|
245
403
|
};
|
|
246
404
|
var InvalidJSONError_default = InvalidJSONError;
|
|
247
405
|
|
|
248
|
-
// src/http/interceptorWorker/HttpInterceptorWorkerStore.ts
|
|
249
|
-
var HttpInterceptorWorkerStore = class _HttpInterceptorWorkerStore {
|
|
250
|
-
static {
|
|
251
|
-
__name(this, "HttpInterceptorWorkerStore");
|
|
252
|
-
}
|
|
253
|
-
static _defaultOnUnhandledRequest = {
|
|
254
|
-
local: { ...DEFAULT_UNHANDLED_REQUEST_STRATEGY.local },
|
|
255
|
-
remote: { ...DEFAULT_UNHANDLED_REQUEST_STRATEGY.remote }
|
|
256
|
-
};
|
|
257
|
-
class = _HttpInterceptorWorkerStore;
|
|
258
|
-
defaultOnUnhandledRequest(interceptorType) {
|
|
259
|
-
return this.class._defaultOnUnhandledRequest[interceptorType];
|
|
260
|
-
}
|
|
261
|
-
setDefaultOnUnhandledRequest(interceptorType, strategy) {
|
|
262
|
-
if (interceptorType === "local") {
|
|
263
|
-
this.class._defaultOnUnhandledRequest[interceptorType] = strategy;
|
|
264
|
-
} else {
|
|
265
|
-
this.class._defaultOnUnhandledRequest[interceptorType] = strategy;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
var HttpInterceptorWorkerStore_default = HttpInterceptorWorkerStore;
|
|
270
|
-
|
|
271
406
|
// src/http/interceptorWorker/HttpInterceptorWorker.ts
|
|
272
407
|
var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
273
408
|
static {
|
|
@@ -277,7 +412,6 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
|
277
412
|
isRunning = false;
|
|
278
413
|
startingPromise;
|
|
279
414
|
stoppingPromise;
|
|
280
|
-
store = new HttpInterceptorWorkerStore_default();
|
|
281
415
|
runningInterceptors = [];
|
|
282
416
|
async sharedStart(internalStart) {
|
|
283
417
|
if (this.isRunning) {
|
|
@@ -336,24 +470,18 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
|
336
470
|
);
|
|
337
471
|
}
|
|
338
472
|
async getUnhandledRequestStrategyCandidates(request, interceptorType) {
|
|
339
|
-
const globalDefaultStrategy =
|
|
473
|
+
const globalDefaultStrategy = DEFAULT_UNHANDLED_REQUEST_STRATEGY[interceptorType];
|
|
340
474
|
try {
|
|
341
475
|
const interceptor = this.findInterceptorByRequestBaseURL(request);
|
|
342
476
|
if (!interceptor) {
|
|
343
477
|
return [];
|
|
344
478
|
}
|
|
345
479
|
const requestClone = request.clone();
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
this.getInterceptorUnhandledRequestStrategy(requestClone, interceptor)
|
|
349
|
-
]);
|
|
350
|
-
const candidatesOrPromises = [interceptorStrategy, defaultStrategy, globalDefaultStrategy];
|
|
351
|
-
const candidateStrategies = await Promise.all(candidatesOrPromises.filter(isDefined_default));
|
|
352
|
-
return candidateStrategies;
|
|
480
|
+
const interceptorStrategy = await this.getInterceptorUnhandledRequestStrategy(requestClone, interceptor);
|
|
481
|
+
return [interceptorStrategy, globalDefaultStrategy].filter(isDefined_default);
|
|
353
482
|
} catch (error) {
|
|
354
483
|
console.error(error);
|
|
355
|
-
|
|
356
|
-
return candidateStrategies;
|
|
484
|
+
return [globalDefaultStrategy];
|
|
357
485
|
}
|
|
358
486
|
}
|
|
359
487
|
registerRunningInterceptor(interceptor) {
|
|
@@ -368,17 +496,6 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
|
368
496
|
});
|
|
369
497
|
return interceptor;
|
|
370
498
|
}
|
|
371
|
-
getGlobalDefaultUnhandledRequestStrategy(interceptorType) {
|
|
372
|
-
return DEFAULT_UNHANDLED_REQUEST_STRATEGY[interceptorType];
|
|
373
|
-
}
|
|
374
|
-
async getDefaultUnhandledRequestStrategy(request, interceptorType) {
|
|
375
|
-
const defaultStrategyOrFactory = this.store.defaultOnUnhandledRequest(interceptorType);
|
|
376
|
-
if (typeof defaultStrategyOrFactory === "function") {
|
|
377
|
-
const parsedRequest = await _HttpInterceptorWorker.parseRawUnhandledRequest(request);
|
|
378
|
-
return defaultStrategyOrFactory(parsedRequest);
|
|
379
|
-
}
|
|
380
|
-
return defaultStrategyOrFactory;
|
|
381
|
-
}
|
|
382
499
|
async getInterceptorUnhandledRequestStrategy(request, interceptor) {
|
|
383
500
|
if (typeof interceptor.onUnhandledRequest === "function") {
|
|
384
501
|
const parsedRequest = await _HttpInterceptorWorker.parseRawUnhandledRequest(request);
|
|
@@ -592,7 +709,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
|
|
|
592
709
|
]);
|
|
593
710
|
logWithPrefix(
|
|
594
711
|
[
|
|
595
|
-
`${action === "bypass" ? "Warning:" : "Error:"} Request was not handled and was ${action === "bypass" ?
|
|
712
|
+
`${action === "bypass" ? "Warning:" : "Error:"} Request was not handled and was ${action === "bypass" ? color2.yellow("bypassed") : color2.red("rejected")}.
|
|
596
713
|
|
|
597
714
|
`,
|
|
598
715
|
`${request.method} ${request.url}`,
|
|
@@ -667,127 +784,6 @@ function deserializeResponse(serializedResponse) {
|
|
|
667
784
|
}
|
|
668
785
|
__name(deserializeResponse, "deserializeResponse");
|
|
669
786
|
|
|
670
|
-
// src/utils/webSocket.ts
|
|
671
|
-
var WebSocketTimeoutError = class extends Error {
|
|
672
|
-
static {
|
|
673
|
-
__name(this, "WebSocketTimeoutError");
|
|
674
|
-
}
|
|
675
|
-
};
|
|
676
|
-
var WebSocketOpenTimeoutError = class extends WebSocketTimeoutError {
|
|
677
|
-
static {
|
|
678
|
-
__name(this, "WebSocketOpenTimeoutError");
|
|
679
|
-
}
|
|
680
|
-
constructor(reachedTimeout) {
|
|
681
|
-
super(`Web socket open timed out after ${reachedTimeout}ms.`);
|
|
682
|
-
this.name = "WebSocketOpenTimeout";
|
|
683
|
-
}
|
|
684
|
-
};
|
|
685
|
-
var WebSocketMessageTimeoutError = class extends WebSocketTimeoutError {
|
|
686
|
-
static {
|
|
687
|
-
__name(this, "WebSocketMessageTimeoutError");
|
|
688
|
-
}
|
|
689
|
-
constructor(reachedTimeout) {
|
|
690
|
-
super(`Web socket message timed out after ${reachedTimeout}ms.`);
|
|
691
|
-
this.name = "WebSocketMessageTimeout";
|
|
692
|
-
}
|
|
693
|
-
};
|
|
694
|
-
var WebSocketMessageAbortError = class extends WebSocketTimeoutError {
|
|
695
|
-
static {
|
|
696
|
-
__name(this, "WebSocketMessageAbortError");
|
|
697
|
-
}
|
|
698
|
-
constructor() {
|
|
699
|
-
super("Web socket message was aborted.");
|
|
700
|
-
this.name = "WebSocketMessageAbortError";
|
|
701
|
-
}
|
|
702
|
-
};
|
|
703
|
-
var WebSocketCloseTimeoutError = class extends WebSocketTimeoutError {
|
|
704
|
-
static {
|
|
705
|
-
__name(this, "WebSocketCloseTimeoutError");
|
|
706
|
-
}
|
|
707
|
-
constructor(reachedTimeout) {
|
|
708
|
-
super(`Web socket close timed out after ${reachedTimeout}ms.`);
|
|
709
|
-
this.name = "WebSocketCloseTimeout";
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
var DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT = 60 * 1e3;
|
|
713
|
-
var DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT = 3 * 60 * 1e3;
|
|
714
|
-
async function waitForOpenClientSocket(socket, options = {}) {
|
|
715
|
-
const { timeout: timeoutDuration = DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT } = options;
|
|
716
|
-
const isAlreadyOpen = socket.readyState === socket.OPEN;
|
|
717
|
-
if (isAlreadyOpen) {
|
|
718
|
-
return;
|
|
719
|
-
}
|
|
720
|
-
await new Promise((resolve, reject) => {
|
|
721
|
-
function handleOpenError(error) {
|
|
722
|
-
socket.removeEventListener("open", handleOpenSuccess);
|
|
723
|
-
reject(error);
|
|
724
|
-
}
|
|
725
|
-
__name(handleOpenError, "handleOpenError");
|
|
726
|
-
const openTimeout = setTimeout(() => {
|
|
727
|
-
const timeoutError = new WebSocketOpenTimeoutError(timeoutDuration);
|
|
728
|
-
handleOpenError(timeoutError);
|
|
729
|
-
}, timeoutDuration);
|
|
730
|
-
function handleOpenSuccess() {
|
|
731
|
-
socket.removeEventListener("error", handleOpenError);
|
|
732
|
-
clearTimeout(openTimeout);
|
|
733
|
-
resolve();
|
|
734
|
-
}
|
|
735
|
-
__name(handleOpenSuccess, "handleOpenSuccess");
|
|
736
|
-
socket.addEventListener("open", handleOpenSuccess);
|
|
737
|
-
socket.addEventListener("error", handleOpenError);
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
__name(waitForOpenClientSocket, "waitForOpenClientSocket");
|
|
741
|
-
async function closeClientSocket(socket, options = {}) {
|
|
742
|
-
const { timeout: timeoutDuration = DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT } = options;
|
|
743
|
-
const isAlreadyClosed = socket.readyState === socket.CLOSED;
|
|
744
|
-
if (isAlreadyClosed) {
|
|
745
|
-
return;
|
|
746
|
-
}
|
|
747
|
-
await new Promise((resolve, reject) => {
|
|
748
|
-
function handleCloseError(error) {
|
|
749
|
-
socket.removeEventListener("close", handleCloseSuccess);
|
|
750
|
-
reject(error);
|
|
751
|
-
}
|
|
752
|
-
__name(handleCloseError, "handleCloseError");
|
|
753
|
-
const closeTimeout = setTimeout(() => {
|
|
754
|
-
const timeoutError = new WebSocketCloseTimeoutError(timeoutDuration);
|
|
755
|
-
handleCloseError(timeoutError);
|
|
756
|
-
}, timeoutDuration);
|
|
757
|
-
function handleCloseSuccess() {
|
|
758
|
-
socket.removeEventListener("error", handleCloseError);
|
|
759
|
-
clearTimeout(closeTimeout);
|
|
760
|
-
resolve();
|
|
761
|
-
}
|
|
762
|
-
__name(handleCloseSuccess, "handleCloseSuccess");
|
|
763
|
-
socket.addEventListener("error", handleCloseError);
|
|
764
|
-
socket.addEventListener("close", handleCloseSuccess);
|
|
765
|
-
socket.close();
|
|
766
|
-
});
|
|
767
|
-
}
|
|
768
|
-
__name(closeClientSocket, "closeClientSocket");
|
|
769
|
-
async function closeServerSocket(socket, options = {}) {
|
|
770
|
-
const { timeout: timeoutDuration = DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT } = options;
|
|
771
|
-
await new Promise((resolve, reject) => {
|
|
772
|
-
const closeTimeout = setTimeout(() => {
|
|
773
|
-
const timeoutError = new WebSocketCloseTimeoutError(timeoutDuration);
|
|
774
|
-
reject(timeoutError);
|
|
775
|
-
}, timeoutDuration);
|
|
776
|
-
for (const client of socket.clients) {
|
|
777
|
-
client.terminate();
|
|
778
|
-
}
|
|
779
|
-
socket.close((error) => {
|
|
780
|
-
clearTimeout(closeTimeout);
|
|
781
|
-
if (error) {
|
|
782
|
-
reject(error);
|
|
783
|
-
} else {
|
|
784
|
-
resolve();
|
|
785
|
-
}
|
|
786
|
-
});
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
__name(closeServerSocket, "closeServerSocket");
|
|
790
|
-
|
|
791
787
|
// src/utils/crypto.ts
|
|
792
788
|
var importCrypto = createCachedDynamicImport_default(async () => {
|
|
793
789
|
const globalCrypto = globalThis.crypto;
|
|
@@ -1106,41 +1102,6 @@ var WebSocketServer = class extends WebSocketHandler_default {
|
|
|
1106
1102
|
}
|
|
1107
1103
|
};
|
|
1108
1104
|
var WebSocketServer_default = WebSocketServer;
|
|
1109
|
-
var ALLOWED_ACCESS_CONTROL_HTTP_METHODS = HTTP_METHODS.join(",");
|
|
1110
|
-
var DEFAULT_ACCESS_CONTROL_HEADERS = Object.freeze({
|
|
1111
|
-
"access-control-allow-origin": "*",
|
|
1112
|
-
"access-control-allow-methods": ALLOWED_ACCESS_CONTROL_HTTP_METHODS,
|
|
1113
|
-
"access-control-allow-headers": "*",
|
|
1114
|
-
"access-control-expose-headers": "*",
|
|
1115
|
-
"access-control-max-age": ""
|
|
1116
|
-
});
|
|
1117
|
-
var DEFAULT_PREFLIGHT_STATUS_CODE = 204;
|
|
1118
|
-
var DEFAULT_HOSTNAME = "localhost";
|
|
1119
|
-
var DEFAULT_LOG_UNHANDLED_REQUESTS = true;
|
|
1120
|
-
|
|
1121
|
-
// src/server/errors/NotRunningInterceptorServerError.ts
|
|
1122
|
-
var NotRunningInterceptorServerError = class extends Error {
|
|
1123
|
-
static {
|
|
1124
|
-
__name(this, "NotRunningInterceptorServerError");
|
|
1125
|
-
}
|
|
1126
|
-
constructor() {
|
|
1127
|
-
super("The interceptor server is not running. Did you forget to start it?");
|
|
1128
|
-
this.name = "NotRunningInterceptorServerError";
|
|
1129
|
-
}
|
|
1130
|
-
};
|
|
1131
|
-
var NotRunningInterceptorServerError_default = NotRunningInterceptorServerError;
|
|
1132
|
-
|
|
1133
|
-
// src/server/errors/RunningInterceptorServerError.ts
|
|
1134
|
-
var RunningInterceptorServerError = class extends Error {
|
|
1135
|
-
static {
|
|
1136
|
-
__name(this, "RunningInterceptorServerError");
|
|
1137
|
-
}
|
|
1138
|
-
constructor(additionalMessage) {
|
|
1139
|
-
super(`The interceptor server is running.${additionalMessage}`);
|
|
1140
|
-
this.name = "RunningInterceptorServerError";
|
|
1141
|
-
}
|
|
1142
|
-
};
|
|
1143
|
-
var RunningInterceptorServerError_default = RunningInterceptorServerError;
|
|
1144
1105
|
|
|
1145
1106
|
// src/server/utils/fetch.ts
|
|
1146
1107
|
async function getFetchAPI() {
|
|
@@ -1425,26 +1386,7 @@ function createInterceptorServer(options = {}) {
|
|
|
1425
1386
|
return new InterceptorServer_default(options);
|
|
1426
1387
|
}
|
|
1427
1388
|
__name(createInterceptorServer, "createInterceptorServer");
|
|
1428
|
-
|
|
1429
|
-
// src/server/namespace/InterceptorServerNamespace.ts
|
|
1430
|
-
var InterceptorServerNamespace = class {
|
|
1431
|
-
static {
|
|
1432
|
-
__name(this, "InterceptorServerNamespace");
|
|
1433
|
-
}
|
|
1434
|
-
/**
|
|
1435
|
-
* Creates an {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server interceptor server}.
|
|
1436
|
-
*
|
|
1437
|
-
* @param options The options to create the server.
|
|
1438
|
-
* @returns The created server.
|
|
1439
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server-programmatic-usage `zimic-interceptor server` programmatic usage}
|
|
1440
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/getting‐started#remote-http-interceptors Remote HTTP Interceptors} .
|
|
1441
|
-
*/
|
|
1442
|
-
create = createInterceptorServer;
|
|
1443
|
-
};
|
|
1444
|
-
var InterceptorServerNamespace_default = InterceptorServerNamespace;
|
|
1445
|
-
|
|
1446
|
-
// src/server/index.ts
|
|
1447
|
-
var interceptorServer = Object.freeze(new InterceptorServerNamespace_default());
|
|
1389
|
+
/* istanbul ignore next -- @preserve */
|
|
1448
1390
|
/* istanbul ignore if -- @preserve
|
|
1449
1391
|
* This is expected not to happen since the servers are not stopped unless they are running. */
|
|
1450
1392
|
/* istanbul ignore if -- @preserve
|
|
@@ -1460,7 +1402,6 @@ var interceptorServer = Object.freeze(new InterceptorServerNamespace_default());
|
|
|
1460
1402
|
/* istanbul ignore next -- @preserve
|
|
1461
1403
|
* Reply listeners are always present when notified in normal conditions. If they were not present, the request
|
|
1462
1404
|
* would reach a timeout and not be responded. The empty set serves as a fallback. */
|
|
1463
|
-
/* istanbul ignore next -- @preserve */
|
|
1464
1405
|
/* istanbul ignore if -- @preserve
|
|
1465
1406
|
* The HTTP server is initialized before using this method in normal conditions. */
|
|
1466
1407
|
/* istanbul ignore if -- @preserve
|
|
@@ -1471,6 +1412,6 @@ var interceptorServer = Object.freeze(new InterceptorServerNamespace_default());
|
|
|
1471
1412
|
* This try..catch is for the case when the remote interceptor web socket client is closed before responding.
|
|
1472
1413
|
* Since simulating this scenario is difficult, we are ignoring this branch fow now. */
|
|
1473
1414
|
|
|
1474
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default, RunningInterceptorServerError_default, createCachedDynamicImport_default,
|
|
1475
|
-
//# sourceMappingURL=chunk-
|
|
1476
|
-
//# sourceMappingURL=chunk-
|
|
1415
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default, RunningInterceptorServerError_default, createCachedDynamicImport_default, createInterceptorServer, logWithPrefix };
|
|
1416
|
+
//# sourceMappingURL=chunk-NTRC2S4I.mjs.map
|
|
1417
|
+
//# sourceMappingURL=chunk-NTRC2S4I.mjs.map
|