1 | /// <reference types="node" />
|
---|
2 | import http = require("http");
|
---|
3 | import { EventEmitter } from "events";
|
---|
4 | import { ExtendedError, Namespace } from "./namespace";
|
---|
5 | import { Adapter, Room, SocketId } from "socket.io-adapter";
|
---|
6 | import * as parser from "socket.io-parser";
|
---|
7 | import type { Encoder } from "socket.io-parser";
|
---|
8 | import { Socket } from "./socket";
|
---|
9 | import type { CookieSerializeOptions } from "cookie";
|
---|
10 | import type { CorsOptions } from "cors";
|
---|
11 | declare type Transport = "polling" | "websocket";
|
---|
12 | declare type ParentNspNameMatchFn = (name: string, auth: {
|
---|
13 | [key: string]: any;
|
---|
14 | }, fn: (err: Error | null, success: boolean) => void) => void;
|
---|
15 | interface EngineOptions {
|
---|
16 | /**
|
---|
17 | * how many ms without a pong packet to consider the connection closed
|
---|
18 | * @default 5000
|
---|
19 | */
|
---|
20 | pingTimeout: number;
|
---|
21 | /**
|
---|
22 | * how many ms before sending a new ping packet
|
---|
23 | * @default 25000
|
---|
24 | */
|
---|
25 | pingInterval: number;
|
---|
26 | /**
|
---|
27 | * how many ms before an uncompleted transport upgrade is cancelled
|
---|
28 | * @default 10000
|
---|
29 | */
|
---|
30 | upgradeTimeout: number;
|
---|
31 | /**
|
---|
32 | * how many bytes or characters a message can be, before closing the session (to avoid DoS).
|
---|
33 | * @default 1e5 (100 KB)
|
---|
34 | */
|
---|
35 | maxHttpBufferSize: number;
|
---|
36 | /**
|
---|
37 | * A function that receives a given handshake or upgrade request as its first parameter,
|
---|
38 | * and can decide whether to continue or not. The second argument is a function that needs
|
---|
39 | * to be called with the decided information: fn(err, success), where success is a boolean
|
---|
40 | * value where false means that the request is rejected, and err is an error code.
|
---|
41 | */
|
---|
42 | allowRequest: (req: http.IncomingMessage, fn: (err: string | null | undefined, success: boolean) => void) => void;
|
---|
43 | /**
|
---|
44 | * the low-level transports that are enabled
|
---|
45 | * @default ["polling", "websocket"]
|
---|
46 | */
|
---|
47 | transports: Transport[];
|
---|
48 | /**
|
---|
49 | * whether to allow transport upgrades
|
---|
50 | * @default true
|
---|
51 | */
|
---|
52 | allowUpgrades: boolean;
|
---|
53 | /**
|
---|
54 | * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
|
---|
55 | * @default false
|
---|
56 | */
|
---|
57 | perMessageDeflate: boolean | object;
|
---|
58 | /**
|
---|
59 | * parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable.
|
---|
60 | * @default true
|
---|
61 | */
|
---|
62 | httpCompression: boolean | object;
|
---|
63 | /**
|
---|
64 | * what WebSocket server implementation to use. Specified module must
|
---|
65 | * conform to the ws interface (see ws module api docs). Default value is ws.
|
---|
66 | * An alternative c++ addon is also available by installing uws module.
|
---|
67 | */
|
---|
68 | wsEngine: string;
|
---|
69 | /**
|
---|
70 | * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
|
---|
71 | */
|
---|
72 | initialPacket: any;
|
---|
73 | /**
|
---|
74 | * configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie
|
---|
75 | * might be used for sticky-session. Defaults to not sending any cookie.
|
---|
76 | * @default false
|
---|
77 | */
|
---|
78 | cookie: CookieSerializeOptions | boolean;
|
---|
79 | /**
|
---|
80 | * the options that will be forwarded to the cors module
|
---|
81 | */
|
---|
82 | cors: CorsOptions;
|
---|
83 | /**
|
---|
84 | * whether to enable compatibility with Socket.IO v2 clients
|
---|
85 | * @default false
|
---|
86 | */
|
---|
87 | allowEIO3: boolean;
|
---|
88 | }
|
---|
89 | interface AttachOptions {
|
---|
90 | /**
|
---|
91 | * name of the path to capture
|
---|
92 | * @default "/engine.io"
|
---|
93 | */
|
---|
94 | path: string;
|
---|
95 | /**
|
---|
96 | * destroy unhandled upgrade requests
|
---|
97 | * @default true
|
---|
98 | */
|
---|
99 | destroyUpgrade: boolean;
|
---|
100 | /**
|
---|
101 | * milliseconds after which unhandled requests are ended
|
---|
102 | * @default 1000
|
---|
103 | */
|
---|
104 | destroyUpgradeTimeout: number;
|
---|
105 | }
|
---|
106 | interface EngineAttachOptions extends EngineOptions, AttachOptions {
|
---|
107 | }
|
---|
108 | interface ServerOptions extends EngineAttachOptions {
|
---|
109 | /**
|
---|
110 | * name of the path to capture
|
---|
111 | * @default "/socket.io"
|
---|
112 | */
|
---|
113 | path: string;
|
---|
114 | /**
|
---|
115 | * whether to serve the client files
|
---|
116 | * @default true
|
---|
117 | */
|
---|
118 | serveClient: boolean;
|
---|
119 | /**
|
---|
120 | * the adapter to use
|
---|
121 | * @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
|
---|
122 | */
|
---|
123 | adapter: any;
|
---|
124 | /**
|
---|
125 | * the parser to use
|
---|
126 | * @default the default parser (https://github.com/socketio/socket.io-parser)
|
---|
127 | */
|
---|
128 | parser: any;
|
---|
129 | /**
|
---|
130 | * how many ms before a client without namespace is closed
|
---|
131 | * @default 45000
|
---|
132 | */
|
---|
133 | connectTimeout: number;
|
---|
134 | }
|
---|
135 | export declare class Server extends EventEmitter {
|
---|
136 | readonly sockets: Namespace;
|
---|
137 | /** @private */
|
---|
138 | readonly _parser: typeof parser;
|
---|
139 | /** @private */
|
---|
140 | readonly encoder: Encoder;
|
---|
141 | /**
|
---|
142 | * @private
|
---|
143 | */
|
---|
144 | _nsps: Map<string, Namespace>;
|
---|
145 | private parentNsps;
|
---|
146 | private _adapter?;
|
---|
147 | private _serveClient;
|
---|
148 | private opts;
|
---|
149 | private eio;
|
---|
150 | private engine;
|
---|
151 | private _path;
|
---|
152 | private clientPathRegex;
|
---|
153 | /**
|
---|
154 | * @private
|
---|
155 | */
|
---|
156 | _connectTimeout: number;
|
---|
157 | private httpServer;
|
---|
158 | /**
|
---|
159 | * Server constructor.
|
---|
160 | *
|
---|
161 | * @param srv http server, port, or options
|
---|
162 | * @param [opts]
|
---|
163 | * @public
|
---|
164 | */
|
---|
165 | constructor(opts?: Partial<ServerOptions>);
|
---|
166 | constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
|
---|
167 | constructor(srv: undefined | Partial<ServerOptions> | http.Server | number, opts?: Partial<ServerOptions>);
|
---|
168 | /**
|
---|
169 | * Sets/gets whether client code is being served.
|
---|
170 | *
|
---|
171 | * @param v - whether to serve client code
|
---|
172 | * @return self when setting or value when getting
|
---|
173 | * @public
|
---|
174 | */
|
---|
175 | serveClient(v: boolean): this;
|
---|
176 | serveClient(): boolean;
|
---|
177 | serveClient(v?: boolean): this | boolean;
|
---|
178 | /**
|
---|
179 | * Executes the middleware for an incoming namespace not already created on the server.
|
---|
180 | *
|
---|
181 | * @param name - name of incoming namespace
|
---|
182 | * @param auth - the auth parameters
|
---|
183 | * @param fn - callback
|
---|
184 | *
|
---|
185 | * @private
|
---|
186 | */
|
---|
187 | _checkNamespace(name: string, auth: {
|
---|
188 | [key: string]: any;
|
---|
189 | }, fn: (nsp: Namespace | false) => void): void;
|
---|
190 | /**
|
---|
191 | * Sets the client serving path.
|
---|
192 | *
|
---|
193 | * @param {String} v pathname
|
---|
194 | * @return {Server|String} self when setting or value when getting
|
---|
195 | * @public
|
---|
196 | */
|
---|
197 | path(v: string): this;
|
---|
198 | path(): string;
|
---|
199 | path(v?: string): this | string;
|
---|
200 | /**
|
---|
201 | * Set the delay after which a client without namespace is closed
|
---|
202 | * @param v
|
---|
203 | * @public
|
---|
204 | */
|
---|
205 | connectTimeout(v: number): this;
|
---|
206 | connectTimeout(): number;
|
---|
207 | connectTimeout(v?: number): this | number;
|
---|
208 | /**
|
---|
209 | * Sets the adapter for rooms.
|
---|
210 | *
|
---|
211 | * @param v pathname
|
---|
212 | * @return self when setting or value when getting
|
---|
213 | * @public
|
---|
214 | */
|
---|
215 | adapter(): typeof Adapter | undefined;
|
---|
216 | adapter(v: typeof Adapter): this;
|
---|
217 | adapter(v?: typeof Adapter): typeof Adapter | undefined | this;
|
---|
218 | /**
|
---|
219 | * Attaches socket.io to a server or port.
|
---|
220 | *
|
---|
221 | * @param srv - server or port
|
---|
222 | * @param opts - options passed to engine.io
|
---|
223 | * @return self
|
---|
224 | * @public
|
---|
225 | */
|
---|
226 | listen(srv: http.Server | number, opts?: Partial<ServerOptions>): this;
|
---|
227 | /**
|
---|
228 | * Attaches socket.io to a server or port.
|
---|
229 | *
|
---|
230 | * @param srv - server or port
|
---|
231 | * @param opts - options passed to engine.io
|
---|
232 | * @return self
|
---|
233 | * @public
|
---|
234 | */
|
---|
235 | attach(srv: http.Server | number, opts?: Partial<ServerOptions>): this;
|
---|
236 | /**
|
---|
237 | * Initialize engine
|
---|
238 | *
|
---|
239 | * @param srv - the server to attach to
|
---|
240 | * @param opts - options passed to engine.io
|
---|
241 | * @private
|
---|
242 | */
|
---|
243 | private initEngine;
|
---|
244 | /**
|
---|
245 | * Attaches the static file serving.
|
---|
246 | *
|
---|
247 | * @param srv http server
|
---|
248 | * @private
|
---|
249 | */
|
---|
250 | private attachServe;
|
---|
251 | /**
|
---|
252 | * Handles a request serving of client source and map
|
---|
253 | *
|
---|
254 | * @param req
|
---|
255 | * @param res
|
---|
256 | * @private
|
---|
257 | */
|
---|
258 | private serve;
|
---|
259 | /**
|
---|
260 | * @param filename
|
---|
261 | * @param req
|
---|
262 | * @param res
|
---|
263 | * @private
|
---|
264 | */
|
---|
265 | private static sendFile;
|
---|
266 | /**
|
---|
267 | * Binds socket.io to an engine.io instance.
|
---|
268 | *
|
---|
269 | * @param {engine.Server} engine engine.io (or compatible) server
|
---|
270 | * @return self
|
---|
271 | * @public
|
---|
272 | */
|
---|
273 | bind(engine: any): this;
|
---|
274 | /**
|
---|
275 | * Called with each incoming transport connection.
|
---|
276 | *
|
---|
277 | * @param {engine.Socket} conn
|
---|
278 | * @return self
|
---|
279 | * @private
|
---|
280 | */
|
---|
281 | private onconnection;
|
---|
282 | /**
|
---|
283 | * Looks up a namespace.
|
---|
284 | *
|
---|
285 | * @param {String|RegExp|Function} name nsp name
|
---|
286 | * @param fn optional, nsp `connection` ev handler
|
---|
287 | * @public
|
---|
288 | */
|
---|
289 | of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket) => void): Namespace;
|
---|
290 | /**
|
---|
291 | * Closes server connection
|
---|
292 | *
|
---|
293 | * @param [fn] optional, called as `fn([err])` on error OR all conns closed
|
---|
294 | * @public
|
---|
295 | */
|
---|
296 | close(fn?: (err?: Error) => void): void;
|
---|
297 | /**
|
---|
298 | * Sets up namespace middleware.
|
---|
299 | *
|
---|
300 | * @return self
|
---|
301 | * @public
|
---|
302 | */
|
---|
303 | use(fn: (socket: Socket, next: (err?: ExtendedError) => void) => void): this;
|
---|
304 | /**
|
---|
305 | * Targets a room when emitting.
|
---|
306 | *
|
---|
307 | * @param name
|
---|
308 | * @return self
|
---|
309 | * @public
|
---|
310 | */
|
---|
311 | to(name: Room): this;
|
---|
312 | /**
|
---|
313 | * Targets a room when emitting.
|
---|
314 | *
|
---|
315 | * @param name
|
---|
316 | * @return self
|
---|
317 | * @public
|
---|
318 | */
|
---|
319 | in(name: Room): this;
|
---|
320 | /**
|
---|
321 | * Sends a `message` event to all clients.
|
---|
322 | *
|
---|
323 | * @return self
|
---|
324 | * @public
|
---|
325 | */
|
---|
326 | send(...args: readonly any[]): this;
|
---|
327 | /**
|
---|
328 | * Sends a `message` event to all clients.
|
---|
329 | *
|
---|
330 | * @return self
|
---|
331 | * @public
|
---|
332 | */
|
---|
333 | write(...args: readonly any[]): this;
|
---|
334 | /**
|
---|
335 | * Gets a list of socket ids.
|
---|
336 | *
|
---|
337 | * @public
|
---|
338 | */
|
---|
339 | allSockets(): Promise<Set<SocketId>>;
|
---|
340 | /**
|
---|
341 | * Sets the compress flag.
|
---|
342 | *
|
---|
343 | * @param compress - if `true`, compresses the sending data
|
---|
344 | * @return self
|
---|
345 | * @public
|
---|
346 | */
|
---|
347 | compress(compress: boolean): this;
|
---|
348 | /**
|
---|
349 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
---|
350 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
---|
351 | * and is in the middle of a request-response cycle).
|
---|
352 | *
|
---|
353 | * @return self
|
---|
354 | * @public
|
---|
355 | */
|
---|
356 | get volatile(): this;
|
---|
357 | /**
|
---|
358 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
---|
359 | *
|
---|
360 | * @return self
|
---|
361 | * @public
|
---|
362 | */
|
---|
363 | get local(): this;
|
---|
364 | }
|
---|
365 | export { Socket, ServerOptions, Namespace };
|
---|