Comparing version 0.4.0 to 0.5.0
@@ -249,2 +249,9 @@ import BN from 'bn.js'; | ||
writer.writeFixedArray(value); | ||
} else if(fieldType.length === 2 && typeof fieldType[1] === 'number' ) { | ||
if (value.length !== fieldType[1]) { | ||
throw new BorshError(`Expecting byte array of length ${fieldType[1]}, but got ${value.length} bytes`); | ||
} | ||
for(let i = 0; i < fieldType[1]; i++) { | ||
serializeField(schema, null, value[i], fieldType[0], writer); | ||
} | ||
} else { | ||
@@ -277,3 +284,7 @@ writer.writeArray(value, (item: any) => { serializeField(schema, fieldName, item, fieldType[0], writer); }); | ||
function serializeStruct(schema: Schema, obj: any, writer: any) { | ||
function serializeStruct(schema: Schema, obj: any, writer: BinaryWriter) { | ||
if (typeof obj.borshSerialize === 'function') { | ||
obj.borshSerialize(writer); | ||
return; | ||
} | ||
const structSchema = schema.get(obj.constructor); | ||
@@ -319,5 +330,11 @@ if (!structSchema) { | ||
return reader.readFixedArray(fieldType[0]); | ||
} else if(typeof fieldType[1] === 'number') { | ||
const arr = []; | ||
for(let i = 0; i < fieldType[1]; i++) { | ||
arr.push(deserializeField(schema, null, fieldType[0], reader)); | ||
} | ||
return arr; | ||
} else { | ||
return reader.readArray(() => deserializeField(schema, fieldName, fieldType[0], reader)); | ||
} | ||
return reader.readArray(() => deserializeField(schema, fieldName, fieldType[0], reader)); | ||
} | ||
@@ -349,2 +366,6 @@ | ||
function deserializeStruct(schema: Schema, classType: any, reader: BinaryReader) { | ||
if (typeof classType.borshDeserialize === 'function') { | ||
return classType.borshDeserialize(reader); | ||
} | ||
const structSchema = schema.get(classType); | ||
@@ -351,0 +372,0 @@ if (!structSchema) { |
@@ -14,2 +14,16 @@ const borsh = require('../../lib/index'); | ||
class Serializable { | ||
constructor(data) { | ||
this.data = data; | ||
} | ||
static borshDeserialize(reader) { | ||
return new Serializable(reader.readU8()); | ||
} | ||
borshSerialize(writer) { | ||
writer.writeU8(this.data); | ||
} | ||
} | ||
test('serialize object', async () => { | ||
@@ -72,5 +86,60 @@ const value = new Test({ x: 255, y: 20, z: '123', q: [1, 2, 3] }); | ||
test('serialize/deserialize with class methods', () => { | ||
const item = new Serializable(10); | ||
const buf = borsh.serialize(null, item); | ||
const newValue = borsh.deserialize(null, Serializable, buf); | ||
expect(newValue).toEqual(item); | ||
}); | ||
test('serialize/deserialize fixed array', () => { | ||
const value = new Test({ | ||
a: ['hello', 'world'] | ||
}); | ||
const schema = new Map([[Test, { | ||
kind: 'struct', | ||
fields: [ | ||
['a', ['string', 2]] | ||
] | ||
}]]); | ||
const buf = borsh.serialize(schema, value); | ||
const deserializedValue = borsh.deserialize(schema, Test, buf); | ||
expect(buf).toEqual(Buffer.from([5, 0, 0, 0, 104, 101, 108, 108, 111, 5, 0, 0, 0, 119, 111, 114, 108, 100])); | ||
expect(deserializedValue.a).toEqual(['hello', 'world']); | ||
}); | ||
test('errors serializing fixed array of wrong size', () => { | ||
const value = new Test({ | ||
a: ['hello', 'world', 'you'] | ||
}); | ||
const schema = new Map([[Test, { | ||
kind: 'struct', | ||
fields: [ | ||
['a', ['string', 2]] | ||
] | ||
}]]); | ||
expect(() => borsh.serialize(schema, value)).toThrow('Expecting byte array of length 2, but got 3 bytes'); | ||
}); | ||
test('errors serializing fixed array of wrong type', () => { | ||
const value = new Test({ | ||
a: [244, 34] | ||
}); | ||
const schema = new Map([[Test, { | ||
kind: 'struct', | ||
fields: [ | ||
['a', ['string', 2]] | ||
] | ||
}]]); | ||
expect(() => borsh.serialize(schema, value)).toThrow('The first argument must be of type string'); | ||
}); | ||
test('baseEncode string test', async () => { | ||
const encodedValue = borsh.baseEncode("244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM"); | ||
const expectedValue = "HKk9gqNj4xb4rLdJuzT5zzJbLa4vHBdYCxQT9H99csQh6nz3Hfpqn4jtWA92"; | ||
const encodedValue = borsh.baseEncode('244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM'); | ||
const expectedValue = 'HKk9gqNj4xb4rLdJuzT5zzJbLa4vHBdYCxQT9H99csQh6nz3Hfpqn4jtWA92'; | ||
expect(encodedValue).toEqual(expectedValue); | ||
@@ -84,3 +153,3 @@ }); | ||
test('baseDecode test', async () => { | ||
const value = "HKk9gqNj4xb4rLdJu"; | ||
const value = 'HKk9gqNj4xb4rLdJu'; | ||
const expectedDecodedArray = [3, 96, 254, 84, 10, 240, 93, 199, 52, 244, 164, 240, 6]; | ||
@@ -87,0 +156,0 @@ const expectedBuffer = Buffer.from(expectedDecodedArray); |
@@ -263,2 +263,10 @@ "use strict"; | ||
} | ||
else if (fieldType.length === 2 && typeof fieldType[1] === 'number') { | ||
if (value.length !== fieldType[1]) { | ||
throw new BorshError(`Expecting byte array of length ${fieldType[1]}, but got ${value.length} bytes`); | ||
} | ||
for (let i = 0; i < fieldType[1]; i++) { | ||
serializeField(schema, null, value[i], fieldType[0], writer); | ||
} | ||
} | ||
else { | ||
@@ -295,2 +303,6 @@ writer.writeArray(value, (item) => { serializeField(schema, fieldName, item, fieldType[0], writer); }); | ||
function serializeStruct(schema, obj, writer) { | ||
if (typeof obj.borshSerialize === 'function') { | ||
obj.borshSerialize(writer); | ||
return; | ||
} | ||
const structSchema = schema.get(obj.constructor); | ||
@@ -337,3 +349,12 @@ if (!structSchema) { | ||
} | ||
return reader.readArray(() => deserializeField(schema, fieldName, fieldType[0], reader)); | ||
else if (typeof fieldType[1] === 'number') { | ||
const arr = []; | ||
for (let i = 0; i < fieldType[1]; i++) { | ||
arr.push(deserializeField(schema, null, fieldType[0], reader)); | ||
} | ||
return arr; | ||
} | ||
else { | ||
return reader.readArray(() => deserializeField(schema, fieldName, fieldType[0], reader)); | ||
} | ||
} | ||
@@ -357,2 +378,5 @@ if (fieldType.kind === 'option') { | ||
function deserializeStruct(schema, classType, reader) { | ||
if (typeof classType.borshDeserialize === 'function') { | ||
return classType.borshDeserialize(reader); | ||
} | ||
const structSchema = schema.get(classType); | ||
@@ -359,0 +383,0 @@ if (!structSchema) { |
{ | ||
"name": "borsh", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Binary Object Representation Serializer for Hashing", | ||
@@ -18,3 +18,3 @@ "main": "lib/index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/nearprotocol/borsh.git" | ||
"url": "git+https://github.com/near/borsh-js.git" | ||
}, | ||
@@ -32,8 +32,9 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/nearprotocol/borsh/issues" | ||
"url": "https://github.com/near/borsh-js/issues" | ||
}, | ||
"homepage": "https://github.com/nearprotocol/borsh#readme", | ||
"homepage": "https://github.com/near/borsh-js#readme", | ||
"devDependencies": { | ||
"@types/babel__core": "^7.1.2", | ||
"@types/babel__template": "^7.0.2", | ||
"@types/bn.js": "^5.1.0", | ||
"@types/node": "^12.7.3", | ||
@@ -50,4 +51,3 @@ "@typescript-eslint/eslint-plugin": "^2.18.0", | ||
"dependencies": { | ||
"@types/bn.js": "^4.11.5", | ||
"bn.js": "^5.0.0", | ||
"bn.js": "^5.2.0", | ||
"bs58": "^4.0.0", | ||
@@ -54,0 +54,0 @@ "text-encoding-utf-8": "^1.0.2" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85115
3
1751
12
- Removed@types/bn.js@^4.11.5
- Removed@types/bn.js@4.11.6(transitive)
- Removed@types/node@22.9.0(transitive)
- Removedundici-types@6.19.8(transitive)
Updatedbn.js@^5.2.0