Skip to content

improve type annotations and comments #5364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/engine.io-parser/lib/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export type RawData = any;

export interface Packet {
type: PacketType;
options?: { compress: boolean };
options?: {
compress: boolean;
wsPreEncoded?: string;
wsPreEncodedFrame?: boolean;
};
data?: RawData;
}

Expand Down
40 changes: 26 additions & 14 deletions packages/engine.io/lib/engine.io.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { createServer } from "http";
import { createServer, Server as HttpServer } from "http";
import { Server, AttachOptions, ServerOptions } from "./server";
import transports from "./transports/index";
import * as parser from "engine.io-parser";

export { Server, transports, listen, attach, parser };
export type { AttachOptions, ServerOptions, BaseServer } from "./server";
export type {
AttachOptions,
ServerOptions,
BaseServer,
ErrorCallback,
} from "./server";
export { uServer } from "./userver";
export { Socket } from "./socket";
export { Transport } from "./transport";
export const protocol = parser.protocol;

/**
* Creates an http.Server exclusively used for WS upgrades.
* Creates an http.Server exclusively used for WS upgrades, and start listening.
*
* @param {Number} port
* @param {Function} callback
* @param {Object} options
* @return {Server} websocket.io server
* @param port
* @param options
* @param listenCallback callback for http.Server.listen()
* @return engine.io server
*/

function listen(port, options: AttachOptions & ServerOptions, fn) {
function listen(
port: number,
options?: AttachOptions & ServerOptions,
listenCallback?: () => void,
): Server {
if ("function" === typeof options) {
fn = options;
listenCallback = options;
options = {};
}

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

server.listen(port, fn);
server.listen(port, listenCallback);

return engine;
}

/**
* Captures upgrade requests for a http.Server.
*
* @param {http.Server} server
* @param {Object} options
* @return {Server} engine server
* @param server
* @param options
* @return engine.io server
*/

function attach(server, options: AttachOptions & ServerOptions) {
function attach(
server: HttpServer,
options: AttachOptions & ServerOptions,
): Server {
const engine = new Server(options);
engine.attach(server, options);
return engine;
Expand Down
14 changes: 7 additions & 7 deletions packages/engine.io/lib/parser-v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ const EMPTY_BUFFER = Buffer.concat([]);
*
* @api private
*/

export function encodePacket (packet, supportsBinary, utf8encode, callback) {
export function encodePacket (packet: any, supportsBinary?: any, utf8encode?: any, callback?: any) {
if (typeof supportsBinary === 'function') {
callback = supportsBinary;
supportsBinary = null;
Expand All @@ -86,7 +85,7 @@ export function encodePacket (packet, supportsBinary, utf8encode, callback) {
}

return callback('' + encoded);
};
}

/**
* Encode Buffer data
Expand Down Expand Up @@ -120,16 +119,16 @@ export function encodeBase64Packet (packet, callback){
/**
* Decodes a packet. Data also available as an ArrayBuffer if requested.
*
* @return {Object} with `type` and `data` (if any)
* @return {import('engine.io-parser').Packet} with `type` and `data` (if any)
* @api private
*/

export function decodePacket (data, binaryType, utf8decode) {
export function decodePacket (data: any, binaryType?: any, utf8decode?: any): any {
if (data === undefined) {
return err;
}

var type;
let type: string | number;

// String data
if (typeof data === 'string') {
Expand All @@ -147,6 +146,7 @@ export function decodePacket (data, binaryType, utf8decode) {
}
}

// @ts-expect-error
if (Number(type) != type || !packetslist[type]) {
return err;
}
Expand Down Expand Up @@ -274,7 +274,7 @@ function map(ary, each, done) {
* @api public
*/

export function decodePayload (data, binaryType, callback) {
export function decodePayload (data: any, binaryType?: any, callback?: any) {
if (typeof data !== 'string') {
return decodePayloadAsBinary(data, binaryType, callback);
}
Expand Down
Loading