@wooksjs/event-ws 0.7.2 → 0.7.3
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/index.cjs +22 -11
- package/dist/index.d.ts +21 -16
- package/dist/index.mjs +21 -12
- package/package.json +5 -5
- package/skills/wooksjs-event-ws/SKILL.md +3 -0
- package/skills/wooksjs-event-ws/core.md +3 -3
package/dist/index.cjs
CHANGED
|
@@ -167,6 +167,17 @@ function useWsServer() {
|
|
|
167
167
|
};
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region packages/event-ws/src/event-ws.ts
|
|
172
|
+
/** Creates a WS connection event context and runs `fn` inside it. */
|
|
173
|
+
function createWsConnectionContext(options, seeds, fn) {
|
|
174
|
+
return (0, __wooksjs_event_core.createEventContext)(options, wsConnectionKind, seeds, fn);
|
|
175
|
+
}
|
|
176
|
+
/** Creates a WS message event context and runs `fn` inside it. */
|
|
177
|
+
function createWsMessageContext(options, seeds, fn) {
|
|
178
|
+
return (0, __wooksjs_event_core.createEventContext)(options, wsMessageKind, seeds, fn);
|
|
179
|
+
}
|
|
180
|
+
|
|
170
181
|
//#endregion
|
|
171
182
|
//#region packages/event-ws/src/adapters/ws-adapter-default.ts
|
|
172
183
|
/** Creates the default WsServerAdapter wrapping the `ws` package (peer dependency). */
|
|
@@ -426,13 +437,13 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
426
437
|
return this.server;
|
|
427
438
|
}
|
|
428
439
|
doUpgrade(req, socket, head, parentCtx) {
|
|
440
|
+
const ctxOptions = parentCtx ? {
|
|
441
|
+
...this.eventContextOptions,
|
|
442
|
+
parent: parentCtx
|
|
443
|
+
} : this.eventContextOptions;
|
|
429
444
|
this.wsServer.handleUpgrade(req, socket, head, (ws$1) => {
|
|
430
445
|
const id = crypto.randomUUID();
|
|
431
|
-
|
|
432
|
-
...this.eventContextOptions,
|
|
433
|
-
...parentCtx ? { parent: parentCtx } : {}
|
|
434
|
-
};
|
|
435
|
-
(0, __wooksjs_event_core.createEventContext)(ctxOptions, wsConnectionKind, {
|
|
446
|
+
createWsConnectionContext(ctxOptions, {
|
|
436
447
|
id,
|
|
437
448
|
ws: ws$1
|
|
438
449
|
}, () => {
|
|
@@ -441,10 +452,7 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
441
452
|
try {
|
|
442
453
|
if (this.onConnectHandler) {
|
|
443
454
|
const result = this.onConnectHandler();
|
|
444
|
-
if (result !== null && result !== void 0 && typeof result.then === "function")
|
|
445
|
-
result.then(() => this.acceptConnection(connection)).catch((error) => this.rejectConnection(connection, error));
|
|
446
|
-
return;
|
|
447
|
-
}
|
|
455
|
+
if (result !== null && result !== void 0 && typeof result.then === "function") return result.then(() => this.acceptConnection(connection)).catch((error) => this.rejectConnection(connection, error));
|
|
448
456
|
}
|
|
449
457
|
this.acceptConnection(connection);
|
|
450
458
|
} catch (error) {
|
|
@@ -492,10 +500,10 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
492
500
|
return;
|
|
493
501
|
}
|
|
494
502
|
const { event, path, data, id: messageId } = msg;
|
|
495
|
-
(
|
|
503
|
+
createWsMessageContext({
|
|
496
504
|
...this.eventContextOptions,
|
|
497
505
|
parent: connection.ctx
|
|
498
|
-
},
|
|
506
|
+
}, {
|
|
499
507
|
data,
|
|
500
508
|
rawMessage: raw,
|
|
501
509
|
messageId,
|
|
@@ -512,6 +520,7 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
512
520
|
if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
|
|
513
521
|
this.handleHandlerError(error, connection, messageId);
|
|
514
522
|
});
|
|
523
|
+
return result;
|
|
515
524
|
});
|
|
516
525
|
}
|
|
517
526
|
processHandlers(handlers, connection, messageId) {
|
|
@@ -702,6 +711,8 @@ exports.WsConnection = WsConnection;
|
|
|
702
711
|
exports.WsError = WsError;
|
|
703
712
|
exports.WsRoomManager = WsRoomManager;
|
|
704
713
|
exports.createWsApp = createWsApp;
|
|
714
|
+
exports.createWsConnectionContext = createWsConnectionContext;
|
|
715
|
+
exports.createWsMessageContext = createWsMessageContext;
|
|
705
716
|
exports.currentConnection = currentConnection;
|
|
706
717
|
exports.prepareTestWsConnectionContext = prepareTestWsConnectionContext;
|
|
707
718
|
exports.prepareTestWsMessageContext = prepareTestWsMessageContext;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _wooksjs_event_core from '@wooksjs/event-core';
|
|
2
|
-
import { EventContext, EventContextOptions } from '@wooksjs/event-core';
|
|
2
|
+
import { EventContext, EventContextOptions, EventKindSeeds } from '@wooksjs/event-core';
|
|
3
3
|
export { EventContext, useLogger, useRouteParams } from '@wooksjs/event-core';
|
|
4
4
|
import { TConsoleBase } from '@prostojs/logger';
|
|
5
5
|
import http, { IncomingMessage, Server } from 'http';
|
|
@@ -186,6 +186,25 @@ declare function useWsServer(): {
|
|
|
186
186
|
roomConnections(room: string): Set<WsConnection>;
|
|
187
187
|
};
|
|
188
188
|
|
|
189
|
+
/** Event kind for WebSocket connections (long-lived, one per client). */
|
|
190
|
+
declare const wsConnectionKind: _wooksjs_event_core.EventKind<{
|
|
191
|
+
id: _wooksjs_event_core.SlotMarker<string>;
|
|
192
|
+
ws: _wooksjs_event_core.SlotMarker<WsSocket>;
|
|
193
|
+
}>;
|
|
194
|
+
/** Event kind for WebSocket messages (short-lived, one per incoming message). */
|
|
195
|
+
declare const wsMessageKind: _wooksjs_event_core.EventKind<{
|
|
196
|
+
data: _wooksjs_event_core.SlotMarker<unknown>;
|
|
197
|
+
rawMessage: _wooksjs_event_core.SlotMarker<string | Buffer<ArrayBufferLike>>;
|
|
198
|
+
messageId: _wooksjs_event_core.SlotMarker<string | number | undefined>;
|
|
199
|
+
messagePath: _wooksjs_event_core.SlotMarker<string>;
|
|
200
|
+
messageEvent: _wooksjs_event_core.SlotMarker<string>;
|
|
201
|
+
}>;
|
|
202
|
+
|
|
203
|
+
/** Creates a WS connection event context and runs `fn` inside it. */
|
|
204
|
+
declare function createWsConnectionContext<R>(options: EventContextOptions, seeds: EventKindSeeds<typeof wsConnectionKind>, fn: () => R): R;
|
|
205
|
+
/** Creates a WS message event context and runs `fn` inside it. */
|
|
206
|
+
declare function createWsMessageContext<R>(options: EventContextOptions, seeds: EventKindSeeds<typeof wsMessageKind>, fn: () => R): R;
|
|
207
|
+
|
|
189
208
|
/** WebSocket adapter for Wooks. Implements WooksUpgradeHandler for HTTP integration. */
|
|
190
209
|
declare class WooksWs extends WooksAdapterBase implements WooksUpgradeHandler {
|
|
191
210
|
protected logger: TConsoleBase;
|
|
@@ -258,20 +277,6 @@ declare class WooksWs extends WooksAdapterBase implements WooksUpgradeHandler {
|
|
|
258
277
|
*/
|
|
259
278
|
declare function createWsApp(wooksOrOpts?: Wooks | WooksAdapterBase | TWooksWsOptions, opts?: TWooksWsOptions): WooksWs;
|
|
260
279
|
|
|
261
|
-
/** Event kind for WebSocket connections (long-lived, one per client). */
|
|
262
|
-
declare const wsConnectionKind: _wooksjs_event_core.EventKind<{
|
|
263
|
-
id: _wooksjs_event_core.SlotMarker<string>;
|
|
264
|
-
ws: _wooksjs_event_core.SlotMarker<WsSocket>;
|
|
265
|
-
}>;
|
|
266
|
-
/** Event kind for WebSocket messages (short-lived, one per incoming message). */
|
|
267
|
-
declare const wsMessageKind: _wooksjs_event_core.EventKind<{
|
|
268
|
-
data: _wooksjs_event_core.SlotMarker<unknown>;
|
|
269
|
-
rawMessage: _wooksjs_event_core.SlotMarker<string | Buffer<ArrayBufferLike>>;
|
|
270
|
-
messageId: _wooksjs_event_core.SlotMarker<string | number | undefined>;
|
|
271
|
-
messagePath: _wooksjs_event_core.SlotMarker<string>;
|
|
272
|
-
messageEvent: _wooksjs_event_core.SlotMarker<string>;
|
|
273
|
-
}>;
|
|
274
|
-
|
|
275
280
|
/** WebSocket error with a numeric code following HTTP conventions (401, 403, 404, 500, etc.). */
|
|
276
281
|
declare class WsError extends Error {
|
|
277
282
|
readonly code: number;
|
|
@@ -331,5 +336,5 @@ declare function prepareTestWsConnectionContext(options?: TTestWsConnectionConte
|
|
|
331
336
|
*/
|
|
332
337
|
declare function prepareTestWsMessageContext(options: TTestWsMessageContext): <T>(cb: (...a: any[]) => T) => T;
|
|
333
338
|
|
|
334
|
-
export { WooksWs, WsConnection, WsError, WsRoomManager, createWsApp, currentConnection, prepareTestWsConnectionContext, prepareTestWsMessageContext, useWsConnection, useWsMessage, useWsRooms, useWsServer, wsConnectionKind, wsMessageKind };
|
|
339
|
+
export { WooksWs, WsConnection, WsError, WsRoomManager, createWsApp, createWsConnectionContext, createWsMessageContext, currentConnection, prepareTestWsConnectionContext, prepareTestWsMessageContext, useWsConnection, useWsMessage, useWsRooms, useWsServer, wsConnectionKind, wsMessageKind };
|
|
335
340
|
export type { TTestWsConnectionContext, TTestWsMessageContext, TWooksWsOptions, WsBroadcastOptions, WsBroadcastTransport, WsClientMessage, WsPushMessage, WsReplyMessage, WsServerAdapter, WsServerInstance, WsSocket };
|
package/dist/index.mjs
CHANGED
|
@@ -144,6 +144,17 @@ function useWsServer() {
|
|
|
144
144
|
};
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region packages/event-ws/src/event-ws.ts
|
|
149
|
+
/** Creates a WS connection event context and runs `fn` inside it. */
|
|
150
|
+
function createWsConnectionContext(options, seeds, fn) {
|
|
151
|
+
return createEventContext(options, wsConnectionKind, seeds, fn);
|
|
152
|
+
}
|
|
153
|
+
/** Creates a WS message event context and runs `fn` inside it. */
|
|
154
|
+
function createWsMessageContext(options, seeds, fn) {
|
|
155
|
+
return createEventContext(options, wsMessageKind, seeds, fn);
|
|
156
|
+
}
|
|
157
|
+
|
|
147
158
|
//#endregion
|
|
148
159
|
//#region packages/event-ws/src/adapters/ws-adapter-default.ts
|
|
149
160
|
/** Creates the default WsServerAdapter wrapping the `ws` package (peer dependency). */
|
|
@@ -403,13 +414,13 @@ var WooksWs = class extends WooksAdapterBase {
|
|
|
403
414
|
return this.server;
|
|
404
415
|
}
|
|
405
416
|
doUpgrade(req, socket, head, parentCtx) {
|
|
417
|
+
const ctxOptions = parentCtx ? {
|
|
418
|
+
...this.eventContextOptions,
|
|
419
|
+
parent: parentCtx
|
|
420
|
+
} : this.eventContextOptions;
|
|
406
421
|
this.wsServer.handleUpgrade(req, socket, head, (ws) => {
|
|
407
422
|
const id = crypto.randomUUID();
|
|
408
|
-
|
|
409
|
-
...this.eventContextOptions,
|
|
410
|
-
...parentCtx ? { parent: parentCtx } : {}
|
|
411
|
-
};
|
|
412
|
-
createEventContext(ctxOptions, wsConnectionKind, {
|
|
423
|
+
createWsConnectionContext(ctxOptions, {
|
|
413
424
|
id,
|
|
414
425
|
ws
|
|
415
426
|
}, () => {
|
|
@@ -418,10 +429,7 @@ var WooksWs = class extends WooksAdapterBase {
|
|
|
418
429
|
try {
|
|
419
430
|
if (this.onConnectHandler) {
|
|
420
431
|
const result = this.onConnectHandler();
|
|
421
|
-
if (result !== null && result !== void 0 && typeof result.then === "function")
|
|
422
|
-
result.then(() => this.acceptConnection(connection)).catch((error) => this.rejectConnection(connection, error));
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
432
|
+
if (result !== null && result !== void 0 && typeof result.then === "function") return result.then(() => this.acceptConnection(connection)).catch((error) => this.rejectConnection(connection, error));
|
|
425
433
|
}
|
|
426
434
|
this.acceptConnection(connection);
|
|
427
435
|
} catch (error) {
|
|
@@ -469,10 +477,10 @@ var WooksWs = class extends WooksAdapterBase {
|
|
|
469
477
|
return;
|
|
470
478
|
}
|
|
471
479
|
const { event, path, data, id: messageId } = msg;
|
|
472
|
-
|
|
480
|
+
createWsMessageContext({
|
|
473
481
|
...this.eventContextOptions,
|
|
474
482
|
parent: connection.ctx
|
|
475
|
-
},
|
|
483
|
+
}, {
|
|
476
484
|
data,
|
|
477
485
|
rawMessage: raw,
|
|
478
486
|
messageId,
|
|
@@ -489,6 +497,7 @@ var WooksWs = class extends WooksAdapterBase {
|
|
|
489
497
|
if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
|
|
490
498
|
this.handleHandlerError(error, connection, messageId);
|
|
491
499
|
});
|
|
500
|
+
return result;
|
|
492
501
|
});
|
|
493
502
|
}
|
|
494
503
|
processHandlers(handlers, connection, messageId) {
|
|
@@ -674,4 +683,4 @@ function prepareTestWsMessageContext(options) {
|
|
|
674
683
|
}
|
|
675
684
|
|
|
676
685
|
//#endregion
|
|
677
|
-
export { WooksWs, WsConnection, WsError, WsRoomManager, createWsApp, currentConnection, prepareTestWsConnectionContext, prepareTestWsMessageContext, useLogger, useRouteParams, useWsConnection, useWsMessage, useWsRooms, useWsServer, wsConnectionKind, wsMessageKind };
|
|
686
|
+
export { WooksWs, WsConnection, WsError, WsRoomManager, createWsApp, createWsConnectionContext, createWsMessageContext, currentConnection, prepareTestWsConnectionContext, prepareTestWsMessageContext, useLogger, useRouteParams, useWsConnection, useWsMessage, useWsRooms, useWsServer, wsConnectionKind, wsMessageKind };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-ws",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "WebSocket adapter for Wooks with path-based message routing, rooms, and broadcasting",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -47,15 +47,15 @@
|
|
|
47
47
|
"@types/ws": "^8.18.1",
|
|
48
48
|
"typescript": "^5.9.3",
|
|
49
49
|
"vitest": "^3.2.4",
|
|
50
|
-
"@wooksjs/event-core": "^0.7.
|
|
51
|
-
"wooks": "^0.7.
|
|
50
|
+
"@wooksjs/event-core": "^0.7.3",
|
|
51
|
+
"wooks": "^0.7.3"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@prostojs/logger": "^0.4.3",
|
|
55
55
|
"@prostojs/router": "^0.3.2",
|
|
56
56
|
"ws": "^8.0.0",
|
|
57
|
-
"@wooksjs/event-core": "^0.7.
|
|
58
|
-
"wooks": "^0.7.
|
|
57
|
+
"@wooksjs/event-core": "^0.7.3",
|
|
58
|
+
"wooks": "^0.7.3"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "rolldown -c ../../rolldown.config.mjs",
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
## Concepts
|
|
6
6
|
|
|
7
|
-
`@wooksjs/event-ws` follows the Wooks adapter pattern. It creates two nested context layers:
|
|
7
|
+
`@wooksjs/event-ws` follows the Wooks adapter pattern. It creates two nested context layers via dedicated context factories:
|
|
8
8
|
|
|
9
|
-
1. **Connection context** (`ws:connection` kind) — long-lived, one per connected client.
|
|
10
|
-
2. **Message context** (`ws:message` kind) — short-lived, one per incoming message.
|
|
9
|
+
1. **Connection context** (`ws:connection` kind) — long-lived, one per connected client. Created via `createWsConnectionContext(options, seeds, fn)`, seeded with `id` and `ws` socket.
|
|
10
|
+
2. **Message context** (`ws:message` kind) — short-lived, one per incoming message. Created via `createWsMessageContext(options, seeds, fn)` with `parent` set to the connection context.
|
|
11
11
|
|
|
12
12
|
Messages follow a JSON wire protocol with `event` + `path` for routing and an optional `id` for request-response correlation.
|
|
13
13
|
|