@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
@@ -12,7 +12,7 @@ import {
12
12
  HttpInterceptorResponse,
13
13
  HttpRequestHandlerResponseDeclaration,
14
14
  HttpRequestHandlerResponseDeclarationFactory,
15
- TrackedHttpInterceptorRequest,
15
+ InterceptedHttpInterceptorRequest,
16
16
  } from './types/requests';
17
17
  import { HttpRequestHandlerRestriction } from './types/restrictions';
18
18
 
@@ -27,7 +27,7 @@ class RemoteHttpRequestHandler<
27
27
  {
28
28
  readonly type = 'remote';
29
29
 
30
- private _client: HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
30
+ client: HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
31
31
 
32
32
  private syncPromises: Promise<unknown>[] = [];
33
33
 
@@ -35,7 +35,7 @@ class RemoteHttpRequestHandler<
35
35
  private synced: this;
36
36
 
37
37
  constructor(interceptor: HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>, method: Method, path: Path) {
38
- this._client = new HttpRequestHandlerClient(interceptor, method, path, this);
38
+ this.client = new HttpRequestHandlerClient(interceptor, method, path, this);
39
39
  this.unsynced = this;
40
40
  this.synced = this.createSyncedProxy();
41
41
  }
@@ -62,20 +62,16 @@ class RemoteHttpRequestHandler<
62
62
  return PENDING_PROPERTIES.has(property);
63
63
  }
64
64
 
65
- client() {
66
- return this._client;
65
+ get method() {
66
+ return this.client.method;
67
67
  }
68
68
 
69
- method() {
70
- return this._client.method();
71
- }
72
-
73
- path() {
74
- return this._client.path();
69
+ get path() {
70
+ return this.client.path;
75
71
  }
76
72
 
77
73
  with(restriction: HttpRequestHandlerRestriction<Schema, Method, Path>): this {
78
- this._client.with(restriction);
74
+ this.client.with(restriction);
79
75
  return this.unsynced;
80
76
  }
81
77
 
@@ -85,19 +81,19 @@ class RemoteHttpRequestHandler<
85
81
  | HttpRequestHandlerResponseDeclarationFactory<Path, Default<Schema[Path][Method]>, NewStatusCode>,
86
82
  ): RemoteHttpRequestHandler<Schema, Method, Path, NewStatusCode> {
87
83
  const newUnsyncedThis = this.unsynced as unknown as RemoteHttpRequestHandler<Schema, Method, Path, NewStatusCode>;
88
- newUnsyncedThis._client.respond(declaration);
84
+ newUnsyncedThis.client.respond(declaration);
89
85
  return newUnsyncedThis;
90
86
  }
91
87
 
92
88
  times(minNumberOfRequests: number, maxNumberOfRequests?: number): this {
93
- this._client.times(minNumberOfRequests, maxNumberOfRequests);
89
+ this.client.times(minNumberOfRequests, maxNumberOfRequests);
94
90
  return this;
95
91
  }
96
92
 
97
93
  async checkTimes() {
98
94
  return new Promise<void>((resolve, reject) => {
99
95
  try {
100
- this._client.checkTimes();
96
+ this.client.checkTimes();
101
97
  resolve();
102
98
  } catch (error) {
103
99
  reject(error);
@@ -106,42 +102,36 @@ class RemoteHttpRequestHandler<
106
102
  }
107
103
 
108
104
  clear(): this {
109
- this._client.clear();
105
+ this.client.clear();
110
106
  return this.unsynced;
111
107
  }
112
108
 
113
- requests(): Promise<readonly TrackedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[]> {
114
- return new Promise((resolve, reject) => {
115
- try {
116
- resolve(this._client.requests());
117
- } catch (error) {
118
- reject(error);
119
- }
120
- });
109
+ get requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
110
+ return this.client.requests;
121
111
  }
122
112
 
123
113
  matchesRequest(request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>): Promise<boolean> {
124
- return this._client.matchesRequest(request);
114
+ return this.client.matchesRequest(request);
125
115
  }
126
116
 
127
117
  async applyResponseDeclaration(
128
118
  request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>,
129
119
  ): Promise<HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, StatusCode>> {
130
- return this._client.applyResponseDeclaration(request);
120
+ return this.client.applyResponseDeclaration(request);
131
121
  }
132
122
 
133
123
  saveInterceptedRequest(
134
124
  request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>,
135
125
  response: HttpInterceptorResponse<Default<Schema[Path][Method]>, StatusCode>,
136
126
  ) {
137
- this._client.saveInterceptedRequest(request, response);
127
+ this.client.saveInterceptedRequest(request, response);
138
128
  }
139
129
 
140
130
  registerSyncPromise(promise: Promise<unknown>) {
141
131
  this.syncPromises.push(promise);
142
132
  }
143
133
 
144
- isSynced(): boolean {
134
+ get isSynced() {
145
135
  return this.syncPromises.length === 0;
146
136
  }
147
137
 
@@ -162,7 +152,7 @@ class RemoteHttpRequestHandler<
162
152
  .then(() => {
163
153
  this.syncPromises = this.syncPromises.filter((promise) => !promisesToWait.has(promise));
164
154
 
165
- return this.isSynced() ? this.synced : this.unsynced;
155
+ return this.isSynced ? this.synced : this.unsynced;
166
156
  })
167
157
  .then(onFulfilled, onRejected);
168
158
  }
@@ -11,7 +11,7 @@ import HttpRequestHandlerClient from '../HttpRequestHandlerClient';
11
11
  import {
12
12
  HttpRequestHandlerResponseDeclaration,
13
13
  HttpRequestHandlerResponseDeclarationFactory,
14
- TrackedHttpInterceptorRequest,
14
+ InterceptedHttpInterceptorRequest,
15
15
  } from './requests';
16
16
  import { HttpRequestHandlerRestriction } from './restrictions';
17
17
 
@@ -30,17 +30,19 @@ export interface HttpRequestHandler<
30
30
  Path extends HttpSchemaPath<Schema, Method>,
31
31
  > {
32
32
  /**
33
- * @returns The method that matches this handler.
34
- * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlermethod `handler.method()` API reference}
33
+ * The method that matches this handler.
34
+ *
35
+ * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlermethod `handler.method` API reference}
35
36
  */
36
- method: () => Method;
37
+ method: Method;
37
38
 
38
39
  /**
39
- * @returns The path that matches this handler. The base URL of the interceptor is not included, but it is used when
40
- * matching requests.
41
- * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerpath `handler.path()` API reference}
40
+ * The path that matches this handler. The base URL of the interceptor is not included, but it is used when matching
41
+ * requests.
42
+ *
43
+ * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerpath `handler.path` API reference}
42
44
  */
43
- path: () => Path;
45
+ path: Path;
44
46
  }
45
47
 
46
48
  export interface InternalHttpRequestHandler<
@@ -49,7 +51,7 @@ export interface InternalHttpRequestHandler<
49
51
  Path extends HttpSchemaPath<Schema, Method>,
50
52
  StatusCode extends HttpStatusCode = never,
51
53
  > extends HttpRequestHandler<Schema, Method, Path> {
52
- client: () => HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
54
+ client: HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
53
55
  }
54
56
 
55
57
  /**
@@ -118,7 +120,7 @@ export interface LocalHttpRequestHandler<
118
120
  * intercepted request in the
119
121
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptormethodpath `interceptor.<method>(path)` API reference}.
120
122
  *
121
- * **IMPORTANT**: To make sure that all expected requests were made, use
123
+ * **Important**: To make sure that all expected requests were made, use
122
124
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
123
125
  * or {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlertimes `handler.times()`}.
124
126
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
@@ -170,18 +172,17 @@ export interface LocalHttpRequestHandler<
170
172
  clear: () => this;
171
173
 
172
174
  /**
173
- * Returns the intercepted requests that matched this handler, along with the responses returned to each of them. This
174
- * is useful for testing that the correct requests were made by your application.
175
+ * The intercepted requests that matched this handler, along with the responses returned to each of them. This is
176
+ * useful for testing that the correct requests were made by your application.
175
177
  *
176
- * **IMPORTANT**: This method can only be used if `saveRequests` was set to `true` when creating the interceptor. See
178
+ * **Important**: This method can only be used if `saveRequests` was set to `true` when creating the interceptor. See
177
179
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
178
180
  * for more information.
179
181
  *
180
- * @returns The intercepted requests.
181
182
  * @throws {DisabledRequestSavingError} If the interceptor was not created with `saveRequests: true`.
182
- * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests()` API reference}
183
+ * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests` API reference}
183
184
  */
184
- requests: () => readonly TrackedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[];
185
+ requests: readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[];
185
186
  }
186
187
 
187
188
  /**
@@ -249,7 +250,7 @@ export interface SyncedRemoteHttpRequestHandler<
249
250
  * intercepted request in the
250
251
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptormethodpath `interceptor.<method>(path)` API reference}.
251
252
  *
252
- * **IMPORTANT**: To make sure that all expected requests were made, use
253
+ * **Important**: To make sure that all expected requests were made, use
253
254
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
254
255
  * or
255
256
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerchecktimes `handler.checkTimes()`}.
@@ -306,18 +307,17 @@ export interface SyncedRemoteHttpRequestHandler<
306
307
  clear: () => PendingRemoteHttpRequestHandler<Schema, Method, Path, StatusCode>;
307
308
 
308
309
  /**
309
- * Returns the intercepted requests that matched this handler, along with the responses returned to each of them. This
310
- * is useful for testing that the correct requests were made by your application.
310
+ * The intercepted requests that matched this handler, along with the responses returned to each of them. This is
311
+ * useful for testing that the correct requests were made by your application.
311
312
  *
312
- * **IMPORTANT**: This method can only be used if `saveRequests` was set to `true` when creating the interceptor. See
313
+ * **Important**: This method can only be used if `saveRequests` was set to `true` when creating the interceptor. See
313
314
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
314
315
  * for more information.
315
316
  *
316
- * @returns The intercepted requests.
317
317
  * @throws {DisabledRequestSavingError} If the interceptor was not created with `saveRequests: true`.
318
- * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests()` API reference}
318
+ * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests` API reference}
319
319
  */
320
- requests: () => Promise<readonly TrackedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[]>;
320
+ requests: readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[];
321
321
  }
322
322
 
323
323
  /**
@@ -115,7 +115,7 @@ export const HTTP_INTERCEPTOR_RESPONSE_HIDDEN_PROPERTIES = Object.freeze(
115
115
  * A strict representation of an intercepted HTTP request, along with its response. The body, search params and path
116
116
  * params are already parsed by default.
117
117
  */
118
- export interface TrackedHttpInterceptorRequest<
118
+ export interface InterceptedHttpInterceptorRequest<
119
119
  Path extends string,
120
120
  MethodSchema extends HttpMethodSchema,
121
121
  StatusCode extends HttpStatusCode = never,
@@ -34,12 +34,12 @@ interface HttpHandler {
34
34
  }
35
35
 
36
36
  class InterceptorServer implements PublicInterceptorServer {
37
- private _httpServer?: HttpServer;
38
- private _webSocketServer?: WebSocketServer<InterceptorServerWebSocketSchema>;
37
+ private httpServer?: HttpServer;
38
+ private webSocketServer?: WebSocketServer<InterceptorServerWebSocketSchema>;
39
39
 
40
- private _hostname: string;
41
- private _port?: number;
42
- private _logUnhandledRequests: boolean;
40
+ hostname: string;
41
+ port: number | undefined;
42
+ logUnhandledRequests: boolean;
43
43
 
44
44
  private httpHandlerGroups: {
45
45
  [Method in HttpMethod]: HttpHandler[];
@@ -56,87 +56,71 @@ class InterceptorServer implements PublicInterceptorServer {
56
56
  private knownWorkerSockets = new Set<Socket>();
57
57
 
58
58
  constructor(options: InterceptorServerOptions) {
59
- this._hostname = options.hostname ?? 'localhost';
60
- this._port = options.port;
61
- this._logUnhandledRequests = options.logUnhandledRequests ?? DEFAULT_LOG_UNHANDLED_REQUESTS;
59
+ this.hostname = options.hostname ?? 'localhost';
60
+ this.port = options.port;
61
+ this.logUnhandledRequests = options.logUnhandledRequests ?? DEFAULT_LOG_UNHANDLED_REQUESTS;
62
62
  }
63
63
 
64
- hostname() {
65
- return this._hostname;
66
- }
67
-
68
- port() {
69
- return this._port;
70
- }
71
-
72
- logUnhandledRequests() {
73
- return this._logUnhandledRequests;
74
- }
75
-
76
- httpURL() {
77
- if (this._port === undefined) {
64
+ get httpURL() {
65
+ if (this.port === undefined) {
78
66
  return undefined;
79
67
  }
80
- return `http://${this._hostname}:${this._port}`;
68
+ return `http://${this.hostname}:${this.port}`;
81
69
  }
82
70
 
83
- isRunning() {
84
- return !!this._httpServer?.listening && !!this._webSocketServer?.isRunning();
71
+ get isRunning() {
72
+ return !!this.httpServer?.listening && !!this.webSocketServer?.isRunning;
85
73
  }
86
74
 
87
- private httpServer() {
75
+ private get httpServerOrThrow(): HttpServer {
88
76
  /* istanbul ignore if -- @preserve
89
77
  * The HTTP server is initialized before using this method in normal conditions. */
90
- if (!this._httpServer) {
78
+ if (!this.httpServer) {
91
79
  throw new NotStartedInterceptorServerError();
92
80
  }
93
- return this._httpServer;
81
+ return this.httpServer;
94
82
  }
95
83
 
96
- private webSocketServer() {
84
+ private get webSocketServerOrThrow(): WebSocketServer<InterceptorServerWebSocketSchema> {
97
85
  /* istanbul ignore if -- @preserve
98
86
  * The web socket server is initialized before using this method in normal conditions. */
99
- if (!this._webSocketServer) {
87
+ if (!this.webSocketServer) {
100
88
  throw new NotStartedInterceptorServerError();
101
89
  }
102
- return this._webSocketServer;
90
+ return this.webSocketServer;
103
91
  }
104
92
 
105
93
  async start() {
106
- if (this.isRunning()) {
94
+ if (this.isRunning) {
107
95
  return;
108
96
  }
109
97
 
110
- this._httpServer = createServer({
98
+ this.httpServer = createServer({
111
99
  keepAlive: true,
112
100
  joinDuplicateHeaders: true,
113
101
  });
114
102
  await this.startHttpServer();
115
103
 
116
- this._webSocketServer = new WebSocketServer({
117
- httpServer: this._httpServer,
104
+ this.webSocketServer = new WebSocketServer({
105
+ httpServer: this.httpServer,
118
106
  });
119
107
  this.startWebSocketServer();
120
108
  }
121
109
 
122
110
  private async startHttpServer() {
123
- const httpServer = this.httpServer();
124
-
125
- await startHttpServer(httpServer, {
126
- hostname: this._hostname,
127
- port: this._port,
111
+ await startHttpServer(this.httpServerOrThrow, {
112
+ hostname: this.hostname,
113
+ port: this.port,
128
114
  });
129
- this._port = getHttpServerPort(httpServer);
115
+ this.port = getHttpServerPort(this.httpServerOrThrow);
130
116
 
131
- httpServer.on('request', this.handleHttpRequest);
117
+ this.httpServerOrThrow.on('request', this.handleHttpRequest);
132
118
  }
133
119
 
134
120
  private startWebSocketServer() {
135
- const webSocketServer = this.webSocketServer();
136
-
137
- webSocketServer.start();
138
- webSocketServer.onEvent('interceptors/workers/use/commit', this.commitWorker);
139
- webSocketServer.onEvent('interceptors/workers/use/reset', this.resetWorker);
121
+ this.webSocketServerOrThrow.start();
122
+ this.webSocketServerOrThrow.onEvent('interceptors/workers/use/commit', this.commitWorker);
123
+ this.webSocketServerOrThrow.onEvent('interceptors/workers/use/reset', this.resetWorker);
140
124
  }
141
125
 
142
126
  private commitWorker = (
@@ -161,7 +145,7 @@ class InterceptorServer implements PublicInterceptorServer {
161
145
  if (isWorkerNoLongerCommitted) {
162
146
  // When a worker is no longer committed, we should abort all requests that were using it.
163
147
  // This ensures that we only wait for responses from committed worker sockets.
164
- this.webSocketServer().abortSocketMessages([socket]);
148
+ this.webSocketServerOrThrow.abortSocketMessages([socket]);
165
149
  } else {
166
150
  for (const handler of handlersToResetTo) {
167
151
  this.registerHttpHandler(handler, socket);
@@ -209,32 +193,28 @@ class InterceptorServer implements PublicInterceptorServer {
209
193
  }
210
194
  }
211
195
 
212
- stop = async () => {
213
- if (!this.isRunning()) {
196
+ async stop() {
197
+ if (!this.isRunning) {
214
198
  return;
215
199
  }
216
200
 
217
201
  await this.stopWebSocketServer();
218
202
  await this.stopHttpServer();
219
- };
203
+ }
220
204
 
221
205
  private async stopHttpServer() {
222
- const httpServer = this.httpServer();
223
-
224
- await stopHttpServer(httpServer);
225
- httpServer.removeAllListeners();
226
-
227
- this._httpServer = undefined;
206
+ await stopHttpServer(this.httpServerOrThrow);
207
+ this.httpServerOrThrow.removeAllListeners();
208
+ this.httpServer = undefined;
228
209
  }
229
210
 
230
211
  private async stopWebSocketServer() {
231
- const webSocketServer = this.webSocketServer();
212
+ this.webSocketServerOrThrow.offEvent('interceptors/workers/use/commit', this.commitWorker);
213
+ this.webSocketServerOrThrow.offEvent('interceptors/workers/use/reset', this.resetWorker);
232
214
 
233
- webSocketServer.offEvent('interceptors/workers/use/commit', this.commitWorker);
234
- webSocketServer.offEvent('interceptors/workers/use/reset', this.resetWorker);
235
- await webSocketServer.stop();
215
+ await this.webSocketServerOrThrow.stop();
236
216
 
237
- this._webSocketServer = undefined;
217
+ this.webSocketServer = undefined;
238
218
  }
239
219
 
240
220
  private handleHttpRequest = async (nodeRequest: IncomingMessage, nodeResponse: ServerResponse) => {
@@ -278,7 +258,6 @@ class InterceptorServer implements PublicInterceptorServer {
278
258
  };
279
259
 
280
260
  private async createResponseForRequest(request: SerializedHttpRequest) {
281
- const webSocketServer = this.webSocketServer();
282
261
  const methodHandlers = this.httpHandlerGroups[request.method as HttpMethod];
283
262
 
284
263
  const requestURL = excludeURLParams(new URL(request.url)).toString();
@@ -295,7 +274,7 @@ class InterceptorServer implements PublicInterceptorServer {
295
274
 
296
275
  matchedSomeInterceptor = true;
297
276
 
298
- const { response: serializedResponse } = await webSocketServer.request(
277
+ const { response: serializedResponse } = await this.webSocketServerOrThrow.request(
299
278
  'interceptors/responses/create',
300
279
  { handlerId: handler.id, request },
301
280
  { sockets: [handler.socket] },
@@ -329,13 +308,11 @@ class InterceptorServer implements PublicInterceptorServer {
329
308
  }
330
309
 
331
310
  private async logUnhandledRequestIfNecessary(request: HttpRequest, serializedRequest: SerializedHttpRequest) {
332
- const webSocketServer = this.webSocketServer();
333
-
334
311
  const handler = this.findHttpHandlerByRequestBaseURL(request);
335
312
 
336
313
  if (handler) {
337
314
  try {
338
- const { wasLogged: wasRequestLoggedByRemoteInterceptor } = await webSocketServer.request(
315
+ const { wasLogged: wasRequestLoggedByRemoteInterceptor } = await this.webSocketServerOrThrow.request(
339
316
  'interceptors/responses/unhandled',
340
317
  { request: serializedRequest },
341
318
  { sockets: [handler.socket] },
@@ -357,7 +334,7 @@ class InterceptorServer implements PublicInterceptorServer {
357
334
  }
358
335
  }
359
336
 
360
- if (!this._logUnhandledRequests) {
337
+ if (!this.logUnhandledRequests) {
361
338
  return;
362
339
  }
363
340
 
@@ -6,35 +6,40 @@
6
6
  */
7
7
  export interface InterceptorServer {
8
8
  /**
9
- * @returns The hostname of the server.
9
+ * The hostname of the server.
10
+ *
10
11
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
11
12
  */
12
- hostname: () => string;
13
+ hostname: string;
13
14
 
14
15
  /**
15
- * @returns The port of the server.
16
+ * The port of the server.
17
+ *
16
18
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
17
19
  */
18
- port: () => number | undefined;
20
+ port: number | undefined;
19
21
 
20
22
  /**
23
+ * Whether to log warnings about unhandled requests to the console.
24
+ *
21
25
  * @default true
22
- * @returns Whether to log warnings about unhandled requests to the console.
23
26
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
24
27
  */
25
- logUnhandledRequests: () => boolean;
28
+ logUnhandledRequests: boolean;
26
29
 
27
30
  /**
28
- * @returns The HTTP URL of the server.
31
+ * The HTTP URL of the server.
32
+ *
29
33
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
30
34
  */
31
- httpURL: () => string | undefined;
35
+ httpURL: string | undefined;
32
36
 
33
37
  /**
34
- * @returns Whether the server is running.
38
+ * Whether the server is running.
39
+ *
35
40
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
36
41
  */
37
- isRunning: () => boolean;
42
+ isRunning: boolean;
38
43
 
39
44
  /**
40
45
  * Starts the server.
@@ -27,7 +27,7 @@ class WebSocketClient<Schema extends WebSocket.ServiceSchema> extends WebSocketH
27
27
  validateURLProtocol(this.url, SUPPORTED_WEB_SOCKET_PROTOCOLS);
28
28
  }
29
29
 
30
- isRunning() {
30
+ get isRunning() {
31
31
  return this.socket !== undefined && this.socket.readyState === this.socket.OPEN;
32
32
  }
33
33
 
@@ -18,8 +18,8 @@ import { WebSocket } from './types';
18
18
  abstract class WebSocketHandler<Schema extends WebSocket.ServiceSchema> {
19
19
  private sockets = new Set<ClientSocket>();
20
20
 
21
- private _socketTimeout: number;
22
- private _messageTimeout: number;
21
+ socketTimeout: number;
22
+ messageTimeout: number;
23
23
 
24
24
  private channelListeners: {
25
25
  [Channel in WebSocket.ServiceChannel<Schema>]?: {
@@ -33,22 +33,14 @@ abstract class WebSocketHandler<Schema extends WebSocket.ServiceSchema> {
33
33
  };
34
34
 
35
35
  protected constructor(options: { socketTimeout?: number; messageTimeout?: number }) {
36
- this._socketTimeout = options.socketTimeout ?? DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT;
37
- this._messageTimeout = options.messageTimeout ?? DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT;
36
+ this.socketTimeout = options.socketTimeout ?? DEFAULT_WEB_SOCKET_LIFECYCLE_TIMEOUT;
37
+ this.messageTimeout = options.messageTimeout ?? DEFAULT_WEB_SOCKET_MESSAGE_TIMEOUT;
38
38
  }
39
39
 
40
- abstract isRunning(): boolean;
41
-
42
- socketTimeout() {
43
- return this._socketTimeout;
44
- }
45
-
46
- messageTimeout() {
47
- return this._messageTimeout;
48
- }
40
+ abstract isRunning: boolean;
49
41
 
50
42
  protected async registerSocket(socket: ClientSocket) {
51
- const openPromise = waitForOpenClientSocket(socket, { timeout: this._socketTimeout });
43
+ const openPromise = waitForOpenClientSocket(socket, { timeout: this.socketTimeout });
52
44
 
53
45
  const handleSocketMessage = async (rawMessage: ClientSocket.MessageEvent) => {
54
46
  await this.handleSocketMessage(socket, rawMessage);
@@ -173,7 +165,7 @@ abstract class WebSocketHandler<Schema extends WebSocket.ServiceSchema> {
173
165
 
174
166
  protected async closeClientSockets(sockets: Collection<ClientSocket> = this.sockets) {
175
167
  const closingPromises = Array.from(sockets, async (socket) => {
176
- await closeClientSocket(socket, { timeout: this._socketTimeout });
168
+ await closeClientSocket(socket, { timeout: this.socketTimeout });
177
169
  });
178
170
  await Promise.all(closingPromises);
179
171
  }
@@ -232,9 +224,9 @@ abstract class WebSocketHandler<Schema extends WebSocket.ServiceSchema> {
232
224
  this.offReply(channel, replyListener); // eslint-disable-line @typescript-eslint/no-use-before-define
233
225
  this.offAbortSocketMessages(sockets, abortListener); // eslint-disable-line @typescript-eslint/no-use-before-define
234
226
 
235
- const timeoutError = new WebSocketMessageTimeoutError(this._messageTimeout);
227
+ const timeoutError = new WebSocketMessageTimeoutError(this.messageTimeout);
236
228
  reject(timeoutError);
237
- }, this._messageTimeout);
229
+ }, this.messageTimeout);
238
230
 
239
231
  const abortListener = this.onAbortSocketMessages(sockets, (error) => {
240
232
  clearTimeout(replyTimeout);
@@ -274,7 +266,7 @@ abstract class WebSocketHandler<Schema extends WebSocket.ServiceSchema> {
274
266
  const reply = await this.createReplyMessage(request, replyData);
275
267
 
276
268
  // If this handler received a request and was stopped before responding, discard any pending replies.
277
- if (this.isRunning()) {
269
+ if (this.isRunning) {
278
270
  this.sendMessage(reply, options.sockets);
279
271
  }
280
272
  }
@@ -298,7 +290,7 @@ abstract class WebSocketHandler<Schema extends WebSocket.ServiceSchema> {
298
290
  message: WebSocket.ServiceMessage<Schema, Channel>,
299
291
  sockets: Collection<ClientSocket> = this.sockets,
300
292
  ) {
301
- if (!this.isRunning()) {
293
+ if (!this.isRunning) {
302
294
  throw new NotStartedWebSocketHandlerError();
303
295
  }
304
296
 
@@ -27,12 +27,12 @@ class WebSocketServer<Schema extends WebSocket.ServiceSchema> extends WebSocketH
27
27
  this.httpServer = options.httpServer;
28
28
  }
29
29
 
30
- isRunning() {
30
+ get isRunning() {
31
31
  return this.webSocketServer !== undefined;
32
32
  }
33
33
 
34
34
  start() {
35
- if (this.isRunning()) {
35
+ if (this.isRunning) {
36
36
  return;
37
37
  }
38
38
 
@@ -54,7 +54,7 @@ class WebSocketServer<Schema extends WebSocket.ServiceSchema> extends WebSocketH
54
54
  }
55
55
 
56
56
  async stop() {
57
- if (!this.webSocketServer || !this.isRunning()) {
57
+ if (!this.webSocketServer || !this.isRunning) {
58
58
  return;
59
59
  }
60
60
 
@@ -62,7 +62,7 @@ class WebSocketServer<Schema extends WebSocket.ServiceSchema> extends WebSocketH
62
62
  super.abortSocketMessages();
63
63
  await super.closeClientSockets();
64
64
 
65
- await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout() });
65
+ await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout });
66
66
  this.webSocketServer.removeAllListeners();
67
67
  this.webSocketServer = undefined;
68
68
  }