From 82debd228c9ef0ac548bda511ae86e5d0b2f008a Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Sun, 17 Mar 2024 17:54:45 +0100 Subject: [PATCH 1/2] Refactor code to use modern `Buffer.alloc()` with zero-filled memory Fixes the `'new Buffer()' was deprecated since v6.0.0. Use 'Buffer.alloc()' or 'Buffer.from()' instead. (n/no-deprecated-api)` error --- lib/create.js | 6 +++--- lib/encode.js | 2 +- lib/is-alias.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/create.js b/lib/create.js index 4520344..0697659 100644 --- a/lib/create.js +++ b/lib/create.js @@ -80,7 +80,7 @@ module.exports = exports = function (targetPath) { }()); (function addType1 () { - var b = new Buffer(4) + var b = Buffer.alloc(4) b.writeUInt32BE(info.parent.id, 0) @@ -93,7 +93,7 @@ module.exports = exports = function (targetPath) { (function addType14 () { var l = info.target.filename.length - var b = new Buffer(2 + (l * 2)) + var b = Buffer.alloc(2 + (l * 2)) b.writeUInt16BE(l, 0) utf16be(info.target.filename).copy(b, 2) @@ -107,7 +107,7 @@ module.exports = exports = function (targetPath) { (function addType15 () { var l = info.volume.name.length - var b = new Buffer(2 + (l * 2)) + var b = Buffer.alloc(2 + (l * 2)) b.writeUInt16BE(l, 0) utf16be(info.volume.name).copy(b, 2) diff --git a/lib/encode.js b/lib/encode.js index ee47929..00ea8f6 100644 --- a/lib/encode.js +++ b/lib/encode.js @@ -24,7 +24,7 @@ module.exports = exports = function (info) { }, 0) var trailerLength = 4 - var buf = new Buffer(baseLength + extraLength + trailerLength) + var buf = Buffer.alloc(baseLength + extraLength + trailerLength) buf.writeUInt32BE(0, 0) diff --git a/lib/is-alias.js b/lib/is-alias.js index 52225f2..f98484a 100644 --- a/lib/is-alias.js +++ b/lib/is-alias.js @@ -6,7 +6,7 @@ module.exports = function isAlias (path) { var fd = fs.openSync(path, 'r') try { - read = new Buffer(16) + read = Buffer.alloc(16) fs.readSync(fd, read, 0, 16, 0) } finally { fs.closeSync(fd) From 8e7c13154f72a60afb6e5e152e7fac2ee16f7d18 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Sun, 17 Mar 2024 17:55:12 +0100 Subject: [PATCH 2/2] Refactor code to use modern `Buffer.from()` creation api Fixes the `'new Buffer()' was deprecated since v6.0.0. Use 'Buffer.alloc()' or 'Buffer.from()' instead. (n/no-deprecated-api)` error --- lib/create.js | 8 ++++---- test/basics.js | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/create.js b/lib/create.js index 0697659..9ec4de4 100644 --- a/lib/create.js +++ b/lib/create.js @@ -30,7 +30,7 @@ var findVolume = function (startPath, startStat) { } var utf16be = function (str) { - var b = new Buffer(str, 'ucs2') + var b = Buffer.from(str, 'ucs2') for (var i = 0; i < b.length; i += 2) { var a = b[i] b[i] = b[i + 1] @@ -70,7 +70,7 @@ module.exports = exports = function (targetPath) { }; (function addType0 () { - var b = new Buffer(info.parent.name, 'utf8') + var b = Buffer.from(info.parent.name, 'utf8') info.extra.push({ type: 0, @@ -123,7 +123,7 @@ module.exports = exports = function (targetPath) { var vl = volumePath.length assert.equal(targetPath.slice(0, vl), volumePath) var lp = targetPath.slice(vl) - var b = new Buffer(lp, 'utf8') + var b = Buffer.from(lp, 'utf8') info.extra.push({ type: 18, @@ -133,7 +133,7 @@ module.exports = exports = function (targetPath) { }()); (function addType19 () { - var b = new Buffer(volumePath, 'utf8') + var b = Buffer.from(volumePath, 'utf8') info.extra.push({ type: 19, diff --git a/test/basics.js b/test/basics.js index 3cdacab..cb2a7b8 100644 --- a/test/basics.js +++ b/test/basics.js @@ -7,7 +7,7 @@ var path = require('path') var temp = require('fs-temp') var assert = require('assert') -var rawData = new Buffer( +var rawData = Buffer.from( 'AAAAAAEqAAIAAApUZXN0IFRpdGxlAAAAAAAAAAAAAAAAAAAAAADO615USCsA' + 'BQAAABMMVGVzdEJrZy50aWZmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFM7rXlgAAAAAAAAAAP////8A' + @@ -70,8 +70,8 @@ describe('isAlias', function () { var aliasFile, garbageFile before(function () { - aliasFile = temp.writeFileSync(new Buffer('626f6f6b000000006d61726b00000000', 'hex')) - garbageFile = temp.writeFileSync(new Buffer('Hello my name is Linus!')) + aliasFile = temp.writeFileSync(Buffer.from('626f6f6b000000006d61726b00000000', 'hex')) + garbageFile = temp.writeFileSync(Buffer.from('Hello my name is Linus!')) }) after(function () {