Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: use Buffer.from() instead of new Buffer() #6367

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The `spkac` argument must be a Node.js [`Buffer`][].
```js
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
console.log(cert.verifySpkac(new Buffer(spkac)));
console.log(cert.verifySpkac(Buffer.from(spkac)));
// Prints true or false
```

Expand Down
6 changes: 3 additions & 3 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Example of sending a UDP packet to a random port on `localhost`;

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

```js
const dgram = require('dgram');
const buf1 = new Buffer('Some ');
const buf2 = new Buffer('bytes');
const buf1 = Buffer.from('Some ');
const buf2 = Buffer.from('bytes');
const client = dgram.createSocket('udp4');
client.send([buf1, buf2], 41234, 'localhost', (err) => {
client.close();
Expand Down
4 changes: 2 additions & 2 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function parseHeader(stream, callback) {
var split = str.split(/\n\n/);
header += split.shift();
var remaining = split.join('\n\n');
var buf = new Buffer(remaining, 'utf8');
var buf = Buffer.from(remaining, 'utf8');
if (buf.length)
stream.unshift(buf);
stream.removeListener('error', callback);
Expand Down Expand Up @@ -985,7 +985,7 @@ Counter.prototype._read = function() {
this.push(null);
else {
var str = '' + i;
var buf = new Buffer(str, 'ascii');
var buf = Buffer.from(str, 'ascii');
this.push(buf);
}
};
Expand Down
4 changes: 2 additions & 2 deletions doc/api/string_decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ additional support for utf8.
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');

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

const euro = new Buffer([0xE2, 0x82, 0xAC]);
const euro = Buffer.from([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro));
```

Expand Down
2 changes: 1 addition & 1 deletion doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ util.isBuffer({ length: 0 })
// false
util.isBuffer([])
// false
util.isBuffer(new Buffer('hello world'))
util.isBuffer(Buffer.from('hello world'))
// true
```

Expand Down
4 changes: 2 additions & 2 deletions doc/api/zlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ zlib.deflate(input, (err, buffer) => {
}
});

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

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

zlib.unzip(buffer, { finishFlush: zlib.Z_SYNC_FLUSH }, (err, buffer) => {
if (!err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const StringDecoder = exports.StringDecoder = function(encoding) {
// returned when calling write again with the remaining bytes.
//
// Note: Converting a Buffer containing an orphan surrogate to a String
// currently works, but converting a String to a Buffer (via `new Buffer`, or
// Buffer#write) will replace incomplete surrogates with the unicode
// currently works, but converting a String to a Buffer (via `Buffer.from()`,
Copy link
Member

@DavidCai1111 DavidCai1111 Apr 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be better that we keep the same writing style as afterwards Buffer#write and use Buffer#from here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Copy link
Member

@addaleax addaleax Apr 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer#write is used as a shortcut for Buffer.prototype.write, so it’s something you can call on a Buffer instance. Buffer.from() however needs to be called on the global Buffer object (just as it is written). So, no, it is correct to leave these as they are.

Copy link
Member

@DavidCai1111 DavidCai1111 Apr 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax 👍 , but Buffer.from or Buffer.from() ?

// or Buffer#write) will replace incomplete surrogates with the unicode
// replacement character. See https://codereview.chromium.org/121173009/ .
StringDecoder.prototype.write = function(buffer) {
var charStr = '';
Expand Down