@@ -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()
@@ -845,21 +846,6 @@ object Buffer extends js.Object {
845
846
// Properties
846
847
// ///////////////////////////////////////////////////////////////////////////////
847
848
848
- /**
849
- * Returns the maximum number of bytes that will be returned when buf.inspect() is called.
850
- * This can be overridden by user modules. See util.inspect() for more details on buf.inspect() behavior.
851
- *
852
- * Note that this is a property on the buffer module returned by require('buffer'), not on the
853
- * Buffer global or a Buffer instance.
854
- */
855
- val INSPECT_MAX_BYTES : Int = js.native
856
-
857
- /**
858
- * On 32-bit architectures, this value is (2^30)-1 (~1GB). On 64-bit architectures, this value is (2^31)-1 (~2GB).F
859
- * Note that this is a property on the buffer module returned by require('buffer'), not on the Buffer global or a Buffer instance.
860
- */
861
- val kMaxLength : Int = js.native
862
-
863
849
/**
864
850
* This is the number of bytes used to determine the size of pre-allocated, internal Buffer instances used for pooling.
865
851
* This value may be modified.
@@ -871,143 +857,127 @@ object Buffer extends js.Object {
871
857
// ///////////////////////////////////////////////////////////////////////////////
872
858
873
859
/**
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]]) }}}
860
+ * Allocates a new `Buffer` of `size` bytes.
861
+ * If `fill` is `undefined`, the `Buffer` will be zero-filled.
862
+ *
863
+ * @param size The desired length of the new `Buffer`
864
+ * @param fill A value to pre-fill the new `Buffer` with. Default: `0`
865
+ * @param encoding If fill` is a string, this is its encoding. Default: `'utf8'`
866
+ *
867
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding ]]
880
868
*/
881
- def alloc (size : Int , fill : Buffer | Int | String = js.native, encoding : String = js.native): Buffer = js.native
869
+ def alloc (size : Int , fill : Uint8Array | Int | String = js.native, encoding : String = js.native): Buffer = js.native
882
870
883
871
/**
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)
872
+ * Allocates a new `Buffer` of `size` bytes.
873
+ * If `size` is larger than [[constants.MAX_LENGTH ]] or smaller than `0`, `ERR_INVALID_OPT_VALUE` is thrown.
874
+ * A zero-length `Buffer` is created if size is `0`.
875
+ *
876
+ * @param size The desired length of the new `Buffer`.
877
+ *
878
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_allocunsafe_size ]]
888
879
*/
889
880
def allocUnsafe (size : Int ): Buffer = js.native
890
881
891
882
/**
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.
883
+ * Allocates a new `Buffer` of `size` bytes.
884
+ * If `size` is larger than [[constants.MAX_LENGTH ]] or smaller than `0`, `ERR_INVALID_OPT_VALUE` is thrown.
885
+ * A zero-length `Buffer` is created if size is `0`.
898
886
*
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.
887
+ * @param size The desired length of the new `Buffer`.
903
888
*
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)
889
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_allocunsafeslow_size ]]
909
890
*/
910
891
def allocUnsafeSlow (size : Int ): Buffer = js.native
911
892
912
893
/**
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])
894
+ * Returns the actual byte length of a string.
895
+ * This is not the same as `String.prototype.length` since that returns the number of characters in a string.
896
+ *
897
+ * @param string A value to calculate the length of.
898
+ * @param encoding If string` is a string, this is its encoding. Default: `'utf8'`.
899
+ *
900
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding ]]
918
901
*/
919
- def byteLength (string : js.Any , encoding : String = " utf8" ): Int = js.native
902
+ def byteLength (string : String | TypedArray [_, _] | DataView | ArrayBuffer , encoding : String = " utf8" ): Int =
903
+ js.native
920
904
921
905
/**
922
- * Compares buf1 to buf2 typically for the purpose of sorting arrays of Buffers.
923
- * This is equivalent is calling buf1.compare(buf2).
906
+ * Compares `buf1` to `buf2` typically for the purpose of sorting arrays of `Buffer` instances.
907
+ * This is equivalent to calling `buf1.compare(buf2)`.
908
+ *
909
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_compare_buf1_buf2 ]]
924
910
*/
925
- def compare (buf1 : Buffer , buf2 : Buffer ): Int = js.native
911
+ def compare (buf1 : Uint8Array , buf2 : Uint8Array ): Int = js.native
926
912
927
913
/**
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
914
+ * Returns a new `Buffer` which is the result of concatenating all the `Buffer`s in the `list` together.
937
915
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])
916
+ * @param list List of [[Buffer ]] or [[Uint8Array ]] instances to concat.
917
+ * @param totalLength Total length of the `Buffer` instances in `list` when concatenated.
918
+ *
919
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_concat_list_totallength ]]
920
+ *
945
921
*/
946
- def concat (list : js.Array [Buffer ]): Buffer = js.native
947
-
948
- /**
949
- * When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer
950
- * 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
922
+ def concat (list : js.Array [Buffer ] | js.Array [Uint8Array ], totalLength : Int = js.native): Buffer = js.native
954
923
955
924
/**
956
925
* When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer
957
926
* 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
927
+ *
928
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length ]]
929
+ */
930
+ def from (arrayBuffer : ArrayBuffer , byteOffset : Int = js.native, length : Int = js.native): Buffer = js.native
961
931
962
932
/**
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
933
+ * Copies the passed `buffer` data onto a new `Buffer` instance.
934
+ *
935
+ * @param buffer An existing [[Buffer ]] or [[Uint8Array ]] from which to copy data.
936
+ *
937
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_buffer ]]
938
+ */
939
+ def from (buffer : Uint8Array ): Buffer = js.native
968
940
969
941
/**
970
- * Allocates a new Buffer using an array of octets.
971
- * @example Buffer.from(array)
942
+ * Allocates a new `Buffer` using an `array` of octets.
943
+ *
944
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array ]]
972
945
*/
973
946
def from (array : js.Array [Int ]): Buffer = js.native
974
947
975
948
/**
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
949
+ * Creates a new `Buffer` containing `string`.
950
+ * The `encoding` parameter identifies the character encoding of `string`.
951
+ * @param str A string to encode.
952
+ * @param encoding The encoding of string. Default: 'utf8'.
953
+ *
954
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding ]]
981
955
*/
982
- def from (str : String , encoding : String = js.native ): Buffer = js.native
956
+ def from (str : String , encoding : String ): Buffer = js.native
983
957
984
958
/**
985
- * Returns true if obj is a Buffer, false otherwise.
986
- * @param obj the given object
987
- * @return true if obj is a Buffer, false otherwise.
959
+ * Creates a new `Buffer` containing `string`.
960
+ * UTF-8 encoding is used.
961
+ *
962
+ * @param str A string to encode.
963
+ *
964
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding ]]
988
965
*/
989
- def isBuffer ( obj : js. Any ): Boolean = js.native
966
+ def from ( str : String ): Buffer = js.native
990
967
991
968
/**
992
- * Returns true if encoding contains a supported character encoding, or false otherwise.
993
- * @param encoding A character encoding name to check
994
- * @return true if encoding contains a supported character encoding, or false otherwise.
969
+ * Returns `true` if `obj` is a `Buffer`, `false` otherwise.
970
+ * @param obj the given object
971
+ *
972
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_isbuffer_obj ]]
995
973
*/
996
- def isEncoding ( encoding : String ): Boolean = js.native
974
+ def isBuffer ( obj : js. Object ): Boolean = js.native
997
975
998
976
/**
999
- * Re-encodes the given Buffer instance from one character encoding to another. Returns a new Buffer instance .
977
+ * Returns true if `encoding` contains a supported character encoding, or false otherwise .
1000
978
*
1001
- * Throws if the fromEnc or toEnc specify invalid character encodings or if conversion from fromEnc to toEnc
1002
- * is not permitted.
1003
- *
1004
- * The transcoding process will use substitution characters if a given byte sequence cannot be adequately
1005
- * represented in the target encoding.
1006
- * @param source A Buffer instance
1007
- * @param fromEnc The current encoding
1008
- * @param toEnc To target encoding
1009
- * @return a new Buffer instance.
979
+ * @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_isencoding_encoding ]]
1010
980
*/
1011
- def transcode ( source : Buffer , fromEnc : String , toEnc : String ): Buffer = js.native
981
+ def isEncoding ( encoding : String ): Boolean = js.native
1012
982
1013
983
}
0 commit comments