@chainsafe/ssz
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -0,1 +1,13 @@ | ||
## 0.8.3 (2021-05-04) | ||
## Features | ||
- Improve hexToString performance ([106991](https://github.com/chainsafe/ssz/commit/106991)) | ||
## Chores | ||
- Update as-sha256 & persistent-merkle-tree ([212927](https://github.com/chainsafe/ssz/commit/212927)) | ||
- Use for of instead of forEach ([e195df](https://github.com/chainsafe/ssz/commit/e195df)) | ||
- Add whitespace ([516421](https://github.com/chainsafe/ssz/commit/516421)) | ||
## 0.8.2 (2021-04-05) | ||
@@ -2,0 +14,0 @@ |
@@ -26,3 +26,3 @@ import { Json, ObjectLike } from "../../interface"; | ||
struct_convertToTree(value: T): Tree; | ||
struct_getPropertyNames(): (string | number)[]; | ||
struct_getPropertyNames(): string[]; | ||
bytes_getVariableOffsets(target: Uint8Array): [number, number][]; | ||
@@ -29,0 +29,0 @@ tree_defaultNode(): Node; |
@@ -49,5 +49,7 @@ "use strict"; | ||
const obj = {}; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
obj[fieldName] = fieldType.struct_defaultValue(); | ||
}); | ||
} | ||
return obj; | ||
@@ -58,3 +60,4 @@ } | ||
let s = 0; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
if (fieldType.hasVariableSerializedLength()) { | ||
@@ -65,3 +68,4 @@ s += fieldType.struct_getSerializedLength(value[fieldName]) + 4; | ||
} | ||
}); | ||
} | ||
return s; | ||
@@ -83,3 +87,3 @@ } | ||
struct_assertValidValue(value) { | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
try { | ||
@@ -91,3 +95,3 @@ // @ts-ignore | ||
} | ||
}); | ||
} | ||
} | ||
@@ -105,5 +109,7 @@ | ||
const newValue = {}; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
newValue[fieldName] = fieldType.struct_clone(value[fieldName]); | ||
}); | ||
} | ||
return newValue; | ||
@@ -139,3 +145,4 @@ } | ||
let offsetIndex = 0; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType], i) => { | ||
for (const [i, [fieldName, fieldType]] of Object.entries(this.fields).entries()) { | ||
try { | ||
@@ -166,3 +173,3 @@ const fieldSize = fixedSizes[i]; | ||
} | ||
}); | ||
} | ||
@@ -190,3 +197,4 @@ if (offsets.length > 1) { | ||
let fixedIndex = offset; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
if (fieldType.hasVariableSerializedLength()) { | ||
@@ -201,3 +209,4 @@ // write offset | ||
} | ||
}); | ||
} | ||
return variableIndex; | ||
@@ -218,3 +227,4 @@ } | ||
const value = {}; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
const expectedCase = options ? options.case : null; | ||
@@ -228,3 +238,4 @@ const expectedFieldName = (0, _json.toExpectedCase)(fieldName, expectedCase); | ||
value[fieldName] = fieldType.fromJson(data[expectedFieldName], options); | ||
}); | ||
} | ||
return value; | ||
@@ -236,5 +247,7 @@ } | ||
const expectedCase = options ? options.case : null; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType]) => { | ||
for (const [fieldName, fieldType] of Object.entries(this.fields)) { | ||
data[(0, _json.toExpectedCase)(fieldName, expectedCase)] = fieldType.toJson(value[fieldName], options); | ||
}); | ||
} | ||
return data; | ||
@@ -261,2 +274,3 @@ } | ||
bytes_getVariableOffsets(target) { | ||
const types = Object.values(this.fields); | ||
const offsets = []; // variable-sized values can be interspersed with fixed-sized values | ||
@@ -271,3 +285,4 @@ // variable-sized value indices are serialized as offsets, indices deeper in the byte array | ||
let variableIndex = 0; | ||
Object.values(this.fields).forEach((fieldType, i) => { | ||
for (const [i, fieldType] of types.entries()) { | ||
if (fieldType.hasVariableSerializedLength()) { | ||
@@ -288,6 +303,8 @@ const offset = fixedSection.getUint32(currentIndex, true); | ||
} | ||
}); | ||
} | ||
variableOffsets.push(target.length); | ||
variableIndex = 0; | ||
Object.values(this.fields).forEach((fieldType, i) => { | ||
for (const [i, fieldType] of types.entries()) { | ||
if (fieldType.hasVariableSerializedLength()) { | ||
@@ -303,3 +320,4 @@ if (variableOffsets[variableIndex] > variableOffsets[variableIndex + 1]) { | ||
} | ||
}); | ||
} | ||
return offsets; | ||
@@ -324,3 +342,4 @@ } | ||
const value = {}; | ||
Object.entries(this.fields).forEach(([fieldName, fieldType], i) => { | ||
for (const [i, [fieldName, fieldType]] of Object.entries(this.fields).entries()) { | ||
if (!(0, _abstract.isCompositeType)(fieldType)) { | ||
@@ -333,3 +352,4 @@ const chunk = this.tree_getRootAtChunkIndex(target, i); | ||
} | ||
}); | ||
} | ||
return value; | ||
@@ -340,3 +360,4 @@ } | ||
let s = 0; | ||
Object.values(this.fields).forEach((fieldType, i) => { | ||
for (const [i, fieldType] of Object.values(this.fields).entries()) { | ||
if (fieldType.hasVariableSerializedLength()) { | ||
@@ -347,3 +368,4 @@ s += fieldType.tree_getSerializedLength(this.tree_getSubtreeAtChunkIndex(target, i)) + 4; | ||
} | ||
}); | ||
} | ||
return s; | ||
@@ -355,3 +377,4 @@ } | ||
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start)); | ||
Object.values(this.fields).forEach((fieldType, i) => { | ||
for (const [i, fieldType] of Object.values(this.fields).entries()) { | ||
const [currentOffset, nextOffset] = offsets[i]; | ||
@@ -369,3 +392,4 @@ | ||
} | ||
}); | ||
} | ||
return target; | ||
@@ -372,0 +396,0 @@ } |
@@ -197,3 +197,5 @@ "use strict"; | ||
let newLength; | ||
values.forEach(value => newLength = this.tree_pushSingle(target, value)); | ||
for (const value of values) newLength = this.tree_pushSingle(target, value); | ||
return newLength; | ||
@@ -404,3 +406,5 @@ } | ||
let newLength; | ||
values.forEach(value => newLength = this.tree_pushSingle(target, value)); | ||
for (const value of values) newLength = this.tree_pushSingle(target, value); | ||
return newLength; | ||
@@ -407,0 +411,0 @@ } |
import { ByteVector } from "../interface"; | ||
export declare function toHexString(target: Uint8Array | ByteVector): string; | ||
export declare function fromHexString(data: string): Uint8Array; | ||
export declare function toHexString(bytes: Uint8Array | ByteVector): string; | ||
export declare function fromHexString(hex: string): Uint8Array; | ||
export declare function byteArrayEquals(a: Uint8Array, b: Uint8Array): boolean; | ||
export declare function getByteBits(target: Uint8Array, offset: number): boolean[]; |
@@ -10,19 +10,40 @@ "use strict"; | ||
exports.getByteBits = getByteBits; | ||
// Caching this info costs about ~1000 bytes and speeds up toHexString() by x6 | ||
const hexByByte = []; | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
function toHexString(target) { | ||
return "0x" + [...target].map(b => b.toString(16).padStart(2, "0")).join(""); | ||
function toHexString(bytes) { | ||
let hex = "0x"; | ||
for (const byte of bytes) { | ||
if (!hexByByte[byte]) { | ||
hexByByte[byte] = byte < 16 ? "0" + byte.toString(16) : byte.toString(16); | ||
} | ||
hex += hexByByte[byte]; | ||
} | ||
return hex; | ||
} | ||
function fromHexString(data) { | ||
if (typeof data !== "string") { | ||
function fromHexString(hex) { | ||
if (typeof hex !== "string") { | ||
throw new Error("Expected hex string to be a string"); | ||
} | ||
if (data.length % 2 !== 0) { | ||
if (hex.startsWith("0x")) { | ||
hex = hex.slice(2); | ||
} | ||
if (hex.length % 2 !== 0) { | ||
throw new Error("Expected an even number of characters"); | ||
} | ||
data = data.replace("0x", ""); | ||
return new Uint8Array(data.match(/.{1,2}/g).map(b => parseInt(b, 16))); | ||
const bytes = []; | ||
for (let i = 0, len = hex.length; i < len; i += 2) { | ||
const byte = parseInt(hex.slice(i, i + 2), 16); | ||
bytes.push(byte); | ||
} | ||
return new Uint8Array(bytes); | ||
} | ||
@@ -29,0 +50,0 @@ |
@@ -7,3 +7,3 @@ { | ||
"homepage": "https://github.com/chainsafe/ssz", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"main": "lib/index.js", | ||
@@ -34,4 +34,4 @@ "files": [ | ||
"dependencies": { | ||
"@chainsafe/as-sha256": "^0.2.0", | ||
"@chainsafe/persistent-merkle-tree": "^0.3.0", | ||
"@chainsafe/as-sha256": "^0.2.1", | ||
"@chainsafe/persistent-merkle-tree": "^0.3.1", | ||
"case": "^1.6.3" | ||
@@ -38,0 +38,0 @@ }, |
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
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
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
435321
4516