mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-14 17:45:48 +00:00
feat: remove Buffer support and bundle polyfill (#2526)
- Removes need for bundlers to polyfill the `Buffer` class. `UInt8Array` are used instead which are native to the browser and node. - Reduces bundle size 7.1kb gzipped and eliminates 4 runtime dependencies: `base-x`, `base64-js`, `buffer`, and `ieee754`. BREAKING CHANGE: All methods that previously took a `Buffer` now accept a `UInt8Array`. --------- Co-authored-by: Jackson Mills <jmills@ripple.com>
This commit is contained in:
@@ -5,6 +5,7 @@ const { encode, decode } = require('../src')
|
||||
const { makeParser, BytesList, BinarySerializer } = binary
|
||||
const { coreTypes } = require('../src/types')
|
||||
const { UInt8, UInt16, UInt32, UInt64, STObject } = coreTypes
|
||||
|
||||
const deliverMinTx = require('./fixtures/delivermin-tx.json')
|
||||
const deliverMinTxBinary = require('./fixtures/delivermin-tx-binary.json')
|
||||
const SignerListSet = {
|
||||
@@ -105,12 +106,12 @@ const NegativeUNL = require('./fixtures/negative-unl.json')
|
||||
|
||||
function bytesListTest() {
|
||||
const list = new BytesList()
|
||||
.put(Buffer.from([0]))
|
||||
.put(Buffer.from([2, 3]))
|
||||
.put(Buffer.from([4, 5]))
|
||||
it('is an Array<Buffer>', function () {
|
||||
.put(Uint8Array.from([0]))
|
||||
.put(Uint8Array.from([2, 3]))
|
||||
.put(Uint8Array.from([4, 5]))
|
||||
it('is an Array<Uint8Array>', function () {
|
||||
expect(Array.isArray(list.bytesArray)).toBe(true)
|
||||
expect(list.bytesArray[0] instanceof Buffer).toBe(true)
|
||||
expect(list.bytesArray[0] instanceof Uint8Array).toBe(true)
|
||||
})
|
||||
it('keeps track of the length itself', function () {
|
||||
expect(list.getLength()).toBe(5)
|
||||
@@ -118,7 +119,7 @@ function bytesListTest() {
|
||||
it('can join all arrays into one via toBytes', function () {
|
||||
const joined = list.toBytes()
|
||||
expect(joined.length).toEqual(5)
|
||||
expect(joined).toEqual(Buffer.from([0, 2, 3, 4, 5]))
|
||||
expect(joined).toEqual(Uint8Array.from([0, 2, 3, 4, 5]))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -149,10 +150,18 @@ function check(type, n, expected) {
|
||||
return
|
||||
}
|
||||
serializer.writeType(type, n)
|
||||
expect(bl.toBytes()).toEqual(Buffer.from(expected))
|
||||
expect(bl.toBytes()).toEqual(Uint8Array.from(expected))
|
||||
})
|
||||
}
|
||||
|
||||
it(`Uint16 serializes 5 as 0,5`, function () {
|
||||
const bl = new BytesList()
|
||||
const serializer = new BinarySerializer(bl)
|
||||
const expected = [0, 5]
|
||||
serializer.writeType(UInt16, 5)
|
||||
expect(bl.toBytes()).toEqual(Uint8Array.from(expected))
|
||||
})
|
||||
|
||||
check(UInt8, 5, [5])
|
||||
check(UInt16, 5, [0, 5])
|
||||
check(UInt32, 5, [0, 0, 0, 5])
|
||||
|
||||
Reference in New Issue
Block a user