Skip to content

Commit be13cca

Browse files
jokesterdarrachequesne
authored andcommitted
refactor: improve type annotations and comments (#5364)
1 parent e95f6ab commit be13cca

File tree

16 files changed

+218
-109
lines changed

16 files changed

+218
-109
lines changed

packages/engine.io-parser/lib/commons.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export type RawData = any;
3232

3333
export interface Packet {
3434
type: PacketType;
35-
options?: { compress: boolean };
35+
options?: {
36+
compress: boolean;
37+
wsPreEncoded?: string; // deprecated in favor of `wsPreEncodedFrame`
38+
wsPreEncodedFrame?: any; // computed in the socket.io-adapter package (should be typed as Buffer)
39+
};
3640
data?: RawData;
3741
}
3842

packages/engine.io/lib/engine.io.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
import { createServer } from "http";
1+
import { createServer, Server as HttpServer } from "http";
22
import { Server, AttachOptions, ServerOptions } from "./server";
33
import transports from "./transports/index";
44
import * as parser from "engine.io-parser";
55

66
export { Server, transports, listen, attach, parser };
7-
export type { AttachOptions, ServerOptions, BaseServer } from "./server";
7+
export type {
8+
AttachOptions,
9+
ServerOptions,
10+
BaseServer,
11+
ErrorCallback,
12+
} from "./server";
813
export { uServer } from "./userver";
914
export { Socket } from "./socket";
1015
export { Transport } from "./transport";
1116
export const protocol = parser.protocol;
1217

1318
/**
14-
* Creates an http.Server exclusively used for WS upgrades.
19+
* Creates an http.Server exclusively used for WS upgrades, and starts listening.
1520
*
16-
* @param {Number} port
17-
* @param {Function} callback
18-
* @param {Object} options
19-
* @return {Server} websocket.io server
21+
* @param port
22+
* @param options
23+
* @param listenCallback - callback for http.Server.listen()
24+
* @return engine.io server
2025
*/
2126

22-
function listen(port, options: AttachOptions & ServerOptions, fn) {
27+
function listen(
28+
port: number,
29+
options?: AttachOptions & ServerOptions,
30+
listenCallback?: () => void,
31+
): Server {
2332
if ("function" === typeof options) {
24-
fn = options;
33+
listenCallback = options;
2534
options = {};
2635
}
2736

@@ -34,20 +43,23 @@ function listen(port, options: AttachOptions & ServerOptions, fn) {
3443
const engine = attach(server, options);
3544
engine.httpServer = server;
3645

37-
server.listen(port, fn);
46+
server.listen(port, listenCallback);
3847

3948
return engine;
4049
}
4150

4251
/**
4352
* Captures upgrade requests for a http.Server.
4453
*
45-
* @param {http.Server} server
46-
* @param {Object} options
47-
* @return {Server} engine server
54+
* @param server
55+
* @param options
56+
* @return engine.io server
4857
*/
4958

50-
function attach(server, options: AttachOptions & ServerOptions) {
59+
function attach(
60+
server: HttpServer,
61+
options: AttachOptions & ServerOptions,
62+
): Server {
5163
const engine = new Server(options);
5264
engine.attach(server, options);
5365
return engine;

packages/engine.io/lib/parser-v3/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ const EMPTY_BUFFER = Buffer.concat([]);
5959
*
6060
* @api private
6161
*/
62-
63-
export function encodePacket (packet, supportsBinary, utf8encode, callback) {
62+
export function encodePacket (packet: any, supportsBinary?: any, utf8encode?: any, callback?: any) {
6463
if (typeof supportsBinary === 'function') {
6564
callback = supportsBinary;
6665
supportsBinary = null;
@@ -86,7 +85,7 @@ export function encodePacket (packet, supportsBinary, utf8encode, callback) {
8685
}
8786

8887
return callback('' + encoded);
89-
};
88+
}
9089

9190
/**
9291
* Encode Buffer data
@@ -120,16 +119,16 @@ export function encodeBase64Packet (packet, callback){
120119
/**
121120
* Decodes a packet. Data also available as an ArrayBuffer if requested.
122121
*
123-
* @return {Object} with `type` and `data` (if any)
122+
* @return {import('engine.io-parser').Packet} with `type` and `data` (if any)
124123
* @api private
125124
*/
126125

127-
export function decodePacket (data, binaryType, utf8decode) {
126+
export function decodePacket (data: any, binaryType?: any, utf8decode?: any): any {
128127
if (data === undefined) {
129128
return err;
130129
}
131130

132-
var type;
131+
let type: string | number;
133132

134133
// String data
135134
if (typeof data === 'string') {
@@ -147,6 +146,7 @@ export function decodePacket (data, binaryType, utf8decode) {
147146
}
148147
}
149148

149+
// @ts-expect-error
150150
if (Number(type) != type || !packetslist[type]) {
151151
return err;
152152
}
@@ -274,7 +274,7 @@ function map(ary, each, done) {
274274
* @api public
275275
*/
276276

277-
export function decodePayload (data, binaryType, callback) {
277+
export function decodePayload (data: any, binaryType?: any, callback?: any) {
278278
if (typeof data !== 'string') {
279279
return decodePayloadAsBinary(data, binaryType, callback);
280280
}

0 commit comments

Comments
 (0)