Skip to content

Commit d38238e

Browse files
authored
Use modern Buffer APIs (LinusU#11)
Fixes the LinusU#10 `DeprecationWarning: Buffer() is deprecated due to security and usability issues` warning
2 parents 7402b5d + 13aba98 commit d38238e

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

lib/create.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var findVolume = function (startPath, startStat) {
3030
}
3131

3232
var utf16be = function (str) {
33-
var b = new Buffer(str, 'ucs2')
33+
var b = Buffer.from(str, 'ucs2')
3434
for (var i = 0; i < b.length; i += 2) {
3535
var a = b[i]
3636
b[i] = b[i + 1]
@@ -72,7 +72,7 @@ module.exports = exports = function (targetPath, options) {
7272
};
7373

7474
(function addType0 () {
75-
var b = new Buffer(info.parent.name, 'utf8')
75+
var b = Buffer.from(info.parent.name, 'utf8')
7676

7777
info.extra.push({
7878
type: 0,
@@ -82,7 +82,7 @@ module.exports = exports = function (targetPath, options) {
8282
}());
8383

8484
(function addType1 () {
85-
var b = new Buffer(4)
85+
var b = Buffer.alloc(4)
8686

8787
b.writeUInt32BE(info.parent.id, 0)
8888

@@ -95,7 +95,7 @@ module.exports = exports = function (targetPath, options) {
9595

9696
(function addType14 () {
9797
var l = info.target.filename.length
98-
var b = new Buffer(2 + (l * 2))
98+
var b = Buffer.alloc(2 + (l * 2))
9999

100100
b.writeUInt16BE(l, 0)
101101
utf16be(info.target.filename).copy(b, 2)
@@ -109,7 +109,7 @@ module.exports = exports = function (targetPath, options) {
109109

110110
(function addType15 () {
111111
var l = info.volume.name.length
112-
var b = new Buffer(2 + (l * 2))
112+
var b = Buffer.alloc(2 + (l * 2))
113113

114114
b.writeUInt16BE(l, 0)
115115
utf16be(info.volume.name).copy(b, 2)
@@ -125,7 +125,7 @@ module.exports = exports = function (targetPath, options) {
125125
var vl = volumePath.length
126126
assert.equal(targetPath.slice(0, vl), volumePath)
127127
var lp = targetPath.slice(vl)
128-
var b = new Buffer(lp, 'utf8')
128+
var b = Buffer.from(lp, 'utf8')
129129

130130
info.extra.push({
131131
type: 18,
@@ -135,7 +135,7 @@ module.exports = exports = function (targetPath, options) {
135135
}());
136136

137137
(function addType19 () {
138-
var b = new Buffer(volumePath, 'utf8')
138+
var b = Buffer.from(volumePath, 'utf8')
139139

140140
info.extra.push({
141141
type: 19,

lib/encode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = exports = function (info) {
2323
}, 0)
2424
var trailerLength = 4
2525

26-
var buf = new Buffer(baseLength + extraLength + trailerLength)
26+
var buf = Buffer.alloc(baseLength + extraLength + trailerLength)
2727

2828
buf.writeUInt32BE(0, 0)
2929

lib/is-alias.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = function isAlias (path) {
66
var fd = fs.openSync(path, 'r')
77

88
try {
9-
read = new Buffer(16)
9+
read = Buffer.alloc(16)
1010
fs.readSync(fd, read, 0, 16, 0)
1111
} finally {
1212
fs.closeSync(fd)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"contributors": [
88
"Linus Unnebäck <linus@folkdatorn.se>",
99
"Davide Liessi <davide.liessi@gmail.com>",
10-
"Joshua Warner <joshuawarner32@gmail.com>"
10+
"Joshua Warner <joshuawarner32@gmail.com>",
11+
"Jozef Izso <jozef.izso@@gmail.com>"
1112
],
1213
"devDependencies": {
1314
"fs-temp": "^1.1.1",

test/basics.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var path = require('path')
77
var temp = require('fs-temp')
88
var assert = require('assert')
99

10-
var rawData = new Buffer(
10+
var rawData = Buffer.from(
1111
'AAAAAAEqAAIAAApUZXN0IFRpdGxlAAAAAAAAAAAAAAAAAAAAAADO615USCsA' +
1212
'BQAAABMMVGVzdEJrZy50aWZmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +
1313
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFM7rXlgAAAAAAAAAAP////8A' +
@@ -72,8 +72,8 @@ describe('isAlias', function () {
7272
var aliasFile, garbageFile
7373

7474
before(function () {
75-
aliasFile = temp.writeFileSync(new Buffer('626f6f6b000000006d61726b00000000', 'hex'))
76-
garbageFile = temp.writeFileSync(new Buffer('Hello my name is Linus!'))
75+
aliasFile = temp.writeFileSync(Buffer.from('626f6f6b000000006d61726b00000000', 'hex'))
76+
garbageFile = temp.writeFileSync(Buffer.from('Hello my name is Linus!'))
7777
})
7878

7979
after(function () {

0 commit comments

Comments
 (0)