@tide-ota/cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 HashimTheArab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,37 @@
1
- # Temporary Holding Version
1
+ # @tide-ota/cli
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Publish signed Expo OTA updates to your own R2 or S3 bucket.**
4
+
5
+ The `tide` command for [Tide](https://github.com/HashimTheArab/tide), a self-hosted Expo Updates server: build and sign updates, roll back, and keep
6
+ downloads small with bundle diffing.
7
+
8
+ ```sh
9
+ npm install --save-dev @tide-ota/cli
10
+ ```
11
+
12
+ ## Publish
13
+
14
+ Two steps, so the one that holds your keys never runs your app's code:
15
+
16
+ ```sh
17
+ npx tide export --project-root <app> --out <dir> # builds the app; needs no secrets
18
+ npx tide publish --from <dir> --channel main # signs and uploads; runs no app code
19
+ ```
20
+
21
+ `tide publish` signs with a key from `TIDE_SIGNING_KEY_FILE`, `TIDE_SIGNING_KEY` or AWS KMS (`TIDE_KMS_*`), checks
22
+ every signature against your app's certificate, and writes to the bucket named by `TIDE_S3_*` or `TIDE_DISK_DIR`.
23
+
24
+ ## Other commands
25
+
26
+ | Command | What it does |
27
+ | ------------------------------- | ----------------------------------------------------------- |
28
+ | `tide register-build` | Lets fresh installs get a patch instead of the whole bundle |
29
+ | `tide rollback --embedded` | Sends apps back to the bundle inside the store build |
30
+ | `tide rollback --to <updateId>` | Serves an earlier update again |
31
+ | `tide republish <updateId>...` | Serves stored updates again, optionally on another channel |
32
+ | `tide status` | Lists what each channel serves right now |
33
+ | `tide storage` | Lists files nothing uses any more; deletes nothing |
34
+ | `tide probe` | Checks a live server the way an installed app would |
35
+
36
+ The server is [`@tide-ota/server`](https://www.npmjs.com/package/@tide-ota/server). CI setup, keys and every flag:
37
+ [publishing](https://github.com/HashimTheArab/tide/blob/main/docs/deploy.md#publishing) and [the CLI reference](https://github.com/HashimTheArab/tide/blob/main/docs/design.md#cli).
@@ -0,0 +1,17 @@
1
+ /** The largest base or target `makeBsdiff` takes, which keeps every suffix index far inside an Int32Array. */
2
+ export declare const MAX_BSDIFF_INPUT: number;
3
+ /**
4
+ * Returns a BSDIFF40 patch from `base` to `target`, which depends only on the input bytes and is not checked
5
+ * here. Throws a RangeError when either is over `MAX_BSDIFF_INPUT`.
6
+ */
7
+ export declare function makeBsdiff(base: Uint8Array, target: Uint8Array): Uint8Array<ArrayBuffer>;
8
+ /**
9
+ * Returns bsdiff 4.3's uncompressed ctrl, diff and extra blocks; `exhausted` is set when matching hit its work budget
10
+ * and wrote the rest of the target as extra bytes.
11
+ */
12
+ export declare function diffBlocks(base: Uint8Array, target: Uint8Array): {
13
+ ctrl: Uint8Array<ArrayBuffer>;
14
+ diff: Uint8Array<ArrayBuffer>;
15
+ extra: Uint8Array<ArrayBuffer>;
16
+ exhausted: boolean;
17
+ };
package/dist/bsdiff.js ADDED
@@ -0,0 +1,204 @@
1
+ /*-
2
+ * Copyright 2003-2005 Colin Percival
3
+ * All rights reserved
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted providing that the following conditions
7
+ * are met:
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ * POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+ // The matching below is a port of bsdiff 4.3's search and scan loop, with a work budget added.
27
+ import { bzip2, sortRotations } from "./bzip2.js";
28
+ /** The largest base or target `makeBsdiff` takes, which keeps every suffix index far inside an Int32Array. */
29
+ export const MAX_BSDIFF_INPUT = 64 * 1024 * 1024;
30
+ // Matching may compare this many bytes per input byte, plus a floor, before it writes the rest as extra bytes.
31
+ const WORK_PER_BYTE = 32;
32
+ const WORK_FLOOR = 2 ** 24;
33
+ /**
34
+ * Returns a BSDIFF40 patch from `base` to `target`, which depends only on the input bytes and is not checked
35
+ * here. Throws a RangeError when either is over `MAX_BSDIFF_INPUT`.
36
+ */
37
+ export function makeBsdiff(base, target) {
38
+ const blocks = diffBlocks(base, target);
39
+ const ctrl = bzip2(blocks.ctrl);
40
+ const diff = bzip2(blocks.diff);
41
+ const extra = bzip2(blocks.extra);
42
+ const patch = new Uint8Array(32 + ctrl.length + diff.length + extra.length);
43
+ patch.set(new TextEncoder().encode("BSDIFF40"));
44
+ offtout(ctrl.length, patch, 8);
45
+ offtout(diff.length, patch, 16);
46
+ offtout(target.length, patch, 24);
47
+ patch.set(ctrl, 32);
48
+ patch.set(diff, 32 + ctrl.length);
49
+ patch.set(extra, 32 + ctrl.length + diff.length);
50
+ return patch;
51
+ }
52
+ /**
53
+ * Returns bsdiff 4.3's uncompressed ctrl, diff and extra blocks; `exhausted` is set when matching hit its work budget
54
+ * and wrote the rest of the target as extra bytes.
55
+ */
56
+ export function diffBlocks(base, target) {
57
+ if (base.length > MAX_BSDIFF_INPUT || target.length > MAX_BSDIFF_INPUT) {
58
+ throw new RangeError(`makeBsdiff takes inputs of at most ${MAX_BSDIFF_INPUT} bytes`);
59
+ }
60
+ const oldsize = base.length;
61
+ const newsize = target.length;
62
+ const I = sortRotations(base, true);
63
+ // Every byte `search` compares counts; once the budget is spent, matching stops.
64
+ const budget = WORK_PER_BYTE * (oldsize + newsize) + WORK_FLOOR;
65
+ let work = 0;
66
+ const spent = () => work >= budget;
67
+ /** Returns how many bytes `base[o..]` and `target[t..]` share, comparing at most `max`. */
68
+ const common = (o, t, max) => {
69
+ const limit = Math.min(max, budget - work);
70
+ let i = 0;
71
+ while (i < limit && base[o + i] === target[t + i])
72
+ i++;
73
+ work += i < limit ? i + 1 : limit;
74
+ return i;
75
+ };
76
+ const matchlen = (o, t) => common(o, t, Math.min(oldsize - o, newsize - t));
77
+ let pos = 0;
78
+ /** Returns the longest match for `target[scan..]` and sets `pos` to its start in `base`. */
79
+ const search = (scan) => {
80
+ let st = 0;
81
+ let en = oldsize;
82
+ while (en - st >= 2 && !spent()) {
83
+ const x = st + ((en - st) >>> 1);
84
+ const o = I[x] ?? 0;
85
+ const max = Math.min(oldsize - o, newsize - scan);
86
+ const i = common(o, scan, max);
87
+ if (i < max && (base[o + i] ?? 0) < (target[scan + i] ?? 0))
88
+ st = x;
89
+ else
90
+ en = x;
91
+ }
92
+ const s = I[st] ?? 0;
93
+ const e = I[en] ?? 0;
94
+ const x = matchlen(s, scan);
95
+ const y = matchlen(e, scan);
96
+ pos = x > y ? s : e;
97
+ return x > y ? x : y;
98
+ };
99
+ const ctrl = []; // Triples of add length, copy length and seek.
100
+ const db = new Uint8Array(newsize);
101
+ const eb = new Uint8Array(newsize);
102
+ let dblen = 0;
103
+ let eblen = 0;
104
+ let scan = 0;
105
+ let len = 0;
106
+ let lastscan = 0;
107
+ let lastpos = 0;
108
+ let lastoffset = 0;
109
+ while (scan < newsize) {
110
+ let oldscore = 0;
111
+ for (let scsc = (scan += len); scan < newsize; scan++) {
112
+ len = search(scan);
113
+ if (spent()) {
114
+ scan = newsize;
115
+ break;
116
+ }
117
+ for (; scsc < scan + len; scsc++)
118
+ if (base[scsc + lastoffset] === target[scsc])
119
+ oldscore++;
120
+ if ((len === oldscore && len !== 0) || len > oldscore + 8)
121
+ break;
122
+ if (base[scan + lastoffset] === target[scan])
123
+ oldscore--;
124
+ }
125
+ if (len !== oldscore || scan === newsize) {
126
+ // Out of budget, nothing is extended: the rest of the target is extra bytes and the seek is zero.
127
+ const out = spent();
128
+ let s = 0;
129
+ let best = 0;
130
+ let lenf = 0;
131
+ for (let i = 0; !out && lastscan + i < scan && lastpos + i < oldsize;) {
132
+ if (base[lastpos + i] === target[lastscan + i])
133
+ s++;
134
+ i++;
135
+ if (s * 2 - i > best * 2 - lenf) {
136
+ best = s;
137
+ lenf = i;
138
+ }
139
+ }
140
+ let lenb = 0;
141
+ if (scan < newsize) {
142
+ s = 0;
143
+ best = 0;
144
+ for (let i = 1; scan >= lastscan + i && pos >= i; i++) {
145
+ if (base[pos - i] === target[scan - i])
146
+ s++;
147
+ if (s * 2 - i > best * 2 - lenb) {
148
+ best = s;
149
+ lenb = i;
150
+ }
151
+ }
152
+ }
153
+ if (lastscan + lenf > scan - lenb) {
154
+ const overlap = lastscan + lenf - (scan - lenb);
155
+ s = 0;
156
+ best = 0;
157
+ let lens = 0;
158
+ for (let i = 0; i < overlap; i++) {
159
+ if (target[lastscan + lenf - overlap + i] === base[lastpos + lenf - overlap + i])
160
+ s++;
161
+ if (target[scan - lenb + i] === base[pos - lenb + i])
162
+ s--;
163
+ if (s > best) {
164
+ best = s;
165
+ lens = i + 1;
166
+ }
167
+ }
168
+ lenf += lens - overlap;
169
+ lenb -= lens;
170
+ }
171
+ if (out)
172
+ pos = lastpos;
173
+ for (let i = 0; i < lenf; i++) {
174
+ db[dblen + i] = (target[lastscan + i] ?? 0) - (base[lastpos + i] ?? 0);
175
+ }
176
+ const copy = scan - lenb - (lastscan + lenf);
177
+ eb.set(target.subarray(lastscan + lenf, lastscan + lenf + copy), eblen);
178
+ dblen += lenf;
179
+ eblen += copy;
180
+ ctrl.push(lenf, copy, pos - lenb - (lastpos + lenf));
181
+ lastscan = scan - lenb;
182
+ lastpos = pos - lenb;
183
+ lastoffset = pos - scan;
184
+ }
185
+ }
186
+ const ctrlBytes = new Uint8Array(8 * ctrl.length);
187
+ ctrl.forEach((x, i) => offtout(x, ctrlBytes, 8 * i));
188
+ return {
189
+ ctrl: ctrlBytes,
190
+ diff: db.slice(0, dblen),
191
+ extra: eb.slice(0, eblen),
192
+ exhausted: spent(),
193
+ };
194
+ }
195
+ /** Writes `x` at `at` as 8 bytes little-endian sign-magnitude. */
196
+ function offtout(x, into, at) {
197
+ let y = Math.abs(x);
198
+ for (let i = 0; i < 8; i++) {
199
+ into[at + i] = y % 256;
200
+ y = Math.floor(y / 256);
201
+ }
202
+ if (x < 0)
203
+ into[at + 7] = (into[at + 7] ?? 0) | 0x80;
204
+ }
@@ -0,0 +1,54 @@
1
+ interface BucketObject {
2
+ readonly bytes: Uint8Array<ArrayBuffer>;
3
+ readonly etag: string;
4
+ }
5
+ /** A listed name, a store key or a stored name outside the key grammar, and its stored bytes. */
6
+ export type ListEntry = ({
7
+ readonly key: string;
8
+ } | {
9
+ readonly unknown: string;
10
+ }) & {
11
+ readonly size: number;
12
+ };
13
+ /**
14
+ * The CLI's read-write view of the bucket. A conditional write resolves undefined only when its precondition was
15
+ * evaluated against stored state and failed; every other failure rejects with a `BucketError`.
16
+ */
17
+ export interface Bucket {
18
+ /** Undefined only when absent; an object over `maxBytes` is never buffered. */
19
+ get(key: string, maxBytes: number): Promise<BucketObject | undefined>;
20
+ /** Returns the new ETag. */
21
+ put(key: string, bytes: Uint8Array<ArrayBuffer>): Promise<string>;
22
+ /**
23
+ * Creates `key` only if nothing is stored there. A `contentType` marks immutable public content, served with it
24
+ * and cached forever; disk keeps no headers.
25
+ */
26
+ putIfAbsent(key: string, bytes: Uint8Array<ArrayBuffer>, contentType?: string): Promise<string | undefined>;
27
+ /** Replaces `key` only if its stored ETag is exactly `etag`. */
28
+ putIfMatch(key: string, bytes: Uint8Array<ArrayBuffer>, etag: string): Promise<string | undefined>;
29
+ /** Absent is success. */
30
+ delete(key: string): Promise<void>;
31
+ /** Every entry whose name starts with `prefix`, in ascending UTF-8 byte order, across all pages. */
32
+ list(prefix: string): Promise<ListEntry[]>;
33
+ /** Epoch milliseconds by the bucket's clock. */
34
+ time(): Promise<number>;
35
+ }
36
+ /**
37
+ * A bucket failure; its message never names a key or holds provider text. `unknown` means a mutation may have
38
+ * committed, or may still commit, so the caller must re-read before acting on it.
39
+ */
40
+ export declare class BucketError extends Error {
41
+ readonly outcome: "failed" | "unknown";
42
+ constructor(message: string, outcome: "failed" | "unknown");
43
+ }
44
+ /** One strong RFC 9110 entity-tag: no `W/`, no `*`, no list. */
45
+ export declare const STRONG_ETAG: RegExp;
46
+ /** True when `key` is in the store key grammar. */
47
+ export declare function isKey(key: string): boolean;
48
+ /** Rejects a key outside the store grammar before any I/O. */
49
+ export declare function checkKey(key: string): void;
50
+ /** Rejects an ETag that is not one strong entity-tag before any I/O. */
51
+ export declare function checkEtag(etag: string): void;
52
+ /** Returns -1, 0 or 1 by UTF-8 byte order, which is S3's listing order. */
53
+ export declare function byteOrder(a: string, b: string): number;
54
+ export {};
package/dist/bucket.js ADDED
@@ -0,0 +1,39 @@
1
+ import { diskPath } from "@tide-ota/protocol";
2
+ /**
3
+ * A bucket failure; its message never names a key or holds provider text. `unknown` means a mutation may have
4
+ * committed, or may still commit, so the caller must re-read before acting on it.
5
+ */
6
+ export class BucketError extends Error {
7
+ outcome;
8
+ constructor(message, outcome) {
9
+ super(message);
10
+ this.name = "BucketError";
11
+ this.outcome = outcome;
12
+ }
13
+ }
14
+ /** One strong RFC 9110 entity-tag: no `W/`, no `*`, no list. */
15
+ export const STRONG_ETAG = /^"[\x21\x23-\x2B\x2D-\x7E]{1,256}"$/;
16
+ /** True when `key` is in the store key grammar. */
17
+ export function isKey(key) {
18
+ try {
19
+ diskPath(key);
20
+ return true;
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ }
26
+ /** Rejects a key outside the store grammar before any I/O. */
27
+ export function checkKey(key) {
28
+ if (!isKey(key))
29
+ throw new BucketError("invalid store key", "failed");
30
+ }
31
+ /** Rejects an ETag that is not one strong entity-tag before any I/O. */
32
+ export function checkEtag(etag) {
33
+ if (!STRONG_ETAG.test(etag))
34
+ throw new BucketError("ETag must be one strong entity-tag", "failed");
35
+ }
36
+ /** Returns -1, 0 or 1 by UTF-8 byte order, which is S3's listing order. */
37
+ export function byteOrder(a, b) {
38
+ return Buffer.compare(Buffer.from(a), Buffer.from(b));
39
+ }
@@ -0,0 +1,9 @@
1
+ /** Returns `input` as one level-9 bzip2 stream; an empty input gives a stream with no blocks. */
2
+ export declare function bzip2(input: Uint8Array): Uint8Array<ArrayBuffer>;
3
+ /**
4
+ * Returns the start of each cyclic rotation of `bytes` in sorted order, in O(n log n) for any input. With `sentinel`,
5
+ * a symbol below every byte follows `bytes`, so rotation order is suffix order and the result has `bytes.length + 1`
6
+ * entries, the empty suffix first. Equal rotations, which only a periodic input without a sentinel has, come in an
7
+ * unspecified fixed order.
8
+ */
9
+ export declare function sortRotations(bytes: Uint8Array, sentinel: boolean): Int32Array;
package/dist/bzip2.js ADDED
@@ -0,0 +1,293 @@
1
+ import { bzip2CrcStep } from "@tide-ota/protocol";
2
+ /** libbzip2's `nblockMAX` at level 9: the most RLE1 bytes one block holds. */
3
+ const BLOCK_MAX = 900_000 - 19;
4
+ const MAX_RUN = 255; // libbzip2's longest RLE1 run.
5
+ const MAX_CODE_LEN = 17; // libbzip2's encoder limit; decoders accept 20.
6
+ const GROUP_SIZE = 50;
7
+ /** Returns `input` as one level-9 bzip2 stream; an empty input gives a stream with no blocks. */
8
+ export function bzip2(input) {
9
+ const w = bitWriter(input.length);
10
+ for (const c of [0x42, 0x5a, 0x68, 0x39])
11
+ w.put(8, c); // "BZh9"
12
+ const block = new Uint8Array(Math.min(BLOCK_MAX, 2 * input.length));
13
+ let combined = 0;
14
+ let at = 0;
15
+ while (at < input.length) {
16
+ // RLE1, one whole token at a time, so no block ends inside a run.
17
+ const start = at;
18
+ let n = 0;
19
+ while (at < input.length) {
20
+ const b = input[at] ?? 0;
21
+ let run = 1;
22
+ while (run < MAX_RUN && at + run < input.length && input[at + run] === b)
23
+ run++;
24
+ if (run < 4) {
25
+ if (n + run > BLOCK_MAX)
26
+ break;
27
+ block.fill(b, n, n + run);
28
+ n += run;
29
+ }
30
+ else {
31
+ if (n + 5 > BLOCK_MAX)
32
+ break;
33
+ block.fill(b, n, n + 4);
34
+ block[n + 4] = run - 4;
35
+ n += 5;
36
+ }
37
+ at += run;
38
+ }
39
+ let crc = 0xffffffff;
40
+ for (let i = start; i < at; i++)
41
+ crc = bzip2CrcStep(crc, input[i] ?? 0);
42
+ crc = ~crc >>> 0;
43
+ writeBlock(w, block.subarray(0, n), crc);
44
+ combined = (((combined << 1) | (combined >>> 31)) ^ crc) >>> 0;
45
+ }
46
+ w.put(24, 0x177245);
47
+ w.put(24, 0x385090);
48
+ w.put(16, combined >>> 16);
49
+ w.put(16, combined & 0xffff);
50
+ return w.finish();
51
+ }
52
+ function writeBlock(w, block, crc) {
53
+ const n = block.length;
54
+ const order = sortRotations(block, false);
55
+ const seq = new Int16Array(256).fill(-1); // Byte to its index among the bytes in use.
56
+ for (const b of block)
57
+ seq[b] = 0;
58
+ let nInUse = 0;
59
+ for (let b = 0; b < 256; b++)
60
+ if (seq[b] === 0)
61
+ seq[b] = nInUse++;
62
+ // Move-to-front over the last column; zero runs become RUNA (0) and RUNB (1) digits, bijective base 2.
63
+ const syms = new Uint16Array(n + 1);
64
+ const mtf = Uint8Array.from({ length: nInUse }, (_, i) => i);
65
+ let nSyms = 0;
66
+ let zeros = 0;
67
+ let origPtr = 0;
68
+ const flush = () => {
69
+ if (zeros === 0)
70
+ return;
71
+ for (let z = zeros - 1;; z = (z - 2) >>> 1) {
72
+ syms[nSyms++] = z & 1;
73
+ if (z < 2)
74
+ break;
75
+ }
76
+ zeros = 0;
77
+ };
78
+ for (let i = 0; i < n; i++) {
79
+ const r = order[i] ?? 0;
80
+ if (r === 0)
81
+ origPtr = i;
82
+ const s = seq[block[(r === 0 ? n : r) - 1] ?? 0] ?? 0;
83
+ if (mtf[0] === s) {
84
+ zeros++;
85
+ continue;
86
+ }
87
+ flush();
88
+ let prev = mtf[0] ?? 0;
89
+ mtf[0] = s;
90
+ let j = 1;
91
+ for (; mtf[j] !== s; j++) {
92
+ const next = mtf[j] ?? 0;
93
+ mtf[j] = prev;
94
+ prev = next;
95
+ }
96
+ mtf[j] = prev;
97
+ syms[nSyms++] = j + 1;
98
+ }
99
+ flush();
100
+ const alphaSize = nInUse + 2;
101
+ syms[nSyms++] = alphaSize - 1; // End of block.
102
+ // One Huffman table, written into both of the two slots the format requires; every selector names it.
103
+ const freq = new Int32Array(alphaSize);
104
+ for (let i = 0; i < nSyms; i++) {
105
+ const s = syms[i] ?? 0;
106
+ freq[s] = (freq[s] ?? 0) + 1;
107
+ }
108
+ const lengths = codeLengths(freq);
109
+ const codes = new Int32Array(alphaSize);
110
+ let code = 0;
111
+ for (let len = 1; len <= MAX_CODE_LEN; len++) {
112
+ for (let s = 0; s < alphaSize; s++)
113
+ if (lengths[s] === len)
114
+ codes[s] = code++;
115
+ code <<= 1;
116
+ }
117
+ w.put(24, 0x314159);
118
+ w.put(24, 0x265359);
119
+ w.put(16, crc >>> 16);
120
+ w.put(16, crc & 0xffff);
121
+ w.put(1, 0); // Not randomised.
122
+ w.put(24, origPtr);
123
+ let ranges = 0;
124
+ for (let b = 0; b < 256; b++)
125
+ if ((seq[b] ?? -1) >= 0)
126
+ ranges |= 0x8000 >>> (b >> 4);
127
+ w.put(16, ranges);
128
+ for (let r = 0; r < 16; r++) {
129
+ if ((ranges & (0x8000 >>> r)) === 0)
130
+ continue;
131
+ let used = 0;
132
+ for (let j = 0; j < 16; j++)
133
+ if ((seq[r * 16 + j] ?? -1) >= 0)
134
+ used |= 0x8000 >>> j;
135
+ w.put(16, used);
136
+ }
137
+ w.put(3, 2);
138
+ const nSelectors = Math.ceil(nSyms / GROUP_SIZE);
139
+ w.put(15, nSelectors);
140
+ for (let i = 0; i < nSelectors; i++)
141
+ w.put(1, 0);
142
+ for (let t = 0; t < 2; t++) {
143
+ let curr = lengths[0] ?? 0;
144
+ w.put(5, curr);
145
+ for (let s = 0; s < alphaSize; s++) {
146
+ const len = lengths[s] ?? 0;
147
+ for (; curr < len; curr++)
148
+ w.put(2, 2);
149
+ for (; curr > len; curr--)
150
+ w.put(2, 3);
151
+ w.put(1, 0);
152
+ }
153
+ }
154
+ for (let i = 0; i < nSyms; i++) {
155
+ const s = syms[i] ?? 0;
156
+ w.put(lengths[s] ?? 0, codes[s] ?? 0);
157
+ }
158
+ }
159
+ /**
160
+ * Returns Huffman code lengths of at most 17 bits for every symbol, from a two-queue build over `max(freq, 1)` with
161
+ * ties broken by symbol and leaves before internal nodes, so the result depends only on `freq`.
162
+ */
163
+ function codeLengths(freq) {
164
+ const n = freq.length;
165
+ const weight = Array.from(freq, (f) => Math.max(1, f));
166
+ const leaves = Array.from({ length: n }, (_, i) => i);
167
+ const nodeWeight = new Float64Array(2 * n - 1);
168
+ const parent = new Int32Array(2 * n - 1);
169
+ const depth = new Int32Array(2 * n - 1);
170
+ for (;;) {
171
+ leaves.sort((a, b) => (weight[a] ?? 0) - (weight[b] ?? 0) || a - b);
172
+ for (let i = 0; i < n; i++)
173
+ nodeWeight[i] = weight[i] ?? 0;
174
+ let leaf = 0;
175
+ let inner = n;
176
+ const take = (made) => {
177
+ const l = leaves[leaf];
178
+ if (l !== undefined && (inner >= made || (nodeWeight[l] ?? 0) <= (nodeWeight[inner] ?? 0))) {
179
+ leaf++;
180
+ return l;
181
+ }
182
+ return inner++;
183
+ };
184
+ for (let made = n; made < 2 * n - 1; made++) {
185
+ const a = take(made);
186
+ const b = take(made);
187
+ nodeWeight[made] = (nodeWeight[a] ?? 0) + (nodeWeight[b] ?? 0);
188
+ parent[a] = made;
189
+ parent[b] = made;
190
+ }
191
+ // A parent is made after its children, so walking down from the root sets each depth after its parent's.
192
+ depth[2 * n - 2] = 0;
193
+ let longest = 0;
194
+ for (let i = 2 * n - 3; i >= 0; i--) {
195
+ depth[i] = (depth[parent[i] ?? 0] ?? 0) + 1;
196
+ if (i < n)
197
+ longest = Math.max(longest, depth[i] ?? 0);
198
+ }
199
+ if (longest <= MAX_CODE_LEN)
200
+ return depth.slice(0, n);
201
+ for (let i = 0; i < n; i++)
202
+ weight[i] = Math.max(1, (weight[i] ?? 0) >> 1);
203
+ }
204
+ }
205
+ function bitWriter(sizeHint) {
206
+ let bytes = new Uint8Array(Math.max(64, sizeHint >> 2));
207
+ let length = 0;
208
+ let buf = 0;
209
+ let count = 0; // At most 7 bits stay in `buf` between calls.
210
+ const byte = (b) => {
211
+ if (length === bytes.length) {
212
+ const grown = new Uint8Array(bytes.length * 2);
213
+ grown.set(bytes);
214
+ bytes = grown;
215
+ }
216
+ bytes[length++] = b;
217
+ };
218
+ return {
219
+ put(n, value) {
220
+ buf = (buf << n) | value;
221
+ count += n;
222
+ while (count >= 8) {
223
+ count -= 8;
224
+ byte((buf >>> count) & 0xff);
225
+ }
226
+ buf &= (1 << count) - 1;
227
+ },
228
+ finish() {
229
+ if (count > 0)
230
+ byte((buf << (8 - count)) & 0xff);
231
+ count = 0;
232
+ return bytes.slice(0, length);
233
+ },
234
+ };
235
+ }
236
+ /**
237
+ * Returns the start of each cyclic rotation of `bytes` in sorted order, in O(n log n) for any input. With `sentinel`,
238
+ * a symbol below every byte follows `bytes`, so rotation order is suffix order and the result has `bytes.length + 1`
239
+ * entries, the empty suffix first. Equal rotations, which only a periodic input without a sentinel has, come in an
240
+ * unspecified fixed order.
241
+ */
242
+ export function sortRotations(bytes, sentinel) {
243
+ // Prefix doubling: each round counting-sorts by (rank of the first h symbols, rank of the next h).
244
+ const len = bytes.length;
245
+ const n = len + (sentinel ? 1 : 0);
246
+ const sa = new Int32Array(n);
247
+ let rank = new Int32Array(n);
248
+ let tmp = new Int32Array(n);
249
+ const cnt = new Int32Array(Math.max(258, n + 1));
250
+ for (let i = 0; i < len; i++)
251
+ rank[i] = (bytes[i] ?? 0) + 1; // The sentinel is rank 0.
252
+ for (let i = 0; i < n; i++)
253
+ tmp[i] = i;
254
+ let classes = 258;
255
+ for (let h = 0;; h = h === 0 ? 1 : 2 * h) {
256
+ // `tmp` lists rotations in order of their second key; a stable sort by the first key orders them by both.
257
+ cnt.fill(0, 0, classes);
258
+ for (let i = 0; i < n; i++) {
259
+ const r = rank[i] ?? 0;
260
+ cnt[r] = (cnt[r] ?? 0) + 1;
261
+ }
262
+ for (let c = 1; c < classes; c++)
263
+ cnt[c] = (cnt[c] ?? 0) + (cnt[c - 1] ?? 0);
264
+ for (let i = n - 1; i >= 0; i--) {
265
+ const j = tmp[i] ?? 0;
266
+ const r = rank[j] ?? 0;
267
+ const at = (cnt[r] ?? 0) - 1;
268
+ cnt[r] = at;
269
+ sa[at] = j;
270
+ }
271
+ // Renumber densely; a rotation's class is now its first 2h symbols (its first symbol in round 0).
272
+ let c = 0;
273
+ for (let i = 0; i < n; i++) {
274
+ const a = sa[i] ?? 0;
275
+ const b = sa[i - 1] ?? 0;
276
+ const ah = a + h < n ? a + h : a + h - n;
277
+ const bh = b + h < n ? b + h : b + h - n;
278
+ if (i > 0 && (rank[a] !== rank[b] || (h > 0 && rank[ah] !== rank[bh])))
279
+ c++;
280
+ tmp[a] = c;
281
+ }
282
+ [rank, tmp] = [tmp, rank];
283
+ classes = c + 1;
284
+ // Rotations still equal after n symbols are identical.
285
+ const next = h === 0 ? 1 : 2 * h;
286
+ if (classes >= n || next >= n)
287
+ return sa;
288
+ for (let i = 0; i < n; i++) {
289
+ const j = (sa[i] ?? 0) - next;
290
+ tmp[i] = j < 0 ? j + n : j;
291
+ }
292
+ }
293
+ }