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.4.4 to 3.5.0

54

lib/cjs/binary.d.ts
export declare class BinaryWriter {
buf: DataView;
length: number;
_buf: DataView;
_length: number;
constructor();
maybeResize(): void;
writeBool(value: boolean): void;
writeU8(value: number): void;
writeU16(value: number): void;
writeU32(value: number): void;
writeU64(value: number | bigint): void;
writeU128(value: number | bigint): void;
writeU256(value: number | bigint): void;
writeU512(value: number | bigint): void;
private writeBuffer;
writeString(str: string): void;
writeFixedArray(array: Uint8Array): void;
writeArray(array: any[], fn: any): void;
bool(value: boolean): void;
u8(value: number): void;
u16(value: number): void;
u32(value: number): void;
u64(value: number | bigint): void;
u128(value: number | bigint): void;
u256(value: number | bigint): void;
u512(value: number | bigint): void;
string(str: string): void;
uint8Array(array: Uint8Array): void;
private buffer;
array(array: any[], fn: any): void;
toArray(): Uint8Array;
}
export declare class BinaryReader {
buf: DataView;
offset: number;
_buf: DataView;
_offset: number;
constructor(buf: Uint8Array);
readBool(): boolean;
readU8(): number;
readU16(): number;
readU32(): number;
readU64(): bigint;
readU128(): bigint;
readU256(): bigint;
readU512(): bigint;
private readBuffer;
readString(): string;
readFixedArray(len: number): Uint8Array;
bool(): boolean;
u8(): number;
u16(): number;
u32(): number;
u64(): bigint;
u128(): bigint;
u256(): bigint;
u512(): bigint;
private buffer;
string(): string;
uint8Array(): Uint8Array;
readArray(fn: any): any[];
}

@@ -44,69 +44,70 @@ "use strict";

constructor() {
this.buf = new DataView(new ArrayBuffer(INITIAL_LENGTH));
this.length = 0;
this._buf = new DataView(new ArrayBuffer(INITIAL_LENGTH));
this._length = 0;
}
maybeResize() {
if (this.buf.byteLength < 64 + 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);
if (this._buf.byteLength < 64 + 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);
}
}
writeBool(value) {
bool(value) {
this.maybeResize();
this.buf.setUint8(this.length, value ? 1 : 0);
this.length += 1;
this._buf.setUint8(this._length, value ? 1 : 0);
this._length += 1;
}
writeU8(value) {
u8(value) {
this.maybeResize();
this.buf.setUint8(this.length, value);
this.length += 1;
this._buf.setUint8(this._length, value);
this._length += 1;
}
writeU16(value) {
u16(value) {
this.maybeResize();
this.buf.setUint16(this.length, value, true);
this.length += 2;
this._buf.setUint16(this._length, value, true);
this._length += 2;
}
writeU32(value) {
u32(value) {
this.maybeResize();
this.buf.setUint32(this.length, value, true);
this.length += 4;
this._buf.setUint32(this._length, value, true);
this._length += 4;
}
writeU64(value) {
u64(value) {
this.maybeResize();
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 8));
this.buffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 8));
}
writeU128(value) {
u128(value) {
this.maybeResize();
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 16));
this.buffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 16));
}
writeU256(value) {
u256(value) {
this.maybeResize();
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 32));
this.buffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 32));
}
writeU512(value) {
u512(value) {
this.maybeResize();
this.writeBuffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 64));
this.buffer((0, bigint_1.toBufferLE)(typeof value === 'number' ? BigInt(value) : value, 64));
}
writeBuffer(buffer) {
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) {
string(str) {
this.maybeResize();
const b = textEncoder.encode(str);
this.writeU32(b.length);
this.writeBuffer(b);
this.u32(b.length);
this.buffer(b);
}
writeFixedArray(array) {
this.writeBuffer(array);
uint8Array(array) {
this.u32(array.length);
this.buffer(array);
}
writeArray(array, fn) {
buffer(buffer) {
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);
}
array(array, fn) {
this.maybeResize();
this.writeU32(array.length);
this.u32(array.length);
for (const elem of array) {

@@ -118,3 +119,3 @@ this.maybeResize();

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

@@ -142,52 +143,52 @@ }

constructor(buf) {
this.buf = new DataView(buf.buffer);
this.offset = buf.byteOffset;
this._buf = new DataView(buf.buffer);
this._offset = buf.byteOffset;
}
readBool() {
const value = this.buf.getUint8(this.offset);
this.offset += 1;
bool() {
const value = this._buf.getUint8(this._offset);
this._offset += 1;
return value ? true : false;
}
readU8() {
const value = this.buf.getUint8(this.offset);
this.offset += 1;
u8() {
const value = this._buf.getUint8(this._offset);
this._offset += 1;
return value;
}
readU16() {
const value = this.buf.getUint16(this.offset, true);
this.offset += 2;
u16() {
const value = this._buf.getUint16(this._offset, true);
this._offset += 2;
return value;
}
readU32() {
const value = this.buf.getUint32(this.offset, true);
this.offset += 4;
u32() {
const value = this._buf.getUint32(this._offset, true);
this._offset += 4;
return value;
}
readU64() {
const buf = this.readBuffer(8);
u64() {
const buf = this.buffer(8);
return (0, bigint_1.toBigIntLE)(buf);
}
readU128() {
const buf = this.readBuffer(16);
u128() {
const buf = this.buffer(16);
return (0, bigint_1.toBigIntLE)(buf);
}
readU256() {
const buf = this.readBuffer(32);
u256() {
const buf = this.buffer(32);
return (0, bigint_1.toBigIntLE)(buf);
}
readU512() {
const buf = this.readBuffer(64);
u512() {
const buf = this.buffer(64);
return (0, bigint_1.toBigIntLE)(buf);
}
readBuffer(len) {
if (this.offset + len > this.buf.byteLength) {
buffer(len) {
if (this._offset + len > this._buf.byteLength) {
throw new error_1.BorshError(`Expected buffer length ${len} isn't within bounds`);
}
const result = this.buf.buffer.slice(this.offset, this.offset + len);
this.offset += len;
const result = this._buf.buffer.slice(this._offset, this._offset + len);
this._offset += len;
return new Uint8Array(result);
}
readString() {
const len = this.readU32();
const buf = this.readBuffer(len);
string() {
const len = this.u32();
const buf = this.buffer(len);
try {

@@ -201,7 +202,8 @@ // NOTE: Using TextDecoder to fail on invalid UTF-8

}
readFixedArray(len) {
return new Uint8Array(this.readBuffer(len));
uint8Array() {
const len = this.u32();
return new Uint8Array(this.buffer(len));
}
readArray(fn) {
const len = this.readU32();
const len = this.u32();
const result = Array();

@@ -216,30 +218,30 @@ for (let i = 0; i < len; ++i) {

handlingRangeError
], BinaryReader.prototype, "readBool", null);
], BinaryReader.prototype, "bool", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU8", null);
], BinaryReader.prototype, "u8", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU16", null);
], BinaryReader.prototype, "u16", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU32", null);
], BinaryReader.prototype, "u32", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU64", null);
], BinaryReader.prototype, "u64", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU128", null);
], BinaryReader.prototype, "u128", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU256", null);
], BinaryReader.prototype, "u256", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readU512", null);
], BinaryReader.prototype, "u512", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readString", null);
], BinaryReader.prototype, "string", null);
__decorate([
handlingRangeError
], BinaryReader.prototype, "readFixedArray", null);
], BinaryReader.prototype, "uint8Array", null);
__decorate([

@@ -246,0 +248,0 @@ handlingRangeError

@@ -7,3 +7,3 @@ import { StructKind, SimpleField, CustomField, Constructor, AbstractType } from "./types";

export declare function serializeField(fieldName: string, value: any, fieldType: any, // A simple type of a CustomField
writer: any): void;
writer: BinaryWriter): void;
export declare function serializeStruct(obj: any, writer: BinaryWriter): void;

@@ -10,0 +10,0 @@ export declare function serialize(obj: any, Writer?: typeof BinaryWriter): Uint8Array;

@@ -24,5 +24,2 @@ "use strict";

__exportStar(require("./error"), exports);
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function serializeField(fieldName, value, fieldType, // A simple type of a CustomField

@@ -37,3 +34,3 @@ writer) {

if (fieldType instanceof types_1.OptionKind) {
writer.writeU8(0);
writer.u8(0);
}

@@ -45,7 +42,11 @@ else {

else if (fieldType instanceof types_1.OptionKind) {
writer.writeU8(1);
writer.u8(1);
serializeField(fieldName, value, fieldType.elementType, writer);
}
else if (fieldType === Uint8Array) {
const arr = value;
writer.uint8Array(arr);
}
else if (typeof fieldType === "string") {
writer[`write${capitalizeFirstLetter(fieldType)}`](value);
writer[fieldType](value);
}

@@ -61,3 +62,3 @@ else if (fieldType instanceof types_1.VecKind ||

else {
writer.writeU32(len); // For dynamically sized array we write the size as u32 according to specification
writer.u32(len); // For dynamically sized array we write the size as u32 according to specification
}

@@ -102,11 +103,11 @@ for (let i = 0; i < len; i++) {

if (typeof index === "number") {
writer.writeU8(index);
writer.u8(index);
}
else if (Array.isArray(index)) {
index.forEach((i) => {
writer.writeU8(i);
writer.u8(i);
});
}
else { // is string
writer.writeString(index);
writer.string(index);
}

@@ -135,8 +136,11 @@ }

if (typeof fieldType === "string") {
return reader[`read${capitalizeFirstLetter(fieldType)}`]();
return reader[fieldType]();
}
if (fieldType === Uint8Array) {
return reader.uint8Array();
}
if (fieldType instanceof types_1.VecKind || fieldType instanceof types_1.FixedArrayKind) {
let len = fieldType instanceof types_1.FixedArrayKind
? fieldType.length
: reader.readU32();
: reader.u32();
let arr = new Array(len);

@@ -152,3 +156,3 @@ for (let i = 0; i < len; i++) {

if (fieldType instanceof types_1.OptionKind) {
const option = reader.readU8();
const option = reader.u8();
if (option) {

@@ -180,11 +184,11 @@ return deserializeField(fieldName, fieldType.elementType, reader);

if (typeof index === "number") {
reader.readU8();
reader.u8();
}
else if (Array.isArray(index)) {
for (const _ of index) {
reader.readU8();
reader.u8();
}
}
else { // string
reader.readString();
reader.string();
}

@@ -213,3 +217,3 @@ }

if (!variantsIndex) {
variantsIndex = [reader.readU8()];
variantsIndex = [reader.u8()];
}

@@ -225,3 +229,3 @@ if (variantIndex == variantsIndex[0]) {

while (variantsIndex.length < variantIndex.length) {
variantsIndex.push(reader.readU8());
variantsIndex.push(reader.u8());
}

@@ -238,3 +242,3 @@ }

if (variantString == undefined) {
variantString = reader.readString();
variantString = reader.string();
}

@@ -296,4 +300,4 @@ // Compare variants is just string compare

const result = deserializeStruct(classType, reader);
if (!unchecked && reader.offset !== buffer.byteOffset + buffer.length) {
throw new error_1.BorshError(`Unexpected ${buffer.length - reader.offset} bytes after deserialized data`);
if (!unchecked && reader._offset !== buffer.byteOffset + buffer.length) {
throw new error_1.BorshError(`Unexpected ${buffer.length - reader._offset} bytes after deserialized data`);
}

@@ -583,10 +587,10 @@ return result;

if (typeof variant === 'string') {
writer.writeString(variant);
writer.string(variant);
}
else if (typeof variant === 'number') {
writer.writeU8(variant);
writer.u8(variant);
}
else if (Array.isArray(variant)) {
variant.forEach((v) => {
writer.writeU8(v);
writer.u8(v);
});

@@ -593,0 +597,0 @@ }

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

}
export type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "string" | Constructor<any> | AbstractType<any> | CustomField<any> | WrappedType;
export type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "string" | Constructor<any> | AbstractType<any> | CustomField<any> | WrappedType | Uint8Array;
export type SimpleField = {

@@ -17,0 +17,0 @@ type: FieldType;

export declare class BinaryWriter {
buf: DataView;
length: number;
_buf: DataView;
_length: number;
constructor();
maybeResize(): void;
writeBool(value: boolean): void;
writeU8(value: number): void;
writeU16(value: number): void;
writeU32(value: number): void;
writeU64(value: number | bigint): void;
writeU128(value: number | bigint): void;
writeU256(value: number | bigint): void;
writeU512(value: number | bigint): void;
private writeBuffer;
writeString(str: string): void;
writeFixedArray(array: Uint8Array): void;
writeArray(array: any[], fn: any): void;
bool(value: boolean): void;
u8(value: number): void;
u16(value: number): void;
u32(value: number): void;
u64(value: number | bigint): void;
u128(value: number | bigint): void;
u256(value: number | bigint): void;
u512(value: number | bigint): void;
string(str: string): void;
uint8Array(array: Uint8Array): void;
private buffer;
array(array: any[], fn: any): void;
toArray(): Uint8Array;
}
export declare class BinaryReader {
buf: DataView;
offset: number;
_buf: DataView;
_offset: number;
constructor(buf: Uint8Array);
readBool(): boolean;
readU8(): number;
readU16(): number;
readU32(): number;
readU64(): bigint;
readU128(): bigint;
readU256(): bigint;
readU512(): bigint;
private readBuffer;
readString(): string;
readFixedArray(len: number): Uint8Array;
bool(): boolean;
u8(): number;
u16(): number;
u32(): number;
u64(): bigint;
u128(): bigint;
u256(): bigint;
u512(): bigint;
private buffer;
string(): string;
uint8Array(): Uint8Array;
readArray(fn: any): any[];
}

@@ -7,3 +7,3 @@ import { StructKind, SimpleField, CustomField, Constructor, AbstractType } from "./types";

export declare function serializeField(fieldName: string, value: any, fieldType: any, // A simple type of a CustomField
writer: any): void;
writer: BinaryWriter): void;
export declare function serializeStruct(obj: any, writer: BinaryWriter): void;

@@ -10,0 +10,0 @@ export declare function serialize(obj: any, Writer?: typeof BinaryWriter): Uint8Array;

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

}
export type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "string" | Constructor<any> | AbstractType<any> | CustomField<any> | WrappedType;
export type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "string" | Constructor<any> | AbstractType<any> | CustomField<any> | WrappedType | Uint8Array;
export type SimpleField = {

@@ -17,0 +17,0 @@ type: FieldType;

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

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

@@ -199,6 +199,6 @@ # Borsh TS

serialize: (value: number, writer) => {
writer.writeU16(value);
writer.u16(value);
},
deserialize: (reader): number => {
return reader.readU16();
return reader.u16();
},

@@ -205,0 +205,0 @@ })

@@ -215,3 +215,20 @@ import { BinaryReader } from "../binary";

});
it("u8intarary", () => {
class TestStruct {
@field({ type: Uint8Array })
public a: Uint8Array;
constructor(properties?: { a: Uint8Array }) {
if (properties) {
this.a = properties.a;
}
}
}
validate(TestStruct);
const buf = serialize(new TestStruct({ a: new Uint8Array([1, 2, 3]) }));
expect(buf).toEqual(new Uint8Array([3, 0, 0, 0, 1, 2, 3]));
const deserialized = deserialize(buf, TestStruct);
expect(deserialized.a).toEqual(new Uint8Array([1, 2, 3]));
});
test("vec struct", () => {

@@ -995,6 +1012,6 @@ class Element {

serialize: (value: number, writer) => {
writer.writeU16(value);
writer.u16(value);
},
deserialize: (reader): number => {
return reader.readU16();
return reader.u16();
},

@@ -1023,14 +1040,14 @@ })

if (typeof value !== "number") {
writer.writeU8(0);
writer.u8(0);
return;
}
writer.writeU8(1);
writer.writeU32(value);
writer.u8(1);
writer.u32(value);
},
deserialize: (reader): number => {
const option = reader.readU8();
const option = reader.u8();
if (option === 0) {
return undefined;
}
return reader.readU32();
return reader.u32();
},

@@ -1064,6 +1081,6 @@ })

serialize: (value: number, writer) => {
writer.writeU8(value);
writer.u8(value);
},
deserialize: (reader): number => {
return reader.readU8();
return reader.u8();
},

@@ -1070,0 +1087,0 @@ }),

@@ -16,86 +16,91 @@ const INITIAL_LENGTH = 1024;

export class BinaryWriter {
buf: DataView;
length: number;
_buf: DataView;
_length: number;
public constructor() {
this.buf = new DataView(new ArrayBuffer(INITIAL_LENGTH));
this.length = 0;
this._buf = new DataView(new ArrayBuffer(INITIAL_LENGTH));
this._length = 0;
}
maybeResize() {
if (this.buf.byteLength < 64 + 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);
if (this._buf.byteLength < 64 + 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);
}
}
public writeBool(value: boolean) {
public bool(value: boolean) {
this.maybeResize();
this.buf.setUint8(this.length, value ? 1 : 0);
this.length += 1;
this._buf.setUint8(this._length, value ? 1 : 0);
this._length += 1;
}
public writeU8(value: number) {
public u8(value: number) {
this.maybeResize();
this.buf.setUint8(this.length, value);
this.length += 1;
this._buf.setUint8(this._length, value);
this._length += 1;
}
public writeU16(value: number) {
public u16(value: number) {
this.maybeResize();
this.buf.setUint16(this.length, value, true);
this.length += 2;
this._buf.setUint16(this._length, value, true);
this._length += 2;
}
public writeU32(value: number) {
public u32(value: number) {
this.maybeResize();
this.buf.setUint32(this.length, value, true);
this.length += 4;
this._buf.setUint32(this._length, value, true);
this._length += 4;
}
public writeU64(value: number | bigint) {
public u64(value: number | bigint) {
this.maybeResize();
this.writeBuffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 8))
this.buffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 8))
}
public writeU128(value: number | bigint) {
public u128(value: number | bigint) {
this.maybeResize();
this.writeBuffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 16))
this.buffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 16))
}
public writeU256(value: number | bigint) {
public u256(value: number | bigint) {
this.maybeResize();
this.writeBuffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 32))
this.buffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 32))
}
public writeU512(value: number | bigint) {
public u512(value: number | bigint) {
this.maybeResize();
this.writeBuffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 64))
this.buffer(toBufferLE(typeof value === 'number' ? BigInt(value) : value, 64))
}
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);
}
public writeString(str: string) {
public string(str: string) {
this.maybeResize();
const b = textEncoder.encode(str);
this.writeU32(b.length);
this.writeBuffer(b);
this.u32(b.length);
this.buffer(b);
}
public writeFixedArray(array: Uint8Array) {
this.writeBuffer(array);
public uint8Array(array: Uint8Array) {
this.u32(array.length)
this.buffer(array);
}
public writeArray(array: any[], fn: any) {
private buffer(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);
}
public array(array: any[], fn: any) {
this.maybeResize();
this.writeU32(array.length);
this.u32(array.length);
for (const elem of array) {

@@ -108,3 +113,3 @@ this.maybeResize();

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

@@ -137,14 +142,14 @@ }

export class BinaryReader {
buf: DataView;
offset: number;
_buf: DataView;
_offset: number;
public constructor(buf: Uint8Array) {
this.buf = new DataView(buf.buffer);
this.offset = buf.byteOffset;
this._buf = new DataView(buf.buffer);
this._offset = buf.byteOffset;
}
@handlingRangeError
readBool(): boolean {
const value = this.buf.getUint8(this.offset);
this.offset += 1;
bool(): boolean {
const value = this._buf.getUint8(this._offset);
this._offset += 1;
return value ? true : false;

@@ -154,5 +159,5 @@ }

@handlingRangeError
readU8(): number {
const value = this.buf.getUint8(this.offset);
this.offset += 1;
u8(): number {
const value = this._buf.getUint8(this._offset);
this._offset += 1;
return value;

@@ -162,5 +167,5 @@ }

@handlingRangeError
readU16(): number {
const value = this.buf.getUint16(this.offset, true);
this.offset += 2;
u16(): number {
const value = this._buf.getUint16(this._offset, true);
this._offset += 2;
return value;

@@ -170,5 +175,5 @@ }

@handlingRangeError
readU32(): number {
const value = this.buf.getUint32(this.offset, true);
this.offset += 4;
u32(): number {
const value = this._buf.getUint32(this._offset, true);
this._offset += 4;
return value;

@@ -178,4 +183,4 @@ }

@handlingRangeError
readU64(): bigint {
const buf = this.readBuffer(8);
u64(): bigint {
const buf = this.buffer(8);
return toBigIntLE(buf)

@@ -185,4 +190,4 @@ }

@handlingRangeError
readU128(): bigint {
const buf = this.readBuffer(16);
u128(): bigint {
const buf = this.buffer(16);
return toBigIntLE(buf)

@@ -192,4 +197,4 @@ }

@handlingRangeError
readU256(): bigint {
const buf = this.readBuffer(32);
u256(): bigint {
const buf = this.buffer(32);
return toBigIntLE(buf)

@@ -199,13 +204,13 @@ }

@handlingRangeError
readU512(): bigint {
const buf = this.readBuffer(64);
u512(): bigint {
const buf = this.buffer(64);
return toBigIntLE(buf)
}
private readBuffer(len: number): Uint8Array {
if (this.offset + len > this.buf.byteLength) {
private buffer(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.buffer.slice(this.offset, this.offset + len);
this.offset += len;
const result = this._buf.buffer.slice(this._offset, this._offset + len);
this._offset += len;
return new Uint8Array(result);

@@ -215,5 +220,5 @@ }

@handlingRangeError
readString(): string {
const len = this.readU32();
const buf = this.readBuffer(len);
string(): string {
const len = this.u32();
const buf = this.buffer(len);
try {

@@ -228,4 +233,5 @@ // NOTE: Using TextDecoder to fail on invalid UTF-8

@handlingRangeError
readFixedArray(len: number): Uint8Array {
return new Uint8Array(this.readBuffer(len));
uint8Array(): Uint8Array {
const len = this.u32();
return new Uint8Array(this.buffer(len));
}

@@ -235,3 +241,3 @@

readArray(fn: any): any[] {
const len = this.readU32();
const len = this.u32();
const result = Array<any>();

@@ -238,0 +244,0 @@ for (let i = 0; i < len; ++i) {

@@ -19,6 +19,2 @@ import {

function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
export function serializeField(

@@ -28,3 +24,3 @@ fieldName: string,

fieldType: any, // A simple type of a CustomField
writer: any
writer: BinaryWriter
) {

@@ -38,3 +34,3 @@ try {

if (fieldType instanceof OptionKind) {
writer.writeU8(0);
writer.u8(0);
} else {

@@ -45,7 +41,11 @@ throw new BorshError(`Trying to serialize a null value to field "${fieldName}" which is not allowed since the field is not decorated with "option(...)" but "${typeof fieldType === 'function' && fieldType?.name ? fieldType?.name : fieldType}". Most likely you have forgotten to assign this value before serializing`)

else if (fieldType instanceof OptionKind) {
writer.writeU8(1);
writer.u8(1);
serializeField(fieldName, value, fieldType.elementType, writer);
}
else if (fieldType === Uint8Array) {
const arr = value as Uint8Array;
writer.uint8Array(arr)
}
else if (typeof fieldType === "string") {
writer[`write${capitalizeFirstLetter(fieldType)}`](value);
(writer as any)[fieldType](value);
} else if (

@@ -64,3 +64,3 @@ fieldType instanceof VecKind ||

} else {
writer.writeU32(len); // For dynamically sized array we write the size as u32 according to specification
writer.u32(len); // For dynamically sized array we write the size as u32 according to specification
}

@@ -110,10 +110,10 @@ for (let i = 0; i < len; i++) {

if (typeof index === "number") {
writer.writeU8(index);
writer.u8(index);
} else if (Array.isArray(index)) {
index.forEach((i) => {
writer.writeU8(i);
writer.u8(i);
});
}
else { // is string
writer.writeString(index);
writer.string(index);
}

@@ -150,5 +150,9 @@ }

if (typeof fieldType === "string") {
return (reader as any)[`read${capitalizeFirstLetter(fieldType)}`]();
return (reader as any)[fieldType]();
}
if (fieldType === Uint8Array) {
return reader.uint8Array()
}
if (fieldType instanceof VecKind || fieldType instanceof FixedArrayKind) {

@@ -158,3 +162,3 @@ let len =

? fieldType.length
: reader.readU32();
: reader.u32();
let arr = new Array(len);

@@ -171,3 +175,3 @@ for (let i = 0; i < len; i++) {

if (fieldType instanceof OptionKind) {
const option = reader.readU8();
const option = reader.u8();
if (option) {

@@ -207,10 +211,10 @@ return deserializeField(

if (typeof index === "number") {
reader.readU8();
reader.u8();
} else if (Array.isArray(index)) {
for (const _ of index) {
reader.readU8();
reader.u8();
}
}
else { // string
reader.readString();
reader.string();
}

@@ -249,3 +253,3 @@ }

if (!variantsIndex) {
variantsIndex = [reader.readU8()];
variantsIndex = [reader.u8()];
}

@@ -262,3 +266,3 @@ if (variantIndex == variantsIndex[0]) {

while (variantsIndex.length < variantIndex.length) {
variantsIndex.push(reader.readU8());
variantsIndex.push(reader.u8());
}

@@ -279,3 +283,3 @@ }

if (variantString == undefined) {
variantString = reader.readString();
variantString = reader.string();
}

@@ -348,5 +352,5 @@ // Compare variants is just string compare

const result = deserializeStruct(classType, reader);
if (!unchecked && reader.offset !== buffer.byteOffset + buffer.length) {
if (!unchecked && reader._offset !== buffer.byteOffset + buffer.length) {
throw new BorshError(
`Unexpected ${buffer.length - reader.offset
`Unexpected ${buffer.length - reader._offset
} bytes after deserialized data`

@@ -679,10 +683,10 @@ );

if (typeof variant === 'string') {
writer.writeString(variant)
writer.string(variant)
}
else if (typeof variant === 'number') {
writer.writeU8(variant)
writer.u8(variant)
}
else if (Array.isArray(variant)) {
variant.forEach((v) => {
writer.writeU8(v)
writer.u8(v)
})

@@ -689,0 +693,0 @@ }

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

| CustomField<any>
| WrappedType;
| WrappedType
| Uint8Array
export type SimpleField = { type: FieldType; index?: number };

@@ -51,0 +53,0 @@ export interface CustomField<T> extends OverrideType<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

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