@qntx/ur 1.8.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,3 +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-CZ0CIHJk.mjs";
2
- import { a as fromUr, c as toUrString, i as firstTagUrType, l as Ur, n as MultipartEncoder, o as fromUrString, r as UrCodec, s as toUr, t as MultipartDecoder } from "./index-BeG2XIr5.mjs";
3
- 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,3 +1,3 @@
1
- import { S as UrError, d as UrType, h as DEFAULT_LIMITS } from "./ur-BUUcXyia.mjs";
2
- import { a as fromUrString, c as Ur, i as fromUr, n as MultipartEncoder, o as toUr, r as firstTagUrType, s as toUrString, t as MultipartDecoder } from "./typed-BmHx2GWT.mjs";
3
- 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 };