diff --git a/lib/socket.ts b/lib/socket.ts index 6697ed45..233506e6 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -6,6 +6,14 @@ import { Server } from "./server"; const debug = debugModule("engine:socket"); +type PacketType = "error" | "message" | "open" | "ping" | "pong"; + +export interface Packet { + type: PacketType, + options: {compress: boolean}, + data?: string +} + export class Socket extends EventEmitter { public readonly protocol: number; public readonly request: IncomingMessage; @@ -115,7 +123,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"); } @@ -440,7 +448,7 @@ export class Socket extends EventEmitter { * @param {Object} options * @api private */ - private sendPacket(type, data?, options?, callback?) { + private sendPacket(type: PacketType, data?, options?, callback?) { if ("function" === typeof options) { callback = options; options = null; @@ -452,10 +460,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..9640a357 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 "./socket"; 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); }