Skip to content

Commit

Permalink
feat: codec supports for esm
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Jul 11, 2023
1 parent 78247eb commit f5b5293
Show file tree
Hide file tree
Showing 33 changed files with 358 additions and 242 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ module.exports = {
"SharedArrayBuffer": "readonly"
},
"extends": [
"plugin:import/recommended",
"plugin:import/recommended",
"plugin:import/typescript"
],
"rules": {
"no-var": "error",
"import/no-unresolved": "off",
"import/no-useless-path-segments": "error",
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down
4 changes: 3 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const buildCjs = process.env.MODULE === "cjs";
const buildEsm = process.env.MODULE === "esm";

const presets = [
["@babel/preset-env",
[
"@babel/preset-env",
{
targets: {
chrome: "79",
Expand All @@ -16,6 +17,7 @@ const presets = [
const plugins = [
buildCjs && "@babel/plugin-proposal-export-namespace-from",
buildCjs && "@babel/plugin-transform-modules-commonjs",
require.resolve("babel-plugin-add-import-extension"),
].filter(Boolean);

module.exports = { presets, plugins: plugins };
7 changes: 0 additions & 7 deletions devtools/playground/cjs.test.cjs

This file was deleted.

10 changes: 0 additions & 10 deletions devtools/playground/esm.test.mjs

This file was deleted.

22 changes: 22 additions & 0 deletions devtools/playground/module.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createRequire } from "node:module";
import test from "ava";
import { minimatch } from "minimatch";

const MODULES = [
"@ckb-lumos/bi",
"@ckb-lumos/codec",
"@ckb-lumos/codec/bytes",
"@ckb-lumos/codec/number",
];

const require = createRequire(import.meta.url);

MODULES.forEach((module) => {
test(`Resolve esm&cjs from ${module}`, async (t) => {
const resolvedEsm = await import.meta.resolve(module);
const resolvedCjs = require.resolve(module);

t.true(minimatch(resolvedEsm, `**/lib.esm/**/*.js`));
t.true(minimatch(resolvedCjs, `**/lib/**/*.js`));
});
});
4 changes: 3 additions & 1 deletion devtools/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
]
},
"dependencies": {
"@ckb-lumos/bi": "0.20.0-alpha.3"
"@ckb-lumos/bi": "0.20.0-alpha.3",
"@ckb-lumos/codec": "0.20.0-alpha.3",
"minimatch": "^9.0.3"
}
}
10 changes: 10 additions & 0 deletions devtools/playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "bin",
"declaration": false,
"declarationMap": false,
"sourceMap": false
},
"include": ["src"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"ava": "^3.8.2",
"babel-plugin-add-import-extension": "^1.6.0",
"c8": "^7.10.0",
"eslint": "^8.40.0",
"eslint-import-resolver-typescript": "^2.7.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/base/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
number,
molecule,
bytes,
BytesCodec,
FixedBytesCodec,
} from "@ckb-lumos/codec";
import { BytesCodec, FixedBytesCodec } from "@ckb-lumos/codec/lib/base";

import type * as api from "./api";
import { BI } from "@ckb-lumos/bi";
Expand Down
2 changes: 1 addition & 1 deletion packages/codec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This module provides a set of functions to pack(encode) and unpack(decode) data.

```mermaid
graph TD;
graph TD
Uint8Array-->Codec;
Codec-->|unpack|JSObject;
JSObject-->Codec;
Expand Down
29 changes: 26 additions & 3 deletions packages/codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@
"author": "",
"homepage": "https://github.com/ckb-js/lumos#readme",
"license": "MIT",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "./lib/index.js",
"module": "./lib.esm/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": {
"import": "./lib.esm/index.js",
"default": "./lib/index.js"
},
"./bytes": {
"import": "./lib.esm/bytes.js",
"default": "./lib/bytes.js",
"types": "./lib/bytes.d.ts"
},
"./number": {
"import": "./lib.esm/number/index.js",
"default": "./lib/number/index.js",
"types": "./lib/number/index.d.ts"
},
"./molecule": {
"import": "./lib.esm/molecule/index.js",
"default": "./lib/molecule/index.js",
"types": "./lib/molecule/index.d.ts"
}
},
"engines": {
"node": ">=12.0.0"
},
"files": [
"lib",
"lib.esm",
"src"
],
"repository": {
Expand All @@ -22,7 +45,7 @@
"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": "lumos-build --types --cjs --esm",
"build:types": "tsc --declaration --declarationMap --emitDeclarationOnly",
"build:js": "babel --root-mode upward src --out-dir lib --extensions .ts -s",
"clean": "rm -rf lib",
Expand Down
12 changes: 8 additions & 4 deletions packages/codec/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import { assertBufferLength, isObjectLike } from "./utils";
import { bytify } from "./bytes";

export interface Codec<
export type Codec<
Packed,
Unpacked,
Packable = Unpacked,
Unpackable = Packed
> {
> = {
pack: (packable: Packable) => Packed;
unpack: (unpackable: Unpackable) => Unpacked;
}
};

export type AnyCodec = Codec<any, any>;

Expand Down Expand Up @@ -91,7 +91,11 @@ export type FixedBytesCodec<Unpacked = any, Packable = Unpacked> = BytesCodec<
export function isFixedCodec<T>(
codec: BytesCodec<T>
): codec is FixedBytesCodec<T> {
return isObjectLike(codec) && !!codec.__isFixedCodec__;
return (
isObjectLike(codec) &&
"__isFixedCodec__" in codec &&
!!codec.__isFixedCodec__
);
}

export function createFixedBytesCodec<Unpacked, Packable = Unpacked>(
Expand Down
4 changes: 4 additions & 0 deletions packages/codec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export type {
UnpackParam,
BytesLike,
AnyCodec,
BytesCodec,
FixedBytesCodec,
Codec,
Fixed,
} from "./base";
export { createBytesCodec, createFixedBytesCodec, isFixedCodec } from "./base";
export * from "./high-order";
Expand Down
13 changes: 12 additions & 1 deletion packages/codec/src/molecule/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
export { byteOf, byteArrayOf, byteVecOf } from "./helper";
export { table, array, option, struct, vector, union } from "./layout";
export {
table,
array,
option,
struct,
vector,
union,
OptionCodec,
UnionCodec,
ObjectCodec,
ArrayCodec,
} from "./layout";
38 changes: 17 additions & 21 deletions packages/codec/src/molecule/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ export type ObjectCodec<T extends Record<string, BytesCodec>> = BytesCodec<
PartialNullable<{ [key in keyof T]: PackParam<T[key]> }>
>;

export interface OptionCodec<T extends BytesCodec>
extends BytesCodec<UnpackResult<T> | undefined> {
pack: (packable?: PackParam<T>) => Uint8Array;
}
export type OptionCodec<Unpacked, Packable = Unpacked> = BytesCodec<
Unpacked | undefined,
Packable | undefined
>;

export type ArrayCodec<T extends BytesCodec> = BytesCodec<
Array<UnpackResult<T>>,
Expand Down Expand Up @@ -131,11 +131,9 @@ export function struct<T extends Record<string, FixedBytesCodec>>(
}, Uint8Array.from([]));
},
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;

fields.forEach((field) => {
Expand Down Expand Up @@ -296,11 +294,9 @@ export function table<T extends Record<string, BytesCodec>>(
);
}
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 {
const offsets = fields.map((_, index) =>
Uint32LE.unpack(buf.slice(4 + index * 4, 8 + index * 4))
Expand All @@ -315,11 +311,9 @@ export function table<T extends Record<string, BytesCodec>>(
const itemBuf = buf.slice(start, end);
Object.assign(obj, { [field]: itemCodec.unpack(itemBuf) });
}
return obj as PartialNullable<
{
[key in keyof T]: UnpackResult<T[key]>;
}
>;
return obj as PartialNullable<{
[key in keyof T]: UnpackResult<T[key]>;
}>;
}
},
});
Expand Down Expand Up @@ -376,9 +370,11 @@ export function union<T extends Record<string, BytesCodec>>(
* - if it's not empty, just serialize the inner item (the size is same as the inner item's size).
* @param itemCodec
*/
export function option<T extends BytesCodec>(itemCodec: T): OptionCodec<T> {
export function option<Unpacked, Packable>(
itemCodec: BytesCodec<Unpacked, Packable>
): OptionCodec<Unpacked, Packable> {
return createBytesCodec({
pack(obj?) {
pack(obj) {
const nullableCodec = createNullableCodec(itemCodec);
if (obj !== undefined && obj !== null) {
return nullableCodec.pack(obj);
Expand Down
2 changes: 1 addition & 1 deletion packages/codec/tests/test-vector/codecs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FixedBytesCodec } from "./../../src/base";
import { FixedBytesCodec } from "../../src";
import { Uint8 } from "../../src/number";
import {
testArray,
Expand Down
6 changes: 3 additions & 3 deletions packages/common-scripts/src/omnilock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import {
option,
table,
vector,
} from "@ckb-lumos/codec/lib/molecule";
} from "@ckb-lumos/codec/molecule";
import {
BytesOpt,
createFixedHexBytesCodec,
} from "@ckb-lumos/codec/lib/blockchain";
import { bytify, hexify } from "@ckb-lumos/codec/lib/bytes";
} from "@ckb-lumos/base/lib/blockchain";
import { bytify, hexify } from "@ckb-lumos/codec/bytes";
const { ScriptValue } = values;

export type OmnilockInfo = {
Expand Down
5 changes: 3 additions & 2 deletions packages/common-scripts/tests/omnilock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
createTestContext,
getDefaultConfig,
} from "@ckb-lumos/debugger/lib/context";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { common, omnilock } from "../src";
import { WitnessArgs } from "@ckb-lumos/codec/lib/blockchain";
import { WitnessArgs } from "@ckb-lumos/base/lib/blockchain";
import { CellProvider } from "./cell_provider";
import { charlesOmnilockInputs } from "./inputs";
import { bytes } from "@ckb-lumos/codec";

const { hexify } = bytes;

const downloader = new CKBDebuggerDownloader();
const context = createTestContext(getDefaultConfig());
const ckbConfig: Config = {
Expand Down
4 changes: 2 additions & 2 deletions packages/debugger/src/codecs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { struct, vector } from "@ckb-lumos/codec/lib/molecule";
import { struct, vector } from "@ckb-lumos/codec/molecule";
import { Byte32 } from "@ckb-lumos/base/lib/blockchain";
import { createFixedBytesCodec } from "@ckb-lumos/codec";
import { Uint32 } from "@ckb-lumos/codec/lib/number";
import { Uint32 } from "@ckb-lumos/codec/number";
import { BI } from "@ckb-lumos/bi";

export const OutPoint = struct(
Expand Down
4 changes: 2 additions & 2 deletions packages/debugger/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { CKBDebuggerDownloader } from "./download";
import { CKBDebugger } from "./executor";
import * as fs from "fs";
import * as path from "path";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { Uint32 } from "@ckb-lumos/codec/lib/number";
import { hexify } from "@ckb-lumos/codec/bytes";
import { Uint32 } from "@ckb-lumos/codec/number";

export function mockOutPoint(): OutPoint {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/debugger/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HexString, OutPoint } from "@ckb-lumos/base";
import * as fs from "fs";
import { ckbHash } from "@ckb-lumos/base/lib/utils";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { hexify } from "@ckb-lumos/codec/bytes";
import { OutPoint as OutPointCodec, OutPointVec } from "./codecs";

export type LoadedCode = { codeHash: HexString; binary: HexString };
Expand Down
2 changes: 1 addition & 1 deletion packages/debugger/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TransactionSkeletonType,
} from "@ckb-lumos/helpers";
import { CellDep, HexString } from "@ckb-lumos/base";
import { bytify } from "@ckb-lumos/codec/lib/bytes";
import { bytify } from "@ckb-lumos/codec/bytes";
import { OutPointVec } from "./codecs";
import { ParamsFormatter } from "@ckb-lumos/rpc";
import { RPC } from "@ckb-lumos/rpc/lib/types/rpc";
Expand Down
6 changes: 4 additions & 2 deletions packages/debugger/tests/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
} from "../src/context";
import { randomBytes } from "crypto";
import { privateKeyToBlake160, signRecoverable } from "@ckb-lumos/hd/lib/key";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { bytes } from "@ckb-lumos/codec";
import {
createP2PKHMessageGroup,
parseFromInfo,
} from "@ckb-lumos/common-scripts";
import { WitnessArgs } from "@ckb-lumos/codec/lib/blockchain";
import { WitnessArgs } from "@ckb-lumos/base/lib/blockchain";

const hexify = bytes.hexify;

const downloader = new CKBDebuggerDownloader();
const context = createTestContext(getDefaultConfig());
Expand Down
Loading

0 comments on commit f5b5293

Please sign in to comment.