@qntx/ur 1.1.0 → 1.8.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,150 @@
1
+ import { C as fail, a as decodeWithType, d as UrType, n as Encoder, o as encode, s as toQrString, t as Decoder, w as failPoison } from "./ur-BUUcXyia.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 (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
+ 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) {
138
+ const poison = this.#inner.poisonState;
139
+ if (poison === void 0) fail("DecoderState");
140
+ failPoison(poison);
141
+ }
142
+ const data = this.#inner.message();
143
+ if (data === void 0) return;
144
+ const { type } = this.#inner;
145
+ if (type === void 0) fail("DecoderState");
146
+ return Ur.fromCborData(type, data);
147
+ }
148
+ };
149
+ //#endregion
150
+ export { fromUrString as a, Ur as c, fromUr as i, MultipartEncoder as n, toUr as o, firstTagUrType as r, toUrString as s, MultipartDecoder 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
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";
59
3
  export { DEFAULT_LIMITS, type DecoderLimits, type DecoderPoison, MultipartDecoder, MultipartEncoder, Ur, type UrCodec, UrError, type UrErrorCode, UrType, firstTagUrType, fromUr, fromUrString, toUr, toUrString };
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
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";
146
3
  export { DEFAULT_LIMITS, MultipartDecoder, MultipartEncoder, Ur, UrError, UrType, firstTagUrType, fromUr, fromUrString, toUr, toUrString };