Skip to content

Commit

Permalink
refactor: improve types (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jun 21, 2024
1 parent e5d912d commit 9b1af95
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
1 change: 0 additions & 1 deletion lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
14 changes: 7 additions & 7 deletions lib/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export abstract class Transport extends EventEmitter {
*
* @param {EngineRequest} req
*/
constructor(req: EngineRequest) {
constructor(req: { _query: Record<string, string> }) {
super();
this.protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
this.parser = this.protocol === 4 ? parser_v4 : parser_v3;
Expand All @@ -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;
}
Expand Down Expand Up @@ -172,18 +172,18 @@ export abstract class Transport extends EventEmitter {
/**
* The name of the transport.
*/
abstract get name();
abstract get name(): string;

/**
* Sends an array of packets.
*
* @param {Array} packets
* @package
*/
abstract send(packets);
abstract send(packets): void;

/**
* Closes the transport.
*/
abstract doClose(fn?: () => void);
abstract doClose(fn?: () => void): void;
}
15 changes: 2 additions & 13 deletions lib/transports/polling-jsonp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions lib/transports/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Polling extends Transport {
* Overrides onRequest.
*
* @param {EngineRequest} req
* @private
* @package
*/
onRequest(req: EngineRequest) {
const res = req.res;
Expand All @@ -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'
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -312,7 +312,7 @@ export class Polling extends Transport {
*
* @private
*/
compress(data, encoding, callback) {
private compress(data, encoding, callback) {
debug("compressing");

const buffers = [];
Expand All @@ -335,7 +335,7 @@ export class Polling extends Transport {
*
* @private
*/
doClose(fn: () => void) {
override doClose(fn: () => void) {
debug("closing");

let closeTimeoutTimer;
Expand Down
1 change: 0 additions & 1 deletion lib/transports/webtransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9b1af95

Please sign in to comment.