@@ -120,8 +120,6 @@ impact* on performance. Use of the `--zero-fill-buffers` option is recommended
120120only when necessary to enforce that newly allocated ` Buffer ` instances cannot
121121contain potentially sensitive data.
122122
123- Example:
124-
125123``` txt
126124$ node --zero-fill-buffers
127125> Buffer.allocUnsafe(5);
@@ -157,8 +155,6 @@ such as UTF-8, UCS2, Base64, or even Hex-encoded data. It is possible to
157155convert back and forth between ` Buffer ` instances and ordinary JavaScript strings
158156by using an explicit character encoding.
159157
160- Example:
161-
162158``` js
163159const buf = Buffer .from (' hello world' , ' ascii' );
164160
@@ -229,8 +225,6 @@ elements, and not as a byte array of the target type. That is,
229225It is possible to create a new ` Buffer ` that shares the same allocated memory as
230226a [ ` TypedArray ` ] instance by using the TypeArray object's ` .buffer ` property.
231227
232- Example:
233-
234228``` js
235229const arr = new Uint16Array (2 );
236230
@@ -259,8 +253,6 @@ Note that when creating a `Buffer` using a [`TypedArray`]'s `.buffer`, it is
259253possible to use only a portion of the underlying [ ` ArrayBuffer ` ] by passing in
260254` byteOffset ` and ` length ` parameters.
261255
262- Example:
263-
264256``` js
265257const arr = new Uint16Array (20 );
266258const buf = Buffer .from (arr .buffer , 0 , 16 );
@@ -289,8 +281,6 @@ function:
289281` Buffer ` instances can be iterated over using the [ ` ECMAScript 2015 ` ] (ES6) ` for..of `
290282syntax.
291283
292- Example:
293-
294284``` js
295285const buf = Buffer .from ([1 , 2 , 3 ]);
296286
@@ -329,8 +319,6 @@ changes:
329319
330320Allocates a new ` Buffer ` using an ` array ` of octets.
331321
332- Example:
333-
334322``` js
335323// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
336324const buf = new Buffer ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
@@ -370,8 +358,6 @@ share the same allocated memory as the [`TypedArray`].
370358The optional ` byteOffset ` and ` length ` arguments specify a memory range within
371359the ` arrayBuffer ` that will be shared by the ` Buffer ` .
372360
373- Example:
374-
375361``` js
376362const arr = new Uint16Array (2 );
377363
@@ -409,8 +395,6 @@ changes:
409395
410396Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
411397
412- Example:
413-
414398``` js
415399const buf1 = new Buffer (' buffer' );
416400const buf2 = new Buffer (buf1);
@@ -453,8 +437,6 @@ created in this way is *not initialized*. The contents of a newly created
453437[ ` Buffer.alloc(size) ` ] [ `Buffer.alloc()` ] instead to initialize a ` Buffer `
454438to zeroes.
455439
456- Example:
457-
458440``` js
459441const buf = new Buffer (10 );
460442
@@ -514,8 +496,6 @@ changes:
514496Allocates a new ` Buffer ` of ` size ` bytes. If ` fill ` is ` undefined ` , the
515497` Buffer ` will be * zero-filled* .
516498
517- Example:
518-
519499``` js
520500const buf = Buffer .alloc (5 );
521501
@@ -530,8 +510,6 @@ thrown. A zero-length `Buffer` will be created if `size` is 0.
530510If ` fill ` is specified, the allocated ` Buffer ` will be initialized by calling
531511[ ` buf.fill(fill) ` ] [ `buf.fill()` ] .
532512
533- Example:
534-
535513``` js
536514const buf = Buffer .alloc (5 , ' a' );
537515
@@ -542,8 +520,6 @@ console.log(buf);
542520If both ` fill ` and ` encoding ` are specified, the allocated ` Buffer ` will be
543521initialized by calling [ ` buf.fill(fill, encoding) ` ] [ `buf.fill()` ] .
544522
545- Example:
546-
547523``` js
548524const buf = Buffer .alloc (11 , ' aGVsbG8gd29ybGQ=' , ' base64' );
549525
@@ -577,8 +553,6 @@ initialized*. The contents of the newly created `Buffer` are unknown and
577553* may contain sensitive data* . Use [ ` Buffer.alloc() ` ] instead to initialize
578554` Buffer ` instances to zeroes.
579555
580- Example:
581-
582556``` js
583557const buf = Buffer .allocUnsafe (10 );
584558
@@ -635,8 +609,6 @@ memory from a pool for an indeterminate amount of time, it may be appropriate
635609to create an un-pooled ` Buffer ` instance using ` Buffer.allocUnsafeSlow() ` then
636610copy out the relevant bits.
637611
638- Example:
639-
640612``` js
641613// Need to keep around a few small chunks of memory
642614const store = [];
@@ -686,8 +658,6 @@ For `'base64'` and `'hex'`, this function assumes valid input. For strings that
686658contain non-Base64/Hex-encoded data (e.g. whitespace), the return value might be
687659greater than the length of a ` Buffer ` created from the string.
688660
689- Example:
690-
691661``` js
692662const str = ' \u00bd + \u00bc = \u00be ' ;
693663
@@ -716,8 +686,6 @@ Compares `buf1` to `buf2` typically for the purpose of sorting arrays of
716686` Buffer ` instances. This is equivalent to calling
717687[ ` buf1.compare(buf2) ` ] [ `buf.compare()` ] .
718688
719- Example:
720-
721689``` js
722690const buf1 = Buffer .from (' 1234' );
723691const buf2 = Buffer .from (' 0123' );
@@ -757,9 +725,9 @@ If `totalLength` is provided, it is coerced to an unsigned integer. If the
757725combined length of the ` Buffer ` s in ` list ` exceeds ` totalLength ` , the result is
758726truncated to ` totalLength ` .
759727
760- Example: Create a single ` Buffer ` from a list of three ` Buffer ` instances
761-
762728``` js
729+ // Create a single `Buffer` from a list of three `Buffer` instances.
730+
763731const buf1 = Buffer .alloc (10 );
764732const buf2 = Buffer .alloc (14 );
765733const buf3 = Buffer .alloc (18 );
@@ -785,8 +753,6 @@ added: v5.10.0
785753
786754Allocates a new ` Buffer ` using an ` array ` of octets.
787755
788- Example:
789-
790756``` js
791757// Creates a new Buffer containing UTF-8 bytes of the string 'buffer'
792758const buf = Buffer .from ([0x62 , 0x75 , 0x66 , 0x66 , 0x65 , 0x72 ]);
@@ -810,8 +776,6 @@ memory. For example, when passed a reference to the `.buffer` property of a
810776[ ` TypedArray ` ] instance, the newly created ` Buffer ` will share the same
811777allocated memory as the [ ` TypedArray ` ] .
812778
813- Example:
814-
815779``` js
816780const arr = new Uint16Array (2 );
817781
@@ -834,8 +798,6 @@ console.log(buf);
834798The optional ` byteOffset ` and ` length ` arguments specify a memory range within
835799the ` arrayBuffer ` that will be shared by the ` Buffer ` .
836800
837- Example:
838-
839801``` js
840802const ab = new ArrayBuffer (10 );
841803const buf = Buffer .from (ab, 0 , 2 );
@@ -856,8 +818,6 @@ added: v5.10.0
856818
857819Copies the passed ` buffer ` data onto a new ` Buffer ` instance.
858820
859- Example:
860-
861821``` js
862822const buf1 = Buffer .from (' buffer' );
863823const buf2 = Buffer .from (buf1);
@@ -976,9 +936,9 @@ This operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
976936access is the same as ` UInt8Array ` - that is, getting returns ` undefined ` and
977937setting does nothing.
978938
979- Example: Copy an ASCII string into a ` Buffer ` , one byte at a time
980-
981939``` js
940+ // Copy an ASCII string into a `Buffer` one byte at a time.
941+
982942const str = ' Node.js' ;
983943const buf = Buffer .allocUnsafe (str .length );
984944
@@ -1090,10 +1050,8 @@ added: v0.1.90
10901050Copies data from a region of ` buf ` to a region in ` target ` even if the ` target `
10911051memory region overlaps with ` buf ` .
10921052
1093- Example: Create two ` Buffer ` instances, ` buf1 ` and ` buf2 ` , and copy ` buf1 ` from
1094- byte 16 through byte 19 into ` buf2 ` , starting at the 8th byte in ` buf2 `
1095-
10961053``` js
1054+ // Create two `Buffer` instances.
10971055const buf1 = Buffer .allocUnsafe (26 );
10981056const buf2 = Buffer .allocUnsafe (26 ).fill (' !' );
10991057
@@ -1102,16 +1060,17 @@ for (let i = 0; i < 26; i++) {
11021060 buf1[i] = i + 97 ;
11031061}
11041062
1063+ // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`
11051064buf1 .copy (buf2, 8 , 16 , 20 );
11061065
11071066console .log (buf2 .toString (' ascii' , 0 , 25 ));
11081067// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
11091068```
11101069
1111- Example: Create a single ` Buffer ` and copy data from one region to an
1112- overlapping region within the same ` Buffer `
1113-
11141070``` js
1071+ // Create a `Buffer` and copy data from one region to an overlapping region
1072+ // within the same `Buffer`.
1073+
11151074const buf = Buffer .allocUnsafe (26 );
11161075
11171076for (let i = 0 ; i < 26 ; i++ ) {
@@ -1135,9 +1094,9 @@ added: v1.1.0
11351094Creates and returns an [ iterator] of ` [index, byte] ` pairs from the contents of
11361095` buf ` .
11371096
1138- Example: Log the entire contents of a ` Buffer `
1139-
11401097``` js
1098+ // Log the entire contents of a `Buffer`.
1099+
11411100const buf = Buffer .from (' buffer' );
11421101
11431102for (const pair of buf .entries ()) {
@@ -1198,9 +1157,9 @@ Fills `buf` with the specified `value`. If the `offset` and `end` are not given,
11981157the entire ` buf ` will be filled. This is meant to be a small simplification to
11991158allow the creation and filling of a ` Buffer ` to be done on a single line.
12001159
1201- Example: Fill a ` Buffer ` with the ASCII character ` 'h' `
1202-
12031160``` js
1161+ // Fill a `Buffer` with the ASCII character 'h'.
1162+
12041163const b = Buffer .allocUnsafe (50 ).fill (' h' );
12051164
12061165console .log (b .toString ());
@@ -1212,9 +1171,9 @@ console.log(b.toString());
12121171If the final write of a ` fill() ` operation falls on a multi-byte character,
12131172then only the first bytes of that character that fit into ` buf ` are written.
12141173
1215- Example: Fill a ` Buffer ` with a two-byte character
1216-
12171174``` js
1175+ // Fill a `Buffer` with a two-byte character.
1176+
12181177console .log (Buffer .allocUnsafe (3 ).fill (' \u0222 ' ));
12191178// Prints: <Buffer c8 a2 c8>
12201179```
@@ -1355,8 +1314,6 @@ added: v1.1.0
13551314
13561315Creates and returns an [ iterator] of ` buf ` keys (indices).
13571316
1358- Example:
1359-
13601317``` js
13611318const buf = Buffer .from (' buffer' );
13621319
@@ -1457,9 +1414,9 @@ added: v0.1.90
14571414Returns the amount of memory allocated for ` buf ` in bytes. Note that this
14581415does not necessarily reflect the amount of "usable" data within ` buf ` .
14591416
1460- Example: Create a ` Buffer ` and write a shorter ASCII string to it
1461-
14621417``` js
1418+ // Create a `Buffer` and write a shorter ASCII string to it.
1419+
14631420const buf = Buffer .alloc (1234 );
14641421
14651422console .log (buf .length );
@@ -1819,10 +1776,10 @@ that of `end` equal to [`buf.length`].
18191776Modifying the new ` Buffer ` slice will modify the memory in the original ` Buffer `
18201777because the allocated memory of the two objects overlap.
18211778
1822- Example: Create a ` Buffer ` with the ASCII alphabet, take a slice, and then modify
1823- one byte from the original ` Buffer `
1824-
18251779``` js
1780+ // Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
1781+ // from the original `Buffer`.
1782+
18261783const buf1 = Buffer .allocUnsafe (26 );
18271784
18281785for (let i = 0 ; i < 26 ; i++ ) {
@@ -1954,8 +1911,6 @@ added: v0.9.2
19541911Returns a JSON representation of ` buf ` . [ ` JSON.stringify() ` ] implicitly calls
19551912this function when stringifying a ` Buffer ` instance.
19561913
1957- Example:
1958-
19591914``` js
19601915const buf = Buffer .from ([0x1 , 0x2 , 0x3 , 0x4 , 0x5 ]);
19611916const json = JSON .stringify (buf);
@@ -2065,8 +2020,6 @@ The `length` parameter is the number of bytes to write. If `buf` did not contain
20652020enough space to fit the entire string, only a partial amount of ` string ` will
20662021be written. However, partially encoded characters will not be written.
20672022
2068- Example:
2069-
20702023``` js
20712024const buf = Buffer .allocUnsafe (256 );
20722025
@@ -2477,8 +2430,6 @@ In the case where a developer may need to retain a small chunk of memory from a
24772430pool for an indeterminate amount of time, it may be appropriate to create an
24782431un-pooled ` Buffer ` instance using ` SlowBuffer ` then copy out the relevant bits.
24792432
2480- Example:
2481-
24822433``` js
24832434// Need to keep around a few small chunks of memory
24842435const store = [];
@@ -2513,10 +2464,8 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
25132464thrown. A zero-length ` Buffer ` will be created if ` size ` is 0.
25142465
25152466The underlying memory for ` SlowBuffer ` instances is * not initialized* . The
2516- contents of a newly created ` SlowBuffer ` are unknown and may contain
2517- sensitive data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
2518-
2519- Example:
2467+ contents of a newly created ` SlowBuffer ` are unknown and may contain sensitive
2468+ data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
25202469
25212470``` js
25222471const { SlowBuffer } = require (' buffer' );
0 commit comments