Skip to content

Commit

Permalink
refactor: replace deprecated String.prototype.substr() (#646)
Browse files Browse the repository at this point in the history
`.substr()` is deprecated so we replace it with `.slice()` which works
similarily but isn't deprecated.

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

Signed-off-by: Lam Wei Li <peteriman@mail.com>
  • Loading branch information
lamweili committed Jun 6, 2022
1 parent 020801a commit 917d1d2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/parser-v3/index.ts
Expand Up @@ -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) {
Expand All @@ -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] };
}
Expand Down Expand Up @@ -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++){
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/server.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/server.js
Expand Up @@ -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/`)
Expand Down Expand Up @@ -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/`)
Expand Down Expand Up @@ -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/`)
Expand Down

0 comments on commit 917d1d2

Please sign in to comment.