@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/types-DdNb8cAz.d.ts
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { StateTransition, TransitionListener as TransitionListener$1, PeerId } from '@kyneta/transport';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Websocket ready states — mirrors the standard WebSocket readyState
|
|
5
|
-
* values as human-readable strings.
|
|
6
|
-
*/
|
|
7
|
-
type SocketReadyState = "connecting" | "open" | "closing" | "closed";
|
|
8
|
-
/**
|
|
9
|
-
* Framework-agnostic Websocket interface.
|
|
10
|
-
*
|
|
11
|
-
* This allows the adapter to work with any Websocket library:
|
|
12
|
-
* - Browser `WebSocket` via `wrapStandardWebsocket()`
|
|
13
|
-
* - Node.js `ws` library via `wrapNodeWebsocket()`
|
|
14
|
-
* - Bun `ServerWebSocket` via `wrapBunWebsocket()`
|
|
15
|
-
*
|
|
16
|
-
* The interface is intentionally minimal — only the operations the
|
|
17
|
-
* adapter needs are exposed.
|
|
18
|
-
*/
|
|
19
|
-
interface Socket {
|
|
20
|
-
/** Send binary or text data through the Websocket. */
|
|
21
|
-
send(data: Uint8Array | string): void;
|
|
22
|
-
/** Close the Websocket connection. */
|
|
23
|
-
close(code?: number, reason?: string): void;
|
|
24
|
-
/** Register a handler for incoming messages (binary or text). */
|
|
25
|
-
onMessage(handler: (data: Uint8Array | string) => void): void;
|
|
26
|
-
/** Register a handler for connection close. */
|
|
27
|
-
onClose(handler: (code: number, reason: string) => void): void;
|
|
28
|
-
/** Register a handler for errors. */
|
|
29
|
-
onError(handler: (error: Error) => void): void;
|
|
30
|
-
/** The current ready state of the Websocket. */
|
|
31
|
-
readonly readyState: SocketReadyState;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Options for handling a new Websocket connection on the server.
|
|
35
|
-
*/
|
|
36
|
-
interface WebsocketConnectionOptions {
|
|
37
|
-
/** The Websocket instance, wrapped in the Socket interface. */
|
|
38
|
-
socket: Socket;
|
|
39
|
-
/** Optional peer ID extracted from the upgrade request. */
|
|
40
|
-
peerId?: PeerId;
|
|
41
|
-
/** Optional authentication token from the upgrade request. */
|
|
42
|
-
authToken?: string;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Handle for an active Websocket connection.
|
|
46
|
-
*/
|
|
47
|
-
interface WebsocketConnectionHandle {
|
|
48
|
-
/** The peer ID for this connection. */
|
|
49
|
-
readonly peerId: PeerId;
|
|
50
|
-
/** The channel ID for this connection. */
|
|
51
|
-
readonly channelId: number;
|
|
52
|
-
/** Close the connection. */
|
|
53
|
-
close(code?: number, reason?: string): void;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Result of handling a Websocket connection on the server.
|
|
57
|
-
*/
|
|
58
|
-
interface WebsocketConnectionResult {
|
|
59
|
-
/** The connection handle for managing this peer. */
|
|
60
|
-
connection: WebsocketConnectionHandle;
|
|
61
|
-
/** Call this to start processing messages. */
|
|
62
|
-
start(): void;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Discriminated union describing why a Websocket connection was lost.
|
|
66
|
-
*/
|
|
67
|
-
type DisconnectReason = {
|
|
68
|
-
type: "intentional";
|
|
69
|
-
} | {
|
|
70
|
-
type: "error";
|
|
71
|
-
error: Error;
|
|
72
|
-
} | {
|
|
73
|
-
type: "closed";
|
|
74
|
-
code: number;
|
|
75
|
-
reason: string;
|
|
76
|
-
} | {
|
|
77
|
-
type: "max-retries-exceeded";
|
|
78
|
-
attempts: number;
|
|
79
|
-
} | {
|
|
80
|
-
type: "not-started";
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* All possible states of the Websocket client.
|
|
84
|
-
*
|
|
85
|
-
* State machine transitions:
|
|
86
|
-
* ```
|
|
87
|
-
* disconnected → connecting → connected → ready
|
|
88
|
-
* ↓ ↓ ↓
|
|
89
|
-
* reconnecting ← ─ ┴ ─ ─ ─ ─ ┘
|
|
90
|
-
* ↓
|
|
91
|
-
* connecting (retry)
|
|
92
|
-
* ↓
|
|
93
|
-
* disconnected (max retries)
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
type WebsocketClientState = {
|
|
97
|
-
status: "disconnected";
|
|
98
|
-
reason?: DisconnectReason;
|
|
99
|
-
} | {
|
|
100
|
-
status: "connecting";
|
|
101
|
-
attempt: number;
|
|
102
|
-
} | {
|
|
103
|
-
status: "connected";
|
|
104
|
-
} | {
|
|
105
|
-
status: "ready";
|
|
106
|
-
} | {
|
|
107
|
-
status: "reconnecting";
|
|
108
|
-
attempt: number;
|
|
109
|
-
nextAttemptMs: number;
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* A state transition event for websocket client states.
|
|
113
|
-
* Specialized from the generic `StateTransition<S>`.
|
|
114
|
-
*/
|
|
115
|
-
type WebsocketClientStateTransition = StateTransition<WebsocketClientState>;
|
|
116
|
-
/**
|
|
117
|
-
* Listener for websocket client state transitions.
|
|
118
|
-
* Specialized from the generic `TransitionListener<S>`.
|
|
119
|
-
*/
|
|
120
|
-
type TransitionListener = TransitionListener$1<WebsocketClientState>;
|
|
121
|
-
/**
|
|
122
|
-
* Wrap a standard `WebSocket` (browser or Node.js `ws` via `ws` package
|
|
123
|
-
* in `WebSocket`-compatible mode) into the `Socket` interface.
|
|
124
|
-
*
|
|
125
|
-
* Handles `ArrayBuffer`, `Blob`, and string messages.
|
|
126
|
-
*/
|
|
127
|
-
declare function wrapStandardWebsocket(ws: WebSocket): Socket;
|
|
128
|
-
/**
|
|
129
|
-
* The minimal interface we need from the Node.js `ws` library's `WebSocket`.
|
|
130
|
-
*
|
|
131
|
-
* Using a structural type rather than importing `ws` — consumers provide
|
|
132
|
-
* the actual `ws` instance, we just need these methods.
|
|
133
|
-
*/
|
|
134
|
-
interface NodeWebsocketLike {
|
|
135
|
-
send(data: Uint8Array | string): void;
|
|
136
|
-
close(code?: number, reason?: string): void;
|
|
137
|
-
on(event: "message", handler: (data: Buffer | ArrayBuffer | string, isBinary: boolean) => void): void;
|
|
138
|
-
on(event: "close", handler: (code: number, reason: Buffer) => void): void;
|
|
139
|
-
on(event: "error", handler: (error: Error) => void): void;
|
|
140
|
-
readyState: number;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Wrap a Node.js `ws` library WebSocket into the `Socket` interface.
|
|
144
|
-
*
|
|
145
|
-
* Handles `Buffer` → `Uint8Array` conversion for binary messages.
|
|
146
|
-
*/
|
|
147
|
-
declare function wrapNodeWebsocket(ws: NodeWebsocketLike): Socket;
|
|
148
|
-
|
|
149
|
-
export { type DisconnectReason as D, type NodeWebsocketLike as N, type Socket as S, type TransitionListener as T, type WebsocketClientState as W, type SocketReadyState as a, type WebsocketClientStateTransition as b, type WebsocketConnectionOptions as c, type WebsocketConnectionResult as d, type WebsocketConnectionHandle as e, wrapNodeWebsocket as f, wrapStandardWebsocket as w };
|