Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 0bc2e72

Browse files
author
exoego
committed
wip Buffer
1 parent 71a2405 commit 0bc2e72

File tree

2 files changed

+184
-120
lines changed

2 files changed

+184
-120
lines changed

app/current/src/main/scala/io/scalajs/nodejs/buffer/Buffer.scala

Lines changed: 143 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,97 @@ import io.scalajs.collection.Iterator
44

55
import scala.scalajs.js
66
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}
88
import scala.scalajs.js.|
99

1010
/**
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]]
2312
*/
2413
@js.native
2514
@JSImport("buffer", "Buffer")
26-
class Buffer() extends js.Object {
15+
class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -1) {
2716

2817
/////////////////////////////////////////////////////////////////////////////////
2918
// Constructors
3019
/////////////////////////////////////////////////////////////////////////////////
3120

3221
/**
33-
* @example new Buffer(size)
22+
* Use [[Buffer.alloc()]] instead.
23+
*
24+
* @see [[https://nodejs.org/api/buffer.html#buffer_new_buffer_size]]
3425
*/
3526
@inline
27+
@deprecated("Use Buffer.alloc(size) instead.", since = "Node.js v6.0.0")
3628
def this(size: Int) = this()
3729

3830
/**
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]]
4734
*/
4835
@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()
5138

5239
/**
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]]
5443
*/
5544
@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()
5847

5948
/**
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]]
6151
*/
6252
@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()
6555

6656
/**
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]]
6860
*/
6961
@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()
7264

7365
/**
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]]
7569
*/
7670
@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()
7973

8074
/////////////////////////////////////////////////////////////////////////////////
8175
// Accessors and Mutators
8276
/////////////////////////////////////////////////////////////////////////////////
8377

8478
/**
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+
*
8788
* @param index the given index
8889
* @return the value at the given index
8990
*/
9091
@JSBracketAccess
91-
def apply(index: Int): Int = js.native
92+
override def apply(index: Int): Short = js.native
9293

9394
/**
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()]]
9696
* @param index the given index
97+
* @param value the value to replace the existing value at the given index
9798
*/
9899
@JSBracketAccess
99100
def update(index: Int, value: Int): Unit = js.native
@@ -222,7 +223,7 @@ class Buffer() extends js.Object {
222223
* @return the amount of memory allocated for buf in bytes.
223224
* @example buf.length
224225
*/
225-
def length: Int = js.native
226+
override val length: Int = js.native
226227

227228
/**
228229
* 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 {
866867
*/
867868
var poolSize: Int = js.native
868869

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+
869893
/////////////////////////////////////////////////////////////////////////////////
870894
// Methods
871895
/////////////////////////////////////////////////////////////////////////////////
872896

873897
/**
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]]
880906
*/
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
882908

883909
/**
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]]
888917
*/
889918
def allocUnsafe(size: Int): Buffer = js.native
890919

891920
/**
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`.
898924
*
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`.
903926
*
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]]
909928
*/
910929
def allocUnsafeSlow(size: Int): Buffer = js.native
911930

912931
/**
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]]
918939
*/
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
920942

921943
/**
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]]
924948
*/
925-
def compare(buf1: Buffer, buf2: Buffer): Int = js.native
949+
def compare(buf1: Uint8Array, buf2: Uint8Array): Int = js.native
926950

927951
/**
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.
937953
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+
*
945959
*/
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
947961

948962
/**
949963
* When passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer
950964
* 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
954969

955970
/**
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
961978

962979
/**
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
968985

969986
/**
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]]
972993
*/
973-
def from(array: js.Array[Int]): Buffer = js.native
994+
def from(str: String, encoding: String): Buffer = js.native
974995

975996
/**
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]]
9811003
*/
982-
def from(str: String, encoding: String = js.native): Buffer = js.native
1004+
def from(str: String): Buffer = js.native
9831005

9841006
/**
985-
* Returns true if obj is a Buffer, false otherwise.
1007+
* Returns `true` if `obj` is a `Buffer`, `false` otherwise.
9861008
* @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]]
9881011
*/
989-
def isBuffer(obj: js.Any): Boolean = js.native
1012+
def isBuffer(obj: js.Object): Boolean = js.native
9901013

9911014
/**
9921015
* Returns true if encoding contains a supported character encoding, or false otherwise.

0 commit comments

Comments
 (0)