Skip to content

Commit

Permalink
Merge pull request #51 from ngraveio/fix-types
Browse files Browse the repository at this point in the history
Add registry support for items that has NaN as tag
Update readme
Fix error on commonjs types when importing cbor2
  • Loading branch information
irfan798 authored Jan 24, 2025
2 parents 96b86fe + 7c6f1f9 commit 0975441
Show file tree
Hide file tree
Showing 9 changed files with 961 additions and 478 deletions.
1,095 changes: 775 additions & 320 deletions README.md

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngraveio/bc-ur",
"version": "2.0.1-beta.1",
"version": "2.0.0-beta.4",
"author": "Antonis Poulakis <[email protected]>, Irfan Bilaloğlu <[email protected]>, Pieter Uyttersprot <[email protected]>",
"description": "A JS implementation of the Uniform Resources (UR) specification from Blockchain Commons",
"license": "MIT",
Expand All @@ -17,10 +17,10 @@
"scripts": {
"clean": "rimraf dist && rimraf tsconfig.tsbuildinfo",
"build": "yarn clean && yarn compile",
"compile": "tshy && rollup -c",
"compile": "tshy && rollup -c && \\cp -f ./src/wrappers/cbor2Wrapper.ts ./dist/commonjs/wrappers/cbor2Wrapper.d.ts",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"prepublishOnly": "yarn build",
"postinstall": "patch-package",
"prebuild": "patch-package",
"lint": "eslint \"./src/**/*.{ts,tsx}\" --max-warnings=0",
"start": "live-server --watch=src,dist --port=8080"
},
Expand Down Expand Up @@ -49,6 +49,9 @@
"tslib": "^2.8.1",
"typescript": "5.6.2"
},
"bundleDependencies": [
"@keystonehq/alias-sampling"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ngraveio/bc-ur.git"
Expand Down
33 changes: 20 additions & 13 deletions patches/@keystonehq+alias-sampling+0.1.2.patch
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
diff --git a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts
index a11c780..814932f 100644
index a11c780..600c193 100644
--- a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts
+++ b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts
@@ -8,3 +8,4 @@ declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunct
@@ -7,4 +7,5 @@ declare const next: (aliasData: AliasData, outcomes: any[], rng: RNGFunction, nu
declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunction) => {
next: (numOfSamples?: number) => any | any[];
};
export default sample;
-export default sample;
+export default sample; // Next line patched
+export { sample }
diff --git a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js
index 48c7d33..5bc3815 100644
index 48c7d33..8db5def 100644
--- a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js
+++ b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js
@@ -77,3 +77,4 @@ var sample = function (probabilities, outcomes, rng) {
@@ -76,4 +76,5 @@ var sample = function (probabilities, outcomes, rng) {
}
};
};
exports.default = sample;
-exports.default = sample;
+exports.default = sample; // Next line patched
+exports.sample = sample;
diff --git a/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts b/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts
index a11c780..2ec4586 100644
index a11c780..a50cde5 100644
--- a/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts
+++ b/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts
@@ -8,3 +8,5 @@ declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunct
@@ -7,4 +7,5 @@ declare const next: (aliasData: AliasData, outcomes: any[], rng: RNGFunction, nu
declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunction) => {
next: (numOfSamples?: number) => any | any[];
};
export default sample;
-export default sample;
+export default sample; // Next line patched
+export { sample };
+
diff --git a/node_modules/@keystonehq/alias-sampling/dist/esm/index.js b/node_modules/@keystonehq/alias-sampling/dist/esm/index.js
index 0761ba7..1d598ec 100644
index 0761ba7..cfee806 100644
--- a/node_modules/@keystonehq/alias-sampling/dist/esm/index.js
+++ b/node_modules/@keystonehq/alias-sampling/dist/esm/index.js
@@ -75,3 +75,4 @@ var sample = function (probabilities, outcomes, rng) {
@@ -74,4 +74,5 @@ var sample = function (probabilities, outcomes, rng) {
}
};
};
export default sample;
-export default sample;
+export default sample; // Next line patched
+export { sample };
7 changes: 5 additions & 2 deletions src/classes/RegistryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export abstract class RegistryItemBase {
toUr() {
return new Ur(this);
}

public encodeKeys = encodeKeys;
public decodeKeys = decodeKeys;
}

/**
Expand All @@ -130,7 +133,7 @@ export abstract class RegistryItemBase {
* @param input
* @returns
*/
export function registryItemFactory(input: IRegistryType): RegistryItemClass {
export function registryItemFactory<T extends RegistryItemBase>(input: IRegistryType): RegistryItemClass<T> {
const { tag, URType, CDDL, keyMap, allowKeysNotInMap = true } = input;
const _keyMap = keyMap;

Expand Down Expand Up @@ -173,7 +176,7 @@ export function registryItemFactory(input: IRegistryType): RegistryItemClass {
// Return an instance of the generated class
return new this(data);
}
};
} as RegistryItemClass<T>;
}

// Helper type to define the RegistryItem class with custom constructors and static properties
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { Ur } from "./classes/Ur.js"
import { UrRegistry } from "./registry.js"
import { registryItemFactory } from "./classes/RegistryItem.js"
import { registryItemFactory, RegistryItemBase } from "./classes/RegistryItem.js"
import { UrFountainEncoder } from "./classes/UrFountainEncoder.js"
import { UrFountainDecoder } from "./classes/UrFountainDecoder.js"
import { FountainEncoder } from "./classes/FountainEncoder.js"
import { FountainDecoder } from "./classes/FountainDecoder.js"
import { defaultEncoders, dataPipeline } from "./encodingMethods/index.js"

import { CborEncoding } from "./encodingMethods/CborEncoding.js"
import { BytewordEncoding } from "./encodingMethods/BytewordEncoding.js"
import { HexEncoding } from "./encodingMethods/HexEncoding.js"

export {
UrRegistry,
registryItemFactory,
RegistryItemBase,
Ur,
UrFountainDecoder,
UrFountainEncoder,
FountainDecoder,
FountainEncoder,
CborEncoding,
BytewordEncoding,
HexEncoding,
defaultEncoders,
dataPipeline,
}
Expand Down
20 changes: 12 additions & 8 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ export class URRegistryClass {
this.log(`Warning: Tag collision detected for tag: ${item.tag}`);
}
this.registry.set(item.URType, item);
this.tagMap.set(item.tag, item.URType);
Tag.registerDecoder(item.tag, (tag: Tag, opts: any) => {
return item.fromCBORData.bind(item)(
tag.contents,
item.allowKeysNotInMap,
opts
);
});

// Register to CBOR decoder only if it has a tag
if (!Number.isNaN(item.tag)) {
this.tagMap.set(item.tag, item.URType);
Tag.registerDecoder(item.tag, (tag: Tag, opts: any) => {
return item.fromCBORData.bind(item)(
tag.contents,
item.allowKeysNotInMap,
opts
);
});
}
}

public addItems(items: RegistryItemClass[]): void {
Expand Down
2 changes: 2 additions & 0 deletions src/wrappers/cbor2-deno.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// If you need a provide an ESM dialect that doesn't support CommonJS (eg, deno, browser, etc), then you can do this:
// No dual package hazard here
//@ts-ignore
export { decode, encode, Tag, registerEncoder } from "./cbor2Wrapper.js";
//@ts-ignore
export type { DecodeOptions, EncodeOptions } from "./cbor2Wrapper.js";
1 change: 1 addition & 0 deletions src/wrappers/cbor2Wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DecodeOptions,
EncodeOptions,
//@ts-ignore
} from 'cbor2' with { 'resolution-mode': 'import' };

//@ts-ignore
Expand Down
Loading

0 comments on commit 0975441

Please sign in to comment.