@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/publish.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { NO_UPDATE_KEY, ProtocolError, assetContentType, baseKey, buildManifest, buildNoUpdateAvailable, contentKey, encodeMultipart, encodeBaseRecord, encodeSfvDictionary, encodeStoredReply, expoSignature, replyKey, } from "@tide-ota/protocol";
|
|
5
|
+
import { BucketError } from "./bucket.js";
|
|
6
|
+
import { KeyError, openWriter } from "./keys.js";
|
|
7
|
+
import { createdAtFloor, readPointer, writePointer, writing } from "./pointer.js";
|
|
8
|
+
import { readProjectExport } from "./publish-export.js";
|
|
9
|
+
import { preparePatches, uploadPatches } from "./publish-patch.js";
|
|
10
|
+
import { BASE_PREFIX, PublishError, RECORD_PREFIX, assetUrl, encodeRecord, launchUrl, readBaseRecord, readReleaseRecord, recordKey, urlBase, } from "./publish-record.js";
|
|
11
|
+
export const PUBLISH_USAGE = "usage: tide publish --from <dir> --channel <name>";
|
|
12
|
+
const MAX_SKEW_MS = 60_000;
|
|
13
|
+
/**
|
|
14
|
+
* Publishes one update per platform to `--channel`. Returns 0 when every pointer is written, 2 when the publish is
|
|
15
|
+
* refused before any write, and 1 when it fails after writes began.
|
|
16
|
+
*/
|
|
17
|
+
export async function publish(args, env, options) {
|
|
18
|
+
return await execute(options, async () => {
|
|
19
|
+
const { ready, exported, stored, patches } = await prepare(args, env, options);
|
|
20
|
+
return async () => {
|
|
21
|
+
await uploadObjects(ready.writer.bucket, exported, stored);
|
|
22
|
+
await uploadPatches(ready.writer.bucket, patches);
|
|
23
|
+
await commit(ready, options.out);
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** Runs a write command: 0 once its write step succeeds, 2 when `prepare` refuses, 1 when a write fails. */
|
|
28
|
+
export async function execute(options, prepare) {
|
|
29
|
+
let write;
|
|
30
|
+
try {
|
|
31
|
+
write = await prepare();
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
return report(e, options, 2);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
await write();
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
return report(e, options, 1);
|
|
41
|
+
}
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
/** Prints a write command's own error and returns `code`; anything else is a bug and is rethrown. */
|
|
45
|
+
export function report(e, { err }, code) {
|
|
46
|
+
if (!(e instanceof PublishError ||
|
|
47
|
+
e instanceof KeyError ||
|
|
48
|
+
e instanceof BucketError ||
|
|
49
|
+
e instanceof ProtocolError)) {
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
err(`tide: ${e.message}`);
|
|
53
|
+
return code;
|
|
54
|
+
}
|
|
55
|
+
async function prepare(args, env, { now = Date.now, cwd = process.cwd() }) {
|
|
56
|
+
const { channel, from } = parse(args, cwd);
|
|
57
|
+
const serverUrl = configuredBase(env, "TIDE_SERVER_URL");
|
|
58
|
+
const assetBase = configuredBase(env, "TIDE_ASSET_URL");
|
|
59
|
+
const artifact = await readProjectExport(from);
|
|
60
|
+
const writer = await openWriter(env, { now, app: artifact });
|
|
61
|
+
const exported = artifact.platforms.map((p) => ({
|
|
62
|
+
...p,
|
|
63
|
+
key: replyKey(channel, p.runtime, p.platform),
|
|
64
|
+
}));
|
|
65
|
+
const { keys, bases } = await recordedKeys(writer.bucket, { targets: exported });
|
|
66
|
+
for (const { platform, assets } of exported) {
|
|
67
|
+
for (const a of assets)
|
|
68
|
+
addKey(keys, a.key, a.hash, `this export (${platform})`);
|
|
69
|
+
}
|
|
70
|
+
const { targets, local, checkCertificate } = await readTargets(writer, exported, now);
|
|
71
|
+
const records = [];
|
|
72
|
+
const writes = [];
|
|
73
|
+
for (const t of targets) {
|
|
74
|
+
const draft = {
|
|
75
|
+
channel,
|
|
76
|
+
runtimeVersion: t.runtime,
|
|
77
|
+
platform: t.platform,
|
|
78
|
+
serverUrl,
|
|
79
|
+
assetUrl: assetBase,
|
|
80
|
+
launchAsset: { hash: t.bundle.hash },
|
|
81
|
+
assets: t.assets.map((a) => ({ key: a.key, hash: a.hash, fileExtension: `.${a.ext}` })),
|
|
82
|
+
extra: { expoClient: artifact.expoClient },
|
|
83
|
+
};
|
|
84
|
+
const c = await compose(writer, draft, createdAtFloor(local, t.previous), t);
|
|
85
|
+
records.push(...c.records);
|
|
86
|
+
writes.push(c.pointer);
|
|
87
|
+
}
|
|
88
|
+
const stored = await storedContent(writer.bucket);
|
|
89
|
+
// Before the plan, so its fresh certificate check also covers the time spent diffing.
|
|
90
|
+
const patches = await preparePatches(writer.bucket, exported, bases, stored);
|
|
91
|
+
return {
|
|
92
|
+
ready: await plan(writer, checkCertificate, records, writes),
|
|
93
|
+
exported,
|
|
94
|
+
stored,
|
|
95
|
+
patches,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Reads each item's pointer, then the bucket's time, refusing a local clock more than a minute from it or a
|
|
100
|
+
* certificate invalid by either clock. Returns the pointers and the one local sample every new time comes from.
|
|
101
|
+
*/
|
|
102
|
+
export async function readTargets(writer, items, now) {
|
|
103
|
+
const { bucket } = writer;
|
|
104
|
+
const targets = [];
|
|
105
|
+
for (const t of items)
|
|
106
|
+
targets.push({ ...t, previous: await readPointer(bucket, t.key) });
|
|
107
|
+
const bucketTime = await bucket.time();
|
|
108
|
+
const local = now();
|
|
109
|
+
if (Math.abs(local - bucketTime) > MAX_SKEW_MS) {
|
|
110
|
+
throw new PublishError("the local clock differs from the bucket's by more than a minute: fix the clock");
|
|
111
|
+
}
|
|
112
|
+
await writer.checkCertificate(local);
|
|
113
|
+
await writer.checkCertificate(bucketTime);
|
|
114
|
+
// The local clock last, so expiry during the bucket read still counts.
|
|
115
|
+
const checkCertificate = async () => {
|
|
116
|
+
await writer.checkCertificate(await bucket.time());
|
|
117
|
+
await writer.checkCertificate(now());
|
|
118
|
+
};
|
|
119
|
+
return { targets, local, checkCertificate };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Ends a plan once everything is signed: builds `reply/no-update` if the bucket lacks one, then checks the
|
|
123
|
+
* certificate afresh, so an expiry before the first write is a refusal.
|
|
124
|
+
*/
|
|
125
|
+
export async function plan(writer, checkCertificate, records, pointers) {
|
|
126
|
+
const noUpdate = writer.noUpdate ? undefined : await noUpdateReply(writer);
|
|
127
|
+
await checkCertificate();
|
|
128
|
+
return { writer, noUpdate, records, pointers, checkCertificate };
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Writes `reply/no-update` if needed, the records, then each pointer. The certificate is re-checked before a pointer
|
|
132
|
+
* only once a PUT has been attempted since the plan's final check, so every failure here follows a write.
|
|
133
|
+
*/
|
|
134
|
+
export async function commit({ writer, noUpdate, records, pointers, checkCertificate }, out) {
|
|
135
|
+
const { bucket } = writer;
|
|
136
|
+
let attempted = false;
|
|
137
|
+
if (noUpdate !== undefined) {
|
|
138
|
+
attempted = true;
|
|
139
|
+
await writing(NO_UPDATE_KEY, () => ensureNoUpdate(writer, noUpdate));
|
|
140
|
+
}
|
|
141
|
+
for (const [key, bytes] of records) {
|
|
142
|
+
attempted = true;
|
|
143
|
+
const etag = await writing(key, () => bucket.putIfAbsent(key, bytes), "no pointer names it yet");
|
|
144
|
+
if (etag === undefined)
|
|
145
|
+
throw new PublishError(`${key} already exists`);
|
|
146
|
+
}
|
|
147
|
+
for (const p of pointers) {
|
|
148
|
+
if (attempted)
|
|
149
|
+
await checkCertificate();
|
|
150
|
+
attempted = true;
|
|
151
|
+
await writePointer(bucket, p.key, p.reply, p.previous);
|
|
152
|
+
out(p.line);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function parse(args, cwd) {
|
|
156
|
+
const { values } = parsed(PUBLISH_USAGE, () => parseArgs({
|
|
157
|
+
args: [...args],
|
|
158
|
+
options: { from: { type: "string" }, channel: { type: "string" } },
|
|
159
|
+
strict: true,
|
|
160
|
+
allowPositionals: false,
|
|
161
|
+
}));
|
|
162
|
+
const { from, channel } = values;
|
|
163
|
+
if (from === undefined || channel === undefined) {
|
|
164
|
+
throw new PublishError(`--from and --channel are required; ${PUBLISH_USAGE}`);
|
|
165
|
+
}
|
|
166
|
+
replyKey(channel, "1", "ios"); // Refuses a bad channel before the export is read.
|
|
167
|
+
return { channel, from: resolve(cwd, from) };
|
|
168
|
+
}
|
|
169
|
+
/** Returns `parse()`, turning parseArgs' own error into a usage refusal. */
|
|
170
|
+
export function parsed(usage, parse) {
|
|
171
|
+
try {
|
|
172
|
+
return parse();
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
throw new PublishError(`unrecognised arguments; ${usage}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function configuredBase(env, name) {
|
|
179
|
+
const value = env[name];
|
|
180
|
+
if (value === undefined || value === "") {
|
|
181
|
+
throw new PublishError(`${name} is not set: Tide writes it into every manifest`);
|
|
182
|
+
}
|
|
183
|
+
const base = urlBase(value);
|
|
184
|
+
if (base === undefined) {
|
|
185
|
+
throw new PublishError(`${name} must be an https URL with no credentials, query or fragment (http only to a loopback IP)`);
|
|
186
|
+
}
|
|
187
|
+
return base;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns the asset keys every retained release record and registered build names, the release records stored at
|
|
191
|
+
* `wanted` keys, and the bases of each `targets` runtime and platform. A record that cannot be read refuses, since
|
|
192
|
+
* the key check cannot be proven without it.
|
|
193
|
+
*/
|
|
194
|
+
export async function recordedKeys(bucket, { wanted = [], targets = [], } = {}) {
|
|
195
|
+
const keys = new Map();
|
|
196
|
+
const records = new Map();
|
|
197
|
+
const bases = [];
|
|
198
|
+
const created = new Map(); // Update ID to createdAt, for target pairs only.
|
|
199
|
+
const targeted = (r) => targets.some((t) => t.runtime === r.runtimeVersion && t.platform === r.platform);
|
|
200
|
+
for (const key of await keysUnder(bucket, RECORD_PREFIX)) {
|
|
201
|
+
const record = await readReleaseRecord(bucket, key);
|
|
202
|
+
if (record === undefined)
|
|
203
|
+
continue;
|
|
204
|
+
for (const a of record.assets)
|
|
205
|
+
addKey(keys, a.key, a.hash, key);
|
|
206
|
+
// Others are dropped, so memory follows the key index rather than the release history.
|
|
207
|
+
if (wanted.includes(key))
|
|
208
|
+
records.set(key, record);
|
|
209
|
+
if (targeted(record))
|
|
210
|
+
created.set(record.updateId, record.createdAt);
|
|
211
|
+
}
|
|
212
|
+
for (const key of await keysUnder(bucket, BASE_PREFIX)) {
|
|
213
|
+
const base = await readBaseRecord(bucket, key);
|
|
214
|
+
if (base === undefined)
|
|
215
|
+
continue;
|
|
216
|
+
if (base.kind === "embedded")
|
|
217
|
+
for (const a of base.assets)
|
|
218
|
+
addKey(keys, a.key, a.hash, key);
|
|
219
|
+
if (!targeted(base))
|
|
220
|
+
continue;
|
|
221
|
+
const createdAt = created.get(base.updateId);
|
|
222
|
+
// Writers store the release record first, so only a damaged bucket lacks it.
|
|
223
|
+
if (base.kind === "update" && createdAt === undefined) {
|
|
224
|
+
throw new PublishError(`${key} has no release record ${recordKey(base.updateId)}`);
|
|
225
|
+
}
|
|
226
|
+
const { kind, runtimeVersion, platform } = base;
|
|
227
|
+
bases.push({ key, kind, runtimeVersion, platform, hash: base.launchAsset.hash, createdAt });
|
|
228
|
+
}
|
|
229
|
+
return { keys, records, bases };
|
|
230
|
+
}
|
|
231
|
+
/** Returns the keys listed under `prefix`; a name outside the key grammar refuses. */
|
|
232
|
+
async function keysUnder(bucket, prefix) {
|
|
233
|
+
return (await bucket.list(prefix)).map((entry) => {
|
|
234
|
+
if (!("key" in entry))
|
|
235
|
+
throw new PublishError(`${prefix}: an object name outside the key grammar`);
|
|
236
|
+
return entry.key;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/** Records that `key` names `hash`; a key seen with another hash is refused, whichever source came first. */
|
|
240
|
+
export function addKey(keys, key, hash, source) {
|
|
241
|
+
const seen = keys.get(key.toLowerCase());
|
|
242
|
+
if (seen === undefined)
|
|
243
|
+
keys.set(key.toLowerCase(), { hash, source });
|
|
244
|
+
else if (seen.hash !== hash) {
|
|
245
|
+
throw new PublishError(`asset key ${key} names different bytes in ${seen.source} and ${source}: a key must never change its bytes`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/** Builds and signs a new update from `draft` under a fresh ID: its records and its write to `target`. */
|
|
249
|
+
export async function compose(writer, draft, createdAtMs, { key, previous }) {
|
|
250
|
+
const updateId = randomUUID();
|
|
251
|
+
const date = new Date(createdAtMs);
|
|
252
|
+
const createdAt = date.toISOString();
|
|
253
|
+
const manifest = buildManifest({
|
|
254
|
+
id: updateId,
|
|
255
|
+
createdAt: date,
|
|
256
|
+
runtimeVersion: draft.runtimeVersion,
|
|
257
|
+
launchAsset: {
|
|
258
|
+
hash: draft.launchAsset.hash,
|
|
259
|
+
contentType: assetContentType("bundle"),
|
|
260
|
+
fileExtension: ".bundle",
|
|
261
|
+
url: launchUrl(draft.serverUrl, draft.launchAsset.hash),
|
|
262
|
+
},
|
|
263
|
+
assets: draft.assets.map((a) => ({
|
|
264
|
+
hash: a.hash,
|
|
265
|
+
key: a.key,
|
|
266
|
+
contentType: assetContentType(a.fileExtension.slice(1)),
|
|
267
|
+
fileExtension: a.fileExtension,
|
|
268
|
+
url: assetUrl(draft.assetUrl, a.hash),
|
|
269
|
+
})),
|
|
270
|
+
metadata: { channel: draft.channel },
|
|
271
|
+
extra: draft.extra,
|
|
272
|
+
});
|
|
273
|
+
const signature = await expoSignature(manifest, writer.signer);
|
|
274
|
+
const { boundary, body } = encodeMultipart([{ name: "manifest", body: manifest, signature }]);
|
|
275
|
+
const reply = encodeStoredReply({
|
|
276
|
+
kind: "manifest",
|
|
277
|
+
updateId,
|
|
278
|
+
boundary,
|
|
279
|
+
manifestFilters: encodeSfvDictionary({ channel: draft.channel }),
|
|
280
|
+
body,
|
|
281
|
+
});
|
|
282
|
+
const base = encodeBaseRecord({
|
|
283
|
+
kind: "update",
|
|
284
|
+
updateId,
|
|
285
|
+
runtimeVersion: draft.runtimeVersion,
|
|
286
|
+
platform: draft.platform,
|
|
287
|
+
launchAsset: draft.launchAsset,
|
|
288
|
+
});
|
|
289
|
+
return {
|
|
290
|
+
records: [
|
|
291
|
+
[recordKey(updateId), encodeRecord({ ...draft, updateId, createdAt })],
|
|
292
|
+
[baseKey(updateId), base],
|
|
293
|
+
],
|
|
294
|
+
pointer: { key, previous, reply, line: `${key} ${updateId} ${createdAt}` },
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** Builds and signs the stored `reply/no-update`. */
|
|
298
|
+
async function noUpdateReply(writer) {
|
|
299
|
+
const directive = buildNoUpdateAvailable();
|
|
300
|
+
const signature = await expoSignature(directive, writer.signer);
|
|
301
|
+
const { boundary, body } = encodeMultipart([{ name: "directive", body: directive, signature }]);
|
|
302
|
+
return encodeStoredReply({ kind: "noUpdateAvailable", boundary, body });
|
|
303
|
+
}
|
|
304
|
+
/** Creates `reply/no-update` from `bytes`, or accepts one another writer created under this identity. */
|
|
305
|
+
async function ensureNoUpdate(writer, bytes) {
|
|
306
|
+
if ((await writer.bucket.putIfAbsent(NO_UPDATE_KEY, bytes)) !== undefined)
|
|
307
|
+
return;
|
|
308
|
+
if (!(await writer.checkNoUpdate())) {
|
|
309
|
+
throw new PublishError(`${NO_UPDATE_KEY} was removed by another writer during the publish`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Uploads every bundle and asset missing from `stored`, as immutable public content. A lost create means another
|
|
314
|
+
* writer stored the same content-addressed bytes, which is accepted.
|
|
315
|
+
*/
|
|
316
|
+
async function uploadObjects(bucket, exported, stored) {
|
|
317
|
+
const existing = new Set(stored.keys());
|
|
318
|
+
const upload = async (key, bytes, contentType) => {
|
|
319
|
+
if (existing.has(key))
|
|
320
|
+
return;
|
|
321
|
+
existing.add(key);
|
|
322
|
+
await bucket.putIfAbsent(key, bytes, contentType);
|
|
323
|
+
};
|
|
324
|
+
for (const { bundle, assets } of exported) {
|
|
325
|
+
await upload(`bundles/${contentKey(bundle.hash)}`, bundle.bytes, assetContentType("bundle"));
|
|
326
|
+
for (const a of assets) {
|
|
327
|
+
await upload(`assets/${contentKey(a.hash)}`, a.bytes, assetContentType(a.ext));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/** Returns every stored bundle and asset key, objects the server can serve, with its size. */
|
|
332
|
+
export async function storedContent(bucket) {
|
|
333
|
+
const entries = [...(await bucket.list("assets/")), ...(await bucket.list("bundles/"))];
|
|
334
|
+
return new Map(entries.flatMap((e) => ("key" in e ? [[e.key, e.size]] : [])));
|
|
335
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Env } from "./keys.ts";
|
|
2
|
+
import type { PublishOptions } from "./publish.ts";
|
|
3
|
+
export declare const REGISTER_BUILD_USAGE = "usage: tide register-build --platform ios|android --runtime-version <version> --manifest <app.manifest> --bundle <file> [--assets-dir <dir>]";
|
|
4
|
+
/**
|
|
5
|
+
* Records a store build's embedded bundle as `bases/<id>`, its assets joining the asset key identity check. Exit codes
|
|
6
|
+
* as publish: 0 registered (or already), 2 refused before any write, 1 failed after a write was attempted.
|
|
7
|
+
*/
|
|
8
|
+
export declare function registerBuild(args: readonly string[], env: Env, options: PublishOptions): Promise<0 | 1 | 2>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readdirSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { MAX_BASE_BYTES, ProtocolError, assetContentType, baseKey, contentKey, encodeBaseRecord, replyKey, sha256Base64Url, } from "@tide-ota/protocol";
|
|
6
|
+
import { buildIdentity, checkBucket, member } from "./keys.js";
|
|
7
|
+
import { writing } from "./pointer.js";
|
|
8
|
+
import { readRegular } from "./publish-export.js";
|
|
9
|
+
import { PublishError, platformOf } from "./publish-record.js";
|
|
10
|
+
import { addKey, execute, parsed, recordedKeys } from "./publish.js";
|
|
11
|
+
import { bucketFromEnv } from "./storage.js";
|
|
12
|
+
export const REGISTER_BUILD_USAGE = "usage: tide register-build --platform ios|android --runtime-version <version> --manifest <app.manifest> --bundle <file> [--assets-dir <dir>]";
|
|
13
|
+
const MANIFEST_LIMIT = 16 * 1024 * 1024;
|
|
14
|
+
const MD5 = /^[0-9a-f]{32}$/;
|
|
15
|
+
const RERUN = "run register-build again before any other writer";
|
|
16
|
+
const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
17
|
+
/**
|
|
18
|
+
* Records a store build's embedded bundle as `bases/<id>`, its assets joining the asset key identity check. Exit codes
|
|
19
|
+
* as publish: 0 registered (or already), 2 refused before any write, 1 failed after a write was attempted.
|
|
20
|
+
*/
|
|
21
|
+
export async function registerBuild(args, env, options) {
|
|
22
|
+
return await execute(options, () => prepare(args, env, options));
|
|
23
|
+
}
|
|
24
|
+
async function prepare(args, env, { out, now = Date.now, cwd = process.cwd() }) {
|
|
25
|
+
const given = parse(args);
|
|
26
|
+
replyKey("main", given.runtime, given.platform); // Refuses a bad runtime.
|
|
27
|
+
// The bucket must serve the shipped builds' identity, so a build is never registered in another environment's.
|
|
28
|
+
const identity = await buildIdentity(env, now());
|
|
29
|
+
const bucket = bucketFromEnv(env);
|
|
30
|
+
const { id, key, keys } = readManifest(resolve(cwd, given.manifest));
|
|
31
|
+
const bundle = readRegular(resolve(cwd, given.bundle), Infinity, "--bundle");
|
|
32
|
+
const launch = await sha256Base64Url(bundle);
|
|
33
|
+
const assets = await embeddedAssets(keys, given.assetsDir === undefined ? undefined : resolve(cwd, given.assetsDir));
|
|
34
|
+
const record = encodeBaseRecord({
|
|
35
|
+
kind: "embedded",
|
|
36
|
+
updateId: id,
|
|
37
|
+
runtimeVersion: given.runtime,
|
|
38
|
+
platform: given.platform,
|
|
39
|
+
launchAsset: { hash: launch },
|
|
40
|
+
assets,
|
|
41
|
+
});
|
|
42
|
+
// An empty bucket serves no identity, so its first publish could pin another under this build's asset keys.
|
|
43
|
+
if (!(await checkBucket(bucket, identity))) {
|
|
44
|
+
throw new PublishError("reply/no-update is absent: publish before register-build, so the bucket serves the builds' identity");
|
|
45
|
+
}
|
|
46
|
+
const stored = await bucket.get(key, MAX_BASE_BYTES);
|
|
47
|
+
if (stored !== undefined && !Buffer.from(record).equals(stored.bytes)) {
|
|
48
|
+
throw new PublishError(`${key} holds another build`);
|
|
49
|
+
}
|
|
50
|
+
const { keys: recorded } = await recordedKeys(bucket);
|
|
51
|
+
for (const a of assets)
|
|
52
|
+
addKey(recorded, a.key, a.hash, "this build");
|
|
53
|
+
const bundleKey = `bundles/${contentKey(launch)}`;
|
|
54
|
+
return async () => {
|
|
55
|
+
// The bundle is written first, so a stored identical base proves it is there; a lost create stored the same bytes.
|
|
56
|
+
if (stored === undefined) {
|
|
57
|
+
await writing(bundleKey, () => bucket.putIfAbsent(bundleKey, bundle, assetContentType("bundle")), RERUN);
|
|
58
|
+
await createBase(bucket, key, record);
|
|
59
|
+
}
|
|
60
|
+
out(`${key} ${bundleKey}`);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Creates `key`; a lost create succeeds only when the stored bytes are `record`. */
|
|
64
|
+
async function createBase(bucket, key, record) {
|
|
65
|
+
if ((await writing(key, () => bucket.putIfAbsent(key, record), RERUN)) !== undefined)
|
|
66
|
+
return;
|
|
67
|
+
const stored = await writing(key, () => bucket.get(key, MAX_BASE_BYTES), RERUN);
|
|
68
|
+
if (stored === undefined || !Buffer.from(record).equals(stored.bytes)) {
|
|
69
|
+
throw new PublishError(`${key} holds another build`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function parse(args) {
|
|
73
|
+
const { values } = parsed(REGISTER_BUILD_USAGE, () => parseArgs({
|
|
74
|
+
args: [...args],
|
|
75
|
+
options: {
|
|
76
|
+
platform: { type: "string", multiple: true },
|
|
77
|
+
"runtime-version": { type: "string", multiple: true },
|
|
78
|
+
manifest: { type: "string", multiple: true },
|
|
79
|
+
bundle: { type: "string", multiple: true },
|
|
80
|
+
"assets-dir": { type: "string", multiple: true },
|
|
81
|
+
},
|
|
82
|
+
strict: true,
|
|
83
|
+
allowPositionals: false,
|
|
84
|
+
}));
|
|
85
|
+
const once = (name) => {
|
|
86
|
+
const list = values[name];
|
|
87
|
+
if (list !== undefined && list.length > 1)
|
|
88
|
+
throw new PublishError(`--${name} is given twice`);
|
|
89
|
+
return list?.[0];
|
|
90
|
+
};
|
|
91
|
+
const required = (name) => {
|
|
92
|
+
const value = once(name);
|
|
93
|
+
if (value === undefined)
|
|
94
|
+
throw new PublishError(`--${name} is required; ${REGISTER_BUILD_USAGE}`);
|
|
95
|
+
return value;
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
platform: platformOf(required("platform")),
|
|
99
|
+
runtime: required("runtime-version"),
|
|
100
|
+
manifest: required("manifest"),
|
|
101
|
+
bundle: required("bundle"),
|
|
102
|
+
assetsDir: once("assets-dir"),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Returns the embedded update ID, its base record key and each asset's key from a build's `app.manifest`. */
|
|
106
|
+
function readManifest(path) {
|
|
107
|
+
let manifest;
|
|
108
|
+
try {
|
|
109
|
+
manifest = JSON.parse(utf8.decode(readRegular(path, MANIFEST_LIMIT, "--manifest")));
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
if (e instanceof PublishError)
|
|
113
|
+
throw e;
|
|
114
|
+
throw new PublishError("--manifest is not UTF-8 JSON");
|
|
115
|
+
}
|
|
116
|
+
const id = member(manifest, "id");
|
|
117
|
+
let key;
|
|
118
|
+
try {
|
|
119
|
+
key = baseKey(id);
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
if (!(e instanceof ProtocolError))
|
|
123
|
+
throw e;
|
|
124
|
+
throw new PublishError("the manifest's id is not a lowercase UUID");
|
|
125
|
+
}
|
|
126
|
+
const assets = member(manifest, "assets");
|
|
127
|
+
if (!Array.isArray(assets))
|
|
128
|
+
throw new PublishError("the manifest's assets is not an array");
|
|
129
|
+
const keys = assets.map((a, i) => {
|
|
130
|
+
const key = member(a, "packagerHash");
|
|
131
|
+
if (typeof key !== "string" || !MD5.test(key)) {
|
|
132
|
+
throw new PublishError(`the manifest's assets[${i}].packagerHash is not 32 lowercase hex`);
|
|
133
|
+
}
|
|
134
|
+
return key;
|
|
135
|
+
});
|
|
136
|
+
return { id, key, keys };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Returns each key with the SHA-256 of the file under `dir` whose MD5 it is. Files are found by content, never by
|
|
140
|
+
* location, since a packaged copy may be re-encoded; symlinks and special files are skipped.
|
|
141
|
+
*/
|
|
142
|
+
async function embeddedAssets(keys, dir) {
|
|
143
|
+
if (keys.length === 0)
|
|
144
|
+
return [];
|
|
145
|
+
if (dir === undefined)
|
|
146
|
+
throw new PublishError("the manifest lists assets: give --assets-dir");
|
|
147
|
+
const byMd5 = new Map();
|
|
148
|
+
const walk = async (rel) => {
|
|
149
|
+
let entries;
|
|
150
|
+
try {
|
|
151
|
+
entries = readdirSync(join(dir, rel), { withFileTypes: true });
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
throw new PublishError(`--assets-dir ${JSON.stringify(rel || ".")} cannot be listed`);
|
|
155
|
+
}
|
|
156
|
+
for (const e of entries) {
|
|
157
|
+
const path = rel === "" ? e.name : `${rel}/${e.name}`;
|
|
158
|
+
if (e.isDirectory())
|
|
159
|
+
await walk(path);
|
|
160
|
+
if (!e.isFile())
|
|
161
|
+
continue;
|
|
162
|
+
const bytes = readRegular(join(dir, path), Infinity, `--assets-dir ${JSON.stringify(path)}`);
|
|
163
|
+
const md5 = createHash("md5").update(bytes).digest("hex");
|
|
164
|
+
const hash = await sha256Base64Url(bytes);
|
|
165
|
+
const seen = byMd5.get(md5);
|
|
166
|
+
// MD5 collides; SHA-256 is the identity the key check compares.
|
|
167
|
+
if (seen !== undefined && seen !== hash) {
|
|
168
|
+
throw new PublishError(`--assets-dir holds two different files with MD5 ${md5}`);
|
|
169
|
+
}
|
|
170
|
+
byMd5.set(md5, hash);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
await walk("");
|
|
174
|
+
return keys.map((key) => {
|
|
175
|
+
const hash = byMd5.get(key);
|
|
176
|
+
if (hash === undefined)
|
|
177
|
+
throw new PublishError(`no file in --assets-dir has the MD5 ${key}`);
|
|
178
|
+
return { key, hash };
|
|
179
|
+
});
|
|
180
|
+
}
|
package/dist/s3.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { S3Config } from "@tide-ota/protocol";
|
|
2
|
+
import type { Bucket } from "./bucket.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Returns a bucket over an S3-compatible API (R2, AWS S3), path-style. One deadline covers each operation, body
|
|
5
|
+
* included. A 412 on a conditional PUT is a lost race; every other failure is a `BucketError` built by `S3Error`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function s3Bucket(config: S3Config, options?: {
|
|
8
|
+
readonly timeoutMs?: number;
|
|
9
|
+
}): Bucket;
|