@dao-xyz/borsh
Advanced tools
Comparing version 5.1.2 to 5.1.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.checkInt = exports.readUInt16LE = exports.readUInt32LE = exports.readUIntLE = exports.readBigUInt64LE = exports.writeBigUint64Le = exports.writeUInt16LE = exports.writeUInt32LE = exports.writeBufferLEBigInt = exports.toBigIntLE = void 0; | ||
function arrayToHex(arr) { | ||
return [...new Uint8Array(arr)] | ||
function arrayToHex(arr, reverse = false) { | ||
return [...(reverse ? new Uint8Array(arr).reverse() : new Uint8Array(arr))] | ||
.map(b => b.toString(16).padStart(2, "0")) | ||
@@ -10,4 +10,3 @@ .join(""); | ||
function toBigIntLE(buf) { | ||
const reversed = buf.reverse(); | ||
const hex = arrayToHex(reversed); | ||
const hex = arrayToHex(buf, true); | ||
if (hex.length === 0) { | ||
@@ -92,4 +91,3 @@ return BigInt(0); | ||
function readUIntLE(buf, offset, width) { | ||
const reversed = buf.slice(offset, offset + width).reverse(); | ||
const hex = arrayToHex(reversed); | ||
const hex = arrayToHex(buf.subarray(offset, offset + width), true); | ||
if (hex.length === 0) { | ||
@@ -96,0 +94,0 @@ return BigInt(0); |
@@ -1,3 +0,3 @@ | ||
function arrayToHex(arr) { | ||
return [...new Uint8Array(arr)] | ||
function arrayToHex(arr, reverse = false) { | ||
return [...(reverse ? new Uint8Array(arr).reverse() : new Uint8Array(arr))] | ||
.map(b => b.toString(16).padStart(2, "0")) | ||
@@ -7,4 +7,3 @@ .join(""); | ||
export function toBigIntLE(buf) { | ||
const reversed = buf.reverse(); | ||
const hex = arrayToHex(reversed); | ||
const hex = arrayToHex(buf, true); | ||
if (hex.length === 0) { | ||
@@ -83,4 +82,3 @@ return BigInt(0); | ||
export function readUIntLE(buf, offset, width) { | ||
const reversed = buf.slice(offset, offset + width).reverse(); | ||
const hex = arrayToHex(reversed); | ||
const hex = arrayToHex(buf.subarray(offset, offset + width), true); | ||
if (hex.length === 0) { | ||
@@ -87,0 +85,0 @@ return BigInt(0); |
{ | ||
"name": "@dao-xyz/borsh", | ||
"version": "5.1.2", | ||
"version": "5.1.3", | ||
"readme": "README.md", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README", |
@@ -254,2 +254,42 @@ import { BinaryReader, BinaryWriter } from "../binary.js"; | ||
test("byte array should deserialize zero-copy from Uint8array", () => { | ||
class TestStruct { | ||
@field({ type: fixedArray("u8", 3) }) | ||
public a: Uint8Array | number[]; | ||
constructor(properties?: { a: number[] }) { | ||
if (properties) { | ||
this.a = properties.a; | ||
} | ||
} | ||
} | ||
validate(TestStruct); | ||
const buf = new Uint8Array(serialize(new TestStruct({ a: [1, 2, 3] }))); | ||
expect(new Uint8Array(buf)).toEqual(new Uint8Array([1, 2, 3])); | ||
const deserialized = deserialize(buf, TestStruct); | ||
deserialized.a[0] = 123; | ||
expect(buf[0]).toEqual(123); | ||
}); | ||
test("byte array should deserialize zero-copy from Buffer", () => { | ||
class TestStruct { | ||
@field({ type: fixedArray("u8", 3) }) | ||
public a: Uint8Array | number[]; | ||
constructor(properties?: { a: number[] }) { | ||
if (properties) { | ||
this.a = properties.a; | ||
} | ||
} | ||
} | ||
validate(TestStruct); | ||
const buf = serialize(new TestStruct({ a: [1, 2, 3] })); | ||
expect(new Uint8Array(buf)).toEqual(new Uint8Array([1, 2, 3])); | ||
const deserialized = deserialize(buf, TestStruct); | ||
deserialized.a[0] = 123; | ||
expect(buf[0]).toEqual(123); | ||
}); | ||
test("fixed array wrong length serialize", () => { | ||
@@ -544,9 +584,35 @@ class TestStruct { | ||
const buf = serialize(instance); | ||
expect(new Uint8Array(buf)).toEqual( | ||
new Uint8Array([123, ...new Array(31).fill(0)]) | ||
); | ||
const serializedExpected = new Uint8Array([123, ...new Array(31).fill(0)]); | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(n); | ||
// check that the original array has not been modified | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
}); | ||
test("u256 with Uin8array", () => { | ||
class Struct { | ||
@field({ type: "u256" }) | ||
public a: bigint; | ||
constructor(a: bigint) { | ||
this.a = a; | ||
} | ||
} | ||
const n = BigInt(123); | ||
const instance = new Struct(n); | ||
const buf = new Uint8Array(serialize(instance)); | ||
const serializedExpected = new Uint8Array([123, ...new Array(31).fill(0)]); | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(n); | ||
// check that the original array has not been modified | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
}); | ||
test("u512 is le", () => { | ||
@@ -563,9 +629,31 @@ class Struct { | ||
const buf = serialize(instance); | ||
expect(new Uint8Array(buf)).toEqual( | ||
new Uint8Array([3, ...new Array(63).fill(0)]) | ||
); | ||
const serializedExpected = new Uint8Array([3, ...new Array(63).fill(0)]); | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(BigInt(3)); | ||
// check that the original array has not been modified | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
}); | ||
test("u512 with 8int8array", () => { | ||
class Struct { | ||
@field({ type: "u512" }) | ||
public a: bigint; | ||
constructor(a: bigint) { | ||
this.a = a; | ||
} | ||
} | ||
const instance = new Struct(BigInt(3)); | ||
const buf = new Uint8Array(serialize(instance)); | ||
const serializedExpected = new Uint8Array([3, ...new Array(63).fill(0)]); | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(BigInt(3)); | ||
// check that the original array has not been modified | ||
expect(new Uint8Array(buf)).toEqual(serializedExpected); | ||
}); | ||
test("u512 max", () => { | ||
@@ -572,0 +660,0 @@ class Struct { |
@@ -1,3 +0,3 @@ | ||
function arrayToHex(arr: Uint8Array): string { | ||
return [...new Uint8Array(arr)] | ||
function arrayToHex(arr: Uint8Array, reverse: boolean = false): string { | ||
return [...(reverse ? new Uint8Array(arr).reverse() : new Uint8Array(arr))] | ||
.map(b => b.toString(16).padStart(2, "0")) | ||
@@ -8,4 +8,3 @@ .join(""); | ||
export function toBigIntLE(buf: Uint8Array): bigint { | ||
const reversed = buf.reverse(); | ||
const hex = arrayToHex(reversed); | ||
const hex = arrayToHex(buf, true); | ||
if (hex.length === 0) { | ||
@@ -95,4 +94,3 @@ return BigInt(0); | ||
export function readUIntLE(buf: Uint8Array, offset: number, width: number): bigint { | ||
const reversed = buf.slice(offset, offset + width).reverse(); | ||
const hex = arrayToHex(reversed); | ||
const hex = arrayToHex(buf.subarray(offset, offset + width), true); | ||
if (hex.length === 0) { | ||
@@ -99,0 +97,0 @@ return BigInt(0); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
337575
6286