Skip to content

Commit 19fb134

Browse files
JacksonTianFishrock123
authored andcommitted
doc: use Buffer.from() instead of new Buffer()
Use new API of Buffer to developers in most documents. PR-URL: #6367 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
1 parent cbbe95e commit 19fb134

File tree

7 files changed

+13
-13
lines changed

7 files changed

+13
-13
lines changed

doc/api/crypto.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ The `spkac` argument must be a Node.js [`Buffer`][].
9393
```js
9494
const cert = require('crypto').Certificate();
9595
const spkac = getSpkacSomehow();
96-
console.log(cert.verifySpkac(new Buffer(spkac)));
96+
console.log(cert.verifySpkac(Buffer.from(spkac)));
9797
// Prints true or false
9898
```
9999

doc/api/dgram.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Example of sending a UDP packet to a random port on `localhost`;
233233

234234
```js
235235
const dgram = require('dgram');
236-
const message = new Buffer('Some bytes');
236+
const message = Buffer.from('Some bytes');
237237
const client = dgram.createSocket('udp4');
238238
client.send(message, 41234, 'localhost', (err) => {
239239
client.close();
@@ -244,8 +244,8 @@ Example of sending a UDP packet composed of multiple buffers to a random port on
244244

245245
```js
246246
const dgram = require('dgram');
247-
const buf1 = new Buffer('Some ');
248-
const buf2 = new Buffer('bytes');
247+
const buf1 = Buffer.from('Some ');
248+
const buf2 = Buffer.from('bytes');
249249
const client = dgram.createSocket('udp4');
250250
client.send([buf1, buf2], 41234, 'localhost', (err) => {
251251
client.close();

doc/api/stream.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function parseHeader(stream, callback) {
490490
var split = str.split(/\n\n/);
491491
header += split.shift();
492492
var remaining = split.join('\n\n');
493-
var buf = new Buffer(remaining, 'utf8');
493+
var buf = Buffer.from(remaining, 'utf8');
494494
if (buf.length)
495495
stream.unshift(buf);
496496
stream.removeListener('error', callback);
@@ -985,7 +985,7 @@ Counter.prototype._read = function() {
985985
this.push(null);
986986
else {
987987
var str = '' + i;
988-
var buf = new Buffer(str, 'ascii');
988+
var buf = Buffer.from(str, 'ascii');
989989
this.push(buf);
990990
}
991991
};

doc/api/string_decoder.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ additional support for utf8.
1010
const StringDecoder = require('string_decoder').StringDecoder;
1111
const decoder = new StringDecoder('utf8');
1212

13-
const cent = new Buffer([0xC2, 0xA2]);
13+
const cent = Buffer.from([0xC2, 0xA2]);
1414
console.log(decoder.write(cent));
1515

16-
const euro = new Buffer([0xE2, 0x82, 0xAC]);
16+
const euro = Buffer.from([0xE2, 0x82, 0xAC]);
1717
console.log(decoder.write(euro));
1818
```
1919

doc/api/util.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ util.isBuffer({ length: 0 })
297297
// false
298298
util.isBuffer([])
299299
// false
300-
util.isBuffer(new Buffer('hello world'))
300+
util.isBuffer(Buffer.from('hello world'))
301301
// true
302302
```
303303

doc/api/zlib.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ zlib.deflate(input, (err, buffer) => {
3737
}
3838
});
3939

40-
const buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');
40+
const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
4141
zlib.unzip(buffer, (err, buffer) => {
4242
if (!err) {
4343
console.log(buffer.toString());
@@ -117,7 +117,7 @@ method that is used to compressed the last chunk of input data:
117117

118118
```js
119119
// This is a truncated version of the buffer from the above examples
120-
const buffer = new Buffer('eJzT0yMA', 'base64');
120+
const buffer = Buffer.from('eJzT0yMA', 'base64');
121121

122122
zlib.unzip(buffer, { finishFlush: zlib.Z_SYNC_FLUSH }, (err, buffer) => {
123123
if (!err) {

lib/string_decoder.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const StringDecoder = exports.StringDecoder = function(encoding) {
5858
// returned when calling write again with the remaining bytes.
5959
//
6060
// Note: Converting a Buffer containing an orphan surrogate to a String
61-
// currently works, but converting a String to a Buffer (via `new Buffer`, or
62-
// Buffer#write) will replace incomplete surrogates with the unicode
61+
// currently works, but converting a String to a Buffer (via `Buffer.from()`,
62+
// or Buffer#write) will replace incomplete surrogates with the unicode
6363
// replacement character. See https://codereview.chromium.org/121173009/ .
6464
StringDecoder.prototype.write = function(buffer) {
6565
var charStr = '';

0 commit comments

Comments
 (0)