@wooksjs/event-ws 0.7.10 → 0.7.12
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 +83 -92
- package/dist/index.mjs +21 -36
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -6,12 +7,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
9
|
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
@@ -21,24 +26,25 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
26
|
}) : target, mod));
|
|
22
27
|
|
|
23
28
|
//#endregion
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
let _wooksjs_event_core = require("@wooksjs/event-core");
|
|
30
|
+
let http = require("http");
|
|
31
|
+
http = __toESM(http);
|
|
32
|
+
let wooks = require("wooks");
|
|
33
|
+
let ws = require("ws");
|
|
28
34
|
|
|
29
35
|
//#region packages/event-ws/src/ws-kind.ts
|
|
30
36
|
/** Event kind for WebSocket connections (long-lived, one per client). */
|
|
31
|
-
const wsConnectionKind = (0,
|
|
32
|
-
id: (0,
|
|
33
|
-
ws: (0,
|
|
37
|
+
const wsConnectionKind = (0, _wooksjs_event_core.defineEventKind)("ws:connection", {
|
|
38
|
+
id: (0, _wooksjs_event_core.slot)(),
|
|
39
|
+
ws: (0, _wooksjs_event_core.slot)()
|
|
34
40
|
});
|
|
35
41
|
/** Event kind for WebSocket messages (short-lived, one per incoming message). */
|
|
36
|
-
const wsMessageKind = (0,
|
|
37
|
-
data: (0,
|
|
38
|
-
rawMessage: (0,
|
|
39
|
-
messageId: (0,
|
|
40
|
-
messagePath: (0,
|
|
41
|
-
messageEvent: (0,
|
|
42
|
+
const wsMessageKind = (0, _wooksjs_event_core.defineEventKind)("ws:message", {
|
|
43
|
+
data: (0, _wooksjs_event_core.slot)(),
|
|
44
|
+
rawMessage: (0, _wooksjs_event_core.slot)(),
|
|
45
|
+
messageId: (0, _wooksjs_event_core.slot)(),
|
|
46
|
+
messagePath: (0, _wooksjs_event_core.slot)(),
|
|
47
|
+
messageEvent: (0, _wooksjs_event_core.slot)()
|
|
42
48
|
});
|
|
43
49
|
|
|
44
50
|
//#endregion
|
|
@@ -64,25 +70,24 @@ function getAdapterState() {
|
|
|
64
70
|
* - In `onMessage`: returns `current().parent` (the connection context).
|
|
65
71
|
*/
|
|
66
72
|
function currentConnection(ctx) {
|
|
67
|
-
const c = ctx ?? (0,
|
|
73
|
+
const c = ctx ?? (0, _wooksjs_event_core.current)();
|
|
68
74
|
return c.parent ?? c;
|
|
69
75
|
}
|
|
70
76
|
/**
|
|
71
77
|
* Provides access to the current WebSocket connection (id, send, close).
|
|
72
78
|
* Works in both connection and message contexts via parent chain traversal.
|
|
73
79
|
*/
|
|
74
|
-
const useWsConnection = (0,
|
|
80
|
+
const useWsConnection = (0, _wooksjs_event_core.defineWook)((ctx) => {
|
|
75
81
|
const id = ctx.get(wsConnectionKind.keys.id);
|
|
76
|
-
const ws
|
|
77
|
-
const
|
|
78
|
-
const connection = state$1.connections.get(id);
|
|
82
|
+
const ws = ctx.get(wsConnectionKind.keys.ws);
|
|
83
|
+
const connection = getAdapterState().connections.get(id);
|
|
79
84
|
return {
|
|
80
85
|
id,
|
|
81
86
|
send(event, path, data, params) {
|
|
82
87
|
connection.send(event, path, data, params);
|
|
83
88
|
},
|
|
84
89
|
close(code, reason) {
|
|
85
|
-
ws
|
|
90
|
+
ws.close(code, reason);
|
|
86
91
|
},
|
|
87
92
|
context: currentConnection(ctx)
|
|
88
93
|
};
|
|
@@ -97,7 +102,7 @@ const useWsConnection = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
|
97
102
|
function useWsMessage(ctx) {
|
|
98
103
|
return useWsMessageInternal(ctx);
|
|
99
104
|
}
|
|
100
|
-
const useWsMessageInternal = (0,
|
|
105
|
+
const useWsMessageInternal = (0, _wooksjs_event_core.defineWook)((ctx) => {
|
|
101
106
|
return {
|
|
102
107
|
data: ctx.get(wsMessageKind.keys.data),
|
|
103
108
|
raw: ctx.get(wsMessageKind.keys.rawMessage),
|
|
@@ -114,28 +119,27 @@ const useWsMessageInternal = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
|
114
119
|
* All methods default to the current message path as the room.
|
|
115
120
|
* Only available in message context (inside `onMessage` handlers).
|
|
116
121
|
*/
|
|
117
|
-
const useWsRooms = (0,
|
|
122
|
+
const useWsRooms = (0, _wooksjs_event_core.defineWook)((ctx) => {
|
|
118
123
|
const messagePath = ctx.get(wsMessageKind.keys.messagePath);
|
|
119
124
|
const connectionId = ctx.get(wsConnectionKind.keys.id);
|
|
120
|
-
const state
|
|
121
|
-
const connection = state
|
|
125
|
+
const state = getAdapterState();
|
|
126
|
+
const connection = state.connections.get(connectionId);
|
|
122
127
|
return {
|
|
123
128
|
join(room) {
|
|
124
|
-
state
|
|
129
|
+
state.roomManager.join(connection, room ?? messagePath);
|
|
125
130
|
},
|
|
126
131
|
leave(room) {
|
|
127
|
-
state
|
|
132
|
+
state.roomManager.leave(connection, room ?? messagePath);
|
|
128
133
|
},
|
|
129
134
|
broadcast(event, data, options) {
|
|
130
135
|
const room = options?.room ?? messagePath;
|
|
131
136
|
const excludeSelf = options?.excludeSelf ?? true;
|
|
132
137
|
let params;
|
|
133
138
|
try {
|
|
134
|
-
const
|
|
135
|
-
const p = rp.params;
|
|
139
|
+
const p = (0, _wooksjs_event_core.useRouteParams)(ctx).params;
|
|
136
140
|
if (Object.keys(p).length > 0) params = p;
|
|
137
141
|
} catch {}
|
|
138
|
-
state
|
|
142
|
+
state.roomManager.broadcast(room, event, room, data, params, excludeSelf ? connection : void 0);
|
|
139
143
|
},
|
|
140
144
|
rooms() {
|
|
141
145
|
return [...connection.rooms];
|
|
@@ -150,19 +154,19 @@ const useWsRooms = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
|
150
154
|
* Not a `defineWook` — reads directly from the adapter state.
|
|
151
155
|
*/
|
|
152
156
|
function useWsServer() {
|
|
153
|
-
const state
|
|
157
|
+
const state = getAdapterState();
|
|
154
158
|
return {
|
|
155
159
|
connections() {
|
|
156
|
-
return state
|
|
160
|
+
return state.connections;
|
|
157
161
|
},
|
|
158
162
|
broadcast(event, path, data, params) {
|
|
159
|
-
for (const conn of state
|
|
163
|
+
for (const conn of state.connections.values()) conn.send(event, path, data, params);
|
|
160
164
|
},
|
|
161
165
|
getConnection(id) {
|
|
162
|
-
return state
|
|
166
|
+
return state.connections.get(id);
|
|
163
167
|
},
|
|
164
168
|
roomConnections(room) {
|
|
165
|
-
return state
|
|
169
|
+
return state.roomManager.connections(room);
|
|
166
170
|
}
|
|
167
171
|
};
|
|
168
172
|
}
|
|
@@ -171,11 +175,11 @@ function useWsServer() {
|
|
|
171
175
|
//#region packages/event-ws/src/event-ws.ts
|
|
172
176
|
/** Creates a WS connection event context and runs `fn` inside it. */
|
|
173
177
|
function createWsConnectionContext(options, seeds, fn) {
|
|
174
|
-
return (0,
|
|
178
|
+
return (0, _wooksjs_event_core.createEventContext)(options, wsConnectionKind, seeds, fn);
|
|
175
179
|
}
|
|
176
180
|
/** Creates a WS message event context and runs `fn` inside it. */
|
|
177
181
|
function createWsMessageContext(options, seeds, fn) {
|
|
178
|
-
return (0,
|
|
182
|
+
return (0, _wooksjs_event_core.createEventContext)(options, wsMessageKind, seeds, fn);
|
|
179
183
|
}
|
|
180
184
|
|
|
181
185
|
//#endregion
|
|
@@ -201,13 +205,13 @@ function createDefaultWsServerAdapter() {
|
|
|
201
205
|
//#region packages/event-ws/src/ws-connection.ts
|
|
202
206
|
/** Internal class representing a connected WebSocket client. */
|
|
203
207
|
var WsConnection = class {
|
|
204
|
-
|
|
205
|
-
alive = true;
|
|
206
|
-
constructor(id, ws$1, ctx, serializer) {
|
|
208
|
+
constructor(id, ws, ctx, serializer) {
|
|
207
209
|
this.id = id;
|
|
208
|
-
this.ws = ws
|
|
210
|
+
this.ws = ws;
|
|
209
211
|
this.ctx = ctx;
|
|
210
212
|
this.serializer = serializer;
|
|
213
|
+
this.rooms = /* @__PURE__ */ new Set();
|
|
214
|
+
this.alive = true;
|
|
211
215
|
}
|
|
212
216
|
/** Send a push message to this connection. */
|
|
213
217
|
send(event, path, data, params) {
|
|
@@ -260,9 +264,9 @@ var WsError = class extends Error {
|
|
|
260
264
|
//#region packages/event-ws/src/ws-room-manager.ts
|
|
261
265
|
/** Manages room → connections mapping with optional distributed broadcast transport. */
|
|
262
266
|
var WsRoomManager = class {
|
|
263
|
-
rooms = /* @__PURE__ */ new Map();
|
|
264
267
|
constructor(transport) {
|
|
265
268
|
this.transport = transport;
|
|
269
|
+
this.rooms = /* @__PURE__ */ new Map();
|
|
266
270
|
if (transport) {}
|
|
267
271
|
}
|
|
268
272
|
/** Add a connection to a room. */
|
|
@@ -339,31 +343,20 @@ var WsRoomManager = class {
|
|
|
339
343
|
//#region packages/event-ws/src/ws-adapter.ts
|
|
340
344
|
const DEFAULT_HEARTBEAT_INTERVAL = 3e4;
|
|
341
345
|
const DEFAULT_MAX_MESSAGE_SIZE = 1024 * 1024;
|
|
342
|
-
const upgradeReqKey = (0,
|
|
343
|
-
const upgradeSocketKey = (0,
|
|
344
|
-
const upgradeHeadKey = (0,
|
|
346
|
+
const upgradeReqKey = (0, _wooksjs_event_core.key)("ws:upgrade-req");
|
|
347
|
+
const upgradeSocketKey = (0, _wooksjs_event_core.key)("ws:upgrade-socket");
|
|
348
|
+
const upgradeHeadKey = (0, _wooksjs_event_core.key)("ws:upgrade-head");
|
|
345
349
|
/** WebSocket adapter for Wooks. Implements WooksUpgradeHandler for HTTP integration. */
|
|
346
350
|
var WooksWs = class extends wooks.WooksAdapterBase {
|
|
347
|
-
logger;
|
|
348
|
-
eventContextOptions;
|
|
349
|
-
connections = /* @__PURE__ */ new Map();
|
|
350
|
-
roomManager;
|
|
351
|
-
wsServer;
|
|
352
|
-
opts;
|
|
353
|
-
serializer;
|
|
354
|
-
parser;
|
|
355
|
-
onConnectHandler;
|
|
356
|
-
onDisconnectHandler;
|
|
357
|
-
heartbeatTimer;
|
|
358
|
-
server;
|
|
359
|
-
reqKey = upgradeReqKey;
|
|
360
|
-
socketKey = upgradeSocketKey;
|
|
361
|
-
headKey = upgradeHeadKey;
|
|
362
351
|
constructor(wooksOrOpts, opts) {
|
|
363
352
|
const isWooks = wooksOrOpts instanceof wooks.WooksAdapterBase || wooksOrOpts && "getRouter" in wooksOrOpts;
|
|
364
353
|
const wooks$1 = isWooks ? wooksOrOpts : void 0;
|
|
365
354
|
const resolvedOpts = isWooks ? opts ?? {} : wooksOrOpts ?? {};
|
|
366
355
|
super(wooks$1, resolvedOpts.logger, void 0);
|
|
356
|
+
this.connections = /* @__PURE__ */ new Map();
|
|
357
|
+
this.reqKey = upgradeReqKey;
|
|
358
|
+
this.socketKey = upgradeSocketKey;
|
|
359
|
+
this.headKey = upgradeHeadKey;
|
|
367
360
|
if (wooksOrOpts && typeof wooksOrOpts.ws === "function") wooksOrOpts.ws(this);
|
|
368
361
|
this.opts = resolvedOpts;
|
|
369
362
|
this.logger = resolvedOpts.logger || this.getLogger(`[96m[wooks-ws]`);
|
|
@@ -371,8 +364,7 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
371
364
|
this.serializer = resolvedOpts.messageSerializer ?? JSON.stringify;
|
|
372
365
|
this.parser = resolvedOpts.messageParser ?? defaultParser;
|
|
373
366
|
this.roomManager = new WsRoomManager(resolvedOpts.broadcastTransport);
|
|
374
|
-
|
|
375
|
-
this.wsServer = adapter.create();
|
|
367
|
+
this.wsServer = (resolvedOpts.wsServerAdapter ?? createDefaultWsServerAdapter()).create();
|
|
376
368
|
setAdapterState({
|
|
377
369
|
connections: this.connections,
|
|
378
370
|
roomManager: this.roomManager,
|
|
@@ -398,7 +390,7 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
398
390
|
* The HTTP context becomes the parent of the WS connection context.
|
|
399
391
|
*/
|
|
400
392
|
upgrade() {
|
|
401
|
-
const httpCtx = (0,
|
|
393
|
+
const httpCtx = (0, _wooksjs_event_core.current)();
|
|
402
394
|
const req = httpCtx.get(upgradeReqKey);
|
|
403
395
|
const socket = httpCtx.get(upgradeSocketKey);
|
|
404
396
|
const head = httpCtx.get(upgradeHeadKey);
|
|
@@ -441,14 +433,13 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
441
433
|
...this.eventContextOptions,
|
|
442
434
|
parent: parentCtx
|
|
443
435
|
} : this.eventContextOptions;
|
|
444
|
-
this.wsServer.handleUpgrade(req, socket, head, (ws
|
|
436
|
+
this.wsServer.handleUpgrade(req, socket, head, (ws) => {
|
|
445
437
|
const id = crypto.randomUUID();
|
|
446
438
|
createWsConnectionContext(ctxOptions, {
|
|
447
439
|
id,
|
|
448
|
-
ws
|
|
440
|
+
ws
|
|
449
441
|
}, () => {
|
|
450
|
-
const
|
|
451
|
-
const connection = new WsConnection(id, ws$1, connectionCtx, this.serializer);
|
|
442
|
+
const connection = new WsConnection(id, ws, (0, _wooksjs_event_core.current)(), this.serializer);
|
|
452
443
|
try {
|
|
453
444
|
if (this.onConnectHandler) {
|
|
454
445
|
const result = this.onConnectHandler();
|
|
@@ -464,17 +455,17 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
464
455
|
acceptConnection(connection) {
|
|
465
456
|
this.connections.set(connection.id, connection);
|
|
466
457
|
this.logger.debug(`WS connected: ${connection.id}`);
|
|
467
|
-
const ws
|
|
468
|
-
ws
|
|
458
|
+
const ws = connection.ws;
|
|
459
|
+
ws.on("message", (raw) => {
|
|
469
460
|
this.handleMessage(connection, raw);
|
|
470
461
|
});
|
|
471
|
-
ws
|
|
462
|
+
ws.on("close", () => {
|
|
472
463
|
this.handleClose(connection);
|
|
473
464
|
});
|
|
474
|
-
ws
|
|
465
|
+
ws.on("error", (err) => {
|
|
475
466
|
this.logger.error(`WS error [${connection.id}]:`, err);
|
|
476
467
|
});
|
|
477
|
-
ws
|
|
468
|
+
ws.on("pong", () => {
|
|
478
469
|
connection.alive = true;
|
|
479
470
|
});
|
|
480
471
|
}
|
|
@@ -510,7 +501,7 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
510
501
|
messagePath: path,
|
|
511
502
|
messageEvent: event
|
|
512
503
|
}, () => {
|
|
513
|
-
const msgCtx = (0,
|
|
504
|
+
const msgCtx = (0, _wooksjs_event_core.current)();
|
|
514
505
|
const handlers = this.wooks.lookupHandlers(event, path, msgCtx);
|
|
515
506
|
if (!handlers) {
|
|
516
507
|
if (messageId !== void 0) connection.replyError(messageId, 404, "Not found");
|
|
@@ -578,7 +569,7 @@ var WooksWs = class extends wooks.WooksAdapterBase {
|
|
|
578
569
|
handleClose(connection) {
|
|
579
570
|
this.logger.debug(`WS disconnected: ${connection.id}`);
|
|
580
571
|
if (this.onDisconnectHandler) try {
|
|
581
|
-
const result = (0,
|
|
572
|
+
const result = (0, _wooksjs_event_core.run)(connection.ctx, this.onDisconnectHandler);
|
|
582
573
|
if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
|
|
583
574
|
this.logger.error("onDisconnect error:", error);
|
|
584
575
|
});
|
|
@@ -656,17 +647,17 @@ function createMockWsSocket() {
|
|
|
656
647
|
*/
|
|
657
648
|
function prepareTestWsConnectionContext(options) {
|
|
658
649
|
const id = options?.id ?? "test-conn-id";
|
|
659
|
-
const ws
|
|
660
|
-
const ctx = new
|
|
650
|
+
const ws = createMockWsSocket();
|
|
651
|
+
const ctx = new _wooksjs_event_core.EventContext({
|
|
661
652
|
logger: console,
|
|
662
653
|
...options?.parentCtx ? { parent: options.parentCtx } : {}
|
|
663
654
|
});
|
|
664
655
|
ctx.seed(wsConnectionKind, {
|
|
665
656
|
id,
|
|
666
|
-
ws
|
|
657
|
+
ws
|
|
667
658
|
});
|
|
668
|
-
if (options?.params) ctx.set(
|
|
669
|
-
return (cb) => (0,
|
|
659
|
+
if (options?.params) ctx.set(_wooksjs_event_core.routeParamsKey, options.params);
|
|
660
|
+
return (cb) => (0, _wooksjs_event_core.run)(ctx, cb);
|
|
670
661
|
}
|
|
671
662
|
/**
|
|
672
663
|
* Creates a fully initialized WS message context for testing.
|
|
@@ -675,16 +666,16 @@ function prepareTestWsConnectionContext(options) {
|
|
|
675
666
|
*/
|
|
676
667
|
function prepareTestWsMessageContext(options) {
|
|
677
668
|
const id = options.id ?? "test-conn-id";
|
|
678
|
-
const ws
|
|
679
|
-
const connectionCtx = new
|
|
669
|
+
const ws = createMockWsSocket();
|
|
670
|
+
const connectionCtx = new _wooksjs_event_core.EventContext({
|
|
680
671
|
logger: console,
|
|
681
672
|
...options.parentCtx ? { parent: options.parentCtx } : {}
|
|
682
673
|
});
|
|
683
674
|
connectionCtx.seed(wsConnectionKind, {
|
|
684
675
|
id,
|
|
685
|
-
ws
|
|
676
|
+
ws
|
|
686
677
|
});
|
|
687
|
-
const messageCtx = new
|
|
678
|
+
const messageCtx = new _wooksjs_event_core.EventContext({
|
|
688
679
|
logger: console,
|
|
689
680
|
parent: connectionCtx
|
|
690
681
|
});
|
|
@@ -701,8 +692,8 @@ function prepareTestWsMessageContext(options) {
|
|
|
701
692
|
messagePath: options.path,
|
|
702
693
|
messageEvent: options.event
|
|
703
694
|
});
|
|
704
|
-
if (options.params) messageCtx.set(
|
|
705
|
-
return (cb) => (0,
|
|
695
|
+
if (options.params) messageCtx.set(_wooksjs_event_core.routeParamsKey, options.params);
|
|
696
|
+
return (cb) => (0, _wooksjs_event_core.run)(messageCtx, cb);
|
|
706
697
|
}
|
|
707
698
|
|
|
708
699
|
//#endregion
|
|
@@ -719,13 +710,13 @@ exports.prepareTestWsMessageContext = prepareTestWsMessageContext;
|
|
|
719
710
|
Object.defineProperty(exports, 'useLogger', {
|
|
720
711
|
enumerable: true,
|
|
721
712
|
get: function () {
|
|
722
|
-
return
|
|
713
|
+
return _wooksjs_event_core.useLogger;
|
|
723
714
|
}
|
|
724
715
|
});
|
|
725
716
|
Object.defineProperty(exports, 'useRouteParams', {
|
|
726
717
|
enumerable: true,
|
|
727
718
|
get: function () {
|
|
728
|
-
return
|
|
719
|
+
return _wooksjs_event_core.useRouteParams;
|
|
729
720
|
}
|
|
730
721
|
});
|
|
731
722
|
exports.useWsConnection = useWsConnection;
|
package/dist/index.mjs
CHANGED
|
@@ -51,8 +51,7 @@ function currentConnection(ctx) {
|
|
|
51
51
|
const useWsConnection = defineWook((ctx) => {
|
|
52
52
|
const id = ctx.get(wsConnectionKind.keys.id);
|
|
53
53
|
const ws = ctx.get(wsConnectionKind.keys.ws);
|
|
54
|
-
const
|
|
55
|
-
const connection = state$1.connections.get(id);
|
|
54
|
+
const connection = getAdapterState().connections.get(id);
|
|
56
55
|
return {
|
|
57
56
|
id,
|
|
58
57
|
send(event, path, data, params) {
|
|
@@ -94,25 +93,24 @@ const useWsMessageInternal = defineWook((ctx) => {
|
|
|
94
93
|
const useWsRooms = defineWook((ctx) => {
|
|
95
94
|
const messagePath = ctx.get(wsMessageKind.keys.messagePath);
|
|
96
95
|
const connectionId = ctx.get(wsConnectionKind.keys.id);
|
|
97
|
-
const state
|
|
98
|
-
const connection = state
|
|
96
|
+
const state = getAdapterState();
|
|
97
|
+
const connection = state.connections.get(connectionId);
|
|
99
98
|
return {
|
|
100
99
|
join(room) {
|
|
101
|
-
state
|
|
100
|
+
state.roomManager.join(connection, room ?? messagePath);
|
|
102
101
|
},
|
|
103
102
|
leave(room) {
|
|
104
|
-
state
|
|
103
|
+
state.roomManager.leave(connection, room ?? messagePath);
|
|
105
104
|
},
|
|
106
105
|
broadcast(event, data, options) {
|
|
107
106
|
const room = options?.room ?? messagePath;
|
|
108
107
|
const excludeSelf = options?.excludeSelf ?? true;
|
|
109
108
|
let params;
|
|
110
109
|
try {
|
|
111
|
-
const
|
|
112
|
-
const p = rp.params;
|
|
110
|
+
const p = useRouteParams$1(ctx).params;
|
|
113
111
|
if (Object.keys(p).length > 0) params = p;
|
|
114
112
|
} catch {}
|
|
115
|
-
state
|
|
113
|
+
state.roomManager.broadcast(room, event, room, data, params, excludeSelf ? connection : void 0);
|
|
116
114
|
},
|
|
117
115
|
rooms() {
|
|
118
116
|
return [...connection.rooms];
|
|
@@ -127,19 +125,19 @@ const useWsRooms = defineWook((ctx) => {
|
|
|
127
125
|
* Not a `defineWook` — reads directly from the adapter state.
|
|
128
126
|
*/
|
|
129
127
|
function useWsServer() {
|
|
130
|
-
const state
|
|
128
|
+
const state = getAdapterState();
|
|
131
129
|
return {
|
|
132
130
|
connections() {
|
|
133
|
-
return state
|
|
131
|
+
return state.connections;
|
|
134
132
|
},
|
|
135
133
|
broadcast(event, path, data, params) {
|
|
136
|
-
for (const conn of state
|
|
134
|
+
for (const conn of state.connections.values()) conn.send(event, path, data, params);
|
|
137
135
|
},
|
|
138
136
|
getConnection(id) {
|
|
139
|
-
return state
|
|
137
|
+
return state.connections.get(id);
|
|
140
138
|
},
|
|
141
139
|
roomConnections(room) {
|
|
142
|
-
return state
|
|
140
|
+
return state.roomManager.connections(room);
|
|
143
141
|
}
|
|
144
142
|
};
|
|
145
143
|
}
|
|
@@ -178,13 +176,13 @@ function createDefaultWsServerAdapter() {
|
|
|
178
176
|
//#region packages/event-ws/src/ws-connection.ts
|
|
179
177
|
/** Internal class representing a connected WebSocket client. */
|
|
180
178
|
var WsConnection = class {
|
|
181
|
-
rooms = /* @__PURE__ */ new Set();
|
|
182
|
-
alive = true;
|
|
183
179
|
constructor(id, ws, ctx, serializer) {
|
|
184
180
|
this.id = id;
|
|
185
181
|
this.ws = ws;
|
|
186
182
|
this.ctx = ctx;
|
|
187
183
|
this.serializer = serializer;
|
|
184
|
+
this.rooms = /* @__PURE__ */ new Set();
|
|
185
|
+
this.alive = true;
|
|
188
186
|
}
|
|
189
187
|
/** Send a push message to this connection. */
|
|
190
188
|
send(event, path, data, params) {
|
|
@@ -237,9 +235,9 @@ var WsError = class extends Error {
|
|
|
237
235
|
//#region packages/event-ws/src/ws-room-manager.ts
|
|
238
236
|
/** Manages room → connections mapping with optional distributed broadcast transport. */
|
|
239
237
|
var WsRoomManager = class {
|
|
240
|
-
rooms = /* @__PURE__ */ new Map();
|
|
241
238
|
constructor(transport) {
|
|
242
239
|
this.transport = transport;
|
|
240
|
+
this.rooms = /* @__PURE__ */ new Map();
|
|
243
241
|
if (transport) {}
|
|
244
242
|
}
|
|
245
243
|
/** Add a connection to a room. */
|
|
@@ -321,26 +319,15 @@ const upgradeSocketKey = key("ws:upgrade-socket");
|
|
|
321
319
|
const upgradeHeadKey = key("ws:upgrade-head");
|
|
322
320
|
/** WebSocket adapter for Wooks. Implements WooksUpgradeHandler for HTTP integration. */
|
|
323
321
|
var WooksWs = class extends WooksAdapterBase {
|
|
324
|
-
logger;
|
|
325
|
-
eventContextOptions;
|
|
326
|
-
connections = /* @__PURE__ */ new Map();
|
|
327
|
-
roomManager;
|
|
328
|
-
wsServer;
|
|
329
|
-
opts;
|
|
330
|
-
serializer;
|
|
331
|
-
parser;
|
|
332
|
-
onConnectHandler;
|
|
333
|
-
onDisconnectHandler;
|
|
334
|
-
heartbeatTimer;
|
|
335
|
-
server;
|
|
336
|
-
reqKey = upgradeReqKey;
|
|
337
|
-
socketKey = upgradeSocketKey;
|
|
338
|
-
headKey = upgradeHeadKey;
|
|
339
322
|
constructor(wooksOrOpts, opts) {
|
|
340
323
|
const isWooks = wooksOrOpts instanceof WooksAdapterBase || wooksOrOpts && "getRouter" in wooksOrOpts;
|
|
341
324
|
const wooks = isWooks ? wooksOrOpts : void 0;
|
|
342
325
|
const resolvedOpts = isWooks ? opts ?? {} : wooksOrOpts ?? {};
|
|
343
326
|
super(wooks, resolvedOpts.logger, void 0);
|
|
327
|
+
this.connections = /* @__PURE__ */ new Map();
|
|
328
|
+
this.reqKey = upgradeReqKey;
|
|
329
|
+
this.socketKey = upgradeSocketKey;
|
|
330
|
+
this.headKey = upgradeHeadKey;
|
|
344
331
|
if (wooksOrOpts && typeof wooksOrOpts.ws === "function") wooksOrOpts.ws(this);
|
|
345
332
|
this.opts = resolvedOpts;
|
|
346
333
|
this.logger = resolvedOpts.logger || this.getLogger(`[96m[wooks-ws]`);
|
|
@@ -348,8 +335,7 @@ var WooksWs = class extends WooksAdapterBase {
|
|
|
348
335
|
this.serializer = resolvedOpts.messageSerializer ?? JSON.stringify;
|
|
349
336
|
this.parser = resolvedOpts.messageParser ?? defaultParser;
|
|
350
337
|
this.roomManager = new WsRoomManager(resolvedOpts.broadcastTransport);
|
|
351
|
-
|
|
352
|
-
this.wsServer = adapter.create();
|
|
338
|
+
this.wsServer = (resolvedOpts.wsServerAdapter ?? createDefaultWsServerAdapter()).create();
|
|
353
339
|
setAdapterState({
|
|
354
340
|
connections: this.connections,
|
|
355
341
|
roomManager: this.roomManager,
|
|
@@ -424,8 +410,7 @@ var WooksWs = class extends WooksAdapterBase {
|
|
|
424
410
|
id,
|
|
425
411
|
ws
|
|
426
412
|
}, () => {
|
|
427
|
-
const
|
|
428
|
-
const connection = new WsConnection(id, ws, connectionCtx, this.serializer);
|
|
413
|
+
const connection = new WsConnection(id, ws, current(), this.serializer);
|
|
429
414
|
try {
|
|
430
415
|
if (this.onConnectHandler) {
|
|
431
416
|
const result = this.onConnectHandler();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-ws",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.12",
|
|
4
4
|
"description": "WebSocket adapter for Wooks with path-based message routing, rooms, and broadcasting",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -42,15 +42,15 @@
|
|
|
42
42
|
"@types/ws": "^8.18.1",
|
|
43
43
|
"typescript": "^5.9.3",
|
|
44
44
|
"vitest": "^3.2.4",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
45
|
+
"@wooksjs/event-core": "^0.7.12",
|
|
46
|
+
"wooks": "^0.7.12"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"@prostojs/logger": "^0.4.3",
|
|
50
|
-
"@prostojs/router": "^0.3.
|
|
50
|
+
"@prostojs/router": "^0.3.4",
|
|
51
51
|
"ws": "^8.0.0",
|
|
52
|
-
"@wooksjs/event-core": "^0.7.
|
|
53
|
-
"wooks": "^0.7.
|
|
52
|
+
"@wooksjs/event-core": "^0.7.12",
|
|
53
|
+
"wooks": "^0.7.12"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "rolldown -c ../../rolldown.config.mjs"
|