New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ckb-lumos/codec

Package Overview
Dependencies
Maintainers
3
Versions
147
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ckb-lumos/codec - npm Package Compare versions

Comparing version 0.20.0 to 0.21.0-next.0

20

lib/base.d.ts

@@ -5,10 +5,10 @@ export interface Codec<Packed, Unpacked, Packable = Unpacked, Unpackable = Packed> {

}
export declare type AnyCodec = Codec<any, any>;
export declare type PackResult<T extends AnyCodec> = T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packed : never;
export declare type PackParam<T extends AnyCodec> = T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packable : never;
export declare type UnpackResult<T extends AnyCodec> = T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpacked : never;
export declare type UnpackParam<T extends AnyCodec> = T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpackable : never;
export declare type Uint8ArrayCodec<Unpacked = any, Packable = Unpacked> = Codec<Uint8Array, Unpacked, Packable>;
export declare type BytesLike = ArrayLike<number> | ArrayBuffer | string;
export declare type BytesCodec<Unpacked = any, Packable = Unpacked> = Codec<Uint8Array, Unpacked, Packable, BytesLike>;
export type AnyCodec = Codec<any, any>;
export type PackResult<T extends AnyCodec> = T extends Codec<infer Packed, any, any, any> ? Packed : never;
export type PackParam<T extends AnyCodec> = T extends Codec<any, any, infer Packable, any> ? Packable : never;
export type UnpackResult<T extends AnyCodec> = T extends Codec<any, infer Unpacked, any, any> ? Unpacked : never;
export type UnpackParam<T extends AnyCodec> = T extends Codec<any, any, any, infer Unpackable> ? Unpackable : never;
export type Uint8ArrayCodec<Unpacked = any, Packable = Unpacked> = Codec<Uint8Array, Unpacked, Packable>;
export type BytesLike = ArrayLike<number> | ArrayBuffer | string;
export type BytesCodec<Unpacked = any, Packable = Unpacked> = Codec<Uint8Array, Unpacked, Packable, BytesLike>;
/**

@@ -19,7 +19,7 @@ * This function helps to create a codec that can

export declare function createBytesCodec<Unpacked, Packable = Unpacked>(codec: Uint8ArrayCodec<Unpacked, Packable>): BytesCodec<Unpacked, Packable>;
export declare type Fixed = {
export type Fixed = {
readonly __isFixedCodec__: true;
readonly byteLength: number;
};
export declare type FixedBytesCodec<Unpacked = any, Packable = Unpacked> = BytesCodec<Unpacked, Packable> & Fixed;
export type FixedBytesCodec<Unpacked = any, Packable = Unpacked> = BytesCodec<Unpacked, Packable> & Fixed;
export declare function isFixedCodec<T>(codec: BytesCodec<T>): codec is FixedBytesCodec<T>;

@@ -26,0 +26,0 @@ export declare function createFixedBytesCodec<Unpacked, Packable = Unpacked>(codec: Uint8ArrayCodec<Unpacked, Packable> & {

@@ -9,7 +9,4 @@ "use strict";

exports.isFixedCodec = isFixedCodec;
var _utils = require("./utils");
var _bytes = require("./bytes");
/* eslint-disable @typescript-eslint/no-explicit-any */

@@ -27,7 +24,5 @@

}
function isFixedCodec(codec) {
return (0, _utils.isObjectLike)(codec) && !!codec.__isFixedCodec__;
}
function createFixedBytesCodec(codec) {

@@ -34,0 +29,0 @@ const byteLength = codec.byteLength;

@@ -13,5 +13,5 @@ import { AnyCodec, BytesCodec, BytesLike, FixedBytesCodec, PackParam, UnpackResult } from "./base";

export declare const BytesOpt: import("./molecule/layout").OptionCodec<BytesCodec<string, BytesLike>>;
export declare const BytesVec: BytesCodec<string[], BytesLike[]>;
export declare const BytesVec: import("./molecule/layout").ArrayCodec<BytesCodec<string, BytesLike>>;
export declare const Byte32: FixedBytesCodec<string, BytesLike>;
export declare const Byte32Vec: BytesCodec<string[], BytesLike[]>;
export declare const Byte32Vec: import("./molecule/layout").ArrayCodec<FixedBytesCodec<string, BytesLike>>;
export declare function WitnessArgsOf<LockCodec extends AnyCodec, InputTypeCodec extends AnyCodec, OutputTypeCodec extends AnyCodec>(payload: {

@@ -43,6 +43,6 @@ lock: LockCodec;

}, {
lock?: string | ArrayBuffer | ArrayLike<number> | undefined;
input_type?: string | ArrayBuffer | ArrayLike<number> | undefined;
output_type?: string | ArrayBuffer | ArrayLike<number> | undefined;
lock?: BytesLike | undefined;
input_type?: BytesLike | undefined;
output_type?: BytesLike | undefined;
}>;
//# sourceMappingURL=blockchain.d.ts.map

@@ -9,9 +9,5 @@ "use strict";

exports.createFixedHexBytesCodec = createFixedHexBytesCodec;
var _base = require("./base");
var _bytes = require("./bytes");
var _molecule = require("./molecule");
function createFixedHexBytesCodec(byteLength) {

@@ -24,2 +20,3 @@ return (0, _base.createFixedBytesCodec)({

}
/**

@@ -34,5 +31,4 @@ * placeholder codec, generally used as a placeholder

// export const UnusedOpt = option(Unknown);
// vector Bytes <byte>
const Bytes = (0, _molecule.byteVecOf)({

@@ -51,3 +47,2 @@ pack: _bytes.bytify,

exports.Byte32Vec = Byte32Vec;
function WitnessArgsOf(payload) {

@@ -60,3 +55,2 @@ return (0, _molecule.table)({

}
const HexifyCodec = (0, _base.createBytesCodec)({

@@ -66,2 +60,3 @@ pack: _bytes.bytify,

});
/**

@@ -75,3 +70,2 @@ *

*/
const WitnessArgs = WitnessArgsOf({

@@ -78,0 +72,0 @@ lock: HexifyCodec,

@@ -11,5 +11,3 @@ "use strict";

exports.hexify = hexify;
var _utils = require("./utils");
function bytifyRawString(rawString) {

@@ -19,3 +17,2 @@ (0, _utils.assertUtf8String)(rawString);

const view = new DataView(buffer);
for (let i = 0; i < rawString.length; i++) {

@@ -25,6 +22,4 @@ const c = rawString.charCodeAt(i);

}
return new Uint8Array(buffer);
}
function bytifyHex(hex) {

@@ -34,19 +29,15 @@ (0, _utils.assertHexString)(hex);

const uint8s = [];
for (let i = 0; i < hex.length; i += 2) {
uint8s.push(parseInt(hex.substr(i, 2), 16));
}
return Uint8Array.from(uint8s);
}
function bytifyArrayLike(xs) {
const isValidU8Vec = Array.from(xs).every(v => v >= 0 && v <= 255);
if (!isValidU8Vec) {
throw new Error("invalid ArrayLike, all elements must be 0-255");
}
return Uint8Array.from(xs);
}
/**

@@ -56,4 +47,2 @@ * convert a {@link BytesLike} to an Uint8Array

*/
function bytify(bytesLike) {

@@ -66,3 +55,2 @@ if (bytesLike instanceof ArrayBuffer) return new Uint8Array(bytesLike);

}
function equal(a, b) {

@@ -73,10 +61,7 @@ const aUint8Array = bytify(a);

}
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;

@@ -91,4 +76,2 @@ }

*/
function hexify(buf) {

@@ -98,3 +81,2 @@ const hex = Array.from(bytify(buf)).map(b => b.toString(16).padStart(2, "0")).join("");

}
function concat(...bytesLikes) {

@@ -110,3 +92,5 @@ const unmerged = bytesLikes.map(bytify);

return merged;
} // export function split(bytes: BytesLike, points: number[]): Uint8Array[] {
}
// export function split(bytes: BytesLike, points: number[]): Uint8Array[] {
// const u8vec = bytify(bytes);

@@ -113,0 +97,0 @@ // const result: Uint8Array[] = [];

export declare const CODEC_OPTIONAL_PATH = "__lc_option__";
declare type CodecOptionalPath = typeof CODEC_OPTIONAL_PATH;
type CodecOptionalPath = typeof CODEC_OPTIONAL_PATH;
export declare class CodecBaseParseError extends Error {

@@ -4,0 +4,0 @@ expectedType: string;

@@ -8,6 +8,8 @@ "use strict";

exports.isCodecExecuteError = isCodecExecuteError;
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
// lc for lumos codec
const CODEC_OPTIONAL_PATH = "__lc_option__";
exports.CODEC_OPTIONAL_PATH = CODEC_OPTIONAL_PATH;
class CodecBaseParseError extends Error {

@@ -18,8 +20,5 @@ constructor(message, expectedType) {

}
}
exports.CodecBaseParseError = CodecBaseParseError;
const CODEC_EXECUTE_ERROR_NAME = "CodecExecuteError";
function isCodecExecuteError(error) {

@@ -29,17 +28,13 @@ if (!(error instanceof Error)) return false;

}
/**
* This Error class can collect CodecBaseParseError, and put an human-readable error
*/
class CodecExecuteError extends Error {
name = CODEC_EXECUTE_ERROR_NAME;
constructor(origin) {
super();
_defineProperty(this, "name", CODEC_EXECUTE_ERROR_NAME);
_defineProperty(this, "keys", []);
this.origin = origin;
}
keys = [];
updateKey(key) {

@@ -49,6 +44,4 @@ this.keys.push(key);

}
getPackErrorMessage() {
var _this$origin$stack;
const reducer = (acc, cur, index) => {

@@ -62,6 +55,4 @@ if (cur === CODEC_OPTIONAL_PATH) {

}
return acc + cur;
};
const path = this.keys.reduceRight(reducer, "input");

@@ -72,6 +63,4 @@ return `Expect type ${this.origin.expectedType} at ${path} but got error: ${this.origin.message}

}
}
exports.CodecExecuteError = CodecExecuteError;
//# sourceMappingURL=error.js.map

@@ -30,4 +30,3 @@ "use strict";

});
var _nested = require("./nested");
//# sourceMappingURL=index.js.map

@@ -7,4 +7,4 @@ import { AnyCodec, Codec, PackParam, PackResult, UnpackParam, UnpackResult } from "../base";

export declare function createNullableCodec<C extends AnyCodec = AnyCodec>(codec: C): NullableCodec<C>;
declare type ObjectCodecShape = Record<string, AnyCodec>;
export declare type ObjectCodec<Shape extends ObjectCodecShape = ObjectCodecShape> = Codec<{
type ObjectCodecShape = Record<string, AnyCodec>;
export type ObjectCodec<Shape extends ObjectCodecShape = ObjectCodecShape> = Codec<{
[key in keyof Shape]: PackResult<Shape[key]>;

@@ -34,3 +34,3 @@ }, {

export declare function createObjectCodec<Shape extends ObjectCodecShape>(codecShape: Shape): ObjectCodec<Shape>;
export declare type ArrayCodec<C extends AnyCodec> = Codec<PackResult<C>[], UnpackResult<C>[], PackParam<C>[], UnpackParam<C>[]>;
export type ArrayCodec<C extends AnyCodec> = Codec<PackResult<C>[], UnpackResult<C>[], PackParam<C>[], UnpackParam<C>[]>;
export declare function createArrayCodec<C extends AnyCodec>(codec: C): ArrayCodec<C>;

@@ -37,0 +37,0 @@ /**

@@ -10,7 +10,4 @@ "use strict";

exports.enhancePack = enhancePack;
var _utils = require("../utils");
var _error = require("../error");
function createNullableCodec(codec) {

@@ -28,3 +25,2 @@ return {

}
/**

@@ -68,3 +64,2 @@ * a high-order codec that helps to organize multiple codecs together into a single object

}
function createArrayCodec(codec) {

@@ -76,2 +71,3 @@ return {

}
/**

@@ -82,4 +78,2 @@ * @param codec

*/
function enhancePack(codec, afterCodecPack, beforeCodecUnpack) {

@@ -86,0 +80,0 @@ return {

@@ -34,7 +34,4 @@ "use strict";

exports.number = exports.molecule = void 0;
var _base = require("./base");
var _highOrder = require("./high-order");
Object.keys(_highOrder).forEach(function (key) {

@@ -51,18 +48,10 @@ if (key === "default" || key === "__esModule") return;

});
var _bytes = _interopRequireWildcard(require("./bytes"));
exports.bytes = _bytes;
var _number = _interopRequireWildcard(require("./number"));
exports.number = _number;
var _molecule = _interopRequireWildcard(require("./molecule"));
exports.molecule = _molecule;
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); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
//# sourceMappingURL=index.js.map

@@ -9,11 +9,6 @@ "use strict";

exports.byteVecOf = byteVecOf;
var _utils = require("../utils");
var _bytes = require("../bytes");
var _base = require("../base");
var _number = require("../number");
/**

@@ -31,2 +26,3 @@ * a helper function to create custom codec of `array SomeType [byte; n]`

}
/**

@@ -36,9 +32,9 @@ * a helper function to create custom codec of `byte`

*/
function byteOf(codec) {
return byteArrayOf({ ...codec,
return byteArrayOf({
...codec,
byteLength: 1
});
}
/**

@@ -48,4 +44,2 @@ * a helper function to create custom codec of `vector Bytes <byte>`

*/
function byteVecOf(codec) {

@@ -55,19 +49,13 @@ return (0, _base.createBytesCodec)({

const payload = codec.pack(unpacked);
const header = _number.Uint32LE.pack(payload.byteLength);
return (0, _bytes.concat)(header, payload);
},
unpack(packed) {
(0, _utils.assertMinBufferLength)(packed, 4);
const header = _number.Uint32LE.unpack(packed.slice(0, 4));
(0, _utils.assertBufferLength)(packed.slice(4), header);
return codec.unpack(packed.slice(4));
}
});
}
//# sourceMappingURL=helper.js.map

@@ -60,6 +60,4 @@ "use strict";

});
var _helper = require("./helper");
var _layout = require("./layout");
//# sourceMappingURL=index.js.map

@@ -13,10 +13,10 @@ /**

import { BytesCodec, Fixed, FixedBytesCodec, PackParam, UnpackResult } from "../base";
declare type NullableKeys<O extends Record<string, unknown>> = {
type NullableKeys<O extends Record<string, unknown>> = {
[K in keyof O]-?: [O[K] & (undefined | null)] extends [never] ? never : K;
}[keyof O];
declare type NonNullableKeys<O extends Record<string, unknown>> = {
type NonNullableKeys<O extends Record<string, unknown>> = {
[K in keyof O]-?: [O[K] & (undefined | null)] extends [never] ? K : never;
}[keyof O];
declare type PartialNullable<O extends Record<string, unknown>> = Partial<Pick<O, NullableKeys<O>>> & Pick<O, NonNullableKeys<O>>;
export declare type ObjectCodec<T extends Record<string, BytesCodec>> = BytesCodec<PartialNullable<{
type PartialNullable<O extends Record<string, unknown>> = 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]>;

@@ -29,4 +29,4 @@ }>, PartialNullable<{

}
export declare type ArrayCodec<T extends BytesCodec> = BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>;
export declare type UnionCodec<T extends Record<string, BytesCodec>> = BytesCodec<{
export type ArrayCodec<T extends BytesCodec> = BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>;
export type UnionCodec<T extends Record<string, BytesCodec>> = BytesCodec<{
[key in keyof T]: {

@@ -33,0 +33,0 @@ type: key;

@@ -14,13 +14,7 @@ "use strict";

exports.vector = vector;
var _base = require("../base");
var _number = require("../number");
var _bytes = require("../bytes");
var _error = require("../error");
var _highOrder = require("../high-order");
/**

@@ -38,2 +32,4 @@ * | Type | Header | Body |

// prettier-ignore
/**

@@ -49,3 +45,2 @@ * The array is a fixed-size type: it has a fixed-size inner type and a fixed length.

byteLength: itemCodec.byteLength * itemCount,
pack(items) {

@@ -55,21 +50,15 @@ const itemsBuf = enhancedArrayCodec.pack(items);

},
unpack(buf) {
const result = [];
const itemLength = itemCodec.byteLength;
for (let offset = 0; offset < buf.byteLength; offset += itemLength) {
result.push(itemCodec.unpack(buf.slice(offset, offset + itemLength)));
}
return result;
}
});
}
function diff(x1, x2) {
return x1.filter(x => !x2.includes(x));
}
function checkShape(shape, fields) {

@@ -79,3 +68,2 @@ const shapeKeys = Object.keys(shape);

const missingShape = diff(fields, shapeKeys);
if (missingFields.length > 0 || missingShape.length > 0) {

@@ -85,2 +73,3 @@ throw new Error(`Invalid shape: missing fields ${missingFields.join(", ")} or shape ${missingShape.join(", ")}`);

}
/**

@@ -92,4 +81,2 @@ * Struct is a fixed-size type: all fields in struct are fixed-size and it has a fixed quantity of fields.

*/
function struct(shape, fields) {

@@ -100,3 +87,2 @@ checkShape(shape, fields);

byteLength: fields.reduce((sum, field) => sum + shape[field].byteLength, 0),
pack(obj) {

@@ -108,3 +94,2 @@ const packed = objectCodec.pack(obj);

},
unpack(buf) {

@@ -123,5 +108,5 @@ const result = {};

}
});
}
/**

@@ -131,4 +116,2 @@ * Vector with fixed size item codec

*/
function fixvec(itemCodec) {

@@ -140,3 +123,2 @@ return (0, _base.createBytesCodec)({

},
unpack(buf) {

@@ -146,10 +128,8 @@ if (buf.byteLength < 4) {

}
const itemCount = _number.Uint32LE.unpack(buf.slice(0, 4));
return array(itemCodec, itemCount).unpack(buf.slice(4));
}
});
}
/**

@@ -160,4 +140,2 @@ * Vector with dynamic size item codec

*/
function dynvec(itemCodec) {

@@ -169,3 +147,2 @@ return (0, _base.createBytesCodec)({

const packedHeader = _number.Uint32LE.pack(result.offset);
return {

@@ -181,17 +158,11 @@ header: (0, _bytes.concat)(result.header, packedHeader),

});
const packedTotalSize = _number.Uint32LE.pack(packed.header.byteLength + packed.body.byteLength + 4);
return (0, _bytes.concat)(packedTotalSize, packed.header, packed.body);
},
unpack(buf) {
const totalSize = _number.Uint32LE.unpack(buf.slice(0, 4));
if (totalSize !== buf.byteLength) {
throw new Error(`Invalid buffer size, read from header: ${totalSize}, actual: ${buf.byteLength}`);
}
const result = [];
if (totalSize <= 4) {

@@ -201,3 +172,2 @@ return result;

const offset0 = _number.Uint32LE.unpack(buf.slice(4, 8));
const itemCount = (offset0 - 4) / 4;

@@ -207,3 +177,2 @@ const offsets = new Array(itemCount).fill(1).map((_, index) => _number.Uint32LE.unpack(buf.slice(4 + index * 4, 8 + index * 4)));

const result = [];
for (let index = 0; index < offsets.length - 1; index++) {

@@ -215,9 +184,8 @@ const start = offsets[index];

}
return result;
}
}
});
}
/**

@@ -227,4 +195,2 @@ * General vector codec, if `itemCodec` is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created.

*/
function vector(itemCodec) {

@@ -234,5 +200,5 @@ if ((0, _base.isFixedCodec)(itemCodec)) {

}
return dynvec(itemCodec);
}
/**

@@ -243,4 +209,2 @@ * Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed.

*/
function table(shape, fields) {

@@ -255,5 +219,3 @@ checkShape(shape, fields);

const packedItem = packedObj[field];
const packedOffset = _number.Uint32LE.pack(result.offset);
return {

@@ -269,15 +231,10 @@ header: (0, _bytes.concat)(result.header, packedOffset),

});
const packedTotalSize = _number.Uint32LE.pack(packed.header.byteLength + packed.body.byteLength + 4);
return (0, _bytes.concat)(packedTotalSize, packed.header, packed.body);
},
unpack(buf) {
const totalSize = _number.Uint32LE.unpack(buf.slice(0, 4));
if (totalSize !== buf.byteLength) {
throw new Error(`Invalid buffer size, read from header: ${totalSize}, actual: ${buf.byteLength}`);
}
if (totalSize <= 4 || fields.length === 0) {

@@ -289,3 +246,2 @@ return {};

const obj = {};
for (let index = 0; index < offsets.length - 1; index++) {

@@ -301,9 +257,8 @@ const start = offsets[index];

}
return obj;
}
}
});
}
/**

@@ -317,4 +272,2 @@ * Union is a dynamic-size type.

*/
function union(itemCodec, fields) {

@@ -325,23 +278,17 @@ return (0, _base.createBytesCodec)({

const typeName = `Union(${fields.join(" | ")})`;
/* c8 ignore next */
if (typeof type !== "string") {
throw new _error.CodecBaseParseError(`Invalid type in union, type must be a string`, typeName);
}
const fieldIndex = fields.indexOf(type);
if (fieldIndex === -1) {
throw new _error.CodecBaseParseError(`Unknown union type: ${String(obj.type)}`, typeName);
}
const packedFieldIndex = _number.Uint32LE.pack(fieldIndex);
const packedBody = itemCodec[type].pack(obj.value);
return (0, _bytes.concat)(packedFieldIndex, packedBody);
},
unpack(buf) {
const typeIndex = _number.Uint32LE.unpack(buf.slice(0, 4));
const type = fields[typeIndex];

@@ -353,5 +300,5 @@ return {

}
});
}
/**

@@ -364,4 +311,2 @@ * Option is a dynamic-size type.

*/
function option(itemCodec) {

@@ -371,3 +316,2 @@ return (0, _base.createBytesCodec)({

const nullableCodec = (0, _highOrder.createNullableCodec)(itemCodec);
if (obj !== undefined && obj !== null) {

@@ -379,3 +323,2 @@ return nullableCodec.pack(obj);

},
unpack(buf) {

@@ -385,8 +328,6 @@ if (buf.byteLength === 0) {

}
return itemCodec.unpack(buf);
}
});
}
//# sourceMappingURL=layout.js.map

@@ -6,5 +6,3 @@ "use strict";

});
var _uint = require("./uint");
Object.keys(_uint).forEach(function (key) {

@@ -11,0 +9,0 @@ if (key === "default" || key === "__esModule") return;

@@ -7,12 +7,7 @@ "use strict";

exports.Uint8 = exports.Uint64LE = exports.Uint64BE = exports.Uint64 = exports.Uint512LE = exports.Uint512BE = exports.Uint512 = exports.Uint32LE = exports.Uint32BE = exports.Uint32 = exports.Uint256LE = exports.Uint256BE = exports.Uint256 = exports.Uint16LE = exports.Uint16BE = exports.Uint16 = exports.Uint128LE = exports.Uint128BE = exports.Uint128 = void 0;
var _bi = require("@ckb-lumos/bi");
var _base = require("../base");
var _error = require("../error");
function assertNumberRange(value, min, max, typeName) {
value = _bi.BI.from(value);
if (value.lt(min) || value.gt(max)) {

@@ -22,3 +17,2 @@ throw new _error.CodecBaseParseError(`Value must be between ${min.toString()} and ${max.toString()}, but got ${value.toString()}`, typeName);

}
function createUintNumberCodec(byteLength, littleEndian = false) {

@@ -33,27 +27,18 @@ const codec = createUintBICodec(byteLength, littleEndian);

}
const createUintBICodec = (byteLength, littleEndian = false) => {
const max = _bi.BI.from(1).shl(byteLength * 8).sub(1);
return (0, _base.createFixedBytesCodec)({
byteLength,
pack(biIsh) {
let endianType = littleEndian ? "LE" : "BE";
if (byteLength <= 1) {
endianType = "";
}
const typeName = `Uint${byteLength * 8}${endianType}`;
if (typeof biIsh === "number" && !Number.isSafeInteger(biIsh)) {
throw new _error.CodecBaseParseError(`${biIsh} is not a safe integer`, typeName);
}
let num = _bi.BI.from(biIsh);
assertNumberRange(num, 0, max, typeName);
const result = new DataView(new ArrayBuffer(byteLength));
for (let i = 0; i < byteLength; i++) {

@@ -65,14 +50,9 @@ if (littleEndian) {

}
num = num.shr(8);
}
return new Uint8Array(result.buffer);
},
unpack: buf => {
const view = new DataView(Uint8Array.from(buf).buffer);
let result = _bi.BI.from(0);
for (let i = 0; i < byteLength; i++) {

@@ -85,3 +65,2 @@ if (littleEndian) {

}
return result;

@@ -91,3 +70,2 @@ }

};
const Uint8 = createUintNumberCodec(1);

@@ -101,3 +79,2 @@ exports.Uint8 = Uint8;

*/
exports.Uint16BE = Uint16BE;

@@ -112,3 +89,2 @@ const Uint16 = Uint16LE;

*/
exports.Uint32BE = Uint32BE;

@@ -123,3 +99,2 @@ const Uint32 = Uint32LE;

*/
exports.Uint64BE = Uint64BE;

@@ -134,3 +109,2 @@ const Uint64 = Uint64LE;

*/
exports.Uint128BE = Uint128BE;

@@ -145,3 +119,2 @@ const Uint128 = Uint128LE;

*/
exports.Uint256BE = Uint256BE;

@@ -156,3 +129,2 @@ const Uint256 = Uint256LE;

*/
exports.Uint512BE = Uint512BE;

@@ -159,0 +131,0 @@ const Uint512 = Uint512LE;

@@ -13,12 +13,8 @@ "use strict";

exports.trackCodeExecuteError = trackCodeExecuteError;
var _error = require("./error");
const HEX_DECIMAL_REGEX = /^0x([0-9a-fA-F])+$/;
const HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP = new Map();
function assertHexDecimal(str, byteLength) {
if (byteLength) {
let regex = HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP.get(byteLength);
if (!regex) {

@@ -29,3 +25,2 @@ const newRegex = new RegExp(`^0x([0-9a-fA-F]){1,${byteLength * 2}}$`);

}
if (!regex.test(str)) {

@@ -40,10 +35,7 @@ throw new Error("Invalid hex decimal!");

}
const HEX_STRING_REGEX = /^0x([0-9a-fA-F][0-9a-fA-F])*$/;
const HEX_STRING_WITH_BYTELENGTH_REGEX_MAP = new Map();
function assertHexString(str, byteLength) {
if (byteLength) {
let regex = HEX_STRING_WITH_BYTELENGTH_REGEX_MAP.get(byteLength);
if (!regex) {

@@ -54,3 +46,2 @@ const newRegex = new RegExp(`^0x([0-9a-fA-F][0-9a-fA-F]){${byteLength}}$`);

}
if (!regex.test(str)) {

@@ -65,7 +56,5 @@ throw new Error("Invalid hex string!");

}
function assertUtf8String(str) {
for (let i = 0; i < str.length; i++) {
const c = str.charCodeAt(i);
if (c > 0xff) {

@@ -76,3 +65,2 @@ throw new Error("Invalid UTF-8 raw string!");

}
function assertBufferLength(buf, length) {

@@ -83,3 +71,2 @@ if (buf.byteLength !== length) {

}
function assertMinBufferLength(buf, length) {

@@ -90,3 +77,2 @@ if (buf.byteLength < length) {

}
function isObjectLike(x) {

@@ -96,3 +82,2 @@ if (!x) return false;

}
function trackCodeExecuteError(path, fn) {

@@ -99,0 +84,0 @@ try {

{
"name": "@ckb-lumos/codec",
"version": "0.20.0",
"version": "0.21.0-next.0",
"description": "Make your own molecule binding in JavaScript(TypeScript)",

@@ -21,15 +21,4 @@ "author": "",

},
"scripts": {
"fmt": "prettier --write \"{src,tests,examples}/**/*.ts\" package.json",
"lint": "eslint -c ../../.eslintrc.js \"{src,tests,examples}/**/*.ts\"",
"test": "ava **/*.test.ts --timeout=2m",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --declaration --declarationMap --emitDeclarationOnly",
"build:js": "babel --root-mode upward src --out-dir lib --extensions .ts -s",
"clean": "rm -rf lib",
"prepublishOnly": "yarn run clean && yarn run build",
"release": "npm publish"
},
"dependencies": {
"@ckb-lumos/bi": "0.20.0"
"@ckb-lumos/bi": "0.21.0-next.0"
},

@@ -54,3 +43,11 @@ "publishConfig": {

},
"gitHead": "28756be37c312eca132cac0c00b40510803030e0"
}
"scripts": {
"fmt": "prettier --write \"{src,tests,examples}/**/*.ts\" package.json",
"lint": "eslint -c ../../.eslintrc.js \"{src,tests,examples}/**/*.ts\"",
"test": "ava **/*.test.ts --timeout=2m",
"build": "pnpm run build:types && pnpm run build:js",
"build:types": "tsc --declaration --declarationMap --emitDeclarationOnly",
"build:js": "babel --root-mode upward src --out-dir lib --extensions .ts -s",
"clean": "rm -rf lib"
}
}

@@ -19,5 +19,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */

infer Packed,
infer Unpacked,
infer Packable,
infer Unpackable
any,
any,
any
>

@@ -27,6 +27,6 @@ ? Packed

export type PackParam<T extends AnyCodec> = T extends Codec<
infer Packed,
infer Unpacked,
any,
any,
infer Packable,
infer Unpackable
any
>

@@ -37,6 +37,6 @@ ? Packable

export type UnpackResult<T extends AnyCodec> = T extends Codec<
infer Packed,
any,
infer Unpacked,
infer Packable,
infer Unpackable
any,
any
>

@@ -47,5 +47,5 @@ ? Unpacked

export type UnpackParam<T extends AnyCodec> = T extends Codec<
infer Packed,
infer Unpacked,
infer Packable,
any,
any,
any,
infer Unpackable

@@ -52,0 +52,0 @@ >

@@ -96,3 +96,3 @@ /**

function checkShape<T>(shape: T, fields: (keyof T)[]) {
function checkShape<T extends object>(shape: T, fields: (keyof T)[]) {
const shapeKeys = Object.keys(shape) as (keyof T)[];

@@ -135,5 +135,7 @@

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;

@@ -299,5 +301,7 @@

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 {

@@ -317,5 +321,7 @@ 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]>;
}
>;
}

@@ -322,0 +328,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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc