@@ -4,96 +4,97 @@ import io.scalajs.collection.Iterator
4
4
5
5
import scala .scalajs .js
6
6
import scala .scalajs .js .annotation .{JSBracketAccess , JSGlobal , JSImport }
7
- import scala .scalajs .js .typedarray .ArrayBuffer
7
+ import scala .scalajs .js .typedarray .{ ArrayBuffer , DataView , TypedArray , Uint8Array }
8
8
import scala .scalajs .js .|
9
9
10
10
/**
11
- * Prior to the introduction of TypedArray in ECMAScript 2015 (ES6), the JavaScript language had no mechanism for
12
- * reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to
13
- * make it possible to interact with octet streams in the context of things like TCP streams and file system operations.
14
- *
15
- * Now that TypedArray has been added in ES6, the Buffer class implements the Uint8Array API in a manner that is more
16
- * optimized and suitable for Node.js' use cases.
17
- *
18
- * Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory
19
- * allocations outside the V8 heap. The size of the Buffer is established when it is created and cannot be resized.
20
- *
21
- * The Buffer class is a global within Node.js, making it unlikely that one would need to ever use require('buffer').
22
- * @see https://nodejs.org/api/buffer.html
11
+ * @see [[https://nodejs.org/api/buffer.html ]]
23
12
*/
24
13
@ js.native
25
14
@ JSImport (" buffer" , " Buffer" )
26
- class Buffer () extends js. Object {
15
+ class Buffer protected () extends Uint8Array ( /* dummy to trick constructor */ - 1 ) {
27
16
28
17
// ///////////////////////////////////////////////////////////////////////////////
29
18
// Constructors
30
19
// ///////////////////////////////////////////////////////////////////////////////
31
20
32
21
/**
33
- * @example new Buffer(size)
22
+ * Use [[Buffer.alloc() ]] instead.
23
+ *
24
+ * @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_size ]]
34
25
*/
35
26
@ inline
27
+ @ deprecated(" Use Buffer.alloc(size) instead." , since = " Node.js v6.0.0" )
36
28
def this (size : Int ) = this ()
37
29
38
30
/**
39
- * @example new Buffer(str, [encoding])
40
- */
41
- @ inline
42
- @ deprecated(" Use Buffer.from(str[, encoding]) instead." , since = " 6.0.0" )
43
- def this (str : String , encoding : String = js.native) = this ()
44
-
45
- /**
46
- * @example new Buffer(array)
31
+ * Use [[Buffer.from(str,encoding) ]] instead.
32
+ *
33
+ * @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_string_encoding ]]
47
34
*/
48
35
@ inline
49
- @ deprecated(" Use Buffer.from(array ) instead." , since = " 6 .0.0" )
50
- def this (array : js. Array [ Int ] ) = this ()
36
+ @ deprecated(" Use Buffer.from(str[, encoding] ) instead." , since = " Node.js v6 .0.0" )
37
+ def this (str : String ) = this ()
51
38
52
39
/**
53
- * @example new Buffer(buffer)
40
+ * Use [[Buffer.from(str,encoding) ]] instead.
41
+ *
42
+ * @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_string_encoding ]]
54
43
*/
55
44
@ inline
56
- @ deprecated(" Use Buffer.from(buffer ) instead." , since = " 6 .0.0" )
57
- def this (buffer : Buffer ) = this ()
45
+ @ deprecated(" Use Buffer.from(str[, encoding] ) instead." , since = " Node.js v6 .0.0" )
46
+ def this (str : String , encoding : String ) = this ()
58
47
59
48
/**
60
- * @example {{{ new Buffer(arrayBuffer[, byteOffset[, length]]) }}}
49
+ * Use [[Buffer.from(array) ]] instead.
50
+ * @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_array ]]
61
51
*/
62
52
@ inline
63
- @ deprecated(" Use Buffer.from(arrayBuffer[, byteOffset [, length]] ) instead." , since = " 6 .0.0" )
64
- def this (arrayBuffer : ArrayBuffer , byteOffset : Int , length : Int ) = this ()
53
+ @ deprecated(" Use Buffer.from(array ) instead." , since = " Node.js v6 .0.0" )
54
+ def this (array : js. Array [ Int ] ) = this ()
65
55
66
56
/**
67
- * @example {{{ new Buffer(arrayBuffer[, byteOffset[, length]]) }}}
57
+ * Use [[Buffer.from(buffer) ]] instead.
58
+ *
59
+ * @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_buffer ]]
68
60
*/
69
61
@ inline
70
- @ deprecated(" Use Buffer.from(arrayBuffer[, byteOffset [, length]] ) instead." , since = " 6 .0.0" )
71
- def this (arrayBuffer : ArrayBuffer , byteOffset : Int ) = this ()
62
+ @ deprecated(" Use Buffer.from(buffer ) instead." , since = " Node.js v6 .0.0" )
63
+ def this (buffer : Buffer ) = this ()
72
64
73
65
/**
74
- * @example {{{ new Buffer(arrayBuffer[, byteOffset[, length]]) }}}
66
+ * Use [[Buffer.from(arrayBuffer,byteOffset,length) ]] instead.
67
+ *
68
+ * @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_arraybuffer_byteoffset_length ]]
75
69
*/
76
70
@ inline
77
- @ deprecated(" Use Buffer.from(arrayBuffer[, byteOffset [, length]]) instead." , since = " 6 .0.0" )
78
- def this (arrayBuffer : ArrayBuffer ) = this ()
71
+ @ deprecated(" Use Buffer.from(arrayBuffer[, byteOffset [, length]]) instead." , since = " Node.js v6 .0.0" )
72
+ def this (arrayBuffer : ArrayBuffer , byteOffset : Int = js.native, length : Int = js.native ) = this ()
79
73
80
74
// ///////////////////////////////////////////////////////////////////////////////
81
75
// Accessors and Mutators
82
76
// ///////////////////////////////////////////////////////////////////////////////
83
77
84
78
/**
85
- * The index operator [index] can be used to get and set the octet at position index in buf. The values refer
86
- * to individual bytes, so the legal value range is between 0x00 and 0xFF (hex) or 0 and 255 (decimal).
79
+ * The index operator `[index]` can be used to get and set the octet at position `index` in `buf`.
80
+ * The values refer to individual bytes, so the legal value range is between `0x00` and `0xFF` (hex) or
81
+ * `0` and `255` (decimal).
82
+ *
83
+ * This operator is inherited from [[Uint8Array ]], so its behavior on out-of-bounds access is the same as
84
+ * [[Uint8Array ]] - that is, getting returns `undefined` and setting does nothing.
85
+ *
86
+ * Note) In Scala.js, getting on out-of-bounds access will throw, since `undefined` can not be casted to `Short`.
87
+ *
87
88
* @param index the given index
88
89
* @return the value at the given index
89
90
*/
90
91
@ JSBracketAccess
91
- def apply (index : Int ): Int = js.native
92
+ override def apply (index : Int ): Short = js.native
92
93
93
94
/**
94
- * The index operator [index] can be used to get and set the octet at position index in buf. The values refer
95
- * to individual bytes, so the legal value range is between 0x00 and 0xFF (hex) or 0 and 255 (decimal).
95
+ * @see [[apply() ]]
96
96
* @param index the given index
97
+ * @param value the value to replace the existing value at the given index
97
98
*/
98
99
@ JSBracketAccess
99
100
def update (index : Int , value : Int ): Unit = js.native
@@ -222,7 +223,7 @@ class Buffer() extends js.Object {
222
223
* @return the amount of memory allocated for buf in bytes.
223
224
* @example buf.length
224
225
*/
225
- def length : Int = js.native
226
+ override val length : Int = js.native
226
227
227
228
/**
228
229
* Reads a 64-bit double from buf at the specified offset with specified endian format (readDoubleBE()
@@ -866,127 +867,149 @@ object Buffer extends js.Object {
866
867
*/
867
868
var poolSize : Int = js.native
868
869
870
+ @ js.native
871
+ object constants extends js.Object {
872
+
873
+ /**
874
+ * The largest size allowed for a single `Buffer` instance.
875
+ *
876
+ * On 32-bit architectures, this value is `(2^30)-1` (~1GB).
877
+ * On 64-bit architectures, this value is `(2^31)-1` (~2GB).
878
+ *
879
+ * This value is also available as [[Buffer.kMaxLength ]].
880
+ */
881
+ val MAX_LENGTH : Int = js.native
882
+
883
+ /**
884
+ * The largest length allowed for a single `String` instance.
885
+ *
886
+ * Represents the largest `length` that a `String` primitive can have, counted in UTF-16 code units.
887
+ *
888
+ * This value may depend on the JS engine that is being used.
889
+ */
890
+ val MAX_STRING_LENGTH : Int = js.native
891
+ }
892
+
869
893
// ///////////////////////////////////////////////////////////////////////////////
870
894
// Methods
871
895
// ///////////////////////////////////////////////////////////////////////////////
872
896
873
897
/**
874
- * Allocates a new Buffer of size bytes. If fill is undefined, the Buffer will be zero-filled.
875
- * @param size The desired length of the new Buffer
876
- * @param fill A value to pre-fill the new Buffer with. Default: 0
877
- * @param encoding If fill is a string, this is its encoding. Default: 'utf8'
878
- * @return a new [[Buffer ]]
879
- * @example {{{ Buffer.alloc(size[, fill[, encoding]]) }}}
898
+ * Allocates a new `Buffer` of `size` bytes.
899
+ * If `fill` is `undefined`, the `Buffer` will be zero-filled.
900
+ *
901
+ * @param size The desired length of the new `Buffer`
902
+ * @param fill A value to pre-fill the new `Buffer` with. Default: `0`
903
+ * @param encoding If fill` is a string, this is its encoding. Default: `'utf8'`
904
+ *
905
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding ]]
880
906
*/
881
- def alloc (size : Int , fill : Buffer | Int | String = js.native, encoding : String = js.native): Buffer = js.native
907
+ def alloc (size : Int , fill : Uint8Array | Int | String = js.native, encoding : String = js.native): Buffer = js.native
882
908
883
909
/**
884
- * Calling Buffer.alloc(size) can be significantly slower than the alternative Buffer.allocUnsafe(size) but ensures
885
- * that the newly created Buffer instance contents will never contain sensitive data.
886
- * @param size the allocated size.
887
- * @example Buffer.allocUnsafe(size)
910
+ * Allocates a new `Buffer` of `size` bytes.
911
+ * If `size` is larger than [[constants.MAX_LENGTH ]] or smaller than `0`, `ERR_INVALID_OPT_VALUE` is thrown.
912
+ * A zero-length `Buffer` is created if size is `0`.
913
+ *
914
+ * @param size The desired length of the new `Buffer`.
915
+ *
916
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_allocunsafe_size ]]
888
917
*/
889
918
def allocUnsafe (size : Int ): Buffer = js.native
890
919
891
920
/**
892
- * Allocates a new non-zero-filled and non-pooled Buffer of size bytes. The size must be less than or equal to the
893
- * value of require('buffer').kMaxLength (on 64-bit architectures, kMaxLength is {{{ (2^31)-1). }}} Otherwise, a RangeError
894
- * is thrown. A zero-length Buffer will be created if a size less than or equal to 0 is specified.
895
- *
896
- * The underlying memory for Buffer instances created in this way is not initialized. The contents of the newly
897
- * created Buffer are unknown and may contain sensitive data. Use buf.fill(0) to initialize such Buffer instances to zeroes.
921
+ * Allocates a new `Buffer` of `size` bytes.
922
+ * If `size` is larger than [[constants.MAX_LENGTH ]] or smaller than `0`, `ERR_INVALID_OPT_VALUE` is thrown.
923
+ * A zero-length `Buffer` is created if size is `0`.
898
924
*
899
- * When using Buffer.allocUnsafe() to allocate new Buffer instances, allocations under 4KB are, by default, sliced
900
- * from a single pre-allocated Buffer. This allows applications to avoid the garbage collection overhead of creating
901
- * many individually allocated Buffers. This approach improves both performance and memory usage by eliminating the
902
- * need to track and cleanup as many Persistent objects.
925
+ * @param size The desired length of the new `Buffer`.
903
926
*
904
- * However, in the case where a developer may need to retain a small chunk of memory from a pool for an indeterminate
905
- * amount of time, it may be appropriate to create an un-pooled Buffer instance using Buffer.allocUnsafeSlow() then
906
- * copy out the relevant bits.
907
- * @param size the allocated size.
908
- * @example Buffer.allocUnsafeSlow(size)
927
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_allocunsafeslow_size ]]
909
928
*/
910
929
def allocUnsafeSlow (size : Int ): Buffer = js.native
911
930
912
931
/**
913
- * Returns the actual byte length of a string. This is not the same as String.prototype.length since that returns
914
- * the number of characters in a string.
915
- * @param string <String> | <Buffer> | <TypedArray> | <DataView> | <ArrayBuffer>
916
- * @param encoding the optional encoding (default "utf8")
917
- * @example Buffer.byteLength(string[, encoding])
932
+ * Returns the actual byte length of a string.
933
+ * This is not the same as `String.prototype.length` since that returns the number of characters in a string.
934
+ *
935
+ * @param string A value to calculate the length of.
936
+ * @param encoding If string` is a string, this is its encoding. Default: `'utf8'`.
937
+ *
938
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding ]]
918
939
*/
919
- def byteLength (string : js.Any , encoding : String = " utf8" ): Int = js.native
940
+ def byteLength (string : String | TypedArray [_, _] | DataView | ArrayBuffer , encoding : String = " utf8" ): Int =
941
+ js.native
920
942
921
943
/**
922
- * Compares buf1 to buf2 typically for the purpose of sorting arrays of Buffers.
923
- * This is equivalent is calling buf1.compare(buf2).
944
+ * Compares `buf1` to `buf2` typically for the purpose of sorting arrays of `Buffer` instances.
945
+ * This is equivalent to calling `buf1.compare(buf2)`.
946
+ *
947
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_compare_buf1_buf2 ]]
924
948
*/
925
- def compare (buf1 : Buffer , buf2 : Buffer ): Int = js.native
949
+ def compare (buf1 : Uint8Array , buf2 : Uint8Array ): Int = js.native
926
950
927
951
/**
928
- * Returns a new Buffer which is the result of concatenating all the Buffers in the list together.
929
- * If the list has no items, or if the totalLength is 0, then a new zero-length Buffer is returned.
930
- * If totalLength is not provided, it is calculated from the Buffers in the list. This, however, adds an additional
931
- * loop to the function, so it is faster to provide the length explicitly.
932
- * @param list the list of Buffer objects to concat
933
- * @param totalLength the optional total length
934
- * @example Buffer.concat(list[, totalLength])
935
- */
936
- def concat (list : js.Array [Buffer ], totalLength : Int ): Buffer = js.native
952
+ * Returns a new `Buffer` which is the result of concatenating all the `Buffer`s in the `list` together.
937
953
938
- /**
939
- * Returns a new Buffer which is the result of concatenating all the Buffers in the list together.
940
- * If the list has no items, or if the totalLength is 0, then a new zero-length Buffer is returned.
941
- * If totalLength is not provided, it is calculated from the Buffers in the list. This, however, adds an additional
942
- * loop to the function, so it is faster to provide the length explicitly.
943
- * @param list the list of Buffer objects to concat
944
- * @example Buffer.concat(list[, totalLength])
954
+ * @param list List of [[Buffer ]] or [[Uint8Array ]] instances to concat.
955
+ * @param totalLength Total length of the `Buffer` instances in `list` when concatenated.
956
+ *
957
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_concat_list_totallength ]]
958
+ *
945
959
*/
946
- def concat (list : js.Array [Buffer ]): Buffer = js.native
960
+ def concat (list : js.Array [Buffer ] | js. Array [ Uint8Array ], totalLength : Int = js.native ): Buffer = js.native
947
961
948
962
/**
949
963
* When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer
950
964
* will share the same allocated memory as the TypedArray.
951
- * @example {{{ Buffer.from(arrayBuffer[, byteOffset[, length]]) }}}
952
- **/
953
- def from (arrayBuffer : ArrayBuffer , byteOffset : Int , length : Int ): Buffer = js.native
965
+ *
966
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length ]]
967
+ */
968
+ def from (arrayBuffer : ArrayBuffer , byteOffset : Int = js.native, length : Int = js.native): Buffer = js.native
954
969
955
970
/**
956
- * When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer
957
- * will share the same allocated memory as the TypedArray.
958
- * @example {{{ Buffer.from(arrayBuffer[, byteOffset[, length]]) }}}
959
- **/
960
- def from (arrayBuffer : ArrayBuffer , byteOffset : Int ): Buffer = js.native
971
+ * Copies the passed `buffer` data onto a new `Buffer` instance.
972
+ *
973
+ * @param buffer An existing [[Buffer ]] or [[Uint8Array ]] from which to copy data.
974
+ *
975
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_buffer ]]
976
+ */
977
+ def from (buffer : Uint8Array ): Buffer = js.native
961
978
962
979
/**
963
- * When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer
964
- * will share the same allocated memory as the TypedArray.
965
- * @example {{{ Buffer.from(arrayBuffer[, byteOffset[, length]]) }}}
966
- ** /
967
- def from (arrayBuffer : ArrayBuffer ): Buffer = js.native
980
+ * Allocates a new `Buffer` using an `array` of octets.
981
+ *
982
+ * @see [[ https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array ]]
983
+ */
984
+ def from (array : js. Array [ Int ] ): Buffer = js.native
968
985
969
986
/**
970
- * Allocates a new Buffer using an array of octets.
971
- * @example Buffer.from(array)
987
+ * Creates a new `Buffer` containing `string`.
988
+ * The `encoding` parameter identifies the character encoding of `string`.
989
+ * @param str A string to encode.
990
+ * @param encoding The encoding of string. Default: 'utf8'.
991
+ *
992
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding ]]
972
993
*/
973
- def from (array : js. Array [ Int ] ): Buffer = js.native
994
+ def from (str : String , encoding : String ): Buffer = js.native
974
995
975
996
/**
976
- * Creates a new Buffer containing the given JavaScript string str. If provided, the encoding parameter identifies
977
- * the strings character encoding.
978
- * @param str the source string
979
- * @param encoding the given encoding
980
- * @return a new Buffer
997
+ * Creates a new `Buffer` containing `string`.
998
+ * UTF-8 encoding is used.
999
+ *
1000
+ * @param str A string to encode.
1001
+ *
1002
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding ]]
981
1003
*/
982
- def from (str : String , encoding : String = js.native ): Buffer = js.native
1004
+ def from (str : String ): Buffer = js.native
983
1005
984
1006
/**
985
- * Returns true if obj is a Buffer, false otherwise.
1007
+ * Returns ` true` if ` obj` is a ` Buffer`, ` false` otherwise.
986
1008
* @param obj the given object
987
- * @return true if obj is a Buffer, false otherwise.
1009
+ *
1010
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_isbuffer_obj ]]
988
1011
*/
989
- def isBuffer (obj : js.Any ): Boolean = js.native
1012
+ def isBuffer (obj : js.Object ): Boolean = js.native
990
1013
991
1014
/**
992
1015
* Returns true if encoding contains a supported character encoding, or false otherwise.
0 commit comments