@wooksjs/event-ws 0.7.1 → 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 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,30 +437,28 @@ 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
- const ctxOptions = {
432
- ...this.eventContextOptions,
433
- ...parentCtx ? { parent: parentCtx } : {}
434
- };
435
- const connectionCtx = new __wooksjs_event_core.EventContext(ctxOptions);
436
- const connection = new WsConnection(id, ws$1, connectionCtx, this.serializer);
437
- connectionCtx.seed(wsConnectionKind, {
446
+ createWsConnectionContext(ctxOptions, {
438
447
  id,
439
448
  ws: ws$1
440
- });
441
- try {
442
- if (this.onConnectHandler) {
443
- const result = (0, __wooksjs_event_core.run)(connectionCtx, 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;
449
+ }, () => {
450
+ const connectionCtx = (0, __wooksjs_event_core.current)();
451
+ const connection = new WsConnection(id, ws$1, connectionCtx, this.serializer);
452
+ try {
453
+ if (this.onConnectHandler) {
454
+ const result = this.onConnectHandler();
455
+ if (result !== null && result !== void 0 && typeof result.then === "function") return result.then(() => this.acceptConnection(connection)).catch((error) => this.rejectConnection(connection, error));
447
456
  }
457
+ this.acceptConnection(connection);
458
+ } catch (error) {
459
+ this.rejectConnection(connection, error);
448
460
  }
449
- this.acceptConnection(connection);
450
- } catch (error) {
451
- this.rejectConnection(connection, error);
452
- }
461
+ });
453
462
  });
454
463
  }
455
464
  acceptConnection(connection) {
@@ -491,18 +500,17 @@ var WooksWs = class extends wooks.WooksAdapterBase {
491
500
  return;
492
501
  }
493
502
  const { event, path, data, id: messageId } = msg;
494
- const msgCtx = new __wooksjs_event_core.EventContext({
503
+ createWsMessageContext({
495
504
  ...this.eventContextOptions,
496
505
  parent: connection.ctx
497
- });
498
- msgCtx.seed(wsMessageKind, {
506
+ }, {
499
507
  data,
500
508
  rawMessage: raw,
501
509
  messageId,
502
510
  messagePath: path,
503
511
  messageEvent: event
504
- });
505
- (0, __wooksjs_event_core.run)(msgCtx, () => {
512
+ }, () => {
513
+ const msgCtx = (0, __wooksjs_event_core.current)();
506
514
  const handlers = this.wooks.lookupHandlers(event, path, msgCtx);
507
515
  if (!handlers) {
508
516
  if (messageId !== void 0) connection.replyError(messageId, 404, "Not found");
@@ -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
@@ -1,4 +1,4 @@
1
- import { EventContext, current, defineEventKind, defineWook, key, routeParamsKey, run, slot, useLogger, useRouteParams, useRouteParams as useRouteParams$1 } from "@wooksjs/event-core";
1
+ import { EventContext, createEventContext, current, defineEventKind, defineWook, key, routeParamsKey, run, slot, useLogger, useRouteParams, useRouteParams as useRouteParams$1 } from "@wooksjs/event-core";
2
2
  import http from "http";
3
3
  import { WooksAdapterBase } from "wooks";
4
4
  import { WebSocketServer } from "ws";
@@ -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,30 +414,28 @@ 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
- const ctxOptions = {
409
- ...this.eventContextOptions,
410
- ...parentCtx ? { parent: parentCtx } : {}
411
- };
412
- const connectionCtx = new EventContext(ctxOptions);
413
- const connection = new WsConnection(id, ws, connectionCtx, this.serializer);
414
- connectionCtx.seed(wsConnectionKind, {
423
+ createWsConnectionContext(ctxOptions, {
415
424
  id,
416
425
  ws
417
- });
418
- try {
419
- if (this.onConnectHandler) {
420
- const result = run(connectionCtx, 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;
426
+ }, () => {
427
+ const connectionCtx = current();
428
+ const connection = new WsConnection(id, ws, connectionCtx, this.serializer);
429
+ try {
430
+ if (this.onConnectHandler) {
431
+ const result = this.onConnectHandler();
432
+ if (result !== null && result !== void 0 && typeof result.then === "function") return result.then(() => this.acceptConnection(connection)).catch((error) => this.rejectConnection(connection, error));
424
433
  }
434
+ this.acceptConnection(connection);
435
+ } catch (error) {
436
+ this.rejectConnection(connection, error);
425
437
  }
426
- this.acceptConnection(connection);
427
- } catch (error) {
428
- this.rejectConnection(connection, error);
429
- }
438
+ });
430
439
  });
431
440
  }
432
441
  acceptConnection(connection) {
@@ -468,18 +477,17 @@ var WooksWs = class extends WooksAdapterBase {
468
477
  return;
469
478
  }
470
479
  const { event, path, data, id: messageId } = msg;
471
- const msgCtx = new EventContext({
480
+ createWsMessageContext({
472
481
  ...this.eventContextOptions,
473
482
  parent: connection.ctx
474
- });
475
- msgCtx.seed(wsMessageKind, {
483
+ }, {
476
484
  data,
477
485
  rawMessage: raw,
478
486
  messageId,
479
487
  messagePath: path,
480
488
  messageEvent: event
481
- });
482
- run(msgCtx, () => {
489
+ }, () => {
490
+ const msgCtx = current();
483
491
  const handlers = this.wooks.lookupHandlers(event, path, msgCtx);
484
492
  if (!handlers) {
485
493
  if (messageId !== void 0) connection.replyError(messageId, 404, "Not found");
@@ -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.1",
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.1",
51
- "wooks": "^0.7.1"
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.1",
58
- "wooks": "^0.7.1"
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",
@@ -31,6 +31,9 @@ import {
31
31
  useWsRooms,
32
32
  useWsServer,
33
33
  currentConnection,
34
+ // context factories
35
+ createWsConnectionContext,
36
+ createWsMessageContext,
34
37
  // kinds & types
35
38
  wsConnectionKind,
36
39
  wsMessageKind,
@@ -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. Seeded with `id` and `ws` socket.
10
- 2. **Message context** (`ws:message` kind) — short-lived, one per incoming message. Its `parent` is the connection context.
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