diff --git a/lib/client.ts b/lib/client.ts index 46823f691a..268cfbd085 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -7,6 +7,7 @@ import type { Namespace } from "./namespace"; import type { EventsMap } from "./typed-events"; import type { Socket } from "./socket"; import type { SocketId } from "socket.io-adapter"; +import type { Socket as RawSocket } from "engine.io"; const debug = debugModule("socket.io:client"); @@ -22,7 +23,7 @@ export class Client< EmitEvents extends EventsMap, ServerSideEvents extends EventsMap > { - public readonly conn; + public readonly conn: RawSocket; private readonly id: string; private readonly server: Server; diff --git a/lib/index.ts b/lib/index.ts index 785cefb57d..0da2f3f2fb 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -4,7 +4,12 @@ import { createDeflate, createGzip, createBrotliCompress } from "zlib"; import accepts = require("accepts"); import { pipeline } from "stream"; import path = require("path"); -import engine = require("engine.io"); +import { + attach, + Server as Engine, + ServerOptions as EngineOptions, + AttachOptions, +} from "engine.io"; import { Client } from "./client"; import { EventEmitter } from "events"; import { ExtendedError, Namespace, ServerReservedEventsMap } from "./namespace"; @@ -14,8 +19,6 @@ import * as parser from "socket.io-parser"; import type { Encoder } from "socket.io-parser"; import debugModule from "debug"; import { Socket } from "./socket"; -import type { CookieSerializeOptions } from "cookie"; -import type { CorsOptions } from "cors"; import type { BroadcastOperator, RemoteSocket } from "./broadcast-operator"; import { EventsMap, @@ -30,7 +33,6 @@ const debug = debugModule("socket.io:server"); const clientVersion = require("../package.json").version; const dotMapRegex = /\.map/; -type Transport = "polling" | "websocket"; type ParentNspNameMatchFn = ( name: string, auth: { [key: string]: any }, @@ -39,107 +41,7 @@ type ParentNspNameMatchFn = ( type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter); -interface EngineOptions { - /** - * how many ms without a pong packet to consider the connection closed - * @default 20000 - */ - pingTimeout: number; - /** - * how many ms before sending a new ping packet - * @default 25000 - */ - pingInterval: number; - /** - * how many ms before an uncompleted transport upgrade is cancelled - * @default 10000 - */ - upgradeTimeout: number; - /** - * how many bytes or characters a message can be, before closing the session (to avoid DoS). - * @default 1e5 (100 KB) - */ - maxHttpBufferSize: number; - /** - * A function that receives a given handshake or upgrade request as its first parameter, - * and can decide whether to continue or not. The second argument is a function that needs - * to be called with the decided information: fn(err, success), where success is a boolean - * value where false means that the request is rejected, and err is an error code. - */ - allowRequest: ( - req: http.IncomingMessage, - fn: (err: string | null | undefined, success: boolean) => void - ) => void; - /** - * the low-level transports that are enabled - * @default ["polling", "websocket"] - */ - transports: Transport[]; - /** - * whether to allow transport upgrades - * @default true - */ - allowUpgrades: boolean; - /** - * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable. - * @default false - */ - perMessageDeflate: boolean | object; - /** - * parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable. - * @default true - */ - httpCompression: boolean | object; - /** - * what WebSocket server implementation to use. Specified module must - * conform to the ws interface (see ws module api docs). - * An alternative c++ addon is also available by installing eiows module. - * - * @default `require("ws").Server` - */ - wsEngine: Function; - /** - * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO. - */ - initialPacket: any; - /** - * configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie - * might be used for sticky-session. Defaults to not sending any cookie. - * @default false - */ - cookie: (CookieSerializeOptions & { name: string }) | boolean; - /** - * the options that will be forwarded to the cors module - */ - cors: CorsOptions; - /** - * whether to enable compatibility with Socket.IO v2 clients - * @default false - */ - allowEIO3: boolean; -} - -interface AttachOptions { - /** - * name of the path to capture - * @default "/engine.io" - */ - path: string; - /** - * destroy unhandled upgrade requests - * @default true - */ - destroyUpgrade: boolean; - /** - * milliseconds after which unhandled requests are ended - * @default 1000 - */ - destroyUpgradeTimeout: number; -} - -interface EngineAttachOptions extends EngineOptions, AttachOptions {} - -interface ServerOptions extends EngineAttachOptions { +interface ServerOptions extends EngineOptions, AttachOptions { /** * name of the path to capture * @default "/socket.io" @@ -210,7 +112,7 @@ export class Server< private _adapter?: AdapterConstructor; private _serveClient: boolean; private opts: Partial; - private eio; + private eio: Engine; private _path: string; private clientPathRegex: RegExp; @@ -443,11 +345,11 @@ export class Server< */ private initEngine( srv: http.Server, - opts: Partial + opts: EngineOptions & AttachOptions ): void { // initialize engine debug("creating engine.io instance with opts %j", opts); - this.eio = engine.attach(srv, opts); + this.eio = attach(srv, opts); // attach static file serving if (this._serveClient) this.attachServe(srv); diff --git a/package-lock.json b/package-lock.json index b8f64e23be..284a85b5d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -882,17 +882,40 @@ "dev": true }, "engine.io": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-5.2.0.tgz", - "integrity": "sha512-d1DexkQx87IFr1FLuV+0f5kAm1Hk1uOVijLOb+D1sDO2QMb7YjE02VHtZtxo7xIXMgcWLb+vl3HRT0rI9tr4jQ==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.0.0.tgz", + "integrity": "sha512-Ui7yl3JajEIaACg8MOUwWvuuwU7jepZqX3BKs1ho7NQRuP4LhN4XIykXhp8bEy+x/DhA0LBZZXYSCkZDqrwMMg==", "requires": { + "@types/cookie": "^0.4.1", + "@types/cors": "^2.8.12", + "@types/node": ">=10.0.0", "accepts": "~1.3.4", "base64id": "2.0.0", "cookie": "~0.4.1", "cors": "~2.8.5", "debug": "~4.3.1", - "engine.io-parser": "~4.0.0", - "ws": "~7.4.2" + "engine.io-parser": "~5.0.0", + "ws": "~8.2.3" + }, + "dependencies": { + "base64-arraybuffer": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.1.tgz", + "integrity": "sha512-vFIUq7FdLtjZMhATwDul5RZWv2jpXQ09Pd6jcVEOvIsqCWTRFD/ONHNfyOS8dA/Ippi5dsIgpyKWKZaAKZltbA==" + }, + "engine.io-parser": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.0.tgz", + "integrity": "sha512-wn6QavHEqXoM+cg+x8uUG7GhxLBCfKEKNEsCNc7V2ugj3gB3lK91l1MuZiy6xFB2V9D1eew0aWkmpiT/aBb/KA==", + "requires": { + "base64-arraybuffer": "~1.0.1" + } + }, + "ws": { + "version": "8.2.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz", + "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==" + } } }, "engine.io-client": { @@ -924,7 +947,8 @@ "engine.io-parser": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-4.0.1.tgz", - "integrity": "sha512-v5aZK1hlckcJDGmHz3W8xvI3NUHYc9t8QtTbqdR5OaH3S9iJZilPubauOm+vLWOMMWzpE3hiq92l9lTAHamRCg==" + "integrity": "sha512-v5aZK1hlckcJDGmHz3W8xvI3NUHYc9t8QtTbqdR5OaH3S9iJZilPubauOm+vLWOMMWzpE3hiq92l9lTAHamRCg==", + "dev": true }, "error-ex": { "version": "1.3.2", @@ -2915,7 +2939,8 @@ "ws": { "version": "7.4.2", "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.2.tgz", - "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==" + "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==", + "dev": true }, "xmlhttprequest-ssl": { "version": "1.5.5", diff --git a/package.json b/package.json index ee33aaa057..b9c843899a 100644 --- a/package.json +++ b/package.json @@ -45,13 +45,10 @@ "prepack": "npm run compile" }, "dependencies": { - "@types/cookie": "^0.4.1", - "@types/cors": "^2.8.12", - "@types/node": ">=10.0.0", "accepts": "~1.3.4", "base64id": "~2.0.0", "debug": "~4.3.2", - "engine.io": "~5.2.0", + "engine.io": "~6.0.0", "socket.io-adapter": "~2.3.2", "socket.io-parser": "~4.0.4" },