@zimic/interceptor 0.14.0 → 0.15.0-canary.1

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.
Files changed (43) hide show
  1. package/README.md +7 -8
  2. package/dist/{chunk-FEGMEAEO.mjs → chunk-3O3YPVEG.mjs} +69 -104
  3. package/dist/chunk-3O3YPVEG.mjs.map +1 -0
  4. package/dist/{chunk-3623PRKE.js → chunk-CWBDZYUG.js} +69 -104
  5. package/dist/chunk-CWBDZYUG.js.map +1 -0
  6. package/dist/cli.js +7 -7
  7. package/dist/cli.js.map +1 -1
  8. package/dist/cli.mjs +3 -3
  9. package/dist/cli.mjs.map +1 -1
  10. package/dist/http.d.ts +47 -38
  11. package/dist/http.js +173 -235
  12. package/dist/http.js.map +1 -1
  13. package/dist/http.mjs +173 -235
  14. package/dist/http.mjs.map +1 -1
  15. package/dist/server.d.ts +15 -10
  16. package/dist/server.js +5 -5
  17. package/dist/server.mjs +1 -1
  18. package/package.json +3 -3
  19. package/src/cli/server/start.ts +1 -1
  20. package/src/http/index.ts +14 -1
  21. package/src/http/interceptor/HttpInterceptorClient.ts +23 -34
  22. package/src/http/interceptor/HttpInterceptorStore.ts +2 -2
  23. package/src/http/interceptor/LocalHttpInterceptor.ts +22 -25
  24. package/src/http/interceptor/RemoteHttpInterceptor.ts +22 -25
  25. package/src/http/interceptor/errors/NotStartedHttpInterceptorError.ts +1 -1
  26. package/src/http/interceptor/errors/UnknownHttpInterceptorPlatformError.ts +1 -1
  27. package/src/http/interceptor/types/options.ts +3 -3
  28. package/src/http/interceptor/types/public.ts +12 -9
  29. package/src/http/interceptorWorker/HttpInterceptorWorker.ts +10 -28
  30. package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +21 -25
  31. package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +22 -26
  32. package/src/http/requestHandler/HttpRequestHandlerClient.ts +13 -23
  33. package/src/http/requestHandler/LocalHttpRequestHandler.ts +17 -21
  34. package/src/http/requestHandler/RemoteHttpRequestHandler.ts +19 -29
  35. package/src/http/requestHandler/types/public.ts +23 -23
  36. package/src/http/requestHandler/types/requests.ts +1 -1
  37. package/src/server/InterceptorServer.ts +45 -68
  38. package/src/server/types/public.ts +15 -10
  39. package/src/webSocket/WebSocketClient.ts +1 -1
  40. package/src/webSocket/WebSocketHandler.ts +11 -19
  41. package/src/webSocket/WebSocketServer.ts +4 -4
  42. package/dist/chunk-3623PRKE.js.map +0 -1
  43. package/dist/chunk-FEGMEAEO.mjs.map +0 -1
package/README.md CHANGED
@@ -149,7 +149,7 @@ Check our [getting started guide](https://github.com/zimicjs/zimic/wiki/getting
149
149
  const interceptor = httpInterceptor.create<Schema>({
150
150
  type: 'local',
151
151
  baseURL: 'http://localhost:3000',
152
- saveRequests: true, // Allow access to `handler.requests()`
152
+ saveRequests: true, // Allow access to `handler.requests`
153
153
  });
154
154
  ```
155
155
 
@@ -227,21 +227,20 @@ Check our [getting started guide](https://github.com/zimicjs/zimic/wiki/getting
227
227
  test('example', async () => {
228
228
  // Your application makes requests...
229
229
 
230
- const requests = handler.requests();
231
- expect(requests).toHaveLength(1);
230
+ expect(handler.requests).toHaveLength(1);
232
231
 
233
- expect(requests[0].headers.get('authorization')).toBe('Bearer my-token');
232
+ expect(handler.requests[0].headers.get('authorization')).toBe('Bearer my-token');
234
233
 
235
- expect(requests[0].searchParams.size).toBe(1);
236
- expect(requests[0].searchParams.get('username')).toBe('my');
234
+ expect(handler.requests[0].searchParams.size).toBe(1);
235
+ expect(handler.requests[0].searchParams.get('username')).toBe('my');
237
236
 
238
- expect(requests[0].body).toBe(null);
237
+ expect(handler.requests[0].body).toBe(null);
239
238
  });
240
239
  ```
241
240
 
242
241
  > [!NOTE]
243
242
  >
244
- > The 5.2 checks the application requests manually. This is optional in this example because the
243
+ > Step 5.2 manually verifies the requests made by the application. This is optional in this example because the
245
244
  > [`with`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerwithrestriction) and
246
245
  > [`times`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlertimes) calls (step 5.1) already
247
246
  > act as a declarative validation, expressing that exactly one request is expected with specific data. If fewer or more
@@ -273,26 +273,14 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
273
273
  static {
274
274
  __name(this, "HttpInterceptorWorker");
275
275
  }
276
- _platform = null;
277
- _isRunning = false;
276
+ platform = null;
277
+ isRunning = false;
278
278
  startingPromise;
279
279
  stoppingPromise;
280
280
  store = new HttpInterceptorWorkerStore_default();
281
281
  runningInterceptors = [];
282
- platform() {
283
- return this._platform;
284
- }
285
- setPlatform(platform) {
286
- this._platform = platform;
287
- }
288
- isRunning() {
289
- return this._isRunning;
290
- }
291
- setIsRunning(isRunning) {
292
- this._isRunning = isRunning;
293
- }
294
282
  async sharedStart(internalStart) {
295
- if (this.isRunning()) {
283
+ if (this.isRunning) {
296
284
  return;
297
285
  }
298
286
  if (this.startingPromise) {
@@ -311,7 +299,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
311
299
  }
312
300
  }
313
301
  async sharedStop(internalStop) {
314
- if (!this.isRunning()) {
302
+ if (!this.isRunning) {
315
303
  return;
316
304
  }
317
305
  if (this.stoppingPromise) {
@@ -376,8 +364,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
376
364
  }
377
365
  findInterceptorByRequestBaseURL(request) {
378
366
  const interceptor = this.runningInterceptors.findLast((interceptor2) => {
379
- const baseURL = interceptor2.baseURL();
380
- return request.url.startsWith(baseURL);
367
+ return request.url.startsWith(interceptor2.baseURLAsString);
381
368
  });
382
369
  return interceptor;
383
370
  }
@@ -393,12 +380,11 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
393
380
  return defaultStrategyOrFactory;
394
381
  }
395
382
  async getInterceptorUnhandledRequestStrategy(request, interceptor) {
396
- const interceptorStrategyOrFactory = interceptor.onUnhandledRequest();
397
- if (typeof interceptorStrategyOrFactory === "function") {
383
+ if (typeof interceptor.onUnhandledRequest === "function") {
398
384
  const parsedRequest = await _HttpInterceptorWorker.parseRawUnhandledRequest(request);
399
- return interceptorStrategyOrFactory(parsedRequest);
385
+ return interceptor.onUnhandledRequest(parsedRequest);
400
386
  }
401
- return interceptorStrategyOrFactory;
387
+ return interceptor.onUnhandledRequest;
402
388
  }
403
389
  static createResponseFromDeclaration(request, declaration) {
404
390
  const headers = new HttpHeaders(declaration.headers);
@@ -838,24 +824,18 @@ var WebSocketHandler = class {
838
824
  __name(this, "WebSocketHandler");
839
825
  }
840
826
  sockets = /* @__PURE__ */ new Set();
841
- _socketTimeout;
842
- _messageTimeout;
827
+ socketTimeout;
828
+ messageTimeout;
843
829
  channelListeners = {};
844
830
  socketListeners = {
845
831
  messageAbort: /* @__PURE__ */ new Map()
846
832
  };
847
833
  constructor(options) {
848
- this._socketTimeout = options.socketTimeout ?? DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT;
849
- this._messageTimeout = options.messageTimeout ?? DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT;
850
- }
851
- socketTimeout() {
852
- return this._socketTimeout;
853
- }
854
- messageTimeout() {
855
- return this._messageTimeout;
834
+ this.socketTimeout = options.socketTimeout ?? DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT;
835
+ this.messageTimeout = options.messageTimeout ?? DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT;
856
836
  }
857
837
  async registerSocket(socket) {
858
- const openPromise = waitForOpenClientSocket(socket, { timeout: this._socketTimeout });
838
+ const openPromise = waitForOpenClientSocket(socket, { timeout: this.socketTimeout });
859
839
  const handleSocketMessage = /* @__PURE__ */ __name(async (rawMessage) => {
860
840
  await this.handleSocketMessage(socket, rawMessage);
861
841
  }, "handleSocketMessage");
@@ -942,7 +922,7 @@ var WebSocketHandler = class {
942
922
  }
943
923
  async closeClientSockets(sockets = this.sockets) {
944
924
  const closingPromises = Array.from(sockets, async (socket) => {
945
- await closeClientSocket(socket, { timeout: this._socketTimeout });
925
+ await closeClientSocket(socket, { timeout: this.socketTimeout });
946
926
  });
947
927
  await Promise.all(closingPromises);
948
928
  }
@@ -974,9 +954,9 @@ var WebSocketHandler = class {
974
954
  const replyTimeout = setTimeout(() => {
975
955
  this.offReply(channel, replyListener);
976
956
  this.offAbortSocketMessages(sockets, abortListener);
977
- const timeoutError = new WebSocketMessageTimeoutError(this._messageTimeout);
957
+ const timeoutError = new WebSocketMessageTimeoutError(this.messageTimeout);
978
958
  reject(timeoutError);
979
- }, this._messageTimeout);
959
+ }, this.messageTimeout);
980
960
  const abortListener = this.onAbortSocketMessages(sockets, (error) => {
981
961
  clearTimeout(replyTimeout);
982
962
  this.offReply(channel, replyListener);
@@ -998,7 +978,7 @@ var WebSocketHandler = class {
998
978
  }
999
979
  async reply(request, replyData, options) {
1000
980
  const reply = await this.createReplyMessage(request, replyData);
1001
- if (this.isRunning()) {
981
+ if (this.isRunning) {
1002
982
  this.sendMessage(reply, options.sockets);
1003
983
  }
1004
984
  }
@@ -1013,7 +993,7 @@ var WebSocketHandler = class {
1013
993
  return replyMessage;
1014
994
  }
1015
995
  sendMessage(message, sockets = this.sockets) {
1016
- if (!this.isRunning()) {
996
+ if (!this.isRunning) {
1017
997
  throw new NotStartedWebSocketHandlerError_default();
1018
998
  }
1019
999
  const stringifiedMessage = JSON.stringify(message);
@@ -1093,11 +1073,11 @@ var WebSocketServer = class extends WebSocketHandler_default {
1093
1073
  });
1094
1074
  this.httpServer = options.httpServer;
1095
1075
  }
1096
- isRunning() {
1076
+ get isRunning() {
1097
1077
  return this.webSocketServer !== void 0;
1098
1078
  }
1099
1079
  start() {
1100
- if (this.isRunning()) {
1080
+ if (this.isRunning) {
1101
1081
  return;
1102
1082
  }
1103
1083
  const webSocketServer = new ServerSocket({ server: this.httpServer });
@@ -1114,13 +1094,13 @@ var WebSocketServer = class extends WebSocketHandler_default {
1114
1094
  this.webSocketServer = webSocketServer;
1115
1095
  }
1116
1096
  async stop() {
1117
- if (!this.webSocketServer || !this.isRunning()) {
1097
+ if (!this.webSocketServer || !this.isRunning) {
1118
1098
  return;
1119
1099
  }
1120
1100
  super.removeAllChannelListeners();
1121
1101
  super.abortSocketMessages();
1122
1102
  await super.closeClientSockets();
1123
- await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout() });
1103
+ await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout });
1124
1104
  this.webSocketServer.removeAllListeners();
1125
1105
  this.webSocketServer = void 0;
1126
1106
  }
@@ -1182,11 +1162,11 @@ var InterceptorServer = class {
1182
1162
  static {
1183
1163
  __name(this, "InterceptorServer");
1184
1164
  }
1185
- _httpServer;
1186
- _webSocketServer;
1187
- _hostname;
1188
- _port;
1189
- _logUnhandledRequests;
1165
+ httpServer;
1166
+ webSocketServer;
1167
+ hostname;
1168
+ port;
1169
+ logUnhandledRequests;
1190
1170
  httpHandlerGroups = {
1191
1171
  GET: [],
1192
1172
  POST: [],
@@ -1198,68 +1178,57 @@ var InterceptorServer = class {
1198
1178
  };
1199
1179
  knownWorkerSockets = /* @__PURE__ */ new Set();
1200
1180
  constructor(options) {
1201
- this._hostname = options.hostname ?? "localhost";
1202
- this._port = options.port;
1203
- this._logUnhandledRequests = options.logUnhandledRequests ?? DEFAULT_LOG_UNHANDLED_REQUESTS;
1204
- }
1205
- hostname() {
1206
- return this._hostname;
1207
- }
1208
- port() {
1209
- return this._port;
1210
- }
1211
- logUnhandledRequests() {
1212
- return this._logUnhandledRequests;
1181
+ this.hostname = options.hostname ?? "localhost";
1182
+ this.port = options.port;
1183
+ this.logUnhandledRequests = options.logUnhandledRequests ?? DEFAULT_LOG_UNHANDLED_REQUESTS;
1213
1184
  }
1214
- httpURL() {
1215
- if (this._port === void 0) {
1185
+ get httpURL() {
1186
+ if (this.port === void 0) {
1216
1187
  return void 0;
1217
1188
  }
1218
- return `http://${this._hostname}:${this._port}`;
1189
+ return `http://${this.hostname}:${this.port}`;
1219
1190
  }
1220
- isRunning() {
1221
- return !!this._httpServer?.listening && !!this._webSocketServer?.isRunning();
1191
+ get isRunning() {
1192
+ return !!this.httpServer?.listening && !!this.webSocketServer?.isRunning;
1222
1193
  }
1223
- httpServer() {
1224
- if (!this._httpServer) {
1194
+ get httpServerOrThrow() {
1195
+ if (!this.httpServer) {
1225
1196
  throw new NotStartedInterceptorServerError_default();
1226
1197
  }
1227
- return this._httpServer;
1198
+ return this.httpServer;
1228
1199
  }
1229
- webSocketServer() {
1230
- if (!this._webSocketServer) {
1200
+ get webSocketServerOrThrow() {
1201
+ if (!this.webSocketServer) {
1231
1202
  throw new NotStartedInterceptorServerError_default();
1232
1203
  }
1233
- return this._webSocketServer;
1204
+ return this.webSocketServer;
1234
1205
  }
1235
1206
  async start() {
1236
- if (this.isRunning()) {
1207
+ if (this.isRunning) {
1237
1208
  return;
1238
1209
  }
1239
- this._httpServer = createServer({
1210
+ this.httpServer = createServer({
1240
1211
  keepAlive: true,
1241
1212
  joinDuplicateHeaders: true
1242
1213
  });
1243
1214
  await this.startHttpServer();
1244
- this._webSocketServer = new WebSocketServer_default({
1245
- httpServer: this._httpServer
1215
+ this.webSocketServer = new WebSocketServer_default({
1216
+ httpServer: this.httpServer
1246
1217
  });
1247
1218
  this.startWebSocketServer();
1248
1219
  }
1249
1220
  async startHttpServer() {
1250
- const httpServer = this.httpServer();
1251
- await startHttpServer(httpServer, {
1252
- hostname: this._hostname,
1253
- port: this._port
1221
+ await startHttpServer(this.httpServerOrThrow, {
1222
+ hostname: this.hostname,
1223
+ port: this.port
1254
1224
  });
1255
- this._port = getHttpServerPort(httpServer);
1256
- httpServer.on("request", this.handleHttpRequest);
1225
+ this.port = getHttpServerPort(this.httpServerOrThrow);
1226
+ this.httpServerOrThrow.on("request", this.handleHttpRequest);
1257
1227
  }
1258
1228
  startWebSocketServer() {
1259
- const webSocketServer = this.webSocketServer();
1260
- webSocketServer.start();
1261
- webSocketServer.onEvent("interceptors/workers/use/commit", this.commitWorker);
1262
- webSocketServer.onEvent("interceptors/workers/use/reset", this.resetWorker);
1229
+ this.webSocketServerOrThrow.start();
1230
+ this.webSocketServerOrThrow.onEvent("interceptors/workers/use/commit", this.commitWorker);
1231
+ this.webSocketServerOrThrow.onEvent("interceptors/workers/use/reset", this.resetWorker);
1263
1232
  }
1264
1233
  commitWorker = /* @__PURE__ */ __name((message, socket) => {
1265
1234
  const commit = message.data;
@@ -1272,7 +1241,7 @@ var InterceptorServer = class {
1272
1241
  const handlersToResetTo = message.data;
1273
1242
  const isWorkerNoLongerCommitted = handlersToResetTo === void 0;
1274
1243
  if (isWorkerNoLongerCommitted) {
1275
- this.webSocketServer().abortSocketMessages([socket]);
1244
+ this.webSocketServerOrThrow.abortSocketMessages([socket]);
1276
1245
  } else {
1277
1246
  for (const handler of handlersToResetTo) {
1278
1247
  this.registerHttpHandler(handler, socket);
@@ -1310,25 +1279,23 @@ var InterceptorServer = class {
1310
1279
  removeArrayIndex(handlerGroups, socketIndex);
1311
1280
  }
1312
1281
  }
1313
- stop = /* @__PURE__ */ __name(async () => {
1314
- if (!this.isRunning()) {
1282
+ async stop() {
1283
+ if (!this.isRunning) {
1315
1284
  return;
1316
1285
  }
1317
1286
  await this.stopWebSocketServer();
1318
1287
  await this.stopHttpServer();
1319
- }, "stop");
1288
+ }
1320
1289
  async stopHttpServer() {
1321
- const httpServer = this.httpServer();
1322
- await stopHttpServer(httpServer);
1323
- httpServer.removeAllListeners();
1324
- this._httpServer = void 0;
1290
+ await stopHttpServer(this.httpServerOrThrow);
1291
+ this.httpServerOrThrow.removeAllListeners();
1292
+ this.httpServer = void 0;
1325
1293
  }
1326
1294
  async stopWebSocketServer() {
1327
- const webSocketServer = this.webSocketServer();
1328
- webSocketServer.offEvent("interceptors/workers/use/commit", this.commitWorker);
1329
- webSocketServer.offEvent("interceptors/workers/use/reset", this.resetWorker);
1330
- await webSocketServer.stop();
1331
- this._webSocketServer = void 0;
1295
+ this.webSocketServerOrThrow.offEvent("interceptors/workers/use/commit", this.commitWorker);
1296
+ this.webSocketServerOrThrow.offEvent("interceptors/workers/use/reset", this.resetWorker);
1297
+ await this.webSocketServerOrThrow.stop();
1298
+ this.webSocketServer = void 0;
1332
1299
  }
1333
1300
  handleHttpRequest = /* @__PURE__ */ __name(async (nodeRequest, nodeResponse) => {
1334
1301
  const request = normalizeNodeRequest(nodeRequest, await getFetchAPI());
@@ -1361,7 +1328,6 @@ var InterceptorServer = class {
1361
1328
  }
1362
1329
  }, "handleHttpRequest");
1363
1330
  async createResponseForRequest(request) {
1364
- const webSocketServer = this.webSocketServer();
1365
1331
  const methodHandlers = this.httpHandlerGroups[request.method];
1366
1332
  const requestURL = excludeURLParams_default(new URL(request.url)).toString();
1367
1333
  let matchedSomeInterceptor = false;
@@ -1372,7 +1338,7 @@ var InterceptorServer = class {
1372
1338
  continue;
1373
1339
  }
1374
1340
  matchedSomeInterceptor = true;
1375
- const { response: serializedResponse } = await webSocketServer.request(
1341
+ const { response: serializedResponse } = await this.webSocketServerOrThrow.request(
1376
1342
  "interceptors/responses/create",
1377
1343
  { handlerId: handler.id, request },
1378
1344
  { sockets: [handler.socket] }
@@ -1396,11 +1362,10 @@ var InterceptorServer = class {
1396
1362
  }
1397
1363
  }
1398
1364
  async logUnhandledRequestIfNecessary(request, serializedRequest) {
1399
- const webSocketServer = this.webSocketServer();
1400
1365
  const handler = this.findHttpHandlerByRequestBaseURL(request);
1401
1366
  if (handler) {
1402
1367
  try {
1403
- const { wasLogged: wasRequestLoggedByRemoteInterceptor } = await webSocketServer.request(
1368
+ const { wasLogged: wasRequestLoggedByRemoteInterceptor } = await this.webSocketServerOrThrow.request(
1404
1369
  "interceptors/responses/unhandled",
1405
1370
  { request: serializedRequest },
1406
1371
  { sockets: [handler.socket] }
@@ -1415,7 +1380,7 @@ var InterceptorServer = class {
1415
1380
  }
1416
1381
  }
1417
1382
  }
1418
- if (!this._logUnhandledRequests) {
1383
+ if (!this.logUnhandledRequests) {
1419
1384
  return;
1420
1385
  }
1421
1386
  await HttpInterceptorWorker_default.logUnhandledRequestWarning(request, "reject");
@@ -1482,5 +1447,5 @@ var interceptorServer = Object.freeze(new InterceptorServerNamespace_default());
1482
1447
  * Since simulating this scenario is difficult, we are ignoring this branch fow now. */
1483
1448
 
1484
1449
  export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default, createCachedDynamicImport_default, interceptorServer, logWithPrefix };
1485
- //# sourceMappingURL=chunk-FEGMEAEO.mjs.map
1486
- //# sourceMappingURL=chunk-FEGMEAEO.mjs.map
1450
+ //# sourceMappingURL=chunk-3O3YPVEG.mjs.map
1451
+ //# sourceMappingURL=chunk-3O3YPVEG.mjs.map