Skip to content

Commit 0c3e8a5

Browse files
committed
feat: improve packet type
1 parent 9a42664 commit 0c3e8a5

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/socket.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import { Server } from "./server";
66

77
const debug = debugModule("engine:socket");
88

9+
type PacketType = "error" | "message" | "open" | "ping" | "pong";
10+
11+
export interface Packet {
12+
type: PacketType;
13+
options: { compress: boolean };
14+
data?: string;
15+
}
16+
917
export class Socket extends EventEmitter {
1018
public readonly protocol: number;
1119
public readonly request: IncomingMessage;
@@ -115,7 +123,7 @@ export class Socket extends EventEmitter {
115123
* @param {Object} packet
116124
* @api private
117125
*/
118-
private onPacket(packet) {
126+
private onPacket(packet: Packet) {
119127
if ("open" !== this.readyState) {
120128
return debug("packet received with closed socket");
121129
}
@@ -440,7 +448,7 @@ export class Socket extends EventEmitter {
440448
* @param {Object} options
441449
* @api private
442450
*/
443-
private sendPacket(type, data?, options?, callback?) {
451+
private sendPacket(type: PacketType, data?, options?, callback?) {
444452
if ("function" === typeof options) {
445453
callback = options;
446454
options = null;
@@ -452,10 +460,11 @@ export class Socket extends EventEmitter {
452460
if ("closing" !== this.readyState && "closed" !== this.readyState) {
453461
debug('sending packet "%s" (%s)', type, data);
454462

455-
const packet: any = {
456-
type: type,
457-
options: options
463+
const packet: Packet = {
464+
type,
465+
options
458466
};
467+
459468
if (data) packet.data = data;
460469

461470
// exports packetCreate event

lib/transport.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as parser_v4 from "engine.io-parser";
33
import * as parser_v3 from "./parser-v3/index";
44
import debugModule from "debug";
55
import { IncomingMessage } from "http";
6+
import { Packet } from "./socket";
67

78
const debug = debugModule("engine:transport");
89

@@ -111,7 +112,7 @@ export abstract class Transport extends EventEmitter {
111112
* @param {Object} packet
112113
* @api protected
113114
*/
114-
protected onPacket(packet) {
115+
protected onPacket(packet: Packet) {
115116
this.emit("packet", packet);
116117
}
117118

0 commit comments

Comments
 (0)