crossws 0.0.0 → 0.0.1

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.
@@ -0,0 +1,299 @@
1
+ import { WebSocketAdapter } from '../index.js';
2
+ import { IncomingMessage as IncomingMessage$1 } from 'node:http';
3
+ import { Duplex as Duplex$1 } from 'node:stream';
4
+ import { EventEmitter } from 'events';
5
+ import { ClientRequestArgs, IncomingMessage, ClientRequest, Agent, Server as Server$1, OutgoingHttpHeaders } from 'http';
6
+ import { Server as Server$2 } from 'https';
7
+ import { Duplex, DuplexOptions } from 'stream';
8
+ import { SecureContextOptions } from 'tls';
9
+ import { URL } from 'url';
10
+ import { ZlibOptions } from 'zlib';
11
+
12
+ type BufferLike = string | Buffer | DataView | number | ArrayBufferView | Uint8Array | ArrayBuffer | SharedArrayBuffer | readonly any[] | readonly number[] | {
13
+ valueOf(): ArrayBuffer;
14
+ } | {
15
+ valueOf(): SharedArrayBuffer;
16
+ } | {
17
+ valueOf(): Uint8Array;
18
+ } | {
19
+ valueOf(): readonly number[];
20
+ } | {
21
+ valueOf(): string;
22
+ } | {
23
+ [Symbol.toPrimitive](hint: string): string;
24
+ };
25
+ declare class WebSocket$1 extends EventEmitter {
26
+ static readonly createWebSocketStream: typeof createWebSocketStream;
27
+ static readonly WebSocketServer: WebSocketServer;
28
+ static readonly Server: typeof Server;
29
+ static readonly WebSocket: typeof WebSocket$1;
30
+ /** The connection is not yet open. */
31
+ static readonly CONNECTING: 0;
32
+ /** The connection is open and ready to communicate. */
33
+ static readonly OPEN: 1;
34
+ /** The connection is in the process of closing. */
35
+ static readonly CLOSING: 2;
36
+ /** The connection is closed. */
37
+ static readonly CLOSED: 3;
38
+ binaryType: "nodebuffer" | "arraybuffer" | "fragments";
39
+ readonly bufferedAmount: number;
40
+ readonly extensions: string;
41
+ /** Indicates whether the websocket is paused */
42
+ readonly isPaused: boolean;
43
+ readonly protocol: string;
44
+ /** The current state of the connection */
45
+ readonly readyState: typeof WebSocket$1.CONNECTING | typeof WebSocket$1.OPEN | typeof WebSocket$1.CLOSING | typeof WebSocket$1.CLOSED;
46
+ readonly url: string;
47
+ /** The connection is not yet open. */
48
+ readonly CONNECTING: 0;
49
+ /** The connection is open and ready to communicate. */
50
+ readonly OPEN: 1;
51
+ /** The connection is in the process of closing. */
52
+ readonly CLOSING: 2;
53
+ /** The connection is closed. */
54
+ readonly CLOSED: 3;
55
+ onopen: ((event: Event) => void) | null;
56
+ onerror: ((event: ErrorEvent) => void) | null;
57
+ onclose: ((event: CloseEvent) => void) | null;
58
+ onmessage: ((event: MessageEvent) => void) | null;
59
+ constructor(address: null);
60
+ constructor(address: string | URL, options?: ClientOptions | ClientRequestArgs);
61
+ constructor(address: string | URL, protocols?: string | string[], options?: ClientOptions | ClientRequestArgs);
62
+ close(code?: number, data?: string | Buffer): void;
63
+ ping(data?: any, mask?: boolean, cb?: (err: Error) => void): void;
64
+ pong(data?: any, mask?: boolean, cb?: (err: Error) => void): void;
65
+ send(data: BufferLike, cb?: (err?: Error) => void): void;
66
+ send(data: BufferLike, options: {
67
+ mask?: boolean | undefined;
68
+ binary?: boolean | undefined;
69
+ compress?: boolean | undefined;
70
+ fin?: boolean | undefined;
71
+ }, cb?: (err?: Error) => void): void;
72
+ terminate(): void;
73
+ /**
74
+ * Pause the websocket causing it to stop emitting events. Some events can still be
75
+ * emitted after this is called, until all buffered data is consumed. This method
76
+ * is a noop if the ready state is `CONNECTING` or `CLOSED`.
77
+ */
78
+ pause(): void;
79
+ /**
80
+ * Make a paused socket resume emitting events. This method is a noop if the ready
81
+ * state is `CONNECTING` or `CLOSED`.
82
+ */
83
+ resume(): void;
84
+ addEventListener(method: "message", cb: (event: MessageEvent) => void, options?: EventListenerOptions): void;
85
+ addEventListener(method: "close", cb: (event: CloseEvent) => void, options?: EventListenerOptions): void;
86
+ addEventListener(method: "error", cb: (event: ErrorEvent) => void, options?: EventListenerOptions): void;
87
+ addEventListener(method: "open", cb: (event: Event) => void, options?: EventListenerOptions): void;
88
+ removeEventListener(method: "message", cb: (event: MessageEvent) => void): void;
89
+ removeEventListener(method: "close", cb: (event: CloseEvent) => void): void;
90
+ removeEventListener(method: "error", cb: (event: ErrorEvent) => void): void;
91
+ removeEventListener(method: "open", cb: (event: Event) => void): void;
92
+ on(event: "close", listener: (this: WebSocket$1, code: number, reason: Buffer) => void): this;
93
+ on(event: "error", listener: (this: WebSocket$1, err: Error) => void): this;
94
+ on(event: "upgrade", listener: (this: WebSocket$1, request: IncomingMessage) => void): this;
95
+ on(event: "message", listener: (this: WebSocket$1, data: RawData, isBinary: boolean) => void): this;
96
+ on(event: "open", listener: (this: WebSocket$1) => void): this;
97
+ on(event: "ping" | "pong", listener: (this: WebSocket$1, data: Buffer) => void): this;
98
+ on(event: "unexpected-response", listener: (this: WebSocket$1, request: ClientRequest, response: IncomingMessage) => void): this;
99
+ on(event: string | symbol, listener: (this: WebSocket$1, ...args: any[]) => void): this;
100
+ once(event: "close", listener: (this: WebSocket$1, code: number, reason: Buffer) => void): this;
101
+ once(event: "error", listener: (this: WebSocket$1, err: Error) => void): this;
102
+ once(event: "upgrade", listener: (this: WebSocket$1, request: IncomingMessage) => void): this;
103
+ once(event: "message", listener: (this: WebSocket$1, data: RawData, isBinary: boolean) => void): this;
104
+ once(event: "open", listener: (this: WebSocket$1) => void): this;
105
+ once(event: "ping" | "pong", listener: (this: WebSocket$1, data: Buffer) => void): this;
106
+ once(event: "unexpected-response", listener: (this: WebSocket$1, request: ClientRequest, response: IncomingMessage) => void): this;
107
+ once(event: string | symbol, listener: (this: WebSocket$1, ...args: any[]) => void): this;
108
+ off(event: "close", listener: (this: WebSocket$1, code: number, reason: Buffer) => void): this;
109
+ off(event: "error", listener: (this: WebSocket$1, err: Error) => void): this;
110
+ off(event: "upgrade", listener: (this: WebSocket$1, request: IncomingMessage) => void): this;
111
+ off(event: "message", listener: (this: WebSocket$1, data: RawData, isBinary: boolean) => void): this;
112
+ off(event: "open", listener: (this: WebSocket$1) => void): this;
113
+ off(event: "ping" | "pong", listener: (this: WebSocket$1, data: Buffer) => void): this;
114
+ off(event: "unexpected-response", listener: (this: WebSocket$1, request: ClientRequest, response: IncomingMessage) => void): this;
115
+ off(event: string | symbol, listener: (this: WebSocket$1, ...args: any[]) => void): this;
116
+ addListener(event: "close", listener: (code: number, reason: Buffer) => void): this;
117
+ addListener(event: "error", listener: (err: Error) => void): this;
118
+ addListener(event: "upgrade", listener: (request: IncomingMessage) => void): this;
119
+ addListener(event: "message", listener: (data: RawData, isBinary: boolean) => void): this;
120
+ addListener(event: "open", listener: () => void): this;
121
+ addListener(event: "ping" | "pong", listener: (data: Buffer) => void): this;
122
+ addListener(event: "unexpected-response", listener: (request: ClientRequest, response: IncomingMessage) => void): this;
123
+ addListener(event: string | symbol, listener: (...args: any[]) => void): this;
124
+ removeListener(event: "close", listener: (code: number, reason: Buffer) => void): this;
125
+ removeListener(event: "error", listener: (err: Error) => void): this;
126
+ removeListener(event: "upgrade", listener: (request: IncomingMessage) => void): this;
127
+ removeListener(event: "message", listener: (data: RawData, isBinary: boolean) => void): this;
128
+ removeListener(event: "open", listener: () => void): this;
129
+ removeListener(event: "ping" | "pong", listener: (data: Buffer) => void): this;
130
+ removeListener(event: "unexpected-response", listener: (request: ClientRequest, response: IncomingMessage) => void): this;
131
+ removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
132
+ }
133
+ /**
134
+ * Data represents the raw message payload received over the
135
+ */
136
+ type RawData = Buffer | ArrayBuffer | Buffer[];
137
+ /**
138
+ * Data represents the message payload received over the
139
+ */
140
+ type Data = string | Buffer | ArrayBuffer | Buffer[];
141
+ /**
142
+ * CertMeta represents the accepted types for certificate & key data.
143
+ */
144
+ type CertMeta = string | string[] | Buffer | Buffer[];
145
+ /**
146
+ * VerifyClientCallbackSync is a synchronous callback used to inspect the
147
+ * incoming message. The return value (boolean) of the function determines
148
+ * whether or not to accept the handshake.
149
+ */
150
+ type VerifyClientCallbackSync<Request extends IncomingMessage = IncomingMessage> = (info: {
151
+ origin: string;
152
+ secure: boolean;
153
+ req: Request;
154
+ }) => boolean;
155
+ /**
156
+ * VerifyClientCallbackAsync is an asynchronous callback used to inspect the
157
+ * incoming message. The return value (boolean) of the function determines
158
+ * whether or not to accept the handshake.
159
+ */
160
+ type VerifyClientCallbackAsync<Request extends IncomingMessage = IncomingMessage> = (info: {
161
+ origin: string;
162
+ secure: boolean;
163
+ req: Request;
164
+ }, callback: (res: boolean, code?: number, message?: string, headers?: OutgoingHttpHeaders) => void) => void;
165
+ interface ClientOptions extends SecureContextOptions {
166
+ protocol?: string | undefined;
167
+ followRedirects?: boolean | undefined;
168
+ generateMask?(mask: Buffer): void;
169
+ handshakeTimeout?: number | undefined;
170
+ maxRedirects?: number | undefined;
171
+ perMessageDeflate?: boolean | PerMessageDeflateOptions | undefined;
172
+ localAddress?: string | undefined;
173
+ protocolVersion?: number | undefined;
174
+ headers?: {
175
+ [key: string]: string;
176
+ } | undefined;
177
+ origin?: string | undefined;
178
+ agent?: Agent | undefined;
179
+ host?: string | undefined;
180
+ family?: number | undefined;
181
+ checkServerIdentity?(servername: string, cert: CertMeta): boolean;
182
+ rejectUnauthorized?: boolean | undefined;
183
+ maxPayload?: number | undefined;
184
+ skipUTF8Validation?: boolean | undefined;
185
+ }
186
+ interface PerMessageDeflateOptions {
187
+ serverNoContextTakeover?: boolean | undefined;
188
+ clientNoContextTakeover?: boolean | undefined;
189
+ serverMaxWindowBits?: number | undefined;
190
+ clientMaxWindowBits?: number | undefined;
191
+ zlibDeflateOptions?: {
192
+ flush?: number | undefined;
193
+ finishFlush?: number | undefined;
194
+ chunkSize?: number | undefined;
195
+ windowBits?: number | undefined;
196
+ level?: number | undefined;
197
+ memLevel?: number | undefined;
198
+ strategy?: number | undefined;
199
+ dictionary?: Buffer | Buffer[] | DataView | undefined;
200
+ info?: boolean | undefined;
201
+ } | undefined;
202
+ zlibInflateOptions?: ZlibOptions | undefined;
203
+ threshold?: number | undefined;
204
+ concurrencyLimit?: number | undefined;
205
+ }
206
+ interface Event {
207
+ type: string;
208
+ target: WebSocket$1;
209
+ }
210
+ interface ErrorEvent {
211
+ error: any;
212
+ message: string;
213
+ type: string;
214
+ target: WebSocket$1;
215
+ }
216
+ interface CloseEvent {
217
+ wasClean: boolean;
218
+ code: number;
219
+ reason: string;
220
+ type: string;
221
+ target: WebSocket$1;
222
+ }
223
+ interface MessageEvent {
224
+ data: Data;
225
+ type: string;
226
+ target: WebSocket$1;
227
+ }
228
+ interface EventListenerOptions {
229
+ once?: boolean | undefined;
230
+ }
231
+ interface ServerOptions<U extends typeof WebSocket$1 = typeof WebSocket$1, V extends typeof IncomingMessage = typeof IncomingMessage> {
232
+ host?: string | undefined;
233
+ port?: number | undefined;
234
+ backlog?: number | undefined;
235
+ server?: Server$1<V> | Server$2<V> | undefined;
236
+ verifyClient?: VerifyClientCallbackAsync<InstanceType<V>> | VerifyClientCallbackSync<InstanceType<V>> | undefined;
237
+ handleProtocols?: (protocols: Set<string>, request: InstanceType<V>) => string | false;
238
+ path?: string | undefined;
239
+ noServer?: boolean | undefined;
240
+ clientTracking?: boolean | undefined;
241
+ perMessageDeflate?: boolean | PerMessageDeflateOptions | undefined;
242
+ maxPayload?: number | undefined;
243
+ skipUTF8Validation?: boolean | undefined;
244
+ WebSocket?: U | undefined;
245
+ }
246
+ interface AddressInfo {
247
+ address: string;
248
+ family: string;
249
+ port: number;
250
+ }
251
+ declare class Server<T extends typeof WebSocket$1 = typeof WebSocket$1, U extends typeof IncomingMessage = typeof IncomingMessage> extends EventEmitter {
252
+ options: ServerOptions<T, U>;
253
+ path: string;
254
+ clients: Set<InstanceType<T>>;
255
+ constructor(options?: ServerOptions<T, U>, callback?: () => void);
256
+ address(): AddressInfo | string;
257
+ close(cb?: (err?: Error) => void): void;
258
+ handleUpgrade(request: InstanceType<U>, socket: Duplex, upgradeHead: Buffer, callback: (client: InstanceType<T>, request: InstanceType<U>) => void): void;
259
+ shouldHandle(request: InstanceType<U>): boolean | Promise<boolean>;
260
+ on(event: "connection", cb: (this: Server<T>, socket: InstanceType<T>, request: InstanceType<U>) => void): this;
261
+ on(event: "error", cb: (this: Server<T>, error: Error) => void): this;
262
+ on(event: "headers", cb: (this: Server<T>, headers: string[], request: InstanceType<U>) => void): this;
263
+ on(event: "close" | "listening", cb: (this: Server<T>) => void): this;
264
+ on(event: string | symbol, listener: (this: Server<T>, ...args: any[]) => void): this;
265
+ once(event: "connection", cb: (this: Server<T>, socket: InstanceType<T>, request: InstanceType<U>) => void): this;
266
+ once(event: "error", cb: (this: Server<T>, error: Error) => void): this;
267
+ once(event: "headers", cb: (this: Server<T>, headers: string[], request: InstanceType<U>) => void): this;
268
+ once(event: "close" | "listening", cb: (this: Server<T>) => void): this;
269
+ once(event: string | symbol, listener: (this: Server<T>, ...args: any[]) => void): this;
270
+ off(event: "connection", cb: (this: Server<T>, socket: InstanceType<T>, request: InstanceType<U>) => void): this;
271
+ off(event: "error", cb: (this: Server<T>, error: Error) => void): this;
272
+ off(event: "headers", cb: (this: Server<T>, headers: string[], request: InstanceType<U>) => void): this;
273
+ off(event: "close" | "listening", cb: (this: Server<T>) => void): this;
274
+ off(event: string | symbol, listener: (this: Server<T>, ...args: any[]) => void): this;
275
+ addListener(event: "connection", cb: (client: InstanceType<T>, request: InstanceType<U>) => void): this;
276
+ addListener(event: "error", cb: (err: Error) => void): this;
277
+ addListener(event: "headers", cb: (headers: string[], request: InstanceType<U>) => void): this;
278
+ addListener(event: "close" | "listening", cb: () => void): this;
279
+ addListener(event: string | symbol, listener: (...args: any[]) => void): this;
280
+ removeListener(event: "connection", cb: (client: InstanceType<T>, request: InstanceType<U>) => void): this;
281
+ removeListener(event: "error", cb: (err: Error) => void): this;
282
+ removeListener(event: "headers", cb: (headers: string[], request: InstanceType<U>) => void): this;
283
+ removeListener(event: "close" | "listening", cb: () => void): this;
284
+ removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
285
+ }
286
+ type WebSocketServer = Server;
287
+ declare function createWebSocketStream(websocket: WebSocket$1, options?: DuplexOptions): Duplex;
288
+
289
+ declare const WebSocket: WebSocket$1;
290
+ interface AdapterOptions {
291
+ wss?: WebSocketServer;
292
+ serverOptions?: ServerOptions;
293
+ }
294
+ interface Adapter {
295
+ handleUpgrade(req: IncomingMessage$1, socket: Duplex$1, head: Buffer): void;
296
+ }
297
+ declare const _default: WebSocketAdapter<Adapter, AdapterOptions>;
298
+
299
+ export { type Adapter, type AdapterOptions, WebSocket, _default as default };