Skip to content

Commit 0975441

Browse files
authored
Merge pull request #51 from ngraveio/fix-types
Add registry support for items that has NaN as tag Update readme Fix error on commonjs types when importing cbor2
2 parents 96b86fe + 7c6f1f9 commit 0975441

9 files changed

+961
-478
lines changed

README.md

+775-320
Large diffs are not rendered by default.

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ngraveio/bc-ur",
3-
"version": "2.0.1-beta.1",
3+
"version": "2.0.0-beta.4",
44
"author": "Antonis Poulakis <antwnic4@gmail.com>, Irfan Bilaloğlu <irfanbilaloglu@gmail.com>, Pieter Uyttersprot <pieter.uyttersprot@ngrave.io>",
55
"description": "A JS implementation of the Uniform Resources (UR) specification from Blockchain Commons",
66
"license": "MIT",
@@ -17,10 +17,10 @@
1717
"scripts": {
1818
"clean": "rimraf dist && rimraf tsconfig.tsbuildinfo",
1919
"build": "yarn clean && yarn compile",
20-
"compile": "tshy && rollup -c",
20+
"compile": "tshy && rollup -c && \\cp -f ./src/wrappers/cbor2Wrapper.ts ./dist/commonjs/wrappers/cbor2Wrapper.d.ts",
2121
"test": "node --experimental-vm-modules node_modules/.bin/jest",
2222
"prepublishOnly": "yarn build",
23-
"postinstall": "patch-package",
23+
"prebuild": "patch-package",
2424
"lint": "eslint \"./src/**/*.{ts,tsx}\" --max-warnings=0",
2525
"start": "live-server --watch=src,dist --port=8080"
2626
},
@@ -49,6 +49,9 @@
4949
"tslib": "^2.8.1",
5050
"typescript": "5.6.2"
5151
},
52+
"bundleDependencies": [
53+
"@keystonehq/alias-sampling"
54+
],
5255
"repository": {
5356
"type": "git",
5457
"url": "git+https://github.com/ngraveio/bc-ur.git"
+20-13
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
diff --git a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts
2-
index a11c780..814932f 100644
2+
index a11c780..600c193 100644
33
--- a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts
44
+++ b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.d.ts
5-
@@ -8,3 +8,4 @@ declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunct
5+
@@ -7,4 +7,5 @@ declare const next: (aliasData: AliasData, outcomes: any[], rng: RNGFunction, nu
6+
declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunction) => {
67
next: (numOfSamples?: number) => any | any[];
78
};
8-
export default sample;
9+
-export default sample;
10+
+export default sample; // Next line patched
911
+export { sample }
1012
diff --git a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js
11-
index 48c7d33..5bc3815 100644
13+
index 48c7d33..8db5def 100644
1214
--- a/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js
1315
+++ b/node_modules/@keystonehq/alias-sampling/dist/cjs/index.js
14-
@@ -77,3 +77,4 @@ var sample = function (probabilities, outcomes, rng) {
16+
@@ -76,4 +76,5 @@ var sample = function (probabilities, outcomes, rng) {
17+
}
1518
};
1619
};
17-
exports.default = sample;
20+
-exports.default = sample;
21+
+exports.default = sample; // Next line patched
1822
+exports.sample = sample;
1923
diff --git a/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts b/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts
20-
index a11c780..2ec4586 100644
24+
index a11c780..a50cde5 100644
2125
--- a/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts
2226
+++ b/node_modules/@keystonehq/alias-sampling/dist/esm/index.d.ts
23-
@@ -8,3 +8,5 @@ declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunct
27+
@@ -7,4 +7,5 @@ declare const next: (aliasData: AliasData, outcomes: any[], rng: RNGFunction, nu
28+
declare const sample: (probabilities: number[], outcomes?: any[], rng?: RNGFunction) => {
2429
next: (numOfSamples?: number) => any | any[];
2530
};
26-
export default sample;
31+
-export default sample;
32+
+export default sample; // Next line patched
2733
+export { sample };
28-
+
2934
diff --git a/node_modules/@keystonehq/alias-sampling/dist/esm/index.js b/node_modules/@keystonehq/alias-sampling/dist/esm/index.js
30-
index 0761ba7..1d598ec 100644
35+
index 0761ba7..cfee806 100644
3136
--- a/node_modules/@keystonehq/alias-sampling/dist/esm/index.js
3237
+++ b/node_modules/@keystonehq/alias-sampling/dist/esm/index.js
33-
@@ -75,3 +75,4 @@ var sample = function (probabilities, outcomes, rng) {
38+
@@ -74,4 +74,5 @@ var sample = function (probabilities, outcomes, rng) {
39+
}
3440
};
3541
};
36-
export default sample;
42+
-export default sample;
43+
+export default sample; // Next line patched
3744
+export { sample };

src/classes/RegistryItem.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export abstract class RegistryItemBase {
120120
toUr() {
121121
return new Ur(this);
122122
}
123+
124+
public encodeKeys = encodeKeys;
125+
public decodeKeys = decodeKeys;
123126
}
124127

125128
/**
@@ -130,7 +133,7 @@ export abstract class RegistryItemBase {
130133
* @param input
131134
* @returns
132135
*/
133-
export function registryItemFactory(input: IRegistryType): RegistryItemClass {
136+
export function registryItemFactory<T extends RegistryItemBase>(input: IRegistryType): RegistryItemClass<T> {
134137
const { tag, URType, CDDL, keyMap, allowKeysNotInMap = true } = input;
135138
const _keyMap = keyMap;
136139

@@ -173,7 +176,7 @@ export function registryItemFactory(input: IRegistryType): RegistryItemClass {
173176
// Return an instance of the generated class
174177
return new this(data);
175178
}
176-
};
179+
} as RegistryItemClass<T>;
177180
}
178181

179182
// Helper type to define the RegistryItem class with custom constructors and static properties

src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import { Ur } from "./classes/Ur.js"
22
import { UrRegistry } from "./registry.js"
3-
import { registryItemFactory } from "./classes/RegistryItem.js"
3+
import { registryItemFactory, RegistryItemBase } from "./classes/RegistryItem.js"
44
import { UrFountainEncoder } from "./classes/UrFountainEncoder.js"
55
import { UrFountainDecoder } from "./classes/UrFountainDecoder.js"
66
import { FountainEncoder } from "./classes/FountainEncoder.js"
77
import { FountainDecoder } from "./classes/FountainDecoder.js"
88
import { defaultEncoders, dataPipeline } from "./encodingMethods/index.js"
99

10+
import { CborEncoding } from "./encodingMethods/CborEncoding.js"
11+
import { BytewordEncoding } from "./encodingMethods/BytewordEncoding.js"
12+
import { HexEncoding } from "./encodingMethods/HexEncoding.js"
13+
1014
export {
1115
UrRegistry,
1216
registryItemFactory,
17+
RegistryItemBase,
1318
Ur,
1419
UrFountainDecoder,
1520
UrFountainEncoder,
1621
FountainDecoder,
1722
FountainEncoder,
23+
CborEncoding,
24+
BytewordEncoding,
25+
HexEncoding,
1826
defaultEncoders,
1927
dataPipeline,
2028
}

src/registry.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ export class URRegistryClass {
5151
this.log(`Warning: Tag collision detected for tag: ${item.tag}`);
5252
}
5353
this.registry.set(item.URType, item);
54-
this.tagMap.set(item.tag, item.URType);
55-
Tag.registerDecoder(item.tag, (tag: Tag, opts: any) => {
56-
return item.fromCBORData.bind(item)(
57-
tag.contents,
58-
item.allowKeysNotInMap,
59-
opts
60-
);
61-
});
54+
55+
// Register to CBOR decoder only if it has a tag
56+
if (!Number.isNaN(item.tag)) {
57+
this.tagMap.set(item.tag, item.URType);
58+
Tag.registerDecoder(item.tag, (tag: Tag, opts: any) => {
59+
return item.fromCBORData.bind(item)(
60+
tag.contents,
61+
item.allowKeysNotInMap,
62+
opts
63+
);
64+
});
65+
}
6266
}
6367

6468
public addItems(items: RegistryItemClass[]): void {

src/wrappers/cbor2-deno.mts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// If you need a provide an ESM dialect that doesn't support CommonJS (eg, deno, browser, etc), then you can do this:
22
// No dual package hazard here
3+
//@ts-ignore
34
export { decode, encode, Tag, registerEncoder } from "./cbor2Wrapper.js";
5+
//@ts-ignore
46
export type { DecodeOptions, EncodeOptions } from "./cbor2Wrapper.js";

src/wrappers/cbor2Wrapper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
DecodeOptions,
33
EncodeOptions,
4+
//@ts-ignore
45
} from 'cbor2' with { 'resolution-mode': 'import' };
56

67
//@ts-ignore

0 commit comments

Comments
 (0)