crossws 0.4.1 → 0.4.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/_chunks/_request.mjs +82 -0
- package/dist/_chunks/_types.d.mts +24 -0
- package/dist/_chunks/adapter.d.mts +174 -0
- package/dist/_chunks/adapter.mjs +93 -0
- package/dist/_chunks/bun.d.mts +38 -0
- package/dist/_chunks/cloudflare.d.mts +45 -0
- package/dist/_chunks/deno.d.mts +17 -0
- package/dist/_chunks/error.mjs +10 -0
- package/dist/_chunks/libs/ws.mjs +3531 -0
- package/dist/_chunks/node.d.mts +299 -0
- package/dist/_chunks/peer.mjs +244 -0
- package/dist/_chunks/rolldown-runtime.mjs +32 -0
- package/dist/_chunks/sse.d.mts +12 -0
- package/dist/_chunks/web.d.mts +298 -0
- package/dist/adapters/bun.d.mts +2 -41
- package/dist/adapters/bun.mjs +83 -93
- package/dist/adapters/cloudflare.d.mts +2 -46
- package/dist/adapters/cloudflare.mjs +173 -218
- package/dist/adapters/deno.d.mts +2 -19
- package/dist/adapters/deno.mjs +65 -74
- package/dist/adapters/node.d.mts +2 -299
- package/dist/adapters/node.mjs +119 -156
- package/dist/adapters/sse.d.mts +2 -13
- package/dist/adapters/sse.mjs +98 -118
- package/dist/adapters/uws.d.mts +44 -44
- package/dist/adapters/uws.mjs +152 -175
- package/dist/index.d.mts +2 -170
- package/dist/index.mjs +3 -1
- package/dist/server/bun.d.mts +8 -21
- package/dist/server/bun.mjs +24 -31
- package/dist/server/cloudflare.d.mts +8 -21
- package/dist/server/cloudflare.mjs +21 -30
- package/dist/server/default.d.mts +8 -21
- package/dist/server/default.mjs +22 -26
- package/dist/server/deno.d.mts +8 -21
- package/dist/server/deno.mjs +21 -24
- package/dist/server/node.d.mts +8 -21
- package/dist/server/node.mjs +32 -43
- package/dist/websocket/native.d.mts +3 -2
- package/dist/websocket/native.mjs +4 -1
- package/dist/websocket/node.d.mts +3 -2
- package/dist/websocket/node.mjs +7 -13
- package/dist/websocket/sse.d.mts +34 -34
- package/dist/websocket/sse.mjs +112 -123
- package/package.json +36 -28
- package/adapters/cloudflare-durable.d.ts +0 -2
- package/dist/shared/crossws.95-eYp2D.d.mts +0 -23
- package/dist/shared/crossws.B31KJMcF.mjs +0 -83
- package/dist/shared/crossws.BQXMA5bH.d.mts +0 -297
- package/dist/shared/crossws.By9qWDAI.mjs +0 -8
- package/dist/shared/crossws.CPlNx7g8.mjs +0 -105
- package/dist/shared/crossws.CipVM6lf.mjs +0 -4973
- package/dist/shared/crossws.WpyOHUXc.mjs +0 -330
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapter.mjs";
|
|
2
|
+
import { EventEmitter } from "events";
|
|
3
|
+
import { Agent, ClientRequest, ClientRequestArgs, IncomingMessage, OutgoingHttpHeaders, Server } from "node:http";
|
|
4
|
+
import { Duplex, DuplexOptions } from "node:stream";
|
|
5
|
+
import { Server as Server$1 } from "node:https";
|
|
6
|
+
import { SecureContextOptions } from "node:tls";
|
|
7
|
+
import { URL } from "node:url";
|
|
8
|
+
import { ZlibOptions } from "node:zlib";
|
|
9
|
+
|
|
10
|
+
//#region types/ws.d.ts
|
|
11
|
+
type BufferLike = string | Buffer | DataView | number | ArrayBufferView | Uint8Array | ArrayBuffer | SharedArrayBuffer | readonly any[] | readonly number[] | {
|
|
12
|
+
valueOf(): ArrayBuffer;
|
|
13
|
+
} | {
|
|
14
|
+
valueOf(): SharedArrayBuffer;
|
|
15
|
+
} | {
|
|
16
|
+
valueOf(): Uint8Array;
|
|
17
|
+
} | {
|
|
18
|
+
valueOf(): readonly number[];
|
|
19
|
+
} | {
|
|
20
|
+
valueOf(): string;
|
|
21
|
+
} | {
|
|
22
|
+
[Symbol.toPrimitive](hint: string): string;
|
|
23
|
+
};
|
|
24
|
+
declare class WebSocket extends EventEmitter {
|
|
25
|
+
static readonly createWebSocketStream: typeof createWebSocketStream;
|
|
26
|
+
static readonly WebSocketServer: WebSocketServer;
|
|
27
|
+
static readonly Server: typeof Server$2;
|
|
28
|
+
static readonly WebSocket: typeof WebSocket;
|
|
29
|
+
/** The connection is not yet open. */
|
|
30
|
+
static readonly CONNECTING: 0;
|
|
31
|
+
/** The connection is open and ready to communicate. */
|
|
32
|
+
static readonly OPEN: 1;
|
|
33
|
+
/** The connection is in the process of closing. */
|
|
34
|
+
static readonly CLOSING: 2;
|
|
35
|
+
/** The connection is closed. */
|
|
36
|
+
static readonly CLOSED: 3;
|
|
37
|
+
binaryType: "nodebuffer" | "arraybuffer" | "fragments";
|
|
38
|
+
readonly bufferedAmount: number;
|
|
39
|
+
readonly extensions: string;
|
|
40
|
+
/** Indicates whether the websocket is paused */
|
|
41
|
+
readonly isPaused: boolean;
|
|
42
|
+
readonly protocol: string;
|
|
43
|
+
/** The current state of the connection */
|
|
44
|
+
readonly readyState: typeof WebSocket.CONNECTING | typeof WebSocket.OPEN | typeof WebSocket.CLOSING | typeof WebSocket.CLOSED;
|
|
45
|
+
readonly url: string;
|
|
46
|
+
/** The connection is not yet open. */
|
|
47
|
+
readonly CONNECTING: 0;
|
|
48
|
+
/** The connection is open and ready to communicate. */
|
|
49
|
+
readonly OPEN: 1;
|
|
50
|
+
/** The connection is in the process of closing. */
|
|
51
|
+
readonly CLOSING: 2;
|
|
52
|
+
/** The connection is closed. */
|
|
53
|
+
readonly CLOSED: 3;
|
|
54
|
+
onopen: ((event: Event) => void) | null;
|
|
55
|
+
onerror: ((event: ErrorEvent) => void) | null;
|
|
56
|
+
onclose: ((event: CloseEvent) => void) | null;
|
|
57
|
+
onmessage: ((event: MessageEvent) => void) | null;
|
|
58
|
+
constructor(address: null);
|
|
59
|
+
constructor(address: string | URL, options?: ClientOptions | ClientRequestArgs);
|
|
60
|
+
constructor(address: string | URL, protocols?: string | string[], options?: ClientOptions | ClientRequestArgs);
|
|
61
|
+
close(code?: number, data?: string | Buffer): void;
|
|
62
|
+
ping(data?: any, mask?: boolean, cb?: (err: Error) => void): void;
|
|
63
|
+
pong(data?: any, mask?: boolean, cb?: (err: Error) => void): void;
|
|
64
|
+
send(data: BufferLike, cb?: (err?: Error) => void): void;
|
|
65
|
+
send(data: BufferLike, options: {
|
|
66
|
+
mask?: boolean | undefined;
|
|
67
|
+
binary?: boolean | undefined;
|
|
68
|
+
compress?: boolean | undefined;
|
|
69
|
+
fin?: boolean | undefined;
|
|
70
|
+
}, cb?: (err?: Error) => void): void;
|
|
71
|
+
terminate(): void;
|
|
72
|
+
/**
|
|
73
|
+
* Pause the websocket causing it to stop emitting events. Some events can still be
|
|
74
|
+
* emitted after this is called, until all buffered data is consumed. This method
|
|
75
|
+
* is a noop if the ready state is `CONNECTING` or `CLOSED`.
|
|
76
|
+
*/
|
|
77
|
+
pause(): void;
|
|
78
|
+
/**
|
|
79
|
+
* Make a paused socket resume emitting events. This method is a noop if the ready
|
|
80
|
+
* state is `CONNECTING` or `CLOSED`.
|
|
81
|
+
*/
|
|
82
|
+
resume(): void;
|
|
83
|
+
addEventListener(method: "message", cb: (event: MessageEvent) => void, options?: EventListenerOptions): void;
|
|
84
|
+
addEventListener(method: "close", cb: (event: CloseEvent) => void, options?: EventListenerOptions): void;
|
|
85
|
+
addEventListener(method: "error", cb: (event: ErrorEvent) => void, options?: EventListenerOptions): void;
|
|
86
|
+
addEventListener(method: "open", cb: (event: Event) => void, options?: EventListenerOptions): void;
|
|
87
|
+
removeEventListener(method: "message", cb: (event: MessageEvent) => void): void;
|
|
88
|
+
removeEventListener(method: "close", cb: (event: CloseEvent) => void): void;
|
|
89
|
+
removeEventListener(method: "error", cb: (event: ErrorEvent) => void): void;
|
|
90
|
+
removeEventListener(method: "open", cb: (event: Event) => void): void;
|
|
91
|
+
on(event: "close", listener: (this: WebSocket, code: number, reason: Buffer) => void): this;
|
|
92
|
+
on(event: "error", listener: (this: WebSocket, err: Error) => void): this;
|
|
93
|
+
on(event: "upgrade", listener: (this: WebSocket, request: IncomingMessage) => void): this;
|
|
94
|
+
on(event: "message", listener: (this: WebSocket, data: RawData, isBinary: boolean) => void): this;
|
|
95
|
+
on(event: "open", listener: (this: WebSocket) => void): this;
|
|
96
|
+
on(event: "ping" | "pong", listener: (this: WebSocket, data: Buffer) => void): this;
|
|
97
|
+
on(event: "unexpected-response", listener: (this: WebSocket, request: ClientRequest, response: IncomingMessage) => void): this;
|
|
98
|
+
on(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;
|
|
99
|
+
once(event: "close", listener: (this: WebSocket, code: number, reason: Buffer) => void): this;
|
|
100
|
+
once(event: "error", listener: (this: WebSocket, err: Error) => void): this;
|
|
101
|
+
once(event: "upgrade", listener: (this: WebSocket, request: IncomingMessage) => void): this;
|
|
102
|
+
once(event: "message", listener: (this: WebSocket, data: RawData, isBinary: boolean) => void): this;
|
|
103
|
+
once(event: "open", listener: (this: WebSocket) => void): this;
|
|
104
|
+
once(event: "ping" | "pong", listener: (this: WebSocket, data: Buffer) => void): this;
|
|
105
|
+
once(event: "unexpected-response", listener: (this: WebSocket, request: ClientRequest, response: IncomingMessage) => void): this;
|
|
106
|
+
once(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;
|
|
107
|
+
off(event: "close", listener: (this: WebSocket, code: number, reason: Buffer) => void): this;
|
|
108
|
+
off(event: "error", listener: (this: WebSocket, err: Error) => void): this;
|
|
109
|
+
off(event: "upgrade", listener: (this: WebSocket, request: IncomingMessage) => void): this;
|
|
110
|
+
off(event: "message", listener: (this: WebSocket, data: RawData, isBinary: boolean) => void): this;
|
|
111
|
+
off(event: "open", listener: (this: WebSocket) => void): this;
|
|
112
|
+
off(event: "ping" | "pong", listener: (this: WebSocket, data: Buffer) => void): this;
|
|
113
|
+
off(event: "unexpected-response", listener: (this: WebSocket, request: ClientRequest, response: IncomingMessage) => void): this;
|
|
114
|
+
off(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;
|
|
115
|
+
addListener(event: "close", listener: (code: number, reason: Buffer) => void): this;
|
|
116
|
+
addListener(event: "error", listener: (err: Error) => void): this;
|
|
117
|
+
addListener(event: "upgrade", listener: (request: IncomingMessage) => void): this;
|
|
118
|
+
addListener(event: "message", listener: (data: RawData, isBinary: boolean) => void): this;
|
|
119
|
+
addListener(event: "open", listener: () => void): this;
|
|
120
|
+
addListener(event: "ping" | "pong", listener: (data: Buffer) => void): this;
|
|
121
|
+
addListener(event: "unexpected-response", listener: (request: ClientRequest, response: IncomingMessage) => void): this;
|
|
122
|
+
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
123
|
+
removeListener(event: "close", listener: (code: number, reason: Buffer) => void): this;
|
|
124
|
+
removeListener(event: "error", listener: (err: Error) => void): this;
|
|
125
|
+
removeListener(event: "upgrade", listener: (request: IncomingMessage) => void): this;
|
|
126
|
+
removeListener(event: "message", listener: (data: RawData, isBinary: boolean) => void): this;
|
|
127
|
+
removeListener(event: "open", listener: () => void): this;
|
|
128
|
+
removeListener(event: "ping" | "pong", listener: (data: Buffer) => void): this;
|
|
129
|
+
removeListener(event: "unexpected-response", listener: (request: ClientRequest, response: IncomingMessage) => void): this;
|
|
130
|
+
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Data represents the raw message payload received over the
|
|
134
|
+
*/
|
|
135
|
+
type RawData = Buffer | ArrayBuffer | Buffer[];
|
|
136
|
+
/**
|
|
137
|
+
* Data represents the message payload received over the
|
|
138
|
+
*/
|
|
139
|
+
type Data = string | Buffer | ArrayBuffer | Buffer[];
|
|
140
|
+
/**
|
|
141
|
+
* CertMeta represents the accepted types for certificate & key data.
|
|
142
|
+
*/
|
|
143
|
+
type CertMeta = string | string[] | Buffer | Buffer[];
|
|
144
|
+
/**
|
|
145
|
+
* VerifyClientCallbackSync is a synchronous callback used to inspect the
|
|
146
|
+
* incoming message. The return value (boolean) of the function determines
|
|
147
|
+
* whether or not to accept the handshake.
|
|
148
|
+
*/
|
|
149
|
+
type VerifyClientCallbackSync<Request extends IncomingMessage = IncomingMessage> = (info: {
|
|
150
|
+
origin: string;
|
|
151
|
+
secure: boolean;
|
|
152
|
+
req: Request;
|
|
153
|
+
}) => boolean;
|
|
154
|
+
/**
|
|
155
|
+
* VerifyClientCallbackAsync is an asynchronous callback used to inspect the
|
|
156
|
+
* incoming message. The return value (boolean) of the function determines
|
|
157
|
+
* whether or not to accept the handshake.
|
|
158
|
+
*/
|
|
159
|
+
type VerifyClientCallbackAsync<Request extends IncomingMessage = IncomingMessage> = (info: {
|
|
160
|
+
origin: string;
|
|
161
|
+
secure: boolean;
|
|
162
|
+
req: Request;
|
|
163
|
+
}, callback: (res: boolean, code?: number, message?: string, headers?: OutgoingHttpHeaders) => void) => void;
|
|
164
|
+
interface ClientOptions extends SecureContextOptions {
|
|
165
|
+
protocol?: string | undefined;
|
|
166
|
+
followRedirects?: boolean | undefined;
|
|
167
|
+
generateMask?(mask: Buffer): void;
|
|
168
|
+
handshakeTimeout?: number | undefined;
|
|
169
|
+
maxRedirects?: number | undefined;
|
|
170
|
+
perMessageDeflate?: boolean | PerMessageDeflateOptions | undefined;
|
|
171
|
+
localAddress?: string | undefined;
|
|
172
|
+
protocolVersion?: number | undefined;
|
|
173
|
+
headers?: {
|
|
174
|
+
[key: string]: string;
|
|
175
|
+
} | undefined;
|
|
176
|
+
origin?: string | undefined;
|
|
177
|
+
agent?: Agent | undefined;
|
|
178
|
+
host?: string | undefined;
|
|
179
|
+
family?: number | undefined;
|
|
180
|
+
checkServerIdentity?(servername: string, cert: CertMeta): boolean;
|
|
181
|
+
rejectUnauthorized?: boolean | undefined;
|
|
182
|
+
maxPayload?: number | undefined;
|
|
183
|
+
skipUTF8Validation?: boolean | undefined;
|
|
184
|
+
}
|
|
185
|
+
interface PerMessageDeflateOptions {
|
|
186
|
+
serverNoContextTakeover?: boolean | undefined;
|
|
187
|
+
clientNoContextTakeover?: boolean | undefined;
|
|
188
|
+
serverMaxWindowBits?: number | undefined;
|
|
189
|
+
clientMaxWindowBits?: number | undefined;
|
|
190
|
+
zlibDeflateOptions?: {
|
|
191
|
+
flush?: number | undefined;
|
|
192
|
+
finishFlush?: number | undefined;
|
|
193
|
+
chunkSize?: number | undefined;
|
|
194
|
+
windowBits?: number | undefined;
|
|
195
|
+
level?: number | undefined;
|
|
196
|
+
memLevel?: number | undefined;
|
|
197
|
+
strategy?: number | undefined;
|
|
198
|
+
dictionary?: Buffer | Buffer[] | DataView | undefined;
|
|
199
|
+
info?: boolean | undefined;
|
|
200
|
+
} | undefined;
|
|
201
|
+
zlibInflateOptions?: ZlibOptions | undefined;
|
|
202
|
+
threshold?: number | undefined;
|
|
203
|
+
concurrencyLimit?: number | undefined;
|
|
204
|
+
}
|
|
205
|
+
interface Event {
|
|
206
|
+
type: string;
|
|
207
|
+
target: WebSocket;
|
|
208
|
+
}
|
|
209
|
+
interface ErrorEvent {
|
|
210
|
+
error: any;
|
|
211
|
+
message: string;
|
|
212
|
+
type: string;
|
|
213
|
+
target: WebSocket;
|
|
214
|
+
}
|
|
215
|
+
interface CloseEvent {
|
|
216
|
+
wasClean: boolean;
|
|
217
|
+
code: number;
|
|
218
|
+
reason: string;
|
|
219
|
+
type: string;
|
|
220
|
+
target: WebSocket;
|
|
221
|
+
}
|
|
222
|
+
interface MessageEvent {
|
|
223
|
+
data: Data;
|
|
224
|
+
type: string;
|
|
225
|
+
target: WebSocket;
|
|
226
|
+
}
|
|
227
|
+
interface EventListenerOptions {
|
|
228
|
+
once?: boolean | undefined;
|
|
229
|
+
}
|
|
230
|
+
interface ServerOptions<U extends typeof WebSocket = typeof WebSocket, V extends typeof IncomingMessage = typeof IncomingMessage> {
|
|
231
|
+
host?: string | undefined;
|
|
232
|
+
port?: number | undefined;
|
|
233
|
+
backlog?: number | undefined;
|
|
234
|
+
server?: Server<V> | Server$1<V> | undefined;
|
|
235
|
+
verifyClient?: VerifyClientCallbackAsync<InstanceType<V>> | VerifyClientCallbackSync<InstanceType<V>> | undefined;
|
|
236
|
+
handleProtocols?: (protocols: Set<string>, request: InstanceType<V>) => string | false;
|
|
237
|
+
path?: string | undefined;
|
|
238
|
+
noServer?: boolean | undefined;
|
|
239
|
+
clientTracking?: boolean | undefined;
|
|
240
|
+
perMessageDeflate?: boolean | PerMessageDeflateOptions | undefined;
|
|
241
|
+
maxPayload?: number | undefined;
|
|
242
|
+
skipUTF8Validation?: boolean | undefined;
|
|
243
|
+
WebSocket?: U | undefined;
|
|
244
|
+
}
|
|
245
|
+
interface AddressInfo {
|
|
246
|
+
address: string;
|
|
247
|
+
family: string;
|
|
248
|
+
port: number;
|
|
249
|
+
}
|
|
250
|
+
declare class Server$2<T extends typeof WebSocket = typeof WebSocket, U extends typeof IncomingMessage = typeof IncomingMessage> extends EventEmitter {
|
|
251
|
+
options: ServerOptions<T, U>;
|
|
252
|
+
path: string;
|
|
253
|
+
clients: Set<InstanceType<T>>;
|
|
254
|
+
constructor(options?: ServerOptions<T, U>, callback?: () => void);
|
|
255
|
+
address(): AddressInfo | string;
|
|
256
|
+
close(cb?: (err?: Error) => void): void;
|
|
257
|
+
handleUpgrade(request: InstanceType<U>, socket: Duplex, upgradeHead: Buffer, callback: (client: InstanceType<T>, request: InstanceType<U>) => void): void;
|
|
258
|
+
shouldHandle(request: InstanceType<U>): boolean | Promise<boolean>;
|
|
259
|
+
on(event: "connection", cb: (this: Server$2<T>, socket: InstanceType<T>, request: InstanceType<U>) => void): this;
|
|
260
|
+
on(event: "error", cb: (this: Server$2<T>, error: Error) => void): this;
|
|
261
|
+
on(event: "headers", cb: (this: Server$2<T>, headers: string[], request: InstanceType<U>) => void): this;
|
|
262
|
+
on(event: "close" | "listening", cb: (this: Server$2<T>) => void): this;
|
|
263
|
+
on(event: string | symbol, listener: (this: Server$2<T>, ...args: any[]) => void): this;
|
|
264
|
+
once(event: "connection", cb: (this: Server$2<T>, socket: InstanceType<T>, request: InstanceType<U>) => void): this;
|
|
265
|
+
once(event: "error", cb: (this: Server$2<T>, error: Error) => void): this;
|
|
266
|
+
once(event: "headers", cb: (this: Server$2<T>, headers: string[], request: InstanceType<U>) => void): this;
|
|
267
|
+
once(event: "close" | "listening", cb: (this: Server$2<T>) => void): this;
|
|
268
|
+
once(event: string | symbol, listener: (this: Server$2<T>, ...args: any[]) => void): this;
|
|
269
|
+
off(event: "connection", cb: (this: Server$2<T>, socket: InstanceType<T>, request: InstanceType<U>) => void): this;
|
|
270
|
+
off(event: "error", cb: (this: Server$2<T>, error: Error) => void): this;
|
|
271
|
+
off(event: "headers", cb: (this: Server$2<T>, headers: string[], request: InstanceType<U>) => void): this;
|
|
272
|
+
off(event: "close" | "listening", cb: (this: Server$2<T>) => void): this;
|
|
273
|
+
off(event: string | symbol, listener: (this: Server$2<T>, ...args: any[]) => void): this;
|
|
274
|
+
addListener(event: "connection", cb: (client: InstanceType<T>, request: InstanceType<U>) => void): this;
|
|
275
|
+
addListener(event: "error", cb: (err: Error) => void): this;
|
|
276
|
+
addListener(event: "headers", cb: (headers: string[], request: InstanceType<U>) => void): this;
|
|
277
|
+
addListener(event: "close" | "listening", cb: () => void): this;
|
|
278
|
+
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
279
|
+
removeListener(event: "connection", cb: (client: InstanceType<T>, request: InstanceType<U>) => void): this;
|
|
280
|
+
removeListener(event: "error", cb: (err: Error) => void): this;
|
|
281
|
+
removeListener(event: "headers", cb: (headers: string[], request: InstanceType<U>) => void): this;
|
|
282
|
+
removeListener(event: "close" | "listening", cb: () => void): this;
|
|
283
|
+
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
284
|
+
}
|
|
285
|
+
type WebSocketServer = Server$2;
|
|
286
|
+
declare function createWebSocketStream(websocket: WebSocket, options?: DuplexOptions): Duplex;
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/adapters/node.d.ts
|
|
289
|
+
interface NodeAdapter extends AdapterInstance {
|
|
290
|
+
handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer, webRequest?: Request): Promise<void>;
|
|
291
|
+
closeAll: (code?: number, data?: string | Buffer, force?: boolean) => void;
|
|
292
|
+
}
|
|
293
|
+
interface NodeOptions extends AdapterOptions {
|
|
294
|
+
wss?: WebSocketServer;
|
|
295
|
+
serverOptions?: ServerOptions;
|
|
296
|
+
}
|
|
297
|
+
declare const nodeAdapter: Adapter<NodeAdapter, NodeOptions>;
|
|
298
|
+
//#endregion
|
|
299
|
+
export { NodeOptions as n, nodeAdapter as r, NodeAdapter as t };
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
//#region src/utils.ts
|
|
2
|
+
const kNodeInspect = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
|
|
3
|
+
function toBufferLike(val) {
|
|
4
|
+
if (val === void 0 || val === null) return "";
|
|
5
|
+
const type = typeof val;
|
|
6
|
+
if (type === "string") return val;
|
|
7
|
+
if (type === "number" || type === "boolean" || type === "bigint") return val.toString();
|
|
8
|
+
if (type === "function" || type === "symbol") return "{}";
|
|
9
|
+
if (val instanceof Uint8Array || val instanceof ArrayBuffer) return val;
|
|
10
|
+
if (isPlainObject(val)) return JSON.stringify(val);
|
|
11
|
+
return val;
|
|
12
|
+
}
|
|
13
|
+
function toString(val) {
|
|
14
|
+
if (typeof val === "string") return val;
|
|
15
|
+
const data = toBufferLike(val);
|
|
16
|
+
if (typeof data === "string") return data;
|
|
17
|
+
return `data:application/octet-stream;base64,${btoa(String.fromCharCode(...new Uint8Array(data)))}`;
|
|
18
|
+
}
|
|
19
|
+
function isPlainObject(value) {
|
|
20
|
+
if (value === null || typeof value !== "object") return false;
|
|
21
|
+
const prototype = Object.getPrototypeOf(value);
|
|
22
|
+
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) return false;
|
|
23
|
+
if (Symbol.iterator in value) return false;
|
|
24
|
+
if (Symbol.toStringTag in value) return Object.prototype.toString.call(value) === "[object Module]";
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/message.ts
|
|
30
|
+
var Message = class {
|
|
31
|
+
/** Access to the original [message event](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event) if available. */
|
|
32
|
+
event;
|
|
33
|
+
/** Access to the Peer that emitted the message. */
|
|
34
|
+
peer;
|
|
35
|
+
/** Raw message data (can be of any type). */
|
|
36
|
+
rawData;
|
|
37
|
+
#id;
|
|
38
|
+
#uint8Array;
|
|
39
|
+
#arrayBuffer;
|
|
40
|
+
#blob;
|
|
41
|
+
#text;
|
|
42
|
+
#json;
|
|
43
|
+
constructor(rawData, peer, event) {
|
|
44
|
+
this.rawData = rawData || "";
|
|
45
|
+
this.peer = peer;
|
|
46
|
+
this.event = event;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the message.
|
|
50
|
+
*/
|
|
51
|
+
get id() {
|
|
52
|
+
if (!this.#id) this.#id = crypto.randomUUID();
|
|
53
|
+
return this.#id;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get data as [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) value.
|
|
57
|
+
*
|
|
58
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
59
|
+
*/
|
|
60
|
+
uint8Array() {
|
|
61
|
+
const _uint8Array = this.#uint8Array;
|
|
62
|
+
if (_uint8Array) return _uint8Array;
|
|
63
|
+
const rawData = this.rawData;
|
|
64
|
+
if (rawData instanceof Uint8Array) return this.#uint8Array = rawData;
|
|
65
|
+
if (rawData instanceof ArrayBuffer || rawData instanceof SharedArrayBuffer) {
|
|
66
|
+
this.#arrayBuffer = rawData;
|
|
67
|
+
return this.#uint8Array = new Uint8Array(rawData);
|
|
68
|
+
}
|
|
69
|
+
if (typeof rawData === "string") {
|
|
70
|
+
this.#text = rawData;
|
|
71
|
+
return this.#uint8Array = new TextEncoder().encode(this.#text);
|
|
72
|
+
}
|
|
73
|
+
if (Symbol.iterator in rawData) return this.#uint8Array = new Uint8Array(rawData);
|
|
74
|
+
if (typeof rawData?.length === "number") return this.#uint8Array = new Uint8Array(rawData);
|
|
75
|
+
if (rawData instanceof DataView) return this.#uint8Array = new Uint8Array(rawData.buffer, rawData.byteOffset, rawData.byteLength);
|
|
76
|
+
throw new TypeError(`Unsupported message type: ${Object.prototype.toString.call(rawData)}`);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get data as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) or [SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) value.
|
|
80
|
+
*
|
|
81
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
82
|
+
*/
|
|
83
|
+
arrayBuffer() {
|
|
84
|
+
const _arrayBuffer = this.#arrayBuffer;
|
|
85
|
+
if (_arrayBuffer) return _arrayBuffer;
|
|
86
|
+
const rawData = this.rawData;
|
|
87
|
+
if (rawData instanceof ArrayBuffer || rawData instanceof SharedArrayBuffer) return this.#arrayBuffer = rawData;
|
|
88
|
+
return this.#arrayBuffer = this.uint8Array().buffer;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get data as [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) value.
|
|
92
|
+
*
|
|
93
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded. */
|
|
94
|
+
blob() {
|
|
95
|
+
const _blob = this.#blob;
|
|
96
|
+
if (_blob) return _blob;
|
|
97
|
+
const rawData = this.rawData;
|
|
98
|
+
if (rawData instanceof Blob) return this.#blob = rawData;
|
|
99
|
+
return this.#blob = new Blob([this.uint8Array()]);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get stringified text version of the message.
|
|
103
|
+
*
|
|
104
|
+
* If raw data is in any other format, it will be automatically converted and decoded.
|
|
105
|
+
*/
|
|
106
|
+
text() {
|
|
107
|
+
const _text = this.#text;
|
|
108
|
+
if (_text) return _text;
|
|
109
|
+
const rawData = this.rawData;
|
|
110
|
+
if (typeof rawData === "string") return this.#text = rawData;
|
|
111
|
+
return this.#text = new TextDecoder().decode(this.uint8Array());
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get parsed version of the message text with [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
|
|
115
|
+
*/
|
|
116
|
+
json() {
|
|
117
|
+
const _json = this.#json;
|
|
118
|
+
if (_json) return _json;
|
|
119
|
+
return this.#json = JSON.parse(this.text());
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Message data (value varies based on `peer.websocket.binaryType`).
|
|
123
|
+
*/
|
|
124
|
+
get data() {
|
|
125
|
+
switch (this.peer?.websocket?.binaryType) {
|
|
126
|
+
case "arraybuffer": return this.arrayBuffer();
|
|
127
|
+
case "blob": return this.blob();
|
|
128
|
+
case "nodebuffer": return globalThis.Buffer ? Buffer.from(this.uint8Array()) : this.uint8Array();
|
|
129
|
+
case "uint8array": return this.uint8Array();
|
|
130
|
+
case "text": return this.text();
|
|
131
|
+
default: return this.rawData;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
toString() {
|
|
135
|
+
return this.text();
|
|
136
|
+
}
|
|
137
|
+
[Symbol.toPrimitive]() {
|
|
138
|
+
return this.text();
|
|
139
|
+
}
|
|
140
|
+
[kNodeInspect]() {
|
|
141
|
+
return { message: {
|
|
142
|
+
id: this.id,
|
|
143
|
+
peer: this.peer,
|
|
144
|
+
text: this.text()
|
|
145
|
+
} };
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/peer.ts
|
|
151
|
+
var Peer = class {
|
|
152
|
+
_internal;
|
|
153
|
+
_topics;
|
|
154
|
+
_id;
|
|
155
|
+
#ws;
|
|
156
|
+
constructor(internal) {
|
|
157
|
+
this._topics = /* @__PURE__ */ new Set();
|
|
158
|
+
this._internal = internal;
|
|
159
|
+
}
|
|
160
|
+
get context() {
|
|
161
|
+
return this._internal.context ??= {};
|
|
162
|
+
}
|
|
163
|
+
get namespace() {
|
|
164
|
+
return this._internal.namespace;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
|
|
168
|
+
*/
|
|
169
|
+
get id() {
|
|
170
|
+
if (!this._id) this._id = crypto.randomUUID();
|
|
171
|
+
return this._id;
|
|
172
|
+
}
|
|
173
|
+
/** IP address of the peer */
|
|
174
|
+
get remoteAddress() {}
|
|
175
|
+
/** upgrade request */
|
|
176
|
+
get request() {
|
|
177
|
+
return this._internal.request;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
|
|
181
|
+
*
|
|
182
|
+
* **Note:** crossws adds polyfill for the following properties if native values are not available:
|
|
183
|
+
* - `protocol`: Extracted from the `sec-websocket-protocol` header.
|
|
184
|
+
* - `extensions`: Extracted from the `sec-websocket-extensions` header.
|
|
185
|
+
* - `url`: Extracted from the request URL (http -> ws).
|
|
186
|
+
* */
|
|
187
|
+
get websocket() {
|
|
188
|
+
if (!this.#ws) {
|
|
189
|
+
const _ws = this._internal.ws;
|
|
190
|
+
const _request = this._internal.request;
|
|
191
|
+
this.#ws = _request ? createWsProxy(_ws, _request) : _ws;
|
|
192
|
+
}
|
|
193
|
+
return this.#ws;
|
|
194
|
+
}
|
|
195
|
+
/** All connected peers to the server */
|
|
196
|
+
get peers() {
|
|
197
|
+
return this._internal.peers || /* @__PURE__ */ new Set();
|
|
198
|
+
}
|
|
199
|
+
/** All topics, this peer has been subscribed to. */
|
|
200
|
+
get topics() {
|
|
201
|
+
return this._topics;
|
|
202
|
+
}
|
|
203
|
+
/** Abruptly close the connection */
|
|
204
|
+
terminate() {
|
|
205
|
+
this.close();
|
|
206
|
+
}
|
|
207
|
+
/** Subscribe to a topic */
|
|
208
|
+
subscribe(topic) {
|
|
209
|
+
this._topics.add(topic);
|
|
210
|
+
}
|
|
211
|
+
/** Unsubscribe from a topic */
|
|
212
|
+
unsubscribe(topic) {
|
|
213
|
+
this._topics.delete(topic);
|
|
214
|
+
}
|
|
215
|
+
toString() {
|
|
216
|
+
return this.id;
|
|
217
|
+
}
|
|
218
|
+
[Symbol.toPrimitive]() {
|
|
219
|
+
return this.id;
|
|
220
|
+
}
|
|
221
|
+
[Symbol.toStringTag]() {
|
|
222
|
+
return "WebSocket";
|
|
223
|
+
}
|
|
224
|
+
[kNodeInspect]() {
|
|
225
|
+
return { peer: {
|
|
226
|
+
id: this.id,
|
|
227
|
+
ip: this.remoteAddress
|
|
228
|
+
} };
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
function createWsProxy(ws, request) {
|
|
232
|
+
return new Proxy(ws, { get: (target, prop) => {
|
|
233
|
+
const value = Reflect.get(target, prop);
|
|
234
|
+
if (!value) switch (prop) {
|
|
235
|
+
case "protocol": return request?.headers?.get("sec-websocket-protocol") || "";
|
|
236
|
+
case "extensions": return request?.headers?.get("sec-websocket-extensions") || "";
|
|
237
|
+
case "url": return request?.url?.replace(/^http/, "ws") || void 0;
|
|
238
|
+
}
|
|
239
|
+
return value;
|
|
240
|
+
} });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
//#endregion
|
|
244
|
+
export { toString as i, Message as n, toBufferLike as r, Peer as t };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
16
|
+
__defProp(to, key, {
|
|
17
|
+
get: ((k) => from[k]).bind(null, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: true
|
|
28
|
+
}) : target, mod));
|
|
29
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
export { __require as n, __toESM as r, __commonJSMin as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapter.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/adapters/sse.d.ts
|
|
4
|
+
interface SSEAdapter extends AdapterInstance {
|
|
5
|
+
fetch(req: Request): Promise<Response>;
|
|
6
|
+
}
|
|
7
|
+
interface SSEOptions extends AdapterOptions {
|
|
8
|
+
bidir?: boolean;
|
|
9
|
+
}
|
|
10
|
+
declare const sseAdapter: Adapter<SSEAdapter, SSEOptions>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { SSEOptions as n, sseAdapter as r, SSEAdapter as t };
|