@nice-code/action 0.6.3 → 0.7.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/build/index.js CHANGED
@@ -2956,6 +2956,9 @@ function createBinaryWsSessionFactory(domains, options) {
2956
2956
  };
2957
2957
  };
2958
2958
  }
2959
+ // src/ActionRuntime/Handler/ExternalClient/Transport/WebSocket/secureWsChannel.ts
2960
+ import { ClientCryptoKeyLink } from "@nice-code/util";
2961
+
2959
2962
  // src/utils/decodeActionFrame.ts
2960
2963
  function decodeActionFrame(frame, decoder) {
2961
2964
  const decoded = decoder?.incoming?.(frame) ?? (typeof frame === "string" ? parseJsonActionFrame(frame) : undefined);
@@ -3313,6 +3316,44 @@ class WebSocketTransport extends Transport {
3313
3316
  };
3314
3317
  }
3315
3318
  }
3319
+
3320
+ // src/ActionRuntime/Handler/ExternalClient/Transport/WebSocket/secureWsChannel.ts
3321
+ function deriveDictionaryVersion(domains) {
3322
+ const { intToRoute } = buildActionRouteDictionary(domains);
3323
+ const signature = intToRoute.map((route) => `${route.domain}:${route.id}`).join(",");
3324
+ let hash = 2166136261;
3325
+ for (let i = 0;i < signature.length; i++) {
3326
+ hash ^= signature.charCodeAt(i);
3327
+ hash = Math.imul(hash, 16777619);
3328
+ }
3329
+ return `auto:${(hash >>> 0).toString(16).padStart(8, "0")}`;
3330
+ }
3331
+ function defineSecureWsChannel(options) {
3332
+ return {
3333
+ dictionaryVersion: options.dictionaryVersion ?? deriveDictionaryVersion(options.domains),
3334
+ createCodec: createBinaryWsSessionFactory(options.domains, options.sessionOptions)
3335
+ };
3336
+ }
3337
+ function createSecureWebSocketTransport(options) {
3338
+ const link = new ClientCryptoKeyLink({ storageAdapter: options.storageAdapter });
3339
+ return WebSocketTransport.create({
3340
+ createWebSocket: options.createWebSocket ?? (() => {
3341
+ const ws = new WebSocket(options.url);
3342
+ ws.binaryType = "arraybuffer";
3343
+ return ws;
3344
+ }),
3345
+ getTransportCacheKey: options.getTransportCacheKey ?? (() => [options.url]),
3346
+ createFormatMessage: options.channel.createCodec,
3347
+ updateRunConfig: options.updateRunConfig,
3348
+ getRouteInfo: options.getRouteInfo,
3349
+ security: {
3350
+ securityLevel: options.securityLevel,
3351
+ link,
3352
+ localCoordinate: options.runtime.coordinate.toJsonObject(),
3353
+ dictionaryVersion: options.channel.dictionaryVersion
3354
+ }
3355
+ });
3356
+ }
3316
3357
  // src/ActionRuntime/Handler/Server/ActionServerHandler.ts
3317
3358
  class ActionServerHandler extends ActionExternalClientHandler {
3318
3359
  _formatMessage;
@@ -3365,6 +3406,9 @@ class ActionServerHandler extends ActionExternalClientHandler {
3365
3406
  _setIncomingActionDataListener(listener) {
3366
3407
  this._incomingListeners.push(listener);
3367
3408
  }
3409
+ setOnConnectionBound(onConnectionBound) {
3410
+ this._onConnectionBound = onConnectionBound;
3411
+ }
3368
3412
  receive(connection, frame) {
3369
3413
  if (this._security == null || !this._handshakeMode) {
3370
3414
  this._receivePlain(connection, frame);
@@ -3627,6 +3671,42 @@ class ActionServerHandler extends ActionExternalClientHandler {
3627
3671
  var createServerHandler = (options) => {
3628
3672
  return new ActionServerHandler(options);
3629
3673
  };
3674
+ // src/ActionRuntime/Handler/Server/createSecureActionServer.ts
3675
+ import { ClientCryptoKeyLink as ClientCryptoKeyLink2 } from "@nice-code/util";
3676
+ var DEFAULT_SERVER_SECURITY_LEVELS = [
3677
+ "none" /* none */,
3678
+ "authenticated" /* authenticated */,
3679
+ "encrypted" /* encrypted */
3680
+ ];
3681
+ function createSecureActionServerHandler(options) {
3682
+ const link = new ClientCryptoKeyLink2({ storageAdapter: options.storageAdapter });
3683
+ return new ActionServerHandler({
3684
+ clientEnv: options.clientEnv,
3685
+ createFormatMessage: options.channel.createCodec,
3686
+ send: options.send,
3687
+ defaultTimeout: options.defaultTimeout,
3688
+ security: {
3689
+ securityLevel: options.securityLevel ?? DEFAULT_SERVER_SECURITY_LEVELS,
3690
+ link,
3691
+ localCoordinate: options.runtime.coordinate.toJsonObject(),
3692
+ dictionaryVersion: options.channel.dictionaryVersion,
3693
+ verifyKeyResolver: options.verifyKeyResolver ?? createStorageTofuVerifyKeyResolver(options.storageAdapter)
3694
+ }
3695
+ });
3696
+ }
3697
+ function createHibernatableWsServerAdapter(options) {
3698
+ const { handler, getWebSockets, getAttachment, setAttachment } = options;
3699
+ handler.setOnConnectionBound(setAttachment);
3700
+ for (const connection of getWebSockets()) {
3701
+ const binding = getAttachment(connection);
3702
+ if (binding != null)
3703
+ handler.rehydrateConnection(connection, binding);
3704
+ }
3705
+ return {
3706
+ receive: (connection, frame) => handler.receive(connection, frame),
3707
+ drop: (connection) => handler.dropConnection(connection)
3708
+ };
3709
+ }
3630
3710
  export {
3631
3711
  runtimeLinkId,
3632
3712
  isActionPayload_Result_JsonObject,
@@ -3637,13 +3717,17 @@ export {
3637
3717
  err_nice_external_client,
3638
3718
  err_nice_action,
3639
3719
  encodeHandshakeMessage,
3720
+ defineSecureWsChannel,
3640
3721
  decodeHandshakeMessage,
3641
3722
  decodeActionFrame,
3642
3723
  createStorageTofuVerifyKeyResolver,
3643
3724
  createServerHandshake,
3644
3725
  createServerHandler,
3726
+ createSecureWebSocketTransport,
3727
+ createSecureActionServerHandler,
3645
3728
  createLocalHandler,
3646
3729
  createInMemoryTofuVerifyKeyResolver,
3730
+ createHibernatableWsServerAdapter,
3647
3731
  createExternalClientHandler,
3648
3732
  createClientHandshake,
3649
3733
  createBinaryWsSessionFactory,
@@ -0,0 +1,63 @@
1
+ import { type StorageAdapter } from "@nice-code/util";
2
+ import type { ActionDomain } from "../../../../../ActionDefinition/Domain/ActionDomain";
3
+ import type { ActionRuntime } from "../../../../ActionRuntime";
4
+ import type { ITransportRouteActionParams, ITransportRouteInfo, TUpdateActionRunConfig } from "../Transport.types";
5
+ import { ESecurityLevel } from "./actionWsHandshake";
6
+ import { type IBinaryWsSessionOptions } from "./createBinaryWsSessionFactory";
7
+ import type { IActionTransportReadyData_Ws } from "./TransportWebSocket.types";
8
+ import { WebSocketTransport } from "./WebSocketTransport";
9
+ /** The per-connection binary session codec — built once per socket from the channel's domains. */
10
+ type TChannelCodec = NonNullable<IActionTransportReadyData_Ws["formatMessage"]>;
11
+ /**
12
+ * The shared identity of a secure WebSocket channel: the wire dictionary version both ends check
13
+ * during the handshake, plus the per-connection codec factory both ends build from the *same* domain
14
+ * list. Define it once (typically in code shared by client and server) and hand it to
15
+ * {@link createSecureWebSocketTransport} on the client and `createSecureActionServerHandler` on the
16
+ * server, so the codec and version can never drift apart.
17
+ */
18
+ export interface ISecureWsChannel {
19
+ /** Wire dictionary version — derived from the domains by default; the handshake rejects a mismatch. */
20
+ dictionaryVersion: string;
21
+ /** Per-connection session codec factory (call once per live connection). */
22
+ createCodec: () => TChannelCodec;
23
+ }
24
+ /**
25
+ * Bundle a secure channel's shared identity from its transported domains. Both ends MUST call this
26
+ * with the same domains in the same order (the binary wire dictionary is positional). The
27
+ * `dictionaryVersion` is derived from those domains unless you pin an explicit one.
28
+ */
29
+ export declare function defineSecureWsChannel(options: {
30
+ /** Domains transported over this channel, in a stable order. Add new ones to the *end*. */
31
+ domains: ActionDomain<any>[];
32
+ /** Pin a human-readable version instead of the derived hash (must match on both ends). */
33
+ dictionaryVersion?: string;
34
+ /** Tuning for the per-connection binary session (e.g. correlation TTL). */
35
+ sessionOptions?: IBinaryWsSessionOptions;
36
+ }): ISecureWsChannel;
37
+ export interface ISecureWebSocketTransportOptions {
38
+ /** The shared channel identity (codec + dictionary version). */
39
+ channel: ISecureWsChannel;
40
+ /** This client's runtime — its coordinate is the authenticated identity sent in the handshake. */
41
+ runtime: ActionRuntime;
42
+ /** Backing store for this client's crypto identity (a stable verify key across reloads). */
43
+ storageAdapter: StorageAdapter;
44
+ /** The level this client requests; the server must allow it. */
45
+ securityLevel: ESecurityLevel;
46
+ /** Endpoint URL — drives both the socket and the per-endpoint cache key. */
47
+ url: string;
48
+ /** Override socket creation (defaults to a `new WebSocket(url)` with `binaryType = "arraybuffer"`). */
49
+ createWebSocket?: (input: ITransportRouteActionParams) => WebSocket;
50
+ /** Override the reuse key (defaults to `[url]`, so one socket is shared per endpoint). */
51
+ getTransportCacheKey?: (input: ITransportRouteActionParams) => string[];
52
+ updateRunConfig?: TUpdateActionRunConfig;
53
+ getRouteInfo?: (input: ITransportRouteActionParams) => ITransportRouteInfo;
54
+ }
55
+ /**
56
+ * Build a {@link WebSocketTransport} for the secure binary channel with the boilerplate folded in: it
57
+ * creates the {@link ClientCryptoKeyLink} from `storageAdapter`, opens an `arraybuffer` socket to
58
+ * `url`, caches it per endpoint, installs the channel's per-connection codec, and assembles the
59
+ * `security` block from the runtime coordinate + channel version. Pass `createWebSocket` /
60
+ * `getTransportCacheKey` to take over those bits when you need to.
61
+ */
62
+ export declare function createSecureWebSocketTransport(options: ISecureWebSocketTransportOptions): WebSocketTransport;
63
+ export {};
@@ -117,7 +117,7 @@ export declare class ActionServerHandler<TConn = unknown> extends ActionExternal
117
117
  private readonly _createFormatMessage?;
118
118
  private readonly _send;
119
119
  private readonly _serverTimeout;
120
- private readonly _onConnectionBound?;
120
+ private _onConnectionBound?;
121
121
  /** Incoming-data listeners installed by the runtime (`resolveIncomingActionPayload`). */
122
122
  private readonly _incomingListeners;
123
123
  private readonly _security?;
@@ -142,6 +142,12 @@ export declare class ActionServerHandler<TConn = unknown> extends ActionExternal
142
142
  */
143
143
  private _codecFor;
144
144
  _setIncomingActionDataListener(listener: (json: TActionPayload_Any_JsonObject<any>) => void): void;
145
+ /**
146
+ * Register (or replace) the connection-bound persistence callback after construction. Used by
147
+ * lifecycle helpers like {@link createHibernatableWsServerAdapter} so persistence and replay are
148
+ * owned by one place instead of being split across the constructor options.
149
+ */
150
+ setOnConnectionBound(onConnectionBound: (connection: TConn, binding: IActionServerConnectionBinding) => void): void;
145
151
  /**
146
152
  * Feed one inbound frame from a connection into the runtime. Decodes text or binary, binds the
147
153
  * connection to the requesting client's identity, then routes it (requests execute locally;
@@ -0,0 +1,71 @@
1
+ import { type StorageAdapter } from "@nice-code/util";
2
+ import type { ActionRuntime } from "../../ActionRuntime";
3
+ import type { RuntimeCoordinate } from "../../RuntimeCoordinate";
4
+ import { ESecurityLevel, type IClientVerifyKeyResolver } from "../ExternalClient/Transport/WebSocket/actionWsHandshake";
5
+ import type { ISecureWsChannel } from "../ExternalClient/Transport/WebSocket/secureWsChannel";
6
+ import { ActionServerHandler, type IActionServerConnectionBinding } from "./ActionServerHandler";
7
+ export interface ISecureActionServerHandlerOptions<TConn> {
8
+ /** The shared channel identity (codec + dictionary version) — same one the clients use. */
9
+ channel: ISecureWsChannel;
10
+ /**
11
+ * Coordinate of the *connecting clients* (typically env-only, e.g. `RuntimeCoordinate.env("web_app")`),
12
+ * used to route results/pushes back over this handler.
13
+ */
14
+ clientEnv: RuntimeCoordinate;
15
+ /** This server's runtime — its coordinate is the server identity presented in the handshake. */
16
+ runtime: ActionRuntime;
17
+ /**
18
+ * One backing store for the server's crypto identity *and* its trust-on-first-use verify-key pins.
19
+ * Their keys don't collide, so a single adapter is enough; back it with persistent storage (e.g. a
20
+ * Durable Object's storage) so identity and pins survive eviction.
21
+ */
22
+ storageAdapter: StorageAdapter;
23
+ /** Write an encoded frame to a specific live connection (e.g. `(ws, frame) => ws.send(frame)`). */
24
+ send: (connection: TConn, frame: string | Uint8Array | ArrayBuffer) => void;
25
+ /** Accepted level(s); defaults to negotiating any of none/authenticated/encrypted. */
26
+ securityLevel?: ESecurityLevel | readonly ESecurityLevel[];
27
+ /** Trust decision for a client's verify key; defaults to storage-backed TOFU over `storageAdapter`. */
28
+ verifyKeyResolver?: IClientVerifyKeyResolver;
29
+ /** Timeout (ms) applied to server-initiated actions awaiting a client response. */
30
+ defaultTimeout?: number;
31
+ }
32
+ /**
33
+ * Build an {@link ActionServerHandler} for the secure binary channel with the boilerplate folded in:
34
+ * it creates the {@link ClientCryptoKeyLink} and the storage-backed TOFU resolver from a single
35
+ * `storageAdapter`, installs the channel's per-connection codec, and assembles the `security` block
36
+ * from the runtime coordinate + channel version (accepting all three levels by default).
37
+ *
38
+ * For a hibernatable transport (e.g. a Durable Object), pair it with
39
+ * {@link createHibernatableWsServerAdapter} to wire persistence + replay.
40
+ */
41
+ export declare function createSecureActionServerHandler<TConn = unknown>(options: ISecureActionServerHandlerOptions<TConn>): ActionServerHandler<TConn>;
42
+ export interface IHibernatableWsServerAdapterOptions<TConn> {
43
+ /** The handler to drive (from {@link createSecureActionServerHandler} or `createServerHandler`). */
44
+ handler: ActionServerHandler<TConn>;
45
+ /** All currently-live connections — replayed on construction to rebuild bindings after a wake. */
46
+ getWebSockets: () => TConn[];
47
+ /** Read a connection's persisted binding (e.g. `(ws) => ws.deserializeAttachment()`). */
48
+ getAttachment: (connection: TConn) => IActionServerConnectionBinding | undefined;
49
+ /** Persist a connection's binding when it is bound (e.g. `(ws, b) => ws.serializeAttachment(b)`). */
50
+ setAttachment: (connection: TConn, binding: IActionServerConnectionBinding) => void;
51
+ }
52
+ export interface IHibernatableWsServerAdapter<TConn> {
53
+ /** Feed one inbound frame from a connection into the handler. */
54
+ receive: (connection: TConn, frame: string | ArrayBuffer | Uint8Array) => void;
55
+ /** Forget a connection (call on socket close/error). */
56
+ drop: (connection: TConn) => void;
57
+ }
58
+ /**
59
+ * Wire the hibernation lifecycle for a server handler on a transport whose sockets outlive process
60
+ * eviction (e.g. a Durable Object's hibernatable WebSockets). It owns persistence end to end:
61
+ * registers `setAttachment` as the handler's connection-bound callback and immediately replays every
62
+ * live connection's stored binding via `getAttachment`, so results/pushes still route after a wake.
63
+ *
64
+ * Construct it once when the handler is built, then forward socket events:
65
+ * ```ts
66
+ * const wsServer = createHibernatableWsServerAdapter({ handler, getWebSockets, getAttachment, setAttachment });
67
+ * // webSocketMessage(ws, msg) => wsServer.receive(ws, msg);
68
+ * // webSocketClose/Error(ws) => wsServer.drop(ws);
69
+ * ```
70
+ */
71
+ export declare function createHibernatableWsServerAdapter<TConn>(options: IHibernatableWsServerAdapterOptions<TConn>): IHibernatableWsServerAdapter<TConn>;
@@ -26,10 +26,12 @@ export { createActionFrameCrypto, type IActionFrameCrypto, type IActionFrameCryp
26
26
  export { createClientHandshake, createInMemoryTofuVerifyKeyResolver, createServerHandshake, createStorageTofuVerifyKeyResolver, decodeHandshakeMessage, EHandshakeMessageType, ESecurityLevel, encodeHandshakeMessage, type IClientHandshakeConfig, type IClientVerifyKeyResolveInput, type IClientVerifyKeyResolver, type IHandshakeEncryptionKeyMaterial, type IHandshakeResult, type IServerHandshakeConfig, runtimeLinkId, type THandshakeMessage, } from "./ActionRuntime/Handler/ExternalClient/Transport/WebSocket/actionWsHandshake";
27
27
  export { createBinaryWsAdapter } from "./ActionRuntime/Handler/ExternalClient/Transport/WebSocket/createBinaryWsAdapter";
28
28
  export { createBinaryWsSessionFactory, type IBinaryWsSessionOptions, } from "./ActionRuntime/Handler/ExternalClient/Transport/WebSocket/createBinaryWsSessionFactory";
29
+ export { createSecureWebSocketTransport, defineSecureWsChannel, type ISecureWebSocketTransportOptions, type ISecureWsChannel, } from "./ActionRuntime/Handler/ExternalClient/Transport/WebSocket/secureWsChannel";
29
30
  export type { IActionTransportDef_Ws, IActionTransportInitialized_Ws, IActionTransportReadyData_Ws, } from "./ActionRuntime/Handler/ExternalClient/Transport/WebSocket/TransportWebSocket.types";
30
31
  export { type IWebSocketTransportAdvancedOptions, type IWebSocketTransportSocketOptions, type TWebSocketTransportOptions, WebSocketTransport, } from "./ActionRuntime/Handler/ExternalClient/Transport/WebSocket/WebSocketTransport";
31
32
  export { ActionLocalHandler, createLocalHandler, } from "./ActionRuntime/Handler/Local/ActionLocalHandler";
32
33
  export { ActionServerHandler, createServerHandler, type IActionServerConnectionBinding, type IActionServerHandlerOptions, type TActionChannelFormatMessage, type TActionConnectionEncoding, } from "./ActionRuntime/Handler/Server/ActionServerHandler";
34
+ export { createHibernatableWsServerAdapter, createSecureActionServerHandler, type IHibernatableWsServerAdapter, type IHibernatableWsServerAdapterOptions, type ISecureActionServerHandlerOptions, } from "./ActionRuntime/Handler/Server/createSecureActionServer";
33
35
  export * from "./ActionRuntime/RuntimeCoordinate";
34
36
  export { EErrId_NiceAction, err_nice_action } from "./errors/err_nice_action";
35
37
  export { decodeActionFrame, type IActionFrameDecoder } from "./utils/decodeActionFrame";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice-code/action",
3
- "version": "0.6.3",
3
+ "version": "0.7.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -44,9 +44,9 @@
44
44
  "build-types": "tsc --project tsconfig.build.json"
45
45
  },
46
46
  "dependencies": {
47
- "@nice-code/common-errors": "0.6.3",
48
- "@nice-code/error": "0.6.3",
49
- "@nice-code/util": "0.6.3",
47
+ "@nice-code/common-errors": "0.7.0",
48
+ "@nice-code/error": "0.7.0",
49
+ "@nice-code/util": "0.7.0",
50
50
  "@standard-schema/spec": "^1.1.0",
51
51
  "@tanstack/react-virtual": "^3.13.26",
52
52
  "http-status-codes": "^2.3.0",