From b43088c03c1c2c4a72f26f76363e7493fa9be455 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Sat, 25 Aug 2018 03:24:59 -0700 Subject: [PATCH] Apply no-buffer-constructor lint rule --- .eslintrc.json | 3 ++- lib/auth.js | 2 +- lib/common.js | 2 +- lib/cwmp.js | 2 +- lib/nbi.js | 2 +- lib/soap.js | 4 ++-- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b0b4930..66be45f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -13,6 +13,7 @@ "handle-callback-err": "error", "prefer-arrow-callback": "error", "curly": ["error", "multi-or-nest", "consistent"], - "consistent-return": ["error", {"treatUndefinedAsUnspecified": true}] + "consistent-return": ["error", {"treatUndefinedAsUnspecified": true}], + "no-buffer-constructor": "error" } } diff --git a/lib/auth.js b/lib/auth.js index 744f9da..1b8a98f 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -54,7 +54,7 @@ function parseAuthHeader(authHeader) { } function basic(username, password) { - return "Basic " + new Buffer(`${username}:${password}`).toString("base64"); + return "Basic " + Buffer.from(`${username}:${password}`).toString("base64"); } function digest(username, password, uri, httpMethod, body, authHeader) { diff --git a/lib/common.js b/lib/common.js index 6b9db82..50cf32f 100644 --- a/lib/common.js +++ b/lib/common.js @@ -34,7 +34,7 @@ function generateDeviceId(deviceIdStruct) { // Escapes everything except alphanumerics and underscore function esc(str) { return str.replace(/[^A-Za-z0-9_]/g, chr => { - const buf = new Buffer(chr, "utf8"); + const buf = Buffer.from(chr, "utf8"); let rep = ""; for (let b of buf) rep += "%" + b.toString(16).toUpperCase(); return rep; diff --git a/lib/cwmp.js b/lib/cwmp.js index 47fcf3e..b05ece0 100644 --- a/lib/cwmp.js +++ b/lib/cwmp.js @@ -1058,7 +1058,7 @@ function listener(httpRequest, httpResponse) { }); stream.on("end", () => { - const body = new Buffer(bytes); + const body = Buffer.allocUnsafe(bytes); let offset = 0; for (const chunk of chunks) { chunk.copy(body, offset, 0, chunk.length); diff --git a/lib/nbi.js b/lib/nbi.js index 0bec6aa..b805ea6 100644 --- a/lib/nbi.js +++ b/lib/nbi.js @@ -64,7 +64,7 @@ function listener(request, response) { function getBody() { // Write all chunks into a Buffer - const body = new Buffer(bytes); + const body = Buffer.allocUnsafe(bytes); let offset = 0; for (const chunk of chunks) { chunk.copy(body, offset, 0, chunk.length); diff --git a/lib/soap.js b/lib/soap.js index 46a2c75..9a97979 100644 --- a/lib/soap.js +++ b/lib/soap.js @@ -524,7 +524,7 @@ function response(rpc) { SOAPServer: SERVER_NAME }; - if (!rpc) return { code: 204, headers: headers, data: new Buffer(0) }; + if (!rpc) return { code: 204, headers: headers, data: Buffer.allocUnsafe(0) }; const env = createSoapEnv(rpc.cwmpVersion); const header = env.node("soap-env:Header"); @@ -589,7 +589,7 @@ function response(rpc) { return { code: 200, headers: headers, - data: new Buffer(env.doc().toString(LIBXMLJS_SAVE_OPTIONS)) + data: env.doc().toString(LIBXMLJS_SAVE_OPTIONS) }; }