Skip to content

Commit c310b7b

Browse files
refactor: improve types
1 parent 362bc78 commit c310b7b

File tree

11 files changed

+198
-190
lines changed

11 files changed

+198
-190
lines changed

lib/engine.io.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const protocol = parser.protocol;
1717
* @param {Function} callback
1818
* @param {Object} options
1919
* @return {Server} websocket.io server
20-
* @api public
2120
*/
2221

2322
function listen(port, options: AttachOptions & ServerOptions, fn) {
@@ -46,7 +45,6 @@ function listen(port, options: AttachOptions & ServerOptions, fn) {
4645
* @param {http.Server} server
4746
* @param {Object} options
4847
* @return {Server} engine server
49-
* @api public
5048
*/
5149

5250
function attach(server, options: AttachOptions & ServerOptions) {

lib/server.ts

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import type { CorsOptions, CorsOptionsDelegate } from "cors";
1717
import type { Duplex } from "stream";
1818
import { WebTransport } from "./transports/webtransport";
1919
import { createPacketDecoderStream } from "engine.io-parser";
20+
import type { EngineRequest } from "./transport";
2021

2122
const debug = debugModule("engine");
2223

2324
const kResponseHeaders = Symbol("responseHeaders");
2425

25-
type Transport = "polling" | "websocket";
26+
type Transport = "polling" | "websocket" | "webtransport";
2627

2728
export interface AttachOptions {
2829
/**
@@ -169,7 +170,6 @@ export abstract class BaseServer extends EventEmitter {
169170
* Server constructor.
170171
*
171172
* @param {Object} opts - options
172-
* @api public
173173
*/
174174
constructor(opts: ServerOptions = {}) {
175175
super();
@@ -246,21 +246,25 @@ export abstract class BaseServer extends EventEmitter {
246246
* Returns a list of available transports for upgrade given a certain transport.
247247
*
248248
* @return {Array}
249-
* @api public
250249
*/
251-
public upgrades(transport) {
250+
public upgrades(transport: string) {
252251
if (!this.opts.allowUpgrades) return [];
253252
return transports[transport].upgradesTo || [];
254253
}
255254

256255
/**
257256
* Verifies a request.
258257
*
259-
* @param {http.IncomingMessage}
260-
* @return {Boolean} whether the request is valid
261-
* @api private
258+
* @param {EngineRequest} req
259+
* @param upgrade - whether it's an upgrade request
260+
* @param fn
261+
* @protected
262262
*/
263-
protected verify(req, upgrade, fn) {
263+
protected verify(
264+
req: any,
265+
upgrade: boolean,
266+
fn: (errorCode?: number, errorContext?: any) => void
267+
) {
264268
// transport check
265269
const transport = req._query.transport;
266270
// WebTransport does not go through the verify() method, see the onWebTransportSession() method
@@ -384,8 +388,6 @@ export abstract class BaseServer extends EventEmitter {
384388

385389
/**
386390
* Closes all clients.
387-
*
388-
* @api public
389391
*/
390392
public close() {
391393
debug("closing all open clients");
@@ -404,23 +406,26 @@ export abstract class BaseServer extends EventEmitter {
404406
* generate a socket id.
405407
* Overwrite this method to generate your custom socket id
406408
*
407-
* @param {Object} request object
408-
* @api public
409+
* @param {IncomingMessage} req - the request object
409410
*/
410-
public generateId(req) {
411+
public generateId(req: IncomingMessage) {
411412
return base64id.generateId();
412413
}
413414

414415
/**
415416
* Handshakes a new client.
416417
*
417-
* @param {String} transport name
418-
* @param {Object} request object
418+
* @param {String} transportName
419+
* @param {Object} req - the request object
419420
* @param {Function} closeConnection
420421
*
421-
* @api protected
422+
* @protected
422423
*/
423-
protected async handshake(transportName, req, closeConnection) {
424+
protected async handshake(
425+
transportName: string,
426+
req: any,
427+
closeConnection: (errorCode?: number, errorContext?: any) => void
428+
) {
424429
const protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
425430
if (protocol === 3 && !this.opts.allowEIO3) {
426431
debug("unsupported protocol version");
@@ -661,7 +666,7 @@ export class Server extends BaseServer {
661666
/**
662667
* Initialize websocket server
663668
*
664-
* @api protected
669+
* @protected
665670
*/
666671
protected init() {
667672
if (!~this.opts.transports.indexOf("websocket")) return;
@@ -708,30 +713,30 @@ export class Server extends BaseServer {
708713
/**
709714
* Prepares a request by processing the query string.
710715
*
711-
* @api private
716+
* @private
712717
*/
713-
private prepare(req) {
718+
private prepare(req: EngineRequest) {
714719
// try to leverage pre-existing `req._query` (e.g: from connect)
715720
if (!req._query) {
716-
req._query = ~req.url.indexOf("?") ? qs.parse(parse(req.url).query) : {};
721+
req._query = (
722+
~req.url.indexOf("?") ? qs.parse(parse(req.url).query) : {}
723+
) as Record<string, string>;
717724
}
718725
}
719726

720-
protected createTransport(transportName, req) {
727+
protected createTransport(transportName: string, req: IncomingMessage) {
721728
return new transports[transportName](req);
722729
}
723730

724731
/**
725732
* Handles an Engine.IO HTTP request.
726733
*
727-
* @param {IncomingMessage} req
734+
* @param {EngineRequest} req
728735
* @param {ServerResponse} res
729-
* @api public
730736
*/
731-
public handleRequest(req: IncomingMessage, res: ServerResponse) {
737+
public handleRequest(req: EngineRequest, res: ServerResponse) {
732738
debug('handling "%s" http request "%s"', req.method, req.url);
733739
this.prepare(req);
734-
// @ts-ignore
735740
req.res = res;
736741

737742
const callback = (errorCode, errorContext) => {
@@ -746,15 +751,12 @@ export class Server extends BaseServer {
746751
return;
747752
}
748753

749-
// @ts-ignore
750754
if (req._query.sid) {
751755
debug("setting new request for existing client");
752-
// @ts-ignore
753756
this.clients[req._query.sid].transport.onRequest(req);
754757
} else {
755758
const closeConnection = (errorCode, errorContext) =>
756759
abortRequest(res, errorCode, errorContext);
757-
// @ts-ignore
758760
this.handshake(req._query.transport, req, closeConnection);
759761
}
760762
};
@@ -770,11 +772,9 @@ export class Server extends BaseServer {
770772

771773
/**
772774
* Handles an Engine.IO HTTP Upgrade.
773-
*
774-
* @api public
775775
*/
776776
public handleUpgrade(
777-
req: IncomingMessage,
777+
req: EngineRequest,
778778
socket: Duplex,
779779
upgradeHead: Buffer
780780
) {
@@ -819,7 +819,7 @@ export class Server extends BaseServer {
819819
* Called upon a ws.io connection.
820820
*
821821
* @param {ws.Socket} websocket
822-
* @api private
822+
* @private
823823
*/
824824
private onWebSocket(req, socket, websocket) {
825825
websocket.on("error", onUpgradeError);
@@ -877,7 +877,6 @@ export class Server extends BaseServer {
877877
*
878878
* @param {http.Server} server
879879
* @param {Object} options
880-
* @api public
881880
*/
882881
public attach(server: HttpServer, options: AttachOptions = {}) {
883882
const path = this._computePath(options);
@@ -898,7 +897,7 @@ export class Server extends BaseServer {
898897
server.on("request", (req, res) => {
899898
if (check(req)) {
900899
debug('intercepting request for path "%s"', path);
901-
this.handleRequest(req, res);
900+
this.handleRequest(req as EngineRequest, res);
902901
} else {
903902
let i = 0;
904903
const l = listeners.length;
@@ -911,7 +910,7 @@ export class Server extends BaseServer {
911910
if (~this.opts.transports.indexOf("websocket")) {
912911
server.on("upgrade", (req, socket, head) => {
913912
if (check(req)) {
914-
this.handleUpgrade(req, socket, head);
913+
this.handleUpgrade(req as EngineRequest, socket, head);
915914
} else if (false !== options.destroyUpgrade) {
916915
// default node behavior is to disconnect when no handlers
917916
// but by adding a handler, we prevent that
@@ -939,7 +938,7 @@ export class Server extends BaseServer {
939938
* @param errorCode - the error code
940939
* @param errorContext - additional error context
941940
*
942-
* @api private
941+
* @private
943942
*/
944943

945944
function abortRequest(res, errorCode, errorContext) {
@@ -964,8 +963,6 @@ function abortRequest(res, errorCode, errorContext) {
964963
* @param {net.Socket} socket
965964
* @param {string} errorCode - the error code
966965
* @param {object} errorContext - additional error context
967-
*
968-
* @api private
969966
*/
970967

971968
function abortUpgrade(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy