From f3e6fd105d340d346bbf81d44b6179f0f0723e00 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Sat, 4 Jun 2022 23:08:56 +0800 Subject: [PATCH] refactor: replace deprecated `String.prototype.substr()` `.substr()` is deprecated so we replace it with `.slice()` which works similarily but isn't deprecated Signed-off-by: Lam Wei Li --- lib/parser-v3/index.ts | 8 ++++---- lib/server.ts | 4 ++-- test/server.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/parser-v3/index.ts b/lib/parser-v3/index.ts index 6949f187..367ae137 100644 --- a/lib/parser-v3/index.ts +++ b/lib/parser-v3/index.ts @@ -137,7 +137,7 @@ export function decodePacket (data, binaryType, utf8decode) { type = data.charAt(0); if (type === 'b') { - return decodeBase64Packet(data.substr(1), binaryType); + return decodeBase64Packet(data.slice(1), binaryType); } if (utf8decode) { @@ -152,7 +152,7 @@ export function decodePacket (data, binaryType, utf8decode) { } if (data.length > 1) { - return { type: packetslist[type], data: data.substring(1) }; + return { type: packetslist[type], data: data.slice(1) }; } else { return { type: packetslist[type] }; } @@ -191,7 +191,7 @@ function tryDecode(data) { export function decodeBase64Packet (msg, binaryType) { var type = packetslist[msg.charAt(0)]; - var data = Buffer.from(msg.substr(1), 'base64'); + var data = Buffer.from(msg.slice(1), 'base64'); if (binaryType === 'arraybuffer') { var abv = new Uint8Array(data.length); for (var i = 0; i < abv.length; i++){ @@ -305,7 +305,7 @@ export function decodePayload (data, binaryType, callback) { return callback(err, 0, 1); } - msg = data.substr(i + 1, n); + msg = data.slice(i + 1, i + 1 + n); if (length != msg.length) { // parser error - ignoring payload diff --git a/lib/server.ts b/lib/server.ts index 739e38aa..58371c9c 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -555,7 +555,7 @@ export class Server extends BaseServer { return; } - const head = Buffer.from(upgradeHead); // eslint-disable-line node/no-deprecated-api + const head = Buffer.from(upgradeHead); upgradeHead = null; // delegate to ws @@ -643,7 +643,7 @@ export class Server extends BaseServer { path += "/"; function check(req) { - return path === req.url.substr(0, path.length); + return path === req.url.slice(0, path.length); } // cache and clean up listeners diff --git a/test/server.js b/test/server.js index 906bdb6b..5de0e259 100644 --- a/test/server.js +++ b/test/server.js @@ -181,7 +181,7 @@ describe("server", () => { .get(`http://localhost:${port}/engine.io/`) .query({ transport: "polling", EIO: 4 }) .end((err, res) => { - const sid = JSON.parse(res.text.substring(1)).sid; + const sid = JSON.parse(res.text.slice(1)).sid; request .get(`http://localhost:${port}/engine.io/`) @@ -3357,7 +3357,7 @@ describe("server", () => { expect(res.headers["set-cookie"].length).to.be(2); expect(res.headers["set-cookie"][1]).to.be("mycookie=456"); - const sid = JSON.parse(res.text.substring(5)).sid; + const sid = JSON.parse(res.text.slice(5)).sid; request .post(`http://localhost:${port}/engine.io/`) @@ -3394,7 +3394,7 @@ describe("server", () => { expect(res.headers["set-cookie"].length).to.be(2); expect(res.headers["set-cookie"][1]).to.be("mycookie=456"); - const sid = JSON.parse(res.text.substring(5)).sid; + const sid = JSON.parse(res.text.slice(5)).sid; request .post(`http://localhost:${port}/engine.io/`)