@depup/mswjs__interceptors 0.43.2-depup.0 → 0.45.1-depup.0
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/README.md +2 -2
- package/changes.json +1 -1
- package/lib/browser/interceptors/WebSocket/index.d.ts +277 -70
- package/lib/browser/interceptors/WebSocket/index.js +240 -33
- package/lib/browser/interceptors/WebSocket/index.js.map +1 -1
- package/lib/node/interceptors/ClientRequest/index.js +1 -1
- package/lib/node/interceptors/WebSocket/index.d.ts +277 -70
- package/lib/node/interceptors/WebSocket/index.js +240 -33
- package/lib/node/interceptors/WebSocket/index.js.map +1 -1
- package/lib/node/interceptors/XMLHttpRequest/node.js +1 -1
- package/lib/node/interceptors/fetch/node.js +1 -1
- package/lib/node/interceptors/http/index.js +1 -1
- package/lib/node/remote-http-interceptor.js +1 -1
- package/lib/node/{source-Bs0HXaSq.js → source-CuU4Eu3O.js} +89 -10
- package/lib/node/source-CuU4Eu3O.js.map +1 -0
- package/package.json +3 -3
- package/src/events/websocket.ts +16 -11
- package/src/interceptors/WebSocket/index.ts +147 -12
- package/src/interceptors/WebSocket/web-socket-client-connection.ts +120 -26
- package/src/interceptors/WebSocket/web-socket-extension.test.ts +109 -0
- package/src/interceptors/WebSocket/web-socket-extension.ts +243 -0
- package/src/interceptors/WebSocket/web-socket-override.ts +4 -3
- package/src/interceptors/WebSocket/web-socket-server-connection.ts +133 -39
- package/src/interceptors/http/source.ts +126 -12
- package/lib/node/source-Bs0HXaSq.js.map +0 -1
package/README.md
CHANGED
|
@@ -13,8 +13,8 @@ npm install @depup/mswjs__interceptors
|
|
|
13
13
|
|
|
14
14
|
| Field | Value |
|
|
15
15
|
|-------|-------|
|
|
16
|
-
| Original | [@mswjs/interceptors](https://www.npmjs.com/package/@mswjs/interceptors) @ 0.
|
|
17
|
-
| Processed | 2026-09-
|
|
16
|
+
| Original | [@mswjs/interceptors](https://www.npmjs.com/package/@mswjs/interceptors) @ 0.45.1 |
|
|
17
|
+
| Processed | 2026-09-24 |
|
|
18
18
|
| Smoke test | passed |
|
|
19
19
|
| Deps updated | 0 |
|
|
20
20
|
|
package/changes.json
CHANGED
|
@@ -10,25 +10,25 @@ interface WebSocketConnectionInfo {
|
|
|
10
10
|
*/
|
|
11
11
|
protocols: string | Array<string> | undefined;
|
|
12
12
|
}
|
|
13
|
-
interface WebSocketConnectionEventData {
|
|
13
|
+
interface WebSocketConnectionEventData<Message = WebSocketData> {
|
|
14
14
|
/**
|
|
15
15
|
* The incoming WebSocket client connection.
|
|
16
16
|
*/
|
|
17
|
-
client: WebSocketClientConnection
|
|
17
|
+
client: WebSocketClientConnection<Message>;
|
|
18
18
|
/**
|
|
19
19
|
* The original WebSocket server connection.
|
|
20
20
|
*/
|
|
21
|
-
server: WebSocketServerConnection
|
|
21
|
+
server: WebSocketServerConnection<Message>;
|
|
22
22
|
info: WebSocketConnectionInfo;
|
|
23
23
|
}
|
|
24
|
-
declare class WebSocketConnectionEvent<
|
|
25
|
-
client: WebSocketClientConnection
|
|
26
|
-
server: WebSocketServerConnection
|
|
24
|
+
declare class WebSocketConnectionEvent<Message = WebSocketData> extends TypedEvent<WebSocketConnectionEventData<Message>, void, 'connection'> {
|
|
25
|
+
client: WebSocketClientConnection<Message>;
|
|
26
|
+
server: WebSocketServerConnection<Message>;
|
|
27
27
|
info: WebSocketConnectionInfo;
|
|
28
|
-
constructor(data:
|
|
28
|
+
constructor(data: WebSocketConnectionEventData<Message>);
|
|
29
29
|
}
|
|
30
|
-
type WebSocketEventMap$1 = {
|
|
31
|
-
connection: WebSocketConnectionEvent;
|
|
30
|
+
type WebSocketEventMap$1<Message = WebSocketData, Extension = {}> = {
|
|
31
|
+
connection: WebSocketConnectionEvent<Message> & Extension;
|
|
32
32
|
};
|
|
33
33
|
//#endregion
|
|
34
34
|
//#region src/interceptors/WebSocket/utils/events.d.ts
|
|
@@ -97,7 +97,7 @@ interface WebSocketTransport {
|
|
|
97
97
|
}
|
|
98
98
|
//#endregion
|
|
99
99
|
//#region src/interceptors/WebSocket/web-socket-override.d.ts
|
|
100
|
-
type WebSocketEventListener<EventType extends
|
|
100
|
+
type WebSocketEventListener<EventType extends Event = Event> = (this: WebSocket, event: EventType) => void;
|
|
101
101
|
declare const kPassthroughPromise: unique symbol;
|
|
102
102
|
declare const kOnSend: unique symbol;
|
|
103
103
|
declare const kClose: unique symbol;
|
|
@@ -142,52 +142,6 @@ declare class WebSocketOverride extends EventTarget implements WebSocket {
|
|
|
142
142
|
removeEventListener<K extends keyof WebSocketEventMap>(type: K, callback: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
|
|
143
143
|
}
|
|
144
144
|
//#endregion
|
|
145
|
-
//#region src/interceptors/WebSocket/web-socket-client-connection.d.ts
|
|
146
|
-
declare const kEmitter$1: unique symbol;
|
|
147
|
-
interface WebSocketClientEventMap {
|
|
148
|
-
message: MessageEvent<WebSocketData>;
|
|
149
|
-
close: CloseEvent;
|
|
150
|
-
}
|
|
151
|
-
declare abstract class WebSocketClientConnectionProtocol {
|
|
152
|
-
abstract id: string;
|
|
153
|
-
abstract url: URL;
|
|
154
|
-
abstract send(data: WebSocketData): void;
|
|
155
|
-
abstract close(code?: number, reason?: string): void;
|
|
156
|
-
abstract addEventListener<EventType extends keyof WebSocketClientEventMap>(type: EventType, listener: WebSocketEventListener<WebSocketClientEventMap[EventType]>, options?: AddEventListenerOptions | boolean): void;
|
|
157
|
-
abstract removeEventListener<EventType extends keyof WebSocketClientEventMap>(event: EventType, listener: WebSocketEventListener<WebSocketClientEventMap[EventType]>, options?: EventListenerOptions | boolean): void;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* The WebSocket client instance represents an incoming
|
|
161
|
-
* client connection. The user can control the connection,
|
|
162
|
-
* send and receive events.
|
|
163
|
-
*/
|
|
164
|
-
declare class WebSocketClientConnection implements WebSocketClientConnectionProtocol {
|
|
165
|
-
readonly socket: WebSocket;
|
|
166
|
-
private readonly transport;
|
|
167
|
-
readonly id: string;
|
|
168
|
-
readonly url: URL;
|
|
169
|
-
private [kEmitter$1];
|
|
170
|
-
constructor(socket: WebSocket, transport: WebSocketTransport);
|
|
171
|
-
/**
|
|
172
|
-
* Listen for the outgoing events from the connected WebSocket client.
|
|
173
|
-
*/
|
|
174
|
-
addEventListener<EventType extends keyof WebSocketClientEventMap>(type: EventType, listener: WebSocketEventListener<WebSocketClientEventMap[EventType]>, options?: AddEventListenerOptions | boolean): void;
|
|
175
|
-
/**
|
|
176
|
-
* Removes the listener for the given event.
|
|
177
|
-
*/
|
|
178
|
-
removeEventListener<EventType extends keyof WebSocketClientEventMap>(event: EventType, listener: WebSocketEventListener<WebSocketClientEventMap[EventType]>, options?: EventListenerOptions | boolean): void;
|
|
179
|
-
/**
|
|
180
|
-
* Send data to the connected client.
|
|
181
|
-
*/
|
|
182
|
-
send(data: WebSocketData): void;
|
|
183
|
-
/**
|
|
184
|
-
* Close the WebSocket connection.
|
|
185
|
-
* @param {number} code A status code (see https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1).
|
|
186
|
-
* @param {string} reason A custom connection close reason.
|
|
187
|
-
*/
|
|
188
|
-
close(code?: number, reason?: string): void;
|
|
189
|
-
}
|
|
190
|
-
//#endregion
|
|
191
145
|
//#region src/interceptors/WebSocket/web-socket-class-transport.d.ts
|
|
192
146
|
/**
|
|
193
147
|
* Abstraction over the given mock `WebSocket` instance that allows
|
|
@@ -203,27 +157,34 @@ declare class WebSocketClassTransport extends EventTarget implements WebSocketTr
|
|
|
203
157
|
}
|
|
204
158
|
//#endregion
|
|
205
159
|
//#region src/interceptors/WebSocket/web-socket-server-connection.d.ts
|
|
206
|
-
declare const kEmitter: unique symbol;
|
|
160
|
+
declare const kEmitter$1: unique symbol;
|
|
207
161
|
declare const kSend: unique symbol;
|
|
208
|
-
interface WebSocketServerEventMap {
|
|
162
|
+
interface WebSocketServerEventMap<Message = WebSocketData> {
|
|
209
163
|
open: Event;
|
|
210
|
-
message: MessageEvent<
|
|
164
|
+
message: MessageEvent<Message>;
|
|
211
165
|
error: Event;
|
|
212
166
|
close: CloseEvent;
|
|
213
167
|
}
|
|
214
|
-
|
|
168
|
+
/**
|
|
169
|
+
* The handle to the original WebSocket server connection: its controls.
|
|
170
|
+
* A `WebSocketServerConnection` is a handle bound to a connection
|
|
171
|
+
* in this process.
|
|
172
|
+
*/
|
|
173
|
+
declare abstract class WebSocketServerHandle<Message = WebSocketData> {
|
|
174
|
+
extension?: WebSocketExtension<unknown, unknown>;
|
|
215
175
|
abstract connect(): void;
|
|
216
|
-
abstract send(data:
|
|
176
|
+
abstract send(data: Message): void;
|
|
217
177
|
abstract close(): void;
|
|
218
|
-
abstract addEventListener<EventType extends keyof WebSocketServerEventMap
|
|
219
|
-
abstract removeEventListener<EventType extends keyof WebSocketServerEventMap
|
|
178
|
+
abstract addEventListener<EventType extends keyof WebSocketServerEventMap<Message>>(event: EventType, listener: WebSocketEventListener<WebSocketServerEventMap<Message>[EventType]>, options?: AddEventListenerOptions | boolean): void;
|
|
179
|
+
abstract removeEventListener<EventType extends keyof WebSocketServerEventMap<Message>>(event: EventType, listener: WebSocketEventListener<WebSocketServerEventMap<Message>[EventType]>, options?: EventListenerOptions | boolean): void;
|
|
220
180
|
}
|
|
221
181
|
/**
|
|
222
182
|
* The WebSocket server instance represents the actual production
|
|
223
183
|
* WebSocket server connection. It's idle by default but you can
|
|
224
184
|
* establish it by calling `server.connect()`.
|
|
225
185
|
*/
|
|
226
|
-
declare class WebSocketServerConnection implements
|
|
186
|
+
declare class WebSocketServerConnection<Message = WebSocketData> implements WebSocketServerHandle<Message> {
|
|
187
|
+
#private;
|
|
227
188
|
private readonly client;
|
|
228
189
|
private readonly transport;
|
|
229
190
|
private readonly createConnection;
|
|
@@ -233,13 +194,29 @@ declare class WebSocketServerConnection implements WebSocketServerConnectionProt
|
|
|
233
194
|
private realWebSocket?;
|
|
234
195
|
private mockCloseController;
|
|
235
196
|
private realCloseController;
|
|
236
|
-
private [kEmitter];
|
|
197
|
+
private [kEmitter$1];
|
|
198
|
+
/**
|
|
199
|
+
* An optional extension applied to the data crossing this connection:
|
|
200
|
+
* `send()` encodes, incoming server frames are decoded
|
|
201
|
+
* before being dispatched as `message` events.
|
|
202
|
+
*/
|
|
203
|
+
extension?: WebSocketExtension<unknown, unknown>;
|
|
204
|
+
/**
|
|
205
|
+
* The intercepted connection this server belongs to.
|
|
206
|
+
* Provided by the interceptor once both connections exist.
|
|
207
|
+
*/
|
|
208
|
+
[kExtensionContext]?: WebSocketExtensionContext<unknown>;
|
|
237
209
|
constructor(client: WebSocketOverride, transport: WebSocketClassTransport, createConnection: () => WebSocket);
|
|
238
210
|
/**
|
|
239
211
|
* The `WebSocket` instance connected to the original server.
|
|
240
212
|
* Accessing this before calling `server.connect()` will throw.
|
|
241
213
|
*/
|
|
242
214
|
get socket(): WebSocket;
|
|
215
|
+
/**
|
|
216
|
+
* The ready state of the connection to the original WebSocket server.
|
|
217
|
+
* Equals `WebSocket.CLOSED` until `server.connect()` is called.
|
|
218
|
+
*/
|
|
219
|
+
get readyState(): number;
|
|
243
220
|
/**
|
|
244
221
|
* Open connection to the original WebSocket server.
|
|
245
222
|
*/
|
|
@@ -247,11 +224,11 @@ declare class WebSocketServerConnection implements WebSocketServerConnectionProt
|
|
|
247
224
|
/**
|
|
248
225
|
* Listen for the incoming events from the original WebSocket server.
|
|
249
226
|
*/
|
|
250
|
-
addEventListener<EventType extends keyof WebSocketServerEventMap
|
|
227
|
+
addEventListener<EventType extends keyof WebSocketServerEventMap<Message>>(event: EventType, listener: WebSocketEventListener<WebSocketServerEventMap<Message>[EventType]>, options?: AddEventListenerOptions | boolean): void;
|
|
251
228
|
/**
|
|
252
229
|
* Remove the listener for the given event.
|
|
253
230
|
*/
|
|
254
|
-
removeEventListener<EventType extends keyof WebSocketServerEventMap
|
|
231
|
+
removeEventListener<EventType extends keyof WebSocketServerEventMap<Message>>(event: EventType, listener: WebSocketEventListener<WebSocketServerEventMap<Message>[EventType]>, options?: EventListenerOptions | boolean): void;
|
|
255
232
|
/**
|
|
256
233
|
* Send data to the original WebSocket server.
|
|
257
234
|
* @example
|
|
@@ -259,7 +236,12 @@ declare class WebSocketServerConnection implements WebSocketServerConnectionProt
|
|
|
259
236
|
* server.send(new Blob(['hello']))
|
|
260
237
|
* server.send(new TextEncoder().encode('hello'))
|
|
261
238
|
*/
|
|
262
|
-
send(data:
|
|
239
|
+
send(data: Message): void;
|
|
240
|
+
/**
|
|
241
|
+
* Return the original WebSocket, throwing if
|
|
242
|
+
* the connection to the original server was not established.
|
|
243
|
+
*/
|
|
244
|
+
private assertConnected;
|
|
263
245
|
private [kSend];
|
|
264
246
|
/**
|
|
265
247
|
* Close the actual server connection.
|
|
@@ -270,16 +252,241 @@ declare class WebSocketServerConnection implements WebSocketServerConnectionProt
|
|
|
270
252
|
private handleRealClose;
|
|
271
253
|
}
|
|
272
254
|
//#endregion
|
|
255
|
+
//#region src/interceptors/WebSocket/web-socket-extension.d.ts
|
|
256
|
+
declare const kExtensionContext: unique symbol;
|
|
257
|
+
/**
|
|
258
|
+
* The messages the given extension encodes and decodes.
|
|
259
|
+
*
|
|
260
|
+
* @note Inferred from the `encode()` parameter alone. Inferring from the
|
|
261
|
+
* whole class would also consider the return type of `decode()`, where a
|
|
262
|
+
* `Generator<Message>` (whose return type defaults to `any`) would widen
|
|
263
|
+
* the messages to `any`.
|
|
264
|
+
*/
|
|
265
|
+
type WebSocketExtensionMessage<Extension> = Extension extends {
|
|
266
|
+
encode(message: infer Message, context: never): unknown;
|
|
267
|
+
} ? Message : never;
|
|
268
|
+
/**
|
|
269
|
+
* The API the given extension adds to the connection event.
|
|
270
|
+
*/
|
|
271
|
+
type WebSocketExtensionApi<Extension> = Extension extends WebSocketExtension<unknown, infer Api> ? Api : never;
|
|
272
|
+
/**
|
|
273
|
+
* The result of an extension method: a single value, an iterator
|
|
274
|
+
* over multiple values, or `undefined` for no values at all.
|
|
275
|
+
*
|
|
276
|
+
* @note Only iterators (objects with a `next()` method, like generators)
|
|
277
|
+
* are unfolded. Iterables that are not iterators, like strings, arrays,
|
|
278
|
+
* or typed arrays, are treated as a single value so array-shaped
|
|
279
|
+
* messages remain unambiguous.
|
|
280
|
+
*/
|
|
281
|
+
type WebSocketExtensionResult<Value> = Value | Iterator<Value, Value | void> | undefined;
|
|
282
|
+
/**
|
|
283
|
+
* The intercepted connection an extension operates on.
|
|
284
|
+
*/
|
|
285
|
+
interface WebSocketExtensionContext<Message = WebSocketData> {
|
|
286
|
+
client: WebSocketClientConnection<Message>;
|
|
287
|
+
server: WebSocketServerConnection<Message>;
|
|
288
|
+
info: WebSocketConnectionInfo;
|
|
289
|
+
}
|
|
290
|
+
interface WebSocketExtensionMessageContext<Message = WebSocketData> extends WebSocketExtensionContext<Message> {
|
|
291
|
+
/**
|
|
292
|
+
* The connection whose data is being encoded or decoded:
|
|
293
|
+
* the `client` for data crossing the client connection,
|
|
294
|
+
* the `server` for data crossing the original server connection.
|
|
295
|
+
*/
|
|
296
|
+
connection: WebSocketClientConnection<Message> | WebSocketServerConnection<Message>;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* A WebSocket extension: the server-side implementation of a protocol layered on top of
|
|
300
|
+
* WebSocket (e.g. Socket.IO): its message framing (`encode`/`decode`),
|
|
301
|
+
* its session as spoken by a server endpoint (`connect`/`receive`),
|
|
302
|
+
* and the server API it adds to every connection (`extend`).
|
|
303
|
+
*
|
|
304
|
+
* The interceptor decides *when* each part applies: `send()` encodes,
|
|
305
|
+
* frames are decoded before being dispatched as `message` events,
|
|
306
|
+
* `connect` and `receive` frames are sent while the mocked connection
|
|
307
|
+
* plays the server, and the extension is exposed on the connection event.
|
|
308
|
+
*
|
|
309
|
+
* An extension is invisible to the connection consumer: `client.send()`,
|
|
310
|
+
* `server.send()`, and the `message` events all operate on messages,
|
|
311
|
+
* and the session frames never surface. Raw frames forwarded between
|
|
312
|
+
* the client and the original server (passthrough) are never touched.
|
|
313
|
+
*
|
|
314
|
+
* A method returns a single value for a one-to-one mapping,
|
|
315
|
+
* an iterator (e.g. a generator) to unfold one input into many,
|
|
316
|
+
* or nothing to drop the input.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* class SocketIo extends WebSocketExtension<SocketIoMessage, { rooms: Rooms }> {
|
|
320
|
+
* match({ client }) {
|
|
321
|
+
* return client.url.searchParams.has('EIO')
|
|
322
|
+
* }
|
|
323
|
+
* encode(message) { ... }
|
|
324
|
+
* decode(data) { ... }
|
|
325
|
+
* connect() { ... }
|
|
326
|
+
* receive(data) { ... }
|
|
327
|
+
* extend({ client }) {
|
|
328
|
+
* return { rooms: new Rooms(client) }
|
|
329
|
+
* }
|
|
330
|
+
* }
|
|
331
|
+
*
|
|
332
|
+
* new WebSocketInterceptor({ extensions: [new SocketIo()] })
|
|
333
|
+
*/
|
|
334
|
+
declare abstract class WebSocketExtension<Message = WebSocketData, Extension = {}> {
|
|
335
|
+
/**
|
|
336
|
+
* Whether this extension applies to the given connection.
|
|
337
|
+
* Allows applying the extension to a subset of intercepted connections.
|
|
338
|
+
*/
|
|
339
|
+
match?(context: WebSocketExtensionContext<Message>): boolean;
|
|
340
|
+
/**
|
|
341
|
+
* Encode the given message into the raw frame(s) to send to the peer.
|
|
342
|
+
*/
|
|
343
|
+
abstract encode(message: Message, context: WebSocketExtensionMessageContext<Message>): WebSocketExtensionResult<WebSocketData>;
|
|
344
|
+
/**
|
|
345
|
+
* Decode the given raw frame into the message(s) to dispatch
|
|
346
|
+
* as `message` events. Return nothing to swallow the frame
|
|
347
|
+
* (e.g. protocol control frames).
|
|
348
|
+
*/
|
|
349
|
+
abstract decode(data: WebSocketData, context: WebSocketExtensionMessageContext<Message>): WebSocketExtensionResult<Message>;
|
|
350
|
+
/**
|
|
351
|
+
* The raw frame(s) a server of this extension's protocol sends once the
|
|
352
|
+
* connection is established (e.g. a session handshake).
|
|
353
|
+
* Sent to the client by the interceptor when the mocked connection
|
|
354
|
+
* opens, unless a connection to the original server was established,
|
|
355
|
+
* in which case the original server speaks for itself.
|
|
356
|
+
*/
|
|
357
|
+
connect?(context: WebSocketExtensionContext<Message>): WebSocketExtensionResult<WebSocketData>;
|
|
358
|
+
/**
|
|
359
|
+
* The raw frame(s) a server of this extension's protocol sends back in response
|
|
360
|
+
* to the given client frame (e.g. a control frame acknowledgement),
|
|
361
|
+
* the way a WebSocket endpoint answers a ping with a pong.
|
|
362
|
+
* Applied by the interceptor under the same condition as `connect`.
|
|
363
|
+
*/
|
|
364
|
+
receive?(data: WebSocketData, context: WebSocketExtensionContext<Message>): WebSocketExtensionResult<WebSocketData>;
|
|
365
|
+
/**
|
|
366
|
+
* The server API this extension adds to the given connection
|
|
367
|
+
* (e.g. rooms). Exposed on the connection event alongside
|
|
368
|
+
* the `client` and the `server`.
|
|
369
|
+
*/
|
|
370
|
+
extend?(context: WebSocketExtensionContext<Message>): Extension;
|
|
371
|
+
/**
|
|
372
|
+
* Apply this extension to the given connection.
|
|
373
|
+
* From then on, both the client and the server of that connection
|
|
374
|
+
* encode the data they send and decode the data they receive.
|
|
375
|
+
*
|
|
376
|
+
* @note Typed against the connection classes, not the handles:
|
|
377
|
+
* a connection is assignable across `Message` types only when
|
|
378
|
+
* compared as the same class, which is what lets an extension narrow
|
|
379
|
+
* the raw `WebSocketData` connections it is applied to.
|
|
380
|
+
*/
|
|
381
|
+
apply(connection: {
|
|
382
|
+
client: WebSocketClientConnection<Message>;
|
|
383
|
+
server: WebSocketServerConnection<Message>;
|
|
384
|
+
}): void;
|
|
385
|
+
}
|
|
386
|
+
//#endregion
|
|
387
|
+
//#region src/interceptors/WebSocket/web-socket-client-connection.d.ts
|
|
388
|
+
declare const kEmitter: unique symbol;
|
|
389
|
+
interface WebSocketClientEventMap<Message = WebSocketData> {
|
|
390
|
+
open: Event;
|
|
391
|
+
message: MessageEvent<Message>;
|
|
392
|
+
close: CloseEvent;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* The handle to a WebSocket client connection: its identity and controls.
|
|
396
|
+
* A handle is what a connection is regardless of where it lives.
|
|
397
|
+
* A `WebSocketClientConnection` is a handle bound to a connection in this
|
|
398
|
+
* process; a handle can also be revived from a serialized connection
|
|
399
|
+
* in another process (e.g. a connection stored by a worker).
|
|
400
|
+
*
|
|
401
|
+
* `Message` is what crosses this connection: raw WebSocket data, or the
|
|
402
|
+
* messages of the extension applied to it.
|
|
403
|
+
*/
|
|
404
|
+
declare abstract class WebSocketClientHandle<Message = WebSocketData> {
|
|
405
|
+
abstract id: string;
|
|
406
|
+
abstract url: URL;
|
|
407
|
+
extension?: WebSocketExtension<unknown, unknown>;
|
|
408
|
+
abstract send(data: Message): void;
|
|
409
|
+
abstract close(code?: number, reason?: string): void;
|
|
410
|
+
abstract addEventListener<EventType extends keyof WebSocketClientEventMap<Message>>(type: EventType, listener: WebSocketEventListener<WebSocketClientEventMap<Message>[EventType]>, options?: AddEventListenerOptions | boolean): void;
|
|
411
|
+
abstract removeEventListener<EventType extends keyof WebSocketClientEventMap<Message>>(event: EventType, listener: WebSocketEventListener<WebSocketClientEventMap<Message>[EventType]>, options?: EventListenerOptions | boolean): void;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* The WebSocket client instance represents an incoming
|
|
415
|
+
* client connection. The user can control the connection,
|
|
416
|
+
* send and receive events.
|
|
417
|
+
*/
|
|
418
|
+
declare class WebSocketClientConnection<Message = WebSocketData> implements WebSocketClientHandle<Message> {
|
|
419
|
+
#private;
|
|
420
|
+
readonly socket: WebSocket;
|
|
421
|
+
private readonly transport;
|
|
422
|
+
readonly id: string;
|
|
423
|
+
readonly url: URL;
|
|
424
|
+
/**
|
|
425
|
+
* An optional extension applied to the data crossing this connection:
|
|
426
|
+
* `send()` encodes, outgoing client frames are decoded
|
|
427
|
+
* before being dispatched as `message` events.
|
|
428
|
+
*/
|
|
429
|
+
extension?: WebSocketExtension<unknown, unknown>;
|
|
430
|
+
/**
|
|
431
|
+
* The intercepted connection this client belongs to.
|
|
432
|
+
* Provided by the interceptor once both connections exist.
|
|
433
|
+
*/
|
|
434
|
+
[kExtensionContext]?: WebSocketExtensionContext<unknown>;
|
|
435
|
+
private [kEmitter];
|
|
436
|
+
constructor(socket: WebSocket, transport: WebSocketTransport);
|
|
437
|
+
/**
|
|
438
|
+
* Listen for the outgoing events from the connected WebSocket client.
|
|
439
|
+
*/
|
|
440
|
+
addEventListener<EventType extends keyof WebSocketClientEventMap<Message>>(type: EventType, listener: WebSocketEventListener<WebSocketClientEventMap<Message>[EventType]>, options?: AddEventListenerOptions | boolean): void;
|
|
441
|
+
/**
|
|
442
|
+
* Removes the listener for the given event.
|
|
443
|
+
*/
|
|
444
|
+
removeEventListener<EventType extends keyof WebSocketClientEventMap<Message>>(event: EventType, listener: WebSocketEventListener<WebSocketClientEventMap<Message>[EventType]>, options?: EventListenerOptions | boolean): void;
|
|
445
|
+
/**
|
|
446
|
+
* Send data to the connected client.
|
|
447
|
+
*/
|
|
448
|
+
send(data: Message): void;
|
|
449
|
+
/**
|
|
450
|
+
* Close the WebSocket connection.
|
|
451
|
+
* @param {number} code A status code (see https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1).
|
|
452
|
+
* @param {string} reason A custom connection close reason.
|
|
453
|
+
*/
|
|
454
|
+
close(code?: number, reason?: string): void;
|
|
455
|
+
}
|
|
456
|
+
//#endregion
|
|
273
457
|
//#region src/interceptors/WebSocket/index.d.ts
|
|
458
|
+
type WebSocketExtensions = ReadonlyArray<WebSocketExtension<unknown, unknown>>;
|
|
459
|
+
/**
|
|
460
|
+
* The messages crossing the connections intercepted with the given
|
|
461
|
+
* extensions: the messages of the extension applied to a connection,
|
|
462
|
+
* or raw WebSocket data when there are no extensions.
|
|
463
|
+
*
|
|
464
|
+
* @note The types assume one of the extensions matches the connection.
|
|
465
|
+
*/
|
|
466
|
+
type WebSocketInterceptorMessage<Extensions extends WebSocketExtensions> = [Extensions] extends [readonly []] ? WebSocketData : WebSocketExtensionMessage<Extensions[number]>;
|
|
467
|
+
/**
|
|
468
|
+
* The API the extension applied to a connection adds to the connection event.
|
|
469
|
+
*/
|
|
470
|
+
type WebSocketInterceptorApi<Extensions extends WebSocketExtensions> = [Extensions] extends [readonly []] ? {} : WebSocketExtensionApi<Extensions[number]>;
|
|
471
|
+
interface WebSocketInterceptorOptions<Extensions extends WebSocketExtensions = []> {
|
|
472
|
+
/**
|
|
473
|
+
* Extensions to apply to the intercepted connections.
|
|
474
|
+
* The first extension whose `match()` accepts a connection is applied
|
|
475
|
+
* to it. An extension without `match()` accepts every connection.
|
|
476
|
+
*/
|
|
477
|
+
extensions?: Extensions;
|
|
478
|
+
}
|
|
274
479
|
/**
|
|
275
480
|
* Intercept the outgoing WebSocket connections created using
|
|
276
481
|
* the global `WebSocket` class.
|
|
277
482
|
*/
|
|
278
|
-
declare class WebSocketInterceptor extends Interceptor<WebSocketEventMap$1
|
|
483
|
+
declare class WebSocketInterceptor<const Extensions extends WebSocketExtensions = []> extends Interceptor<WebSocketEventMap$1<WebSocketInterceptorMessage<Extensions>, WebSocketInterceptorApi<Extensions>>> {
|
|
279
484
|
static symbol: symbol;
|
|
485
|
+
private readonly extensions;
|
|
486
|
+
constructor(options?: WebSocketInterceptorOptions<Extensions>);
|
|
280
487
|
protected predicate(): boolean;
|
|
281
488
|
protected setup(): void;
|
|
282
489
|
}
|
|
283
490
|
//#endregion
|
|
284
|
-
export { CancelableCloseEvent, CancelableMessageEvent, CloseEvent, WebSocketClientConnection,
|
|
491
|
+
export { CancelableCloseEvent, CancelableMessageEvent, CloseEvent, WebSocketClientConnection, type WebSocketClientEventMap, WebSocketClientHandle, WebSocketConnectionEvent, type WebSocketConnectionEventData, type WebSocketConnectionInfo, type WebSocketData, type WebSocketEventMap$1 as WebSocketEventMap, WebSocketExtension, type WebSocketExtensionApi, type WebSocketExtensionContext, type WebSocketExtensionMessage, type WebSocketExtensionMessageContext, type WebSocketExtensionResult, WebSocketInterceptor, WebSocketInterceptorApi, WebSocketInterceptorMessage, WebSocketInterceptorOptions, WebSocketServerConnection, type WebSocketServerEventMap, WebSocketServerHandle, type WebSocketTransport };
|
|
285
492
|
//# sourceMappingURL=index.d.ts.map
|