@dao-xyz/borsh
Advanced tools
Comparing version 5.0.4 to 5.1.0
@@ -23,2 +23,6 @@ import { PrimitiveType, SmallIntegerType } from './types.js'; | ||
static u512(value: number | bigint, writer: BinaryWriter): void; | ||
f32(value: number): void; | ||
static f32(value: number, writer: BinaryWriter): void; | ||
f64(value: number): void; | ||
static f64(value: number, writer: BinaryWriter): void; | ||
string(str: string): void; | ||
@@ -55,2 +59,6 @@ static string(str: string, writer: BinaryWriter): void; | ||
static u512(reader: BinaryReader): bigint; | ||
f32(): number; | ||
static f32(reader: BinaryReader): number; | ||
f64(): number; | ||
static f64(reader: BinaryReader): number; | ||
string(): string; | ||
@@ -57,0 +65,0 @@ static string(reader: BinaryReader): string; |
@@ -10,2 +10,3 @@ "use strict"; | ||
const utf8_1 = __importDefault(require("@protobufjs/utf8")); | ||
const float_1 = require("@protobufjs/float"); | ||
const allocUnsafeFn = () => { | ||
@@ -99,2 +100,26 @@ if (globalThis.Buffer) { | ||
} | ||
f32(value) { | ||
return BinaryWriter.f32(value, this); | ||
} | ||
static f32(value, writer) { | ||
if (Number.isNaN(value)) { | ||
throw new error_js_1.BorshError("NaN is not supported for f32"); | ||
} | ||
let offset = writer.totalSize; | ||
const last = writer._writes; | ||
writer._writes = () => { last(); (0, float_1.writeFloatLE)(value, writer._buf, offset); }; | ||
writer.totalSize += 4; | ||
} | ||
f64(value) { | ||
return BinaryWriter.f64(value, this); | ||
} | ||
static f64(value, writer) { | ||
if (Number.isNaN(value)) { | ||
throw new error_js_1.BorshError("NaN is not supported for f64"); | ||
} | ||
let offset = writer.totalSize; | ||
const last = writer._writes; | ||
writer._writes = () => { last(); (0, float_1.writeDoubleLE)(value, writer._buf, offset); }; | ||
writer.totalSize += 8; | ||
} | ||
string(str) { | ||
@@ -196,2 +221,8 @@ return BinaryWriter.string(str, this); | ||
} | ||
else if (encoding === 'f32') { | ||
return BinaryWriter.f32; | ||
} | ||
else if (encoding === 'f64') { | ||
return BinaryWriter.f64; | ||
} | ||
else if (encoding === 'string') { | ||
@@ -279,2 +310,24 @@ return BinaryWriter.string; | ||
} | ||
f32() { | ||
return BinaryReader.f32(this); | ||
} | ||
static f32(reader) { | ||
const value = (0, float_1.readFloatLE)(reader._buf, reader._offset); | ||
reader._offset += 4; | ||
if (Number.isNaN(value)) { | ||
throw new error_js_1.BorshError("Recieved NaN reading f32"); | ||
} | ||
return value; | ||
} | ||
f64() { | ||
return BinaryReader.f64(this); | ||
} | ||
static f64(reader) { | ||
const value = (0, float_1.readDoubleLE)(reader._buf, reader._offset); | ||
reader._offset += 8; | ||
if (Number.isNaN(value)) { | ||
throw new error_js_1.BorshError("Recieved NaN reading f64"); | ||
} | ||
return value; | ||
} | ||
string() { | ||
@@ -335,2 +388,8 @@ return BinaryReader.string(this); | ||
} | ||
else if (encoding === 'f32') { | ||
return BinaryReader.f32; | ||
} | ||
else if (encoding === 'f64') { | ||
return BinaryReader.f64; | ||
} | ||
else { | ||
@@ -337,0 +396,0 @@ throw new Error("Unsupported encoding: " + encoding); |
@@ -21,3 +21,4 @@ import { BinaryReader, BinaryWriter } from "./binary.js"; | ||
export type IntegerType = SmallIntegerType | "u64" | "u128" | "u256" | "u512"; | ||
export type PrimitiveType = "bool" | "string" | IntegerType; | ||
export type FloatType = 'f32' | 'f64'; | ||
export type PrimitiveType = "bool" | "string" | IntegerType | FloatType; | ||
export type FieldType = PrimitiveType | Constructor<any> | AbstractType<any> | CustomField<any> | WrappedType | Uint8Array | StringType; | ||
@@ -24,0 +25,0 @@ export type SimpleField = { |
@@ -23,2 +23,6 @@ import { PrimitiveType, SmallIntegerType } from './types.js'; | ||
static u512(value: number | bigint, writer: BinaryWriter): void; | ||
f32(value: number): void; | ||
static f32(value: number, writer: BinaryWriter): void; | ||
f64(value: number): void; | ||
static f64(value: number, writer: BinaryWriter): void; | ||
string(str: string): void; | ||
@@ -55,2 +59,6 @@ static string(str: string, writer: BinaryWriter): void; | ||
static u512(reader: BinaryReader): bigint; | ||
f32(): number; | ||
static f32(reader: BinaryReader): number; | ||
f64(): number; | ||
static f64(reader: BinaryReader): number; | ||
string(): string; | ||
@@ -57,0 +65,0 @@ static string(reader: BinaryReader): string; |
import { toBigIntLE, writeBufferLEBigInt, writeUInt32LE, readUInt32LE, readUInt16LE, writeUInt16LE, readBigUInt64LE, readUIntLE, checkInt } from './bigint.js'; | ||
import { BorshError } from "./error.js"; | ||
import utf8 from '@protobufjs/utf8'; | ||
import { readFloatLE, writeFloatLE, readDoubleLE, writeDoubleLE } from '@protobufjs/float'; | ||
const allocUnsafeFn = () => { | ||
@@ -92,2 +93,26 @@ if (globalThis.Buffer) { | ||
} | ||
f32(value) { | ||
return BinaryWriter.f32(value, this); | ||
} | ||
static f32(value, writer) { | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("NaN is not supported for f32"); | ||
} | ||
let offset = writer.totalSize; | ||
const last = writer._writes; | ||
writer._writes = () => { last(); writeFloatLE(value, writer._buf, offset); }; | ||
writer.totalSize += 4; | ||
} | ||
f64(value) { | ||
return BinaryWriter.f64(value, this); | ||
} | ||
static f64(value, writer) { | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("NaN is not supported for f64"); | ||
} | ||
let offset = writer.totalSize; | ||
const last = writer._writes; | ||
writer._writes = () => { last(); writeDoubleLE(value, writer._buf, offset); }; | ||
writer.totalSize += 8; | ||
} | ||
string(str) { | ||
@@ -189,2 +214,8 @@ return BinaryWriter.string(str, this); | ||
} | ||
else if (encoding === 'f32') { | ||
return BinaryWriter.f32; | ||
} | ||
else if (encoding === 'f64') { | ||
return BinaryWriter.f64; | ||
} | ||
else if (encoding === 'string') { | ||
@@ -271,2 +302,24 @@ return BinaryWriter.string; | ||
} | ||
f32() { | ||
return BinaryReader.f32(this); | ||
} | ||
static f32(reader) { | ||
const value = readFloatLE(reader._buf, reader._offset); | ||
reader._offset += 4; | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("Recieved NaN reading f32"); | ||
} | ||
return value; | ||
} | ||
f64() { | ||
return BinaryReader.f64(this); | ||
} | ||
static f64(reader) { | ||
const value = readDoubleLE(reader._buf, reader._offset); | ||
reader._offset += 8; | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("Recieved NaN reading f64"); | ||
} | ||
return value; | ||
} | ||
string() { | ||
@@ -327,2 +380,8 @@ return BinaryReader.string(this); | ||
} | ||
else if (encoding === 'f32') { | ||
return BinaryReader.f32; | ||
} | ||
else if (encoding === 'f64') { | ||
return BinaryReader.f64; | ||
} | ||
else { | ||
@@ -329,0 +388,0 @@ throw new Error("Unsupported encoding: " + encoding); |
@@ -21,3 +21,4 @@ import { BinaryReader, BinaryWriter } from "./binary.js"; | ||
export type IntegerType = SmallIntegerType | "u64" | "u128" | "u256" | "u512"; | ||
export type PrimitiveType = "bool" | "string" | IntegerType; | ||
export type FloatType = 'f32' | 'f64'; | ||
export type PrimitiveType = "bool" | "string" | IntegerType | FloatType; | ||
export type FieldType = PrimitiveType | Constructor<any> | AbstractType<any> | CustomField<any> | WrappedType | Uint8Array | StringType; | ||
@@ -24,0 +25,0 @@ export type SimpleField = { |
{ | ||
"name": "@dao-xyz/borsh", | ||
"version": "5.0.4", | ||
"version": "5.1.0", | ||
"readme": "README.md", | ||
@@ -58,4 +58,5 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README", | ||
"dependencies": { | ||
"@protobufjs/utf8": "^1.1.0" | ||
"@protobufjs/utf8": "^1.1.0", | ||
"@protobufjs/float": "^1.0.2" | ||
} | ||
} |
@@ -78,3 +78,3 @@ # Borsh TS | ||
## Examples of schema generation using decorators | ||
For more examples, see the [tests](./src/__tests__index.test.ts). | ||
For more examples, see the [tests](./src/__tests__/index.test.ts). | ||
@@ -81,0 +81,0 @@ **Enum, with 2 variants** |
@@ -577,2 +577,87 @@ import { BinaryReader, BinaryWriter } from "../binary.js"; | ||
}); | ||
describe("f32", () => { | ||
class Struct { | ||
@field({ type: "f32" }) | ||
public a: number; | ||
constructor(a: number) { | ||
this.a = a; | ||
} | ||
} | ||
test("f32 decimal", () => { | ||
const instance = new Struct(3.123); | ||
const buf = serialize(instance); | ||
expect(new Uint8Array(buf)).toEqual(new Uint8Array([59, 223, 71, 64])); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(3.122999906539917); | ||
}); | ||
test("f32 min", () => { | ||
const instance = new Struct(-3.40282347e38); | ||
const buf = serialize(instance); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(-3.4028234663852886e38); | ||
}); | ||
test("f32 max", () => { | ||
const instance = new Struct(3.40282347e38); | ||
const buf = serialize(instance); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(3.4028234663852886e38); | ||
}); | ||
test("f32 nan ser", () => { | ||
expect(() => serialize(new Struct(Number.NaN))).toThrowError(BorshError); | ||
}); | ||
test("f32 nan der", () => { | ||
expect(() => | ||
deserialize(new Uint8Array([0, 0, 192, 127]), Struct) | ||
).toThrowError(BorshError); | ||
}); | ||
}); | ||
describe("f64", () => { | ||
class Struct { | ||
@field({ type: "f64" }) | ||
public a: number; | ||
constructor(a: number) { | ||
this.a = a; | ||
} | ||
} | ||
test("f64 decimal", () => { | ||
const instance = new Struct(3.123); | ||
const buf = serialize(instance); | ||
expect(new Uint8Array(buf)).toEqual( | ||
new Uint8Array([150, 67, 139, 108, 231, 251, 8, 64]) | ||
); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(3.123); | ||
}); | ||
test("f64 min", () => { | ||
const instance = new Struct(-1.7976931348623157e308); | ||
const buf = serialize(instance); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(-1.7976931348623157e308); | ||
}); | ||
test("f64 max", () => { | ||
const instance = new Struct(1.7976931348623157e308); | ||
const buf = serialize(instance); | ||
const deserialized = deserialize(buf, Struct); | ||
expect(deserialized.a).toEqual(1.7976931348623157e308); | ||
}); | ||
test("f64 nan ser", () => { | ||
expect(() => serialize(new Struct(Number.NaN))).toThrowError(BorshError); | ||
}); | ||
test("f64 nan der", () => { | ||
expect(() => | ||
deserialize(new Uint8Array([0, 0, 192, 127]), Struct) | ||
).toThrowError(BorshError); | ||
}); | ||
}); | ||
}); | ||
@@ -579,0 +664,0 @@ describe("enum", () => { |
@@ -5,2 +5,3 @@ import { toBigIntLE, writeBufferLEBigInt, writeUInt32LE, readUInt32LE, readUInt16LE, writeUInt16LE, readBigUInt64LE, readUIntLE, checkInt } from './bigint.js'; | ||
import { PrimitiveType, SmallIntegerType } from './types.js'; | ||
import { readFloatLE, writeFloatLE, readDoubleLE, writeDoubleLE } from '@protobufjs/float' | ||
@@ -75,2 +76,3 @@ const allocUnsafeFn = (): (len: number) => Uint8Array => { | ||
public u64(value: number | bigint) { | ||
@@ -125,2 +127,30 @@ return BinaryWriter.u64(value, this) | ||
public f32(value: number) { | ||
return BinaryWriter.f32(value, this) | ||
} | ||
public static f32(value: number, writer: BinaryWriter) { | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("NaN is not supported for f32") | ||
} | ||
let offset = writer.totalSize; | ||
const last = writer._writes; | ||
writer._writes = () => { last(); writeFloatLE(value, writer._buf, offset) } | ||
writer.totalSize += 4; | ||
} | ||
public f64(value: number) { | ||
return BinaryWriter.f64(value, this) | ||
} | ||
public static f64(value: number, writer: BinaryWriter) { | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("NaN is not supported for f64") | ||
} | ||
let offset = writer.totalSize; | ||
const last = writer._writes; | ||
writer._writes = () => { last(); writeDoubleLE(value, writer._buf, offset) } | ||
writer.totalSize += 8; | ||
} | ||
public string(str: string) { | ||
@@ -236,2 +266,8 @@ return BinaryWriter.string(str, this) | ||
} | ||
else if (encoding === 'f32') { | ||
return BinaryWriter.f32 | ||
} | ||
else if (encoding === 'f64') { | ||
return BinaryWriter.f64 | ||
} | ||
else if (encoding === 'string') { | ||
@@ -245,7 +281,2 @@ return BinaryWriter.string | ||
public finalize(): Uint8Array { | ||
@@ -255,3 +286,2 @@ this._buf = allocUnsafe(this.totalSize); | ||
return this._buf; | ||
} | ||
@@ -348,2 +378,31 @@ } | ||
f32(): number { | ||
return BinaryReader.f32(this) | ||
} | ||
static f32(reader: BinaryReader): number { | ||
const value = readFloatLE(reader._buf, reader._offset) | ||
reader._offset += 4; | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("Recieved NaN reading f32") | ||
} | ||
return value; | ||
} | ||
f64(): number { | ||
return BinaryReader.f64(this) | ||
} | ||
static f64(reader: BinaryReader): number { | ||
const value = readDoubleLE(reader._buf, reader._offset) | ||
reader._offset += 8; | ||
if (Number.isNaN(value)) { | ||
throw new BorshError("Recieved NaN reading f64") | ||
} | ||
return value; | ||
} | ||
string(): string { | ||
@@ -405,2 +464,8 @@ return BinaryReader.string(this); | ||
} | ||
else if (encoding === 'f32') { | ||
return BinaryReader.f32 | ||
} | ||
else if (encoding === 'f64') { | ||
return BinaryReader.f64 | ||
} | ||
else { | ||
@@ -407,0 +472,0 @@ throw new Error("Unsupported encoding: " + encoding) |
@@ -49,5 +49,8 @@ import { BinaryReader, BinaryWriter } from "./binary.js"; | ||
export type FloatType = 'f32' | 'f64'; | ||
export type PrimitiveType = "bool" | ||
| "string" | ||
| IntegerType | ||
| FloatType | ||
@@ -54,0 +57,0 @@ export type FieldType = |
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
327271
6119
2
+ Added@protobufjs/float@^1.0.2
+ Added@protobufjs/float@1.0.2(transitive)