@ckb-lumos/codec
Advanced tools
Comparing version 0.18.0 to 0.19.0-alpha.0
@@ -1,3 +0,3 @@ | ||
import { AnyCodec, BytesCodec, FixedBytesCodec, PackParam, UnpackResult } from "./base"; | ||
export declare const createFixedHexBytesCodec: (byteLength: number) => FixedBytesCodec<string>; | ||
import { AnyCodec, BytesCodec, BytesLike, FixedBytesCodec, PackParam, UnpackResult } from "./base"; | ||
export declare function createFixedHexBytesCodec(byteLength: number): FixedBytesCodec<string, BytesLike>; | ||
/** | ||
@@ -11,7 +11,7 @@ * placeholder codec, generally used as a placeholder | ||
*/ | ||
export declare const Bytes: BytesCodec<string, string>; | ||
export declare const BytesOpt: import("./molecule/layout").OptionCodec<BytesCodec<string, string>>; | ||
export declare const BytesVec: BytesCodec<string[], string[]>; | ||
export declare const Byte32: FixedBytesCodec<string, string>; | ||
export declare const Byte32Vec: BytesCodec<string[], string[]>; | ||
export declare const Bytes: BytesCodec<string, BytesLike>; | ||
export declare const BytesOpt: import("./molecule/layout").OptionCodec<BytesCodec<string, BytesLike>>; | ||
export declare const BytesVec: BytesCodec<string[], BytesLike[]>; | ||
export declare const Byte32: FixedBytesCodec<string, BytesLike>; | ||
export declare const Byte32Vec: BytesCodec<string[], BytesLike[]>; | ||
export declare function WitnessArgsOf<LockCodec extends AnyCodec, InputTypeCodec extends AnyCodec, OutputTypeCodec extends AnyCodec>(payload: { | ||
@@ -18,0 +18,0 @@ lock: LockCodec; |
@@ -8,3 +8,3 @@ "use strict"; | ||
exports.WitnessArgsOf = WitnessArgsOf; | ||
exports.createFixedHexBytesCodec = void 0; | ||
exports.createFixedHexBytesCodec = createFixedHexBytesCodec; | ||
@@ -17,7 +17,9 @@ var _base = require("./base"); | ||
const createFixedHexBytesCodec = byteLength => (0, _base.createFixedBytesCodec)({ | ||
byteLength, | ||
pack: hex => (0, _bytes.bytify)(hex), | ||
unpack: buf => (0, _bytes.hexify)(buf) | ||
}); | ||
function createFixedHexBytesCodec(byteLength) { | ||
return (0, _base.createFixedBytesCodec)({ | ||
byteLength, | ||
pack: hex => (0, _bytes.bytify)(hex), | ||
unpack: buf => (0, _bytes.hexify)(buf) | ||
}); | ||
} | ||
/** | ||
@@ -35,6 +37,5 @@ * placeholder codec, generally used as a placeholder | ||
exports.createFixedHexBytesCodec = createFixedHexBytesCodec; | ||
const Bytes = (0, _molecule.byteVecOf)({ | ||
pack: hex => (0, _bytes.bytify)(hex), | ||
unpack: buf => (0, _bytes.hexify)(buf) | ||
pack: _bytes.bytify, | ||
unpack: _bytes.hexify | ||
}); | ||
@@ -41,0 +42,0 @@ exports.Bytes = Bytes; |
import { BytesLike } from "./base"; | ||
export declare function bytifyRawString(rawString: string): Uint8Array; | ||
/** | ||
@@ -7,2 +8,3 @@ * convert a {@link BytesLike} to an Uint8Array | ||
export declare function bytify(bytesLike: BytesLike): Uint8Array; | ||
export declare function equal(a: BytesLike, b: BytesLike): boolean; | ||
/** | ||
@@ -9,0 +11,0 @@ * convert a {@link BytesLike} to an even length hex string prefixed with "0x" |
@@ -7,3 +7,5 @@ "use strict"; | ||
exports.bytify = bytify; | ||
exports.bytifyRawString = bytifyRawString; | ||
exports.concat = concat; | ||
exports.equal = equal; | ||
exports.hexify = hexify; | ||
@@ -13,2 +15,15 @@ | ||
function bytifyRawString(rawString) { | ||
(0, _utils.assertUtf8String)(rawString); | ||
const buffer = new ArrayBuffer(rawString.length); | ||
const view = new DataView(buffer); | ||
for (let i = 0; i < rawString.length; i++) { | ||
const c = rawString.charCodeAt(i); | ||
view.setUint8(i, c); | ||
} | ||
return new Uint8Array(buffer); | ||
} | ||
function bytifyHex(hex) { | ||
@@ -48,2 +63,18 @@ (0, _utils.assertHexString)(hex); | ||
} | ||
function equal(a, b) { | ||
const aUint8Array = bytify(a); | ||
const bUint8Array = bytify(b); | ||
return equalUint8Array(aUint8Array, bUint8Array); | ||
} | ||
function equalUint8Array(a, b) { | ||
if (a.length !== b.length) return false; | ||
for (let i = a.length; -1 < i; i -= 1) { | ||
if (a[i] !== b[i]) return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
@@ -64,3 +95,11 @@ * convert a {@link BytesLike} to an even length hex string prefixed with "0x" | ||
function concat(...bytesLikes) { | ||
return Uint8Array.from(bytesLikes.flatMap(bytes => Array.from(bytify(bytes)))); | ||
const unmerged = bytesLikes.map(bytify); | ||
const totalSize = unmerged.reduce((size, item) => size + item.length, 0); | ||
const merged = new Uint8Array(totalSize); | ||
let offset = 0; | ||
unmerged.forEach(item => { | ||
merged.set(item, offset); | ||
offset += item.length; | ||
}); | ||
return merged; | ||
} // export function split(bytes: BytesLike, points: number[]): Uint8Array[] { | ||
@@ -67,0 +106,0 @@ // const u8vec = bytify(bytes); |
@@ -7,3 +7,2 @@ export type { PackResult, UnpackResult, PackParam, UnpackParam, BytesLike, AnyCodec, } from "./base"; | ||
export * as molecule from "./molecule"; | ||
export * as blockchain from "./blockchain"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -12,6 +12,5 @@ "use strict"; | ||
number: true, | ||
molecule: true, | ||
blockchain: true | ||
molecule: true | ||
}; | ||
exports.bytes = exports.blockchain = void 0; | ||
exports.bytes = void 0; | ||
Object.defineProperty(exports, "createBytesCodec", { | ||
@@ -65,6 +64,2 @@ enumerable: true, | ||
var _blockchain = _interopRequireWildcard(require("./blockchain")); | ||
exports.blockchain = _blockchain; | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
@@ -71,0 +66,0 @@ |
@@ -13,3 +13,3 @@ import { BytesCodec, FixedBytesCodec } from "../base"; | ||
*/ | ||
export declare function byteOf<T>(codec: BytesCodec<T>): FixedBytesCodec<T>; | ||
export declare function byteOf<Packed, Packable = Packed>(codec: BytesCodec<Packed, Packable>): FixedBytesCodec<Packed, Packable>; | ||
/** | ||
@@ -19,3 +19,3 @@ * a helper function to create custom codec of `vector Bytes <byte>` | ||
*/ | ||
export declare const byteVecOf: <T>(codec: BytesCodec<T, T>) => BytesCodec<T, T>; | ||
export declare function byteVecOf<Packed, Packable = Packed>(codec: BytesCodec<Packed, Packable>): BytesCodec<Packed, Packable>; | ||
//# sourceMappingURL=helper.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
exports.byteOf = byteOf; | ||
exports.byteVecOf = void 0; | ||
exports.byteVecOf = byteVecOf; | ||
@@ -48,3 +48,3 @@ var _utils = require("../utils"); | ||
const byteVecOf = codec => { | ||
function byteVecOf(codec) { | ||
return (0, _base.createBytesCodec)({ | ||
@@ -69,5 +69,3 @@ pack(unpacked) { | ||
}); | ||
}; | ||
exports.byteVecOf = byteVecOf; | ||
} | ||
//# sourceMappingURL=helper.js.map |
@@ -12,3 +12,3 @@ /** | ||
*/ | ||
import type { BytesCodec, Fixed, FixedBytesCodec, UnpackResult } from "../base"; | ||
import type { BytesCodec, Fixed, FixedBytesCodec, PackParam, UnpackResult } from "../base"; | ||
declare type NullableKeys<O extends Record<string, unknown>> = { | ||
@@ -23,7 +23,9 @@ [K in keyof O]-?: [O[K] & (undefined | null)] extends [never] ? never : K; | ||
[key in keyof T]: UnpackResult<T[key]>; | ||
}>, PartialNullable<{ | ||
[key in keyof T]: PackParam<T[key]>; | ||
}>>; | ||
export interface OptionCodec<T extends BytesCodec> extends BytesCodec<UnpackResult<T> | undefined> { | ||
pack: (packable?: UnpackResult<T>) => Uint8Array; | ||
pack: (packable?: PackParam<T>) => Uint8Array; | ||
} | ||
export declare type ArrayCodec<T extends BytesCodec> = BytesCodec<Array<UnpackResult<T>>>; | ||
export declare type ArrayCodec<T extends BytesCodec> = BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>; | ||
export declare type UnionCodec<T extends Record<string, BytesCodec>> = BytesCodec<{ | ||
@@ -34,2 +36,7 @@ [key in keyof T]: { | ||
}; | ||
}[keyof T], { | ||
[key in keyof T]: { | ||
type: key; | ||
value: PackParam<T[key]>; | ||
}; | ||
}[keyof T]>; | ||
@@ -36,0 +43,0 @@ export declare function array<T extends FixedBytesCodec>(itemCodec: T, itemCount: number): ArrayCodec<T> & Fixed; |
@@ -251,6 +251,12 @@ "use strict"; | ||
const type = obj.type; | ||
/* c8 ignore next */ | ||
if (typeof type !== "string") { | ||
throw new Error(`Invalid type in union, type must be a string`); | ||
} | ||
const fieldIndex = fields.indexOf(type); | ||
if (fieldIndex === -1) { | ||
throw new Error(`Unknown union type: ${obj.type}`); | ||
throw new Error(`Unknown union type: ${String(obj.type)}`); | ||
} | ||
@@ -257,0 +263,0 @@ |
export declare function assertHexDecimal(str: string, byteLength?: number): void; | ||
export declare function assertHexString(str: string, byteLength?: number): void; | ||
export declare function assertUtf8String(str: string): void; | ||
export declare function assertBufferLength(buf: { | ||
@@ -4,0 +5,0 @@ byteLength: number; |
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.assertMinBufferLength = assertMinBufferLength; | ||
exports.assertUtf8String = assertUtf8String; | ||
exports.isObjectLike = isObjectLike; | ||
@@ -58,2 +59,12 @@ const HEX_DECIMAL_REGEX = /^0x([0-9a-fA-F])+$/; | ||
function assertUtf8String(str) { | ||
for (let i = 0; i < str.length; i++) { | ||
const c = str.charCodeAt(i); | ||
if (c > 0xff) { | ||
throw new Error("Invalid UTF-8 raw string!"); | ||
} | ||
} | ||
} | ||
function assertBufferLength(buf, length) { | ||
@@ -60,0 +71,0 @@ if (buf.byteLength !== length) { |
{ | ||
"name": "@ckb-lumos/codec", | ||
"version": "0.18.0", | ||
"version": "0.19.0-alpha.0", | ||
"description": "Make your own molecule binding in JavaScript(TypeScript)", | ||
@@ -33,3 +33,3 @@ "author": "", | ||
"dependencies": { | ||
"@ckb-lumos/bi": "0.18.0" | ||
"@ckb-lumos/bi": "^0.19.0-alpha.0" | ||
}, | ||
@@ -53,3 +53,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "732d60fdd2f4cbb7401df9b67c6295ea79c99180" | ||
"gitHead": "665f4e985dc1444a27e2e1db499d0ea64cf4d5b8" | ||
} |
@@ -178,4 +178,4 @@ # @ckb-lumos/codec | ||
lock: BytesOpt, // Lock args | ||
input_type: BytesOpt, // Type args for input | ||
output_type: BytesOpt, // Type args for output | ||
inputType: BytesOpt, // Type args for input | ||
outputType: BytesOpt, // Type args for output | ||
} | ||
@@ -182,0 +182,0 @@ |
@@ -14,6 +14,6 @@ import { | ||
export const createFixedHexBytesCodec = ( | ||
export function createFixedHexBytesCodec( | ||
byteLength: number | ||
): FixedBytesCodec<string> => | ||
createFixedBytesCodec<string>({ | ||
): FixedBytesCodec<string, BytesLike> { | ||
return createFixedBytesCodec({ | ||
byteLength, | ||
@@ -23,2 +23,3 @@ pack: (hex) => bytify(hex), | ||
}); | ||
} | ||
@@ -36,6 +37,3 @@ /** | ||
// vector Bytes <byte> | ||
export const Bytes = byteVecOf<string>({ | ||
pack: (hex) => bytify(hex), | ||
unpack: (buf) => hexify(buf), | ||
}); | ||
export const Bytes = byteVecOf({ pack: bytify, unpack: hexify }); | ||
@@ -42,0 +40,0 @@ export const BytesOpt = option(Bytes); |
import { BytesLike } from "./base"; | ||
import { assertHexString } from "./utils"; | ||
import { assertHexString, assertUtf8String } from "./utils"; | ||
export function bytifyRawString(rawString: string): Uint8Array { | ||
assertUtf8String(rawString); | ||
const buffer = new ArrayBuffer(rawString.length); | ||
const view = new DataView(buffer); | ||
for (let i = 0; i < rawString.length; i++) { | ||
const c = rawString.charCodeAt(i); | ||
view.setUint8(i, c); | ||
} | ||
return new Uint8Array(buffer); | ||
} | ||
function bytifyHex(hex: string): Uint8Array { | ||
@@ -38,2 +51,15 @@ assertHexString(hex); | ||
export function equal(a: BytesLike, b: BytesLike): boolean { | ||
const aUint8Array = bytify(a); | ||
const bUint8Array = bytify(b); | ||
return equalUint8Array(aUint8Array, bUint8Array); | ||
} | ||
function equalUint8Array(a: Uint8Array, b: Uint8Array): boolean { | ||
if (a.length !== b.length) return false; | ||
for (let i = a.length; -1 < i; i -= 1) { | ||
if (a[i] !== b[i]) return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
@@ -54,5 +80,14 @@ * convert a {@link BytesLike} to an even length hex string prefixed with "0x" | ||
export function concat(...bytesLikes: BytesLike[]): Uint8Array { | ||
return Uint8Array.from( | ||
bytesLikes.flatMap((bytes) => Array.from(bytify(bytes))) | ||
); | ||
const unmerged = bytesLikes.map(bytify); | ||
const totalSize = unmerged.reduce((size, item) => size + item.length, 0); | ||
const merged = new Uint8Array(totalSize); | ||
let offset = 0; | ||
unmerged.forEach((item) => { | ||
merged.set(item, offset); | ||
offset += item.length; | ||
}); | ||
return merged; | ||
} | ||
@@ -59,0 +94,0 @@ |
@@ -32,10 +32,9 @@ import { | ||
type ObjectCodecShape = Record<string, AnyCodec>; | ||
export type ObjectCodec< | ||
Shape extends ObjectCodecShape = ObjectCodecShape | ||
> = Codec< | ||
{ [key in keyof Shape]: PackResult<Shape[key]> }, | ||
{ [key in keyof Shape]: UnpackResult<Shape[key]> }, | ||
{ [key in keyof Shape]: PackParam<Shape[key]> }, | ||
{ [key in keyof Shape]: UnpackParam<Shape[key]> } | ||
>; | ||
export type ObjectCodec<Shape extends ObjectCodecShape = ObjectCodecShape> = | ||
Codec< | ||
{ [key in keyof Shape]: PackResult<Shape[key]> }, | ||
{ [key in keyof Shape]: UnpackResult<Shape[key]> }, | ||
{ [key in keyof Shape]: PackParam<Shape[key]> }, | ||
{ [key in keyof Shape]: UnpackParam<Shape[key]> } | ||
>; | ||
@@ -42,0 +41,0 @@ /** |
@@ -17,2 +17,1 @@ export type { | ||
export * as molecule from "./molecule"; | ||
export * as blockchain from "./blockchain"; |
@@ -30,3 +30,5 @@ import { assertBufferLength, assertMinBufferLength } from "../utils"; | ||
*/ | ||
export function byteOf<T>(codec: BytesCodec<T>): FixedBytesCodec<T> { | ||
export function byteOf<Packed, Packable = Packed>( | ||
codec: BytesCodec<Packed, Packable> | ||
): FixedBytesCodec<Packed, Packable> { | ||
return byteArrayOf({ ...codec, byteLength: 1 }); | ||
@@ -39,3 +41,5 @@ } | ||
*/ | ||
export const byteVecOf = <T>(codec: BytesCodec<T>): BytesCodec<T> => { | ||
export function byteVecOf<Packed, Packable = Packed>( | ||
codec: BytesCodec<Packed, Packable> | ||
): BytesCodec<Packed, Packable> { | ||
return createBytesCodec({ | ||
@@ -55,2 +59,2 @@ pack(unpacked) { | ||
}); | ||
}; | ||
} |
@@ -13,3 +13,9 @@ /** | ||
import type { BytesCodec, Fixed, FixedBytesCodec, UnpackResult } from "../base"; | ||
import type { | ||
BytesCodec, | ||
Fixed, | ||
FixedBytesCodec, | ||
PackParam, | ||
UnpackResult, | ||
} from "../base"; | ||
import { createBytesCodec, createFixedBytesCodec, isFixedCodec } from "../base"; | ||
@@ -28,7 +34,8 @@ import { Uint32LE } from "../number"; | ||
type PartialNullable<O extends Record<string, unknown>> = | ||
& Partial<Pick<O, NullableKeys<O>>> | ||
& Pick<O, NonNullableKeys<O>>; | ||
& Partial<Pick<O, NullableKeys<O>>> | ||
& Pick<O, NonNullableKeys<O>>; | ||
export type ObjectCodec<T extends Record<string, BytesCodec>> = BytesCodec< | ||
PartialNullable<{ [key in keyof T]: UnpackResult<T[key]> }> | ||
PartialNullable<{ [key in keyof T]: UnpackResult<T[key]> }>, | ||
PartialNullable<{ [key in keyof T]: PackParam<T[key]> }> | ||
>; | ||
@@ -38,11 +45,13 @@ | ||
extends BytesCodec<UnpackResult<T> | undefined> { | ||
pack: (packable?: UnpackResult<T>) => Uint8Array; | ||
pack: (packable?: PackParam<T>) => Uint8Array; | ||
} | ||
export type ArrayCodec<T extends BytesCodec> = BytesCodec< | ||
Array<UnpackResult<T>> | ||
Array<UnpackResult<T>>, | ||
Array<PackParam<T>> | ||
>; | ||
export type UnionCodec<T extends Record<string, BytesCodec>> = BytesCodec< | ||
{ [key in keyof T]: { type: key; value: UnpackResult<T[key]> } }[keyof T] | ||
{ [key in keyof T]: { type: key; value: UnpackResult<T[key]> } }[keyof T], | ||
{ [key in keyof T]: { type: key; value: PackParam<T[key]> } }[keyof T] | ||
>; | ||
@@ -109,5 +118,5 @@ | ||
unpack(buf) { | ||
const result = {} as PartialNullable< | ||
{ [key in keyof T]: UnpackResult<T[key]> } | ||
>; | ||
const result = {} as PartialNullable<{ | ||
[key in keyof T]: UnpackResult<T[key]>; | ||
}>; | ||
let offset = 0; | ||
@@ -255,5 +264,5 @@ | ||
if (totalSize <= 4 || fields.length === 0) { | ||
return {} as PartialNullable< | ||
{ [key in keyof T]: UnpackResult<T[key]> } | ||
>; | ||
return {} as PartialNullable<{ | ||
[key in keyof T]: UnpackResult<T[key]>; | ||
}>; | ||
} else { | ||
@@ -273,5 +282,5 @@ const offsets = fields.map((_, index) => | ||
} | ||
return obj as PartialNullable< | ||
{ [key in keyof T]: UnpackResult<T[key]> } | ||
>; | ||
return obj as PartialNullable<{ | ||
[key in keyof T]: UnpackResult<T[key]>; | ||
}>; | ||
} | ||
@@ -289,5 +298,11 @@ }, | ||
const type = obj.type; | ||
/* c8 ignore next */ | ||
if (typeof type !== "string") { | ||
throw new Error(`Invalid type in union, type must be a string`); | ||
} | ||
const fieldIndex = fields.indexOf(type); | ||
if (fieldIndex === -1) { | ||
throw new Error(`Unknown union type: ${obj.type}`); | ||
throw new Error(`Unknown union type: ${String(obj.type)}`); | ||
} | ||
@@ -294,0 +309,0 @@ const packedFieldIndex = Uint32LE.pack(fieldIndex); |
@@ -47,2 +47,11 @@ const HEX_DECIMAL_REGEX = /^0x([0-9a-fA-F])+$/; | ||
export function assertUtf8String(str: string): void { | ||
for (let i = 0; i < str.length; i++) { | ||
const c = str.charCodeAt(i); | ||
if (c > 0xff) { | ||
throw new Error("Invalid UTF-8 raw string!"); | ||
} | ||
} | ||
} | ||
export function assertBufferLength( | ||
@@ -49,0 +58,0 @@ buf: { byteLength: number }, |
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
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
147235
2097
+ Added@ckb-lumos/bi@0.19.0(transitive)
- Removed@ckb-lumos/bi@0.18.0(transitive)