From e122e4be7bc6b131552dba4dd4581a2ffb3797c6 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 18 Jan 2022 17:55:55 +0100 Subject: [PATCH] refactor: add additional types Merged from https://github.com/socketio/engine.io/pull/630 --- lib/socket.ts | 22 +++++++++++++--------- lib/transport.ts | 3 ++- package-lock.json | 46 ++++++++++++++++++++++------------------------ 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/lib/socket.ts b/lib/socket.ts index 33619cd4..c3a81619 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -4,6 +4,7 @@ import { IncomingMessage } from "http"; import { Transport } from "./transport"; import { Server } from "./server"; import { setTimeout, clearTimeout } from "timers"; +import { Packet, PacketType, RawData } from "engine.io-parser"; const debug = debugModule("engine:socket"); @@ -18,7 +19,7 @@ export class Socket extends EventEmitter { private server: Server; private upgrading: boolean; private upgraded: boolean; - private writeBuffer: any[]; + private writeBuffer: Packet[]; private packetsFn: any[]; private sentCallbackFn: any[]; private cleanupFn: any[]; @@ -116,7 +117,7 @@ export class Socket extends EventEmitter { * @param {Object} packet * @api private */ - private onPacket(packet) { + private onPacket(packet: Packet) { if ("open" !== this.readyState) { return debug("packet received with closed socket"); } @@ -417,7 +418,7 @@ export class Socket extends EventEmitter { /** * Sends a message packet. * - * @param {String} message + * @param {Object} data * @param {Object} options * @param {Function} callback * @return {Socket} for chaining @@ -436,12 +437,14 @@ export class Socket extends EventEmitter { /** * Sends a packet. * - * @param {String} packet type - * @param {String} optional, data + * @param {String} type - packet type + * @param {String} data * @param {Object} options + * @param {Function} callback + * * @api private */ - private sendPacket(type, data?, options?, callback?) { + private sendPacket(type: PacketType, data?: RawData, options?, callback?) { if ("function" === typeof options) { callback = options; options = null; @@ -453,10 +456,11 @@ export class Socket extends EventEmitter { if ("closing" !== this.readyState && "closed" !== this.readyState) { debug('sending packet "%s" (%s)', type, data); - const packet: any = { - type: type, - options: options + const packet: Packet = { + type, + options }; + if (data) packet.data = data; // exports packetCreate event diff --git a/lib/transport.ts b/lib/transport.ts index 53d8ad02..70bc9cfa 100644 --- a/lib/transport.ts +++ b/lib/transport.ts @@ -3,6 +3,7 @@ import * as parser_v4 from "engine.io-parser"; import * as parser_v3 from "./parser-v3/index"; import debugModule from "debug"; import { IncomingMessage } from "http"; +import { Packet } from "engine.io-parser"; const debug = debugModule("engine:transport"); @@ -111,7 +112,7 @@ export abstract class Transport extends EventEmitter { * @param {Object} packet * @api protected */ - protected onPacket(packet) { + protected onPacket(packet: Packet) { this.emit("packet", packet); } diff --git a/package-lock.json b/package-lock.json index dedb2538..be7d844b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -150,6 +150,14 @@ "to-fast-properties": "^2.0.0" } }, + "node_modules/@socket.io/base64-arraybuffer": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@socket.io/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz", + "integrity": "sha512-dOlCBKnDw4iShaIsH/bxujKTM18+2TOAsYz+KSc11Am38H4q5Xw8Bbz97ZYdrVNM+um3p7w86Bvvmcn9q+5+eQ==", + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/@socket.io/component-emitter": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.0.0.tgz", @@ -645,24 +653,16 @@ } }, "node_modules/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==", + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.3.tgz", + "integrity": "sha512-BtQxwF27XUNnSafQLvDi0dQ8s3i6VgzSoQMJacpIcGNrlUdfHSKbgm3jmjCVvQluGzqwujQMPAoMai3oYSTurg==", "dependencies": { - "base64-arraybuffer": "~1.0.1" + "@socket.io/base64-arraybuffer": "~1.0.2" }, "engines": { "node": ">=10.0.0" } }, - "node_modules/engine.io-parser/node_modules/base64-arraybuffer": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.1.tgz", - "integrity": "sha512-vFIUq7FdLtjZMhATwDul5RZWv2jpXQ09Pd6jcVEOvIsqCWTRFD/ONHNfyOS8dA/Ippi5dsIgpyKWKZaAKZltbA==", - "engines": { - "node": ">= 0.6.0" - } - }, "node_modules/escalade": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", @@ -2058,6 +2058,11 @@ "to-fast-properties": "^2.0.0" } }, + "@socket.io/base64-arraybuffer": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@socket.io/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz", + "integrity": "sha512-dOlCBKnDw4iShaIsH/bxujKTM18+2TOAsYz+KSc11Am38H4q5Xw8Bbz97ZYdrVNM+um3p7w86Bvvmcn9q+5+eQ==" + }, "@socket.io/component-emitter": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.0.0.tgz", @@ -2453,18 +2458,11 @@ } }, "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==", + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.3.tgz", + "integrity": "sha512-BtQxwF27XUNnSafQLvDi0dQ8s3i6VgzSoQMJacpIcGNrlUdfHSKbgm3jmjCVvQluGzqwujQMPAoMai3oYSTurg==", "requires": { - "base64-arraybuffer": "~1.0.1" - }, - "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==" - } + "@socket.io/base64-arraybuffer": "~1.0.2" } }, "escalade": { @@ -3270,7 +3268,7 @@ "uWebSockets.js": { "version": "git+https://git@github.com/uNetworking/uWebSockets.js.git#4558ee00f9f1f686fffe1accbfc2e85b1af9c50f", "dev": true, - "from": "uWebSockets.js@uNetworking/uWebSockets.js#v20.0.0" + "from": "uWebSockets.js@github:uNetworking/uWebSockets.js#v20.0.0" }, "vary": { "version": "1.1.2",