@tide-ota/cli 0.0.1 → 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 +21 -0
- package/README.md +35 -1
- package/dist/bsdiff.d.ts +17 -0
- package/dist/bsdiff.js +204 -0
- package/dist/bucket.d.ts +54 -0
- package/dist/bucket.js +39 -0
- package/dist/bzip2.d.ts +9 -0
- package/dist/bzip2.js +293 -0
- package/dist/disk.d.ts +11 -0
- package/dist/disk.js +318 -0
- package/dist/export.d.ts +8 -0
- package/dist/export.js +220 -0
- package/dist/keys-kms.d.ts +16 -0
- package/dist/keys-kms.js +171 -0
- package/dist/keys.d.ts +69 -0
- package/dist/keys.js +238 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +54 -0
- package/dist/pointer.d.ts +20 -0
- package/dist/pointer.js +47 -0
- package/dist/probe.d.ts +23 -0
- package/dist/probe.js +306 -0
- package/dist/publish-export.d.ts +57 -0
- package/dist/publish-export.js +222 -0
- package/dist/publish-patch.d.ts +35 -0
- package/dist/publish-patch.js +78 -0
- package/dist/publish-record.d.ts +52 -0
- package/dist/publish-record.js +176 -0
- package/dist/publish-rollback.d.ts +14 -0
- package/dist/publish-rollback.js +169 -0
- package/dist/publish.d.ts +100 -0
- package/dist/publish.js +335 -0
- package/dist/register-build.d.ts +8 -0
- package/dist/register-build.js +180 -0
- package/dist/s3.d.ts +9 -0
- package/dist/s3.js +221 -0
- package/dist/status.d.ts +26 -0
- package/dist/status.js +117 -0
- package/dist/storage-report.d.ts +7 -0
- package/dist/storage-report.js +107 -0
- package/dist/storage.d.ts +6 -0
- package/dist/storage.js +18 -0
- package/package.json +57 -5
package/dist/disk.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Bucket } from "./bucket.ts";
|
|
2
|
+
export interface DiskOptions {
|
|
3
|
+
readonly busyTimeoutMs?: number;
|
|
4
|
+
readonly now?: () => number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Returns a bucket over a directory, for tests and single-host development. Writers serialise on a SQLite lock
|
|
8
|
+
* (`fcntl`), which the OS frees when its holder dies; each conditional write reads, compares and renames inside it.
|
|
9
|
+
* Readers take no lock: a rename is atomic, and a read holds one open file. Network filesystems are not supported.
|
|
10
|
+
*/
|
|
11
|
+
export declare function diskBucket(root: string, options?: DiskOptions): Bucket;
|
package/dist/disk.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { closeSync, constants, fstatSync, lstatSync, mkdirSync, openSync, readSync, readdirSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { diskKey, diskPath, unescapeDiskPath } from "@tide-ota/protocol";
|
|
5
|
+
import { BucketError, byteOrder, checkEtag, checkKey } from "./bucket.js";
|
|
6
|
+
/** `.tide/` holds the writers' mutex and staged files; `diskPath` never yields a name that starts with `.`. */
|
|
7
|
+
const META = ".tide";
|
|
8
|
+
const HASH_CHUNK = 64 * 1024;
|
|
9
|
+
// O_NONBLOCK: opening a FIFO with no writer must fail the regular-file check, not block.
|
|
10
|
+
const READ_FLAGS = constants.O_RDONLY | constants.O_NONBLOCK;
|
|
11
|
+
const etagOf = (bytes) => `"${createHash("sha256").update(bytes).digest("hex")}"`;
|
|
12
|
+
/**
|
|
13
|
+
* Returns a bucket over a directory, for tests and single-host development. Writers serialise on a SQLite lock
|
|
14
|
+
* (`fcntl`), which the OS frees when its holder dies; each conditional write reads, compares and renames inside it.
|
|
15
|
+
* Readers take no lock: a rename is atomic, and a read holds one open file. Network filesystems are not supported.
|
|
16
|
+
*/
|
|
17
|
+
export function diskBucket(root, options = {}) {
|
|
18
|
+
if (!statSync(root, { throwIfNoEntry: false })?.isDirectory()) {
|
|
19
|
+
throw new BucketError(`disk bucket ${root} is not a directory`, "failed");
|
|
20
|
+
}
|
|
21
|
+
const { busyTimeoutMs = 10_000, now = Date.now } = options;
|
|
22
|
+
const fail = (word, outcome = "failed") => new BucketError(`disk bucket ${root} ${word}`, outcome);
|
|
23
|
+
const code = (e) => e.code ?? "error";
|
|
24
|
+
const rootPresent = () => statSync(root, { throwIfNoEntry: false })?.isDirectory() === true;
|
|
25
|
+
/** ENOENT under a present root is absence; under a missing root it is an outage. */
|
|
26
|
+
const absent = (e) => {
|
|
27
|
+
if (code(e) !== "ENOENT")
|
|
28
|
+
throw fail(`failed: ${code(e)}`);
|
|
29
|
+
if (!rootPresent())
|
|
30
|
+
throw fail("unavailable");
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
/** Creates one directory level, accepting only a real directory that is already there. */
|
|
34
|
+
const ensureDir = (path) => {
|
|
35
|
+
try {
|
|
36
|
+
mkdirSync(path);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
if (code(e) !== "EEXIST" || !isRealDirectory(path))
|
|
40
|
+
throw fail("unavailable");
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const ensureParents = (rel) => {
|
|
44
|
+
const parts = rel.split("/").slice(0, -1);
|
|
45
|
+
for (let i = 1; i <= parts.length; i++)
|
|
46
|
+
ensureDir(join(root, ...parts.slice(0, i)));
|
|
47
|
+
};
|
|
48
|
+
/** Returns an open descriptor's bytes, failing on anything but a regular file of at most `maxBytes`. */
|
|
49
|
+
const readOpen = (fd, maxBytes) => {
|
|
50
|
+
const st = fstatSync(fd);
|
|
51
|
+
if (!st.isFile())
|
|
52
|
+
throw fail("not a regular file");
|
|
53
|
+
if (st.size > maxBytes)
|
|
54
|
+
throw fail(`object over ${maxBytes} bytes`);
|
|
55
|
+
const bytes = new Uint8Array(st.size);
|
|
56
|
+
for (let at = 0; at < bytes.length;) {
|
|
57
|
+
const n = readSync(fd, bytes, at, bytes.length - at, null);
|
|
58
|
+
if (n === 0)
|
|
59
|
+
throw fail("object changed while read");
|
|
60
|
+
at += n;
|
|
61
|
+
}
|
|
62
|
+
return bytes;
|
|
63
|
+
};
|
|
64
|
+
/** Returns the ETag of the regular file at `path`, hashed in chunks, or undefined when absent. */
|
|
65
|
+
const storedEtag = (path) => {
|
|
66
|
+
let fd;
|
|
67
|
+
try {
|
|
68
|
+
fd = openSync(path, READ_FLAGS);
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
if (code(e) === "ENOENT")
|
|
72
|
+
return undefined;
|
|
73
|
+
throw fail(`failed: ${code(e)}`);
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
if (!fstatSync(fd).isFile())
|
|
77
|
+
throw fail("not a regular file");
|
|
78
|
+
const hash = createHash("sha256");
|
|
79
|
+
const chunk = new Uint8Array(HASH_CHUNK);
|
|
80
|
+
for (let n; (n = readSync(fd, chunk, 0, chunk.length, null)) > 0;) {
|
|
81
|
+
hash.update(chunk.subarray(0, n));
|
|
82
|
+
}
|
|
83
|
+
return `"${hash.digest("hex")}"`;
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
closeSync(fd);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Runs `mutate` holding the writers' mutex, then releases it and removes `staged` if it was not renamed.
|
|
91
|
+
* `mutate` calls `done()` right after its rename or unlink, so a later error is reported as `unknown`.
|
|
92
|
+
*/
|
|
93
|
+
const locked = (staged, mutate) => {
|
|
94
|
+
const unstage = () => {
|
|
95
|
+
if (staged !== undefined)
|
|
96
|
+
rmSync(staged, { force: true });
|
|
97
|
+
};
|
|
98
|
+
const db = acquire(unstage);
|
|
99
|
+
let mutated = false;
|
|
100
|
+
const toError = (e) => {
|
|
101
|
+
const outcome = mutated ? "unknown" : "failed";
|
|
102
|
+
return e instanceof BucketError
|
|
103
|
+
? new BucketError(e.message, outcome)
|
|
104
|
+
: fail(`failed: ${code(e)}`, outcome);
|
|
105
|
+
};
|
|
106
|
+
const release = () => {
|
|
107
|
+
try {
|
|
108
|
+
db.exec("COMMIT");
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
db.close();
|
|
112
|
+
unstage();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
let result;
|
|
116
|
+
try {
|
|
117
|
+
result = mutate(() => (mutated = true));
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
try {
|
|
121
|
+
release();
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// The mutation's own error is the one to report.
|
|
125
|
+
}
|
|
126
|
+
throw toError(e);
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
release();
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
throw toError(e);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
};
|
|
136
|
+
/** Opens the mutex connection and takes the lock; on failure, unstages and throws `busy` or `unavailable`. */
|
|
137
|
+
const acquire = (unstage) => {
|
|
138
|
+
let db;
|
|
139
|
+
try {
|
|
140
|
+
// Loaded on first lock, so S3 users never load SQLite, which warns as experimental on early Node 24.
|
|
141
|
+
const sqlite = process.getBuiltinModule("node:sqlite");
|
|
142
|
+
db = new sqlite.DatabaseSync(join(root, META, "mutex.sqlite"), { timeout: busyTimeoutMs });
|
|
143
|
+
db.exec("BEGIN IMMEDIATE");
|
|
144
|
+
return db;
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
// No transaction began, so there is nothing to commit.
|
|
148
|
+
if (db?.isOpen === true)
|
|
149
|
+
db.close();
|
|
150
|
+
unstage();
|
|
151
|
+
const errcode = e.errcode;
|
|
152
|
+
throw fail(errcode === 5 || errcode === 6 ? "busy" : "unavailable");
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
/** Stages `bytes` beside the bucket, outside the mutex; returns the staged path and the bytes' ETag. */
|
|
156
|
+
const stage = (bytes) => {
|
|
157
|
+
ensureDir(join(root, META));
|
|
158
|
+
ensureDir(join(root, META, "tmp"));
|
|
159
|
+
const staged = join(root, META, "tmp", randomUUID());
|
|
160
|
+
try {
|
|
161
|
+
writeFileSync(staged, bytes, { flag: "wx" });
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
rmSync(staged, { force: true });
|
|
165
|
+
throw fail(`failed: ${code(e)}`);
|
|
166
|
+
}
|
|
167
|
+
return { staged, etag: etagOf(bytes) };
|
|
168
|
+
};
|
|
169
|
+
function write(key, bytes, proceed = () => true) {
|
|
170
|
+
checkKey(key);
|
|
171
|
+
const rel = diskPath(key);
|
|
172
|
+
const target = join(root, rel);
|
|
173
|
+
const { staged, etag } = stage(bytes);
|
|
174
|
+
return locked(staged, (done) => {
|
|
175
|
+
if (!proceed(target))
|
|
176
|
+
return undefined;
|
|
177
|
+
ensureParents(rel);
|
|
178
|
+
renameSync(staged, target);
|
|
179
|
+
done();
|
|
180
|
+
return etag;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
get: (key, maxBytes) => settle(() => {
|
|
185
|
+
checkKey(key);
|
|
186
|
+
let fd;
|
|
187
|
+
try {
|
|
188
|
+
fd = openSync(join(root, diskPath(key)), READ_FLAGS);
|
|
189
|
+
}
|
|
190
|
+
catch (e) {
|
|
191
|
+
absent(e);
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const bytes = readOpen(fd, maxBytes);
|
|
196
|
+
return { bytes, etag: etagOf(bytes) };
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
closeSync(fd);
|
|
200
|
+
}
|
|
201
|
+
}),
|
|
202
|
+
put: (key, bytes) => settle(() => write(key, bytes)),
|
|
203
|
+
putIfAbsent: (key, bytes) => settle(() => write(key, bytes, (target) => !lstatSync(target, { throwIfNoEntry: false }))),
|
|
204
|
+
putIfMatch: (key, bytes, etag) => settle(() => {
|
|
205
|
+
checkEtag(etag);
|
|
206
|
+
return write(key, bytes, (target) => storedEtag(target) === etag);
|
|
207
|
+
}),
|
|
208
|
+
delete: (key) => settle(() => {
|
|
209
|
+
checkKey(key);
|
|
210
|
+
const target = join(root, diskPath(key));
|
|
211
|
+
ensureDir(join(root, META));
|
|
212
|
+
locked(undefined, (done) => {
|
|
213
|
+
try {
|
|
214
|
+
unlinkSync(target);
|
|
215
|
+
}
|
|
216
|
+
catch (e) {
|
|
217
|
+
absent(e);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
done();
|
|
221
|
+
});
|
|
222
|
+
}),
|
|
223
|
+
list: (prefix) => settle(() => listSync(prefix)),
|
|
224
|
+
time: () => settle(now),
|
|
225
|
+
};
|
|
226
|
+
function listSync(prefix) {
|
|
227
|
+
const found = [];
|
|
228
|
+
const reaches = (name) => `${name}/`.startsWith(prefix) || prefix.startsWith(`${name}/`);
|
|
229
|
+
const walk = (rel) => {
|
|
230
|
+
let dirents;
|
|
231
|
+
try {
|
|
232
|
+
dirents = readdirSync(rel === "" ? root : join(root, rel), { withFileTypes: true });
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
if (rel !== "" && code(e) === "ENOENT" && rootPresent())
|
|
236
|
+
return; // Removed mid-walk.
|
|
237
|
+
throw rel === "" ? fail("unavailable") : fail(`failed: ${code(e)}`);
|
|
238
|
+
}
|
|
239
|
+
for (const d of dirents) {
|
|
240
|
+
if (rel === "" && d.name === META)
|
|
241
|
+
continue;
|
|
242
|
+
const path = rel === "" ? d.name : `${rel}/${d.name}`;
|
|
243
|
+
const key = diskKey(path);
|
|
244
|
+
// A subtree that cannot hold a match is skipped, so its failures cannot fail this listing.
|
|
245
|
+
if (key === undefined && d.isDirectory()) {
|
|
246
|
+
const lower = path.toLowerCase();
|
|
247
|
+
if (reaches(path) ||
|
|
248
|
+
reaches(unescapeDiskPath(path)) ||
|
|
249
|
+
(reaches(lower) && sameEntry(path, lower))) {
|
|
250
|
+
walk(path);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
else
|
|
254
|
+
found.push({ path, key });
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
walk("");
|
|
258
|
+
// A file or symlink where a directory of the prefix belongs hides everything under it.
|
|
259
|
+
if (found.some((f) => f.key === undefined && prefix.startsWith(`${f.path}/`))) {
|
|
260
|
+
throw fail("failed: ENOTDIR");
|
|
261
|
+
}
|
|
262
|
+
const listed = new Set(found.map((f) => f.key));
|
|
263
|
+
const entries = [];
|
|
264
|
+
for (const f of found) {
|
|
265
|
+
const key = f.key ?? aliasOf(f.path, listed);
|
|
266
|
+
if (!(key ?? f.path).startsWith(prefix))
|
|
267
|
+
continue;
|
|
268
|
+
const size = sizeOf(f.path);
|
|
269
|
+
entries.push(key === undefined ? { unknown: f.path, size } : { key, size });
|
|
270
|
+
}
|
|
271
|
+
const name = (e) => ("key" in e ? e.key : e.unknown);
|
|
272
|
+
return entries.sort((a, b) => byteOrder(name(a), name(b)));
|
|
273
|
+
}
|
|
274
|
+
/** Returns the bytes at `path` without following a link: 0 for anything but a regular file. */
|
|
275
|
+
function sizeOf(path) {
|
|
276
|
+
try {
|
|
277
|
+
const st = lstatSync(join(root, path));
|
|
278
|
+
return st.isFile() ? st.size : 0;
|
|
279
|
+
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
throw fail(`failed: ${code(e)}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Returns the key `get` opens at `path` on a case-insensitive volume, so a listing agrees with reads: the unlisted
|
|
286
|
+
* key whose path is `path` lowercased and is this same file. Undefined otherwise.
|
|
287
|
+
*/
|
|
288
|
+
function aliasOf(path, listed) {
|
|
289
|
+
const lower = path.toLowerCase();
|
|
290
|
+
const key = diskKey(lower);
|
|
291
|
+
return key !== undefined && !listed.has(key) && sameEntry(path, lower) ? key : undefined;
|
|
292
|
+
}
|
|
293
|
+
/** True when both paths name one directory entry, as differently cased names do on a case-insensitive volume. */
|
|
294
|
+
function sameEntry(a, b) {
|
|
295
|
+
try {
|
|
296
|
+
const [x, y] = [a, b].map((p) => lstatSync(join(root, p), { throwIfNoEntry: false }));
|
|
297
|
+
return x !== undefined && y !== undefined && x.ino === y.ino && x.dev === y.dev;
|
|
298
|
+
}
|
|
299
|
+
catch (e) {
|
|
300
|
+
throw fail(`failed: ${code(e)}`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** True for a directory, false for anything else, a symlink to a directory included. */
|
|
305
|
+
function isRealDirectory(path) {
|
|
306
|
+
try {
|
|
307
|
+
return lstatSync(path).isDirectory();
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** Returns `run`'s result as a promise, turning a synchronous throw into a rejection. */
|
|
314
|
+
function settle(run) {
|
|
315
|
+
return new Promise((resolve) => {
|
|
316
|
+
resolve(run());
|
|
317
|
+
});
|
|
318
|
+
}
|
package/dist/export.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Env } from "./keys.ts";
|
|
2
|
+
import type { PublishOptions } from "./publish.ts";
|
|
3
|
+
export declare const EXPORT_USAGE = "usage: tide export --out <dir> [--platform ios|android]... [--runtime-version <version>] [--project-root <dir>] [--pass-env <name>]...";
|
|
4
|
+
/**
|
|
5
|
+
* Exports the app for `tide publish --from`: the public config, each platform's runtime, the certificate and
|
|
6
|
+
* `keyid`, and the files `metadata.json` names. Reads no credential. Exit 0, or 2 on any refusal or tool failure.
|
|
7
|
+
*/
|
|
8
|
+
export declare function exportProject(args: readonly string[], env: Env, options: PublishOptions): Promise<0 | 2>;
|
package/dist/export.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { mkdirSync, mkdtempSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import { replyKey } from "@tide-ota/protocol";
|
|
7
|
+
import { KeyError, isObject, member, readSmallFile, signingMetadata, validCertificate, } from "./keys.js";
|
|
8
|
+
import { EXPORT_FILE, EXPORT_LIMIT, encodeProjectExport, readExport } from "./publish-export.js";
|
|
9
|
+
import { PublishError, platformsOf } from "./publish-record.js";
|
|
10
|
+
import { parsed, report } from "./publish.js";
|
|
11
|
+
export const EXPORT_USAGE = "usage: tide export --out <dir> [--platform ios|android]... [--runtime-version <version>] [--project-root <dir>] [--pass-env <name>]...";
|
|
12
|
+
const STDOUT_LIMIT = 16 * 1024 * 1024;
|
|
13
|
+
// What every child sees from the caller: locale, paths and CI flags, and what Expo inlines into the public bundle.
|
|
14
|
+
const INHERITED = /^(?:PATH|HOME|TMPDIR|LANG|LANGUAGE|TZ|CI|LC_[A-Z]+|EXPO_PUBLIC_\w+)$/;
|
|
15
|
+
const NAME = /^[A-Za-z_]\w*$/;
|
|
16
|
+
// Tide's own, cloud and CI credentials; the export holds none, so project code is never given them.
|
|
17
|
+
const SECRET = /^(?:TIDE|AWS|ACTIONS)_/i;
|
|
18
|
+
/**
|
|
19
|
+
* Exports the app for `tide publish --from`: the public config, each platform's runtime, the certificate and
|
|
20
|
+
* `keyid`, and the files `metadata.json` names. Reads no credential. Exit 0, or 2 on any refusal or tool failure.
|
|
21
|
+
*/
|
|
22
|
+
export async function exportProject(args, env, options) {
|
|
23
|
+
try {
|
|
24
|
+
await run(args, env, options);
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
return report(e, options, 2);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function run(args, env, { out: print, now = Date.now, cwd = process.cwd() }) {
|
|
32
|
+
const { out, platforms, runtimeVersion, projectRoot, passEnv } = parse(args, cwd);
|
|
33
|
+
const childEnv = environment(env, passEnv);
|
|
34
|
+
const config = await expoJson(projectRoot, childEnv, ["config", "--json"], "expo");
|
|
35
|
+
const updates = member(config, "updates");
|
|
36
|
+
const metadata = signingMetadata(member(updates, "codeSigningMetadata"), "app config updates.codeSigningMetadata");
|
|
37
|
+
const certificate = await appCertificate(projectRoot, member(updates, "codeSigningCertificate"), now());
|
|
38
|
+
const expoClient = await expoJson(projectRoot, childEnv, ["config", "--type", "public", "--json"], "expo");
|
|
39
|
+
if (!isObject(expoClient)) {
|
|
40
|
+
throw new PublishError("expo config --type public did not print an object");
|
|
41
|
+
}
|
|
42
|
+
const runtimes = {};
|
|
43
|
+
for (const platform of platforms) {
|
|
44
|
+
const runtime = runtimeVersion ??
|
|
45
|
+
member(await expoJson(projectRoot, childEnv, ["runtimeversion:resolve", "--platform", platform], "expo-updates"), "runtimeVersion");
|
|
46
|
+
if (runtime === null || runtime === undefined) {
|
|
47
|
+
throw new PublishError(`no ${platform} runtime version: set runtimeVersion or pass --runtime-version`);
|
|
48
|
+
}
|
|
49
|
+
if (typeof runtime !== "string") {
|
|
50
|
+
throw new PublishError(`the ${platform} runtime version is not a string`);
|
|
51
|
+
}
|
|
52
|
+
replyKey("main", runtime, platform);
|
|
53
|
+
runtimes[platform] = { runtimeVersion: runtime };
|
|
54
|
+
}
|
|
55
|
+
const value = {
|
|
56
|
+
certificate,
|
|
57
|
+
codeSigningMetadata: metadata,
|
|
58
|
+
expoClient: expoClient,
|
|
59
|
+
platforms: runtimes,
|
|
60
|
+
};
|
|
61
|
+
const envelope = encodeProjectExport(value);
|
|
62
|
+
if (envelope.length > EXPORT_LIMIT) {
|
|
63
|
+
throw new PublishError(`${EXPORT_FILE} would be over ${EXPORT_LIMIT} bytes`);
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
mkdirSync(out);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
throw new PublishError(e.code === "EEXIST"
|
|
70
|
+
? "--out already exists: give a new directory"
|
|
71
|
+
: "--out cannot be created");
|
|
72
|
+
}
|
|
73
|
+
const staging = mkdtempSync(join(tmpdir(), "tide-export-"));
|
|
74
|
+
try {
|
|
75
|
+
const selected = platforms.flatMap((p) => ["--platform", p]);
|
|
76
|
+
await runTool(projectRoot, childEnv, "expo", ["export", "--output-dir", staging, ...selected]);
|
|
77
|
+
const { files } = await readExport(staging, platforms.map((platform) => ({ platform })));
|
|
78
|
+
// Only what metadata.json names: Expo also copies the project's public/ folder into its output.
|
|
79
|
+
for (const [path, bytes] of files)
|
|
80
|
+
write(join(out, "dist", path), bytes);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
rmSync(staging, { recursive: true, force: true });
|
|
84
|
+
}
|
|
85
|
+
// Written last, under its name only once complete, so a partial export is never read as one.
|
|
86
|
+
const temporary = join(out, `.${EXPORT_FILE}.partial`);
|
|
87
|
+
write(temporary, envelope);
|
|
88
|
+
renameSync(temporary, join(out, EXPORT_FILE));
|
|
89
|
+
for (const platform of platforms)
|
|
90
|
+
print(`${platform} ${runtimes[platform]?.runtimeVersion}`);
|
|
91
|
+
}
|
|
92
|
+
function parse(args, cwd) {
|
|
93
|
+
const { values } = parsed(EXPORT_USAGE, () => parseArgs({
|
|
94
|
+
args: [...args],
|
|
95
|
+
options: {
|
|
96
|
+
out: { type: "string" },
|
|
97
|
+
platform: { type: "string", multiple: true },
|
|
98
|
+
"runtime-version": { type: "string" },
|
|
99
|
+
"project-root": { type: "string" },
|
|
100
|
+
"pass-env": { type: "string", multiple: true },
|
|
101
|
+
},
|
|
102
|
+
strict: true,
|
|
103
|
+
allowPositionals: false,
|
|
104
|
+
}));
|
|
105
|
+
if (values.out === undefined)
|
|
106
|
+
throw new PublishError(`--out is required; ${EXPORT_USAGE}`);
|
|
107
|
+
const runtimeVersion = values["runtime-version"];
|
|
108
|
+
if (runtimeVersion !== undefined)
|
|
109
|
+
replyKey("main", runtimeVersion, "ios");
|
|
110
|
+
const passEnv = values["pass-env"] ?? [];
|
|
111
|
+
for (const name of passEnv) {
|
|
112
|
+
if (!NAME.test(name))
|
|
113
|
+
throw new PublishError("--pass-env must name an environment variable");
|
|
114
|
+
if (SECRET.test(name)) {
|
|
115
|
+
throw new PublishError("--pass-env cannot pass TIDE_, AWS_ or ACTIONS_ variables to project code");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
// Both against the caller's directory, before any child runs elsewhere.
|
|
120
|
+
out: resolve(cwd, values.out),
|
|
121
|
+
projectRoot: resolve(cwd, values["project-root"] ?? "."),
|
|
122
|
+
platforms: platformsOf(values.platform),
|
|
123
|
+
runtimeVersion,
|
|
124
|
+
passEnv,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns the environment every child gets: the inherited allowlist, each `--pass-env` name, no dotenv files, and
|
|
129
|
+
* the production mode `expo export` forces, so config, runtime and bundle are evaluated alike.
|
|
130
|
+
*/
|
|
131
|
+
function environment(env, passEnv) {
|
|
132
|
+
const out = {};
|
|
133
|
+
for (const [name, value] of Object.entries(env)) {
|
|
134
|
+
if (value !== undefined && (INHERITED.test(name) || passEnv.includes(name)))
|
|
135
|
+
out[name] = value;
|
|
136
|
+
}
|
|
137
|
+
out.EXPO_NO_DOTENV = "1";
|
|
138
|
+
out.NODE_ENV = "production";
|
|
139
|
+
// As Expo's own setNodeEnv; the runtime resolver sets none, so it gets the same value explicitly.
|
|
140
|
+
out.BABEL_ENV =
|
|
141
|
+
env.BABEL_ENV === undefined || env.BABEL_ENV === "" ? "production" : env.BABEL_ENV;
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
/** Returns the app's certificate PEM, checked against the code-signing profile at epoch milliseconds `now`. */
|
|
145
|
+
async function appCertificate(projectRoot, path, now) {
|
|
146
|
+
if (typeof path !== "string" || path === "") {
|
|
147
|
+
throw new KeyError("app config has no updates.codeSigningCertificate: Tide publishes only signed updates");
|
|
148
|
+
}
|
|
149
|
+
// Invalid UTF-8 decodes to U+FFFD, which the certificate's PEM pattern refuses.
|
|
150
|
+
const pem = readSmallFile(join(projectRoot, path))?.toString("utf8");
|
|
151
|
+
if (pem === undefined) {
|
|
152
|
+
throw new KeyError("updates.codeSigningCertificate is unreadable or over 64 KiB");
|
|
153
|
+
}
|
|
154
|
+
// One CERTIFICATE block and whitespace only, so the export can carry nothing else from the file.
|
|
155
|
+
await validCertificate(pem, "updates.codeSigningCertificate", now);
|
|
156
|
+
return pem;
|
|
157
|
+
}
|
|
158
|
+
/** Writes a new file at `path`, creating its directories. */
|
|
159
|
+
function write(path, bytes) {
|
|
160
|
+
try {
|
|
161
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
162
|
+
writeFileSync(path, bytes, { flag: "wx" });
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
throw new PublishError("the export directory cannot be written");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Runs `tool` from the project's own dependencies and returns its stdout parsed as JSON. */
|
|
169
|
+
async function expoJson(projectRoot, env, args, tool) {
|
|
170
|
+
const stdout = await runTool(projectRoot, env, tool, args, "pipe");
|
|
171
|
+
try {
|
|
172
|
+
return JSON.parse(stdout);
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
throw new PublishError(`${tool} ${args[0] ?? ""} did not print JSON`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Returns the nearest `node_modules/.bin/<tool>` at or above the project root, as npm exec would. */
|
|
179
|
+
function toolPath(projectRoot, tool) {
|
|
180
|
+
for (let dir = projectRoot;; dir = dirname(dir)) {
|
|
181
|
+
const path = join(dir, "node_modules", ".bin", tool);
|
|
182
|
+
if (statSync(path, { throwIfNoEntry: false })?.isFile() === true)
|
|
183
|
+
return path;
|
|
184
|
+
if (dirname(dir) === dir) {
|
|
185
|
+
throw new PublishError(`${tool} is not installed in the project: add expo and expo-updates to its dependencies`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** Runs `tool` with no shell; its stdout is returned, or with "stderr" passed through as progress. */
|
|
190
|
+
function runTool(projectRoot, env, tool, args, stdout = "stderr") {
|
|
191
|
+
const command = `${tool} ${args[0] ?? ""}`;
|
|
192
|
+
const child = spawn(toolPath(projectRoot, tool), args, {
|
|
193
|
+
cwd: projectRoot,
|
|
194
|
+
env,
|
|
195
|
+
shell: false,
|
|
196
|
+
stdio: ["ignore", stdout === "pipe" ? "pipe" : process.stderr, "inherit"],
|
|
197
|
+
});
|
|
198
|
+
return new Promise((resolve, reject) => {
|
|
199
|
+
const chunks = [];
|
|
200
|
+
let size = 0;
|
|
201
|
+
child.stdout?.on("data", (chunk) => {
|
|
202
|
+
size += chunk.length;
|
|
203
|
+
if (size > STDOUT_LIMIT)
|
|
204
|
+
child.kill("SIGKILL");
|
|
205
|
+
else
|
|
206
|
+
chunks.push(chunk);
|
|
207
|
+
});
|
|
208
|
+
child.once("error", () => {
|
|
209
|
+
reject(new PublishError(`${command} could not be started`));
|
|
210
|
+
});
|
|
211
|
+
child.once("close", (code) => {
|
|
212
|
+
if (size > STDOUT_LIMIT)
|
|
213
|
+
reject(new PublishError(`${command} printed over 16 MiB`));
|
|
214
|
+
else if (code !== 0)
|
|
215
|
+
reject(new PublishError(`${command} failed with exit code ${code}`));
|
|
216
|
+
else
|
|
217
|
+
resolve(Buffer.concat(chunks).toString("utf8"));
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Signer } from "@tide-ota/protocol";
|
|
2
|
+
import type { Env } from "./keys.ts";
|
|
3
|
+
/** The KMS signer's variables; the TIDE_ prefix keeps ambient AWS credentials out. */
|
|
4
|
+
export declare const KMS_VARIABLES: {
|
|
5
|
+
readonly keyId: "TIDE_KMS_KEY_ID";
|
|
6
|
+
readonly region: "TIDE_KMS_REGION";
|
|
7
|
+
readonly accessKeyId: "TIDE_KMS_ACCESS_KEY_ID";
|
|
8
|
+
readonly secretAccessKey: "TIDE_KMS_SECRET_ACCESS_KEY";
|
|
9
|
+
readonly sessionToken: "TIDE_KMS_SESSION_TOKEN";
|
|
10
|
+
readonly endpoint: "TIDE_KMS_ENDPOINT";
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Returns a signer over the KMS key the environment configures, signing the SHA-256 of each part with `Sign`
|
|
14
|
+
* (`DIGEST`). Every failure rejects with a fixed-text `KeyError`, never an unsigned result.
|
|
15
|
+
*/
|
|
16
|
+
export declare function kmsSigner(env: Env, keyId: string, timeoutMs: number): Signer;
|