@qntx/ur 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ import { T as fail, a as parseUr, i as toQrString, n as UrEncoder, r as encodeUr, s as parseUrType } from "./ur-7Ge3roVh.mjs";
2
+ import { CborError, cbor, decodeCbor, encodeCbor, extractTaggedContent, taggedValue, validateTag } from "@blockchaincommons/dcbor";
3
+ //#region src/typed/ur.ts
4
+ function mapCborDecode(run) {
5
+ try {
6
+ return run();
7
+ } catch (error) {
8
+ if (CborError.isCborError(error)) fail("CborDecode", { cause: error });
9
+ throw error;
10
+ }
11
+ }
12
+ function mapCborType(run) {
13
+ try {
14
+ return run();
15
+ } catch (error) {
16
+ if (CborError.isCborError(error)) fail("CborType", { cause: error });
17
+ throw error;
18
+ }
19
+ }
20
+ /** Uniform Resource whose payload is deterministic CBOR. */
21
+ var Ur = class Ur {
22
+ type;
23
+ cbor;
24
+ constructor(type, cborValue) {
25
+ this.type = type;
26
+ this.cbor = cborValue;
27
+ }
28
+ static fromCbor(type, input) {
29
+ const prepared = input instanceof Uint8Array ? new Uint8Array(input) : input;
30
+ return new Ur(parseUrType(type), mapCborType(() => cbor(prepared)));
31
+ }
32
+ /** Wrap already-encoded dCBOR bytes. */
33
+ static fromCborData(type, data) {
34
+ const bytes = new Uint8Array(data);
35
+ const value = mapCborDecode(() => decodeCbor(bytes));
36
+ return new Ur(parseUrType(type), value);
37
+ }
38
+ /** Parse a **single-part** UR string; the message must be dCBOR. */
39
+ static parse(text) {
40
+ const parsed = parseUr(text);
41
+ if (parsed.kind !== "single") fail("NotSinglePart");
42
+ return Ur.fromCborData(parsed.type, parsed.message);
43
+ }
44
+ /** Wrap a completed {@link UrDecoder} result; the message must be dCBOR. */
45
+ static fromDecoded(decoded) {
46
+ return Ur.fromCborData(decoded.type, decoded.message);
47
+ }
48
+ toCborData() {
49
+ return mapCborType(() => encodeCbor(this.cbor));
50
+ }
51
+ toString() {
52
+ return encodeUr(this.type, this.toCborData());
53
+ }
54
+ toQrString() {
55
+ return toQrString(this.toString());
56
+ }
57
+ /** L3 encoder over this UR's dCBOR bytes. */
58
+ encoder(options) {
59
+ return new UrEncoder(this.type, this.toCborData(), options);
60
+ }
61
+ };
62
+ //#endregion
63
+ //#region src/typed/codec.ts
64
+ /** UR types a codec accepts on read: every tag name, write-preferred first. */
65
+ function codecUrTypes(codec) {
66
+ const [first, ...rest] = codec.tags;
67
+ return [toUrType(first), ...rest.map(toUrType)];
68
+ }
69
+ function toUrType(tag) {
70
+ const { name } = tag;
71
+ if (name === void 0 || name === "") fail("InvalidType");
72
+ return parseUrType(name);
73
+ }
74
+ /** Typed UR for `value`: first tag name as the type, untagged body. */
75
+ function toUr(value, codec) {
76
+ const [type] = codecUrTypes(codec);
77
+ const body = mapCborType(() => codec.encode(value));
78
+ return Ur.fromCbor(type, body);
79
+ }
80
+ /** Decode `ur`'s body when its type is one of the codec's tag names. */
81
+ function fromUr(ur, codec) {
82
+ const expected = codecUrTypes(codec);
83
+ if (!expected.some((t) => ur.type === t)) fail({
84
+ code: "UnexpectedType",
85
+ expected,
86
+ found: ur.type
87
+ });
88
+ return mapCborType(() => codec.decode(ur.cbor));
89
+ }
90
+ /** `tags[0]`-wrapped dCBOR for `value`. */
91
+ function toTagged(value, codec) {
92
+ return mapCborType(() => taggedValue(codec.tags[0], codec.encode(value)));
93
+ }
94
+ /** Unwraps any of the codec's tags, then decodes the body. */
95
+ function fromTagged(cbor, codec) {
96
+ return mapCborType(() => {
97
+ validateTag(cbor, [...codec.tags]);
98
+ return codec.decode(extractTaggedContent(cbor));
99
+ });
100
+ }
101
+ /** `UrType -> codec` map covering every tag name; a duplicate type is `InvalidType`. */
102
+ function codecMap(codecs) {
103
+ const map = /* @__PURE__ */ new Map();
104
+ for (const codec of codecs) for (const type of codecUrTypes(codec)) {
105
+ if (map.has(type)) fail("InvalidType");
106
+ map.set(type, codec);
107
+ }
108
+ return map;
109
+ }
110
+ /** Decodes `ur` through the codec registered for its type. */
111
+ function fromUrWith(ur, codecs) {
112
+ const codec = codecs.get(ur.type);
113
+ if (codec === void 0) fail({
114
+ code: "UnexpectedType",
115
+ expected: [...codecs.keys()],
116
+ found: ur.type
117
+ });
118
+ return {
119
+ type: ur.type,
120
+ value: fromUr(ur, codec)
121
+ };
122
+ }
123
+ //#endregion
124
+ export { fromUrWith as a, Ur as c, fromUr as i, codecUrTypes as n, toTagged as o, fromTagged as r, toUr as s, codecMap as t };
package/dist/typed.d.mts CHANGED
@@ -1,59 +1,3 @@
1
- import { a as UrError, i as DecoderPoison, n as DEFAULT_LIMITS, o as UrErrorCode, r as DecoderLimits, t as UrType } from "./type-CSfO9fHL.mjs";
2
- import { Cbor, CborInput, Tag } from "@blockchaincommons/dcbor";
3
- //#region src/typed/ur.d.ts
4
- /** Uniform Resource whose payload is deterministic CBOR. */
5
- declare class Ur {
6
- readonly type: UrType;
7
- readonly cbor: Cbor;
8
- private constructor();
9
- static create(type: UrType | string, input: CborInput): Ur;
10
- /** Wrap already-encoded dCBOR bytes. Used by fromUrString. */
11
- static fromCborData(type: UrType | string, data: Uint8Array): Ur;
12
- static fromUrString(uri: string): Ur;
13
- string(): string;
14
- qrString(): string;
15
- checkType(expected: UrType | string): void;
16
- }
17
- //#endregion
18
- //#region src/typed/codec.d.ts
19
- /** First `tags[0].name` is the UR type; body is untagged. */
20
- interface UrCodec<T> {
21
- /** Most-preferred first. First tag.name is the UR type token. */
22
- readonly tags: readonly Tag[];
23
- untaggedCbor(value: T): Cbor;
24
- fromUntaggedCbor(cbor: Cbor): T;
25
- }
26
- declare function firstTagUrType(tags: readonly Tag[]): UrType;
27
- declare function toUr<T>(value: T, codec: UrCodec<T>): Ur;
28
- declare function fromUr<T>(ur: Ur, codec: UrCodec<T>): T;
29
- declare function toUrString<T>(value: T, codec: UrCodec<T>): string;
30
- declare function fromUrString<T>(uri: string, codec: UrCodec<T>): T;
31
- //#endregion
32
- //#region src/typed/multipart.d.ts
33
- declare class MultipartEncoder {
34
- #private;
35
- private constructor();
36
- static create(ur: Ur, maxFragmentLength: number): MultipartEncoder;
37
- nextPart(): string;
38
- get isSinglePart(): boolean;
39
- get fragmentCount(): number;
40
- get currentIndex(): number;
41
- get complete(): boolean;
42
- }
43
- declare class MultipartDecoder {
44
- #private;
45
- constructor(options?: {
46
- limits?: Partial<DecoderLimits>;
47
- expectedType?: UrType;
48
- });
49
- receive(uri: string): void;
50
- get complete(): boolean;
51
- get fragmentCount(): number;
52
- resolvedFragmentCount(): number | undefined;
53
- get type(): UrType | undefined;
54
- get isPoisoned(): boolean;
55
- get poisonState(): DecoderPoison | undefined;
56
- message(): Ur | undefined;
57
- }
58
- //#endregion
59
- export { DEFAULT_LIMITS, type DecoderLimits, type DecoderPoison, MultipartDecoder, MultipartEncoder, Ur, type UrCodec, UrError, type UrErrorCode, UrType, firstTagUrType, fromUr, fromUrString, toUr, toUrString };
1
+ import { C as parseUrType, S as isUrType, _ as UrError, b as UrLimit, g as DecoderLimits, h as DEFAULT_LIMITS, v as UrErrorCode, x as UrType, y as UrErrorInfo } from "./index-DTjNCWBI.mjs";
2
+ import { a as fromUr, c as toUr, i as fromTagged, l as Ur, n as codecMap, o as fromUrWith, r as codecUrTypes, s as toTagged, t as UrCodec } from "./index-CcMfgay6.mjs";
3
+ export { DEFAULT_LIMITS, type DecoderLimits, Ur, type UrCodec, UrError, type UrErrorCode, type UrErrorInfo, type UrLimit, type UrType, codecMap, codecUrTypes, fromTagged, fromUr, fromUrWith, isUrType, parseUrType, toTagged, toUr };
package/dist/typed.mjs CHANGED
@@ -1,146 +1,3 @@
1
- import { C as fail, S as UrError, a as decodeWithType, d as UrType, h as DEFAULT_LIMITS, n as Encoder, o as encode, s as toQrString, t as Decoder, w as failPoison } from "./ur-C5n_FgVn.mjs";
2
- import { CborError, cbor, decodeCbor, encodeCbor } from "@blockchaincommons/dcbor";
3
- //#region src/typed/ur.ts
4
- function mapCborDecode(run) {
5
- try {
6
- return run();
7
- } catch (e) {
8
- if (CborError.isCborError(e)) fail("CborDecode", { cause: e });
9
- throw e;
10
- }
11
- }
12
- function mapCborType(run) {
13
- try {
14
- return run();
15
- } catch (e) {
16
- if (CborError.isCborError(e)) fail("CborType", { cause: e });
17
- throw e;
18
- }
19
- }
20
- function parseType(type) {
21
- return typeof type === "string" ? UrType.parse(type) : type;
22
- }
23
- /** Uniform Resource whose payload is deterministic CBOR. */
24
- var Ur = class Ur {
25
- type;
26
- cbor;
27
- constructor(type, cborValue) {
28
- this.type = type;
29
- this.cbor = cborValue;
30
- }
31
- static create(type, input) {
32
- const prepared = input instanceof Uint8Array ? new Uint8Array(input) : input;
33
- return new Ur(parseType(type), mapCborType(() => cbor(prepared)));
34
- }
35
- /** Wrap already-encoded dCBOR bytes. Used by fromUrString. */
36
- static fromCborData(type, data) {
37
- const bytes = new Uint8Array(data);
38
- const value = mapCborDecode(() => decodeCbor(bytes));
39
- return new Ur(parseType(type), value);
40
- }
41
- static fromUrString(uri) {
42
- const { type, kind, payload } = decodeWithType(uri);
43
- if (kind !== "single") fail("NotSinglePart");
44
- return Ur.fromCborData(type, payload);
45
- }
46
- string() {
47
- const bytes = mapCborType(() => encodeCbor(this.cbor));
48
- return encode(bytes, this.type);
49
- }
50
- qrString() {
51
- return toQrString(this.string());
52
- }
53
- checkType(expected) {
54
- const want = parseType(expected);
55
- if (!this.type.equals(want)) fail("UnexpectedType", {
56
- expected: want.value,
57
- found: this.type.value
58
- });
59
- }
60
- };
61
- //#endregion
62
- //#region src/typed/codec.ts
63
- function firstTagUrType(tags) {
64
- const name = tags[0]?.name;
65
- if (name === void 0 || name === "") fail("InvalidType");
66
- return UrType.parse(name);
67
- }
68
- function toUr(value, codec) {
69
- const type = firstTagUrType(codec.tags);
70
- const body = mapCborType(() => codec.untaggedCbor(value));
71
- return Ur.create(type, body);
72
- }
73
- function fromUr(ur, codec) {
74
- ur.checkType(firstTagUrType(codec.tags));
75
- return mapCborType(() => codec.fromUntaggedCbor(ur.cbor));
76
- }
77
- function toUrString(value, codec) {
78
- return toUr(value, codec).string();
79
- }
80
- function fromUrString(uri, codec) {
81
- return fromUr(Ur.fromUrString(uri), codec);
82
- }
83
- //#endregion
84
- //#region src/typed/multipart.ts
85
- var MultipartEncoder = class MultipartEncoder {
86
- #inner;
87
- constructor(inner) {
88
- this.#inner = inner;
89
- }
90
- static create(ur, maxFragmentLength) {
91
- const bytes = mapCborType(() => encodeCbor(ur.cbor));
92
- return new MultipartEncoder(Encoder.create(bytes, maxFragmentLength, ur.type));
93
- }
94
- nextPart() {
95
- return this.#inner.nextPart();
96
- }
97
- get isSinglePart() {
98
- return this.#inner.isSinglePart;
99
- }
100
- get fragmentCount() {
101
- return this.#inner.fragmentCount;
102
- }
103
- get currentIndex() {
104
- return this.#inner.currentIndex;
105
- }
106
- get complete() {
107
- return this.#inner.complete;
108
- }
109
- };
110
- var MultipartDecoder = class {
111
- #inner;
112
- constructor(options) {
113
- this.#inner = new Decoder(options);
114
- }
115
- receive(uri) {
116
- this.#inner.receive(uri);
117
- }
118
- get complete() {
119
- return this.#inner.complete;
120
- }
121
- get fragmentCount() {
122
- return this.#inner.fragmentCount;
123
- }
124
- resolvedFragmentCount() {
125
- return this.#inner.resolvedFragmentCount();
126
- }
127
- get type() {
128
- return this.#inner.type;
129
- }
130
- get isPoisoned() {
131
- return this.#inner.isPoisoned;
132
- }
133
- get poisonState() {
134
- return this.#inner.poisonState;
135
- }
136
- message() {
137
- if (this.#inner.isPoisoned) failPoison(this.#inner.poisonState);
138
- const data = this.#inner.message();
139
- if (data === void 0) return void 0;
140
- const type = this.#inner.type;
141
- if (type === void 0) fail("DecoderState");
142
- return Ur.fromCborData(type, data);
143
- }
144
- };
145
- //#endregion
146
- export { DEFAULT_LIMITS, MultipartDecoder, MultipartEncoder, Ur, UrError, UrType, firstTagUrType, fromUr, fromUrString, toUr, toUrString };
1
+ import { f as DEFAULT_LIMITS, o as isUrType, s as parseUrType, w as UrError } from "./ur-7Ge3roVh.mjs";
2
+ import { a as fromUrWith, c as Ur, i as fromUr, n as codecUrTypes, o as toTagged, r as fromTagged, s as toUr, t as codecMap } from "./typed-D-SyraAj.mjs";
3
+ export { DEFAULT_LIMITS, Ur, UrError, codecMap, codecUrTypes, fromTagged, fromUr, fromUrWith, isUrType, parseUrType, toTagged, toUr };