@johnhenry/sitefile 0.0.0-stage → 0.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.
Files changed (174) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/LICENSE +21 -0
  3. package/README.md +475 -2
  4. package/lib/src/archive-writers.d.ts +31 -0
  5. package/lib/src/archive-writers.js +197 -0
  6. package/lib/src/attest.d.ts +126 -0
  7. package/lib/src/attest.js +220 -0
  8. package/lib/src/bake.d.ts +74 -0
  9. package/lib/src/bake.js +372 -0
  10. package/lib/src/base-image.d.ts +76 -0
  11. package/lib/src/base-image.js +138 -0
  12. package/lib/src/build-args.d.ts +26 -0
  13. package/lib/src/build-args.js +164 -0
  14. package/lib/src/cache-limit.d.ts +54 -0
  15. package/lib/src/cache-limit.js +239 -0
  16. package/lib/src/casefold-data.d.ts +2 -0
  17. package/lib/src/casefold-data.js +217 -0
  18. package/lib/src/cbor.d.ts +39 -0
  19. package/lib/src/cbor.js +174 -0
  20. package/lib/src/checklinks.d.ts +42 -0
  21. package/lib/src/checklinks.js +118 -0
  22. package/lib/src/cli/bake.d.ts +2 -0
  23. package/lib/src/cli/bake.js +179 -0
  24. package/lib/src/cli/build-image.d.ts +21 -0
  25. package/lib/src/cli/build-image.js +42 -0
  26. package/lib/src/cli/cache.d.ts +8 -0
  27. package/lib/src/cli/cache.js +109 -0
  28. package/lib/src/cli/common.d.ts +29 -0
  29. package/lib/src/cli/common.js +100 -0
  30. package/lib/src/cli/git-context.d.ts +18 -0
  31. package/lib/src/cli/git-context.js +48 -0
  32. package/lib/src/cli/images.d.ts +6 -0
  33. package/lib/src/cli/images.js +285 -0
  34. package/lib/src/cli/index.d.ts +10 -0
  35. package/lib/src/cli/index.js +95 -0
  36. package/lib/src/cli/outdated.d.ts +5 -0
  37. package/lib/src/cli/outdated.js +94 -0
  38. package/lib/src/cli/registry.d.ts +2 -0
  39. package/lib/src/cli/registry.js +122 -0
  40. package/lib/src/cli/serve-worker.d.ts +2 -0
  41. package/lib/src/cli/serve-worker.js +42 -0
  42. package/lib/src/cli/servers.d.ts +15 -0
  43. package/lib/src/cli/servers.js +451 -0
  44. package/lib/src/cli/vendor.d.ts +2 -0
  45. package/lib/src/cli/vendor.js +92 -0
  46. package/lib/src/cli/watch.d.ts +26 -0
  47. package/lib/src/cli/watch.js +81 -0
  48. package/lib/src/cli.d.ts +12 -0
  49. package/lib/src/cli.js +486 -0
  50. package/lib/src/csv.d.ts +27 -0
  51. package/lib/src/csv.js +117 -0
  52. package/lib/src/deflate.d.ts +76 -0
  53. package/lib/src/deflate.js +604 -0
  54. package/lib/src/digest.d.ts +3 -0
  55. package/lib/src/digest.js +7 -0
  56. package/lib/src/errors.d.ts +34 -0
  57. package/lib/src/errors.js +20 -0
  58. package/lib/src/executor.d.ts +386 -0
  59. package/lib/src/executor.js +2193 -0
  60. package/lib/src/explain.d.ts +64 -0
  61. package/lib/src/explain.js +445 -0
  62. package/lib/src/export-formats.d.ts +16 -0
  63. package/lib/src/export-formats.js +52 -0
  64. package/lib/src/exporter.d.ts +103 -0
  65. package/lib/src/exporter.js +676 -0
  66. package/lib/src/fingerprint.d.ts +40 -0
  67. package/lib/src/fingerprint.js +168 -0
  68. package/lib/src/frontmatter.d.ts +22 -0
  69. package/lib/src/frontmatter.js +45 -0
  70. package/lib/src/index.d.ts +12 -0
  71. package/lib/src/index.js +12 -0
  72. package/lib/src/language/ast.d.ts +353 -0
  73. package/lib/src/language/ast.js +57 -0
  74. package/lib/src/language/parser.d.ts +3 -0
  75. package/lib/src/language/parser.js +612 -0
  76. package/lib/src/language/scanner.d.ts +110 -0
  77. package/lib/src/language/scanner.js +449 -0
  78. package/lib/src/language/validate.d.ts +13 -0
  79. package/lib/src/language/validate.js +367 -0
  80. package/lib/src/links.d.ts +79 -0
  81. package/lib/src/links.js +583 -0
  82. package/lib/src/lint.d.ts +10 -0
  83. package/lib/src/lint.js +223 -0
  84. package/lib/src/markdown.d.ts +29 -0
  85. package/lib/src/markdown.js +612 -0
  86. package/lib/src/media.d.ts +82 -0
  87. package/lib/src/media.js +650 -0
  88. package/lib/src/memo.d.ts +57 -0
  89. package/lib/src/memo.js +93 -0
  90. package/lib/src/oci.d.ts +178 -0
  91. package/lib/src/oci.js +380 -0
  92. package/lib/src/outdated.d.ts +46 -0
  93. package/lib/src/outdated.js +195 -0
  94. package/lib/src/pages.d.ts +10 -0
  95. package/lib/src/pages.js +54 -0
  96. package/lib/src/paths.d.ts +39 -0
  97. package/lib/src/paths.js +101 -0
  98. package/lib/src/planner.d.ts +297 -0
  99. package/lib/src/planner.js +410 -0
  100. package/lib/src/plugins/cache.d.ts +70 -0
  101. package/lib/src/plugins/cache.js +226 -0
  102. package/lib/src/plugins/exec.d.ts +51 -0
  103. package/lib/src/plugins/exec.js +144 -0
  104. package/lib/src/plugins/host.d.ts +75 -0
  105. package/lib/src/plugins/host.js +277 -0
  106. package/lib/src/plugins/wasm.d.ts +23 -0
  107. package/lib/src/plugins/wasm.js +249 -0
  108. package/lib/src/profile.d.ts +42 -0
  109. package/lib/src/profile.js +68 -0
  110. package/lib/src/ref.d.ts +40 -0
  111. package/lib/src/ref.js +79 -0
  112. package/lib/src/registry-auth.d.ts +32 -0
  113. package/lib/src/registry-auth.js +151 -0
  114. package/lib/src/registry-cache.d.ts +71 -0
  115. package/lib/src/registry-cache.js +333 -0
  116. package/lib/src/registry-ops.d.ts +51 -0
  117. package/lib/src/registry-ops.js +248 -0
  118. package/lib/src/registry.d.ts +109 -0
  119. package/lib/src/registry.js +505 -0
  120. package/lib/src/render.d.ts +134 -0
  121. package/lib/src/render.js +576 -0
  122. package/lib/src/resources/archive.d.ts +11 -0
  123. package/lib/src/resources/archive.js +306 -0
  124. package/lib/src/resources/bundle-data.d.ts +16 -0
  125. package/lib/src/resources/bundle-data.js +50 -0
  126. package/lib/src/resources/convert.d.ts +12 -0
  127. package/lib/src/resources/convert.js +63 -0
  128. package/lib/src/resources/documents.d.ts +35 -0
  129. package/lib/src/resources/documents.js +122 -0
  130. package/lib/src/resources/git.d.ts +103 -0
  131. package/lib/src/resources/git.js +720 -0
  132. package/lib/src/resources/https.d.ts +40 -0
  133. package/lib/src/resources/https.js +256 -0
  134. package/lib/src/resources/ignore.d.ts +33 -0
  135. package/lib/src/resources/ignore.js +119 -0
  136. package/lib/src/resources/local.d.ts +64 -0
  137. package/lib/src/resources/local.js +248 -0
  138. package/lib/src/resources/lockfile.d.ts +52 -0
  139. package/lib/src/resources/lockfile.js +172 -0
  140. package/lib/src/resources/netpolicy.d.ts +6 -0
  141. package/lib/src/resources/netpolicy.js +82 -0
  142. package/lib/src/resources/types.d.ts +63 -0
  143. package/lib/src/resources/types.js +13 -0
  144. package/lib/src/resources/vendor.d.ts +94 -0
  145. package/lib/src/resources/vendor.js +296 -0
  146. package/lib/src/resources/wbn-reader.d.ts +44 -0
  147. package/lib/src/resources/wbn-reader.js +302 -0
  148. package/lib/src/schema.d.ts +25 -0
  149. package/lib/src/schema.js +169 -0
  150. package/lib/src/secrets.d.ts +21 -0
  151. package/lib/src/secrets.js +103 -0
  152. package/lib/src/serve-rules.d.ts +29 -0
  153. package/lib/src/serve-rules.js +122 -0
  154. package/lib/src/server.d.ts +45 -0
  155. package/lib/src/server.js +246 -0
  156. package/lib/src/servers.d.ts +71 -0
  157. package/lib/src/servers.js +139 -0
  158. package/lib/src/site.d.ts +60 -0
  159. package/lib/src/site.js +169 -0
  160. package/lib/src/store.d.ts +151 -0
  161. package/lib/src/store.js +596 -0
  162. package/lib/src/tree.d.ts +50 -0
  163. package/lib/src/tree.js +134 -0
  164. package/lib/src/values.d.ts +7 -0
  165. package/lib/src/values.js +37 -0
  166. package/lib/src/vendor.d.ts +19 -0
  167. package/lib/src/vendor.js +145 -0
  168. package/lib/src/watch.d.ts +44 -0
  169. package/lib/src/watch.js +137 -0
  170. package/lib/src/wbn.d.ts +5 -0
  171. package/lib/src/wbn.js +147 -0
  172. package/lib/src/yaml.d.ts +34 -0
  173. package/lib/src/yaml.js +562 -0
  174. package/package.json +58 -4
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Deterministic DEFLATE (RFC 1951) and gzip (RFC 1952) encoder.
3
+ *
4
+ * `rust/src/deflate.rs` implements the same algorithm and must produce the same
5
+ * bytes for every input, so that archives and OCI layers (and therefore image
6
+ * IDs) are identical between the two implementations. Any change here must be
7
+ * made in both files, and the differential fixtures regenerated
8
+ * (`node rust/tests/fixtures/deflate/generate.mjs`). Decompression is not
9
+ * affected: any inflater reads this output.
10
+ *
11
+ * Specification (every choice below is part of the output format):
12
+ *
13
+ * LZ77 matching
14
+ * - Window 32768 bytes; matches are 3..258 bytes at distance 1..32768.
15
+ * - Hash of position p (only when p + 3 <= n): x = d[p] << 16 | d[p+1] << 8 | d[p+2];
16
+ * h = (x * 0x9E3779B1 mod 2^32) >> 17 (15 bits). `head[h]` holds the most
17
+ * recent inserted position with that hash, `prev[p & 32767]` the position
18
+ * that was in `head[h]` when p was inserted (-1 = none).
19
+ * - findMatch(p): if p + 3 > n, no match. Otherwise walk the chain starting
20
+ * at head[h(p)]: candidate c is considered while c >= 0, p - c <= 32768 and
21
+ * fewer than MAX_CHAIN (64) candidates have been considered; the next
22
+ * candidate is prev[c & 32767], and the walk stops if that is not < c.
23
+ * For each candidate, len = length of the common prefix of d[c..] and
24
+ * d[p..], at most min(258, n - p). A candidate replaces the best only if
25
+ * len > bestLen (so on ties the nearest wins); the walk stops once
26
+ * bestLen >= 258 (or reaches min(258, n - p)). Then p is inserted.
27
+ * A best match of length 3 at distance > 4096 is dropped (no match).
28
+ * - Every position p with p + 3 <= n is inserted exactly once, in increasing
29
+ * order, before findMatch is called for any later position.
30
+ * - Parsing (lazy, one position at a time): at p, m = findMatch(p). If
31
+ * m.len < 3: literal d[p], p += 1. Otherwise, while m.len < LAZY_LIMIT (16)
32
+ * and p + 1 < n: m2 = findMatch(p + 1), searching at most MAX_CHAIN / 4 (16)
33
+ * candidates when m.len >= GOOD_LENGTH (8); if m2.len > m.len, emit literal
34
+ * d[p], p += 1, m = m2 and repeat; else stop. Then emit match m at p, insert
35
+ * the positions up to p + m.len - 1 that are not yet inserted, p += m.len.
36
+ *
37
+ * Blocks
38
+ * - Symbols (literals and length/distance pairs) are buffered. At the start
39
+ * of each parsing step (p < n), if the buffer holds >= BLOCK_SYMBOLS (16384)
40
+ * symbols it is written as a non-final block. When the input is exhausted
41
+ * the remaining symbols (none only for empty input) are written as the
42
+ * final block (BFINAL = 1).
43
+ * - Each block is written as whichever of stored, fixed Huffman or dynamic
44
+ * Huffman takes the fewest bits, given the current bit position; on a tie
45
+ * the earlier of stored, fixed, dynamic wins. Stored is a candidate only
46
+ * when the block covers <= 65535 input bytes; its cost is 3 header bits,
47
+ * padding to a byte boundary, 32 bits of LEN/NLEN and 8 bits per byte.
48
+ * - Huffman code lengths (literal/length tree max 15 bits, distance tree max
49
+ * 15, code-length tree max 7) come from package-merge: leaves are the
50
+ * symbols with nonzero frequency sorted by (frequency, symbol); L - 1 times,
51
+ * pair consecutive items of the previous list (dropping an odd last item)
52
+ * into packages and merge them with the leaves by weight, a leaf before a
53
+ * package on equal weight; a symbol's length is the number of times it
54
+ * occurs in the first 2k - 2 items of the final list (k = leaf count). If
55
+ * fewer than two symbols are used, the used one (if any) and the lowest
56
+ * unused symbols are added until there are two, each with length 1.
57
+ * Codes are canonical (RFC 1951 3.2.2).
58
+ * - Dynamic header: HLIT = max(257, last used literal/length symbol + 1),
59
+ * HDIST = max(1, last used distance symbol + 1) (an unused tree still has
60
+ * two length-1 codes, so this is at least 2); HCLEN = max(4, last nonzero
61
+ * entry in the order 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 + 1).
62
+ * The HLIT + HDIST lengths form one sequence, run-length coded left to
63
+ * right: for value v with run r of equal values starting here, if v = 0:
64
+ * r >= 11 → 18 (min(r, 138)), r >= 3 → 17 (r), else a single 0; if v != 0:
65
+ * emit v once, then while the rest of the run is >= 3, 16 (min(rest, 6)).
66
+ *
67
+ * gzip: header 1f 8b 08, FLG 0, MTIME 0, XFL 0, OS 3 (Unix), then the raw
68
+ * deflate stream, CRC-32 and ISIZE (little-endian).
69
+ */
70
+ /** Package-merge length-limited Huffman code lengths. */
71
+ export declare function codeLengths(freq: ArrayLike<number>, maxBits: number): Uint8Array;
72
+ /** Raw DEFLATE stream (zip method 8). */
73
+ export declare function deflateRaw(data: Uint8Array): Uint8Array;
74
+ export declare function crc32(data: Uint8Array): number;
75
+ /** gzip member with a fixed header (no mtime, no name, XFL 0, OS 3). */
76
+ export declare function gzip(data: Uint8Array): Uint8Array;
@@ -0,0 +1,604 @@
1
+ /**
2
+ * Deterministic DEFLATE (RFC 1951) and gzip (RFC 1952) encoder.
3
+ *
4
+ * `rust/src/deflate.rs` implements the same algorithm and must produce the same
5
+ * bytes for every input, so that archives and OCI layers (and therefore image
6
+ * IDs) are identical between the two implementations. Any change here must be
7
+ * made in both files, and the differential fixtures regenerated
8
+ * (`node rust/tests/fixtures/deflate/generate.mjs`). Decompression is not
9
+ * affected: any inflater reads this output.
10
+ *
11
+ * Specification (every choice below is part of the output format):
12
+ *
13
+ * LZ77 matching
14
+ * - Window 32768 bytes; matches are 3..258 bytes at distance 1..32768.
15
+ * - Hash of position p (only when p + 3 <= n): x = d[p] << 16 | d[p+1] << 8 | d[p+2];
16
+ * h = (x * 0x9E3779B1 mod 2^32) >> 17 (15 bits). `head[h]` holds the most
17
+ * recent inserted position with that hash, `prev[p & 32767]` the position
18
+ * that was in `head[h]` when p was inserted (-1 = none).
19
+ * - findMatch(p): if p + 3 > n, no match. Otherwise walk the chain starting
20
+ * at head[h(p)]: candidate c is considered while c >= 0, p - c <= 32768 and
21
+ * fewer than MAX_CHAIN (64) candidates have been considered; the next
22
+ * candidate is prev[c & 32767], and the walk stops if that is not < c.
23
+ * For each candidate, len = length of the common prefix of d[c..] and
24
+ * d[p..], at most min(258, n - p). A candidate replaces the best only if
25
+ * len > bestLen (so on ties the nearest wins); the walk stops once
26
+ * bestLen >= 258 (or reaches min(258, n - p)). Then p is inserted.
27
+ * A best match of length 3 at distance > 4096 is dropped (no match).
28
+ * - Every position p with p + 3 <= n is inserted exactly once, in increasing
29
+ * order, before findMatch is called for any later position.
30
+ * - Parsing (lazy, one position at a time): at p, m = findMatch(p). If
31
+ * m.len < 3: literal d[p], p += 1. Otherwise, while m.len < LAZY_LIMIT (16)
32
+ * and p + 1 < n: m2 = findMatch(p + 1), searching at most MAX_CHAIN / 4 (16)
33
+ * candidates when m.len >= GOOD_LENGTH (8); if m2.len > m.len, emit literal
34
+ * d[p], p += 1, m = m2 and repeat; else stop. Then emit match m at p, insert
35
+ * the positions up to p + m.len - 1 that are not yet inserted, p += m.len.
36
+ *
37
+ * Blocks
38
+ * - Symbols (literals and length/distance pairs) are buffered. At the start
39
+ * of each parsing step (p < n), if the buffer holds >= BLOCK_SYMBOLS (16384)
40
+ * symbols it is written as a non-final block. When the input is exhausted
41
+ * the remaining symbols (none only for empty input) are written as the
42
+ * final block (BFINAL = 1).
43
+ * - Each block is written as whichever of stored, fixed Huffman or dynamic
44
+ * Huffman takes the fewest bits, given the current bit position; on a tie
45
+ * the earlier of stored, fixed, dynamic wins. Stored is a candidate only
46
+ * when the block covers <= 65535 input bytes; its cost is 3 header bits,
47
+ * padding to a byte boundary, 32 bits of LEN/NLEN and 8 bits per byte.
48
+ * - Huffman code lengths (literal/length tree max 15 bits, distance tree max
49
+ * 15, code-length tree max 7) come from package-merge: leaves are the
50
+ * symbols with nonzero frequency sorted by (frequency, symbol); L - 1 times,
51
+ * pair consecutive items of the previous list (dropping an odd last item)
52
+ * into packages and merge them with the leaves by weight, a leaf before a
53
+ * package on equal weight; a symbol's length is the number of times it
54
+ * occurs in the first 2k - 2 items of the final list (k = leaf count). If
55
+ * fewer than two symbols are used, the used one (if any) and the lowest
56
+ * unused symbols are added until there are two, each with length 1.
57
+ * Codes are canonical (RFC 1951 3.2.2).
58
+ * - Dynamic header: HLIT = max(257, last used literal/length symbol + 1),
59
+ * HDIST = max(1, last used distance symbol + 1) (an unused tree still has
60
+ * two length-1 codes, so this is at least 2); HCLEN = max(4, last nonzero
61
+ * entry in the order 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 + 1).
62
+ * The HLIT + HDIST lengths form one sequence, run-length coded left to
63
+ * right: for value v with run r of equal values starting here, if v = 0:
64
+ * r >= 11 → 18 (min(r, 138)), r >= 3 → 17 (r), else a single 0; if v != 0:
65
+ * emit v once, then while the rest of the run is >= 3, 16 (min(rest, 6)).
66
+ *
67
+ * gzip: header 1f 8b 08, FLG 0, MTIME 0, XFL 0, OS 3 (Unix), then the raw
68
+ * deflate stream, CRC-32 and ISIZE (little-endian).
69
+ */
70
+ const WSIZE = 32768;
71
+ const WMASK = WSIZE - 1;
72
+ const HASH_BITS = 15;
73
+ const MAX_MATCH = 258;
74
+ const MIN_MATCH = 3;
75
+ const MAX_CHAIN = 64;
76
+ const LAZY_LIMIT = 16;
77
+ const GOOD_LENGTH = 8;
78
+ const TOO_FAR = 4096;
79
+ const BLOCK_SYMBOLS = 16384;
80
+ const STORED_MAX = 65535;
81
+ const LENGTH_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258];
82
+ const LENGTH_EXTRA = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];
83
+ const DIST_BASE = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577];
84
+ const DIST_EXTRA = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
85
+ const CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
86
+ /** match length (3..258) → length code index 0..28 */
87
+ const LEN_CODE = new Uint8Array(MAX_MATCH + 1);
88
+ /** distance - 1 → distance code, for distances 1..32768 */
89
+ const DIST_CODE = new Uint8Array(WSIZE);
90
+ for (let c = 0; c < 29; c++) {
91
+ const end = c === 28 ? 259 : LENGTH_BASE[c + 1];
92
+ for (let l = LENGTH_BASE[c]; l < end; l++)
93
+ LEN_CODE[l] = c;
94
+ }
95
+ LEN_CODE[258] = 28;
96
+ for (let c = 0; c < 30; c++) {
97
+ const end = c === 29 ? WSIZE + 1 : DIST_BASE[c + 1];
98
+ for (let d = DIST_BASE[c]; d < end; d++)
99
+ DIST_CODE[d - 1] = c;
100
+ }
101
+ const FIXED_LIT_LEN = new Uint8Array(288);
102
+ for (let i = 0; i < 288; i++)
103
+ FIXED_LIT_LEN[i] = i < 144 ? 8 : i < 256 ? 9 : i < 280 ? 7 : 8;
104
+ const FIXED_DIST_LEN = new Uint8Array(30).fill(5);
105
+ const FIXED_LIT_CODE = canonicalCodes(FIXED_LIT_LEN);
106
+ const FIXED_DIST_CODE = canonicalCodes(FIXED_DIST_LEN);
107
+ /** Package-merge length-limited Huffman code lengths. */
108
+ export function codeLengths(freq, maxBits) {
109
+ const n = freq.length;
110
+ const lens = new Uint8Array(n);
111
+ const used = [];
112
+ for (let i = 0; i < n; i++)
113
+ if (freq[i] > 0)
114
+ used.push(i);
115
+ if (used.length < 2) {
116
+ const set = new Set(used);
117
+ for (let i = 0; set.size < 2 && i < n; i++)
118
+ set.add(i);
119
+ for (const s of set)
120
+ lens[s] = 1;
121
+ return lens;
122
+ }
123
+ used.sort((a, b) => freq[a] - freq[b] || a - b);
124
+ const k = used.length;
125
+ const leafW = used.map((s) => freq[s]);
126
+ // Each level: weights, and for every item either a leaf index (>= 0) or a package (-1 - index of its first child).
127
+ const levels = [];
128
+ levels.push({ w: leafW.slice(), item: used.map((_, i) => i) });
129
+ for (let l = 1; l < maxBits; l++) {
130
+ const prev = levels[l - 1];
131
+ const pw = [];
132
+ for (let i = 0; i + 1 < prev.w.length; i += 2)
133
+ pw.push(prev.w[i] + prev.w[i + 1]);
134
+ const w = [];
135
+ const item = [];
136
+ let a = 0;
137
+ let b = 0;
138
+ while (a < k || b < pw.length) {
139
+ if (b >= pw.length || (a < k && leafW[a] <= pw[b])) {
140
+ w.push(leafW[a]);
141
+ item.push(a++);
142
+ }
143
+ else {
144
+ w.push(pw[b]);
145
+ item.push(-1 - 2 * b++);
146
+ }
147
+ }
148
+ levels.push({ w, item });
149
+ }
150
+ // Select the first 2k-2 items of the last level and walk down.
151
+ let take = 2 * k - 2;
152
+ for (let l = maxBits - 1; l >= 0; l--) {
153
+ const { item } = levels[l];
154
+ let next = 0;
155
+ for (let i = 0; i < take; i++) {
156
+ const it = item[i];
157
+ if (it >= 0)
158
+ lens[used[it]]++;
159
+ else
160
+ next = Math.max(next, -1 - it + 2);
161
+ }
162
+ take = next; // packages are a prefix of the previous level's pairs
163
+ }
164
+ return lens;
165
+ }
166
+ /** Canonical codes, bit-reversed for LSB-first output. */
167
+ function canonicalCodes(lens) {
168
+ const count = new Uint16Array(16);
169
+ for (const l of lens)
170
+ count[l]++;
171
+ count[0] = 0;
172
+ const next = new Uint16Array(16);
173
+ let code = 0;
174
+ for (let b = 1; b < 16; b++) {
175
+ code = (code + count[b - 1]) << 1;
176
+ next[b] = code;
177
+ }
178
+ const codes = new Uint16Array(lens.length);
179
+ for (let i = 0; i < lens.length; i++) {
180
+ const l = lens[i];
181
+ if (l === 0)
182
+ continue;
183
+ let c = next[l]++;
184
+ let r = 0;
185
+ for (let j = 0; j < l; j++) {
186
+ r = (r << 1) | (c & 1);
187
+ c >>= 1;
188
+ }
189
+ codes[i] = r;
190
+ }
191
+ return codes;
192
+ }
193
+ class BitWriter {
194
+ buf;
195
+ pos = 0;
196
+ bits = 0;
197
+ nbits = 0;
198
+ constructor(size) {
199
+ this.buf = new Uint8Array(Math.max(64, size));
200
+ }
201
+ ensure(extra) {
202
+ if (this.pos + extra <= this.buf.length)
203
+ return;
204
+ const b = new Uint8Array(Math.max(this.buf.length * 2, this.pos + extra));
205
+ b.set(this.buf.subarray(0, this.pos));
206
+ this.buf = b;
207
+ }
208
+ /** Write up to 16 bits; the caller ensures space (4 bytes per call is plenty). */
209
+ put(value, n) {
210
+ this.bits |= value << this.nbits;
211
+ this.nbits += n;
212
+ while (this.nbits >= 8) {
213
+ this.buf[this.pos++] = this.bits & 0xff;
214
+ this.bits >>>= 8;
215
+ this.nbits -= 8;
216
+ }
217
+ }
218
+ alignByte() {
219
+ if (this.nbits > 0) {
220
+ this.buf[this.pos++] = this.bits & 0xff;
221
+ this.bits = 0;
222
+ this.nbits = 0;
223
+ }
224
+ }
225
+ /** Total bits written so far. */
226
+ get bitPos() {
227
+ return this.pos * 8 + this.nbits;
228
+ }
229
+ }
230
+ function dynamicHeader(litFreq, distFreq) {
231
+ const litLens = codeLengths(litFreq, 15);
232
+ const distLens = codeLengths(distFreq, 15);
233
+ let hlit = 286;
234
+ while (hlit > 257 && litLens[hlit - 1] === 0)
235
+ hlit--;
236
+ let hdist = 30;
237
+ while (hdist > 1 && distLens[hdist - 1] === 0)
238
+ hdist--;
239
+ const seq = new Uint8Array(hlit + hdist);
240
+ seq.set(litLens.subarray(0, hlit), 0);
241
+ seq.set(distLens.subarray(0, hdist), hlit);
242
+ const rle = [];
243
+ const clFreq = new Uint32Array(19);
244
+ for (let i = 0; i < seq.length;) {
245
+ const v = seq[i];
246
+ let r = 1;
247
+ while (i + r < seq.length && seq[i + r] === v)
248
+ r++;
249
+ if (v === 0) {
250
+ if (r >= 11) {
251
+ const m = Math.min(r, 138);
252
+ rle.push(18, m - 11);
253
+ clFreq[18]++;
254
+ i += m;
255
+ }
256
+ else if (r >= 3) {
257
+ rle.push(17, r - 3);
258
+ clFreq[17]++;
259
+ i += r;
260
+ }
261
+ else {
262
+ rle.push(0);
263
+ clFreq[0]++;
264
+ i += 1;
265
+ }
266
+ }
267
+ else {
268
+ rle.push(v);
269
+ clFreq[v]++;
270
+ i += 1;
271
+ let rest = r - 1;
272
+ while (rest >= 3) {
273
+ const m = Math.min(rest, 6);
274
+ rle.push(16, m - 3);
275
+ clFreq[16]++;
276
+ i += m;
277
+ rest -= m;
278
+ }
279
+ }
280
+ }
281
+ const clLens = codeLengths(clFreq, 7);
282
+ let hclen = 19;
283
+ while (hclen > 4 && clLens[CL_ORDER[hclen - 1]] === 0)
284
+ hclen--;
285
+ let bits = 5 + 5 + 4 + 3 * hclen;
286
+ for (let s = 0; s < 19; s++)
287
+ bits += clFreq[s] * clLens[s];
288
+ bits += clFreq[16] * 2 + clFreq[17] * 3 + clFreq[18] * 7;
289
+ return { litLens, distLens, hlit, hdist, hclen, clLens, rle, bits };
290
+ }
291
+ class Encoder {
292
+ d;
293
+ n;
294
+ head;
295
+ /** prev[q & WMASK] is only read for positions q inserted in this run, so it needs no reset. */
296
+ prev;
297
+ /** next position to insert */
298
+ ins = 0;
299
+ // Symbol buffer: sDist 0 = literal (sLen = byte), else match length / distance.
300
+ sLen;
301
+ sDist;
302
+ count = 0;
303
+ blockStart = 0;
304
+ litFreq = new Uint32Array(286);
305
+ distFreq = new Uint32Array(30);
306
+ out;
307
+ matchLen = 0;
308
+ matchDist = 0;
309
+ constructor(d, t) {
310
+ this.d = d;
311
+ this.n = d.length;
312
+ this.head = t.head.fill(-1);
313
+ this.prev = t.prev;
314
+ this.sLen = t.sLen;
315
+ this.sDist = t.sDist;
316
+ this.out = new BitWriter((d.length >>> 1) + 1024);
317
+ }
318
+ insertUpTo(end) {
319
+ const d = this.d;
320
+ const last = Math.min(end, this.n - 2);
321
+ const head = this.head;
322
+ const prev = this.prev;
323
+ for (let p = this.ins; p < last; p++) {
324
+ const h = Math.imul((d[p] << 16) | (d[p + 1] << 8) | d[p + 2], 0x9e3779b1) >>> 17;
325
+ prev[p & WMASK] = head[h];
326
+ head[h] = p;
327
+ }
328
+ if (end > this.ins)
329
+ this.ins = end;
330
+ }
331
+ /**
332
+ * Sets matchLen/matchDist for position p (all earlier positions inserted), then inserts p.
333
+ * Only matches longer than `atLeast` are reported (the lazy step needs nothing shorter);
334
+ * this gives the same result as a full search followed by that comparison.
335
+ */
336
+ findMatch(p, atLeast, maxChain) {
337
+ const d = this.d;
338
+ const n = this.n;
339
+ this.matchLen = 0;
340
+ this.matchDist = 0;
341
+ if (p + MIN_MATCH > n)
342
+ return;
343
+ const h = Math.imul((d[p] << 16) | (d[p + 1] << 8) | d[p + 2], 0x9e3779b1) >>> 17;
344
+ const head = this.head;
345
+ const prev = this.prev;
346
+ const maxLen = Math.min(MAX_MATCH, n - p);
347
+ if (atLeast >= maxLen) {
348
+ this.insertUpTo(p + 1);
349
+ return;
350
+ }
351
+ let c = head[h];
352
+ let chain = maxChain;
353
+ let best = atLeast;
354
+ let bestDist = 0;
355
+ const limit = p > WSIZE ? p - WSIZE : 0; // -1 (empty) is below it too
356
+ let scanEnd = d[p + best];
357
+ while (c >= limit && chain-- > 0) {
358
+ // A candidate can only be longer than `best` if it matches at offset `best`.
359
+ if (d[c + best] === scanEnd) {
360
+ let len = 0;
361
+ while (len < maxLen && d[c + len] === d[p + len])
362
+ len++;
363
+ if (len > best) {
364
+ best = len;
365
+ bestDist = p - c;
366
+ if (len >= maxLen)
367
+ break;
368
+ scanEnd = d[p + best];
369
+ }
370
+ }
371
+ const nx = prev[c & WMASK];
372
+ if (nx >= c)
373
+ break;
374
+ c = nx;
375
+ }
376
+ prev[p & WMASK] = head[h];
377
+ head[h] = p;
378
+ this.ins = p + 1;
379
+ if (best === MIN_MATCH && bestDist > TOO_FAR)
380
+ best = 0;
381
+ if (best >= MIN_MATCH && bestDist > 0) {
382
+ this.matchLen = best;
383
+ this.matchDist = bestDist;
384
+ }
385
+ }
386
+ literal(p) {
387
+ const b = this.d[p];
388
+ this.sLen[this.count] = b;
389
+ this.sDist[this.count++] = 0;
390
+ this.litFreq[b]++;
391
+ }
392
+ match(len, dist) {
393
+ this.sLen[this.count] = len;
394
+ this.sDist[this.count++] = dist;
395
+ this.litFreq[257 + LEN_CODE[len]]++;
396
+ this.distFreq[DIST_CODE[dist - 1]]++;
397
+ }
398
+ run() {
399
+ const n = this.n;
400
+ let p = 0;
401
+ while (p < n) {
402
+ if (this.count >= BLOCK_SYMBOLS)
403
+ this.flush(p, false);
404
+ this.findMatch(p, 0, MAX_CHAIN);
405
+ let len = this.matchLen;
406
+ let dist = this.matchDist;
407
+ if (len < MIN_MATCH) {
408
+ this.literal(p);
409
+ p++;
410
+ continue;
411
+ }
412
+ while (len < LAZY_LIMIT && p + 1 < n) {
413
+ this.findMatch(p + 1, len, len >= GOOD_LENGTH ? MAX_CHAIN >> 2 : MAX_CHAIN);
414
+ if (this.matchLen > len) {
415
+ this.literal(p);
416
+ p++;
417
+ len = this.matchLen;
418
+ dist = this.matchDist;
419
+ }
420
+ else
421
+ break;
422
+ }
423
+ this.match(len, dist);
424
+ this.insertUpTo(p + len);
425
+ p += len;
426
+ }
427
+ this.flush(n, true);
428
+ this.out.alignByte();
429
+ return this.out.buf.subarray(0, this.out.pos);
430
+ }
431
+ /** Write the buffered symbols, which cover input [blockStart, end). */
432
+ flush(end, final) {
433
+ const out = this.out;
434
+ const span = end - this.blockStart;
435
+ this.litFreq[256] = 1;
436
+ const lf = this.litFreq;
437
+ const df = this.distFreq;
438
+ // Data bits common to every Huffman variant: extra bits.
439
+ let extra = 0;
440
+ for (let c = 0; c < 29; c++)
441
+ extra += lf[257 + c] * LENGTH_EXTRA[c];
442
+ for (let c = 0; c < 30; c++)
443
+ extra += df[c] * DIST_EXTRA[c];
444
+ let fixedBits = 3 + extra;
445
+ for (let s = 0; s < 286; s++)
446
+ fixedBits += lf[s] * FIXED_LIT_LEN[s];
447
+ for (let s = 0; s < 30; s++)
448
+ fixedBits += df[s] * 5;
449
+ const dyn = dynamicHeader(lf, df);
450
+ let dynBits = 3 + dyn.bits + extra;
451
+ for (let s = 0; s < 286; s++)
452
+ dynBits += lf[s] * dyn.litLens[s];
453
+ for (let s = 0; s < 30; s++)
454
+ dynBits += df[s] * dyn.distLens[s];
455
+ let storedBits = Infinity;
456
+ if (span <= STORED_MAX)
457
+ storedBits = 3 + ((8 - ((out.bitPos + 3) % 8)) % 8) + 32 + 8 * span;
458
+ out.ensure(Math.ceil(Math.min(storedBits, fixedBits, dynBits) / 8) + 16);
459
+ const bfinal = final ? 1 : 0;
460
+ if (storedBits <= fixedBits && storedBits <= dynBits) {
461
+ out.put(bfinal, 3); // BTYPE 00
462
+ out.alignByte();
463
+ out.put(span & 0xff, 8);
464
+ out.put(span >>> 8, 8);
465
+ out.put(~span & 0xff, 8);
466
+ out.put((~span >>> 8) & 0xff, 8);
467
+ out.buf.set(this.d.subarray(this.blockStart, end), out.pos);
468
+ out.pos += span;
469
+ }
470
+ else if (fixedBits <= dynBits) {
471
+ out.put(bfinal | (1 << 1), 3);
472
+ this.writeSymbols(FIXED_LIT_CODE, FIXED_LIT_LEN, FIXED_DIST_CODE, FIXED_DIST_LEN);
473
+ }
474
+ else {
475
+ out.put(bfinal | (2 << 1), 3);
476
+ out.put(dyn.hlit - 257, 5);
477
+ out.put(dyn.hdist - 1, 5);
478
+ out.put(dyn.hclen - 4, 4);
479
+ for (let i = 0; i < dyn.hclen; i++)
480
+ out.put(dyn.clLens[CL_ORDER[i]], 3);
481
+ const clCodes = canonicalCodes(dyn.clLens);
482
+ const rle = dyn.rle;
483
+ for (let i = 0; i < rle.length; i++) {
484
+ const s = rle[i];
485
+ out.put(clCodes[s], dyn.clLens[s]);
486
+ if (s === 16)
487
+ out.put(rle[++i], 2);
488
+ else if (s === 17)
489
+ out.put(rle[++i], 3);
490
+ else if (s === 18)
491
+ out.put(rle[++i], 7);
492
+ }
493
+ this.writeSymbols(canonicalCodes(dyn.litLens), dyn.litLens, canonicalCodes(dyn.distLens), dyn.distLens);
494
+ }
495
+ this.count = 0;
496
+ this.blockStart = end;
497
+ lf.fill(0);
498
+ df.fill(0);
499
+ }
500
+ writeSymbols(litCode, litLen, distCode, distLen) {
501
+ const out = this.out;
502
+ const sLen = this.sLen;
503
+ const sDist = this.sDist;
504
+ // Inlined bit writer for speed.
505
+ const buf = out.buf;
506
+ let pos = out.pos;
507
+ let bits = out.bits;
508
+ let nbits = out.nbits;
509
+ for (let i = 0; i < this.count; i++) {
510
+ const dist = sDist[i];
511
+ if (dist === 0) {
512
+ const b = sLen[i];
513
+ bits |= litCode[b] << nbits;
514
+ nbits += litLen[b];
515
+ }
516
+ else {
517
+ const len = sLen[i];
518
+ const lc = LEN_CODE[len];
519
+ bits |= litCode[257 + lc] << nbits;
520
+ nbits += litLen[257 + lc];
521
+ const le = LENGTH_EXTRA[lc];
522
+ if (le) {
523
+ bits |= (len - LENGTH_BASE[lc]) << nbits;
524
+ nbits += le;
525
+ }
526
+ while (nbits >= 8) {
527
+ buf[pos++] = bits & 0xff;
528
+ bits >>>= 8;
529
+ nbits -= 8;
530
+ }
531
+ const dc = DIST_CODE[dist - 1];
532
+ bits |= distCode[dc] << nbits;
533
+ nbits += distLen[dc];
534
+ const de = DIST_EXTRA[dc];
535
+ if (de) {
536
+ while (nbits >= 8) {
537
+ buf[pos++] = bits & 0xff;
538
+ bits >>>= 8;
539
+ nbits -= 8;
540
+ }
541
+ bits |= (dist - DIST_BASE[dc]) << nbits;
542
+ nbits += de;
543
+ }
544
+ }
545
+ while (nbits >= 8) {
546
+ buf[pos++] = bits & 0xff;
547
+ bits >>>= 8;
548
+ nbits -= 8;
549
+ }
550
+ }
551
+ out.pos = pos;
552
+ out.bits = bits;
553
+ out.nbits = nbits;
554
+ out.put(litCode[256], litLen[256]);
555
+ }
556
+ }
557
+ /** Reused between calls (encoding is synchronous), so small zip entries do not each allocate ~200 kB. */
558
+ let spare;
559
+ /** Raw DEFLATE stream (zip method 8). */
560
+ export function deflateRaw(data) {
561
+ const t = spare ?? {
562
+ head: new Int32Array(1 << HASH_BITS),
563
+ prev: new Int32Array(WSIZE),
564
+ sLen: new Uint16Array(BLOCK_SYMBOLS + MAX_MATCH + 8),
565
+ sDist: new Uint16Array(BLOCK_SYMBOLS + MAX_MATCH + 8),
566
+ };
567
+ spare = undefined;
568
+ try {
569
+ const out = new Encoder(data, t).run();
570
+ // The output buffer was sized for the worst case; copy small results so they do not pin it.
571
+ return out.length < out.buffer.byteLength >>> 1 ? out.slice() : out;
572
+ }
573
+ finally {
574
+ spare = t;
575
+ }
576
+ }
577
+ const CRC_TABLE = (() => {
578
+ const t = new Uint32Array(256);
579
+ for (let n = 0; n < 256; n++) {
580
+ let c = n;
581
+ for (let k = 0; k < 8; k++)
582
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
583
+ t[n] = c >>> 0;
584
+ }
585
+ return t;
586
+ })();
587
+ export function crc32(data) {
588
+ let c = 0xffffffff;
589
+ for (let i = 0; i < data.length; i++)
590
+ c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
591
+ return (c ^ 0xffffffff) >>> 0;
592
+ }
593
+ /** gzip member with a fixed header (no mtime, no name, XFL 0, OS 3). */
594
+ export function gzip(data) {
595
+ const body = deflateRaw(data);
596
+ const out = new Uint8Array(10 + body.length + 8);
597
+ out.set([0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 3], 0);
598
+ out.set(body, 10);
599
+ const v = new DataView(out.buffer, out.byteOffset, out.byteLength);
600
+ v.setUint32(10 + body.length, crc32(data), true);
601
+ v.setUint32(14 + body.length, data.length >>> 0, true);
602
+ return out;
603
+ }
604
+ //# sourceMappingURL=deflate.js.map
@@ -0,0 +1,3 @@
1
+ /** `sha256:<hex>` digest of bytes or a string. */
2
+ export declare function sha256(data: Uint8Array | string): string;
3
+ export declare const INTEGRITY_RE: RegExp;
@@ -0,0 +1,7 @@
1
+ import { createHash } from 'node:crypto';
2
+ /** `sha256:<hex>` digest of bytes or a string. */
3
+ export function sha256(data) {
4
+ return 'sha256:' + createHash('sha256').update(data).digest('hex');
5
+ }
6
+ export const INTEGRITY_RE = /^sha256:[0-9a-f]{64}$/;
7
+ //# sourceMappingURL=digest.js.map