@kyneta/websocket-transport 1.3.0 → 1.4.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 +28 -15
- package/dist/browser.d.ts +59 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +2 -0
- package/dist/bun.d.ts +22 -22
- package/dist/bun.d.ts.map +1 -0
- package/dist/bun.js +95 -43
- package/dist/bun.js.map +1 -1
- package/dist/client-transport-CKjXedwS.d.ts +134 -0
- package/dist/client-transport-CKjXedwS.d.ts.map +1 -0
- package/dist/client-transport-DIZ-LJxs.js +510 -0
- package/dist/client-transport-DIZ-LJxs.js.map +1 -0
- package/dist/server.d.ts +154 -115
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +298 -296
- package/dist/server.js.map +1 -1
- package/dist/types-CJAcr1Df.d.ts +199 -0
- package/dist/types-CJAcr1Df.d.ts.map +1 -0
- package/package.json +15 -15
- package/src/__tests__/client-transport.test.ts +167 -0
- package/src/{client.ts → browser.ts} +15 -10
- package/src/bun-websocket.ts +5 -5
- package/src/bun.ts +1 -1
- package/src/client-transport.ts +42 -67
- package/src/connection.ts +1 -1
- package/src/server-transport.ts +10 -10
- package/src/server.ts +22 -4
- package/src/service-client.ts +52 -0
- package/src/types.ts +74 -6
- package/dist/chunk-PSG3LLT5.js +0 -111
- package/dist/chunk-PSG3LLT5.js.map +0 -1
- package/dist/client.d.ts +0 -207
- package/dist/client.js +0 -508
- package/dist/client.js.map +0 -1
- package/dist/types-DdNb8cAz.d.ts +0 -149
package/dist/client.d.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import { Program, TransitionListener } from '@kyneta/machine';
|
|
2
|
-
import { ReconnectOptions, PeerId, Transport, GeneratedChannel, TransportFactory } from '@kyneta/transport';
|
|
3
|
-
import { W as WebsocketClientState, b as WebsocketClientStateTransition, D as DisconnectReason } from './types-DdNb8cAz.js';
|
|
4
|
-
export { S as Socket, a as SocketReadyState, T as TransitionListener, w as wrapStandardWebsocket } from './types-DdNb8cAz.js';
|
|
5
|
-
|
|
6
|
-
type WsClientMsg = {
|
|
7
|
-
type: "start";
|
|
8
|
-
} | {
|
|
9
|
-
type: "socket-opened";
|
|
10
|
-
} | {
|
|
11
|
-
type: "server-ready";
|
|
12
|
-
} | {
|
|
13
|
-
type: "socket-closed";
|
|
14
|
-
code: number;
|
|
15
|
-
reason: string;
|
|
16
|
-
} | {
|
|
17
|
-
type: "socket-error";
|
|
18
|
-
error: Error;
|
|
19
|
-
} | {
|
|
20
|
-
type: "reconnect-timer-fired";
|
|
21
|
-
} | {
|
|
22
|
-
type: "stop";
|
|
23
|
-
};
|
|
24
|
-
type WsClientEffect = {
|
|
25
|
-
type: "create-websocket";
|
|
26
|
-
attempt: number;
|
|
27
|
-
} | {
|
|
28
|
-
type: "close-websocket";
|
|
29
|
-
} | {
|
|
30
|
-
type: "add-channel-and-establish";
|
|
31
|
-
} | {
|
|
32
|
-
type: "remove-channel";
|
|
33
|
-
} | {
|
|
34
|
-
type: "start-reconnect-timer";
|
|
35
|
-
delayMs: number;
|
|
36
|
-
} | {
|
|
37
|
-
type: "cancel-reconnect-timer";
|
|
38
|
-
} | {
|
|
39
|
-
type: "start-keepalive";
|
|
40
|
-
} | {
|
|
41
|
-
type: "stop-keepalive";
|
|
42
|
-
};
|
|
43
|
-
interface WsClientProgramOptions {
|
|
44
|
-
reconnect?: Partial<ReconnectOptions>;
|
|
45
|
-
/** Inject jitter source for deterministic testing. Default: () => Math.random() * 1000 */
|
|
46
|
-
jitterFn?: () => number;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Create the websocket client connection lifecycle program — a pure Mealy machine.
|
|
50
|
-
*
|
|
51
|
-
* The returned `Program<WsClientMsg, WebsocketClientState, WsClientEffect>`
|
|
52
|
-
* encodes every state transition and effect as inspectable data. The imperative
|
|
53
|
-
* shell interprets `WsClientEffect` as actual I/O.
|
|
54
|
-
*/
|
|
55
|
-
declare function createWsClientProgram(options?: WsClientProgramOptions): Program<WsClientMsg, WebsocketClientState, WsClientEffect>;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Default fragment threshold in bytes.
|
|
59
|
-
* AWS API Gateway has a 128KB limit, so 100KB provides a safe margin.
|
|
60
|
-
*/
|
|
61
|
-
declare const DEFAULT_FRAGMENT_THRESHOLD: number;
|
|
62
|
-
/**
|
|
63
|
-
* Options for the Websocket client transport (browser connections).
|
|
64
|
-
*/
|
|
65
|
-
interface WebsocketClientOptions {
|
|
66
|
-
/** Websocket URL to connect to. Can be a string or a function of peerId. */
|
|
67
|
-
url: string | ((peerId: PeerId) => string);
|
|
68
|
-
/** Optional custom WebSocket implementation (for Node.js or testing). */
|
|
69
|
-
WebSocket?: typeof globalThis.WebSocket;
|
|
70
|
-
/** Reconnection options. */
|
|
71
|
-
reconnect?: {
|
|
72
|
-
enabled?: boolean;
|
|
73
|
-
maxAttempts?: number;
|
|
74
|
-
baseDelay?: number;
|
|
75
|
-
maxDelay?: number;
|
|
76
|
-
};
|
|
77
|
-
/** Keepalive interval in ms (default: 30000). */
|
|
78
|
-
keepaliveInterval?: number;
|
|
79
|
-
/**
|
|
80
|
-
* Fragment threshold in bytes. Messages larger than this are fragmented.
|
|
81
|
-
* Set to 0 to disable fragmentation (not recommended for cloud deployments).
|
|
82
|
-
* Default: 100KB
|
|
83
|
-
*/
|
|
84
|
-
fragmentThreshold?: number;
|
|
85
|
-
/** Lifecycle event callbacks. */
|
|
86
|
-
lifecycle?: WebsocketClientLifecycleEvents;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Lifecycle event callbacks for the Websocket client.
|
|
90
|
-
*/
|
|
91
|
-
interface WebsocketClientLifecycleEvents {
|
|
92
|
-
/** Called on every state transition (delivered async via microtask). */
|
|
93
|
-
onStateChange?: (transition: WebsocketClientStateTransition) => void;
|
|
94
|
-
/** Called when the connection is lost. */
|
|
95
|
-
onDisconnect?: (reason: DisconnectReason) => void;
|
|
96
|
-
/** Called when a reconnection attempt is scheduled. */
|
|
97
|
-
onReconnecting?: (attempt: number, nextAttemptMs: number) => void;
|
|
98
|
-
/** Called when reconnection succeeds after a previous connection. */
|
|
99
|
-
onReconnected?: () => void;
|
|
100
|
-
/** Called when the server sends the "ready" signal. */
|
|
101
|
-
onReady?: () => void;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Options for service-to-service Websocket connections.
|
|
105
|
-
* Extends WebsocketClientOptions with header support for authentication.
|
|
106
|
-
*
|
|
107
|
-
* Note: Headers are a Bun/Node-specific extension. The browser WebSocket API
|
|
108
|
-
* does not support custom headers per the WHATWG spec.
|
|
109
|
-
*/
|
|
110
|
-
interface ServiceWebsocketClientOptions extends WebsocketClientOptions {
|
|
111
|
-
/**
|
|
112
|
-
* Headers to send during Websocket upgrade.
|
|
113
|
-
* Used for authentication in service-to-service communication.
|
|
114
|
-
*/
|
|
115
|
-
headers?: Record<string, string>;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Websocket client network transport for @kyneta/exchange.
|
|
119
|
-
*
|
|
120
|
-
* Connects to a Websocket server, sends and receives ChannelMsg via
|
|
121
|
-
* the kyneta wire format (CBOR codec + framing + fragmentation).
|
|
122
|
-
*
|
|
123
|
-
* Internally, the connection lifecycle is a `Program<Msg, Model, Fx>` —
|
|
124
|
-
* a pure Mealy machine whose transitions are deterministically testable.
|
|
125
|
-
* This class is the imperative shell that interprets data effects as I/O.
|
|
126
|
-
*
|
|
127
|
-
* Prefer the factory functions for construction:
|
|
128
|
-
* - `createWebsocketClient()` — browser-to-server
|
|
129
|
-
* - `createServiceWebsocketClient()` — service-to-service (with headers)
|
|
130
|
-
*/
|
|
131
|
-
declare class WebsocketClientTransport extends Transport<void> {
|
|
132
|
-
#private;
|
|
133
|
-
constructor(options: ServiceWebsocketClientOptions);
|
|
134
|
-
/**
|
|
135
|
-
* Get the current connection state.
|
|
136
|
-
*/
|
|
137
|
-
getState(): WebsocketClientState;
|
|
138
|
-
/**
|
|
139
|
-
* Subscribe to state transitions.
|
|
140
|
-
*/
|
|
141
|
-
subscribeToTransitions(listener: TransitionListener<WebsocketClientState>): () => void;
|
|
142
|
-
/**
|
|
143
|
-
* Wait for a specific state.
|
|
144
|
-
*/
|
|
145
|
-
waitForState(predicate: (state: WebsocketClientState) => boolean, options?: {
|
|
146
|
-
timeoutMs?: number;
|
|
147
|
-
}): Promise<WebsocketClientState>;
|
|
148
|
-
/**
|
|
149
|
-
* Wait for a specific status.
|
|
150
|
-
*/
|
|
151
|
-
waitForStatus(status: WebsocketClientState["status"], options?: {
|
|
152
|
-
timeoutMs?: number;
|
|
153
|
-
}): Promise<WebsocketClientState>;
|
|
154
|
-
/**
|
|
155
|
-
* Whether the client is ready (server ready signal received).
|
|
156
|
-
*/
|
|
157
|
-
get isReady(): boolean;
|
|
158
|
-
protected generate(): GeneratedChannel;
|
|
159
|
-
onStart(): Promise<void>;
|
|
160
|
-
onStop(): Promise<void>;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Create a Websocket client transport factory for browser-to-server
|
|
164
|
-
* connections.
|
|
165
|
-
*
|
|
166
|
-
* Returns an `TransportFactory` — a closure that creates a fresh transport
|
|
167
|
-
* instance when called. Pass directly to `Exchange({ transports: [...] })`.
|
|
168
|
-
*
|
|
169
|
-
* @example
|
|
170
|
-
* ```typescript
|
|
171
|
-
* import { createWebsocketClient } from "@kyneta/websocket-transport/client"
|
|
172
|
-
*
|
|
173
|
-
* const exchange = new Exchange({
|
|
174
|
-
* transports: [createWebsocketClient({
|
|
175
|
-
* url: "ws://localhost:3000/ws",
|
|
176
|
-
* reconnect: { enabled: true },
|
|
177
|
-
* })],
|
|
178
|
-
* })
|
|
179
|
-
* ```
|
|
180
|
-
*/
|
|
181
|
-
declare function createWebsocketClient(options: WebsocketClientOptions): TransportFactory;
|
|
182
|
-
/**
|
|
183
|
-
* Create a Websocket client transport for service-to-service connections.
|
|
184
|
-
*
|
|
185
|
-
* This factory is for backend environments (Bun, Node.js) where you need
|
|
186
|
-
* to pass authentication headers during the Websocket upgrade.
|
|
187
|
-
*
|
|
188
|
-
* Note: Headers are a Bun/Node-specific extension. The browser WebSocket API
|
|
189
|
-
* does not support custom headers. For browser clients, use
|
|
190
|
-
* `createWebsocketClient()` and authenticate via URL query parameters.
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```typescript
|
|
194
|
-
* import { createServiceWebsocketClient } from "@kyneta/websocket-transport/client"
|
|
195
|
-
*
|
|
196
|
-
* const exchange = new Exchange({
|
|
197
|
-
* transports: [createServiceWebsocketClient({
|
|
198
|
-
* url: "ws://primary-server:3000/ws",
|
|
199
|
-
* headers: { Authorization: "Bearer token" },
|
|
200
|
-
* reconnect: { enabled: true },
|
|
201
|
-
* })],
|
|
202
|
-
* })
|
|
203
|
-
* ```
|
|
204
|
-
*/
|
|
205
|
-
declare function createServiceWebsocketClient(options: ServiceWebsocketClientOptions): TransportFactory;
|
|
206
|
-
|
|
207
|
-
export { DEFAULT_FRAGMENT_THRESHOLD, DisconnectReason, type ServiceWebsocketClientOptions, type WebsocketClientLifecycleEvents, type WebsocketClientOptions, WebsocketClientState, WebsocketClientStateTransition, WebsocketClientTransport, type WsClientEffect, type WsClientMsg, type WsClientProgramOptions, createServiceWebsocketClient, createWebsocketClient, createWsClientProgram };
|