Skip to content

Commit a57dc06

Browse files
targosMylesBorins
authored andcommitted
doc: improve Buffer's encoding documentation
- Add a paragraph about case-insensitivity of encoding options. - Document "utf-8", "utf-16le" and "ucs-2" aliases. - Always use "utf8" in documentation for defaults and examples. PR-URL: #37945 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent f3fabb5 commit a57dc06

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

doc/api/buffer.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,21 @@ console.log(Buffer.from('fhqwhgads', 'utf16le'));
7979
// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
8080
```
8181

82+
Node.js buffers accept all case variations of encoding strings that they
83+
receive. For example, UTF-8 can be specified as `'utf8'`, `'UTF8'` or `'uTf8'`.
84+
8285
The character encodings currently supported by Node.js are the following:
8386

84-
* `'utf8'`: Multi-byte encoded Unicode characters. Many web pages and other
85-
document formats use [UTF-8][]. This is the default character encoding.
86-
When decoding a `Buffer` into a string that does not exclusively contain
87-
valid UTF-8 data, the Unicode replacement character `U+FFFD` � will be used
88-
to represent those errors.
87+
* `'utf8'` (alias: `'utf-8'`): Multi-byte encoded Unicode characters. Many web
88+
pages and other document formats use [UTF-8][]. This is the default character
89+
encoding. When decoding a `Buffer` into a string that does not exclusively
90+
contain valid UTF-8 data, the Unicode replacement character `U+FFFD` � will be
91+
used to represent those errors.
8992

90-
* `'utf16le'`: Multi-byte encoded Unicode characters. Unlike `'utf8'`, each
91-
character in the string will be encoded using either 2 or 4 bytes.
92-
Node.js only supports the [little-endian][endianness] variant of [UTF-16][].
93+
* `'utf16le'` (alias: `'utf-16le'`): Multi-byte encoded Unicode characters.
94+
Unlike `'utf8'`, each character in the string will be encoded using either 2
95+
or 4 bytes. Node.js only supports the [little-endian][endianness] variant of
96+
[UTF-16][].
9397

9498
* `'latin1'`: Latin-1 stands for [ISO-8859-1][]. This character encoding only
9599
supports the Unicode characters from `U+0000` to `U+00FF`. Each character is
@@ -132,11 +136,11 @@ The following legacy character encodings are also supported:
132136
* `'binary'`: Alias for `'latin1'`. See [binary strings][] for more background
133137
on this topic. The name of this encoding can be very misleading, as all of the
134138
encodings listed here convert between strings and binary data. For converting
135-
between strings and `Buffer`s, typically `'utf-8'` is the right choice.
139+
between strings and `Buffer`s, typically `'utf8'` is the right choice.
136140

137-
* `'ucs2'`: Alias of `'utf16le'`. UCS-2 used to refer to a variant of UTF-16
138-
that did not support characters that had code points larger than U+FFFF.
139-
In Node.js, these code points are always supported.
141+
* `'ucs2'`, `'ucs-2'`: Aliases of `'utf16le'`. UCS-2 used to refer to a variant
142+
of UTF-16 that did not support characters that had code points larger than
143+
U+FFFF. In Node.js, these code points are always supported.
140144

141145
```js
142146
Buffer.from('1ag', 'hex');
@@ -900,7 +904,7 @@ Returns `true` if `encoding` is the name of a supported character encoding,
900904
or `false` otherwise.
901905

902906
```js
903-
console.log(Buffer.isEncoding('utf-8'));
907+
console.log(Buffer.isEncoding('utf8'));
904908
// Prints: true
905909

906910
console.log(Buffer.isEncoding('hex'));

doc/api/http.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2323,7 +2323,7 @@ changes:
23232323
-->
23242324

23252325
* `chunk` {string | Buffer}
2326-
* `encoding` {string} Optional, **Default**: `utf-8`
2326+
* `encoding` {string} Optional, **Default**: `utf8`
23272327
* `callback` {Function} Optional
23282328
* Returns: {this}
23292329

@@ -2565,7 +2565,7 @@ changes:
25652565
-->
25662566

25672567
* `chunk` {string | Buffer}
2568-
* `encoding` {string} **Default**: `utf-8`
2568+
* `encoding` {string} **Default**: `utf8`
25692569
* `callback` {Function}
25702570
* Returns {boolean}
25712571

doc/api/stream.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ const fs = require('fs');
27592759

27602760
pipeline(
27612761
fs.createReadStream('object.json')
2762-
.setEncoding('utf-8'),
2762+
.setEncoding('utf8'),
27632763
new Transform({
27642764
decodeStrings: false, // Accept string input rather than Buffers
27652765
construct(callback) {

0 commit comments

Comments
 (0)