-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.json
More file actions
2982 lines (2982 loc) · 194 KB
/
buffer.json
File metadata and controls
2982 lines (2982 loc) · 194 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"type": "module",
"source": "doc/api/buffer.md",
"modules": [
{
"textRaw": "Buffer",
"name": "buffer",
"introduced_in": "v0.1.90",
"stability": 2,
"stabilityText": "Stable",
"desc": "<p>Prior to the introduction of <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a>, the JavaScript language had no\nmechanism for reading or manipulating streams of binary data. The <code>Buffer</code> class\nwas introduced as part of the Node.js API to enable interaction with octet\nstreams in TCP streams, file system operations, and other contexts.</p>\n<p>With <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> now available, the <code>Buffer</code> class implements the\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\"><code>Uint8Array</code></a> API in a manner that is more optimized and suitable for Node.js.</p>\n<p>Instances of the <code>Buffer</code> class are similar to arrays of integers but\ncorrespond to fixed-sized, raw memory allocations outside the V8 heap.\nThe size of the <code>Buffer</code> is established when it is created and cannot be\nchanged.</p>\n<p>The <code>Buffer</code> class is within the global scope, making it unlikely that one\nwould need to ever use <code>require('buffer').Buffer</code>.</p>\n<pre><code class=\"language-js\">// Creates a zero-filled Buffer of length 10.\nconst buf1 = Buffer.alloc(10);\n\n// Creates a Buffer of length 10, filled with 0x1.\nconst buf2 = Buffer.alloc(10, 1);\n\n// Creates an uninitialized buffer of length 10.\n// This is faster than calling Buffer.alloc() but the returned\n// Buffer instance might contain old data that needs to be\n// overwritten using either fill() or write().\nconst buf3 = Buffer.allocUnsafe(10);\n\n// Creates a Buffer containing [0x1, 0x2, 0x3].\nconst buf4 = Buffer.from([1, 2, 3]);\n\n// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].\nconst buf5 = Buffer.from('tést');\n\n// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].\nconst buf6 = Buffer.from('tést', 'latin1');\n</code></pre>",
"modules": [
{
"textRaw": "`Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`",
"name": "`buffer.from()`,_`buffer.alloc()`,_and_`buffer.allocunsafe()`",
"desc": "<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the\n<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code>\ndifferently based on what arguments are provided:</p>\n<ul>\n<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>)\nallocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0,\nthe memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and\n<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently\ninitialized by using either <a href=\"#buffer_buf_fill_value_offset_end_encoding\"><code>buf.fill(0)</code></a> or by writing to the\nentire <code>Buffer</code>. While this behavior is <em>intentional</em> to improve performance,\ndevelopment experience has demonstrated that a more explicit distinction is\nrequired between creating a fast-but-uninitialized <code>Buffer</code> versus creating a\nslower-but-safer <code>Buffer</code>. Starting in Node.js 8.0.0, <code>Buffer(num)</code> and\n<code>new Buffer(num)</code> will return a <code>Buffer</code> with initialized memory.</li>\n<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the\npassed object's data into the <code>Buffer</code>.</li>\n<li>Passing an <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a> or a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer\"><code>SharedArrayBuffer</code></a> returns a <code>Buffer</code> that\nshares allocated memory with the given array buffer.</li>\n</ul>\n<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the\nfirst argument, security and reliability issues can be inadvertently introduced\ninto applications when argument validation or <code>Buffer</code> initialization is not\nperformed.</p>\n<p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone,\nthe various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong>\nand replaced by separate <code>Buffer.from()</code>, <a href=\"#buffer_class_method_buffer_alloc_size_fill_encoding\"><code>Buffer.alloc()</code></a>, and\n<a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> methods.</p>\n<p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors\nto one of these new APIs.</em></p>\n<ul>\n<li><a href=\"#buffer_class_method_buffer_from_array\"><code>Buffer.from(array)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the\nprovided octets.</li>\n<li><a href=\"#buffer_class_method_buffer_from_arraybuffer_byteoffset_length\"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>\nreturns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a>.</li>\n<li><a href=\"#buffer_class_method_buffer_from_buffer\"><code>Buffer.from(buffer)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the\ncontents of the given <code>Buffer</code>.</li>\n<li><a href=\"#buffer_class_method_buffer_from_string_encoding\"><code>Buffer.from(string[, encoding])</code></a> returns a new\n<code>Buffer</code> that <em>contains a copy</em> of the provided string.</li>\n<li><a href=\"#buffer_class_method_buffer_alloc_size_fill_encoding\"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new\ninitialized <code>Buffer</code> of the specified size. This method is slower than\n<a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly\ncreated <code>Buffer</code> instances never contain old data that is potentially\nsensitive.</li>\n<li><a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe(size)</code></a> and\n<a href=\"#buffer_class_method_buffer_allocunsafeslow_size\"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a\nnew uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is\nuninitialized, the allocated segment of memory might contain old data that is\npotentially sensitive.</li>\n</ul>\n<p><code>Buffer</code> instances returned by <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> <em>may</em> be allocated off\na shared internal memory pool if <code>size</code> is less than or equal to half\n<a href=\"#buffer_class_property_buffer_poolsize\"><code>Buffer.poolSize</code></a>. Instances returned by <a href=\"#buffer_class_method_buffer_allocunsafeslow_size\"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em>\nuse the shared internal memory pool.</p>",
"modules": [
{
"textRaw": "The `--zero-fill-buffers` command line option",
"name": "the_`--zero-fill-buffers`_command_line_option",
"meta": {
"added": [
"v5.10.0"
],
"changes": []
},
"desc": "<p>Node.js can be started using the <code>--zero-fill-buffers</code> command line option to\ncause all newly allocated <code>Buffer</code> instances to be zero-filled upon creation by\ndefault, including buffers returned by <code>new Buffer(size)</code>,\n<a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a>, <a href=\"#buffer_class_method_buffer_allocunsafeslow_size\"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code>. Use of this flag can have a significant negative impact on\nperformance. Use of the <code>--zero-fill-buffers</code> option is recommended only when\nnecessary to enforce that newly allocated <code>Buffer</code> instances cannot contain old\ndata that is potentially sensitive.</p>\n<pre><code class=\"language-txt\">$ node --zero-fill-buffers\n> Buffer.allocUnsafe(5);\n<Buffer 00 00 00 00 00>\n</code></pre>",
"type": "module",
"displayName": "The `--zero-fill-buffers` command line option"
},
{
"textRaw": "What makes `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()` \"unsafe\"?",
"name": "what_makes_`buffer.allocunsafe()`_and_`buffer.allocunsafeslow()`_\"unsafe\"?",
"desc": "<p>When calling <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> and <a href=\"#buffer_class_method_buffer_allocunsafeslow_size\"><code>Buffer.allocUnsafeSlow()</code></a>, the\nsegment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While\nthis design makes the allocation of memory quite fast, the allocated segment of\nmemory might contain old data that is potentially sensitive. Using a <code>Buffer</code>\ncreated by <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the memory\ncan allow this old data to be leaked when the <code>Buffer</code> memory is read.</p>\n<p>While there are clear performance advantages to using <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a>,\nextra care <em>must</em> be taken in order to avoid introducing security\nvulnerabilities into an application.</p>",
"type": "module",
"displayName": "What makes `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()` \"unsafe\"?"
}
],
"type": "module",
"displayName": "`Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`"
},
{
"textRaw": "Buffers and Character Encodings",
"name": "buffers_and_character_encodings",
"meta": {
"changes": [
{
"version": "v6.4.0",
"pr-url": "https://github.com/nodejs/node/pull/7111",
"description": "Introduced `latin1` as an alias for `binary`."
},
{
"version": "v5.0.0",
"pr-url": "https://github.com/nodejs/node/pull/2859",
"description": "Removed the deprecated `raw` and `raws` encodings."
}
]
},
"desc": "<p>When string data is stored in or extracted out of a <code>Buffer</code> instance, a\ncharacter encoding may be specified.</p>\n<pre><code class=\"language-js\">const buf = Buffer.from('hello world', 'ascii');\n\nconsole.log(buf.toString('hex'));\n// Prints: 68656c6c6f20776f726c64\nconsole.log(buf.toString('base64'));\n// Prints: aGVsbG8gd29ybGQ=\n\nconsole.log(Buffer.from('fhqwhgads', 'ascii'));\n// Prints: <Buffer 66 68 71 77 68 67 61 64 73>\nconsole.log(Buffer.from('fhqwhgads', 'utf16le'));\n// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>\n</code></pre>\n<p>The character encodings currently supported by Node.js include:</p>\n<ul>\n<li>\n<p><code>'ascii'</code> - For 7-bit ASCII data only. This encoding is fast and will strip\nthe high bit if set.</p>\n</li>\n<li>\n<p><code>'utf8'</code> - Multibyte encoded Unicode characters. Many web pages and other\ndocument formats use UTF-8.</p>\n</li>\n<li>\n<p><code>'utf16le'</code> - 2 or 4 bytes, little-endian encoded Unicode characters.\nSurrogate pairs (U+10000 to U+10FFFF) are supported.</p>\n</li>\n<li>\n<p><code>'ucs2'</code> - Alias of <code>'utf16le'</code>.</p>\n</li>\n<li>\n<p><code>'base64'</code> - Base64 encoding. When creating a <code>Buffer</code> from a string,\nthis encoding will also correctly accept \"URL and Filename Safe Alphabet\" as\nspecified in <a href=\"https://tools.ietf.org/html/rfc4648#section-5\">RFC4648, Section 5</a>.</p>\n</li>\n<li>\n<p><code>'latin1'</code> - A way of encoding the <code>Buffer</code> into a one-byte encoded string\n(as defined by the IANA in <a href=\"https://tools.ietf.org/html/rfc1345\">RFC1345</a>,\npage 63, to be the Latin-1 supplement block and C0/C1 control codes).</p>\n</li>\n<li>\n<p><code>'binary'</code> - Alias for <code>'latin1'</code>.</p>\n</li>\n<li>\n<p><code>'hex'</code> - Encode each byte as two hexadecimal characters.</p>\n</li>\n</ul>\n<p>Modern Web browsers follow the <a href=\"https://encoding.spec.whatwg.org/\">WHATWG Encoding Standard</a> which aliases\nboth <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing\nsomething like <code>http.get()</code>, if the returned charset is one of those listed in\nthe WHATWG specification it is possible that the server actually returned\n<code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode\nthe characters.</p>",
"type": "module",
"displayName": "Buffers and Character Encodings"
},
{
"textRaw": "Buffers and TypedArray",
"name": "buffers_and_typedarray",
"meta": {
"changes": [
{
"version": "v3.0.0",
"pr-url": "https://github.com/nodejs/node/pull/2002",
"description": "The `Buffer`s class now inherits from `Uint8Array`."
}
]
},
"desc": "<p><code>Buffer</code> instances are also <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\"><code>Uint8Array</code></a> instances. However, there are subtle\nincompatibilities with <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a>. For example, while\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice\"><code>ArrayBuffer#slice()</code></a> creates a copy of the slice, the implementation of\n<a href=\"#buffer_buf_slice_start_end\"><code>Buffer#slice()</code></a> creates a view over the existing <code>Buffer</code>\nwithout copying, making <a href=\"#buffer_buf_slice_start_end\"><code>Buffer#slice()</code></a> far more efficient.</p>\n<p>It is also possible to create new <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> instances from a <code>Buffer</code> with\nthe following caveats:</p>\n<ol>\n<li>\n<p>The <code>Buffer</code> object's memory is copied to the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a>, not shared.</p>\n</li>\n<li>\n<p>The <code>Buffer</code> object's memory is interpreted as an array of distinct\nelements, and not as a byte array of the target type. That is,\n<code>new Uint32Array(Buffer.from([1, 2, 3, 4]))</code> creates a 4-element <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array\"><code>Uint32Array</code></a>\nwith elements <code>[1, 2, 3, 4]</code>, not a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array\"><code>Uint32Array</code></a> with a single element\n<code>[0x1020304]</code> or <code>[0x4030201]</code>.</p>\n</li>\n</ol>\n<p>It is possible to create a new <code>Buffer</code> that shares the same allocated memory as\na <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> instance by using the <code>TypeArray</code> object's <code>.buffer</code> property.</p>\n<pre><code class=\"language-js\">const arr = new Uint16Array(2);\n\narr[0] = 5000;\narr[1] = 4000;\n\n// Copies the contents of `arr`\nconst buf1 = Buffer.from(arr);\n// Shares memory with `arr`\nconst buf2 = Buffer.from(arr.buffer);\n\nconsole.log(buf1);\n// Prints: <Buffer 88 a0>\nconsole.log(buf2);\n// Prints: <Buffer 88 13 a0 0f>\n\narr[1] = 6000;\n\nconsole.log(buf1);\n// Prints: <Buffer 88 a0>\nconsole.log(buf2);\n// Prints: <Buffer 88 13 70 17>\n</code></pre>\n<p>Note that when creating a <code>Buffer</code> using a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a>'s <code>.buffer</code>, it is\npossible to use only a portion of the underlying <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a> by passing in\n<code>byteOffset</code> and <code>length</code> parameters.</p>\n<pre><code class=\"language-js\">const arr = new Uint16Array(20);\nconst buf = Buffer.from(arr.buffer, 0, 16);\n\nconsole.log(buf.length);\n// Prints: 16\n</code></pre>\n<p>The <code>Buffer.from()</code> and <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from\"><code>TypedArray.from()</code></a> have different signatures and\nimplementations. Specifically, the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> variants accept a second\nargument that is a mapping function that is invoked on every element of the\ntyped array:</p>\n<ul>\n<li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li>\n</ul>\n<p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping\nfunction:</p>\n<ul>\n<li><a href=\"#buffer_class_method_buffer_from_array\"><code>Buffer.from(array)</code></a></li>\n<li><a href=\"#buffer_class_method_buffer_from_buffer\"><code>Buffer.from(buffer)</code></a></li>\n<li><a href=\"#buffer_class_method_buffer_from_arraybuffer_byteoffset_length\"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li>\n<li><a href=\"#buffer_class_method_buffer_from_string_encoding\"><code>Buffer.from(string[, encoding])</code></a></li>\n</ul>",
"type": "module",
"displayName": "Buffers and TypedArray"
},
{
"textRaw": "Buffers and iteration",
"name": "buffers_and_iteration",
"desc": "<p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p>\n<pre><code class=\"language-js\">const buf = Buffer.from([1, 2, 3]);\n\n// Prints:\n// 1\n// 2\n// 3\nfor (const b of buf) {\n console.log(b);\n}\n</code></pre>\n<p>Additionally, the <a href=\"#buffer_buf_values\"><code>buf.values()</code></a>, <a href=\"#buffer_buf_keys\"><code>buf.keys()</code></a>, and\n<a href=\"#buffer_buf_entries\"><code>buf.entries()</code></a> methods can be used to create iterators.</p>",
"type": "module",
"displayName": "Buffers and iteration"
},
{
"textRaw": "Buffer Constants",
"name": "buffer_constants",
"meta": {
"added": [
"v8.2.0"
],
"changes": []
},
"desc": "<p>Note that <code>buffer.constants</code> is a property on the <code>buffer</code> module returned by\n<code>require('buffer')</code>, not on the <code>Buffer</code> global or a <code>Buffer</code> instance.</p>",
"properties": [
{
"textRaw": "`MAX_LENGTH` {integer} The largest size allowed for a single `Buffer` instance.",
"type": "integer",
"name": "MAX_LENGTH",
"meta": {
"added": [
"v8.2.0"
],
"changes": []
},
"desc": "<p>On 32-bit architectures, this value is <code>(2^30)-1</code> (~1GB).\nOn 64-bit architectures, this value is <code>(2^31)-1</code> (~2GB).</p>\n<p>This value is also available as <a href=\"#buffer_buffer_kmaxlength\"><code>buffer.kMaxLength</code></a>.</p>",
"shortDesc": "The largest size allowed for a single `Buffer` instance."
},
{
"textRaw": "`MAX_STRING_LENGTH` {integer} The largest length allowed for a single `string` instance.",
"type": "integer",
"name": "MAX_STRING_LENGTH",
"meta": {
"added": [
"v8.2.0"
],
"changes": []
},
"desc": "<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted\nin UTF-16 code units.</p>\n<p>This value may depend on the JS engine that is being used.</p>",
"shortDesc": "The largest length allowed for a single `string` instance."
}
],
"type": "module",
"displayName": "Buffer Constants"
}
],
"classes": [
{
"textRaw": "Class: Buffer",
"type": "class",
"name": "Buffer",
"desc": "<p>The <code>Buffer</code> class is a global type for dealing with binary data directly.\nIt can be constructed in a variety of ways.</p>",
"classMethods": [
{
"textRaw": "Class Method: Buffer.alloc(size[, fill[, encoding]])",
"type": "classMethod",
"name": "alloc",
"meta": {
"added": [
"v5.10.0"
],
"changes": [
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/18129",
"description": "Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception."
},
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/17427",
"description": "Specifying an invalid string for `fill` triggers a thrown exception."
},
{
"version": "v8.9.3",
"pr-url": "https://github.com/nodejs/node/pull/17428",
"description": "Specifying an invalid string for `fill` now results in a zero-filled buffer."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`size` {integer} The desired length of the new `Buffer`.",
"name": "size",
"type": "integer",
"desc": "The desired length of the new `Buffer`."
},
{
"textRaw": "`fill` {string|Buffer|integer} A value to pre-fill the new `Buffer` with. **Default:** `0`.",
"name": "fill",
"type": "string|Buffer|integer",
"default": "`0`",
"desc": "A value to pre-fill the new `Buffer` with.",
"optional": true
},
{
"textRaw": "`encoding` {string} If `fill` is a string, this is its encoding. **Default:** `'utf8'`.",
"name": "encoding",
"type": "string",
"default": "`'utf8'`",
"desc": "If `fill` is a string, this is its encoding.",
"optional": true
}
]
}
],
"desc": "<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the\n<code>Buffer</code> will be <em>zero-filled</em>.</p>\n<pre><code class=\"language-js\">const buf = Buffer.alloc(5);\n\nconsole.log(buf);\n// Prints: <Buffer 00 00 00 00 00>\n</code></pre>\n<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than\n<a href=\"#buffer_buffer_constants_max_length\"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href=\"errors.html#ERR_INVALID_OPT_VALUE\"><code>ERR_INVALID_OPT_VALUE</code></a> is\nthrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>\n<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling\n<a href=\"#buffer_buf_fill_value_offset_end_encoding\"><code>buf.fill(fill)</code></a>.</p>\n<pre><code class=\"language-js\">const buf = Buffer.alloc(5, 'a');\n\nconsole.log(buf);\n// Prints: <Buffer 61 61 61 61 61>\n</code></pre>\n<p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be\ninitialized by calling <a href=\"#buffer_buf_fill_value_offset_end_encoding\"><code>buf.fill(fill, encoding)</code></a>.</p>\n<pre><code class=\"language-js\">const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');\n\nconsole.log(buf);\n// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>\n</code></pre>\n<p>Calling <a href=\"#buffer_class_method_buffer_alloc_size_fill_encoding\"><code>Buffer.alloc()</code></a> can be significantly slower than the alternative\n<a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance\ncontents will <em>never contain sensitive data</em>.</p>\n<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>"
},
{
"textRaw": "Class Method: Buffer.allocUnsafe(size)",
"type": "classMethod",
"name": "allocUnsafe",
"meta": {
"added": [
"v5.10.0"
],
"changes": [
{
"version": "v7.0.0",
"pr-url": "https://github.com/nodejs/node/pull/7079",
"description": "Passing a negative `size` will now throw an error."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`size` {integer} The desired length of the new `Buffer`.",
"name": "size",
"type": "integer",
"desc": "The desired length of the new `Buffer`."
}
]
}
],
"desc": "<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than\n<a href=\"#buffer_buffer_constants_max_length\"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href=\"errors.html#ERR_INVALID_OPT_VALUE\"><code>ERR_INVALID_OPT_VALUE</code></a> is\nthrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>\n<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not\ninitialized</em>. The contents of the newly created <code>Buffer</code> are unknown and\n<em>may contain sensitive data</em>. Use <a href=\"#buffer_class_method_buffer_alloc_size_fill_encoding\"><code>Buffer.alloc()</code></a> instead to initialize\n<code>Buffer</code> instances with zeroes.</p>\n<pre><code class=\"language-js\">const buf = Buffer.allocUnsafe(10);\n\nconsole.log(buf);\n// Prints: (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>\n\nbuf.fill(0);\n\nconsole.log(buf);\n// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>\n</code></pre>\n<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>\n<p>Note that the <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of\nsize <a href=\"#buffer_class_property_buffer_poolsize\"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new\n<code>Buffer</code> instances created using <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> and the deprecated\n<code>new Buffer(size)</code> constructor only when <code>size</code> is less than or equal to\n<code>Buffer.poolSize >> 1</code> (floor of <a href=\"#buffer_class_property_buffer_poolsize\"><code>Buffer.poolSize</code></a> divided by two).</p>\n<p>Use of this pre-allocated internal memory pool is a key difference between\ncalling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>.\nSpecifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code>\npool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal\n<code>Buffer</code> pool if <code>size</code> is less than or equal to half <a href=\"#buffer_class_property_buffer_poolsize\"><code>Buffer.poolSize</code></a>. The\ndifference is subtle but can be important when an application requires the\nadditional performance that <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> provides.</p>"
},
{
"textRaw": "Class Method: Buffer.allocUnsafeSlow(size)",
"type": "classMethod",
"name": "allocUnsafeSlow",
"meta": {
"added": [
"v5.12.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`size` {integer} The desired length of the new `Buffer`.",
"name": "size",
"type": "integer",
"desc": "The desired length of the new `Buffer`."
}
]
}
],
"desc": "<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than\n<a href=\"#buffer_buffer_constants_max_length\"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href=\"errors.html#ERR_INVALID_OPT_VALUE\"><code>ERR_INVALID_OPT_VALUE</code></a> is\nthrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>\n<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not\ninitialized</em>. The contents of the newly created <code>Buffer</code> are unknown and\n<em>may contain sensitive data</em>. Use <a href=\"#buffer_buf_fill_value_offset_end_encoding\"><code>buf.fill(0)</code></a> to initialize\nsuch <code>Buffer</code> instances with zeroes.</p>\n<p>When using <a href=\"#buffer_class_method_buffer_allocunsafe_size\"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances,\nallocations under 4KB are sliced from a single pre-allocated <code>Buffer</code>. This\nallows applications to avoid the garbage collection overhead of creating many\nindividually allocated <code>Buffer</code> instances. This approach improves both\nperformance and memory usage by eliminating the need to track and clean up as\nmany persistent objects.</p>\n<p>However, in the case where a developer may need to retain a small chunk of\nmemory from a pool for an indeterminate amount of time, it may be appropriate\nto create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and\nthen copying out the relevant bits.</p>\n<pre><code class=\"language-js\">// Need to keep around a few small chunks of memory\nconst store = [];\n\nsocket.on('readable', () => {\n const data = socket.read();\n\n // Allocate for retained data\n const sb = Buffer.allocUnsafeSlow(10);\n\n // Copy the data into the new allocation\n data.copy(sb, 0, 0, 10);\n\n store.push(sb);\n});\n</code></pre>\n<p><code>Buffer.allocUnsafeSlow()</code> should be used only as a last resort after a\ndeveloper has observed undue memory retention in their applications.</p>\n<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>"
},
{
"textRaw": "Class Method: Buffer.byteLength(string[, encoding])",
"type": "classMethod",
"name": "byteLength",
"meta": {
"added": [
"v0.1.90"
],
"changes": [
{
"version": "v7.0.0",
"pr-url": "https://github.com/nodejs/node/pull/8946",
"description": "Passing invalid input will now throw an error."
},
{
"version": "v5.10.0",
"pr-url": "https://github.com/nodejs/node/pull/5255",
"description": "The `string` parameter can now be any `TypedArray`, `DataView` or `ArrayBuffer`."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {integer} The number of bytes contained within `string`.",
"name": "return",
"type": "integer",
"desc": "The number of bytes contained within `string`."
},
"params": [
{
"textRaw": "`string` {string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} A value to calculate the length of.",
"name": "string",
"type": "string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer",
"desc": "A value to calculate the length of."
},
{
"textRaw": "`encoding` {string} If `string` is a string, this is its encoding. **Default:** `'utf8'`.",
"name": "encoding",
"type": "string",
"default": "`'utf8'`",
"desc": "If `string` is a string, this is its encoding.",
"optional": true
}
]
}
],
"desc": "<p>Returns the actual byte length of a string. This is not the same as\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length\"><code>String.prototype.length</code></a> since that returns the number of <em>characters</em> in\na string.</p>\n<p>For <code>'base64'</code> and <code>'hex'</code>, this function assumes valid input. For strings that\ncontain non-Base64/Hex-encoded data (e.g. whitespace), the return value might be\ngreater than the length of a <code>Buffer</code> created from the string.</p>\n<pre><code class=\"language-js\">const str = '\\u00bd + \\u00bc = \\u00be';\n\nconsole.log(`${str}: ${str.length} characters, ` +\n `${Buffer.byteLength(str, 'utf8')} bytes`);\n// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes\n</code></pre>\n<p>When <code>string</code> is a <code>Buffer</code>/<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView\"><code>DataView</code></a>/<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a>/<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a>/\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer\"><code>SharedArrayBuffer</code></a>, the actual byte length is returned.</p>"
},
{
"textRaw": "Class Method: Buffer.compare(buf1, buf2)",
"type": "classMethod",
"name": "compare",
"meta": {
"added": [
"v0.11.13"
],
"changes": [
{
"version": "v8.0.0",
"pr-url": "https://github.com/nodejs/node/pull/10236",
"description": "The arguments can now be `Uint8Array`s."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {integer}",
"name": "return",
"type": "integer"
},
"params": [
{
"textRaw": "`buf1` {Buffer|Uint8Array}",
"name": "buf1",
"type": "Buffer|Uint8Array"
},
{
"textRaw": "`buf2` {Buffer|Uint8Array}",
"name": "buf2",
"type": "Buffer|Uint8Array"
}
]
}
],
"desc": "<p>Compares <code>buf1</code> to <code>buf2</code> typically for the purpose of sorting arrays of\n<code>Buffer</code> instances. This is equivalent to calling\n<a href=\"#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend\"><code>buf1.compare(buf2)</code></a>.</p>\n<pre><code class=\"language-js\">const buf1 = Buffer.from('1234');\nconst buf2 = Buffer.from('0123');\nconst arr = [buf1, buf2];\n\nconsole.log(arr.sort(Buffer.compare));\n// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]\n// (This result is equal to: [buf2, buf1])\n</code></pre>"
},
{
"textRaw": "Class Method: Buffer.concat(list[, totalLength])",
"type": "classMethod",
"name": "concat",
"meta": {
"added": [
"v0.7.11"
],
"changes": [
{
"version": "v8.0.0",
"pr-url": "https://github.com/nodejs/node/pull/10236",
"description": "The elements of `list` can now be `Uint8Array`s."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {Buffer}",
"name": "return",
"type": "Buffer"
},
"params": [
{
"textRaw": "`list` {Buffer[] | Uint8Array[]} List of `Buffer` or [`Uint8Array`] instances to concat.",
"name": "list",
"type": "Buffer[] | Uint8Array[]",
"desc": "List of `Buffer` or [`Uint8Array`] instances to concat."
},
{
"textRaw": "`totalLength` {integer} Total length of the `Buffer` instances in `list` when concatenated.",
"name": "totalLength",
"type": "integer",
"desc": "Total length of the `Buffer` instances in `list` when concatenated.",
"optional": true
}
]
}
],
"desc": "<p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code>\ninstances in the <code>list</code> together.</p>\n<p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length\n<code>Buffer</code> is returned.</p>\n<p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances\nin <code>list</code>. This however causes an additional loop to be executed in order to\ncalculate the <code>totalLength</code>, so it is faster to provide the length explicitly if\nit is already known.</p>\n<p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the\ncombined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is\ntruncated to <code>totalLength</code>.</p>\n<pre><code class=\"language-js\">// Create a single `Buffer` from a list of three `Buffer` instances.\n\nconst buf1 = Buffer.alloc(10);\nconst buf2 = Buffer.alloc(14);\nconst buf3 = Buffer.alloc(18);\nconst totalLength = buf1.length + buf2.length + buf3.length;\n\nconsole.log(totalLength);\n// Prints: 42\n\nconst bufA = Buffer.concat([buf1, buf2, buf3], totalLength);\n\nconsole.log(bufA);\n// Prints: <Buffer 00 00 00 00 ...>\nconsole.log(bufA.length);\n// Prints: 42\n</code></pre>"
},
{
"textRaw": "Class Method: Buffer.from(array)",
"type": "classMethod",
"name": "from",
"meta": {
"added": [
"v5.10.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`array` {integer[]}",
"name": "array",
"type": "integer[]"
}
]
}
],
"desc": "<p>Allocates a new <code>Buffer</code> using an <code>array</code> of octets.</p>\n<pre><code class=\"language-js\">// Creates a new Buffer containing UTF-8 bytes of the string 'buffer'\nconst buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);\n</code></pre>\n<p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code>.</p>"
},
{
"textRaw": "Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]])",
"type": "classMethod",
"name": "from",
"meta": {
"added": [
"v5.10.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`], [`SharedArrayBuffer`], or the `.buffer` property of a [`TypedArray`].",
"name": "arrayBuffer",
"type": "ArrayBuffer|SharedArrayBuffer",
"desc": "An [`ArrayBuffer`], [`SharedArrayBuffer`], or the `.buffer` property of a [`TypedArray`]."
},
{
"textRaw": "`byteOffset` {integer} Index of first byte to expose. **Default:** `0`.",
"name": "byteOffset",
"type": "integer",
"default": "`0`",
"desc": "Index of first byte to expose.",
"optional": true
},
{
"textRaw": "`length` {integer} Number of bytes to expose. **Default:** `arrayBuffer.length - byteOffset`.",
"name": "length",
"type": "integer",
"default": "`arrayBuffer.length - byteOffset`",
"desc": "Number of bytes to expose.",
"optional": true
}
]
}
],
"desc": "<p>This creates a view of the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a> without copying the underlying\nmemory. For example, when passed a reference to the <code>.buffer</code> property of a\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> instance, the newly created <code>Buffer</code> will share the same\nallocated memory as the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a>.</p>\n<pre><code class=\"language-js\">const arr = new Uint16Array(2);\n\narr[0] = 5000;\narr[1] = 4000;\n\n// Shares memory with `arr`\nconst buf = Buffer.from(arr.buffer);\n\nconsole.log(buf);\n// Prints: <Buffer 88 13 a0 0f>\n\n// Changing the original Uint16Array changes the Buffer also\narr[1] = 6000;\n\nconsole.log(buf);\n// Prints: <Buffer 88 13 70 17>\n</code></pre>\n<p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within\nthe <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p>\n<pre><code class=\"language-js\">const ab = new ArrayBuffer(10);\nconst buf = Buffer.from(ab, 0, 2);\n\nconsole.log(buf.length);\n// Prints: 2\n</code></pre>\n<p>A <code>TypeError</code> will be thrown if <code>arrayBuffer</code> is not an <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a> or a\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer\"><code>SharedArrayBuffer</code></a>.</p>"
},
{
"textRaw": "Class Method: Buffer.from(buffer)",
"type": "classMethod",
"name": "from",
"meta": {
"added": [
"v5.10.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`] from which to copy data.",
"name": "buffer",
"type": "Buffer|Uint8Array",
"desc": "An existing `Buffer` or [`Uint8Array`] from which to copy data."
}
]
}
],
"desc": "<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p>\n<pre><code class=\"language-js\">const buf1 = Buffer.from('buffer');\nconst buf2 = Buffer.from(buf1);\n\nbuf1[0] = 0x61;\n\nconsole.log(buf1.toString());\n// Prints: auffer\nconsole.log(buf2.toString());\n// Prints: buffer\n</code></pre>\n<p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code>.</p>"
},
{
"textRaw": "Class Method: Buffer.from(string[, encoding])",
"type": "classMethod",
"name": "from",
"meta": {
"added": [
"v5.10.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`string` {string} A string to encode.",
"name": "string",
"type": "string",
"desc": "A string to encode."
},
{
"textRaw": "`encoding` {string} The encoding of `string`. **Default:** `'utf8'`.",
"name": "encoding",
"type": "string",
"default": "`'utf8'`",
"desc": "The encoding of `string`.",
"optional": true
}
]
}
],
"desc": "<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies\nthe character encoding of <code>string</code>.</p>\n<pre><code class=\"language-js\">const buf1 = Buffer.from('this is a tést');\nconst buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');\n\nconsole.log(buf1.toString());\n// Prints: this is a tést\nconsole.log(buf2.toString());\n// Prints: this is a tést\nconsole.log(buf1.toString('ascii'));\n// Prints: this is a tC)st\n</code></pre>\n<p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string.</p>"
},
{
"textRaw": "Class Method: Buffer.from(object[, offsetOrEncoding[, length]])",
"type": "classMethod",
"name": "from",
"meta": {
"added": [
"v8.2.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`",
"name": "object",
"type": "Object",
"desc": "An object supporting `Symbol.toPrimitive` or `valueOf()`"
},
{
"textRaw": "`offsetOrEncoding` {number|string} A byte-offset or encoding, depending on the value returned either by `object.valueOf()` or `object[Symbol.toPrimitive]()`.",
"name": "offsetOrEncoding",
"type": "number|string",
"desc": "A byte-offset or encoding, depending on the value returned either by `object.valueOf()` or `object[Symbol.toPrimitive]()`.",
"optional": true
},
{
"textRaw": "`length` {number} A length, depending on the value returned either by `object.valueOf()` or `object[Symbol.toPrimitive]()`.",
"name": "length",
"type": "number",
"desc": "A length, depending on the value returned either by `object.valueOf()` or `object[Symbol.toPrimitive]()`.",
"optional": true
}
]
}
],
"desc": "<p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to\n<code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p>\n<pre><code class=\"language-js\">const buf = Buffer.from(new String('this is a test'));\n// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>\n</code></pre>\n<p>For objects that support <code>Symbol.toPrimitive</code>, returns\n<code>Buffer.from(object[Symbol.toPrimitive](), offsetOrEncoding, length)</code>.</p>\n<pre><code class=\"language-js\">class Foo {\n [Symbol.toPrimitive]() {\n return 'this is a test';\n }\n}\n\nconst buf = Buffer.from(new Foo(), 'utf8');\n// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>\n</code></pre>"
},
{
"textRaw": "Class Method: Buffer.isBuffer(obj)",
"type": "classMethod",
"name": "isBuffer",
"meta": {
"added": [
"v0.1.101"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`obj` {Object}",
"name": "obj",
"type": "Object"
}
]
}
],
"desc": "<p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p>"
},
{
"textRaw": "Class Method: Buffer.isEncoding(encoding)",
"type": "classMethod",
"name": "isEncoding",
"meta": {
"added": [
"v0.9.1"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`encoding` {string} A character encoding name to check.",
"name": "encoding",
"type": "string",
"desc": "A character encoding name to check."
}
]
}
],
"desc": "<p>Returns <code>true</code> if <code>encoding</code> contains a supported character encoding, or <code>false</code>\notherwise.</p>"
}
],
"properties": [
{
"textRaw": "`poolSize` {integer} **Default:** `8192`",
"type": "integer",
"name": "poolSize",
"meta": {
"added": [
"v0.11.3"
],
"changes": []
},
"default": "`8192`",
"desc": "<p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used\nfor pooling. This value may be modified.</p>"
},
{
"textRaw": "buf[index]",
"name": "[index]",
"meta": {
"type": "property",
"name": [
"index"
],
"changes": []
},
"desc": "<p>The index operator <code>[index]</code> can be used to get and set the octet at position\n<code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value\nrange is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p>\n<p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds\naccess is the same as <code>UInt8Array</code> - that is, getting returns <code>undefined</code> and\nsetting does nothing.</p>\n<pre><code class=\"language-js\">// Copy an ASCII string into a `Buffer` one byte at a time.\n\nconst str = 'Node.js';\nconst buf = Buffer.allocUnsafe(str.length);\n\nfor (let i = 0; i < str.length; i++) {\n buf[i] = str.charCodeAt(i);\n}\n\nconsole.log(buf.toString('ascii'));\n// Prints: Node.js\n</code></pre>"
},
{
"textRaw": "`buffer` {ArrayBuffer} The underlying `ArrayBuffer` object based on which this `Buffer` object is created.",
"type": "ArrayBuffer",
"name": "buffer",
"desc": "<pre><code class=\"language-js\">const arrayBuffer = new ArrayBuffer(16);\nconst buffer = Buffer.from(arrayBuffer);\n\nconsole.log(buffer.buffer === arrayBuffer);\n// Prints: true\n</code></pre>",
"shortDesc": "The underlying `ArrayBuffer` object based on which this `Buffer` object is created."
},
{
"textRaw": "`byteOffset` {integer} The `byteOffset` on the underlying `ArrayBuffer` object based on which this `Buffer` object is created.",
"type": "integer",
"name": "byteOffset",
"desc": "<p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>\nor sometimes when allocating a buffer smaller than <code>Buffer.poolSize</code> the\nbuffer doesn't start from a zero offset on the underlying <code>ArrayBuffer</code>.</p>\n<p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly\nusing <code>buf.buffer</code>, as the first bytes in this <code>ArrayBuffer</code> may be unrelated\nto the <code>buf</code> object itself.</p>\n<p>A common issue is when casting a <code>Buffer</code> object to a <code>TypedArray</code> object,\nin this case one needs to specify the <code>byteOffset</code> correctly:</p>\n<pre><code class=\"language-js\">// Create a buffer smaller than `Buffer.poolSize`.\nconst nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);\n\n// When casting the Node.js Buffer to an Int8 TypedArray remember to use the\n// byteOffset.\nnew Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);\n</code></pre>",
"shortDesc": "The `byteOffset` on the underlying `ArrayBuffer` object based on which this `Buffer` object is created."
},
{
"textRaw": "`length` {integer}",
"type": "integer",
"name": "length",
"meta": {
"added": [
"v0.1.90"
],
"changes": []
},
"desc": "<p>Returns the amount of memory allocated for <code>buf</code> in bytes. Note that this\ndoes not necessarily reflect the amount of \"usable\" data within <code>buf</code>.</p>\n<pre><code class=\"language-js\">// Create a `Buffer` and write a shorter ASCII string to it.\n\nconst buf = Buffer.alloc(1234);\n\nconsole.log(buf.length);\n// Prints: 1234\n\nbuf.write('some string', 0, 'ascii');\n\nconsole.log(buf.length);\n// Prints: 1234\n</code></pre>\n<p>While the <code>length</code> property is not immutable, changing the value of <code>length</code>\ncan result in undefined and inconsistent behavior. Applications that wish to\nmodify the length of a <code>Buffer</code> should therefore treat <code>length</code> as read-only and\nuse <a href=\"#buffer_buf_slice_start_end\"><code>buf.slice()</code></a> to create a new <code>Buffer</code>.</p>\n<pre><code class=\"language-js\">let buf = Buffer.allocUnsafe(10);\n\nbuf.write('abcdefghj', 0, 'ascii');\n\nconsole.log(buf.length);\n// Prints: 10\n\nbuf = buf.slice(0, 5);\n\nconsole.log(buf.length);\n// Prints: 5\n</code></pre>"
},
{
"textRaw": "buf.parent",
"name": "parent",
"meta": {
"deprecated": [
"v8.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use [`buf.buffer`] instead.",
"desc": "<p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p>"
}
],
"methods": [
{
"textRaw": "buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])",
"type": "method",
"name": "compare",
"meta": {
"added": [
"v0.11.13"
],
"changes": [
{
"version": "v8.0.0",
"pr-url": "https://github.com/nodejs/node/pull/10236",
"description": "The `target` parameter can now be a `Uint8Array`."
},
{
"version": "v5.11.0",
"pr-url": "https://github.com/nodejs/node/pull/5880",
"description": "Additional parameters for specifying offsets are supported now."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {integer}",
"name": "return",
"type": "integer"
},
"params": [
{
"textRaw": "`target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`] with which to compare `buf`.",
"name": "target",
"type": "Buffer|Uint8Array",
"desc": "A `Buffer` or [`Uint8Array`] with which to compare `buf`."
},
{
"textRaw": "`targetStart` {integer} The offset within `target` at which to begin comparison. **Default:** `0`.",
"name": "targetStart",
"type": "integer",
"default": "`0`",
"desc": "The offset within `target` at which to begin comparison.",
"optional": true
},
{
"textRaw": "`targetEnd` {integer} The offset with `target` at which to end comparison (not inclusive). **Default:** `target.length`.",
"name": "targetEnd",
"type": "integer",
"default": "`target.length`",
"desc": "The offset with `target` at which to end comparison (not inclusive).",
"optional": true
},
{
"textRaw": "`sourceStart` {integer} The offset within `buf` at which to begin comparison. **Default:** `0`.",
"name": "sourceStart",
"type": "integer",
"default": "`0`",
"desc": "The offset within `buf` at which to begin comparison.",
"optional": true
},
{
"textRaw": "`sourceEnd` {integer} The offset within `buf` at which to end comparison (not inclusive). **Default:** [`buf.length`].",
"name": "sourceEnd",
"type": "integer",
"default": "[`buf.length`]",
"desc": "The offset within `buf` at which to end comparison (not inclusive).",
"optional": true
}
]
}
],
"desc": "<p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code>\ncomes before, after, or is the same as <code>target</code> in sort order.\nComparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p>\n<ul>\n<li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li>\n<li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li>\n<li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li>\n</ul>\n<pre><code class=\"language-js\">const buf1 = Buffer.from('ABC');\nconst buf2 = Buffer.from('BCD');\nconst buf3 = Buffer.from('ABCD');\n\nconsole.log(buf1.compare(buf1));\n// Prints: 0\nconsole.log(buf1.compare(buf2));\n// Prints: -1\nconsole.log(buf1.compare(buf3));\n// Prints: -1\nconsole.log(buf2.compare(buf1));\n// Prints: 1\nconsole.log(buf2.compare(buf3));\n// Prints: 1\nconsole.log([buf1, buf2, buf3].sort(Buffer.compare));\n// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]\n// (This result is equal to: [buf1, buf3, buf2])\n</code></pre>\n<p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code>\narguments can be used to limit the comparison to specific ranges within <code>target</code>\nand <code>buf</code> respectively.</p>\n<pre><code class=\"language-js\">const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);\nconst buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);\n\nconsole.log(buf1.compare(buf2, 5, 9, 0, 4));\n// Prints: 0\nconsole.log(buf1.compare(buf2, 0, 6, 4));\n// Prints: -1\nconsole.log(buf1.compare(buf2, 5, 6, 5));\n// Prints: 1\n</code></pre>\n<p><a href=\"errors.html#ERR_OUT_OF_RANGE\"><code>ERR_OUT_OF_RANGE</code></a> is thrown if <code>targetStart < 0</code>, <code>sourceStart < 0</code>,\n<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p>"
},
{
"textRaw": "buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])",
"type": "method",
"name": "copy",
"meta": {
"added": [
"v0.1.90"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {integer} The number of bytes copied.",
"name": "return",
"type": "integer",
"desc": "The number of bytes copied."
},
"params": [
{
"textRaw": "`target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`] to copy into.",
"name": "target",
"type": "Buffer|Uint8Array",
"desc": "A `Buffer` or [`Uint8Array`] to copy into."
},
{
"textRaw": "`targetStart` {integer} The offset within `target` at which to begin writing. **Default:** `0`.",
"name": "targetStart",
"type": "integer",
"default": "`0`",
"desc": "The offset within `target` at which to begin writing.",
"optional": true
},
{
"textRaw": "`sourceStart` {integer} The offset within `buf` from which to begin copying. **Default:** `0`.",
"name": "sourceStart",
"type": "integer",
"default": "`0`",
"desc": "The offset within `buf` from which to begin copying.",
"optional": true
},
{
"textRaw": "`sourceEnd` {integer} The offset within `buf` at which to stop copying (not inclusive). **Default:** [`buf.length`].",
"name": "sourceEnd",
"type": "integer",
"default": "[`buf.length`]",
"desc": "The offset within `buf` at which to stop copying (not inclusive).",
"optional": true
}
]
}
],
"desc": "<p>Copies data from a region of <code>buf</code> to a region in <code>target</code> even if the <code>target</code>\nmemory region overlaps with <code>buf</code>.</p>\n<pre><code class=\"language-js\">// Create two `Buffer` instances.\nconst buf1 = Buffer.allocUnsafe(26);\nconst buf2 = Buffer.allocUnsafe(26).fill('!');\n\nfor (let i = 0; i < 26; i++) {\n // 97 is the decimal ASCII value for 'a'\n buf1[i] = i + 97;\n}\n\n// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`\nbuf1.copy(buf2, 8, 16, 20);\n\nconsole.log(buf2.toString('ascii', 0, 25));\n// Prints: !!!!!!!!qrst!!!!!!!!!!!!!\n</code></pre>\n<pre><code class=\"language-js\">// Create a `Buffer` and copy data from one region to an overlapping region\n// within the same `Buffer`.\n\nconst buf = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n // 97 is the decimal ASCII value for 'a'\n buf[i] = i + 97;\n}\n\nbuf.copy(buf, 0, 4, 10);\n\nconsole.log(buf.toString());\n// Prints: efghijghijklmnopqrstuvwxyz\n</code></pre>"
},
{
"textRaw": "buf.entries()",
"type": "method",
"name": "entries",
"meta": {
"added": [
"v1.1.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {Iterator}",
"name": "return",
"type": "Iterator"
},
"params": []
}
],
"desc": "<p>Creates and returns an <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols\">iterator</a> of <code>[index, byte]</code> pairs from the contents of\n<code>buf</code>.</p>\n<pre><code class=\"language-js\">// Log the entire contents of a `Buffer`.\n\nconst buf = Buffer.from('buffer');\n\nfor (const pair of buf.entries()) {\n console.log(pair);\n}\n// Prints:\n// [0, 98]\n// [1, 117]\n// [2, 102]\n// [3, 102]\n// [4, 101]\n// [5, 114]\n</code></pre>"
},
{
"textRaw": "buf.equals(otherBuffer)",
"type": "method",
"name": "equals",
"meta": {
"added": [
"v0.11.13"
],
"changes": [
{
"version": "v8.0.0",
"pr-url": "https://github.com/nodejs/node/pull/10236",
"description": "The arguments can now be `Uint8Array`s."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`otherBuffer` {Buffer} A `Buffer` or [`Uint8Array`] with which to compare `buf`.",
"name": "otherBuffer",
"type": "Buffer",
"desc": "A `Buffer` or [`Uint8Array`] with which to compare `buf`."
}
]
}
],
"desc": "<p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes,\n<code>false</code> otherwise.</p>\n<pre><code class=\"language-js\">const buf1 = Buffer.from('ABC');\nconst buf2 = Buffer.from('414243', 'hex');\nconst buf3 = Buffer.from('ABCD');\n\nconsole.log(buf1.equals(buf2));\n// Prints: true\nconsole.log(buf1.equals(buf3));\n// Prints: false\n</code></pre>"
},
{
"textRaw": "buf.fill(value[, offset[, end]][, encoding])",
"type": "method",
"name": "fill",
"meta": {
"added": [
"v0.5.0"
],
"changes": [
{
"version": "v11.0.0",
"pr-url": "https://github.com/nodejs/node/pull/22969",
"description": "Throws `ERR_OUT_OF_RANGE` instead of `ERR_INDEX_OUT_OF_RANGE`."
},
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/18790",
"description": "Negative `end` values throw an `ERR_INDEX_OUT_OF_RANGE` error."
},
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/18129",
"description": "Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception."
},
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/17427",
"description": "Specifying an invalid string for `value` triggers a thrown exception."
},
{
"version": "v5.7.0",
"pr-url": "https://github.com/nodejs/node/pull/4935",
"description": "The `encoding` parameter is supported now."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {Buffer} A reference to `buf`.",
"name": "return",
"type": "Buffer",
"desc": "A reference to `buf`."
},
"params": [
{
"textRaw": "`value` {string|Buffer|integer} The value with which to fill `buf`.",
"name": "value",
"type": "string|Buffer|integer",
"desc": "The value with which to fill `buf`."
},
{
"textRaw": "`offset` {integer} Number of bytes to skip before starting to fill `buf`. **Default:** `0`.",
"name": "offset",
"type": "integer",
"default": "`0`",
"desc": "Number of bytes to skip before starting to fill `buf`.",
"optional": true
},
{
"textRaw": "`end` {integer} Where to stop filling `buf` (not inclusive). **Default:** [`buf.length`].",
"name": "end",
"type": "integer",
"default": "[`buf.length`]",
"desc": "Where to stop filling `buf` (not inclusive).",
"optional": true
},
{
"textRaw": "`encoding` {string} The encoding for `value` if `value` is a string. **Default:** `'utf8'`.",
"name": "encoding",
"type": "string",
"default": "`'utf8'`",
"desc": "The encoding for `value` if `value` is a string.",
"optional": true
}
]
}
],
"desc": "<p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given,\nthe entire <code>buf</code> will be filled:</p>\n<pre><code class=\"language-js\">// Fill a `Buffer` with the ASCII character 'h'.\n\nconst b = Buffer.allocUnsafe(50).fill('h');\n\nconsole.log(b.toString());\n// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n</code></pre>\n<p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string or integer.</p>\n<p>If the final write of a <code>fill()</code> operation falls on a multi-byte character,\nthen only the bytes of that character that fit into <code>buf</code> are written:</p>\n<pre><code class=\"language-js\">// Fill a `Buffer` with a two-byte character.\n\nconsole.log(Buffer.allocUnsafe(3).fill('\\u0222'));\n// Prints: <Buffer c8 a2 c8>\n</code></pre>\n<p>If <code>value</code> contains invalid characters, it is truncated; if no valid\nfill data remains, an exception is thrown:</p>\n<pre><code class=\"language-js\">const buf = Buffer.allocUnsafe(5);\n\nconsole.log(buf.fill('a'));\n// Prints: <Buffer 61 61 61 61 61>\nconsole.log(buf.fill('aazz', 'hex'));\n// Prints: <Buffer aa aa aa aa aa>\nconsole.log(buf.fill('zz', 'hex'));\n// Throws an exception.\n</code></pre>"
},
{
"textRaw": "buf.includes(value[, byteOffset][, encoding])",
"type": "method",
"name": "includes",
"meta": {
"added": [
"v5.3.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.",
"name": "return",
"type": "boolean",
"desc": "`true` if `value` was found in `buf`, `false` otherwise."
},
"params": [
{
"textRaw": "`value` {string|Buffer|integer} What to search for.",
"name": "value",
"type": "string|Buffer|integer",
"desc": "What to search for."
},
{
"textRaw": "`byteOffset` {integer} Where to begin searching in `buf`. **Default:** `0`.",
"name": "byteOffset",
"type": "integer",
"default": "`0`",
"desc": "Where to begin searching in `buf`.",
"optional": true
},
{
"textRaw": "`encoding` {string} If `value` is a string, this is its encoding. **Default:** `'utf8'`.",
"name": "encoding",
"type": "string",
"default": "`'utf8'`",
"desc": "If `value` is a string, this is its encoding.",
"optional": true
}
]
}
],
"desc": "<p>Equivalent to <a href=\"#buffer_buf_indexof_value_byteoffset_encoding\"><code>buf.indexOf() !== -1</code></a>.</p>\n<pre><code class=\"language-js\">const buf = Buffer.from('this is a buffer');\n\nconsole.log(buf.includes('this'));\n// Prints: true\nconsole.log(buf.includes('is'));\n// Prints: true\nconsole.log(buf.includes(Buffer.from('a buffer')));\n// Prints: true\nconsole.log(buf.includes(97));\n// Prints: true (97 is the decimal ASCII value for 'a')\nconsole.log(buf.includes(Buffer.from('a buffer example')));\n// Prints: false\nconsole.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));\n// Prints: true\nconsole.log(buf.includes('this', 4));\n// Prints: false\n</code></pre>"
},
{
"textRaw": "buf.indexOf(value[, byteOffset][, encoding])",
"type": "method",
"name": "indexOf",
"meta": {
"added": [
"v1.5.0"
],
"changes": [
{
"version": "v8.0.0",
"pr-url": "https://github.com/nodejs/node/pull/10236",
"description": "The `value` can now be a `Uint8Array`."
},
{
"version": "v5.7.0, v4.4.0",
"pr-url": "https://github.com/nodejs/node/pull/4803",
"description": "When `encoding` is being passed, the `byteOffset` parameter is no longer required."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {integer} The index of the first occurrence of `value` in `buf`, or `-1` if `buf` does not contain `value`.",
"name": "return",
"type": "integer",
"desc": "The index of the first occurrence of `value` in `buf`, or `-1` if `buf` does not contain `value`."
},
"params": [
{
"textRaw": "`value` {string|Buffer|Uint8Array|integer} What to search for.",
"name": "value",
"type": "string|Buffer|Uint8Array|integer",
"desc": "What to search for."