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

Commit ae9ef4a

Browse files
author
exoego
committed
Add BigInt-related methods
1 parent 50fedce commit ae9ef4a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.scalajs.nodejs.buffer
22

3+
import com.thoughtworks.enableIf
34
import io.scalajs.collection.Iterator
45

56
import scala.scalajs.js
@@ -841,11 +842,36 @@ class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -
841842
*/
842843
def writeUIntLE(value: Int, offset: Int, byteLength: Int, noAssert: Boolean = js.native): Int = js.native
843844

845+
/**
846+
* @see https://nodejs.org/api/buffer.html#buffer_buf_readbiguint64be_offset
847+
*/
848+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
849+
def readBigInt64BE(offset: Int = js.native): Buffer.UnsafeBigInt = js.native
850+
851+
/**
852+
* @see https://nodejs.org/api/buffer.html#buffer_buf_readbiguint64le_offset
853+
*/
854+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
855+
def readBigUInt64LE(offset: Int = js.native): Buffer.UnsafeBigInt = js.native
856+
857+
/**
858+
* @see https://nodejs.org/api/buffer.html#buffer_buf_writebigint64be_value_offset
859+
*/
860+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
861+
def writeBigInt64BE(value: Buffer.UnsafeBigInt, offset: Int = js.native): Int = js.native
862+
863+
/**
864+
* @see https://nodejs.org/api/buffer.html#buffer_buf_writebigint64le_value_offset
865+
*/
866+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
867+
def writeBigInt64LE(value: Buffer.UnsafeBigInt, offset: Int = js.native): Int = js.native
844868
}
845869

846870
@js.native
847871
@JSGlobal
848872
object Buffer extends js.Object {
873+
// TODO: Node.js added BigInt-related things (e.g. readBigInt64BE). Should support when Scala.js support it
874+
type UnsafeBigInt = js.Dynamic
849875

850876
/////////////////////////////////////////////////////////////////////////////////
851877
// Properties
@@ -984,5 +1010,4 @@ object Buffer extends js.Object {
9841010
* @see [[https://nodejs.org/api/buffer.html#buffer_class_method_buffer_isencoding_encoding]]
9851011
*/
9861012
def isEncoding(encoding: String): Boolean = js.native
987-
9881013
}

app/current/src/test/scala/nodejs/buffer/BufferTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ class BufferTest extends FunSpec {
119119
assert(Buffer.isEncoding("utf8"))
120120
assert(Buffer.isEncoding("UTF-8"))
121121
}
122+
123+
it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") {
124+
val buf = Buffer.allocUnsafe(8)
125+
val v = js.Dynamic.global.BigInt("0x0102030405060708")
126+
buf.writeBigInt64BE(v, 0);
127+
assert(buf === Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8)))
128+
}
122129
}
123130

124131
describe("module members") {

0 commit comments

Comments
 (0)