@mysten/bcs
Advanced tools
Comparing version 0.0.0-experimental-20250128173824 to 0.0.0-experimental-20250131013137
# Change Log | ||
## 0.0.0-experimental-20250128173824 | ||
## 0.0.0-experimental-20250131013137 | ||
### Minor Changes | ||
- 95b1ea5: Add bcs.byteVector for parsing a vector<u8> into a Uint8Array | ||
## 1.3.0 | ||
### Minor Changes | ||
- 10e2724: Allow BcsType.transform to omit input or output transform | ||
@@ -8,0 +14,0 @@ |
@@ -61,2 +61,9 @@ import type { BcsTypeOptions } from './bcs-type.js'; | ||
/** | ||
* Creates a BcsType representing a variable length byte array | ||
* | ||
* @example | ||
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3] | ||
*/ | ||
byteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>>; | ||
/** | ||
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded | ||
@@ -63,0 +70,0 @@ * @example |
@@ -182,2 +182,35 @@ "use strict"; | ||
/** | ||
* Creates a BcsType representing a variable length byte array | ||
* | ||
* @example | ||
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3] | ||
*/ | ||
byteVector(options) { | ||
return new import_bcs_type.BcsType({ | ||
name: `bytesVector`, | ||
read: (reader) => { | ||
const length = reader.readULEB(); | ||
return reader.readBytes(length); | ||
}, | ||
write: (value, writer) => { | ||
const array = new Uint8Array(value); | ||
writer.writeULEB(array.length); | ||
for (let i = 0; i < array.length; i++) { | ||
writer.write8(array[i] ?? 0); | ||
} | ||
}, | ||
...options, | ||
serializedSize: (value) => { | ||
const length = "length" in value ? value.length : null; | ||
return length == null ? null : (0, import_uleb.ulebEncode)(length).length + length; | ||
}, | ||
validate: (value) => { | ||
options?.validate?.(value); | ||
if (!value || typeof value !== "object" || !("length" in value)) { | ||
throw new TypeError(`Expected array, found ${typeof value}`); | ||
} | ||
} | ||
}); | ||
}, | ||
/** | ||
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded | ||
@@ -184,0 +217,0 @@ * @example |
@@ -61,2 +61,9 @@ import type { BcsTypeOptions } from './bcs-type.js'; | ||
/** | ||
* Creates a BcsType representing a variable length byte array | ||
* | ||
* @example | ||
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3] | ||
*/ | ||
byteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>>; | ||
/** | ||
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded | ||
@@ -63,0 +70,0 @@ * @example |
@@ -167,2 +167,35 @@ import { | ||
/** | ||
* Creates a BcsType representing a variable length byte array | ||
* | ||
* @example | ||
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3] | ||
*/ | ||
byteVector(options) { | ||
return new BcsType({ | ||
name: `bytesVector`, | ||
read: (reader) => { | ||
const length = reader.readULEB(); | ||
return reader.readBytes(length); | ||
}, | ||
write: (value, writer) => { | ||
const array = new Uint8Array(value); | ||
writer.writeULEB(array.length); | ||
for (let i = 0; i < array.length; i++) { | ||
writer.write8(array[i] ?? 0); | ||
} | ||
}, | ||
...options, | ||
serializedSize: (value) => { | ||
const length = "length" in value ? value.length : null; | ||
return length == null ? null : ulebEncode(length).length + length; | ||
}, | ||
validate: (value) => { | ||
options?.validate?.(value); | ||
if (!value || typeof value !== "object" || !("length" in value)) { | ||
throw new TypeError(`Expected array, found ${typeof value}`); | ||
} | ||
} | ||
}); | ||
}, | ||
/** | ||
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded | ||
@@ -169,0 +202,0 @@ * @example |
{ | ||
"name": "@mysten/bcs", | ||
"version": "0.0.0-experimental-20250128173824", | ||
"version": "0.0.0-experimental-20250131013137", | ||
"description": "BCS - Canonical Binary Serialization implementation for JavaScript", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -236,4 +236,4 @@ # BCS - Binary Canonical Serialization | ||
If the format you use in your code is different from the format expected for BCS serialization, | ||
you can use the `transform` API to map between the types you use in your application, and the types | ||
If the format you use in your code is different from the format expected for BCS serialization, you | ||
can use the `transform` API to map between the types you use in your application, and the types | ||
needed for serialization. | ||
@@ -240,0 +240,0 @@ |
@@ -182,2 +182,37 @@ // Copyright (c) Mysten Labs, Inc. | ||
/** | ||
* Creates a BcsType representing a variable length byte array | ||
* | ||
* @example | ||
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3] | ||
*/ | ||
byteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>) { | ||
return new BcsType<Uint8Array, Iterable<number>>({ | ||
name: `bytesVector`, | ||
read: (reader) => { | ||
const length = reader.readULEB(); | ||
return reader.readBytes(length); | ||
}, | ||
write: (value, writer) => { | ||
const array = new Uint8Array(value); | ||
writer.writeULEB(array.length); | ||
for (let i = 0; i < array.length; i++) { | ||
writer.write8(array[i] ?? 0); | ||
} | ||
}, | ||
...options, | ||
serializedSize: (value) => { | ||
const length = 'length' in value ? (value.length as number) : null; | ||
return length == null ? null : ulebEncode(length).length + length; | ||
}, | ||
validate: (value) => { | ||
options?.validate?.(value); | ||
if (!value || typeof value !== 'object' || !('length' in value)) { | ||
throw new TypeError(`Expected array, found ${typeof value}`); | ||
} | ||
}, | ||
}); | ||
}, | ||
/** | ||
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded | ||
@@ -184,0 +219,0 @@ * @example |
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
358879
5076