Skip to content

Commit ecba645

Browse files
committed
lib,buffer: replace new Buffer(num) with Buffer.unsafe(num)
1 parent 43e1b38 commit ecba645

File tree

86 files changed

+196
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+196
-197
lines changed

lib/_debugger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Protocol.prototype.execute = function(d) {
108108
var resRawByteLength = Buffer.byteLength(res.raw, 'utf8');
109109

110110
if (resRawByteLength - this.bodyStartByteIndex >= this.contentLength) {
111-
var buf = new Buffer(resRawByteLength);
111+
var buf = Buffer.unsafe(resRawByteLength);
112112
buf.write(res.raw, 0, resRawByteLength, 'utf8');
113113
res.body =
114114
buf.slice(this.bodyStartByteIndex,

lib/_stream_readable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ function fromList(n, state) {
858858
if (stringMode)
859859
ret = '';
860860
else
861-
ret = new Buffer(n);
861+
ret = Buffer.unsafe(n);
862862

863863
var c = 0;
864864
for (var i = 0, l = list.length; i < l && c < n; i++) {

lib/_tls_legacy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function SlabBuffer() {
2323

2424
SlabBuffer.prototype.create = function create() {
2525
this.isFull = false;
26-
this.pool = new Buffer(tls.SLAB_BUFFER_SIZE);
26+
this.pool = Buffer.unsafe(tls.SLAB_BUFFER_SIZE);
2727
this.offset = 0;
2828
this.remaining = this.pool.length;
2929
};

lib/fs.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ ReadFileContext.prototype.read = function() {
285285
var length;
286286

287287
if (this.size === 0) {
288-
buffer = this.buffer = new SlowBuffer(kReadFileBufferLength);
288+
buffer = this.buffer = SlowBuffer.unsafe(kReadFileBufferLength);
289289
offset = 0;
290290
length = kReadFileBufferLength;
291291
} else {
@@ -353,7 +353,7 @@ function readFileAfterStat(err, st) {
353353
return context.close(err);
354354
}
355355

356-
context.buffer = new SlowBuffer(size);
356+
context.buffer = SlowBuffer.unsafe(size);
357357
context.read();
358358
}
359359

@@ -450,7 +450,7 @@ fs.readFileSync = function(path, options) {
450450
} else {
451451
threw = true;
452452
try {
453-
buffer = new Buffer(size);
453+
buffer = Buffer.unsafe(size);
454454
threw = false;
455455
} finally {
456456
if (threw && !isUserFd) fs.closeSync(fd);
@@ -468,7 +468,7 @@ fs.readFileSync = function(path, options) {
468468
} else {
469469
// the kernel lies about many files.
470470
// Go ahead and try to read some bytes.
471-
buffer = new Buffer(8192);
471+
buffer = Buffer.unsafe(8192);
472472
bytesRead = fs.readSync(fd, buffer, 0, 8192);
473473
if (bytesRead) {
474474
buffers.push(buffer.slice(0, bytesRead));
@@ -594,7 +594,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
594594

595595
position = arguments[2];
596596
length = arguments[1];
597-
buffer = new Buffer(length);
597+
buffer = Buffer.unsafe(length);
598598
offset = 0;
599599

600600
callback = function(err, bytesRead) {
@@ -649,7 +649,7 @@ fs.readSync = function(fd, buffer, offset, length, position) {
649649

650650
position = arguments[2];
651651
length = arguments[1];
652-
buffer = new Buffer(length);
652+
buffer = Buffer.unsafe(length);
653653

654654
offset = 0;
655655
}
@@ -1696,7 +1696,7 @@ fs.realpath = function realpath(p, cache, cb) {
16961696
var pool;
16971697

16981698
function allocNewPool(poolSize) {
1699-
pool = new Buffer(poolSize);
1699+
pool = Buffer.unsafe(poolSize);
17001700
pool.used = 0;
17011701
}
17021702

lib/internal/v8_prof_polyfill.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ try {
6060
process.exit(1);
6161
}
6262
const fd = fs.openSync(logFile, 'r');
63-
const buf = new Buffer(4096);
63+
const buf = Buffer.unsafe(4096);
6464
const dec = new (require('string_decoder').StringDecoder)('utf-8');
6565
var line = '';
6666
versionCheck();

lib/string_decoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const StringDecoder = exports.StringDecoder = function(encoding) {
4444

4545
// Enough space to store all bytes of a single character. UTF-8 needs 4
4646
// bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
47-
this.charBuffer = new Buffer(6);
47+
this.charBuffer = Buffer.unsafe(6);
4848
// Number of bytes received for the current incomplete multi-byte character.
4949
this.charReceived = 0;
5050
// Number of bytes expected for the current incomplete multi-byte character.

lib/tls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ exports.getCiphers = function() {
3333
// Convert protocols array into valid OpenSSL protocols list
3434
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
3535
function convertProtocols(protocols) {
36-
var buff = new Buffer(protocols.reduce(function(p, c) {
36+
var buff = Buffer.unsafe(protocols.reduce(function(p, c) {
3737
return p + 1 + Buffer.byteLength(c);
3838
}, 0));
3939

lib/zlib.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ function Zlib(opts, mode) {
378378
strategy,
379379
opts.dictionary);
380380

381-
this._buffer = new Buffer(this._chunkSize);
381+
this._buffer = Buffer.unsafe(this._chunkSize);
382382
this._offset = 0;
383383
this._closed = false;
384384
this._level = level;
@@ -426,7 +426,7 @@ Zlib.prototype.reset = function() {
426426
// This is the _flush function called by the transform class,
427427
// internally, when the last chunk has been written.
428428
Zlib.prototype._flush = function(callback) {
429-
this._transform(new Buffer(0), '', callback);
429+
this._transform(Buffer.unsafe(0), '', callback);
430430
};
431431

432432
Zlib.prototype.flush = function(kind, callback) {
@@ -449,7 +449,7 @@ Zlib.prototype.flush = function(kind, callback) {
449449
}
450450
} else {
451451
this._flushFlag = kind;
452-
this.write(new Buffer(0), '', callback);
452+
this.write(Buffer.unsafe(0), '', callback);
453453
}
454454
};
455455

@@ -580,7 +580,7 @@ Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
580580
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
581581
availOutBefore = self._chunkSize;
582582
self._offset = 0;
583-
self._buffer = new Buffer(self._chunkSize);
583+
self._buffer = Buffer.unsafe(self._chunkSize);
584584
}
585585

586586
if (availOutAfter === 0) {

test/disabled/test-dgram-send-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var assert = require('assert'),
1313
common = require('../common'),
1414
dgram = require('dgram');
1515

16-
var buf = new Buffer(1024);
16+
var buf = Buffer.unsafe(1024);
1717
buf.fill(42);
1818

1919
var packetsReceived = 0,

test/disabled/test-fs-largefile.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fs.truncateSync(fd, offset);
1212
assert.equal(fs.statSync(filepath).size, offset);
1313
var writeBuf = new Buffer(message);
1414
fs.writeSync(fd, writeBuf, 0, writeBuf.length, offset);
15-
var readBuf = new Buffer(writeBuf.length);
15+
var readBuf = Buffer.unsafe(writeBuf.length);
1616
fs.readSync(fd, readBuf, 0, readBuf.length, offset);
1717
assert.equal(readBuf.toString(), message);
1818
fs.readSync(fd, readBuf, 0, 1, 0);
@@ -32,4 +32,3 @@ fs.close(fd);
3232
process.on('exit', function() {
3333
fs.unlinkSync(filepath);
3434
});
35-

0 commit comments

Comments
 (0)