@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/{chunk-6YD4TBYJ.js → chunk-UJTX22M5.js} +60 -18
- package/dist/chunk-UJTX22M5.js.map +1 -0
- package/dist/{chunk-MCLPYYCY.mjs → chunk-V5VFQ6WK.mjs} +60 -18
- package/dist/chunk-V5VFQ6WK.mjs.map +1 -0
- package/dist/cli.js +16 -16
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2 -2
- package/dist/cli.mjs.map +1 -1
- package/dist/http.js +17 -5
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +17 -5
- package/dist/http.mjs.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.mjs +1 -1
- package/package.json +1 -1
- package/src/utils/webSocket/WebSocketHandler.ts +8 -1
- package/src/utils/webSocket/WebSocketServer.ts +58 -12
- package/src/utils/webSocket/constants.ts +7 -0
- package/src/utils/webSocket/types.ts +1 -2
- package/src/utils/webSocket.ts +2 -2
- package/dist/chunk-6YD4TBYJ.js.map +0 -1
- package/dist/chunk-MCLPYYCY.mjs.map +0 -1
package/dist/server.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
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
|
|
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
|
|
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
|
|
18
|
+
get: function () { return chunkUJTX22M5_js.NotRunningInterceptorServerError_default; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "RunningInterceptorServerError", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkUJTX22M5_js.RunningInterceptorServerError_default; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "createInterceptorServer", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
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-
|
|
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
|
@@ -64,7 +64,12 @@ abstract class WebSocketHandler<Schema extends WebSocketSchema> {
|
|
|
64
64
|
};
|
|
65
65
|
socket.addEventListener('message', handleSocketMessage);
|
|
66
66
|
|
|
67
|
-
|
|
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
|
-
|
|
58
|
-
const result = await this.authenticate(socket, request);
|
|
70
|
+
socket.pause();
|
|
59
71
|
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
67
|
-
|
|
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
|
-
|
|
83
|
-
await super.closeClientSockets();
|
|
122
|
+
this.isStopping = true;
|
|
84
123
|
|
|
85
|
-
|
|
124
|
+
try {
|
|
125
|
+
super.offAny();
|
|
126
|
+
await super.closeClientSockets();
|
|
86
127
|
|
|
87
|
-
|
|
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/
|
|
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> {
|
package/src/utils/webSocket.ts
CHANGED
|
@@ -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 ===
|
|
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. */
|