New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dao-xyz/borsh

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dao-xyz/borsh - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

lib/cjs/bigint.d.ts

7

lib/cjs/binary.d.ts

@@ -1,4 +0,3 @@

/// <reference types="node" />
export declare class BinaryWriter {
buf: Buffer;
buf: DataView;
length: number;

@@ -22,5 +21,5 @@ constructor();

export declare class BinaryReader {
buf: Buffer;
buf: DataView;
offset: number;
constructor(buf: Buffer);
constructor(buf: Uint8Array);
readBool(): boolean;

@@ -27,0 +26,0 @@ readU8(): number;

@@ -34,3 +34,3 @@ "use strict";

const INITIAL_LENGTH = 1024;
const bigint_buffer_1 = require("bigint-buffer");
const bigint_1 = require("./bigint");
const error_1 = require("./error");

@@ -40,11 +40,16 @@ const encoding = __importStar(require("text-encoding-utf-8"));

const textDecoder = new ResolvedTextDecoder("utf-8", { fatal: true });
const ResolvedTextEncoder = typeof TextEncoder !== "function" ? encoding.TextEncoder : TextEncoder;
const textEncoder = new ResolvedTextEncoder("utf-8");
/// Binary encoder.
class BinaryWriter {
constructor() {
this.buf = Buffer.alloc(INITIAL_LENGTH);
this.buf = new DataView(new ArrayBuffer(INITIAL_LENGTH));
this.length = 0;
}
maybeResize() {
if (this.buf.length < 16 + this.length) {
this.buf = Buffer.concat([this.buf, Buffer.alloc(INITIAL_LENGTH)]);
if (this.buf.byteLength < 16 + this.length) {
const newArr = new Uint8Array(this.buf.byteLength + INITIAL_LENGTH);
newArr.set(new Uint8Array(this.buf.buffer));
newArr.set(new Uint8Array(INITIAL_LENGTH), this.buf.byteLength);
this.buf = new DataView(newArr.buffer);
}

@@ -54,3 +59,3 @@ }

this.maybeResize();
this.buf.writeUInt8(value ? 1 : 0, this.length);
this.buf.setUint8(value ? 1 : 0, this.length);
this.length += 1;

@@ -60,3 +65,3 @@ }

this.maybeResize();
this.buf.writeUInt8(value, this.length);
this.buf.setUint8(this.length, value);
this.length += 1;

@@ -66,3 +71,3 @@ }

this.maybeResize();
this.buf.writeUInt16LE(value, this.length);
this.buf.setUint16(this.length, value, true);
this.length += 2;

@@ -72,3 +77,3 @@ }

this.maybeResize();
this.buf.writeUInt32LE(value, this.length);
this.buf.setUint32(this.length, value, true);
this.length += 4;

@@ -78,28 +83,27 @@ }

this.maybeResize();
this.writeBuffer((0, bigint_buffer_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 8));
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 8));
}
writeU128(value) {
this.maybeResize();
this.writeBuffer((0, bigint_buffer_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 16));
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 16));
}
writeU256(value) {
this.maybeResize();
this.writeBuffer((0, bigint_buffer_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 32));
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 32));
}
writeU512(value) {
this.maybeResize();
this.writeBuffer((0, bigint_buffer_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 64));
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 64));
}
writeBuffer(buffer) {
// Buffer.from is needed as this.buf.subarray can return plain Uint8Array in browser
this.buf = Buffer.concat([
Buffer.from(this.buf.subarray(0, this.length)),
buffer,
Buffer.alloc(INITIAL_LENGTH),
]);
const newBuf = new Uint8Array(this.length + buffer.length + INITIAL_LENGTH);
newBuf.set(new Uint8Array(this.buf.buffer.slice(0, this.length)));
newBuf.set(buffer, this.length);
newBuf.set(new Uint8Array(INITIAL_LENGTH), this.length + buffer.length);
this.length += buffer.length;
this.buf = new DataView(newBuf.buffer);
}
writeString(str) {
this.maybeResize();
const b = Buffer.from(str, "utf8");
const b = textEncoder.encode(str);
this.writeU32(b.length);

@@ -109,3 +113,3 @@ this.writeBuffer(b);

writeFixedArray(array) {
this.writeBuffer(Buffer.from(array));
this.writeBuffer(array);
}

@@ -121,3 +125,3 @@ writeArray(array, fn) {

toArray() {
return this.buf.subarray(0, this.length);
return new Uint8Array(this.buf.buffer.slice(0, this.length));
}

@@ -145,7 +149,7 @@ }

constructor(buf) {
this.buf = buf;
this.buf = new DataView(buf.buffer);
this.offset = 0;
}
readBool() {
const value = this.buf.readUInt8(this.offset);
const value = this.buf.getUint8(this.offset);
this.offset += 1;

@@ -155,3 +159,3 @@ return value ? true : false;

readU8() {
const value = this.buf.readUInt8(this.offset);
const value = this.buf.getUint8(this.offset);
this.offset += 1;

@@ -161,3 +165,3 @@ return value;

readU16() {
const value = this.buf.readUInt16LE(this.offset);
const value = this.buf.getUint16(this.offset, true);
this.offset += 2;

@@ -167,3 +171,3 @@ return value;

readU32() {
const value = this.buf.readUInt32LE(this.offset);
const value = this.buf.getUint32(this.offset, true);
this.offset += 4;

@@ -174,23 +178,23 @@ return value;

const buf = this.readBuffer(8);
return (0, bigint_buffer_1.toBigIntLE)(buf);
return (0, bigint_1.toBigIntLE)(buf);
}
readU128() {
const buf = this.readBuffer(16);
return (0, bigint_buffer_1.toBigIntLE)(buf);
return (0, bigint_1.toBigIntLE)(buf);
}
readU256() {
const buf = this.readBuffer(32);
return (0, bigint_buffer_1.toBigIntLE)(buf);
return (0, bigint_1.toBigIntLE)(buf);
}
readU512() {
const buf = this.readBuffer(64);
return (0, bigint_buffer_1.toBigIntLE)(buf);
return (0, bigint_1.toBigIntLE)(buf);
}
readBuffer(len) {
if (this.offset + len > this.buf.length) {
if (this.offset + len > this.buf.byteLength) {
throw new error_1.BorshError(`Expected buffer length ${len} isn't within bounds`);
}
const result = this.buf.slice(this.offset, this.offset + len);
const result = this.buf.buffer.slice(this.offset, this.offset + len);
this.offset += len;
return result;
return new Uint8Array(result);
}

@@ -197,0 +201,0 @@ readString() {

@@ -1,2 +0,1 @@

/// <reference types="node" />
import { StructKind, SimpleField, CustomField, Constructor } from "./types";

@@ -18,3 +17,3 @@ import { BinaryWriter, BinaryReader } from "./binary";

*/
export declare function deserialize<T>(buffer: Buffer, classType: {
export declare function deserialize<T>(buffer: Uint8Array, classType: {
new (args: any): T;

@@ -24,3 +23,3 @@ }, unchecked?: boolean, Reader?: typeof BinaryReader): T;

new (args: any): T;
}, buffer: Buffer, Reader?: typeof BinaryReader): T;
}, buffer: Uint8Array, Reader?: typeof BinaryReader): T;
export declare const getSchema: (ctor: Function) => StructKind;

@@ -27,0 +26,0 @@ export declare const getSchemasBottomUp: (ctor: Function) => {

@@ -65,3 +65,3 @@ "use strict";

if (!checkClazzesCompatible(value.constructor, fieldType)) {
throw new error_1.BorshError(`Field value of field ${fieldName} does not instance of expected Class ${getSuperMostClass(fieldType)}. Got: ${value.constructor.name}`);
throw new error_1.BorshError(`Field value of field ${fieldName} is not instance of expected Class ${getSuperMostClass(fieldType)?.name}. Got: ${value.constructor.name}`);
}

@@ -68,0 +68,0 @@ serializeStruct(value, writer);

@@ -1,4 +0,3 @@

/// <reference types="node" />
export declare class BinaryWriter {
buf: Buffer;
buf: DataView;
length: number;

@@ -22,5 +21,5 @@ constructor();

export declare class BinaryReader {
buf: Buffer;
buf: DataView;
offset: number;
constructor(buf: Buffer);
constructor(buf: Uint8Array);
readBool(): boolean;

@@ -27,0 +26,0 @@ readU8(): number;

@@ -1,2 +0,1 @@

/// <reference types="node" />
import { StructKind, SimpleField, CustomField, Constructor } from "./types";

@@ -18,3 +17,3 @@ import { BinaryWriter, BinaryReader } from "./binary";

*/
export declare function deserialize<T>(buffer: Buffer, classType: {
export declare function deserialize<T>(buffer: Uint8Array, classType: {
new (args: any): T;

@@ -24,3 +23,3 @@ }, unchecked?: boolean, Reader?: typeof BinaryReader): T;

new (args: any): T;
}, buffer: Buffer, Reader?: typeof BinaryReader): T;
}, buffer: Uint8Array, Reader?: typeof BinaryReader): T;
export declare const getSchema: (ctor: Function) => StructKind;

@@ -27,0 +26,0 @@ export declare const getSchemasBottomUp: (ctor: Function) => {

{
"name": "@dao-xyz/borsh",
"version": "3.1.0",
"version": "3.2.0",
"readme": "README.md",

@@ -56,5 +56,4 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README",

"dependencies": {
"bigint-buffer": "^1.1.5",
"text-encoding-utf-8": "^1.0.2"
}
}
}

@@ -48,4 +48,4 @@ # Borsh TS

@field({type: vec('u32')})
q: number[]
@field({type: option(vec('u32'))})
q?: number[]

@@ -69,3 +69,3 @@ constructor(data?:SomeClass)

// Deserialize
const deserialized = deserialize(Buffer.from(serialized),SomeClass);
const deserialized = deserialize(serialized,SomeClass);
```

@@ -206,3 +206,3 @@

const serialized = serialize(new TestStruct(3));
const deserialied = deserialize(Buffer.from(serialized), TestStruct);
const deserialied = deserialize(serialized, TestStruct);
expect(deserialied.number).toEqual(3);

@@ -209,0 +209,0 @@ ```

@@ -15,3 +15,2 @@ import { BinaryReader } from "../binary";

} from "../index";
import BN from "bn.js";

@@ -51,8 +50,8 @@ describe("struct", () => {

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([1, 123, 0, 0, 0, 0, 0, 0, 0]));
const deserialized = deserialize(Buffer.from(buf), TestStruct);
expect(buf).toEqual(new Uint8Array([1, 123, 0, 0, 0, 0, 0, 0, 0]));
const deserialized = deserialize(buf, TestStruct);
expect(deserialized.a).toEqual(1);
expect(deserialized.b).toEqual(BigInt(123));
const bufAgain = serialize(deserialized);
expect(bufAgain).toEqual(Buffer.from([1, 123, 0, 0, 0, 0, 0, 0, 0]));
expect(bufAgain).toEqual(new Uint8Array([1, 123, 0, 0, 0, 0, 0, 0, 0]));
});

@@ -137,8 +136,8 @@

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([1, 0]));
const deserialized = deserialize(Buffer.from(buf), TestStruct);
expect(buf).toEqual(new Uint8Array([1, 0]));
const deserialized = deserialize(buf, TestStruct);
expect(deserialized.a).toEqual(true);
expect(deserialized.b).toEqual(false);
const bufAgain = serialize(deserialized);
expect(bufAgain).toEqual(Buffer.from([1, 0]));
expect(bufAgain).toEqual(new Uint8Array([1, 0]));
});

@@ -162,4 +161,4 @@ });

const buf = serialize(new TestStruct({ a: [1, 2, 3] }));
expect(buf).toEqual(Buffer.from([3, 0, 0, 0, 1, 2, 3]));
const deserialized = deserialize(Buffer.from(buf), TestStruct);
expect(buf).toEqual(new Uint8Array([3, 0, 0, 0, 1, 2, 3]));
const deserialized = deserialize(buf, TestStruct);
expect(deserialized.a).toEqual([1, 2, 3]);

@@ -182,4 +181,4 @@ });

const buf = serialize(new TestStruct({ a: [1, 2, 3] }));
expect(buf).toEqual(Buffer.from([1, 2, 3]));
const deserialized = deserialize(Buffer.from(buf), TestStruct);
expect(buf).toEqual(new Uint8Array([1, 2, 3]));
const deserialized = deserialize(buf, TestStruct);
expect(deserialized.a).toEqual([1, 2, 3]);

@@ -216,3 +215,5 @@ });

validate(TestStruct);
expect(() => deserialize(Buffer.from([1, 2]), TestStruct)).toThrowError();
expect(() =>
deserialize(new Uint8Array([1, 2]), TestStruct)
).toThrowError();
});

@@ -250,4 +251,4 @@

const buf = serialize(new TestStruct({ a: arr }));
expect(buf).toEqual(Buffer.from([3, 0, 0, 0, 1, 2, 3]));
const deserialized = deserialize(Buffer.from(buf), TestStruct);
expect(buf).toEqual(new Uint8Array([3, 0, 0, 0, 1, 2, 3]));
const deserialized = deserialize(buf, TestStruct);
expect(deserialized.a).toEqual(arr);

@@ -269,4 +270,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(3);

@@ -286,4 +287,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3, 0]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3, 0]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(3);

@@ -303,4 +304,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3, 0, 0, 0]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3, 0, 0, 0]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(3);

@@ -319,4 +320,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3, ...new Array(7).fill(0)]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3, ...new Array(7).fill(0)]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(BigInt(3));

@@ -336,4 +337,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3, ...new Array(15).fill(0)]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3, ...new Array(15).fill(0)]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(BigInt(3));

@@ -353,4 +354,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3, ...new Array(31).fill(0)]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3, ...new Array(31).fill(0)]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(BigInt(3));

@@ -370,4 +371,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([3, ...new Array(63).fill(0)]));
const deserialized = deserialize(Buffer.from(buf), Struct);
expect(buf).toEqual(new Uint8Array([3, ...new Array(63).fill(0)]));
const deserialized = deserialize(buf, Struct);
expect(deserialized.a).toEqual(BigInt(3));

@@ -390,4 +391,4 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([1, 3]));
const deserialized = deserialize(Buffer.from(buf), TestEnum);
expect(buf).toEqual(new Uint8Array([1, 3]));
const deserialized = deserialize(buf, TestEnum);
expect(deserialized.a).toEqual(3);

@@ -402,3 +403,3 @@ });

const buf = serialize(instance);
expect(buf).toEqual(Buffer.from([1]));
expect(buf).toEqual(new Uint8Array([1]));
});

@@ -467,6 +468,6 @@

const serialized = serialize(instance);
expect(serialized).toEqual(Buffer.from([1, 4]));
expect(serialized).toEqual(new Uint8Array([1, 4]));
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
TestStruct,

@@ -516,6 +517,6 @@ false,

const serialized = serialize(instance);
expect(serialized).toEqual(Buffer.from([1, 4]));
expect(serialized).toEqual(new Uint8Array([1, 4]));
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
SuperSuper,

@@ -567,6 +568,6 @@ false,

const serialized = serialize(instance);
expect(serialized).toEqual(Buffer.from([1, 2, 3, 4, 5]));
expect(serialized).toEqual(new Uint8Array([1, 2, 3, 4, 5]));
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
SuperSuper,

@@ -592,3 +593,3 @@ false,

deserialize(
Buffer.from(serialize(new Clazz())),
new Uint8Array(serialize(new Clazz())),
Clazz,

@@ -600,3 +601,3 @@ false,

deserialize(
Buffer.from(serialize(new Clazz())),
new Uint8Array(serialize(new Clazz())),
Super,

@@ -661,3 +662,3 @@ false,

expect(() =>
deserialize(Buffer.from(Uint8Array.from([0, 0])), Struct)
deserialize(new Uint8Array(Uint8Array.from([0, 0])), Struct)
).toThrowError();

@@ -680,3 +681,3 @@ });

deserialize(
Buffer.from(serialize(new Clazz())),
new Uint8Array(serialize(new Clazz())),
Clazz,

@@ -688,3 +689,3 @@ false,

deserialize(
Buffer.from(serialize(new Clazz())),
new Uint8Array(serialize(new Clazz())),
Super,

@@ -728,6 +729,6 @@ false,

const serialized = serialize(new C1({ a: 1, b: 2 }));
expect(serialized).toEqual(Buffer.from([1, 2, 0]));
expect(serialized).toEqual(new Uint8Array([1, 2, 0]));
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
Super,

@@ -769,5 +770,5 @@ false,

const serialized = serialize(instance);
expect(serialized).toEqual(Buffer.from([1, 2, 3])); // 1 for option, 2 for variant, 3 for value
expect(serialized).toEqual(new Uint8Array([1, 2, 3])); // 1 for option, 2 for variant, 3 for value
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
TestStruct,

@@ -819,5 +820,5 @@ false,

const serialized = serialize(instance);
expect(serialized).toEqual(Buffer.from([1, 2, 4, 5]));
expect(serialized).toEqual(new Uint8Array([1, 2, 4, 5]));
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
TestStruct,

@@ -859,3 +860,3 @@ false,

);
let deserialized = deserialize(Buffer.from(bytes), HighCouncil);
let deserialized = deserialize(new Uint8Array(bytes), HighCouncil);
expect(deserialized).toBeInstanceOf(HighCouncil);

@@ -889,9 +890,9 @@ expect(deserialized.members[0]).toBeInstanceOf(Gorilla);

const bufSome = serialize(new TestStruct(123));
expect(bufSome).toEqual(Buffer.from([1, 123]));
const deserializedSome = deserialize(Buffer.from(bufSome), TestStruct);
expect(bufSome).toEqual(new Uint8Array([1, 123]));
const deserializedSome = deserialize(new Uint8Array(bufSome), TestStruct);
expect(deserializedSome.a).toEqual(123);
const bufNone = serialize(new TestStruct(undefined));
expect(bufNone).toEqual(Buffer.from([0]));
const deserialized = deserialize(Buffer.from(bufNone), TestStruct);
expect(bufNone).toEqual(new Uint8Array([0]));
const deserialized = deserialize(new Uint8Array(bufNone), TestStruct);
expect(deserialized.a).toBeUndefined();

@@ -926,9 +927,9 @@ });

const bufSome = serialize(new TestStruct(new Element(123)));
expect(bufSome).toEqual(Buffer.from([1, 123]));
const deserializedSome = deserialize(Buffer.from(bufSome), TestStruct);
expect(bufSome).toEqual(new Uint8Array([1, 123]));
const deserializedSome = deserialize(new Uint8Array(bufSome), TestStruct);
expect(deserializedSome.a).toEqual(new Element(123));
const bufNone = serialize(new TestStruct(undefined));
expect(bufNone).toEqual(Buffer.from([0]));
const deserialized = deserialize(Buffer.from(bufNone), TestStruct);
expect(bufNone).toEqual(new Uint8Array([0]));
const deserialized = deserialize(new Uint8Array(bufNone), TestStruct);
expect(deserialized.a).toBeUndefined();

@@ -938,2 +939,30 @@ });

describe("string", () => {
test("field string", () => {
class TestStruct {
@field({ type: "string" })
public a: string;
constructor(a: string) {
this.a = a;
}
}
validate(TestStruct);
const expectedResult: StructKind = new StructKind({
fields: [
{
key: "a",
type: "string",
},
],
});
expect(getSchema(TestStruct)).toEqual(expectedResult);
const bufSome = serialize(new TestStruct("a string"));
expect(bufSome).toEqual(
new Uint8Array([8, 0, 0, 0, 97, 32, 115, 116, 114, 105, 110, 103])
);
const deserializedSome = deserialize(new Uint8Array(bufSome), TestStruct);
expect(deserializedSome.a).toEqual("a string");
});
});
describe("override", () => {

@@ -961,3 +990,3 @@ test("serialize/deserialize", () => {

const serialized = serialize(new TestStruct(3));
const deserialied = deserialize(Buffer.from(serialized), TestStruct);
const deserialied = deserialize(new Uint8Array(serialized), TestStruct);
expect(deserialied.number).toEqual(3);

@@ -995,7 +1024,10 @@ });

expect(
deserialize(Buffer.from(serialize(new TestStruct(undefined))), TestStruct)
.number
deserialize(
new Uint8Array(serialize(new TestStruct(undefined))),
TestStruct
).number
).toBeUndefined();
expect(
deserialize(Buffer.from(serialize(new TestStruct(1))), TestStruct).number
deserialize(new Uint8Array(serialize(new TestStruct(1))), TestStruct)
.number
).toEqual(1);

@@ -1030,5 +1062,5 @@ });

const serialized = serialize(new TestStruct(123));
expect(serialized).toStrictEqual(Buffer.from([1, 123]));
expect(serialized).toStrictEqual(new Uint8Array([1, 123]));
const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
TestStruct,

@@ -1042,3 +1074,3 @@ false,

const serializedNone = serialize(new TestStruct(undefined));
expect(serializedNone).toStrictEqual(Buffer.from([0]));
expect(serializedNone).toStrictEqual(new Uint8Array([0]));
});

@@ -1078,3 +1110,3 @@ });

const deserialied = deserialize(
Buffer.from(serialized),
new Uint8Array(serialized),
TestStruct,

@@ -1160,5 +1192,5 @@ false,

expect(() =>
deserialize(Buffer.from(bytes), TestStruct, false)
deserialize(new Uint8Array(bytes), TestStruct, false)
).toThrowError(BorshError);
expect(deserialize(Buffer.from(bytes), TestStruct, true).a).toEqual(1);
expect(deserialize(new Uint8Array(bytes), TestStruct, true).a).toEqual(1);
});

@@ -1350,3 +1382,3 @@

expect(() =>
deserialize(Buffer.from(serialize(new A())), TestStruct)
deserialize(new Uint8Array(serialize(new A())), TestStruct)
).toThrowError(BorshError);

@@ -1353,0 +1385,0 @@ });

const INITIAL_LENGTH = 1024;
import { toBigIntLE, toBufferLE } from 'bigint-buffer';
import { toBigIntLE, toBufferLE } from './bigint';
import { BorshError } from "./error";

@@ -10,11 +10,13 @@ import * as encoding from "text-encoding-utf-8";

const ResolvedTextEncoder =
typeof TextEncoder !== "function" ? encoding.TextEncoder : TextEncoder;
const textEncoder = new ResolvedTextEncoder("utf-8");
/// Binary encoder.
export class BinaryWriter {
buf: Buffer;
buf: DataView;
length: number;
public constructor() {
this.buf = Buffer.alloc(INITIAL_LENGTH);
this.buf = new DataView(new ArrayBuffer(INITIAL_LENGTH));
this.length = 0;

@@ -24,4 +26,7 @@ }

maybeResize() {
if (this.buf.length < 16 + this.length) {
this.buf = Buffer.concat([this.buf, Buffer.alloc(INITIAL_LENGTH)]);
if (this.buf.byteLength < 16 + this.length) {
const newArr = new Uint8Array(this.buf.byteLength + INITIAL_LENGTH);
newArr.set(new Uint8Array(this.buf.buffer));
newArr.set(new Uint8Array(INITIAL_LENGTH), this.buf.byteLength)
this.buf = new DataView(newArr.buffer);
}

@@ -32,3 +37,3 @@ }

this.maybeResize();
this.buf.writeUInt8(value ? 1 : 0, this.length);
this.buf.setUint8(value ? 1 : 0, this.length);
this.length += 1;

@@ -39,3 +44,3 @@ }

this.maybeResize();
this.buf.writeUInt8(value, this.length);
this.buf.setUint8(this.length, value);
this.length += 1;

@@ -46,3 +51,3 @@ }

this.maybeResize();
this.buf.writeUInt16LE(value, this.length);
this.buf.setUint16(this.length, value, true);
this.length += 2;

@@ -53,3 +58,3 @@ }

this.maybeResize();
this.buf.writeUInt32LE(value, this.length);
this.buf.setUint32(this.length, value, true);
this.length += 4;

@@ -78,10 +83,9 @@ }

private writeBuffer(buffer: Buffer) {
// Buffer.from is needed as this.buf.subarray can return plain Uint8Array in browser
this.buf = Buffer.concat([
Buffer.from(this.buf.subarray(0, this.length)),
buffer,
Buffer.alloc(INITIAL_LENGTH),
]);
private writeBuffer(buffer: Uint8Array) {
const newBuf = new Uint8Array(this.length + buffer.length + INITIAL_LENGTH);
newBuf.set(new Uint8Array(this.buf.buffer.slice(0, this.length)));
newBuf.set(buffer, this.length);
newBuf.set(new Uint8Array(INITIAL_LENGTH), this.length + buffer.length);
this.length += buffer.length;
this.buf = new DataView(newBuf.buffer);
}

@@ -91,3 +95,3 @@

this.maybeResize();
const b = Buffer.from(str, "utf8");
const b = textEncoder.encode(str);
this.writeU32(b.length);

@@ -98,3 +102,3 @@ this.writeBuffer(b);

public writeFixedArray(array: Uint8Array) {
this.writeBuffer(Buffer.from(array));
this.writeBuffer(array);
}

@@ -112,3 +116,3 @@

public toArray(): Uint8Array {
return this.buf.subarray(0, this.length);
return new Uint8Array(this.buf.buffer.slice(0, this.length));
}

@@ -141,7 +145,7 @@ }

export class BinaryReader {
buf: Buffer;
buf: DataView;
offset: number;
public constructor(buf: Buffer) {
this.buf = buf;
public constructor(buf: Uint8Array) {
this.buf = new DataView(buf.buffer);
this.offset = 0;

@@ -152,3 +156,3 @@ }

readBool(): boolean {
const value = this.buf.readUInt8(this.offset);
const value = this.buf.getUint8(this.offset);
this.offset += 1;

@@ -160,3 +164,3 @@ return value ? true : false;

readU8(): number {
const value = this.buf.readUInt8(this.offset);
const value = this.buf.getUint8(this.offset);
this.offset += 1;

@@ -168,3 +172,3 @@ return value;

readU16(): number {
const value = this.buf.readUInt16LE(this.offset);
const value = this.buf.getUint16(this.offset, true);
this.offset += 2;

@@ -176,3 +180,3 @@ return value;

readU32(): number {
const value = this.buf.readUInt32LE(this.offset);
const value = this.buf.getUint32(this.offset, true);
this.offset += 4;

@@ -206,9 +210,9 @@ return value;

private readBuffer(len: number): Buffer {
if (this.offset + len > this.buf.length) {
private readBuffer(len: number): Uint8Array {
if (this.offset + len > this.buf.byteLength) {
throw new BorshError(`Expected buffer length ${len} isn't within bounds`);
}
const result = this.buf.slice(this.offset, this.offset + len);
const result = this.buf.buffer.slice(this.offset, this.offset + len);
this.offset += len;
return result;
return new Uint8Array(result);
}

@@ -215,0 +219,0 @@

@@ -67,3 +67,3 @@ import {

if (!checkClazzesCompatible(value.constructor, fieldType)) {
throw new BorshError(`Field value of field ${fieldName} does not instance of expected Class ${getSuperMostClass(fieldType)}. Got: ${value.constructor.name}`)
throw new BorshError(`Field value of field ${fieldName} is not instance of expected Class ${getSuperMostClass(fieldType)?.name}. Got: ${value.constructor.name}`)
}

@@ -317,3 +317,3 @@ serializeStruct(value, writer);

export function deserialize<T>(
buffer: Buffer,
buffer: Uint8Array,
classType: { new(args: any): T },

@@ -337,3 +337,3 @@ unchecked: boolean = false,

classType: { new(args: any): T },
buffer: Buffer,
buffer: Uint8Array,
Reader = BinaryReader

@@ -340,0 +340,0 @@ ): T {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc