Skip to content

Commit

Permalink
feat: improve packet type
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah committed Nov 25, 2021
1 parent 9a42664 commit 4d07892
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: any;
}

export class Socket extends EventEmitter {
public readonly protocol: number;
public readonly request: IncomingMessage;
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 4d07892

Please sign in to comment.