@qntx/ur 1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 QuantX
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ <!-- markdownlint-disable MD033 MD041 -->
2
+
3
+ # ur.js
4
+
5
+ Uniform Resources (UR) for TypeScript.
6
+
7
+ Bytewords, fountain codes, and single- or multi-part UR strings. Transport-only. Wire-compatible with [bcur](https://github.com/qntx/bcur).
8
+
9
+ `Encoder` K==1 outbound is single-part (an ur-rs `Decoder` will not consume it). This package still decodes ur-rs `1-1`.
10
+
11
+ See [docs/](docs/).
12
+
13
+ ## License
14
+
15
+ Licensed under the MIT License ([LICENSE](LICENSE) or <https://opensource.org/licenses/MIT>).
16
+
17
+ Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project shall be licensed as above, without any additional terms or conditions.
18
+
19
+ ---
20
+
21
+ <div align="center">
22
+
23
+ A **[QuantX](https://qntx.org)** open-source project.
24
+
25
+ <a href="https://qntx.org"><img alt="QuantX" width="369" src="https://raw.githubusercontent.com/qntx/.github/main/profile/qntx.svg" /></a>
26
+
27
+ Code is law. We write both.
28
+
29
+ </div>
@@ -0,0 +1,232 @@
1
+ //#region src/error.d.ts
2
+ /** Discriminated error codes for the UR transport stack. */
3
+ type UrErrorCode = "InvalidWord" | "InvalidBytewordsChecksum" | "InvalidBytewordsLength" | "NonAscii" | "EmptyMessage" | "EmptyPart" | "InvalidFragmentLen" | "InvalidSequence" | "InconsistentPart" | "InvalidPadding" | "InvalidMessageChecksum" | "InvalidPartCbor" | "DecoderState" | "SinglePartExhausted" | "ResourceLimit" | "InvalidScheme" | "TypeUnspecified" | "InvalidType" | "InvalidIndices" | "NotSinglePart" | "UnexpectedType";
4
+ /** Structured error thrown by the UR stack. */
5
+ declare class UrError extends Error {
6
+ readonly code: UrErrorCode;
7
+ readonly expected?: string;
8
+ readonly found?: string;
9
+ readonly limit?: string;
10
+ constructor(code: UrErrorCode, options?: {
11
+ expected?: string;
12
+ found?: string;
13
+ limit?: string;
14
+ cause?: unknown;
15
+ });
16
+ }
17
+ /** Fail-closed decoder poison reason. */
18
+ type DecoderPoison = {
19
+ code: "ResourceLimit";
20
+ limit: string;
21
+ } | {
22
+ code: "DecoderState";
23
+ };
24
+ /** Rethrows the stored poison as a {@link UrError}. */
25
+ declare function failPoison(p: DecoderPoison): never;
26
+ //#endregion
27
+ //#region src/crc32.d.ts
28
+ /**
29
+ * CRC-32 ISO-HDLC (polynomial 0xedb88320), matching bcur / ur-rs.
30
+ */
31
+ /** CRC-32 ISO-HDLC checksum of `data`. */
32
+ declare function checksum(data: Uint8Array): number;
33
+ //#endregion
34
+ //#region src/constants.d.ts
35
+ /** BCR-2020-012 bytewords tables. */
36
+ /** Four-letter bytewords (index = byte value). */
37
+ declare const WORDS: readonly string[];
38
+ /** First+last letter of each byteword (UR minimal style). */
39
+ declare const MINIMALS: readonly string[];
40
+ declare namespace index_d_exports {
41
+ export { MINIMALS, Style, WORDS, canonicalizeByteword, decode$1 as decode, encode$1 as encode, encodeRaw };
42
+ }
43
+ /** Bytewords encoding styles (BCR-2020-012). */
44
+ type Style = "standard" | "uri" | "minimal";
45
+ /** Encode `data` with trailing CRC-32 as bytewords. Empty data is allowed. */
46
+ declare function encode$1(data: Uint8Array, style: Style): string;
47
+ /**
48
+ * Encode without CRC trailer.
49
+ * Not for UR bodies — identifiers only.
50
+ */
51
+ declare function encodeRaw(data: Uint8Array, style: Style): string;
52
+ /** Decode bytewords and verify CRC-32. Case-insensitive. */
53
+ declare function decode$1(encoded: string, style: Style): Uint8Array;
54
+ /** Canonicalize a 2–4 letter token to the full 4-letter lowercase byteword. */
55
+ declare function canonicalizeByteword(token: string): string | undefined;
56
+ //#endregion
57
+ //#region src/fountain/limits.d.ts
58
+ /** Hard limits for adversarial multi-part streams. */
59
+ interface DecoderLimits {
60
+ maxMessageLength: number;
61
+ maxFragmentCount: number;
62
+ maxFragmentDataLength: number;
63
+ maxBufferParts: number;
64
+ maxReceivedParts: number;
65
+ maxUriLen: number;
66
+ }
67
+ /** Production Default; same integers as bcur 1.0. Hosts that need another budget pass `new Decoder({ limits })`. */
68
+ declare const DEFAULT_LIMITS: DecoderLimits;
69
+ //#endregion
70
+ //#region src/fountain/part.d.ts
71
+ /** A fountain part (wire metadata + fragment data). */
72
+ declare class Part {
73
+ readonly sequence: number;
74
+ readonly sequenceCount: number;
75
+ readonly messageLength: number;
76
+ readonly checksum: number;
77
+ readonly data: Uint8Array;
78
+ private constructor();
79
+ static fromFields(sequence: number, sequenceCount: number, messageLength: number, checksum: number, data: Uint8Array): Part;
80
+ indexes(): number[];
81
+ isSimple(): boolean;
82
+ toCbor(): Uint8Array;
83
+ static fromCbor(bytes: Uint8Array, maxDataLen?: number, maxFragmentCount?: number): Part;
84
+ sequenceId(): string;
85
+ }
86
+ //#endregion
87
+ //#region src/fountain/decoder.d.ts
88
+ /** Fountain decoder with resource limits and fail-closed poison. */
89
+ declare class FountainDecoder {
90
+ private readonly decoded;
91
+ private readonly received;
92
+ private readonly buffer;
93
+ private readonly queue;
94
+ private sequenceCount;
95
+ private messageLength;
96
+ private messageChecksum;
97
+ private fragmentLength;
98
+ private readonly limits;
99
+ private poisoned;
100
+ constructor(limits?: Partial<DecoderLimits>);
101
+ get maxFragmentDataLength(): number;
102
+ get maxFragmentCount(): number;
103
+ get isPoisoned(): boolean;
104
+ get poisonState(): DecoderPoison | undefined;
105
+ private poison;
106
+ private escalate;
107
+ /**
108
+ * Receive a fountain part.
109
+ * @returns whether the part was newly ingested.
110
+ */
111
+ receive(part: Part): boolean;
112
+ private enqueueSimple;
113
+ private processQueue;
114
+ private reduceBufferedPart;
115
+ private processComplex;
116
+ private insertReduced;
117
+ get complete(): boolean;
118
+ resolvedFragmentCount(): number | undefined;
119
+ get fragmentCount(): number;
120
+ validate(part: Part): boolean;
121
+ /** Decoded message if complete; otherwise `undefined`. */
122
+ message(): Uint8Array | undefined;
123
+ }
124
+ //#endregion
125
+ //#region src/fountain/encoder.d.ts
126
+ /** Fountain encoder. */
127
+ declare class FountainEncoder {
128
+ private readonly parts;
129
+ private readonly sequenceCount;
130
+ private readonly messageLength;
131
+ private readonly messageChecksum;
132
+ private currentSequence;
133
+ private constructor();
134
+ static create(message: Uint8Array, maxFragmentLength: number): FountainEncoder;
135
+ get currentSequenceNum(): number;
136
+ get fragmentCount(): number;
137
+ get complete(): boolean;
138
+ nextPart(): Part;
139
+ }
140
+ //#endregion
141
+ //#region src/ur/type.d.ts
142
+ /** Validated UR type token (stored lowercase, `[a-z0-9-]+`). */
143
+ declare class UrType {
144
+ readonly value: string;
145
+ private constructor();
146
+ static parse(s: string): UrType;
147
+ static bytes(): UrType;
148
+ equals(other: UrType): boolean;
149
+ }
150
+ //#endregion
151
+ //#region src/ur/parse.d.ts
152
+ type Kind = "single" | "multi";
153
+ interface ParsedUr {
154
+ type: UrType;
155
+ kind: Kind;
156
+ indices?: {
157
+ seq: number;
158
+ count: number;
159
+ };
160
+ body: string;
161
+ }
162
+ /** Lowercase a UR string for case-insensitive QR transport. */
163
+ declare function normalizeUr(uri: string): string;
164
+ /** Parse a UR (full-URI case fold). Does not decode bytewords. */
165
+ declare function parse(uri: string): ParsedUr;
166
+ /** Parse an already-lowercased (or body-normalized) UR. */
167
+ declare function parseNormalized(uri: string): ParsedUr;
168
+ //#endregion
169
+ //#region src/ur/index.d.ts
170
+ /** Encode a single-part UR. Empty data is allowed. */
171
+ declare function encode(data: Uint8Array, type: UrType): string;
172
+ /**
173
+ * Decode payload from a single- or multi-part UR.
174
+ * Multi-part returns the CBOR-encoded fountain part bytes, not the message.
175
+ */
176
+ declare function decode(uri: string): {
177
+ kind: Kind;
178
+ payload: Uint8Array;
179
+ };
180
+ /** Like {@link decode} but retains the normalized type. */
181
+ declare function decodeWithType(uri: string): {
182
+ type: UrType;
183
+ kind: Kind;
184
+ payload: Uint8Array;
185
+ };
186
+ /** Decode a single-part UR payload. Multi-part URIs throw `NotSinglePart`. */
187
+ declare function decodeMessage(uri: string): Uint8Array;
188
+ /** Uppercase UR string for denser QR alphanumeric mode. */
189
+ declare function toQrString(uri: string): string;
190
+ /** UR encoder. `K == 1` emits single-part; otherwise fountain. */
191
+ declare class Encoder {
192
+ private readonly fountain;
193
+ private readonly urType;
194
+ private readonly message;
195
+ private singleEmitted;
196
+ private constructor();
197
+ static create(message: Uint8Array, maxFragmentLength: number, type: UrType): Encoder;
198
+ static bytes(message: Uint8Array, maxFragmentLength: number): Encoder;
199
+ get isSinglePart(): boolean;
200
+ get fragmentCount(): number;
201
+ get currentIndex(): number;
202
+ get complete(): boolean;
203
+ nextPart(): string;
204
+ }
205
+ /** UR decoder with type stickiness and URI limits. */
206
+ declare class Decoder {
207
+ private readonly fountain;
208
+ private readonly maxUriLen;
209
+ private readonly maxMessageLength;
210
+ private readonly expectedType;
211
+ private seenType;
212
+ private single;
213
+ private poisoned;
214
+ constructor(options?: {
215
+ limits?: Partial<DecoderLimits>;
216
+ expectedType?: UrType;
217
+ });
218
+ private poison;
219
+ private escalate;
220
+ get poisonState(): DecoderPoison | undefined;
221
+ receive(uri: string): void;
222
+ private receiveSingle;
223
+ private receiveFountain;
224
+ get complete(): boolean;
225
+ message(): Uint8Array | undefined;
226
+ resolvedFragmentCount(): number | undefined;
227
+ get fragmentCount(): number;
228
+ get type(): UrType | undefined;
229
+ get isPoisoned(): boolean;
230
+ }
231
+ //#endregion
232
+ export { DEFAULT_LIMITS, Decoder, type DecoderLimits, type DecoderPoison, Encoder, FountainDecoder, FountainEncoder, type Kind, type ParsedUr, Part, type Style, UrError, type UrErrorCode, UrType, index_d_exports as bytewords, canonicalizeByteword, checksum as crc32, decode, decode$1 as decodeBytewords, decodeMessage, decodeWithType, encode, encode$1 as encodeBytewords, encodeRaw, failPoison, normalizeUr, parse, parseNormalized, toQrString };