From 9b1af95e5dd97af08978ad49dc775e733c5a6838 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 21 Jun 2024 13:40:25 +0200 Subject: [PATCH] refactor: improve types (2) --- lib/server.ts | 1 - lib/transport.ts | 14 +++++++------- lib/transports/polling-jsonp.ts | 15 ++------------- lib/transports/polling.ts | 12 ++++++------ lib/transports/webtransport.ts | 1 - 5 files changed, 15 insertions(+), 28 deletions(-) diff --git a/lib/server.ts b/lib/server.ts index e3c2c5fc..a010092f 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -753,7 +753,6 @@ export class Server extends BaseServer { if (req._query.sid) { debug("setting new request for existing client"); - // @ts-ignore this.clients[req._query.sid].transport.onRequest(req); } else { const closeConnection = (errorCode, errorContext) => diff --git a/lib/transport.ts b/lib/transport.ts index 8d72c3f3..6a9f3ca0 100644 --- a/lib/transport.ts +++ b/lib/transport.ts @@ -80,7 +80,7 @@ export abstract class Transport extends EventEmitter { * * @param {EngineRequest} req */ - constructor(req: EngineRequest) { + constructor(req: { _query: Record }) { super(); this.protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default this.parser = this.protocol === 4 ? parser_v4 : parser_v3; @@ -99,10 +99,10 @@ export abstract class Transport extends EventEmitter { /** * Called with an incoming HTTP request. * - * @param {EngineRequest} req - * @protected + * @param {http.IncomingMessage} req + * @package */ - protected onRequest(req: EngineRequest) { + onRequest(req) { debug("setting request"); this.req = req; } @@ -172,7 +172,7 @@ export abstract class Transport extends EventEmitter { /** * The name of the transport. */ - abstract get name(); + abstract get name(): string; /** * Sends an array of packets. @@ -180,10 +180,10 @@ export abstract class Transport extends EventEmitter { * @param {Array} packets * @package */ - abstract send(packets); + abstract send(packets): void; /** * Closes the transport. */ - abstract doClose(fn?: () => void); + abstract doClose(fn?: () => void): void; } diff --git a/lib/transports/polling-jsonp.ts b/lib/transports/polling-jsonp.ts index 52c4b0bd..05806008 100644 --- a/lib/transports/polling-jsonp.ts +++ b/lib/transports/polling-jsonp.ts @@ -19,13 +19,7 @@ export class JSONP extends Polling { this.foot = ");"; } - /** - * Handles incoming data. - * Due to a bug in \n handling by browsers, we expect a escaped string. - * - * @protected - */ - protected onData(data: RawData) { + override onData(data: RawData) { // we leverage the qs module so that we get built-in DoS protection // and the fast alternative to decodeURIComponent data = qs.parse(data).d as string; @@ -39,12 +33,7 @@ export class JSONP extends Polling { } } - /** - * Performs the write. - * - * @protected - */ - protected doWrite(data, options, callback) { + override doWrite(data, options, callback) { // we must output valid javascript, not valid json // see: http://timelessrepo.com/json-isnt-a-javascript-subset const js = JSON.stringify(data) diff --git a/lib/transports/polling.ts b/lib/transports/polling.ts index d1f65a0a..3b477a62 100644 --- a/lib/transports/polling.ts +++ b/lib/transports/polling.ts @@ -43,7 +43,7 @@ export class Polling extends Transport { * Overrides onRequest. * * @param {EngineRequest} req - * @private + * @package */ onRequest(req: EngineRequest) { const res = req.res; @@ -65,7 +65,7 @@ export class Polling extends Transport { * * @private */ - onPollRequest(req, res) { + private onPollRequest(req: EngineRequest, res: ServerResponse) { if (this.req) { debug("request overlap"); // assert: this.res, '.req and .res should be (un)set together' @@ -107,7 +107,7 @@ export class Polling extends Transport { * * @private */ - onDataRequest(req: IncomingMessage, res: ServerResponse) { + private onDataRequest(req: IncomingMessage, res: ServerResponse) { if (this.dataReq) { // assert: this.dataRes, '.dataReq and .dataRes should be (un)set together' this.onError("data request overlap from client"); @@ -182,7 +182,7 @@ export class Polling extends Transport { * @param data - encoded payload * @protected */ - protected onData(data: RawData) { + override onData(data: RawData) { debug('received "%s"', data); const callback = (packet) => { if ("close" === packet.type) { @@ -312,7 +312,7 @@ export class Polling extends Transport { * * @private */ - compress(data, encoding, callback) { + private compress(data, encoding, callback) { debug("compressing"); const buffers = []; @@ -335,7 +335,7 @@ export class Polling extends Transport { * * @private */ - doClose(fn: () => void) { + override doClose(fn: () => void) { debug("closing"); let closeTimeoutTimer; diff --git a/lib/transports/webtransport.ts b/lib/transports/webtransport.ts index 534a5241..07852282 100644 --- a/lib/transports/webtransport.ts +++ b/lib/transports/webtransport.ts @@ -11,7 +11,6 @@ export class WebTransport extends Transport { private readonly writer; constructor(private readonly session, stream, reader) { - // @ts-expect-error super({ _query: { EIO: "4" } }); const transformStream = createPacketEncoderStream();