@zimic/interceptor 1.5.2-canary.1 → 1.5.2-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 chunk6YD4TBYJ_js = require('./chunk-6YD4TBYJ.js');
3
+ var chunkUJTX22M5_js = require('./chunk-UJTX22M5.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 chunk6YD4TBYJ_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
10
+ get: function () { return chunkUJTX22M5_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
11
11
  });
12
12
  Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
13
13
  enumerable: true,
14
- get: function () { return chunk6YD4TBYJ_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
14
+ get: function () { return chunkUJTX22M5_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
15
15
  });
16
16
  Object.defineProperty(exports, "NotRunningInterceptorServerError", {
17
17
  enumerable: true,
18
- get: function () { return chunk6YD4TBYJ_js.NotRunningInterceptorServerError_default; }
18
+ get: function () { return chunkUJTX22M5_js.NotRunningInterceptorServerError_default; }
19
19
  });
20
20
  Object.defineProperty(exports, "RunningInterceptorServerError", {
21
21
  enumerable: true,
22
- get: function () { return chunk6YD4TBYJ_js.RunningInterceptorServerError_default; }
22
+ get: function () { return chunkUJTX22M5_js.RunningInterceptorServerError_default; }
23
23
  });
24
24
  Object.defineProperty(exports, "createInterceptorServer", {
25
25
  enumerable: true,
26
- get: function () { return chunk6YD4TBYJ_js.createInterceptorServer; }
26
+ get: function () { return chunkUJTX22M5_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-MCLPYYCY.mjs';
1
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-V5VFQ6WK.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.5.2-canary.1",
17
+ "version": "1.5.2-canary.2",
18
18
  "homepage": "https://zimic.dev/docs/interceptor",
19
19
  "repository": {
20
20
  "type": "git",
@@ -64,7 +64,12 @@ abstract class WebSocketHandler<Schema extends WebSocketSchema> {
64
64
  };
65
65
  socket.addEventListener('message', handleSocketMessage);
66
66
 
67
- await openPromise;
67
+ try {
68
+ await openPromise;
69
+ } catch (error) {
70
+ socket.removeEventListener('message', handleSocketMessage);
71
+ throw error;
72
+ }
68
73
 
69
74
  /* istanbul ignore next -- @preserve
70
75
  * It is difficult to reliably simulate socket errors in tests. */
@@ -87,6 +92,8 @@ abstract class WebSocketHandler<Schema extends WebSocketSchema> {
87
92
  socket.addEventListener('close', handleSocketClose);
88
93
 
89
94
  this.sockets.add(socket);
95
+
96
+ return handleSocketClose;
90
97
  }
91
98
 
92
99
  private handleSocketMessage = async (socket: ClientSocket, rawMessage: ClientSocket.MessageEvent) => {
@@ -4,29 +4,41 @@ import ClientSocket from 'isomorphic-ws';
4
4
 
5
5
  import { closeServerSocket } from '@/utils/webSocket';
6
6
 
7
- import { WebSocketControlMessage } from './constants';
7
+ import { WEB_SOCKET_CLOSE_CODES, WebSocketControlMessage } from './constants';
8
8
  import { WebSocketSchema } from './types';
9
9
  import WebSocketHandler from './WebSocketHandler';
10
10
 
11
11
  const { WebSocketServer: ServerSocket } = ClientSocket;
12
12
 
13
+ function isSocketOpen(socket: ClientSocket) {
14
+ return socket.readyState === socket.OPEN;
15
+ }
16
+
13
17
  export type WebSocketServerAuthenticate = (
14
18
  socket: ClientSocket,
15
19
  request: IncomingMessage,
16
20
  ) => PossiblePromise<{ isValid: true } | { isValid: false; message: string }>;
17
21
 
22
+ export type WebSocketServerConnectionHandler = (
23
+ socket: ClientSocket,
24
+ request: IncomingMessage,
25
+ ) => PossiblePromise<{ handled: boolean }>;
26
+
18
27
  interface WebSocketServerOptions {
19
28
  httpServer: HttpServer;
20
29
  socketTimeout?: number;
21
30
  messageTimeout?: number;
22
31
  authenticate?: WebSocketServerAuthenticate;
32
+ handleConnection?: WebSocketServerConnectionHandler;
23
33
  }
24
34
 
25
35
  class WebSocketServer<Schema extends WebSocketSchema> extends WebSocketHandler<Schema> {
26
36
  private webSocketServer?: InstanceType<typeof ServerSocket>;
37
+ private isStopping = false;
27
38
 
28
39
  private httpServer: HttpServer;
29
40
  private authenticate?: WebSocketServerOptions['authenticate'];
41
+ private handleConnection?: WebSocketServerOptions['handleConnection'];
30
42
 
31
43
  constructor(options: WebSocketServerOptions) {
32
44
  super({
@@ -36,6 +48,7 @@ class WebSocketServer<Schema extends WebSocketSchema> extends WebSocketHandler<S
36
48
 
37
49
  this.httpServer = options.httpServer;
38
50
  this.authenticate = options.authenticate;
51
+ this.handleConnection = options.handleConnection;
39
52
  }
40
53
 
41
54
  get isRunning() {
@@ -54,19 +67,46 @@ class WebSocketServer<Schema extends WebSocketSchema> extends WebSocketHandler<S
54
67
  });
55
68
 
56
69
  webSocketServer.on('connection', async (socket, request) => {
57
- if (this.authenticate) {
58
- const result = await this.authenticate(socket, request);
70
+ socket.pause();
59
71
 
60
- if (!result.isValid) {
61
- socket.close(1008, result.message);
72
+ try {
73
+ if (this.authenticate) {
74
+ const result = await this.authenticate(socket, request);
75
+
76
+ if (!isSocketOpen(socket)) {
77
+ return;
78
+ }
79
+
80
+ if (!result.isValid) {
81
+ socket.resume();
82
+ socket.close(WEB_SOCKET_CLOSE_CODES.POLICY_VIOLATION, result.message);
83
+ return;
84
+ }
85
+ }
86
+
87
+ const connectionResult = await this.handleConnection?.(socket, request);
88
+
89
+ if (!isSocketOpen(socket)) {
62
90
  return;
63
91
  }
64
- }
65
92
 
66
- try {
67
- await super.registerSocket(socket);
93
+ if (connectionResult?.handled) {
94
+ return;
95
+ }
96
+
97
+ const unregisterSocket = await super.registerSocket(socket);
98
+
99
+ if (this.isStopping || !isSocketOpen(socket)) {
100
+ unregisterSocket();
101
+ return;
102
+ }
103
+
104
+ socket.resume();
68
105
  socket.send('socket:auth:valid' satisfies WebSocketControlMessage);
69
106
  } catch (error) {
107
+ socket.resume();
108
+ socket.close(WEB_SOCKET_CLOSE_CODES.INTERNAL_ERROR);
109
+
70
110
  webSocketServer.emit('error', error);
71
111
  }
72
112
  });
@@ -79,12 +119,18 @@ class WebSocketServer<Schema extends WebSocketSchema> extends WebSocketHandler<S
79
119
  return;
80
120
  }
81
121
 
82
- super.offAny();
83
- await super.closeClientSockets();
122
+ this.isStopping = true;
84
123
 
85
- await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout });
124
+ try {
125
+ super.offAny();
126
+ await super.closeClientSockets();
86
127
 
87
- this.webSocketServer = undefined;
128
+ await closeServerSocket(this.webSocketServer, { timeout: this.socketTimeout });
129
+
130
+ this.webSocketServer = undefined;
131
+ } finally {
132
+ this.isStopping = false;
133
+ }
88
134
  }
89
135
  }
90
136
 
@@ -1,2 +1,9 @@
1
1
  export const WEB_SOCKET_CONTROL_MESSAGES = Object.freeze(['socket:auth:valid'] as const);
2
2
  export type WebSocketControlMessage = (typeof WEB_SOCKET_CONTROL_MESSAGES)[number];
3
+
4
+ export const WEB_SOCKET_CLOSE_CODES = Object.freeze({
5
+ DEFAULT: 1000,
6
+ PROTOCOL_ERROR: 1002,
7
+ POLICY_VIOLATION: 1008,
8
+ INTERNAL_ERROR: 1011,
9
+ } as const);
@@ -1,5 +1,4 @@
1
- import { JSONValue } from '@zimic/http';
2
- import { PossiblePromise } from '@zimic/utils/types';
1
+ import type { JSONValue, PossiblePromise } from '@zimic/utils/types';
3
2
  import type { WebSocket as ClientSocket } from 'isomorphic-ws';
4
3
 
5
4
  export interface WebSocketChannelFrame<Channel extends string> {
@@ -1,6 +1,6 @@
1
1
  import ClientSocket, { type WebSocketServer as ServerSocket } from 'isomorphic-ws';
2
2
 
3
- import { WebSocketControlMessage } from '@/utils/webSocket/constants';
3
+ import { WEB_SOCKET_CLOSE_CODES, WebSocketControlMessage } from '@/utils/webSocket/constants';
4
4
  import UnauthorizedWebSocketConnectionError from '@/utils/webSocket/errors/UnauthorizedWebSocketConnectionError';
5
5
 
6
6
  class WebSocketTimeoutError extends Error {}
@@ -65,7 +65,7 @@ export async function waitForOpenClientSocket(
65
65
  }
66
66
 
67
67
  function handleClose(event: ClientSocket.CloseEvent) {
68
- const isUnauthorized = event.code === 1008;
68
+ const isUnauthorized = event.code === WEB_SOCKET_CLOSE_CODES.POLICY_VIOLATION;
69
69
 
70
70
  /* istanbul ignore else -- @preserve
71
71
  * An unauthorized close event is the only one we expect to happen here. */