@zimic/interceptor 0.15.0-canary.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 (41) hide show
  1. package/README.md +6 -7
  2. package/dist/{chunk-YGJD3JT6.mjs → chunk-3O3YPVEG.mjs} +69 -104
  3. package/dist/chunk-3O3YPVEG.mjs.map +1 -0
  4. package/dist/{chunk-L6Y4EV6T.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 +38 -35
  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 +2 -2
  19. package/src/cli/server/start.ts +1 -1
  20. package/src/http/interceptor/HttpInterceptorClient.ts +23 -34
  21. package/src/http/interceptor/HttpInterceptorStore.ts +2 -2
  22. package/src/http/interceptor/LocalHttpInterceptor.ts +22 -25
  23. package/src/http/interceptor/RemoteHttpInterceptor.ts +22 -25
  24. package/src/http/interceptor/errors/NotStartedHttpInterceptorError.ts +1 -1
  25. package/src/http/interceptor/errors/UnknownHttpInterceptorPlatformError.ts +1 -1
  26. package/src/http/interceptor/types/options.ts +3 -3
  27. package/src/http/interceptor/types/public.ts +12 -9
  28. package/src/http/interceptorWorker/HttpInterceptorWorker.ts +10 -28
  29. package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +21 -25
  30. package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +22 -26
  31. package/src/http/requestHandler/HttpRequestHandlerClient.ts +11 -22
  32. package/src/http/requestHandler/LocalHttpRequestHandler.ts +16 -20
  33. package/src/http/requestHandler/RemoteHttpRequestHandler.ts +18 -28
  34. package/src/http/requestHandler/types/public.ts +22 -24
  35. package/src/server/InterceptorServer.ts +45 -68
  36. package/src/server/types/public.ts +15 -10
  37. package/src/webSocket/WebSocketClient.ts +1 -1
  38. package/src/webSocket/WebSocketHandler.ts +11 -19
  39. package/src/webSocket/WebSocketServer.ts +4 -4
  40. package/dist/chunk-L6Y4EV6T.js.map +0 -1
  41. package/dist/chunk-YGJD3JT6.mjs.map +0 -1
@@ -280,26 +280,14 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
280
280
  static {
281
281
  chunkWCQVDF3K_js.__name(this, "HttpInterceptorWorker");
282
282
  }
283
- _platform = null;
284
- _isRunning = false;
283
+ platform = null;
284
+ isRunning = false;
285
285
  startingPromise;
286
286
  stoppingPromise;
287
287
  store = new HttpInterceptorWorkerStore_default();
288
288
  runningInterceptors = [];
289
- platform() {
290
- return this._platform;
291
- }
292
- setPlatform(platform) {
293
- this._platform = platform;
294
- }
295
- isRunning() {
296
- return this._isRunning;
297
- }
298
- setIsRunning(isRunning) {
299
- this._isRunning = isRunning;
300
- }
301
289
  async sharedStart(internalStart) {
302
- if (this.isRunning()) {
290
+ if (this.isRunning) {
303
291
  return;
304
292
  }
305
293
  if (this.startingPromise) {
@@ -318,7 +306,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
318
306
  }
319
307
  }
320
308
  async sharedStop(internalStop) {
321
- if (!this.isRunning()) {
309
+ if (!this.isRunning) {
322
310
  return;
323
311
  }
324
312
  if (this.stoppingPromise) {
@@ -383,8 +371,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
383
371
  }
384
372
  findInterceptorByRequestBaseURL(request) {
385
373
  const interceptor = this.runningInterceptors.findLast((interceptor2) => {
386
- const baseURL = interceptor2.baseURL();
387
- return request.url.startsWith(baseURL);
374
+ return request.url.startsWith(interceptor2.baseURLAsString);
388
375
  });
389
376
  return interceptor;
390
377
  }
@@ -400,12 +387,11 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
400
387
  return defaultStrategyOrFactory;
401
388
  }
402
389
  async getInterceptorUnhandledRequestStrategy(request, interceptor) {
403
- const interceptorStrategyOrFactory = interceptor.onUnhandledRequest();
404
- if (typeof interceptorStrategyOrFactory === "function") {
390
+ if (typeof interceptor.onUnhandledRequest === "function") {
405
391
  const parsedRequest = await _HttpInterceptorWorker.parseRawUnhandledRequest(request);
406
- return interceptorStrategyOrFactory(parsedRequest);
392
+ return interceptor.onUnhandledRequest(parsedRequest);
407
393
  }
408
- return interceptorStrategyOrFactory;
394
+ return interceptor.onUnhandledRequest;
409
395
  }
410
396
  static createResponseFromDeclaration(request, declaration) {
411
397
  const headers = new http.HttpHeaders(declaration.headers);
@@ -845,24 +831,18 @@ var WebSocketHandler = class {
845
831
  chunkWCQVDF3K_js.__name(this, "WebSocketHandler");
846
832
  }
847
833
  sockets = /* @__PURE__ */ new Set();
848
- _socketTimeout;
849
- _messageTimeout;
834
+ socketTimeout;
835
+ messageTimeout;
850
836
  channelListeners = {};
851
837
  socketListeners = {
852
838
  messageAbort: /* @__PURE__ */ new Map()
853
839
  };
854
840
  constructor(options) {
855
- this._socketTimeout = options.socketTimeout ?? DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT;
856
- this._messageTimeout = options.messageTimeout ?? DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT;
857
- }
858
- socketTimeout() {
859
- return this._socketTimeout;
860
- }
861
- messageTimeout() {
862
- return this._messageTimeout;
841
+ this.socketTimeout = options.socketTimeout ?? DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT;
842
+ this.messageTimeout = options.messageTimeout ?? DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT;
863
843
  }
864
844
  async registerSocket(socket) {
865
- const openPromise = waitForOpenClientSocket(socket, { timeout: this._socketTimeout });
845
+ const openPromise = waitForOpenClientSocket(socket, { timeout: this.socketTimeout });
866
846
  const handleSocketMessage = /* @__PURE__ */ chunkWCQVDF3K_js.__name(async (rawMessage) => {
867
847
  await this.handleSocketMessage(socket, rawMessage);
868
848
  }, "handleSocketMessage");
@@ -949,7 +929,7 @@ var WebSocketHandler = class {
949
929
  }
950
930
  async closeClientSockets(sockets = this.sockets) {
951
931
  const closingPromises = Array.from(sockets, async (socket) => {
952
- await closeClientSocket(socket, { timeout: this._socketTimeout });
932
+ await closeClientSocket(socket, { timeout: this.socketTimeout });
953
933
  });
954
934
  await Promise.all(closingPromises);
955
935
  }
@@ -981,9 +961,9 @@ var WebSocketHandler = class {
981
961
  const replyTimeout = setTimeout(() => {
982
962
  this.offReply(channel, replyListener);
983
963
  this.offAbortSocketMessages(sockets, abortListener);
984
- const timeoutError = new WebSocketMessageTimeoutError(this._messageTimeout);
964
+ const timeoutError = new WebSocketMessageTimeoutError(this.messageTimeout);
985
965
  reject(timeoutError);
986
- }, this._messageTimeout);
966
+ }, this.messageTimeout);
987
967
  const abortListener = this.onAbortSocketMessages(sockets, (error) => {
988
968
  clearTimeout(replyTimeout);
989
969
  this.offReply(channel, replyListener);
@@ -1005,7 +985,7 @@ var WebSocketHandler = class {
1005
985
  }
1006
986
  async reply(request, replyData, options) {
1007
987
  const reply = await this.createReplyMessage(request, replyData);
1008
- if (this.isRunning()) {
988
+ if (this.isRunning) {
1009
989
  this.sendMessage(reply, options.sockets);
1010
990
  }
1011
991
  }
@@ -1020,7 +1000,7 @@ var WebSocketHandler = class {
1020
1000
  return replyMessage;
1021
1001
  }
1022
1002
  sendMessage(message, sockets = this.sockets) {
1023
- if (!this.isRunning()) {
1003
+ if (!this.isRunning) {
1024
1004
  throw new NotStartedWebSocketHandlerError_default();
1025
1005
  }
1026
1006
  const stringifiedMessage = JSON.stringify(message);
@@ -1100,11 +1080,11 @@ var WebSocketServer = class extends WebSocketHandler_default {
1100
1080
  });
1101
1081
  this.httpServer = options.httpServer;
1102
1082
  }
1103
- isRunning() {
1083
+ get isRunning() {
1104
1084
  return this.webSocketServer !== void 0;
1105
1085
  }
1106
1086
  start() {
1107
- if (this.isRunning()) {
1087
+ if (this.isRunning) {
1108
1088
  return;
1109
1089
  }
1110
1090
  const webSocketServer = new ServerSocket({ server: this.httpServer });
@@ -1121,13 +1101,13 @@ var WebSocketServer = class extends WebSocketHandler_default {
1121
1101
  this.webSocketServer = webSocketServer;
1122
1102
  }
1123
1103
  async stop() {
1124
- if (!this.webSocketServer || !this.isRunning()) {
1104
+ if (!this.webSocketServer || !this.isRunning) {
1125
1105
  return;
1126
1106
  }
1127
1107
  super.removeAllChannelListeners();
1128
1108
  super.abortSocketMessages();
1129
1109
  await super.closeClientSockets();
1130
- await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout() });
1110
+ await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout });
1131
1111
  this.webSocketServer.removeAllListeners();
1132
1112
  this.webSocketServer = void 0;
1133
1113
  }
@@ -1189,11 +1169,11 @@ var InterceptorServer = class {
1189
1169
  static {
1190
1170
  chunkWCQVDF3K_js.__name(this, "InterceptorServer");
1191
1171
  }
1192
- _httpServer;
1193
- _webSocketServer;
1194
- _hostname;
1195
- _port;
1196
- _logUnhandledRequests;
1172
+ httpServer;
1173
+ webSocketServer;
1174
+ hostname;
1175
+ port;
1176
+ logUnhandledRequests;
1197
1177
  httpHandlerGroups = {
1198
1178
  GET: [],
1199
1179
  POST: [],
@@ -1205,68 +1185,57 @@ var InterceptorServer = class {
1205
1185
  };
1206
1186
  knownWorkerSockets = /* @__PURE__ */ new Set();
1207
1187
  constructor(options) {
1208
- this._hostname = options.hostname ?? "localhost";
1209
- this._port = options.port;
1210
- this._logUnhandledRequests = options.logUnhandledRequests ?? DEFAULT_LOG_UNHANDLED_REQUESTS;
1211
- }
1212
- hostname() {
1213
- return this._hostname;
1214
- }
1215
- port() {
1216
- return this._port;
1217
- }
1218
- logUnhandledRequests() {
1219
- return this._logUnhandledRequests;
1188
+ this.hostname = options.hostname ?? "localhost";
1189
+ this.port = options.port;
1190
+ this.logUnhandledRequests = options.logUnhandledRequests ?? DEFAULT_LOG_UNHANDLED_REQUESTS;
1220
1191
  }
1221
- httpURL() {
1222
- if (this._port === void 0) {
1192
+ get httpURL() {
1193
+ if (this.port === void 0) {
1223
1194
  return void 0;
1224
1195
  }
1225
- return `http://${this._hostname}:${this._port}`;
1196
+ return `http://${this.hostname}:${this.port}`;
1226
1197
  }
1227
- isRunning() {
1228
- return !!this._httpServer?.listening && !!this._webSocketServer?.isRunning();
1198
+ get isRunning() {
1199
+ return !!this.httpServer?.listening && !!this.webSocketServer?.isRunning;
1229
1200
  }
1230
- httpServer() {
1231
- if (!this._httpServer) {
1201
+ get httpServerOrThrow() {
1202
+ if (!this.httpServer) {
1232
1203
  throw new NotStartedInterceptorServerError_default();
1233
1204
  }
1234
- return this._httpServer;
1205
+ return this.httpServer;
1235
1206
  }
1236
- webSocketServer() {
1237
- if (!this._webSocketServer) {
1207
+ get webSocketServerOrThrow() {
1208
+ if (!this.webSocketServer) {
1238
1209
  throw new NotStartedInterceptorServerError_default();
1239
1210
  }
1240
- return this._webSocketServer;
1211
+ return this.webSocketServer;
1241
1212
  }
1242
1213
  async start() {
1243
- if (this.isRunning()) {
1214
+ if (this.isRunning) {
1244
1215
  return;
1245
1216
  }
1246
- this._httpServer = http$1.createServer({
1217
+ this.httpServer = http$1.createServer({
1247
1218
  keepAlive: true,
1248
1219
  joinDuplicateHeaders: true
1249
1220
  });
1250
1221
  await this.startHttpServer();
1251
- this._webSocketServer = new WebSocketServer_default({
1252
- httpServer: this._httpServer
1222
+ this.webSocketServer = new WebSocketServer_default({
1223
+ httpServer: this.httpServer
1253
1224
  });
1254
1225
  this.startWebSocketServer();
1255
1226
  }
1256
1227
  async startHttpServer() {
1257
- const httpServer = this.httpServer();
1258
- await startHttpServer(httpServer, {
1259
- hostname: this._hostname,
1260
- port: this._port
1228
+ await startHttpServer(this.httpServerOrThrow, {
1229
+ hostname: this.hostname,
1230
+ port: this.port
1261
1231
  });
1262
- this._port = getHttpServerPort(httpServer);
1263
- httpServer.on("request", this.handleHttpRequest);
1232
+ this.port = getHttpServerPort(this.httpServerOrThrow);
1233
+ this.httpServerOrThrow.on("request", this.handleHttpRequest);
1264
1234
  }
1265
1235
  startWebSocketServer() {
1266
- const webSocketServer = this.webSocketServer();
1267
- webSocketServer.start();
1268
- webSocketServer.onEvent("interceptors/workers/use/commit", this.commitWorker);
1269
- webSocketServer.onEvent("interceptors/workers/use/reset", this.resetWorker);
1236
+ this.webSocketServerOrThrow.start();
1237
+ this.webSocketServerOrThrow.onEvent("interceptors/workers/use/commit", this.commitWorker);
1238
+ this.webSocketServerOrThrow.onEvent("interceptors/workers/use/reset", this.resetWorker);
1270
1239
  }
1271
1240
  commitWorker = /* @__PURE__ */ chunkWCQVDF3K_js.__name((message, socket) => {
1272
1241
  const commit = message.data;
@@ -1279,7 +1248,7 @@ var InterceptorServer = class {
1279
1248
  const handlersToResetTo = message.data;
1280
1249
  const isWorkerNoLongerCommitted = handlersToResetTo === void 0;
1281
1250
  if (isWorkerNoLongerCommitted) {
1282
- this.webSocketServer().abortSocketMessages([socket]);
1251
+ this.webSocketServerOrThrow.abortSocketMessages([socket]);
1283
1252
  } else {
1284
1253
  for (const handler of handlersToResetTo) {
1285
1254
  this.registerHttpHandler(handler, socket);
@@ -1317,25 +1286,23 @@ var InterceptorServer = class {
1317
1286
  removeArrayIndex(handlerGroups, socketIndex);
1318
1287
  }
1319
1288
  }
1320
- stop = /* @__PURE__ */ chunkWCQVDF3K_js.__name(async () => {
1321
- if (!this.isRunning()) {
1289
+ async stop() {
1290
+ if (!this.isRunning) {
1322
1291
  return;
1323
1292
  }
1324
1293
  await this.stopWebSocketServer();
1325
1294
  await this.stopHttpServer();
1326
- }, "stop");
1295
+ }
1327
1296
  async stopHttpServer() {
1328
- const httpServer = this.httpServer();
1329
- await stopHttpServer(httpServer);
1330
- httpServer.removeAllListeners();
1331
- this._httpServer = void 0;
1297
+ await stopHttpServer(this.httpServerOrThrow);
1298
+ this.httpServerOrThrow.removeAllListeners();
1299
+ this.httpServer = void 0;
1332
1300
  }
1333
1301
  async stopWebSocketServer() {
1334
- const webSocketServer = this.webSocketServer();
1335
- webSocketServer.offEvent("interceptors/workers/use/commit", this.commitWorker);
1336
- webSocketServer.offEvent("interceptors/workers/use/reset", this.resetWorker);
1337
- await webSocketServer.stop();
1338
- this._webSocketServer = void 0;
1302
+ this.webSocketServerOrThrow.offEvent("interceptors/workers/use/commit", this.commitWorker);
1303
+ this.webSocketServerOrThrow.offEvent("interceptors/workers/use/reset", this.resetWorker);
1304
+ await this.webSocketServerOrThrow.stop();
1305
+ this.webSocketServer = void 0;
1339
1306
  }
1340
1307
  handleHttpRequest = /* @__PURE__ */ chunkWCQVDF3K_js.__name(async (nodeRequest, nodeResponse) => {
1341
1308
  const request = server.normalizeNodeRequest(nodeRequest, await getFetchAPI());
@@ -1368,7 +1335,6 @@ var InterceptorServer = class {
1368
1335
  }
1369
1336
  }, "handleHttpRequest");
1370
1337
  async createResponseForRequest(request) {
1371
- const webSocketServer = this.webSocketServer();
1372
1338
  const methodHandlers = this.httpHandlerGroups[request.method];
1373
1339
  const requestURL = excludeURLParams_default(new URL(request.url)).toString();
1374
1340
  let matchedSomeInterceptor = false;
@@ -1379,7 +1345,7 @@ var InterceptorServer = class {
1379
1345
  continue;
1380
1346
  }
1381
1347
  matchedSomeInterceptor = true;
1382
- const { response: serializedResponse } = await webSocketServer.request(
1348
+ const { response: serializedResponse } = await this.webSocketServerOrThrow.request(
1383
1349
  "interceptors/responses/create",
1384
1350
  { handlerId: handler.id, request },
1385
1351
  { sockets: [handler.socket] }
@@ -1403,11 +1369,10 @@ var InterceptorServer = class {
1403
1369
  }
1404
1370
  }
1405
1371
  async logUnhandledRequestIfNecessary(request, serializedRequest) {
1406
- const webSocketServer = this.webSocketServer();
1407
1372
  const handler = this.findHttpHandlerByRequestBaseURL(request);
1408
1373
  if (handler) {
1409
1374
  try {
1410
- const { wasLogged: wasRequestLoggedByRemoteInterceptor } = await webSocketServer.request(
1375
+ const { wasLogged: wasRequestLoggedByRemoteInterceptor } = await this.webSocketServerOrThrow.request(
1411
1376
  "interceptors/responses/unhandled",
1412
1377
  { request: serializedRequest },
1413
1378
  { sockets: [handler.socket] }
@@ -1422,7 +1387,7 @@ var InterceptorServer = class {
1422
1387
  }
1423
1388
  }
1424
1389
  }
1425
- if (!this._logUnhandledRequests) {
1390
+ if (!this.logUnhandledRequests) {
1426
1391
  return;
1427
1392
  }
1428
1393
  await HttpInterceptorWorker_default.logUnhandledRequestWarning(request, "reject");
@@ -1494,5 +1459,5 @@ exports.NotStartedInterceptorServerError_default = NotStartedInterceptorServerEr
1494
1459
  exports.createCachedDynamicImport_default = createCachedDynamicImport_default;
1495
1460
  exports.interceptorServer = interceptorServer;
1496
1461
  exports.logWithPrefix = logWithPrefix;
1497
- //# sourceMappingURL=chunk-L6Y4EV6T.js.map
1498
- //# sourceMappingURL=chunk-L6Y4EV6T.js.map
1462
+ //# sourceMappingURL=chunk-CWBDZYUG.js.map
1463
+ //# sourceMappingURL=chunk-CWBDZYUG.js.map