@zimic/interceptor 1.2.3-canary.0 → 1.2.3-canary.2

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/dist/server.js CHANGED
@@ -1,29 +1,29 @@
1
1
  'use strict';
2
2
 
3
- var chunkEMX4G67A_js = require('./chunk-EMX4G67A.js');
3
+ var chunkZU6IGW27_js = require('./chunk-ZU6IGW27.js');
4
4
  require('./chunk-DGUM43GV.js');
5
5
 
6
6
 
7
7
 
8
8
  Object.defineProperty(exports, "DEFAULT_ACCESS_CONTROL_HEADERS", {
9
9
  enumerable: true,
10
- get: function () { return chunkEMX4G67A_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
10
+ get: function () { return chunkZU6IGW27_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
11
11
  });
12
12
  Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
13
13
  enumerable: true,
14
- get: function () { return chunkEMX4G67A_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
14
+ get: function () { return chunkZU6IGW27_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
15
15
  });
16
16
  Object.defineProperty(exports, "NotRunningInterceptorServerError", {
17
17
  enumerable: true,
18
- get: function () { return chunkEMX4G67A_js.NotRunningInterceptorServerError_default; }
18
+ get: function () { return chunkZU6IGW27_js.NotRunningInterceptorServerError_default; }
19
19
  });
20
20
  Object.defineProperty(exports, "RunningInterceptorServerError", {
21
21
  enumerable: true,
22
- get: function () { return chunkEMX4G67A_js.RunningInterceptorServerError_default; }
22
+ get: function () { return chunkZU6IGW27_js.RunningInterceptorServerError_default; }
23
23
  });
24
24
  Object.defineProperty(exports, "createInterceptorServer", {
25
25
  enumerable: true,
26
- get: function () { return chunkEMX4G67A_js.createInterceptorServer; }
26
+ get: function () { return chunkZU6IGW27_js.createInterceptorServer; }
27
27
  });
28
28
  //# sourceMappingURL=server.js.map
29
29
  //# sourceMappingURL=server.js.map
package/dist/server.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-ZPZDEDJI.mjs';
1
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-XCYZ5L2M.mjs';
2
2
  import './chunk-BJTO5JO5.mjs';
3
3
  //# sourceMappingURL=server.mjs.map
4
4
  //# sourceMappingURL=server.mjs.map
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "api",
15
15
  "static"
16
16
  ],
17
- "version": "1.2.3-canary.0",
17
+ "version": "1.2.3-canary.2",
18
18
  "homepage": "https://zimic.dev/docs/interceptor",
19
19
  "repository": {
20
20
  "type": "git",
@@ -110,9 +110,9 @@
110
110
  "@types/node": "^24.10.1",
111
111
  "@types/ws": "^8.18.1",
112
112
  "@types/yargs": "^17.0.35",
113
- "@vitest/browser": "^4.0.6",
114
- "@vitest/browser-playwright": "^4.0.6",
115
- "@vitest/coverage-istanbul": "^4.0.6",
113
+ "@vitest/browser": "^4.0.9",
114
+ "@vitest/browser-playwright": "^4.0.9",
115
+ "@vitest/coverage-istanbul": "^4.0.9",
116
116
  "@zimic/eslint-config-node": "workspace:*",
117
117
  "@zimic/lint-staged-config": "workspace:*",
118
118
  "@zimic/tsconfig": "workspace:*",
@@ -123,7 +123,7 @@
123
123
  "playwright": "^1.56.1",
124
124
  "tsup": "^8.5.1",
125
125
  "typescript": "^5.9.3",
126
- "vitest": "^4.0.6"
126
+ "vitest": "^4.0.9"
127
127
  },
128
128
  "peerDependencies": {
129
129
  "@zimic/http": "^1.2.0 || workspace:*",
@@ -374,25 +374,21 @@ class HttpInterceptorClient<
374
374
  }
375
375
  }
376
376
 
377
- clear(options: { onCommitSuccess?: () => void; onCommitError?: () => void } = {}) {
378
- const clearResults: Promise<AnyHttpRequestHandlerClient | void>[] = [
377
+ clear() {
378
+ const clearPromises: Promise<AnyHttpRequestHandlerClient | void>[] = [
379
379
  Promise.resolve(this.workerOrThrow.clearInterceptorHandlers(this)),
380
380
  ];
381
381
 
382
382
  for (const method of HTTP_METHODS) {
383
- const newClearResults = this.clearMethodHandlers(method);
384
-
385
- for (const result of newClearResults) {
386
- clearResults.push(Promise.resolve(result));
383
+ for (const result of this.clearMethodHandlers(method)) {
384
+ clearPromises.push(Promise.resolve(result));
387
385
  }
388
386
 
389
387
  const pathHandlers = this.handlers[method];
390
388
  pathHandlers.clear();
391
389
  }
392
390
 
393
- if (options.onCommitSuccess) {
394
- void Promise.all(clearResults).then(options.onCommitSuccess, options.onCommitError);
395
- }
391
+ return Promise.all(clearPromises);
396
392
  }
397
393
 
398
394
  private clearMethodHandlers(method: HttpMethod) {
@@ -116,7 +116,7 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
116
116
  }
117
117
 
118
118
  clear() {
119
- this.client.clear();
119
+ void this.client.clear();
120
120
  }
121
121
  }
122
122
 
@@ -157,12 +157,7 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
157
157
  }
158
158
 
159
159
  async clear() {
160
- await new Promise<void>((resolve, reject) => {
161
- this.client.clear({
162
- onCommitSuccess: resolve,
163
- onCommitError: reject,
164
- });
165
- });
160
+ await this.client.clear();
166
161
  }
167
162
  }
168
163
 
@@ -5,6 +5,7 @@ import { HttpHandlerCommit, InterceptorServerWebSocketSchema } from '@/server/ty
5
5
  import { importCrypto } from '@/utils/crypto';
6
6
  import { isClientSide, isServerSide } from '@/utils/environment';
7
7
  import { deserializeRequest, serializeResponse } from '@/utils/fetch';
8
+ import { WebSocketMessageAbortError } from '@/utils/webSocket';
8
9
  import { WebSocketEventMessage } from '@/webSocket/types';
9
10
  import WebSocketClient from '@/webSocket/WebSocketClient';
10
11
 
@@ -173,8 +174,27 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
173
174
 
174
175
  this.httpHandlers.clear();
175
176
 
176
- if (this.webSocketClient.isRunning) {
177
+ if (!this.webSocketClient.isRunning) {
178
+ return;
179
+ }
180
+
181
+ try {
177
182
  await this.webSocketClient.request('interceptors/workers/reset', undefined);
183
+ } catch (error) {
184
+ /* istanbul ignore next -- @preserve
185
+ *
186
+ * If the socket is closed before receiving a response, the message is aborted with an error. This can happen if
187
+ * we send a request message and the interceptor server closes the socket before sending a response. In this case,
188
+ * we can safely ignore the error because we know that the server is shutting down and resetting is no longer
189
+ * necessary.
190
+ *
191
+ * Due to the rare nature of this edge case, we can't reliably reproduce it in tests. */
192
+ const isMessageAbortError = error instanceof WebSocketMessageAbortError;
193
+
194
+ /* istanbul ignore next -- @preserve */
195
+ if (!isMessageAbortError) {
196
+ throw error;
197
+ }
178
198
  }
179
199
  }
180
200
 
@@ -189,15 +209,34 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
189
209
  }
190
210
  }
191
211
 
192
- if (this.webSocketClient.isRunning) {
193
- const groupsToRecommit = Array.from<HttpHandler, HttpHandlerCommit>(this.httpHandlers.values(), (handler) => ({
194
- id: handler.id,
195
- baseURL: handler.baseURL,
196
- method: handler.method,
197
- path: handler.path,
198
- }));
212
+ if (!this.webSocketClient.isRunning) {
213
+ return;
214
+ }
215
+
216
+ const groupsToRecommit = Array.from<HttpHandler, HttpHandlerCommit>(this.httpHandlers.values(), (handler) => ({
217
+ id: handler.id,
218
+ baseURL: handler.baseURL,
219
+ method: handler.method,
220
+ path: handler.path,
221
+ }));
199
222
 
223
+ try {
200
224
  await this.webSocketClient.request('interceptors/workers/reset', groupsToRecommit);
225
+ } catch (error) {
226
+ /* istanbul ignore next -- @preserve
227
+ *
228
+ * If the socket is closed before receiving a response, the message is aborted with an error. This can happen if
229
+ * we send a request message and the interceptor server closes the socket before sending a response. In this case,
230
+ * we can safely ignore the error because we know that the server is shutting down and resetting is no longer
231
+ * necessary.
232
+ *
233
+ * Due to the rare nature of this edge case, we can't reliably reproduce it in tests. */
234
+ const isMessageAbortError = error instanceof WebSocketMessageAbortError;
235
+
236
+ /* istanbul ignore next -- @preserve */
237
+ if (!isMessageAbortError) {
238
+ throw error;
239
+ }
201
240
  }
202
241
  }
203
242
 
@@ -385,8 +385,13 @@ class InterceptorServer implements PublicInterceptorServer {
385
385
  }
386
386
  } catch (error) {
387
387
  /* istanbul ignore next -- @preserve
388
- * This try..catch is for the case when the remote interceptor web socket client is closed before responding.
389
- * Since simulating this scenario is difficult, we are ignoring this branch fow now. */
388
+ *
389
+ * If the socket is closed before receiving a response, the message is aborted with an error. This can happen if
390
+ * we send a request message and the interceptor worker closes the socket before sending a response. In this
391
+ * case, we can safely ignore the error because we know that the worker is shutting down and won't handle
392
+ * any more requests.
393
+ *
394
+ * Due to the rare nature of this edge case, we can't reliably reproduce it in tests. */
390
395
  const isMessageAbortError = error instanceof WebSocketMessageAbortError;
391
396
 
392
397
  /* istanbul ignore next -- @preserve */