@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.
- package/LICENSE-APACHE +201 -0
- package/README.md +7 -6
- package/dist/index-CcMfgay6.d.mts +54 -0
- package/dist/index-DTjNCWBI.d.mts +228 -0
- package/dist/index.d.mts +44 -163
- package/dist/index.mjs +2 -2
- package/dist/registry.d.mts +92 -33
- package/dist/registry.mjs +443 -93
- package/dist/typed-D-SyraAj.mjs +124 -0
- package/dist/typed.d.mts +3 -3
- package/dist/typed.mjs +3 -3
- package/dist/ur-7Ge3roVh.mjs +1993 -0
- package/package.json +22 -15
- package/dist/index-BeG2XIr5.d.mts +0 -59
- package/dist/rolldown-runtime-D7D4PA-g.mjs +0 -13
- package/dist/type-CZ0CIHJk.d.mts +0 -54
- package/dist/typed-BmHx2GWT.mjs +0 -150
- package/dist/ur-BUUcXyia.mjs +0 -1577
- /package/{LICENSE → LICENSE-MIT} +0 -0
package/dist/ur-BUUcXyia.mjs
DELETED
|
@@ -1,1577 +0,0 @@
|
|
|
1
|
-
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
-
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
|
-
//#region src/error.ts
|
|
4
|
-
const MESSAGES = {
|
|
5
|
-
InvalidWord: "invalid bytewords word",
|
|
6
|
-
InvalidBytewordsChecksum: "invalid bytewords checksum",
|
|
7
|
-
InvalidBytewordsLength: "invalid bytewords length",
|
|
8
|
-
NonAscii: "bytewords string is not ASCII",
|
|
9
|
-
EmptyMessage: "empty message",
|
|
10
|
-
EmptyPart: "empty fountain part",
|
|
11
|
-
InvalidFragmentLen: "invalid maximum fragment length",
|
|
12
|
-
InvalidSequence: "invalid sequence number",
|
|
13
|
-
InconsistentPart: "fountain part inconsistent with previous parts",
|
|
14
|
-
InvalidPadding: "invalid fountain part padding",
|
|
15
|
-
InvalidMessageChecksum: "invalid fountain message checksum",
|
|
16
|
-
InvalidPartCbor: "invalid fountain part CBOR",
|
|
17
|
-
DecoderState: "fountain decoder internal state error",
|
|
18
|
-
SinglePartExhausted: "single-part fountain encoder exhausted",
|
|
19
|
-
ResourceLimit: "resource limit exceeded",
|
|
20
|
-
InvalidScheme: "invalid UR scheme",
|
|
21
|
-
TypeUnspecified: "UR type unspecified",
|
|
22
|
-
InvalidType: "invalid UR type",
|
|
23
|
-
InvalidIndices: "invalid multi-part indices",
|
|
24
|
-
NotSinglePart: "expected single-part UR",
|
|
25
|
-
UnexpectedType: "unexpected UR type",
|
|
26
|
-
CborDecode: "dCBOR decode failed",
|
|
27
|
-
CborType: "dCBOR type mismatch"
|
|
28
|
-
};
|
|
29
|
-
/** Structured error thrown by the UR stack. */
|
|
30
|
-
var UrError = class extends Error {
|
|
31
|
-
code;
|
|
32
|
-
expected;
|
|
33
|
-
found;
|
|
34
|
-
limit;
|
|
35
|
-
constructor(code, options) {
|
|
36
|
-
const expected = options?.expected;
|
|
37
|
-
const found = options?.found;
|
|
38
|
-
const limit = options?.limit;
|
|
39
|
-
let message = MESSAGES[code];
|
|
40
|
-
if (code === "ResourceLimit" && limit !== void 0 && limit !== "") message = `${message}: ${limit}`;
|
|
41
|
-
else if (code === "UnexpectedType" && expected !== void 0 && expected !== "" && found !== void 0 && found !== "") message = `${message}: expected ${expected}, found ${found}`;
|
|
42
|
-
super(message, options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
43
|
-
this.name = "UrError";
|
|
44
|
-
this.code = code;
|
|
45
|
-
this.expected = expected;
|
|
46
|
-
this.found = found;
|
|
47
|
-
this.limit = limit;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
/** Throws a {@link UrError} with the given code. */
|
|
51
|
-
function fail(code, options) {
|
|
52
|
-
throw new UrError(code, options);
|
|
53
|
-
}
|
|
54
|
-
/** Rethrows the stored poison as a {@link UrError}. */
|
|
55
|
-
function failPoison(p) {
|
|
56
|
-
if (p.code === "ResourceLimit") fail("ResourceLimit", { limit: p.limit });
|
|
57
|
-
fail("DecoderState");
|
|
58
|
-
}
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/crc32.ts
|
|
61
|
-
/** CRC-32 ISO-HDLC (polynomial 0xedb88320), matching bcur / ur-rs. */
|
|
62
|
-
const TABLE = (() => {
|
|
63
|
-
const table = /* @__PURE__ */ new Uint32Array(256);
|
|
64
|
-
for (let i = 0; i < 256; i++) {
|
|
65
|
-
let c = i;
|
|
66
|
-
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
67
|
-
table[i] = c >>> 0;
|
|
68
|
-
}
|
|
69
|
-
return table;
|
|
70
|
-
})();
|
|
71
|
-
/** CRC-32 ISO-HDLC checksum of `data`. */
|
|
72
|
-
function checksum(data) {
|
|
73
|
-
let crc = 4294967295;
|
|
74
|
-
for (const b of data) crc = (TABLE[(crc ^ b) & 255] ?? fail("DecoderState")) ^ crc >>> 8;
|
|
75
|
-
return (crc ^ 4294967295) >>> 0;
|
|
76
|
-
}
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/constants.ts
|
|
79
|
-
/** BCR-2020-012 bytewords tables. */
|
|
80
|
-
/** Four-letter bytewords (index = byte value). */
|
|
81
|
-
const WORDS = [
|
|
82
|
-
"able",
|
|
83
|
-
"acid",
|
|
84
|
-
"also",
|
|
85
|
-
"apex",
|
|
86
|
-
"aqua",
|
|
87
|
-
"arch",
|
|
88
|
-
"atom",
|
|
89
|
-
"aunt",
|
|
90
|
-
"away",
|
|
91
|
-
"axis",
|
|
92
|
-
"back",
|
|
93
|
-
"bald",
|
|
94
|
-
"barn",
|
|
95
|
-
"belt",
|
|
96
|
-
"beta",
|
|
97
|
-
"bias",
|
|
98
|
-
"blue",
|
|
99
|
-
"body",
|
|
100
|
-
"brag",
|
|
101
|
-
"brew",
|
|
102
|
-
"bulb",
|
|
103
|
-
"buzz",
|
|
104
|
-
"calm",
|
|
105
|
-
"cash",
|
|
106
|
-
"cats",
|
|
107
|
-
"chef",
|
|
108
|
-
"city",
|
|
109
|
-
"claw",
|
|
110
|
-
"code",
|
|
111
|
-
"cola",
|
|
112
|
-
"cook",
|
|
113
|
-
"cost",
|
|
114
|
-
"crux",
|
|
115
|
-
"curl",
|
|
116
|
-
"cusp",
|
|
117
|
-
"cyan",
|
|
118
|
-
"dark",
|
|
119
|
-
"data",
|
|
120
|
-
"days",
|
|
121
|
-
"deli",
|
|
122
|
-
"dice",
|
|
123
|
-
"diet",
|
|
124
|
-
"door",
|
|
125
|
-
"down",
|
|
126
|
-
"draw",
|
|
127
|
-
"drop",
|
|
128
|
-
"drum",
|
|
129
|
-
"dull",
|
|
130
|
-
"duty",
|
|
131
|
-
"each",
|
|
132
|
-
"easy",
|
|
133
|
-
"echo",
|
|
134
|
-
"edge",
|
|
135
|
-
"epic",
|
|
136
|
-
"even",
|
|
137
|
-
"exam",
|
|
138
|
-
"exit",
|
|
139
|
-
"eyes",
|
|
140
|
-
"fact",
|
|
141
|
-
"fair",
|
|
142
|
-
"fern",
|
|
143
|
-
"figs",
|
|
144
|
-
"film",
|
|
145
|
-
"fish",
|
|
146
|
-
"fizz",
|
|
147
|
-
"flap",
|
|
148
|
-
"flew",
|
|
149
|
-
"flux",
|
|
150
|
-
"foxy",
|
|
151
|
-
"free",
|
|
152
|
-
"frog",
|
|
153
|
-
"fuel",
|
|
154
|
-
"fund",
|
|
155
|
-
"gala",
|
|
156
|
-
"game",
|
|
157
|
-
"gear",
|
|
158
|
-
"gems",
|
|
159
|
-
"gift",
|
|
160
|
-
"girl",
|
|
161
|
-
"glow",
|
|
162
|
-
"good",
|
|
163
|
-
"gray",
|
|
164
|
-
"grim",
|
|
165
|
-
"guru",
|
|
166
|
-
"gush",
|
|
167
|
-
"gyro",
|
|
168
|
-
"half",
|
|
169
|
-
"hang",
|
|
170
|
-
"hard",
|
|
171
|
-
"hawk",
|
|
172
|
-
"heat",
|
|
173
|
-
"help",
|
|
174
|
-
"high",
|
|
175
|
-
"hill",
|
|
176
|
-
"holy",
|
|
177
|
-
"hope",
|
|
178
|
-
"horn",
|
|
179
|
-
"huts",
|
|
180
|
-
"iced",
|
|
181
|
-
"idea",
|
|
182
|
-
"idle",
|
|
183
|
-
"inch",
|
|
184
|
-
"inky",
|
|
185
|
-
"into",
|
|
186
|
-
"iris",
|
|
187
|
-
"iron",
|
|
188
|
-
"item",
|
|
189
|
-
"jade",
|
|
190
|
-
"jazz",
|
|
191
|
-
"join",
|
|
192
|
-
"jolt",
|
|
193
|
-
"jowl",
|
|
194
|
-
"judo",
|
|
195
|
-
"jugs",
|
|
196
|
-
"jump",
|
|
197
|
-
"junk",
|
|
198
|
-
"jury",
|
|
199
|
-
"keep",
|
|
200
|
-
"keno",
|
|
201
|
-
"kept",
|
|
202
|
-
"keys",
|
|
203
|
-
"kick",
|
|
204
|
-
"kiln",
|
|
205
|
-
"king",
|
|
206
|
-
"kite",
|
|
207
|
-
"kiwi",
|
|
208
|
-
"knob",
|
|
209
|
-
"lamb",
|
|
210
|
-
"lava",
|
|
211
|
-
"lazy",
|
|
212
|
-
"leaf",
|
|
213
|
-
"legs",
|
|
214
|
-
"liar",
|
|
215
|
-
"limp",
|
|
216
|
-
"lion",
|
|
217
|
-
"list",
|
|
218
|
-
"logo",
|
|
219
|
-
"loud",
|
|
220
|
-
"love",
|
|
221
|
-
"luau",
|
|
222
|
-
"luck",
|
|
223
|
-
"lung",
|
|
224
|
-
"main",
|
|
225
|
-
"many",
|
|
226
|
-
"math",
|
|
227
|
-
"maze",
|
|
228
|
-
"memo",
|
|
229
|
-
"menu",
|
|
230
|
-
"meow",
|
|
231
|
-
"mild",
|
|
232
|
-
"mint",
|
|
233
|
-
"miss",
|
|
234
|
-
"monk",
|
|
235
|
-
"nail",
|
|
236
|
-
"navy",
|
|
237
|
-
"need",
|
|
238
|
-
"news",
|
|
239
|
-
"next",
|
|
240
|
-
"noon",
|
|
241
|
-
"note",
|
|
242
|
-
"numb",
|
|
243
|
-
"obey",
|
|
244
|
-
"oboe",
|
|
245
|
-
"omit",
|
|
246
|
-
"onyx",
|
|
247
|
-
"open",
|
|
248
|
-
"oval",
|
|
249
|
-
"owls",
|
|
250
|
-
"paid",
|
|
251
|
-
"part",
|
|
252
|
-
"peck",
|
|
253
|
-
"play",
|
|
254
|
-
"plus",
|
|
255
|
-
"poem",
|
|
256
|
-
"pool",
|
|
257
|
-
"pose",
|
|
258
|
-
"puff",
|
|
259
|
-
"puma",
|
|
260
|
-
"purr",
|
|
261
|
-
"quad",
|
|
262
|
-
"quiz",
|
|
263
|
-
"race",
|
|
264
|
-
"ramp",
|
|
265
|
-
"real",
|
|
266
|
-
"redo",
|
|
267
|
-
"rich",
|
|
268
|
-
"road",
|
|
269
|
-
"rock",
|
|
270
|
-
"roof",
|
|
271
|
-
"ruby",
|
|
272
|
-
"ruin",
|
|
273
|
-
"runs",
|
|
274
|
-
"rust",
|
|
275
|
-
"safe",
|
|
276
|
-
"saga",
|
|
277
|
-
"scar",
|
|
278
|
-
"sets",
|
|
279
|
-
"silk",
|
|
280
|
-
"skew",
|
|
281
|
-
"slot",
|
|
282
|
-
"soap",
|
|
283
|
-
"solo",
|
|
284
|
-
"song",
|
|
285
|
-
"stub",
|
|
286
|
-
"surf",
|
|
287
|
-
"swan",
|
|
288
|
-
"taco",
|
|
289
|
-
"task",
|
|
290
|
-
"taxi",
|
|
291
|
-
"tent",
|
|
292
|
-
"tied",
|
|
293
|
-
"time",
|
|
294
|
-
"tiny",
|
|
295
|
-
"toil",
|
|
296
|
-
"tomb",
|
|
297
|
-
"toys",
|
|
298
|
-
"trip",
|
|
299
|
-
"tuna",
|
|
300
|
-
"twin",
|
|
301
|
-
"ugly",
|
|
302
|
-
"undo",
|
|
303
|
-
"unit",
|
|
304
|
-
"urge",
|
|
305
|
-
"user",
|
|
306
|
-
"vast",
|
|
307
|
-
"very",
|
|
308
|
-
"veto",
|
|
309
|
-
"vial",
|
|
310
|
-
"vibe",
|
|
311
|
-
"view",
|
|
312
|
-
"visa",
|
|
313
|
-
"void",
|
|
314
|
-
"vows",
|
|
315
|
-
"wall",
|
|
316
|
-
"wand",
|
|
317
|
-
"warm",
|
|
318
|
-
"wasp",
|
|
319
|
-
"wave",
|
|
320
|
-
"waxy",
|
|
321
|
-
"webs",
|
|
322
|
-
"what",
|
|
323
|
-
"when",
|
|
324
|
-
"whiz",
|
|
325
|
-
"wolf",
|
|
326
|
-
"work",
|
|
327
|
-
"yank",
|
|
328
|
-
"yawn",
|
|
329
|
-
"yell",
|
|
330
|
-
"yoga",
|
|
331
|
-
"yurt",
|
|
332
|
-
"zaps",
|
|
333
|
-
"zero",
|
|
334
|
-
"zest",
|
|
335
|
-
"zinc",
|
|
336
|
-
"zone",
|
|
337
|
-
"zoom"
|
|
338
|
-
];
|
|
339
|
-
/** First+last letter of each byteword (UR minimal style). */
|
|
340
|
-
const MINIMALS = [
|
|
341
|
-
"ae",
|
|
342
|
-
"ad",
|
|
343
|
-
"ao",
|
|
344
|
-
"ax",
|
|
345
|
-
"aa",
|
|
346
|
-
"ah",
|
|
347
|
-
"am",
|
|
348
|
-
"at",
|
|
349
|
-
"ay",
|
|
350
|
-
"as",
|
|
351
|
-
"bk",
|
|
352
|
-
"bd",
|
|
353
|
-
"bn",
|
|
354
|
-
"bt",
|
|
355
|
-
"ba",
|
|
356
|
-
"bs",
|
|
357
|
-
"be",
|
|
358
|
-
"by",
|
|
359
|
-
"bg",
|
|
360
|
-
"bw",
|
|
361
|
-
"bb",
|
|
362
|
-
"bz",
|
|
363
|
-
"cm",
|
|
364
|
-
"ch",
|
|
365
|
-
"cs",
|
|
366
|
-
"cf",
|
|
367
|
-
"cy",
|
|
368
|
-
"cw",
|
|
369
|
-
"ce",
|
|
370
|
-
"ca",
|
|
371
|
-
"ck",
|
|
372
|
-
"ct",
|
|
373
|
-
"cx",
|
|
374
|
-
"cl",
|
|
375
|
-
"cp",
|
|
376
|
-
"cn",
|
|
377
|
-
"dk",
|
|
378
|
-
"da",
|
|
379
|
-
"ds",
|
|
380
|
-
"di",
|
|
381
|
-
"de",
|
|
382
|
-
"dt",
|
|
383
|
-
"dr",
|
|
384
|
-
"dn",
|
|
385
|
-
"dw",
|
|
386
|
-
"dp",
|
|
387
|
-
"dm",
|
|
388
|
-
"dl",
|
|
389
|
-
"dy",
|
|
390
|
-
"eh",
|
|
391
|
-
"ey",
|
|
392
|
-
"eo",
|
|
393
|
-
"ee",
|
|
394
|
-
"ec",
|
|
395
|
-
"en",
|
|
396
|
-
"em",
|
|
397
|
-
"et",
|
|
398
|
-
"es",
|
|
399
|
-
"ft",
|
|
400
|
-
"fr",
|
|
401
|
-
"fn",
|
|
402
|
-
"fs",
|
|
403
|
-
"fm",
|
|
404
|
-
"fh",
|
|
405
|
-
"fz",
|
|
406
|
-
"fp",
|
|
407
|
-
"fw",
|
|
408
|
-
"fx",
|
|
409
|
-
"fy",
|
|
410
|
-
"fe",
|
|
411
|
-
"fg",
|
|
412
|
-
"fl",
|
|
413
|
-
"fd",
|
|
414
|
-
"ga",
|
|
415
|
-
"ge",
|
|
416
|
-
"gr",
|
|
417
|
-
"gs",
|
|
418
|
-
"gt",
|
|
419
|
-
"gl",
|
|
420
|
-
"gw",
|
|
421
|
-
"gd",
|
|
422
|
-
"gy",
|
|
423
|
-
"gm",
|
|
424
|
-
"gu",
|
|
425
|
-
"gh",
|
|
426
|
-
"go",
|
|
427
|
-
"hf",
|
|
428
|
-
"hg",
|
|
429
|
-
"hd",
|
|
430
|
-
"hk",
|
|
431
|
-
"ht",
|
|
432
|
-
"hp",
|
|
433
|
-
"hh",
|
|
434
|
-
"hl",
|
|
435
|
-
"hy",
|
|
436
|
-
"he",
|
|
437
|
-
"hn",
|
|
438
|
-
"hs",
|
|
439
|
-
"id",
|
|
440
|
-
"ia",
|
|
441
|
-
"ie",
|
|
442
|
-
"ih",
|
|
443
|
-
"iy",
|
|
444
|
-
"io",
|
|
445
|
-
"is",
|
|
446
|
-
"in",
|
|
447
|
-
"im",
|
|
448
|
-
"je",
|
|
449
|
-
"jz",
|
|
450
|
-
"jn",
|
|
451
|
-
"jt",
|
|
452
|
-
"jl",
|
|
453
|
-
"jo",
|
|
454
|
-
"js",
|
|
455
|
-
"jp",
|
|
456
|
-
"jk",
|
|
457
|
-
"jy",
|
|
458
|
-
"kp",
|
|
459
|
-
"ko",
|
|
460
|
-
"kt",
|
|
461
|
-
"ks",
|
|
462
|
-
"kk",
|
|
463
|
-
"kn",
|
|
464
|
-
"kg",
|
|
465
|
-
"ke",
|
|
466
|
-
"ki",
|
|
467
|
-
"kb",
|
|
468
|
-
"lb",
|
|
469
|
-
"la",
|
|
470
|
-
"ly",
|
|
471
|
-
"lf",
|
|
472
|
-
"ls",
|
|
473
|
-
"lr",
|
|
474
|
-
"lp",
|
|
475
|
-
"ln",
|
|
476
|
-
"lt",
|
|
477
|
-
"lo",
|
|
478
|
-
"ld",
|
|
479
|
-
"le",
|
|
480
|
-
"lu",
|
|
481
|
-
"lk",
|
|
482
|
-
"lg",
|
|
483
|
-
"mn",
|
|
484
|
-
"my",
|
|
485
|
-
"mh",
|
|
486
|
-
"me",
|
|
487
|
-
"mo",
|
|
488
|
-
"mu",
|
|
489
|
-
"mw",
|
|
490
|
-
"md",
|
|
491
|
-
"mt",
|
|
492
|
-
"ms",
|
|
493
|
-
"mk",
|
|
494
|
-
"nl",
|
|
495
|
-
"ny",
|
|
496
|
-
"nd",
|
|
497
|
-
"ns",
|
|
498
|
-
"nt",
|
|
499
|
-
"nn",
|
|
500
|
-
"ne",
|
|
501
|
-
"nb",
|
|
502
|
-
"oy",
|
|
503
|
-
"oe",
|
|
504
|
-
"ot",
|
|
505
|
-
"ox",
|
|
506
|
-
"on",
|
|
507
|
-
"ol",
|
|
508
|
-
"os",
|
|
509
|
-
"pd",
|
|
510
|
-
"pt",
|
|
511
|
-
"pk",
|
|
512
|
-
"py",
|
|
513
|
-
"ps",
|
|
514
|
-
"pm",
|
|
515
|
-
"pl",
|
|
516
|
-
"pe",
|
|
517
|
-
"pf",
|
|
518
|
-
"pa",
|
|
519
|
-
"pr",
|
|
520
|
-
"qd",
|
|
521
|
-
"qz",
|
|
522
|
-
"re",
|
|
523
|
-
"rp",
|
|
524
|
-
"rl",
|
|
525
|
-
"ro",
|
|
526
|
-
"rh",
|
|
527
|
-
"rd",
|
|
528
|
-
"rk",
|
|
529
|
-
"rf",
|
|
530
|
-
"ry",
|
|
531
|
-
"rn",
|
|
532
|
-
"rs",
|
|
533
|
-
"rt",
|
|
534
|
-
"se",
|
|
535
|
-
"sa",
|
|
536
|
-
"sr",
|
|
537
|
-
"ss",
|
|
538
|
-
"sk",
|
|
539
|
-
"sw",
|
|
540
|
-
"st",
|
|
541
|
-
"sp",
|
|
542
|
-
"so",
|
|
543
|
-
"sg",
|
|
544
|
-
"sb",
|
|
545
|
-
"sf",
|
|
546
|
-
"sn",
|
|
547
|
-
"to",
|
|
548
|
-
"tk",
|
|
549
|
-
"ti",
|
|
550
|
-
"tt",
|
|
551
|
-
"td",
|
|
552
|
-
"te",
|
|
553
|
-
"ty",
|
|
554
|
-
"tl",
|
|
555
|
-
"tb",
|
|
556
|
-
"ts",
|
|
557
|
-
"tp",
|
|
558
|
-
"ta",
|
|
559
|
-
"tn",
|
|
560
|
-
"uy",
|
|
561
|
-
"uo",
|
|
562
|
-
"ut",
|
|
563
|
-
"ue",
|
|
564
|
-
"ur",
|
|
565
|
-
"vt",
|
|
566
|
-
"vy",
|
|
567
|
-
"vo",
|
|
568
|
-
"vl",
|
|
569
|
-
"ve",
|
|
570
|
-
"vw",
|
|
571
|
-
"va",
|
|
572
|
-
"vd",
|
|
573
|
-
"vs",
|
|
574
|
-
"wl",
|
|
575
|
-
"wd",
|
|
576
|
-
"wm",
|
|
577
|
-
"wp",
|
|
578
|
-
"we",
|
|
579
|
-
"wy",
|
|
580
|
-
"ws",
|
|
581
|
-
"wt",
|
|
582
|
-
"wn",
|
|
583
|
-
"wz",
|
|
584
|
-
"wf",
|
|
585
|
-
"wk",
|
|
586
|
-
"yk",
|
|
587
|
-
"yn",
|
|
588
|
-
"yl",
|
|
589
|
-
"ya",
|
|
590
|
-
"yt",
|
|
591
|
-
"zs",
|
|
592
|
-
"zo",
|
|
593
|
-
"zt",
|
|
594
|
-
"zc",
|
|
595
|
-
"ze",
|
|
596
|
-
"zm"
|
|
597
|
-
];
|
|
598
|
-
//#endregion
|
|
599
|
-
//#region src/bytewords/index.ts
|
|
600
|
-
var bytewords_exports = /* @__PURE__ */ __exportAll({
|
|
601
|
-
MINIMALS: () => MINIMALS,
|
|
602
|
-
WORDS: () => WORDS,
|
|
603
|
-
canonicalizeByteword: () => canonicalizeByteword,
|
|
604
|
-
decode: () => decode$1,
|
|
605
|
-
encode: () => encode$1,
|
|
606
|
-
encodeRaw: () => encodeRaw
|
|
607
|
-
});
|
|
608
|
-
const wordByToken = /* @__PURE__ */ new Map();
|
|
609
|
-
for (const [i, word] of WORDS.entries()) wordByToken.set(word, i);
|
|
610
|
-
for (const [i, minimal] of MINIMALS.entries()) wordByToken.set(minimal, i);
|
|
611
|
-
function wordAt(table, byte) {
|
|
612
|
-
const word = table[byte];
|
|
613
|
-
if (word === void 0) throw new Error("byteword index out of range");
|
|
614
|
-
return word;
|
|
615
|
-
}
|
|
616
|
-
/** Encode `data` with trailing CRC-32 as bytewords. Empty data is allowed. */
|
|
617
|
-
function encode$1(data, style) {
|
|
618
|
-
const crc = checksum(data);
|
|
619
|
-
const withCrc = new Uint8Array(data.length + 4);
|
|
620
|
-
withCrc.set(data);
|
|
621
|
-
withCrc[data.length] = crc >>> 24 & 255;
|
|
622
|
-
withCrc[data.length + 1] = crc >>> 16 & 255;
|
|
623
|
-
withCrc[data.length + 2] = crc >>> 8 & 255;
|
|
624
|
-
withCrc[data.length + 3] = crc & 255;
|
|
625
|
-
return encodeWords(withCrc, style);
|
|
626
|
-
}
|
|
627
|
-
/** Encode without CRC trailer. Not for UR bodies — identifiers only. */
|
|
628
|
-
function encodeRaw(data, style) {
|
|
629
|
-
return encodeWords(data, style);
|
|
630
|
-
}
|
|
631
|
-
function encodeWords(data, style) {
|
|
632
|
-
const parts = [];
|
|
633
|
-
const table = style === "minimal" ? MINIMALS : WORDS;
|
|
634
|
-
for (const b of data) parts.push(wordAt(table, b));
|
|
635
|
-
return parts.join({
|
|
636
|
-
standard: " ",
|
|
637
|
-
uri: "-",
|
|
638
|
-
minimal: ""
|
|
639
|
-
}[style]);
|
|
640
|
-
}
|
|
641
|
-
/** Decode bytewords and verify CRC-32. Case-insensitive. */
|
|
642
|
-
function decode$1(encoded, style) {
|
|
643
|
-
for (const ch of encoded) if ((ch.codePointAt(0) ?? 0) > 127) fail("NonAscii");
|
|
644
|
-
const lowered = encoded.toLowerCase();
|
|
645
|
-
if (style === "minimal") return decodeMinimal(lowered);
|
|
646
|
-
const sep = style === "standard" ? " " : "-";
|
|
647
|
-
return decodeParts(lowered.length === 0 ? [] : lowered.split(sep), false);
|
|
648
|
-
}
|
|
649
|
-
function decodeMinimal(encoded) {
|
|
650
|
-
if (encoded.length % 2 !== 0) fail("InvalidBytewordsLength");
|
|
651
|
-
const parts = [];
|
|
652
|
-
for (let i = 0; i < encoded.length; i += 2) parts.push(encoded.slice(i, i + 2));
|
|
653
|
-
return decodeParts(parts, true);
|
|
654
|
-
}
|
|
655
|
-
function decodeParts(parts, minimal) {
|
|
656
|
-
const data = new Uint8Array(parts.length);
|
|
657
|
-
for (const [i, part] of parts.entries()) {
|
|
658
|
-
const expectedLen = minimal ? 2 : 4;
|
|
659
|
-
if (part.length !== expectedLen) fail("InvalidWord");
|
|
660
|
-
const byte = wordByToken.get(part);
|
|
661
|
-
if (byte === void 0) fail("InvalidWord");
|
|
662
|
-
if (part !== wordAt(minimal ? MINIMALS : WORDS, byte)) fail("InvalidWord");
|
|
663
|
-
data[i] = byte;
|
|
664
|
-
}
|
|
665
|
-
return stripChecksum(data);
|
|
666
|
-
}
|
|
667
|
-
function stripChecksum(data) {
|
|
668
|
-
if (data.length < 4) fail("InvalidBytewordsChecksum");
|
|
669
|
-
const split = data.length - 4;
|
|
670
|
-
const payload = data.subarray(0, split);
|
|
671
|
-
const expected = checksum(payload);
|
|
672
|
-
if (new DataView(data.buffer, data.byteOffset + split, 4).getUint32(0) !== expected) fail("InvalidBytewordsChecksum");
|
|
673
|
-
return new Uint8Array(payload);
|
|
674
|
-
}
|
|
675
|
-
/** Canonicalize a 2–4 letter token to the full 4-letter lowercase byteword. */
|
|
676
|
-
function canonicalizeByteword(token) {
|
|
677
|
-
for (const ch of token) if ((ch.codePointAt(0) ?? 0) > 127) return;
|
|
678
|
-
const lower = token.toLowerCase();
|
|
679
|
-
if (lower.length === 4) {
|
|
680
|
-
const b = wordByToken.get(lower);
|
|
681
|
-
return b === void 0 ? void 0 : WORDS[b];
|
|
682
|
-
}
|
|
683
|
-
if (lower.length === 2) {
|
|
684
|
-
const b = wordByToken.get(lower);
|
|
685
|
-
return b === void 0 ? void 0 : WORDS[b];
|
|
686
|
-
}
|
|
687
|
-
if (lower.length === 3) {
|
|
688
|
-
let found;
|
|
689
|
-
for (const word of WORDS) if (word.slice(0, 3) === lower || word.slice(1, 4) === lower) {
|
|
690
|
-
if (found !== void 0) return;
|
|
691
|
-
found = word;
|
|
692
|
-
}
|
|
693
|
-
return found;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
//#endregion
|
|
697
|
-
//#region src/rng/sampler.ts
|
|
698
|
-
function at(list, i) {
|
|
699
|
-
const v = list[i];
|
|
700
|
-
if (v === void 0) throw new Error("unreachable: index out of range");
|
|
701
|
-
return v;
|
|
702
|
-
}
|
|
703
|
-
/** Walker's alias method with reverse-index ordered partition (ur-rs / bcur). */
|
|
704
|
-
var Weighted = class Weighted {
|
|
705
|
-
aliases;
|
|
706
|
-
probs;
|
|
707
|
-
constructor(aliases, probs) {
|
|
708
|
-
this.aliases = aliases;
|
|
709
|
-
this.probs = probs;
|
|
710
|
-
}
|
|
711
|
-
static new(weightsIn) {
|
|
712
|
-
const weights = [...weightsIn];
|
|
713
|
-
for (const w of weights) if (w < 0) throw new Error("negative probability encountered");
|
|
714
|
-
const summed = weights.reduce((a, b) => a + b, 0);
|
|
715
|
-
if (!(summed > 0)) throw new Error("probabilities don't sum to a positive value");
|
|
716
|
-
const count = weights.length;
|
|
717
|
-
for (const [i, w] of weights.entries()) weights[i] = w * (count / summed);
|
|
718
|
-
const s = [];
|
|
719
|
-
const l = [];
|
|
720
|
-
for (let j = count - 1; j >= 0; j--) if (at(weights, j) < 1) s.push(j);
|
|
721
|
-
else l.push(j);
|
|
722
|
-
const probs = Array.from({ length: count }, () => 0);
|
|
723
|
-
const aliases = Array.from({ length: count }, () => 0);
|
|
724
|
-
while (s.length > 0 && l.length > 0) {
|
|
725
|
-
const a = s.pop();
|
|
726
|
-
const g = l.pop();
|
|
727
|
-
if (a === void 0 || g === void 0) break;
|
|
728
|
-
probs[a] = at(weights, a);
|
|
729
|
-
aliases[a] = g;
|
|
730
|
-
weights[g] = at(weights, g) + (at(weights, a) - 1);
|
|
731
|
-
if (at(weights, g) < 1) s.push(g);
|
|
732
|
-
else l.push(g);
|
|
733
|
-
}
|
|
734
|
-
for (const i of l) probs[i] = 1;
|
|
735
|
-
for (const i of s) probs[i] = 1;
|
|
736
|
-
return new Weighted(aliases, probs);
|
|
737
|
-
}
|
|
738
|
-
next(xoshiro) {
|
|
739
|
-
const r1 = xoshiro.nextDouble();
|
|
740
|
-
const r2 = xoshiro.nextDouble();
|
|
741
|
-
const n = this.probs.length;
|
|
742
|
-
const i = Math.trunc(n * r1);
|
|
743
|
-
if (r2 < at(this.probs, i)) return i;
|
|
744
|
-
return at(this.aliases, i);
|
|
745
|
-
}
|
|
746
|
-
};
|
|
747
|
-
//#endregion
|
|
748
|
-
//#region src/rng/xoshiro.ts
|
|
749
|
-
const MASK64 = (1n << 64n) - 1n;
|
|
750
|
-
function rotl(x, k) {
|
|
751
|
-
const v = x & MASK64;
|
|
752
|
-
return (v << BigInt(k) | v >> BigInt(64 - k)) & MASK64;
|
|
753
|
-
}
|
|
754
|
-
function unitInterval(value) {
|
|
755
|
-
return Number(value >> 11n) * (1 / 2 ** 53);
|
|
756
|
-
}
|
|
757
|
-
/** Xoshiro256** with SHA-256 seeding matching URKit / ur-rs / bcur. */
|
|
758
|
-
var Xoshiro256 = class Xoshiro256 {
|
|
759
|
-
s0;
|
|
760
|
-
s1;
|
|
761
|
-
s2;
|
|
762
|
-
s3;
|
|
763
|
-
constructor(s0, s1, s2, s3) {
|
|
764
|
-
this.s0 = s0;
|
|
765
|
-
this.s1 = s1;
|
|
766
|
-
this.s2 = s2;
|
|
767
|
-
this.s3 = s3;
|
|
768
|
-
}
|
|
769
|
-
/** Seed from arbitrary bytes via SHA-256 + BE-limb→LE packing. */
|
|
770
|
-
static fromBytes(bytes) {
|
|
771
|
-
return Xoshiro256.fromDigest(sha256(bytes));
|
|
772
|
-
}
|
|
773
|
-
static fromString(value) {
|
|
774
|
-
return Xoshiro256.fromBytes(new TextEncoder().encode(value));
|
|
775
|
-
}
|
|
776
|
-
/** Seed from CRC-32 BE bytes of `bytes` (test helper path). */
|
|
777
|
-
static fromCrc(bytes) {
|
|
778
|
-
const c = checksum(bytes);
|
|
779
|
-
const seed = /* @__PURE__ */ new Uint8Array(4);
|
|
780
|
-
seed[0] = c >>> 24 & 255;
|
|
781
|
-
seed[1] = c >>> 16 & 255;
|
|
782
|
-
seed[2] = c >>> 8 & 255;
|
|
783
|
-
seed[3] = c & 255;
|
|
784
|
-
return Xoshiro256.fromBytes(seed);
|
|
785
|
-
}
|
|
786
|
-
static fromDigest(seed32) {
|
|
787
|
-
const view = new DataView(seed32.buffer, seed32.byteOffset, seed32.byteLength);
|
|
788
|
-
return new Xoshiro256(view.getBigUint64(0), view.getBigUint64(8), view.getBigUint64(16), view.getBigUint64(24));
|
|
789
|
-
}
|
|
790
|
-
nextU64() {
|
|
791
|
-
const result = rotl(this.s1 * 5n & MASK64, 7) * 9n & MASK64;
|
|
792
|
-
const t = this.s1 << 17n & MASK64;
|
|
793
|
-
this.s2 = (this.s2 ^ this.s0) & MASK64;
|
|
794
|
-
this.s3 = (this.s3 ^ this.s1) & MASK64;
|
|
795
|
-
this.s1 = (this.s1 ^ this.s2) & MASK64;
|
|
796
|
-
this.s0 = (this.s0 ^ this.s3) & MASK64;
|
|
797
|
-
this.s2 = (this.s2 ^ t) & MASK64;
|
|
798
|
-
this.s3 = rotl(this.s3, 45);
|
|
799
|
-
return result;
|
|
800
|
-
}
|
|
801
|
-
nextDouble() {
|
|
802
|
-
return unitInterval(this.nextU64());
|
|
803
|
-
}
|
|
804
|
-
/**
|
|
805
|
-
* Inclusive `[low, high]` via double scaling (normative float path). Truncation toward zero
|
|
806
|
-
* matches Rust `as u64`.
|
|
807
|
-
*/
|
|
808
|
-
nextInt(low, high) {
|
|
809
|
-
const span = high - low + 1;
|
|
810
|
-
return Math.trunc(this.nextDouble() * span) + low;
|
|
811
|
-
}
|
|
812
|
-
/** Remove-order shuffle (not Fisher–Yates); stops after `count` picks. */
|
|
813
|
-
shuffled(items, count = items.length) {
|
|
814
|
-
const pool = [...items];
|
|
815
|
-
const out = [];
|
|
816
|
-
while (pool.length > 0 && out.length < count) {
|
|
817
|
-
const index = this.nextInt(0, pool.length - 1);
|
|
818
|
-
const [item] = pool.splice(index, 1);
|
|
819
|
-
if (item === void 0) throw new Error("unreachable: splice returned empty");
|
|
820
|
-
out.push(item);
|
|
821
|
-
}
|
|
822
|
-
return out;
|
|
823
|
-
}
|
|
824
|
-
chooseDegree(length) {
|
|
825
|
-
const weights = [];
|
|
826
|
-
for (let x = 1; x <= length; x++) weights.push(1 / x);
|
|
827
|
-
return Weighted.new(weights).next(this) + 1;
|
|
828
|
-
}
|
|
829
|
-
nextByte() {
|
|
830
|
-
return this.nextInt(0, 255);
|
|
831
|
-
}
|
|
832
|
-
nextBytes(n) {
|
|
833
|
-
const out = new Uint8Array(n);
|
|
834
|
-
for (let i = 0; i < n; i++) out[i] = this.nextByte();
|
|
835
|
-
return out;
|
|
836
|
-
}
|
|
837
|
-
};
|
|
838
|
-
//#endregion
|
|
839
|
-
//#region src/fountain/choose.ts
|
|
840
|
-
function divCeil(a, b) {
|
|
841
|
-
return Math.trunc((a + b - 1) / b);
|
|
842
|
-
}
|
|
843
|
-
/** Optimal equal fragment length under a max cap. */
|
|
844
|
-
function fragmentLength(dataLength, maxFragmentLength) {
|
|
845
|
-
return divCeil(dataLength, divCeil(dataLength, maxFragmentLength));
|
|
846
|
-
}
|
|
847
|
-
/** Pad and split message into equal fragments. */
|
|
848
|
-
function partition(data, fragLen) {
|
|
849
|
-
const pad = (fragLen - data.length % fragLen) % fragLen;
|
|
850
|
-
const padded = new Uint8Array(data.length + pad);
|
|
851
|
-
padded.set(data);
|
|
852
|
-
const out = [];
|
|
853
|
-
for (let i = 0; i < padded.length; i += fragLen) out.push(padded.subarray(i, i + fragLen));
|
|
854
|
-
return out;
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* Per-stream index generator (BCR-2024-001 §4 FragmentChooser): harmonic degree sampler built once,
|
|
858
|
-
* partial remove-shuffle per sequence.
|
|
859
|
-
*/
|
|
860
|
-
var FragmentChooser = class {
|
|
861
|
-
fragmentCount;
|
|
862
|
-
checksum;
|
|
863
|
-
#degrees;
|
|
864
|
-
constructor(fragmentCount, checksum) {
|
|
865
|
-
this.fragmentCount = fragmentCount;
|
|
866
|
-
this.checksum = checksum;
|
|
867
|
-
const weights = [];
|
|
868
|
-
for (let x = 1; x <= fragmentCount; x++) weights.push(1 / x);
|
|
869
|
-
this.#degrees = Weighted.new(weights);
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* Fragment indexes mixed into sequence `sequence` (1-based), sorted ascending. Normative: simple
|
|
873
|
-
* if sequence <= K; else degree + remove-shuffle.
|
|
874
|
-
*/
|
|
875
|
-
choose(sequence) {
|
|
876
|
-
if (sequence <= this.fragmentCount) return [sequence - 1];
|
|
877
|
-
const seed = /* @__PURE__ */ new Uint8Array(8);
|
|
878
|
-
seed[0] = sequence >>> 24 & 255;
|
|
879
|
-
seed[1] = sequence >>> 16 & 255;
|
|
880
|
-
seed[2] = sequence >>> 8 & 255;
|
|
881
|
-
seed[3] = sequence & 255;
|
|
882
|
-
seed[4] = this.checksum >>> 24 & 255;
|
|
883
|
-
seed[5] = this.checksum >>> 16 & 255;
|
|
884
|
-
seed[6] = this.checksum >>> 8 & 255;
|
|
885
|
-
seed[7] = this.checksum & 255;
|
|
886
|
-
const xoshiro = Xoshiro256.fromBytes(seed);
|
|
887
|
-
const degree = this.#degrees.next(xoshiro) + 1;
|
|
888
|
-
const indexes = Array.from({ length: this.fragmentCount }, (_, i) => i);
|
|
889
|
-
return xoshiro.shuffled(indexes, degree).toSorted((a, b) => a - b);
|
|
890
|
-
}
|
|
891
|
-
};
|
|
892
|
-
/** Fragment indexes mixed into sequence `sequence` (1-based), sorted ascending. */
|
|
893
|
-
function chooseFragments(sequence, fragmentCount, checksum) {
|
|
894
|
-
if (sequence <= fragmentCount) return [sequence - 1];
|
|
895
|
-
return new FragmentChooser(fragmentCount, checksum).choose(sequence);
|
|
896
|
-
}
|
|
897
|
-
//#endregion
|
|
898
|
-
//#region src/fountain/limits.ts
|
|
899
|
-
/**
|
|
900
|
-
* Production default; same integers as bcur 1.0.
|
|
901
|
-
*
|
|
902
|
-
* Hosts that need another budget pass `new Decoder({ limits })`.
|
|
903
|
-
*/
|
|
904
|
-
const DEFAULT_LIMITS = {
|
|
905
|
-
maxMessageLength: 1048576,
|
|
906
|
-
maxFragmentCount: 2e3,
|
|
907
|
-
maxFragmentDataLength: 8192,
|
|
908
|
-
maxBufferParts: 4e3,
|
|
909
|
-
maxReceivedParts: 8e3,
|
|
910
|
-
maxUriLen: 8192
|
|
911
|
-
};
|
|
912
|
-
function mergeLimits(partial) {
|
|
913
|
-
return {
|
|
914
|
-
...DEFAULT_LIMITS,
|
|
915
|
-
...partial
|
|
916
|
-
};
|
|
917
|
-
}
|
|
918
|
-
//#endregion
|
|
919
|
-
//#region src/fountain/part-cbor.ts
|
|
920
|
-
/** Encode a part to deterministic fixed-schema CBOR. */
|
|
921
|
-
function encodePart(part) {
|
|
922
|
-
const { data } = part;
|
|
923
|
-
const out = [133];
|
|
924
|
-
encodeU32(out, part.sequence);
|
|
925
|
-
encodeU32(out, part.sequenceCount);
|
|
926
|
-
encodeU32(out, part.messageLength);
|
|
927
|
-
encodeU32(out, part.checksum);
|
|
928
|
-
encodeBstr(out, data);
|
|
929
|
-
return new Uint8Array(out);
|
|
930
|
-
}
|
|
931
|
-
/** Decode a part from CBOR with data-length and fragment-count caps. */
|
|
932
|
-
function decodePart(bytes, maxDataLen, maxFragmentCount) {
|
|
933
|
-
const cur = { i: 0 };
|
|
934
|
-
if (read(bytes, cur) !== 133) fail("InvalidPartCbor");
|
|
935
|
-
const sequence = decodeU32(bytes, cur);
|
|
936
|
-
const sequenceCount = decodeU32(bytes, cur);
|
|
937
|
-
const messageLength = decodeU32(bytes, cur);
|
|
938
|
-
const checksum = decodeU32(bytes, cur);
|
|
939
|
-
const data = decodeBstr(bytes, cur, maxDataLen);
|
|
940
|
-
if (cur.i !== bytes.length) fail("InvalidPartCbor");
|
|
941
|
-
if (sequence === 0) fail("InvalidSequence");
|
|
942
|
-
if (sequenceCount === 0 || messageLength === 0 || data.length === 0) fail("EmptyPart");
|
|
943
|
-
if (sequenceCount > maxFragmentCount) fail("ResourceLimit", { limit: "fragment_count" });
|
|
944
|
-
return {
|
|
945
|
-
sequence,
|
|
946
|
-
sequenceCount,
|
|
947
|
-
messageLength,
|
|
948
|
-
checksum,
|
|
949
|
-
data
|
|
950
|
-
};
|
|
951
|
-
}
|
|
952
|
-
function encodeU32(out, v) {
|
|
953
|
-
if (v <= 23) out.push(v);
|
|
954
|
-
else if (v <= 255) out.push(24, v);
|
|
955
|
-
else if (v <= 65535) out.push(25, v >>> 8 & 255, v & 255);
|
|
956
|
-
else out.push(26, v >>> 24 & 255, v >>> 16 & 255, v >>> 8 & 255, v & 255);
|
|
957
|
-
}
|
|
958
|
-
function encodeBstr(out, data) {
|
|
959
|
-
const len = data.length;
|
|
960
|
-
if (len <= 23) out.push(64 | len);
|
|
961
|
-
else if (len <= 255) out.push(88, len);
|
|
962
|
-
else if (len <= 65535) out.push(89, len >>> 8 & 255, len & 255);
|
|
963
|
-
else out.push(90, len >>> 24 & 255, len >>> 16 & 255, len >>> 8 & 255, len & 255);
|
|
964
|
-
for (const b of data) out.push(b);
|
|
965
|
-
}
|
|
966
|
-
function decodeU32(bytes, cur) {
|
|
967
|
-
const head = read(bytes, cur);
|
|
968
|
-
const major = head >> 5;
|
|
969
|
-
const ai = head & 31;
|
|
970
|
-
if (major !== 0) fail("InvalidPartCbor");
|
|
971
|
-
if (ai <= 23) return ai;
|
|
972
|
-
if (ai === 24) {
|
|
973
|
-
const v = read(bytes, cur);
|
|
974
|
-
if (v <= 23) fail("InvalidPartCbor");
|
|
975
|
-
return v;
|
|
976
|
-
}
|
|
977
|
-
if (ai === 25) {
|
|
978
|
-
const v = read(bytes, cur) << 8 | read(bytes, cur);
|
|
979
|
-
if (v <= 255) fail("InvalidPartCbor");
|
|
980
|
-
return v;
|
|
981
|
-
}
|
|
982
|
-
if (ai === 26) {
|
|
983
|
-
const v = (read(bytes, cur) << 24 | read(bytes, cur) << 16 | read(bytes, cur) << 8 | read(bytes, cur)) >>> 0;
|
|
984
|
-
if (v <= 65535) fail("InvalidPartCbor");
|
|
985
|
-
return v;
|
|
986
|
-
}
|
|
987
|
-
return fail("InvalidPartCbor");
|
|
988
|
-
}
|
|
989
|
-
function decodeBstr(bytes, cur, maxDataLen) {
|
|
990
|
-
const head = read(bytes, cur);
|
|
991
|
-
const major = head >> 5;
|
|
992
|
-
const ai = head & 31;
|
|
993
|
-
if (major !== 2) fail("InvalidPartCbor");
|
|
994
|
-
let len;
|
|
995
|
-
if (ai <= 23) len = ai;
|
|
996
|
-
else if (ai === 24) {
|
|
997
|
-
len = read(bytes, cur);
|
|
998
|
-
if (len <= 23) fail("InvalidPartCbor");
|
|
999
|
-
} else if (ai === 25) {
|
|
1000
|
-
len = read(bytes, cur) << 8 | read(bytes, cur);
|
|
1001
|
-
if (len <= 255) fail("InvalidPartCbor");
|
|
1002
|
-
} else if (ai === 26) {
|
|
1003
|
-
len = (read(bytes, cur) << 24 | read(bytes, cur) << 16 | read(bytes, cur) << 8 | read(bytes, cur)) >>> 0;
|
|
1004
|
-
if (len <= 65535) fail("InvalidPartCbor");
|
|
1005
|
-
} else fail("InvalidPartCbor");
|
|
1006
|
-
if (len > maxDataLen) fail("ResourceLimit", { limit: "fragment_data" });
|
|
1007
|
-
if (cur.i + len > bytes.length) fail("InvalidPartCbor");
|
|
1008
|
-
const data = bytes.slice(cur.i, cur.i + len);
|
|
1009
|
-
cur.i += len;
|
|
1010
|
-
return data;
|
|
1011
|
-
}
|
|
1012
|
-
function read(bytes, cur) {
|
|
1013
|
-
const b = bytes[cur.i];
|
|
1014
|
-
if (b === void 0) fail("InvalidPartCbor");
|
|
1015
|
-
cur.i += 1;
|
|
1016
|
-
return b;
|
|
1017
|
-
}
|
|
1018
|
-
//#endregion
|
|
1019
|
-
//#region src/fountain/part.ts
|
|
1020
|
-
/** A fountain part (wire metadata + fragment data). */
|
|
1021
|
-
var Part = class Part {
|
|
1022
|
-
sequence;
|
|
1023
|
-
sequenceCount;
|
|
1024
|
-
messageLength;
|
|
1025
|
-
checksum;
|
|
1026
|
-
data;
|
|
1027
|
-
constructor(sequence, sequenceCount, messageLength, checksum, data) {
|
|
1028
|
-
this.sequence = sequence;
|
|
1029
|
-
this.sequenceCount = sequenceCount;
|
|
1030
|
-
this.messageLength = messageLength;
|
|
1031
|
-
this.checksum = checksum;
|
|
1032
|
-
this.data = data;
|
|
1033
|
-
}
|
|
1034
|
-
static fromFields(sequence, sequenceCount, messageLength, checksum, data) {
|
|
1035
|
-
return new Part(sequence, sequenceCount, messageLength, checksum, data);
|
|
1036
|
-
}
|
|
1037
|
-
indexes() {
|
|
1038
|
-
return chooseFragments(this.sequence, this.sequenceCount, this.checksum);
|
|
1039
|
-
}
|
|
1040
|
-
isSimple() {
|
|
1041
|
-
return this.indexes().length === 1;
|
|
1042
|
-
}
|
|
1043
|
-
toCbor() {
|
|
1044
|
-
return encodePart(this);
|
|
1045
|
-
}
|
|
1046
|
-
static fromCbor(bytes, maxDataLen = DEFAULT_LIMITS.maxFragmentDataLength, maxFragmentCount = DEFAULT_LIMITS.maxFragmentCount) {
|
|
1047
|
-
const f = decodePart(bytes, maxDataLen, maxFragmentCount);
|
|
1048
|
-
return Part.fromFields(f.sequence, f.sequenceCount, f.messageLength, f.checksum, f.data);
|
|
1049
|
-
}
|
|
1050
|
-
sequenceId() {
|
|
1051
|
-
return `${this.sequence}-${this.sequenceCount}`;
|
|
1052
|
-
}
|
|
1053
|
-
};
|
|
1054
|
-
//#endregion
|
|
1055
|
-
//#region src/fountain/decoder.ts
|
|
1056
|
-
function keyOf(indexes) {
|
|
1057
|
-
return indexes.join(",");
|
|
1058
|
-
}
|
|
1059
|
-
function xorInto$1(target, src) {
|
|
1060
|
-
if (target.length !== src.length) fail("DecoderState");
|
|
1061
|
-
for (const [i, a] of target.entries()) {
|
|
1062
|
-
const b = src[i];
|
|
1063
|
-
if (b === void 0) fail("DecoderState");
|
|
1064
|
-
target[i] = a ^ b;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
/** Fountain decoder with resource limits and fail-closed poison. */
|
|
1068
|
-
var FountainDecoder = class {
|
|
1069
|
-
decoded = /* @__PURE__ */ new Map();
|
|
1070
|
-
received = /* @__PURE__ */ new Set();
|
|
1071
|
-
buffer = /* @__PURE__ */ new Map();
|
|
1072
|
-
bufferIndex = /* @__PURE__ */ new Map();
|
|
1073
|
-
queue = [];
|
|
1074
|
-
sequenceCount = 0;
|
|
1075
|
-
messageLength = 0;
|
|
1076
|
-
messageChecksum = 0;
|
|
1077
|
-
fragmentLength = 0;
|
|
1078
|
-
limits;
|
|
1079
|
-
chooser;
|
|
1080
|
-
poisoned;
|
|
1081
|
-
constructor(limits) {
|
|
1082
|
-
this.limits = mergeLimits(limits);
|
|
1083
|
-
}
|
|
1084
|
-
get maxFragmentDataLength() {
|
|
1085
|
-
return this.limits.maxFragmentDataLength;
|
|
1086
|
-
}
|
|
1087
|
-
get maxFragmentCount() {
|
|
1088
|
-
return this.limits.maxFragmentCount;
|
|
1089
|
-
}
|
|
1090
|
-
get isPoisoned() {
|
|
1091
|
-
return this.poisoned !== void 0;
|
|
1092
|
-
}
|
|
1093
|
-
get poisonState() {
|
|
1094
|
-
return this.poisoned;
|
|
1095
|
-
}
|
|
1096
|
-
poison(limit) {
|
|
1097
|
-
this.poisoned = {
|
|
1098
|
-
code: "ResourceLimit",
|
|
1099
|
-
limit
|
|
1100
|
-
};
|
|
1101
|
-
fail("ResourceLimit", { limit });
|
|
1102
|
-
}
|
|
1103
|
-
escalate(e) {
|
|
1104
|
-
if (e instanceof UrError) {
|
|
1105
|
-
if (e.code === "ResourceLimit") this.poisoned ??= {
|
|
1106
|
-
code: "ResourceLimit",
|
|
1107
|
-
limit: e.limit ?? "unknown"
|
|
1108
|
-
};
|
|
1109
|
-
else if (e.code === "DecoderState") this.poisoned ??= { code: "DecoderState" };
|
|
1110
|
-
}
|
|
1111
|
-
throw e;
|
|
1112
|
-
}
|
|
1113
|
-
/**
|
|
1114
|
-
* Receive a fountain part.
|
|
1115
|
-
*
|
|
1116
|
-
* @returns Whether the part was newly ingested.
|
|
1117
|
-
*/
|
|
1118
|
-
receive(part) {
|
|
1119
|
-
if (this.poisoned) failPoison(this.poisoned);
|
|
1120
|
-
if (this.complete) return false;
|
|
1121
|
-
if (part.sequenceCount === 0 || part.data.length === 0 || part.messageLength === 0) fail("EmptyPart");
|
|
1122
|
-
if (part.sequence === 0) fail("InvalidSequence");
|
|
1123
|
-
if (part.data.length > this.limits.maxFragmentDataLength) this.poison("fragment_data");
|
|
1124
|
-
if (this.received.size === 0) {
|
|
1125
|
-
const sc = part.sequenceCount;
|
|
1126
|
-
const ml = part.messageLength;
|
|
1127
|
-
if (sc > this.limits.maxFragmentCount) this.poison("fragment_count");
|
|
1128
|
-
if (ml > this.limits.maxMessageLength) this.poison("message_length");
|
|
1129
|
-
const fragLen = part.data.length;
|
|
1130
|
-
const product = fragLen * sc;
|
|
1131
|
-
if (!Number.isSafeInteger(product) || product > 4294967295) this.poison("message_length");
|
|
1132
|
-
if (product < ml || product - ml >= fragLen) fail("InconsistentPart");
|
|
1133
|
-
this.sequenceCount = sc;
|
|
1134
|
-
this.messageLength = ml;
|
|
1135
|
-
this.messageChecksum = part.checksum;
|
|
1136
|
-
this.fragmentLength = fragLen;
|
|
1137
|
-
this.chooser = new FragmentChooser(sc, part.checksum);
|
|
1138
|
-
} else if (!this.validate(part)) fail("InconsistentPart");
|
|
1139
|
-
const { chooser } = this;
|
|
1140
|
-
if (chooser === void 0) fail("DecoderState");
|
|
1141
|
-
const indexes = chooser.choose(part.sequence);
|
|
1142
|
-
const key = keyOf(indexes);
|
|
1143
|
-
if (this.received.has(key)) return false;
|
|
1144
|
-
if (this.received.size >= this.limits.maxReceivedParts) this.poison("received_parts");
|
|
1145
|
-
this.received.add(key);
|
|
1146
|
-
try {
|
|
1147
|
-
if (indexes.length === 1) this.enqueueSimple(part, indexes);
|
|
1148
|
-
else this.processComplex(part, indexes);
|
|
1149
|
-
this.processQueue();
|
|
1150
|
-
} catch (error) {
|
|
1151
|
-
this.escalate(error);
|
|
1152
|
-
}
|
|
1153
|
-
return true;
|
|
1154
|
-
}
|
|
1155
|
-
enqueueSimple(part, indexes) {
|
|
1156
|
-
const [index] = indexes;
|
|
1157
|
-
if (index === void 0) fail("DecoderState");
|
|
1158
|
-
if (this.decoded.has(index)) return;
|
|
1159
|
-
this.decoded.set(index, part);
|
|
1160
|
-
this.queue.push({
|
|
1161
|
-
index,
|
|
1162
|
-
part
|
|
1163
|
-
});
|
|
1164
|
-
}
|
|
1165
|
-
processQueue() {
|
|
1166
|
-
while (this.queue.length > 0) {
|
|
1167
|
-
const item = this.queue.pop();
|
|
1168
|
-
if (item === void 0) fail("DecoderState");
|
|
1169
|
-
const { index, part: simple } = item;
|
|
1170
|
-
const toProcess = this.bufferIndex.get(index);
|
|
1171
|
-
if (toProcess === void 0) continue;
|
|
1172
|
-
for (const k of toProcess) this.reduceBufferedPart(k, index, simple);
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
1175
|
-
reduceBufferedPart(key, knownIndex, simple) {
|
|
1176
|
-
const entry = this.buffer.get(key);
|
|
1177
|
-
if (!entry) fail("DecoderState");
|
|
1178
|
-
this.buffer.delete(key);
|
|
1179
|
-
for (const idx of entry.indexes) {
|
|
1180
|
-
const keys = this.bufferIndex.get(idx);
|
|
1181
|
-
if (keys === void 0) fail("DecoderState");
|
|
1182
|
-
keys.delete(key);
|
|
1183
|
-
if (keys.size === 0) this.bufferIndex.delete(idx);
|
|
1184
|
-
}
|
|
1185
|
-
const newIndexes = entry.indexes.filter((x) => x !== knownIndex);
|
|
1186
|
-
if (newIndexes.length === entry.indexes.length) fail("DecoderState");
|
|
1187
|
-
const data = new Uint8Array(entry.part.data);
|
|
1188
|
-
xorInto$1(data, simple.data);
|
|
1189
|
-
const reduced = Part.fromFields(entry.part.sequence, entry.part.sequenceCount, entry.part.messageLength, entry.part.checksum, data);
|
|
1190
|
-
this.insertReduced(newIndexes, reduced);
|
|
1191
|
-
}
|
|
1192
|
-
processComplex(part, indexes) {
|
|
1193
|
-
const remaining = [];
|
|
1194
|
-
const data = new Uint8Array(part.data);
|
|
1195
|
-
for (const idx of indexes) {
|
|
1196
|
-
const decoded = this.decoded.get(idx);
|
|
1197
|
-
if (decoded === void 0) remaining.push(idx);
|
|
1198
|
-
else xorInto$1(data, decoded.data);
|
|
1199
|
-
}
|
|
1200
|
-
if (remaining.length === 0) return;
|
|
1201
|
-
const reduced = Part.fromFields(part.sequence, part.sequenceCount, part.messageLength, part.checksum, data);
|
|
1202
|
-
this.insertReduced(remaining, reduced);
|
|
1203
|
-
}
|
|
1204
|
-
insertReduced(indexes, part) {
|
|
1205
|
-
if (indexes.length === 1) {
|
|
1206
|
-
const [idx] = indexes;
|
|
1207
|
-
if (idx === void 0) fail("DecoderState");
|
|
1208
|
-
if (this.decoded.has(idx)) return;
|
|
1209
|
-
this.decoded.set(idx, part);
|
|
1210
|
-
this.queue.push({
|
|
1211
|
-
index: idx,
|
|
1212
|
-
part
|
|
1213
|
-
});
|
|
1214
|
-
return;
|
|
1215
|
-
}
|
|
1216
|
-
const key = keyOf(indexes);
|
|
1217
|
-
if (!this.buffer.has(key) && this.buffer.size >= this.limits.maxBufferParts) this.poison("buffer_parts");
|
|
1218
|
-
this.buffer.set(key, {
|
|
1219
|
-
indexes,
|
|
1220
|
-
part
|
|
1221
|
-
});
|
|
1222
|
-
for (const idx of indexes) {
|
|
1223
|
-
let keys = this.bufferIndex.get(idx);
|
|
1224
|
-
if (keys === void 0) {
|
|
1225
|
-
keys = /* @__PURE__ */ new Set();
|
|
1226
|
-
this.bufferIndex.set(idx, keys);
|
|
1227
|
-
}
|
|
1228
|
-
keys.add(key);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
get complete() {
|
|
1232
|
-
return this.messageLength !== 0 && this.decoded.size === this.sequenceCount;
|
|
1233
|
-
}
|
|
1234
|
-
resolvedFragmentCount() {
|
|
1235
|
-
return this.messageLength === 0 ? void 0 : this.decoded.size;
|
|
1236
|
-
}
|
|
1237
|
-
get fragmentCount() {
|
|
1238
|
-
return this.sequenceCount;
|
|
1239
|
-
}
|
|
1240
|
-
validate(part) {
|
|
1241
|
-
if (this.received.size === 0) return false;
|
|
1242
|
-
return part.sequenceCount === this.sequenceCount && part.messageLength === this.messageLength && part.checksum === this.messageChecksum && part.data.length === this.fragmentLength;
|
|
1243
|
-
}
|
|
1244
|
-
/** Decoded message if complete; otherwise `undefined`. */
|
|
1245
|
-
message() {
|
|
1246
|
-
if (this.poisoned) failPoison(this.poisoned);
|
|
1247
|
-
if (!this.complete) return;
|
|
1248
|
-
const combined = new Uint8Array(this.fragmentLength * this.sequenceCount);
|
|
1249
|
-
for (let idx = 0; idx < this.sequenceCount; idx++) {
|
|
1250
|
-
const part = this.decoded.get(idx);
|
|
1251
|
-
if (!part) fail("DecoderState");
|
|
1252
|
-
combined.set(part.data, idx * this.fragmentLength);
|
|
1253
|
-
}
|
|
1254
|
-
for (let i = this.messageLength; i < combined.length; i++) if (combined[i] !== 0) fail("InvalidPadding");
|
|
1255
|
-
const message = combined.subarray(0, this.messageLength);
|
|
1256
|
-
if (checksum(message) !== this.messageChecksum) fail("InvalidMessageChecksum");
|
|
1257
|
-
return new Uint8Array(message);
|
|
1258
|
-
}
|
|
1259
|
-
};
|
|
1260
|
-
//#endregion
|
|
1261
|
-
//#region src/fountain/encoder.ts
|
|
1262
|
-
function xorInto(target, src) {
|
|
1263
|
-
if (target.length !== src.length) fail("DecoderState");
|
|
1264
|
-
for (const [i, a] of target.entries()) {
|
|
1265
|
-
const b = src[i];
|
|
1266
|
-
if (b === void 0) fail("DecoderState");
|
|
1267
|
-
target[i] = a ^ b;
|
|
1268
|
-
}
|
|
1269
|
-
}
|
|
1270
|
-
/** Next 1-based fountain seqNum. Does not wrap. */
|
|
1271
|
-
function nextSequence(current) {
|
|
1272
|
-
if (current === 4294967295) fail("ResourceLimit", { limit: "sequence" });
|
|
1273
|
-
return current + 1;
|
|
1274
|
-
}
|
|
1275
|
-
/** Fountain encoder. */
|
|
1276
|
-
var FountainEncoder = class FountainEncoder {
|
|
1277
|
-
parts;
|
|
1278
|
-
sequenceCount;
|
|
1279
|
-
messageLength;
|
|
1280
|
-
messageChecksum;
|
|
1281
|
-
chooser;
|
|
1282
|
-
currentSequence = 0;
|
|
1283
|
-
constructor(parts, sequenceCount, messageLength, messageChecksum) {
|
|
1284
|
-
this.parts = parts;
|
|
1285
|
-
this.sequenceCount = sequenceCount;
|
|
1286
|
-
this.messageLength = messageLength;
|
|
1287
|
-
this.messageChecksum = messageChecksum;
|
|
1288
|
-
this.chooser = new FragmentChooser(sequenceCount, messageChecksum);
|
|
1289
|
-
}
|
|
1290
|
-
static create(message, maxFragmentLength) {
|
|
1291
|
-
if (message.length === 0) fail("EmptyMessage");
|
|
1292
|
-
if (!Number.isSafeInteger(maxFragmentLength) || maxFragmentLength < 1) fail("InvalidFragmentLen");
|
|
1293
|
-
if (message.length > 4294967295) fail("ResourceLimit", { limit: "message_length" });
|
|
1294
|
-
const fragments = partition(message, fragmentLength(message.length, maxFragmentLength));
|
|
1295
|
-
if (fragments.length > 4294967295) fail("ResourceLimit", { limit: "fragment_count" });
|
|
1296
|
-
return new FountainEncoder(fragments, fragments.length, message.length, checksum(message));
|
|
1297
|
-
}
|
|
1298
|
-
get currentSequenceNum() {
|
|
1299
|
-
return this.currentSequence;
|
|
1300
|
-
}
|
|
1301
|
-
get fragmentCount() {
|
|
1302
|
-
return this.sequenceCount;
|
|
1303
|
-
}
|
|
1304
|
-
get complete() {
|
|
1305
|
-
return this.currentSequence >= this.sequenceCount;
|
|
1306
|
-
}
|
|
1307
|
-
nextPart() {
|
|
1308
|
-
if (this.sequenceCount === 1 && this.currentSequence >= 1) fail("SinglePartExhausted");
|
|
1309
|
-
this.currentSequence = nextSequence(this.currentSequence);
|
|
1310
|
-
const indexes = this.chooser.choose(this.currentSequence);
|
|
1311
|
-
const [first] = this.parts;
|
|
1312
|
-
if (first === void 0) fail("DecoderState");
|
|
1313
|
-
const mixed = new Uint8Array(first.length);
|
|
1314
|
-
for (const idx of indexes) {
|
|
1315
|
-
const fragment = this.parts[idx];
|
|
1316
|
-
if (fragment === void 0) fail("DecoderState");
|
|
1317
|
-
xorInto(mixed, fragment);
|
|
1318
|
-
}
|
|
1319
|
-
return Part.fromFields(this.currentSequence, this.sequenceCount, this.messageLength, this.messageChecksum, mixed);
|
|
1320
|
-
}
|
|
1321
|
-
};
|
|
1322
|
-
//#endregion
|
|
1323
|
-
//#region src/ur/type.ts
|
|
1324
|
-
/** Validated UR type token (stored lowercase, `[a-z0-9-]+`). */
|
|
1325
|
-
var UrType = class UrType {
|
|
1326
|
-
value;
|
|
1327
|
-
constructor(value) {
|
|
1328
|
-
this.value = value;
|
|
1329
|
-
}
|
|
1330
|
-
static parse(s) {
|
|
1331
|
-
const lower = s.toLowerCase();
|
|
1332
|
-
if (lower.length === 0 || !/^[a-z0-9-]+$/.test(lower)) fail("InvalidType");
|
|
1333
|
-
return new UrType(lower);
|
|
1334
|
-
}
|
|
1335
|
-
static bytes() {
|
|
1336
|
-
return new UrType("bytes");
|
|
1337
|
-
}
|
|
1338
|
-
equals(other) {
|
|
1339
|
-
return this.value === other.value;
|
|
1340
|
-
}
|
|
1341
|
-
};
|
|
1342
|
-
//#endregion
|
|
1343
|
-
//#region src/ur/parse.ts
|
|
1344
|
-
/** Lowercase a UR string for case-insensitive QR transport. */
|
|
1345
|
-
function normalizeUr(uri) {
|
|
1346
|
-
return uri.toLowerCase();
|
|
1347
|
-
}
|
|
1348
|
-
/** Parse a UR (full-URI case fold). Does not decode bytewords. */
|
|
1349
|
-
function parse(uri) {
|
|
1350
|
-
return parseNormalized(normalizeUr(uri));
|
|
1351
|
-
}
|
|
1352
|
-
/** Parse an already-lowercased (or body-normalized) UR. */
|
|
1353
|
-
function parseNormalized(uri) {
|
|
1354
|
-
if (!uri.startsWith("ur:")) fail("InvalidScheme");
|
|
1355
|
-
const rest0 = uri.slice(3);
|
|
1356
|
-
const slash = rest0.indexOf("/");
|
|
1357
|
-
if (slash === -1) fail("TypeUnspecified");
|
|
1358
|
-
const typeStr = rest0.slice(0, slash);
|
|
1359
|
-
const rest = rest0.slice(slash + 1);
|
|
1360
|
-
const urType = UrType.parse(typeStr);
|
|
1361
|
-
const lastSlash = rest.lastIndexOf("/");
|
|
1362
|
-
if (lastSlash === -1) return {
|
|
1363
|
-
type: urType,
|
|
1364
|
-
kind: "single",
|
|
1365
|
-
body: rest.toLowerCase()
|
|
1366
|
-
};
|
|
1367
|
-
const indicesStr = rest.slice(0, lastSlash);
|
|
1368
|
-
const body = rest.slice(lastSlash + 1).toLowerCase();
|
|
1369
|
-
return {
|
|
1370
|
-
type: urType,
|
|
1371
|
-
kind: "multi",
|
|
1372
|
-
indices: decodeIndices(indicesStr),
|
|
1373
|
-
body
|
|
1374
|
-
};
|
|
1375
|
-
}
|
|
1376
|
-
function decodeIndices(indices) {
|
|
1377
|
-
const dash = indices.indexOf("-");
|
|
1378
|
-
if (dash === -1) fail("InvalidIndices");
|
|
1379
|
-
const a = indices.slice(0, dash);
|
|
1380
|
-
const b = indices.slice(dash + 1);
|
|
1381
|
-
if (!/^\d+$/.test(a) || !/^\d+$/.test(b)) fail("InvalidIndices");
|
|
1382
|
-
const seq = Number(a);
|
|
1383
|
-
const count = Number(b);
|
|
1384
|
-
if (!Number.isSafeInteger(seq) || !Number.isSafeInteger(count)) fail("InvalidIndices");
|
|
1385
|
-
if (seq === 0 || count === 0) fail("InvalidIndices");
|
|
1386
|
-
if (seq > 4294967295 || count > 4294967295) fail("InvalidIndices");
|
|
1387
|
-
return {
|
|
1388
|
-
seq,
|
|
1389
|
-
count
|
|
1390
|
-
};
|
|
1391
|
-
}
|
|
1392
|
-
//#endregion
|
|
1393
|
-
//#region src/ur/index.ts
|
|
1394
|
-
/** Encode a single-part UR. Empty data is allowed. */
|
|
1395
|
-
function encode(data, type) {
|
|
1396
|
-
const body = encode$1(data, "minimal");
|
|
1397
|
-
return `ur:${type.value}/${body}`;
|
|
1398
|
-
}
|
|
1399
|
-
/**
|
|
1400
|
-
* Decode payload from a single- or multi-part UR. Multi-part returns the CBOR-encoded fountain part
|
|
1401
|
-
* bytes, not the message.
|
|
1402
|
-
*/
|
|
1403
|
-
function decode(uri) {
|
|
1404
|
-
const parsed = parse(uri);
|
|
1405
|
-
const payload = decode$1(parsed.body, "minimal");
|
|
1406
|
-
return {
|
|
1407
|
-
kind: parsed.kind,
|
|
1408
|
-
payload
|
|
1409
|
-
};
|
|
1410
|
-
}
|
|
1411
|
-
/** Like {@link decode} but retains the normalized type. */
|
|
1412
|
-
function decodeWithType(uri) {
|
|
1413
|
-
const parsed = parse(uri);
|
|
1414
|
-
const payload = decode$1(parsed.body, "minimal");
|
|
1415
|
-
return {
|
|
1416
|
-
type: parsed.type,
|
|
1417
|
-
kind: parsed.kind,
|
|
1418
|
-
payload
|
|
1419
|
-
};
|
|
1420
|
-
}
|
|
1421
|
-
/** Decode a single-part UR payload. Multi-part URIs throw `NotSinglePart`. */
|
|
1422
|
-
function decodeMessage(uri) {
|
|
1423
|
-
const { kind, payload } = decode(uri);
|
|
1424
|
-
if (kind !== "single") fail("NotSinglePart");
|
|
1425
|
-
return payload;
|
|
1426
|
-
}
|
|
1427
|
-
/** Uppercase UR string for denser QR alphanumeric mode. */
|
|
1428
|
-
function toQrString(uri) {
|
|
1429
|
-
return uri.toUpperCase();
|
|
1430
|
-
}
|
|
1431
|
-
/** UR encoder. `K == 1` emits single-part; otherwise fountain. */
|
|
1432
|
-
var Encoder = class Encoder {
|
|
1433
|
-
fountain;
|
|
1434
|
-
urType;
|
|
1435
|
-
message;
|
|
1436
|
-
singleEmitted = false;
|
|
1437
|
-
constructor(fountain, urType, message) {
|
|
1438
|
-
this.fountain = fountain;
|
|
1439
|
-
this.urType = urType;
|
|
1440
|
-
this.message = message;
|
|
1441
|
-
}
|
|
1442
|
-
static create(message, maxFragmentLength, type) {
|
|
1443
|
-
return new Encoder(FountainEncoder.create(message, maxFragmentLength), type, new Uint8Array(message));
|
|
1444
|
-
}
|
|
1445
|
-
static bytes(message, maxFragmentLength) {
|
|
1446
|
-
return Encoder.create(message, maxFragmentLength, UrType.bytes());
|
|
1447
|
-
}
|
|
1448
|
-
get isSinglePart() {
|
|
1449
|
-
return this.fountain.fragmentCount === 1;
|
|
1450
|
-
}
|
|
1451
|
-
get fragmentCount() {
|
|
1452
|
-
return this.fountain.fragmentCount;
|
|
1453
|
-
}
|
|
1454
|
-
get currentIndex() {
|
|
1455
|
-
return this.isSinglePart ? this.singleEmitted ? 1 : 0 : this.fountain.currentSequenceNum;
|
|
1456
|
-
}
|
|
1457
|
-
get complete() {
|
|
1458
|
-
return this.isSinglePart ? this.singleEmitted : this.fountain.complete;
|
|
1459
|
-
}
|
|
1460
|
-
nextPart() {
|
|
1461
|
-
if (this.isSinglePart) {
|
|
1462
|
-
this.singleEmitted = true;
|
|
1463
|
-
return encode(this.message, this.urType);
|
|
1464
|
-
}
|
|
1465
|
-
const part = this.fountain.nextPart();
|
|
1466
|
-
const body = encode$1(part.toCbor(), "minimal");
|
|
1467
|
-
return `ur:${this.urType.value}/${part.sequenceId()}/${body}`;
|
|
1468
|
-
}
|
|
1469
|
-
};
|
|
1470
|
-
/** UR decoder with type stickiness and URI limits. */
|
|
1471
|
-
var Decoder = class {
|
|
1472
|
-
fountain;
|
|
1473
|
-
maxUriLen;
|
|
1474
|
-
maxMessageLength;
|
|
1475
|
-
expectedType;
|
|
1476
|
-
seenType;
|
|
1477
|
-
single;
|
|
1478
|
-
poisoned;
|
|
1479
|
-
constructor(options) {
|
|
1480
|
-
const limits = mergeLimits(options?.limits);
|
|
1481
|
-
this.fountain = new FountainDecoder(limits);
|
|
1482
|
-
this.maxUriLen = limits.maxUriLen;
|
|
1483
|
-
this.maxMessageLength = limits.maxMessageLength;
|
|
1484
|
-
this.expectedType = options?.expectedType;
|
|
1485
|
-
}
|
|
1486
|
-
poison(limit) {
|
|
1487
|
-
this.poisoned = {
|
|
1488
|
-
code: "ResourceLimit",
|
|
1489
|
-
limit
|
|
1490
|
-
};
|
|
1491
|
-
fail("ResourceLimit", { limit });
|
|
1492
|
-
}
|
|
1493
|
-
escalate(e) {
|
|
1494
|
-
if (e instanceof UrError) {
|
|
1495
|
-
if (e.code === "ResourceLimit") this.poisoned ??= {
|
|
1496
|
-
code: "ResourceLimit",
|
|
1497
|
-
limit: e.limit ?? "unknown"
|
|
1498
|
-
};
|
|
1499
|
-
else if (e.code === "DecoderState") this.poisoned ??= { code: "DecoderState" };
|
|
1500
|
-
}
|
|
1501
|
-
throw e;
|
|
1502
|
-
}
|
|
1503
|
-
get poisonState() {
|
|
1504
|
-
return this.poisoned ?? this.fountain.poisonState;
|
|
1505
|
-
}
|
|
1506
|
-
receive(uri) {
|
|
1507
|
-
if (this.poisoned) failPoison(this.poisoned);
|
|
1508
|
-
if (this.fountain.isPoisoned) {
|
|
1509
|
-
const poison = this.fountain.poisonState;
|
|
1510
|
-
if (poison === void 0) fail("DecoderState");
|
|
1511
|
-
this.poisoned ??= poison;
|
|
1512
|
-
failPoison(poison);
|
|
1513
|
-
}
|
|
1514
|
-
if (uri.length > this.maxUriLen) this.poison("uri_len");
|
|
1515
|
-
const parsed = parse(uri);
|
|
1516
|
-
if (this.expectedType && !parsed.type.equals(this.expectedType)) fail("UnexpectedType", {
|
|
1517
|
-
expected: this.expectedType.value,
|
|
1518
|
-
found: parsed.type.value
|
|
1519
|
-
});
|
|
1520
|
-
if (this.seenType && !this.seenType.equals(parsed.type)) fail("UnexpectedType", {
|
|
1521
|
-
expected: this.seenType.value,
|
|
1522
|
-
found: parsed.type.value
|
|
1523
|
-
});
|
|
1524
|
-
try {
|
|
1525
|
-
if (parsed.kind === "single") this.receiveSingle(parsed);
|
|
1526
|
-
else this.receiveFountain(parsed);
|
|
1527
|
-
} catch (error) {
|
|
1528
|
-
this.escalate(error);
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
receiveSingle(parsed) {
|
|
1532
|
-
if (this.fountain.resolvedFragmentCount() !== void 0) fail("InconsistentPart");
|
|
1533
|
-
if (this.single !== void 0) return;
|
|
1534
|
-
const data = decode$1(parsed.body, "minimal");
|
|
1535
|
-
if (data.length > this.maxMessageLength) this.poison("message_length");
|
|
1536
|
-
this.seenType = parsed.type;
|
|
1537
|
-
this.single = data;
|
|
1538
|
-
}
|
|
1539
|
-
receiveFountain(parsed) {
|
|
1540
|
-
if (this.single !== void 0) fail("InconsistentPart");
|
|
1541
|
-
const decoded = decode$1(parsed.body, "minimal");
|
|
1542
|
-
const part = Part.fromCbor(decoded, this.fountain.maxFragmentDataLength, this.fountain.maxFragmentCount);
|
|
1543
|
-
const { indices } = parsed;
|
|
1544
|
-
if (!indices) fail("InvalidIndices");
|
|
1545
|
-
if (part.sequence !== indices.seq || part.sequenceCount !== indices.count) fail("InvalidIndices");
|
|
1546
|
-
this.fountain.receive(part);
|
|
1547
|
-
this.seenType ??= parsed.type;
|
|
1548
|
-
}
|
|
1549
|
-
get complete() {
|
|
1550
|
-
return this.single !== void 0 || this.fountain.complete;
|
|
1551
|
-
}
|
|
1552
|
-
message() {
|
|
1553
|
-
if (this.poisoned) failPoison(this.poisoned);
|
|
1554
|
-
if (this.fountain.isPoisoned) {
|
|
1555
|
-
const poison = this.fountain.poisonState;
|
|
1556
|
-
if (poison === void 0) fail("DecoderState");
|
|
1557
|
-
failPoison(poison);
|
|
1558
|
-
}
|
|
1559
|
-
if (this.single) return new Uint8Array(this.single);
|
|
1560
|
-
return this.fountain.message();
|
|
1561
|
-
}
|
|
1562
|
-
resolvedFragmentCount() {
|
|
1563
|
-
if (this.single) return 1;
|
|
1564
|
-
return this.fountain.resolvedFragmentCount();
|
|
1565
|
-
}
|
|
1566
|
-
get fragmentCount() {
|
|
1567
|
-
return this.single ? 1 : this.fountain.fragmentCount;
|
|
1568
|
-
}
|
|
1569
|
-
get type() {
|
|
1570
|
-
return this.seenType;
|
|
1571
|
-
}
|
|
1572
|
-
get isPoisoned() {
|
|
1573
|
-
return this.poisoned !== void 0 || this.fountain.isPoisoned;
|
|
1574
|
-
}
|
|
1575
|
-
};
|
|
1576
|
-
//#endregion
|
|
1577
|
-
export { fail as C, UrError as S, canonicalizeByteword as _, decodeWithType as a, encodeRaw as b, normalizeUr as c, UrType as d, FountainEncoder as f, bytewords_exports as g, DEFAULT_LIMITS as h, decodeMessage as i, parse as l, Part as m, Encoder as n, encode as o, FountainDecoder as p, decode as r, toQrString as s, Decoder as t, parseNormalized as u, decode$1 as v, failPoison as w, checksum as x, encode$1 as y };
|