@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.
- package/README.md +2 -16
- package/dist/index-BeG2XIr5.d.mts +59 -0
- package/dist/index.d.mts +29 -31
- package/dist/index.mjs +1 -1
- package/dist/registry.d.mts +142 -0
- package/dist/registry.mjs +549 -0
- package/dist/{type-CSfO9fHL.d.mts → type-CZ0CIHJk.d.mts} +10 -6
- package/dist/typed-BmHx2GWT.mjs +150 -0
- package/dist/typed.d.mts +2 -58
- package/dist/typed.mjs +2 -145
- package/dist/{ur-C5n_FgVn.mjs → ur-BUUcXyia.mjs} +209 -132
- package/package.json +14 -13
|
@@ -33,15 +33,18 @@ var UrError = class extends Error {
|
|
|
33
33
|
found;
|
|
34
34
|
limit;
|
|
35
35
|
constructor(code, options) {
|
|
36
|
+
const expected = options?.expected;
|
|
37
|
+
const found = options?.found;
|
|
38
|
+
const limit = options?.limit;
|
|
36
39
|
let message = MESSAGES[code];
|
|
37
|
-
if (code === "ResourceLimit" &&
|
|
38
|
-
else if (code === "UnexpectedType" &&
|
|
39
|
-
super(message, options?.cause
|
|
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 });
|
|
40
43
|
this.name = "UrError";
|
|
41
44
|
this.code = code;
|
|
42
|
-
this.expected =
|
|
43
|
-
this.found =
|
|
44
|
-
this.limit =
|
|
45
|
+
this.expected = expected;
|
|
46
|
+
this.found = found;
|
|
47
|
+
this.limit = limit;
|
|
45
48
|
}
|
|
46
49
|
};
|
|
47
50
|
/** Throws a {@link UrError} with the given code. */
|
|
@@ -55,9 +58,7 @@ function failPoison(p) {
|
|
|
55
58
|
}
|
|
56
59
|
//#endregion
|
|
57
60
|
//#region src/crc32.ts
|
|
58
|
-
/**
|
|
59
|
-
* CRC-32 ISO-HDLC (polynomial 0xedb88320), matching bcur / ur-rs.
|
|
60
|
-
*/
|
|
61
|
+
/** CRC-32 ISO-HDLC (polynomial 0xedb88320), matching bcur / ur-rs. */
|
|
61
62
|
const TABLE = (() => {
|
|
62
63
|
const table = /* @__PURE__ */ new Uint32Array(256);
|
|
63
64
|
for (let i = 0; i < 256; i++) {
|
|
@@ -70,10 +71,7 @@ const TABLE = (() => {
|
|
|
70
71
|
/** CRC-32 ISO-HDLC checksum of `data`. */
|
|
71
72
|
function checksum(data) {
|
|
72
73
|
let crc = 4294967295;
|
|
73
|
-
for (
|
|
74
|
-
const b = data[i];
|
|
75
|
-
crc = TABLE[(crc ^ b) & 255] ^ crc >>> 8;
|
|
76
|
-
}
|
|
74
|
+
for (const b of data) crc = (TABLE[(crc ^ b) & 255] ?? fail("DecoderState")) ^ crc >>> 8;
|
|
77
75
|
return (crc ^ 4294967295) >>> 0;
|
|
78
76
|
}
|
|
79
77
|
//#endregion
|
|
@@ -608,9 +606,12 @@ var bytewords_exports = /* @__PURE__ */ __exportAll({
|
|
|
608
606
|
encodeRaw: () => encodeRaw
|
|
609
607
|
});
|
|
610
608
|
const wordByToken = /* @__PURE__ */ new Map();
|
|
611
|
-
for (
|
|
612
|
-
|
|
613
|
-
|
|
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;
|
|
614
615
|
}
|
|
615
616
|
/** Encode `data` with trailing CRC-32 as bytewords. Empty data is allowed. */
|
|
616
617
|
function encode$1(data, style) {
|
|
@@ -623,25 +624,23 @@ function encode$1(data, style) {
|
|
|
623
624
|
withCrc[data.length + 3] = crc & 255;
|
|
624
625
|
return encodeWords(withCrc, style);
|
|
625
626
|
}
|
|
626
|
-
/**
|
|
627
|
-
* Encode without CRC trailer.
|
|
628
|
-
* Not for UR bodies — identifiers only.
|
|
629
|
-
*/
|
|
627
|
+
/** Encode without CRC trailer. Not for UR bodies — identifiers only. */
|
|
630
628
|
function encodeRaw(data, style) {
|
|
631
629
|
return encodeWords(data, style);
|
|
632
630
|
}
|
|
633
631
|
function encodeWords(data, style) {
|
|
634
632
|
const parts = [];
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
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]);
|
|
641
640
|
}
|
|
642
641
|
/** Decode bytewords and verify CRC-32. Case-insensitive. */
|
|
643
642
|
function decode$1(encoded, style) {
|
|
644
|
-
for (
|
|
643
|
+
for (const ch of encoded) if ((ch.codePointAt(0) ?? 0) > 127) fail("NonAscii");
|
|
645
644
|
const lowered = encoded.toLowerCase();
|
|
646
645
|
if (style === "minimal") return decodeMinimal(lowered);
|
|
647
646
|
const sep = style === "standard" ? " " : "-";
|
|
@@ -655,13 +654,12 @@ function decodeMinimal(encoded) {
|
|
|
655
654
|
}
|
|
656
655
|
function decodeParts(parts, minimal) {
|
|
657
656
|
const data = new Uint8Array(parts.length);
|
|
658
|
-
for (
|
|
659
|
-
const part = parts[i];
|
|
657
|
+
for (const [i, part] of parts.entries()) {
|
|
660
658
|
const expectedLen = minimal ? 2 : 4;
|
|
661
659
|
if (part.length !== expectedLen) fail("InvalidWord");
|
|
662
660
|
const byte = wordByToken.get(part);
|
|
663
661
|
if (byte === void 0) fail("InvalidWord");
|
|
664
|
-
if (part !== (minimal ? MINIMALS
|
|
662
|
+
if (part !== wordAt(minimal ? MINIMALS : WORDS, byte)) fail("InvalidWord");
|
|
665
663
|
data[i] = byte;
|
|
666
664
|
}
|
|
667
665
|
return stripChecksum(data);
|
|
@@ -671,12 +669,12 @@ function stripChecksum(data) {
|
|
|
671
669
|
const split = data.length - 4;
|
|
672
670
|
const payload = data.subarray(0, split);
|
|
673
671
|
const expected = checksum(payload);
|
|
674
|
-
if ((data
|
|
675
|
-
return payload
|
|
672
|
+
if (new DataView(data.buffer, data.byteOffset + split, 4).getUint32(0) !== expected) fail("InvalidBytewordsChecksum");
|
|
673
|
+
return new Uint8Array(payload);
|
|
676
674
|
}
|
|
677
675
|
/** Canonicalize a 2–4 letter token to the full 4-letter lowercase byteword. */
|
|
678
676
|
function canonicalizeByteword(token) {
|
|
679
|
-
for (
|
|
677
|
+
for (const ch of token) if ((ch.codePointAt(0) ?? 0) > 127) return;
|
|
680
678
|
const lower = token.toLowerCase();
|
|
681
679
|
if (lower.length === 4) {
|
|
682
680
|
const b = wordByToken.get(lower);
|
|
@@ -689,7 +687,7 @@ function canonicalizeByteword(token) {
|
|
|
689
687
|
if (lower.length === 3) {
|
|
690
688
|
let found;
|
|
691
689
|
for (const word of WORDS) if (word.slice(0, 3) === lower || word.slice(1, 4) === lower) {
|
|
692
|
-
if (found !== void 0) return
|
|
690
|
+
if (found !== void 0) return;
|
|
693
691
|
found = word;
|
|
694
692
|
}
|
|
695
693
|
return found;
|
|
@@ -697,9 +695,12 @@ function canonicalizeByteword(token) {
|
|
|
697
695
|
}
|
|
698
696
|
//#endregion
|
|
699
697
|
//#region src/rng/sampler.ts
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
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). */
|
|
703
704
|
var Weighted = class Weighted {
|
|
704
705
|
aliases;
|
|
705
706
|
probs;
|
|
@@ -708,29 +709,30 @@ var Weighted = class Weighted {
|
|
|
708
709
|
this.probs = probs;
|
|
709
710
|
}
|
|
710
711
|
static new(weightsIn) {
|
|
711
|
-
const weights = weightsIn
|
|
712
|
+
const weights = [...weightsIn];
|
|
712
713
|
for (const w of weights) if (w < 0) throw new Error("negative probability encountered");
|
|
713
714
|
const summed = weights.reduce((a, b) => a + b, 0);
|
|
714
715
|
if (!(summed > 0)) throw new Error("probabilities don't sum to a positive value");
|
|
715
716
|
const count = weights.length;
|
|
716
|
-
for (
|
|
717
|
+
for (const [i, w] of weights.entries()) weights[i] = w * (count / summed);
|
|
717
718
|
const s = [];
|
|
718
719
|
const l = [];
|
|
719
|
-
for (let j = count - 1; j >= 0; j--) if (weights
|
|
720
|
+
for (let j = count - 1; j >= 0; j--) if (at(weights, j) < 1) s.push(j);
|
|
720
721
|
else l.push(j);
|
|
721
722
|
const probs = Array.from({ length: count }, () => 0);
|
|
722
723
|
const aliases = Array.from({ length: count }, () => 0);
|
|
723
724
|
while (s.length > 0 && l.length > 0) {
|
|
724
725
|
const a = s.pop();
|
|
725
726
|
const g = l.pop();
|
|
726
|
-
|
|
727
|
+
if (a === void 0 || g === void 0) break;
|
|
728
|
+
probs[a] = at(weights, a);
|
|
727
729
|
aliases[a] = g;
|
|
728
|
-
weights[g]
|
|
729
|
-
if (weights
|
|
730
|
+
weights[g] = at(weights, g) + (at(weights, a) - 1);
|
|
731
|
+
if (at(weights, g) < 1) s.push(g);
|
|
730
732
|
else l.push(g);
|
|
731
733
|
}
|
|
732
|
-
|
|
733
|
-
|
|
734
|
+
for (const i of l) probs[i] = 1;
|
|
735
|
+
for (const i of s) probs[i] = 1;
|
|
734
736
|
return new Weighted(aliases, probs);
|
|
735
737
|
}
|
|
736
738
|
next(xoshiro) {
|
|
@@ -738,23 +740,21 @@ var Weighted = class Weighted {
|
|
|
738
740
|
const r2 = xoshiro.nextDouble();
|
|
739
741
|
const n = this.probs.length;
|
|
740
742
|
const i = Math.trunc(n * r1);
|
|
741
|
-
if (r2 < this.probs
|
|
742
|
-
return this.aliases
|
|
743
|
+
if (r2 < at(this.probs, i)) return i;
|
|
744
|
+
return at(this.aliases, i);
|
|
743
745
|
}
|
|
744
746
|
};
|
|
745
747
|
//#endregion
|
|
746
748
|
//#region src/rng/xoshiro.ts
|
|
747
749
|
const MASK64 = (1n << 64n) - 1n;
|
|
748
750
|
function rotl(x, k) {
|
|
749
|
-
x
|
|
750
|
-
return (
|
|
751
|
+
const v = x & MASK64;
|
|
752
|
+
return (v << BigInt(k) | v >> BigInt(64 - k)) & MASK64;
|
|
751
753
|
}
|
|
752
754
|
function unitInterval(value) {
|
|
753
755
|
return Number(value >> 11n) * (1 / 2 ** 53);
|
|
754
756
|
}
|
|
755
|
-
/**
|
|
756
|
-
* Xoshiro256** with SHA-256 seeding matching URKit / ur-rs / bcur.
|
|
757
|
-
*/
|
|
757
|
+
/** Xoshiro256** with SHA-256 seeding matching URKit / ur-rs / bcur. */
|
|
758
758
|
var Xoshiro256 = class Xoshiro256 {
|
|
759
759
|
s0;
|
|
760
760
|
s1;
|
|
@@ -784,13 +784,8 @@ var Xoshiro256 = class Xoshiro256 {
|
|
|
784
784
|
return Xoshiro256.fromBytes(seed);
|
|
785
785
|
}
|
|
786
786
|
static fromDigest(seed32) {
|
|
787
|
-
const
|
|
788
|
-
|
|
789
|
-
let v = 0n;
|
|
790
|
-
for (let n = 0; n < 8; n++) v = v << 8n | BigInt(seed32[8 * i + n]);
|
|
791
|
-
limbs.push(v & MASK64);
|
|
792
|
-
}
|
|
793
|
-
return new Xoshiro256(limbs[0], limbs[1], limbs[2], limbs[3]);
|
|
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));
|
|
794
789
|
}
|
|
795
790
|
nextU64() {
|
|
796
791
|
const result = rotl(this.s1 * 5n & MASK64, 7) * 9n & MASK64;
|
|
@@ -807,20 +802,22 @@ var Xoshiro256 = class Xoshiro256 {
|
|
|
807
802
|
return unitInterval(this.nextU64());
|
|
808
803
|
}
|
|
809
804
|
/**
|
|
810
|
-
* Inclusive `[low, high]` via double scaling (normative float path).
|
|
811
|
-
*
|
|
805
|
+
* Inclusive `[low, high]` via double scaling (normative float path). Truncation toward zero
|
|
806
|
+
* matches Rust `as u64`.
|
|
812
807
|
*/
|
|
813
808
|
nextInt(low, high) {
|
|
814
809
|
const span = high - low + 1;
|
|
815
810
|
return Math.trunc(this.nextDouble() * span) + low;
|
|
816
811
|
}
|
|
817
|
-
/** Remove-order shuffle (not Fisher–Yates). */
|
|
818
|
-
shuffled(items) {
|
|
819
|
-
const pool = items
|
|
812
|
+
/** Remove-order shuffle (not Fisher–Yates); stops after `count` picks. */
|
|
813
|
+
shuffled(items, count = items.length) {
|
|
814
|
+
const pool = [...items];
|
|
820
815
|
const out = [];
|
|
821
|
-
while (pool.length > 0) {
|
|
816
|
+
while (pool.length > 0 && out.length < count) {
|
|
822
817
|
const index = this.nextInt(0, pool.length - 1);
|
|
823
|
-
|
|
818
|
+
const [item] = pool.splice(index, 1);
|
|
819
|
+
if (item === void 0) throw new Error("unreachable: splice returned empty");
|
|
820
|
+
out.push(item);
|
|
824
821
|
}
|
|
825
822
|
return out;
|
|
826
823
|
}
|
|
@@ -857,28 +854,53 @@ function partition(data, fragLen) {
|
|
|
857
854
|
return out;
|
|
858
855
|
}
|
|
859
856
|
/**
|
|
860
|
-
*
|
|
861
|
-
*
|
|
857
|
+
* Per-stream index generator (BCR-2024-001 §4 FragmentChooser): harmonic degree sampler built once,
|
|
858
|
+
* partial remove-shuffle per sequence.
|
|
862
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. */
|
|
863
893
|
function chooseFragments(sequence, fragmentCount, checksum) {
|
|
864
894
|
if (sequence <= fragmentCount) return [sequence - 1];
|
|
865
|
-
|
|
866
|
-
seed[0] = sequence >>> 24 & 255;
|
|
867
|
-
seed[1] = sequence >>> 16 & 255;
|
|
868
|
-
seed[2] = sequence >>> 8 & 255;
|
|
869
|
-
seed[3] = sequence & 255;
|
|
870
|
-
seed[4] = checksum >>> 24 & 255;
|
|
871
|
-
seed[5] = checksum >>> 16 & 255;
|
|
872
|
-
seed[6] = checksum >>> 8 & 255;
|
|
873
|
-
seed[7] = checksum & 255;
|
|
874
|
-
const xoshiro = Xoshiro256.fromBytes(seed);
|
|
875
|
-
const degree = xoshiro.chooseDegree(fragmentCount);
|
|
876
|
-
const indexes = Array.from({ length: fragmentCount }, (_, i) => i);
|
|
877
|
-
return xoshiro.shuffled(indexes).slice(0, degree);
|
|
895
|
+
return new FragmentChooser(fragmentCount, checksum).choose(sequence);
|
|
878
896
|
}
|
|
879
897
|
//#endregion
|
|
880
898
|
//#region src/fountain/limits.ts
|
|
881
|
-
/**
|
|
899
|
+
/**
|
|
900
|
+
* Production default; same integers as bcur 1.0.
|
|
901
|
+
*
|
|
902
|
+
* Hosts that need another budget pass `new Decoder({ limits })`.
|
|
903
|
+
*/
|
|
882
904
|
const DEFAULT_LIMITS = {
|
|
883
905
|
maxMessageLength: 1048576,
|
|
884
906
|
maxFragmentCount: 2e3,
|
|
@@ -897,7 +919,7 @@ function mergeLimits(partial) {
|
|
|
897
919
|
//#region src/fountain/part-cbor.ts
|
|
898
920
|
/** Encode a part to deterministic fixed-schema CBOR. */
|
|
899
921
|
function encodePart(part) {
|
|
900
|
-
const data = part
|
|
922
|
+
const { data } = part;
|
|
901
923
|
const out = [133];
|
|
902
924
|
encodeU32(out, part.sequence);
|
|
903
925
|
encodeU32(out, part.sequenceCount);
|
|
@@ -919,7 +941,13 @@ function decodePart(bytes, maxDataLen, maxFragmentCount) {
|
|
|
919
941
|
if (sequence === 0) fail("InvalidSequence");
|
|
920
942
|
if (sequenceCount === 0 || messageLength === 0 || data.length === 0) fail("EmptyPart");
|
|
921
943
|
if (sequenceCount > maxFragmentCount) fail("ResourceLimit", { limit: "fragment_count" });
|
|
922
|
-
return
|
|
944
|
+
return {
|
|
945
|
+
sequence,
|
|
946
|
+
sequenceCount,
|
|
947
|
+
messageLength,
|
|
948
|
+
checksum,
|
|
949
|
+
data
|
|
950
|
+
};
|
|
923
951
|
}
|
|
924
952
|
function encodeU32(out, v) {
|
|
925
953
|
if (v <= 23) out.push(v);
|
|
@@ -933,7 +961,7 @@ function encodeBstr(out, data) {
|
|
|
933
961
|
else if (len <= 255) out.push(88, len);
|
|
934
962
|
else if (len <= 65535) out.push(89, len >>> 8 & 255, len & 255);
|
|
935
963
|
else out.push(90, len >>> 24 & 255, len >>> 16 & 255, len >>> 8 & 255, len & 255);
|
|
936
|
-
for (
|
|
964
|
+
for (const b of data) out.push(b);
|
|
937
965
|
}
|
|
938
966
|
function decodeU32(bytes, cur) {
|
|
939
967
|
const head = read(bytes, cur);
|
|
@@ -956,7 +984,7 @@ function decodeU32(bytes, cur) {
|
|
|
956
984
|
if (v <= 65535) fail("InvalidPartCbor");
|
|
957
985
|
return v;
|
|
958
986
|
}
|
|
959
|
-
fail("InvalidPartCbor");
|
|
987
|
+
return fail("InvalidPartCbor");
|
|
960
988
|
}
|
|
961
989
|
function decodeBstr(bytes, cur, maxDataLen) {
|
|
962
990
|
const head = read(bytes, cur);
|
|
@@ -982,8 +1010,10 @@ function decodeBstr(bytes, cur, maxDataLen) {
|
|
|
982
1010
|
return data;
|
|
983
1011
|
}
|
|
984
1012
|
function read(bytes, cur) {
|
|
985
|
-
|
|
986
|
-
|
|
1013
|
+
const b = bytes[cur.i];
|
|
1014
|
+
if (b === void 0) fail("InvalidPartCbor");
|
|
1015
|
+
cur.i += 1;
|
|
1016
|
+
return b;
|
|
987
1017
|
}
|
|
988
1018
|
//#endregion
|
|
989
1019
|
//#region src/fountain/part.ts
|
|
@@ -1014,7 +1044,8 @@ var Part = class Part {
|
|
|
1014
1044
|
return encodePart(this);
|
|
1015
1045
|
}
|
|
1016
1046
|
static fromCbor(bytes, maxDataLen = DEFAULT_LIMITS.maxFragmentDataLength, maxFragmentCount = DEFAULT_LIMITS.maxFragmentCount) {
|
|
1017
|
-
|
|
1047
|
+
const f = decodePart(bytes, maxDataLen, maxFragmentCount);
|
|
1048
|
+
return Part.fromFields(f.sequence, f.sequenceCount, f.messageLength, f.checksum, f.data);
|
|
1018
1049
|
}
|
|
1019
1050
|
sequenceId() {
|
|
1020
1051
|
return `${this.sequence}-${this.sequenceCount}`;
|
|
@@ -1027,19 +1058,25 @@ function keyOf(indexes) {
|
|
|
1027
1058
|
}
|
|
1028
1059
|
function xorInto$1(target, src) {
|
|
1029
1060
|
if (target.length !== src.length) fail("DecoderState");
|
|
1030
|
-
for (
|
|
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
|
+
}
|
|
1031
1066
|
}
|
|
1032
1067
|
/** Fountain decoder with resource limits and fail-closed poison. */
|
|
1033
1068
|
var FountainDecoder = class {
|
|
1034
1069
|
decoded = /* @__PURE__ */ new Map();
|
|
1035
1070
|
received = /* @__PURE__ */ new Set();
|
|
1036
1071
|
buffer = /* @__PURE__ */ new Map();
|
|
1072
|
+
bufferIndex = /* @__PURE__ */ new Map();
|
|
1037
1073
|
queue = [];
|
|
1038
1074
|
sequenceCount = 0;
|
|
1039
1075
|
messageLength = 0;
|
|
1040
1076
|
messageChecksum = 0;
|
|
1041
1077
|
fragmentLength = 0;
|
|
1042
1078
|
limits;
|
|
1079
|
+
chooser;
|
|
1043
1080
|
poisoned;
|
|
1044
1081
|
constructor(limits) {
|
|
1045
1082
|
this.limits = mergeLimits(limits);
|
|
@@ -1075,7 +1112,8 @@ var FountainDecoder = class {
|
|
|
1075
1112
|
}
|
|
1076
1113
|
/**
|
|
1077
1114
|
* Receive a fountain part.
|
|
1078
|
-
*
|
|
1115
|
+
*
|
|
1116
|
+
* @returns Whether the part was newly ingested.
|
|
1079
1117
|
*/
|
|
1080
1118
|
receive(part) {
|
|
1081
1119
|
if (this.poisoned) failPoison(this.poisoned);
|
|
@@ -1096,22 +1134,27 @@ var FountainDecoder = class {
|
|
|
1096
1134
|
this.messageLength = ml;
|
|
1097
1135
|
this.messageChecksum = part.checksum;
|
|
1098
1136
|
this.fragmentLength = fragLen;
|
|
1137
|
+
this.chooser = new FragmentChooser(sc, part.checksum);
|
|
1099
1138
|
} else if (!this.validate(part)) fail("InconsistentPart");
|
|
1100
|
-
const
|
|
1139
|
+
const { chooser } = this;
|
|
1140
|
+
if (chooser === void 0) fail("DecoderState");
|
|
1141
|
+
const indexes = chooser.choose(part.sequence);
|
|
1142
|
+
const key = keyOf(indexes);
|
|
1101
1143
|
if (this.received.has(key)) return false;
|
|
1102
1144
|
if (this.received.size >= this.limits.maxReceivedParts) this.poison("received_parts");
|
|
1103
1145
|
this.received.add(key);
|
|
1104
1146
|
try {
|
|
1105
|
-
if (
|
|
1106
|
-
else this.processComplex(part);
|
|
1147
|
+
if (indexes.length === 1) this.enqueueSimple(part, indexes);
|
|
1148
|
+
else this.processComplex(part, indexes);
|
|
1107
1149
|
this.processQueue();
|
|
1108
|
-
} catch (
|
|
1109
|
-
this.escalate(
|
|
1150
|
+
} catch (error) {
|
|
1151
|
+
this.escalate(error);
|
|
1110
1152
|
}
|
|
1111
1153
|
return true;
|
|
1112
1154
|
}
|
|
1113
|
-
enqueueSimple(part) {
|
|
1114
|
-
const index =
|
|
1155
|
+
enqueueSimple(part, indexes) {
|
|
1156
|
+
const [index] = indexes;
|
|
1157
|
+
if (index === void 0) fail("DecoderState");
|
|
1115
1158
|
if (this.decoded.has(index)) return;
|
|
1116
1159
|
this.decoded.set(index, part);
|
|
1117
1160
|
this.queue.push({
|
|
@@ -1121,9 +1164,11 @@ var FountainDecoder = class {
|
|
|
1121
1164
|
}
|
|
1122
1165
|
processQueue() {
|
|
1123
1166
|
while (this.queue.length > 0) {
|
|
1124
|
-
const
|
|
1125
|
-
|
|
1126
|
-
|
|
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;
|
|
1127
1172
|
for (const k of toProcess) this.reduceBufferedPart(k, index, simple);
|
|
1128
1173
|
}
|
|
1129
1174
|
}
|
|
@@ -1131,28 +1176,35 @@ var FountainDecoder = class {
|
|
|
1131
1176
|
const entry = this.buffer.get(key);
|
|
1132
1177
|
if (!entry) fail("DecoderState");
|
|
1133
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
|
+
}
|
|
1134
1185
|
const newIndexes = entry.indexes.filter((x) => x !== knownIndex);
|
|
1135
1186
|
if (newIndexes.length === entry.indexes.length) fail("DecoderState");
|
|
1136
|
-
const data = entry.part.data
|
|
1187
|
+
const data = new Uint8Array(entry.part.data);
|
|
1137
1188
|
xorInto$1(data, simple.data);
|
|
1138
1189
|
const reduced = Part.fromFields(entry.part.sequence, entry.part.sequenceCount, entry.part.messageLength, entry.part.checksum, data);
|
|
1139
1190
|
this.insertReduced(newIndexes, reduced);
|
|
1140
1191
|
}
|
|
1141
|
-
processComplex(part) {
|
|
1142
|
-
|
|
1143
|
-
const
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
xorInto$1(data, this.decoded.get(remove).data);
|
|
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);
|
|
1149
1199
|
}
|
|
1200
|
+
if (remaining.length === 0) return;
|
|
1150
1201
|
const reduced = Part.fromFields(part.sequence, part.sequenceCount, part.messageLength, part.checksum, data);
|
|
1151
|
-
this.insertReduced(
|
|
1202
|
+
this.insertReduced(remaining, reduced);
|
|
1152
1203
|
}
|
|
1153
1204
|
insertReduced(indexes, part) {
|
|
1154
1205
|
if (indexes.length === 1) {
|
|
1155
|
-
const idx = indexes
|
|
1206
|
+
const [idx] = indexes;
|
|
1207
|
+
if (idx === void 0) fail("DecoderState");
|
|
1156
1208
|
if (this.decoded.has(idx)) return;
|
|
1157
1209
|
this.decoded.set(idx, part);
|
|
1158
1210
|
this.queue.push({
|
|
@@ -1167,6 +1219,14 @@ var FountainDecoder = class {
|
|
|
1167
1219
|
indexes,
|
|
1168
1220
|
part
|
|
1169
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
|
+
}
|
|
1170
1230
|
}
|
|
1171
1231
|
get complete() {
|
|
1172
1232
|
return this.messageLength !== 0 && this.decoded.size === this.sequenceCount;
|
|
@@ -1184,7 +1244,7 @@ var FountainDecoder = class {
|
|
|
1184
1244
|
/** Decoded message if complete; otherwise `undefined`. */
|
|
1185
1245
|
message() {
|
|
1186
1246
|
if (this.poisoned) failPoison(this.poisoned);
|
|
1187
|
-
if (!this.complete) return
|
|
1247
|
+
if (!this.complete) return;
|
|
1188
1248
|
const combined = new Uint8Array(this.fragmentLength * this.sequenceCount);
|
|
1189
1249
|
for (let idx = 0; idx < this.sequenceCount; idx++) {
|
|
1190
1250
|
const part = this.decoded.get(idx);
|
|
@@ -1194,14 +1254,18 @@ var FountainDecoder = class {
|
|
|
1194
1254
|
for (let i = this.messageLength; i < combined.length; i++) if (combined[i] !== 0) fail("InvalidPadding");
|
|
1195
1255
|
const message = combined.subarray(0, this.messageLength);
|
|
1196
1256
|
if (checksum(message) !== this.messageChecksum) fail("InvalidMessageChecksum");
|
|
1197
|
-
return message
|
|
1257
|
+
return new Uint8Array(message);
|
|
1198
1258
|
}
|
|
1199
1259
|
};
|
|
1200
1260
|
//#endregion
|
|
1201
1261
|
//#region src/fountain/encoder.ts
|
|
1202
1262
|
function xorInto(target, src) {
|
|
1203
1263
|
if (target.length !== src.length) fail("DecoderState");
|
|
1204
|
-
for (
|
|
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
|
+
}
|
|
1205
1269
|
}
|
|
1206
1270
|
/** Next 1-based fountain seqNum. Does not wrap. */
|
|
1207
1271
|
function nextSequence(current) {
|
|
@@ -1214,16 +1278,18 @@ var FountainEncoder = class FountainEncoder {
|
|
|
1214
1278
|
sequenceCount;
|
|
1215
1279
|
messageLength;
|
|
1216
1280
|
messageChecksum;
|
|
1281
|
+
chooser;
|
|
1217
1282
|
currentSequence = 0;
|
|
1218
1283
|
constructor(parts, sequenceCount, messageLength, messageChecksum) {
|
|
1219
1284
|
this.parts = parts;
|
|
1220
1285
|
this.sequenceCount = sequenceCount;
|
|
1221
1286
|
this.messageLength = messageLength;
|
|
1222
1287
|
this.messageChecksum = messageChecksum;
|
|
1288
|
+
this.chooser = new FragmentChooser(sequenceCount, messageChecksum);
|
|
1223
1289
|
}
|
|
1224
1290
|
static create(message, maxFragmentLength) {
|
|
1225
1291
|
if (message.length === 0) fail("EmptyMessage");
|
|
1226
|
-
if (maxFragmentLength
|
|
1292
|
+
if (!Number.isSafeInteger(maxFragmentLength) || maxFragmentLength < 1) fail("InvalidFragmentLen");
|
|
1227
1293
|
if (message.length > 4294967295) fail("ResourceLimit", { limit: "message_length" });
|
|
1228
1294
|
const fragments = partition(message, fragmentLength(message.length, maxFragmentLength));
|
|
1229
1295
|
if (fragments.length > 4294967295) fail("ResourceLimit", { limit: "fragment_count" });
|
|
@@ -1241,10 +1307,15 @@ var FountainEncoder = class FountainEncoder {
|
|
|
1241
1307
|
nextPart() {
|
|
1242
1308
|
if (this.sequenceCount === 1 && this.currentSequence >= 1) fail("SinglePartExhausted");
|
|
1243
1309
|
this.currentSequence = nextSequence(this.currentSequence);
|
|
1244
|
-
const indexes =
|
|
1245
|
-
const first = this.parts
|
|
1310
|
+
const indexes = this.chooser.choose(this.currentSequence);
|
|
1311
|
+
const [first] = this.parts;
|
|
1312
|
+
if (first === void 0) fail("DecoderState");
|
|
1246
1313
|
const mixed = new Uint8Array(first.length);
|
|
1247
|
-
for (const idx of indexes)
|
|
1314
|
+
for (const idx of indexes) {
|
|
1315
|
+
const fragment = this.parts[idx];
|
|
1316
|
+
if (fragment === void 0) fail("DecoderState");
|
|
1317
|
+
xorInto(mixed, fragment);
|
|
1318
|
+
}
|
|
1248
1319
|
return Part.fromFields(this.currentSequence, this.sequenceCount, this.messageLength, this.messageChecksum, mixed);
|
|
1249
1320
|
}
|
|
1250
1321
|
};
|
|
@@ -1283,12 +1354,12 @@ function parseNormalized(uri) {
|
|
|
1283
1354
|
if (!uri.startsWith("ur:")) fail("InvalidScheme");
|
|
1284
1355
|
const rest0 = uri.slice(3);
|
|
1285
1356
|
const slash = rest0.indexOf("/");
|
|
1286
|
-
if (slash
|
|
1357
|
+
if (slash === -1) fail("TypeUnspecified");
|
|
1287
1358
|
const typeStr = rest0.slice(0, slash);
|
|
1288
1359
|
const rest = rest0.slice(slash + 1);
|
|
1289
1360
|
const urType = UrType.parse(typeStr);
|
|
1290
1361
|
const lastSlash = rest.lastIndexOf("/");
|
|
1291
|
-
if (lastSlash
|
|
1362
|
+
if (lastSlash === -1) return {
|
|
1292
1363
|
type: urType,
|
|
1293
1364
|
kind: "single",
|
|
1294
1365
|
body: rest.toLowerCase()
|
|
@@ -1304,7 +1375,7 @@ function parseNormalized(uri) {
|
|
|
1304
1375
|
}
|
|
1305
1376
|
function decodeIndices(indices) {
|
|
1306
1377
|
const dash = indices.indexOf("-");
|
|
1307
|
-
if (dash
|
|
1378
|
+
if (dash === -1) fail("InvalidIndices");
|
|
1308
1379
|
const a = indices.slice(0, dash);
|
|
1309
1380
|
const b = indices.slice(dash + 1);
|
|
1310
1381
|
if (!/^\d+$/.test(a) || !/^\d+$/.test(b)) fail("InvalidIndices");
|
|
@@ -1326,8 +1397,8 @@ function encode(data, type) {
|
|
|
1326
1397
|
return `ur:${type.value}/${body}`;
|
|
1327
1398
|
}
|
|
1328
1399
|
/**
|
|
1329
|
-
* Decode payload from a single- or multi-part UR.
|
|
1330
|
-
*
|
|
1400
|
+
* Decode payload from a single- or multi-part UR. Multi-part returns the CBOR-encoded fountain part
|
|
1401
|
+
* bytes, not the message.
|
|
1331
1402
|
*/
|
|
1332
1403
|
function decode(uri) {
|
|
1333
1404
|
const parsed = parse(uri);
|
|
@@ -1369,7 +1440,7 @@ var Encoder = class Encoder {
|
|
|
1369
1440
|
this.message = message;
|
|
1370
1441
|
}
|
|
1371
1442
|
static create(message, maxFragmentLength, type) {
|
|
1372
|
-
return new Encoder(FountainEncoder.create(message, maxFragmentLength), type, message
|
|
1443
|
+
return new Encoder(FountainEncoder.create(message, maxFragmentLength), type, new Uint8Array(message));
|
|
1373
1444
|
}
|
|
1374
1445
|
static bytes(message, maxFragmentLength) {
|
|
1375
1446
|
return Encoder.create(message, maxFragmentLength, UrType.bytes());
|
|
@@ -1435,8 +1506,10 @@ var Decoder = class {
|
|
|
1435
1506
|
receive(uri) {
|
|
1436
1507
|
if (this.poisoned) failPoison(this.poisoned);
|
|
1437
1508
|
if (this.fountain.isPoisoned) {
|
|
1438
|
-
|
|
1439
|
-
|
|
1509
|
+
const poison = this.fountain.poisonState;
|
|
1510
|
+
if (poison === void 0) fail("DecoderState");
|
|
1511
|
+
this.poisoned ??= poison;
|
|
1512
|
+
failPoison(poison);
|
|
1440
1513
|
}
|
|
1441
1514
|
if (uri.length > this.maxUriLen) this.poison("uri_len");
|
|
1442
1515
|
const parsed = parse(uri);
|
|
@@ -1451,8 +1524,8 @@ var Decoder = class {
|
|
|
1451
1524
|
try {
|
|
1452
1525
|
if (parsed.kind === "single") this.receiveSingle(parsed);
|
|
1453
1526
|
else this.receiveFountain(parsed);
|
|
1454
|
-
} catch (
|
|
1455
|
-
this.escalate(
|
|
1527
|
+
} catch (error) {
|
|
1528
|
+
this.escalate(error);
|
|
1456
1529
|
}
|
|
1457
1530
|
}
|
|
1458
1531
|
receiveSingle(parsed) {
|
|
@@ -1467,7 +1540,7 @@ var Decoder = class {
|
|
|
1467
1540
|
if (this.single !== void 0) fail("InconsistentPart");
|
|
1468
1541
|
const decoded = decode$1(parsed.body, "minimal");
|
|
1469
1542
|
const part = Part.fromCbor(decoded, this.fountain.maxFragmentDataLength, this.fountain.maxFragmentCount);
|
|
1470
|
-
const indices = parsed
|
|
1543
|
+
const { indices } = parsed;
|
|
1471
1544
|
if (!indices) fail("InvalidIndices");
|
|
1472
1545
|
if (part.sequence !== indices.seq || part.sequenceCount !== indices.count) fail("InvalidIndices");
|
|
1473
1546
|
this.fountain.receive(part);
|
|
@@ -1478,8 +1551,12 @@ var Decoder = class {
|
|
|
1478
1551
|
}
|
|
1479
1552
|
message() {
|
|
1480
1553
|
if (this.poisoned) failPoison(this.poisoned);
|
|
1481
|
-
if (this.fountain.isPoisoned)
|
|
1482
|
-
|
|
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);
|
|
1483
1560
|
return this.fountain.message();
|
|
1484
1561
|
}
|
|
1485
1562
|
resolvedFragmentCount() {
|