@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
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { gzipSync } from "node:zlib";
|
|
2
|
+
import { ProtocolError, applyBsdiff, contentKey, patchKey, sha256Base64Url, } from "@tide-ota/protocol";
|
|
3
|
+
import { MAX_BSDIFF_INPUT, makeBsdiff } from "./bsdiff.js";
|
|
4
|
+
import { writing } from "./pointer.js";
|
|
5
|
+
import { PublishError } from "./publish-record.js";
|
|
6
|
+
/** How many of a runtime and platform's newest OTA updates a publish patches from, beside every embedded build. */
|
|
7
|
+
const PATCH_UPDATE_BASES = 5;
|
|
8
|
+
/**
|
|
9
|
+
* Returns the patches to store for each target's new bundle, from every embedded build and the newest
|
|
10
|
+
* `PATCH_UPDATE_BASES` updates of its runtime and platform. `stored` is the `bundles/` listing by key, with sizes.
|
|
11
|
+
* Refuses a base whose bundle is not stored and a patch that fails its self-check; writes nothing.
|
|
12
|
+
*/
|
|
13
|
+
export async function preparePatches(bucket, targets, bases, stored) {
|
|
14
|
+
const patches = new Map();
|
|
15
|
+
for (const t of targets) {
|
|
16
|
+
const toHex = contentKey(t.bundle.hash);
|
|
17
|
+
const pair = bases.filter((b) => b.runtimeVersion === t.runtime && b.platform === t.platform);
|
|
18
|
+
const newest = pair
|
|
19
|
+
.filter((b) => b.kind === "update")
|
|
20
|
+
.toSorted((a, b) => order(b.createdAt, a.createdAt) || order(b.key, a.key))
|
|
21
|
+
.slice(0, PATCH_UPDATE_BASES);
|
|
22
|
+
// The patch key is per bundle pair: bases sharing a launch hash share one patch.
|
|
23
|
+
const from = new Map();
|
|
24
|
+
for (const b of [...pair.filter((b) => b.kind === "embedded"), ...newest]) {
|
|
25
|
+
if (b.hash !== t.bundle.hash && !from.has(b.hash))
|
|
26
|
+
from.set(b.hash, b);
|
|
27
|
+
}
|
|
28
|
+
let gzipped;
|
|
29
|
+
for (const [hash, base] of from) {
|
|
30
|
+
const fromHex = contentKey(hash);
|
|
31
|
+
const key = patchKey(fromHex, toHex);
|
|
32
|
+
if (patches.has(key) || (await bucket.list(key)).some((e) => "key" in e && e.key === key)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const bundleKey = `bundles/${fromHex}`;
|
|
36
|
+
const size = stored.get(bundleKey);
|
|
37
|
+
// Checked before the size cap, so an oversized new bundle cannot hide a missing base.
|
|
38
|
+
if (size === undefined)
|
|
39
|
+
throw missing(base, bundleKey);
|
|
40
|
+
if (size > MAX_BSDIFF_INPUT || t.bundle.bytes.length > MAX_BSDIFF_INPUT)
|
|
41
|
+
continue;
|
|
42
|
+
const object = await bucket.get(bundleKey, MAX_BSDIFF_INPUT);
|
|
43
|
+
if (object === undefined)
|
|
44
|
+
throw missing(base, bundleKey);
|
|
45
|
+
const patch = makeBsdiff(object.bytes, t.bundle.bytes);
|
|
46
|
+
await checkPatch(object.bytes, patch, t.bundle, key);
|
|
47
|
+
// gzip is the encoding every client accepts; a patch must save at least half of that transfer.
|
|
48
|
+
gzipped ??= gzipSync(t.bundle.bytes, { level: 9 }).length;
|
|
49
|
+
if (patch.length * 2 <= gzipped)
|
|
50
|
+
patches.set(key, patch);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return patches;
|
|
54
|
+
}
|
|
55
|
+
/** Refuses `patch` unless protocol `applyBsdiff` turns `base` into bytes whose SHA-256 is `target.hash`. */
|
|
56
|
+
export async function checkPatch(base, patch, target, key) {
|
|
57
|
+
let reason;
|
|
58
|
+
try {
|
|
59
|
+
const applied = applyBsdiff(base, patch, target.bytes.length);
|
|
60
|
+
if ((await sha256Base64Url(applied)) === target.hash)
|
|
61
|
+
return;
|
|
62
|
+
reason = "the result does not hash to the new bundle";
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
if (!(e instanceof ProtocolError))
|
|
66
|
+
throw e;
|
|
67
|
+
reason = e.message;
|
|
68
|
+
}
|
|
69
|
+
throw new PublishError(`${key} failed its self-check: ${reason}`);
|
|
70
|
+
}
|
|
71
|
+
/** Stores each patch; a lost create holds the same bytes, since `makeBsdiff` depends only on its inputs. */
|
|
72
|
+
export async function uploadPatches(bucket, patches) {
|
|
73
|
+
for (const [key, bytes] of patches) {
|
|
74
|
+
await writing(key, () => bucket.putIfAbsent(key, bytes), "a late copy is the same checked patch");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const order = (a = "", b = "") => (a < b ? -1 : a > b ? 1 : 0);
|
|
78
|
+
const missing = (base, bundleKey) => new PublishError(`${base.key} names ${bundleKey}, which is not stored`);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { BaseRecord, JsonObject } from "@tide-ota/protocol";
|
|
2
|
+
import type { Bucket } from "./bucket.ts";
|
|
3
|
+
/** A command's refusal or failure. Its message names keys, variables and rules, never stored data. */
|
|
4
|
+
export declare class PublishError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export type Platform = "ios" | "android";
|
|
8
|
+
/** Returns the `--platform` values, both by default; a repeat is refused. */
|
|
9
|
+
export declare function platformsOf(values: readonly string[] | undefined): Platform[];
|
|
10
|
+
/** Returns `value` as a platform, or refuses it. */
|
|
11
|
+
export declare function platformOf(value: string): Platform;
|
|
12
|
+
/** What `updates/<updateId>.json` holds: enough to rebuild the update's manifest byte for byte, URLs included. */
|
|
13
|
+
export interface ReleaseRecord {
|
|
14
|
+
readonly updateId: string;
|
|
15
|
+
readonly channel: string;
|
|
16
|
+
readonly runtimeVersion: string;
|
|
17
|
+
readonly platform: Platform;
|
|
18
|
+
readonly createdAt: string;
|
|
19
|
+
readonly serverUrl: string;
|
|
20
|
+
readonly assetUrl: string;
|
|
21
|
+
readonly launchAsset: {
|
|
22
|
+
readonly hash: string;
|
|
23
|
+
};
|
|
24
|
+
readonly assets: readonly RecordedAsset[];
|
|
25
|
+
readonly extra: JsonObject;
|
|
26
|
+
}
|
|
27
|
+
interface RecordedAsset {
|
|
28
|
+
readonly key: string;
|
|
29
|
+
readonly hash: string;
|
|
30
|
+
readonly fileExtension: string;
|
|
31
|
+
}
|
|
32
|
+
export declare const RECORD_PREFIX = "updates/";
|
|
33
|
+
export declare const BASE_PREFIX = "bases/";
|
|
34
|
+
export declare const recordKey: (updateId: string) => string;
|
|
35
|
+
/** The launch asset URL shape, permanent once published. */
|
|
36
|
+
export declare const launchUrl: (serverUrl: string, hash: string) => string;
|
|
37
|
+
/** The asset URL shape, permanent once published. */
|
|
38
|
+
export declare const assetUrl: (assetBase: string, hash: string) => string;
|
|
39
|
+
/**
|
|
40
|
+
* Returns `value` as a URL base without a trailing `/`, or undefined unless it is an absolute `https:` URL (or
|
|
41
|
+
* `http:` to a loopback IP literal) with no credentials, query or fragment.
|
|
42
|
+
*/
|
|
43
|
+
export declare function urlBase(value: string): string | undefined;
|
|
44
|
+
/** Returns the record bytes: one JSON object, keys in a fixed order, no trailing newline, at most 2 MiB. */
|
|
45
|
+
export declare function encodeRecord(record: ReleaseRecord): Uint8Array<ArrayBuffer>;
|
|
46
|
+
/** Returns the record `bytes` hold; anything `encodeRecord` would not have written is refused. */
|
|
47
|
+
export declare function decodeRecord(bytes: Uint8Array): ReleaseRecord;
|
|
48
|
+
/** Returns the release record at `key`, undefined when absent; one that does not decode or names another ID refuses. */
|
|
49
|
+
export declare function readReleaseRecord(bucket: Pick<Bucket, "get">, key: string): Promise<ReleaseRecord | undefined>;
|
|
50
|
+
/** Returns the base record at `key`, undefined when absent; one that does not decode or names another ID refuses. */
|
|
51
|
+
export declare function readBaseRecord(bucket: Pick<Bucket, "get">, key: string): Promise<BaseRecord | undefined>;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { MAX_BASE_BYTES, MAX_REPLY_BYTES, ProtocolError, assetHash, baseKey, contentKey, decodeBaseRecord, replyKey, } from "@tide-ota/protocol";
|
|
2
|
+
import { isKey } from "./bucket.js";
|
|
3
|
+
import { isObject } from "./keys.js";
|
|
4
|
+
import { isTime } from "./status.js";
|
|
5
|
+
/** A command's refusal or failure. Its message names keys, variables and rules, never stored data. */
|
|
6
|
+
export class PublishError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "PublishError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Returns the `--platform` values, both by default; a repeat is refused. */
|
|
13
|
+
export function platformsOf(values) {
|
|
14
|
+
const platforms = [];
|
|
15
|
+
for (const p of (values ?? ["ios", "android"]).map(platformOf)) {
|
|
16
|
+
// Two writes over one read ETag would fail the run against its own pointer.
|
|
17
|
+
if (platforms.includes(p))
|
|
18
|
+
throw new PublishError(`--platform ${p} is given twice`);
|
|
19
|
+
platforms.push(p);
|
|
20
|
+
}
|
|
21
|
+
return platforms;
|
|
22
|
+
}
|
|
23
|
+
/** Returns `value` as a platform, or refuses it. */
|
|
24
|
+
export function platformOf(value) {
|
|
25
|
+
if (value !== "ios" && value !== "android")
|
|
26
|
+
throw new PublishError("--platform must be ios or android");
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
export const RECORD_PREFIX = "updates/";
|
|
30
|
+
export const BASE_PREFIX = "bases/";
|
|
31
|
+
const VERSION = 1;
|
|
32
|
+
const KEY = /^[A-Za-z0-9_-]{1,64}$/;
|
|
33
|
+
const FILE_EXTENSION = /^\.[A-Za-z0-9]{1,16}$/;
|
|
34
|
+
const LOOPBACK = /^(?:127(?:\.\d{1,3}){3}|\[::1\])$/;
|
|
35
|
+
const encoder = new TextEncoder();
|
|
36
|
+
const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
37
|
+
export const recordKey = (updateId) => `${RECORD_PREFIX}${updateId}.json`;
|
|
38
|
+
/** The launch asset URL shape, permanent once published. */
|
|
39
|
+
export const launchUrl = (serverUrl, hash) => `${serverUrl}/bundles/${contentKey(hash)}`;
|
|
40
|
+
/** The asset URL shape, permanent once published. */
|
|
41
|
+
export const assetUrl = (assetBase, hash) => `${assetBase}/assets/${contentKey(hash)}`;
|
|
42
|
+
/**
|
|
43
|
+
* Returns `value` as a URL base without a trailing `/`, or undefined unless it is an absolute `https:` URL (or
|
|
44
|
+
* `http:` to a loopback IP literal) with no credentials, query or fragment.
|
|
45
|
+
*/
|
|
46
|
+
export function urlBase(value) {
|
|
47
|
+
const url = URL.parse(value);
|
|
48
|
+
if (url === null ||
|
|
49
|
+
!(url.protocol === "https:" || (url.protocol === "http:" && LOOPBACK.test(url.hostname))) ||
|
|
50
|
+
url.username !== "" ||
|
|
51
|
+
url.password !== "" ||
|
|
52
|
+
url.search !== "" ||
|
|
53
|
+
url.hash !== "" ||
|
|
54
|
+
value.includes("?") ||
|
|
55
|
+
value.includes("#")) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
return url.href.replace(/\/$/, "");
|
|
59
|
+
}
|
|
60
|
+
/** Returns the record bytes: one JSON object, keys in a fixed order, no trailing newline, at most 2 MiB. */
|
|
61
|
+
export function encodeRecord(record) {
|
|
62
|
+
const r = record;
|
|
63
|
+
const fail = (field, rule) => new ProtocolError(`record.${field}`, rule);
|
|
64
|
+
if (typeof r.updateId !== "string" || !isKey(recordKey(r.updateId))) {
|
|
65
|
+
throw fail("updateId", "must be a lowercase UUID");
|
|
66
|
+
}
|
|
67
|
+
// Checked first: replyKey coerces its arguments, and an object without a usable toString throws a TypeError.
|
|
68
|
+
for (const field of ["channel", "runtimeVersion", "platform"]) {
|
|
69
|
+
if (typeof r[field] !== "string")
|
|
70
|
+
throw fail(field, "must be a string");
|
|
71
|
+
}
|
|
72
|
+
replyKey(r.channel, r.runtimeVersion, r.platform);
|
|
73
|
+
if (!isTime(r.createdAt))
|
|
74
|
+
throw fail("createdAt", "must be an ISO 8601 UTC time with milliseconds");
|
|
75
|
+
for (const field of ["serverUrl", "assetUrl"]) {
|
|
76
|
+
if (typeof r[field] !== "string" || urlBase(r[field]) !== r[field]) {
|
|
77
|
+
throw fail(field, "must be an https URL base without a trailing /");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!isObject(r.launchAsset))
|
|
81
|
+
throw fail("launchAsset", "must be an object");
|
|
82
|
+
const launchAsset = { hash: assetHash(r.launchAsset.hash, "record.launchAsset.hash") };
|
|
83
|
+
if (!Array.isArray(r.assets))
|
|
84
|
+
throw fail("assets", "must be an array");
|
|
85
|
+
const assets = r.assets.map((a, i) => {
|
|
86
|
+
if (!isObject(a))
|
|
87
|
+
throw fail(`assets[${i}]`, "must be an object");
|
|
88
|
+
if (typeof a.key !== "string" || !KEY.test(a.key)) {
|
|
89
|
+
throw fail(`assets[${i}].key`, "must be 1 to 64 of A-Z, a-z, 0-9, _ and -");
|
|
90
|
+
}
|
|
91
|
+
if (typeof a.fileExtension !== "string" || !FILE_EXTENSION.test(a.fileExtension)) {
|
|
92
|
+
throw fail(`assets[${i}].fileExtension`, "must be a dot and 1 to 16 of A-Z, a-z and 0-9");
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
key: a.key,
|
|
96
|
+
hash: assetHash(a.hash, `record.assets[${i}].hash`),
|
|
97
|
+
fileExtension: a.fileExtension,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
if (!isObject(r.extra))
|
|
101
|
+
throw fail("extra", "must be an object");
|
|
102
|
+
const bytes = new Uint8Array(encoder.encode(JSON.stringify({
|
|
103
|
+
version: VERSION,
|
|
104
|
+
updateId: r.updateId,
|
|
105
|
+
channel: r.channel,
|
|
106
|
+
runtimeVersion: r.runtimeVersion,
|
|
107
|
+
platform: r.platform,
|
|
108
|
+
createdAt: r.createdAt,
|
|
109
|
+
serverUrl: r.serverUrl,
|
|
110
|
+
assetUrl: r.assetUrl,
|
|
111
|
+
launchAsset,
|
|
112
|
+
assets,
|
|
113
|
+
extra: r.extra,
|
|
114
|
+
})));
|
|
115
|
+
if (bytes.length > MAX_REPLY_BYTES)
|
|
116
|
+
throw fail("", `is over ${MAX_REPLY_BYTES} bytes`);
|
|
117
|
+
return bytes;
|
|
118
|
+
}
|
|
119
|
+
/** Returns the record `bytes` hold; anything `encodeRecord` would not have written is refused. */
|
|
120
|
+
export function decodeRecord(bytes) {
|
|
121
|
+
let parsed;
|
|
122
|
+
try {
|
|
123
|
+
parsed = JSON.parse(utf8.decode(bytes));
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
throw new ProtocolError("record", "is not UTF-8 JSON");
|
|
127
|
+
}
|
|
128
|
+
if (!isObject(parsed))
|
|
129
|
+
throw new ProtocolError("record", "must be a JSON object");
|
|
130
|
+
if (parsed.version !== VERSION)
|
|
131
|
+
throw new ProtocolError("record.version", "is not supported");
|
|
132
|
+
const record = parsed;
|
|
133
|
+
const canonical = encodeRecord(record);
|
|
134
|
+
if (canonical.length !== bytes.length || canonical.some((b, i) => b !== bytes[i])) {
|
|
135
|
+
throw new ProtocolError("record", "is not in the form tide writes");
|
|
136
|
+
}
|
|
137
|
+
return record;
|
|
138
|
+
}
|
|
139
|
+
/** Returns the release record at `key`, undefined when absent; one that does not decode or names another ID refuses. */
|
|
140
|
+
export async function readReleaseRecord(bucket, key) {
|
|
141
|
+
const object = await bucket.get(key, MAX_REPLY_BYTES);
|
|
142
|
+
if (object === undefined)
|
|
143
|
+
return undefined;
|
|
144
|
+
let record;
|
|
145
|
+
try {
|
|
146
|
+
record = decodeRecord(object.bytes);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
if (!(e instanceof ProtocolError))
|
|
150
|
+
throw e;
|
|
151
|
+
throw new PublishError(`${key} is unreadable: ${e.message}`);
|
|
152
|
+
}
|
|
153
|
+
if (recordKey(record.updateId) !== key) {
|
|
154
|
+
throw new PublishError(`${key} is unreadable: its updateId differs from its name`);
|
|
155
|
+
}
|
|
156
|
+
return record;
|
|
157
|
+
}
|
|
158
|
+
/** Returns the base record at `key`, undefined when absent; one that does not decode or names another ID refuses. */
|
|
159
|
+
export async function readBaseRecord(bucket, key) {
|
|
160
|
+
const object = await bucket.get(key, MAX_BASE_BYTES);
|
|
161
|
+
if (object === undefined)
|
|
162
|
+
return undefined;
|
|
163
|
+
let record;
|
|
164
|
+
try {
|
|
165
|
+
record = decodeBaseRecord(object.bytes);
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
if (!(e instanceof ProtocolError))
|
|
169
|
+
throw e;
|
|
170
|
+
throw new PublishError(`${key} is unreadable: ${e.message}`);
|
|
171
|
+
}
|
|
172
|
+
if (baseKey(record.updateId) !== key) {
|
|
173
|
+
throw new PublishError(`${key} is unreadable: its updateId differs from its name`);
|
|
174
|
+
}
|
|
175
|
+
return record;
|
|
176
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Env } from "./keys.ts";
|
|
2
|
+
import type { PublishOptions } from "./publish.ts";
|
|
3
|
+
export declare const ROLLBACK_USAGE = "usage: tide rollback --embedded --channel <name> --runtime-version <version> [--platform ios|android]...\n tide rollback --to <updateId> [--to <updateId>]...";
|
|
4
|
+
export declare const REPUBLISH_USAGE = "usage: tide republish <updateId>... [--channel <name>] [--runtime-version <version>]";
|
|
5
|
+
/**
|
|
6
|
+
* Rolls pointers back: `--embedded` serves a signed `rollBackToEmbedded`, `--to` republishes each update onto its own
|
|
7
|
+
* pointer. Exit codes as publish: 0 every pointer written, 2 refused before any write, 1 failed after writes began.
|
|
8
|
+
*/
|
|
9
|
+
export declare function rollback(args: readonly string[], env: Env, options: PublishOptions): Promise<0 | 1 | 2>;
|
|
10
|
+
/**
|
|
11
|
+
* Republishes each stored update under a new ID with a fresh `createdAt`, onto `--channel` or its own channel. Exit
|
|
12
|
+
* codes as publish.
|
|
13
|
+
*/
|
|
14
|
+
export declare function republish(args: readonly string[], env: Env, options: PublishOptions): Promise<0 | 1 | 2>;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { buildRollBackToEmbedded, contentKey, encodeMultipart, encodeStoredReply, expoSignature, replyKey, } from "@tide-ota/protocol";
|
|
3
|
+
import { isKey } from "./bucket.js";
|
|
4
|
+
import { openWriter } from "./keys.js";
|
|
5
|
+
import { createdAtFloor } from "./pointer.js";
|
|
6
|
+
import { PublishError, platformsOf, recordKey } from "./publish-record.js";
|
|
7
|
+
import { commit, compose, execute, parsed, plan, readTargets, recordedKeys, storedContent, } from "./publish.js";
|
|
8
|
+
export const ROLLBACK_USAGE = `usage: tide rollback --embedded --channel <name> --runtime-version <version> [--platform ios|android]...
|
|
9
|
+
tide rollback --to <updateId> [--to <updateId>]...`;
|
|
10
|
+
export const REPUBLISH_USAGE = "usage: tide republish <updateId>... [--channel <name>] [--runtime-version <version>]";
|
|
11
|
+
/**
|
|
12
|
+
* Rolls pointers back: `--embedded` serves a signed `rollBackToEmbedded`, `--to` republishes each update onto its own
|
|
13
|
+
* pointer. Exit codes as publish: 0 every pointer written, 2 refused before any write, 1 failed after writes began.
|
|
14
|
+
*/
|
|
15
|
+
export async function rollback(args, env, options) {
|
|
16
|
+
return await run(env, options, () => {
|
|
17
|
+
const { values } = parsed(ROLLBACK_USAGE, () => parseArgs({
|
|
18
|
+
args: [...args],
|
|
19
|
+
options: {
|
|
20
|
+
embedded: { type: "boolean" },
|
|
21
|
+
to: { type: "string", multiple: true },
|
|
22
|
+
channel: { type: "string" },
|
|
23
|
+
"runtime-version": { type: "string" },
|
|
24
|
+
platform: { type: "string", multiple: true },
|
|
25
|
+
},
|
|
26
|
+
strict: true,
|
|
27
|
+
allowPositionals: false,
|
|
28
|
+
}));
|
|
29
|
+
const { embedded, to, channel, platform } = values;
|
|
30
|
+
const runtime = values["runtime-version"];
|
|
31
|
+
if (embedded === true && to !== undefined) {
|
|
32
|
+
throw new PublishError(`give --embedded or --to, not both; ${ROLLBACK_USAGE}`);
|
|
33
|
+
}
|
|
34
|
+
if (to !== undefined) {
|
|
35
|
+
if (channel !== undefined || runtime !== undefined || platform !== undefined) {
|
|
36
|
+
throw new PublishError("--to rolls each update back on its own pointer: it takes no --channel, --runtime-version or --platform");
|
|
37
|
+
}
|
|
38
|
+
return republishing(updateIds(to));
|
|
39
|
+
}
|
|
40
|
+
if (embedded !== true)
|
|
41
|
+
throw new PublishError(`give --embedded or --to; ${ROLLBACK_USAGE}`);
|
|
42
|
+
if (channel === undefined || runtime === undefined) {
|
|
43
|
+
throw new PublishError(`--embedded needs --channel and --runtime-version; ${ROLLBACK_USAGE}`);
|
|
44
|
+
}
|
|
45
|
+
const keys = platformsOf(platform).map((p) => replyKey(channel, runtime, p));
|
|
46
|
+
return rollingBack(keys);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Republishes each stored update under a new ID with a fresh `createdAt`, onto `--channel` or its own channel. Exit
|
|
51
|
+
* codes as publish.
|
|
52
|
+
*/
|
|
53
|
+
export async function republish(args, env, options) {
|
|
54
|
+
return await run(env, options, () => {
|
|
55
|
+
const { values, positionals } = parsed(REPUBLISH_USAGE, () => parseArgs({
|
|
56
|
+
args: [...args],
|
|
57
|
+
options: {
|
|
58
|
+
channel: { type: "string" },
|
|
59
|
+
"runtime-version": { type: "string" },
|
|
60
|
+
},
|
|
61
|
+
strict: true,
|
|
62
|
+
allowPositionals: true,
|
|
63
|
+
}));
|
|
64
|
+
if (positionals.length === 0) {
|
|
65
|
+
throw new PublishError(`give at least one update ID; ${REPUBLISH_USAGE}`);
|
|
66
|
+
}
|
|
67
|
+
const { channel } = values;
|
|
68
|
+
const runtime = values["runtime-version"];
|
|
69
|
+
// Refuses a bad channel or runtime before the writer opens.
|
|
70
|
+
if (channel !== undefined)
|
|
71
|
+
replyKey(channel, "1", "ios");
|
|
72
|
+
if (runtime !== undefined)
|
|
73
|
+
replyKey("main", runtime, "ios");
|
|
74
|
+
return republishing(updateIds(positionals), channel, runtime);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** Parses, opens the writer for the shipped builds' identity, prepares, then commits. */
|
|
78
|
+
async function run(env, options, parse) {
|
|
79
|
+
const { now = Date.now } = options;
|
|
80
|
+
return await execute(options, async () => {
|
|
81
|
+
const prepare = parse();
|
|
82
|
+
const writer = await openWriter(env, { now });
|
|
83
|
+
const ready = await prepare(writer, now);
|
|
84
|
+
return () => commit(ready, options.out);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** Returns the IDs, each a lowercase UUID, given once. */
|
|
88
|
+
function updateIds(ids) {
|
|
89
|
+
for (const [i, id] of ids.entries()) {
|
|
90
|
+
if (!isKey(recordKey(id)))
|
|
91
|
+
throw new PublishError("an update ID is not a lowercase UUID");
|
|
92
|
+
if (ids.indexOf(id) !== i)
|
|
93
|
+
throw new PublishError(`update ID ${id} is given twice`);
|
|
94
|
+
}
|
|
95
|
+
return ids;
|
|
96
|
+
}
|
|
97
|
+
/** Signs a `rollBackToEmbedded` for each existing pointer, committed after the time it replaces. */
|
|
98
|
+
function rollingBack(keys) {
|
|
99
|
+
return async (writer, now) => {
|
|
100
|
+
const { targets, local, checkCertificate } = await readTargets(writer, keys.map((key) => ({ key })), now);
|
|
101
|
+
const pointers = [];
|
|
102
|
+
for (const { key, previous } of targets) {
|
|
103
|
+
// A typo in the channel or runtime must not "succeed" while the bad update keeps serving.
|
|
104
|
+
if (previous.etag === undefined) {
|
|
105
|
+
throw new PublishError(`${key} does not exist: there is nothing to roll back`);
|
|
106
|
+
}
|
|
107
|
+
const commitTime = new Date(createdAtFloor(local, previous));
|
|
108
|
+
const body = buildRollBackToEmbedded({ commitTime });
|
|
109
|
+
const signature = await expoSignature(body, writer.signer);
|
|
110
|
+
const multipart = encodeMultipart([{ name: "directive", body, signature }]);
|
|
111
|
+
pointers.push({
|
|
112
|
+
key,
|
|
113
|
+
previous,
|
|
114
|
+
reply: encodeStoredReply({ kind: "rollBackToEmbedded", ...multipart }),
|
|
115
|
+
line: `${key} - ${commitTime.toISOString()}`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return await plan(writer, checkCertificate, [], pointers);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Republishes each update's stored record under a new ID onto `channel`, or its own. Its bundle and assets must be
|
|
123
|
+
* stored already; nothing is uploaded.
|
|
124
|
+
*/
|
|
125
|
+
function republishing(ids, channel, runtime) {
|
|
126
|
+
return async (writer, now) => {
|
|
127
|
+
const { bucket } = writer;
|
|
128
|
+
// Every retained record decodes and agrees on asset keys, the source's included, as a publish requires.
|
|
129
|
+
const { records: retained } = await recordedKeys(bucket, { wanted: ids.map(recordKey) });
|
|
130
|
+
const stored = await storedContent(bucket);
|
|
131
|
+
const sources = [];
|
|
132
|
+
for (const id of ids) {
|
|
133
|
+
const record = retained.get(recordKey(id));
|
|
134
|
+
if (record === undefined)
|
|
135
|
+
throw new PublishError(`no release record for update ${id}`);
|
|
136
|
+
if (runtime !== undefined && record.runtimeVersion !== runtime) {
|
|
137
|
+
throw new PublishError(`update ${id} was built for another runtime version than --runtime-version`);
|
|
138
|
+
}
|
|
139
|
+
const target = channel ?? record.channel;
|
|
140
|
+
const key = replyKey(target, record.runtimeVersion, record.platform);
|
|
141
|
+
const twin = sources.find((s) => s.key === key);
|
|
142
|
+
// Two writes over one read ETag would fail the run against its own pointer.
|
|
143
|
+
if (twin !== undefined) {
|
|
144
|
+
throw new PublishError(`updates ${twin.record.updateId} and ${id} both target ${key}`);
|
|
145
|
+
}
|
|
146
|
+
const referenced = [
|
|
147
|
+
`bundles/${contentKey(record.launchAsset.hash)}`,
|
|
148
|
+
...record.assets.map((a) => `assets/${contentKey(a.hash)}`),
|
|
149
|
+
];
|
|
150
|
+
const missing = referenced.find((k) => !stored.has(k));
|
|
151
|
+
if (missing !== undefined) {
|
|
152
|
+
throw new PublishError(`update ${id} references ${missing}, which is missing`);
|
|
153
|
+
}
|
|
154
|
+
sources.push({ key, record, channel: target });
|
|
155
|
+
}
|
|
156
|
+
const { targets, local, checkCertificate } = await readTargets(writer, sources, now);
|
|
157
|
+
const records = [];
|
|
158
|
+
const pointers = [];
|
|
159
|
+
for (const { key, record, channel: target, previous } of targets) {
|
|
160
|
+
// Newer than the code it copies too, even when that was stamped by a clock ahead of this one.
|
|
161
|
+
const createdAt = Math.max(createdAtFloor(local, previous), Date.parse(record.createdAt) + 1);
|
|
162
|
+
const { updateId: _, createdAt: __, ...draft } = record;
|
|
163
|
+
const c = await compose(writer, { ...draft, channel: target }, createdAt, { key, previous });
|
|
164
|
+
records.push(...c.records);
|
|
165
|
+
pointers.push(c.pointer);
|
|
166
|
+
}
|
|
167
|
+
return await plan(writer, checkCertificate, records, pointers);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { Bucket } from "./bucket.ts";
|
|
2
|
+
import type { Env, Writer } from "./keys.ts";
|
|
3
|
+
import type { PointerState } from "./pointer.ts";
|
|
4
|
+
import type { PatchBase } from "./publish-patch.ts";
|
|
5
|
+
import type { Platform, ReleaseRecord } from "./publish-record.ts";
|
|
6
|
+
export interface PublishOptions {
|
|
7
|
+
readonly out: (line: string) => void;
|
|
8
|
+
readonly err: (line: string) => void;
|
|
9
|
+
readonly now?: () => number;
|
|
10
|
+
readonly cwd?: string;
|
|
11
|
+
}
|
|
12
|
+
/** A release record before it has an update ID and a time: everything its manifest is built from. */
|
|
13
|
+
type DraftRecord = Omit<ReleaseRecord, "updateId" | "createdAt">;
|
|
14
|
+
/** One pointer's reply, built and signed before anything is written, the state it replaces and its stdout line. */
|
|
15
|
+
interface PointerWrite {
|
|
16
|
+
readonly key: string;
|
|
17
|
+
readonly previous: PointerState;
|
|
18
|
+
readonly reply: Uint8Array<ArrayBuffer>;
|
|
19
|
+
readonly line: string;
|
|
20
|
+
}
|
|
21
|
+
/** Everything a write command writes after its uploads, prepared with every refusal already made. */
|
|
22
|
+
export interface Plan {
|
|
23
|
+
readonly writer: Writer;
|
|
24
|
+
readonly noUpdate: Uint8Array<ArrayBuffer> | undefined;
|
|
25
|
+
readonly records: readonly (readonly [string, Uint8Array<ArrayBuffer>])[];
|
|
26
|
+
readonly pointers: readonly PointerWrite[];
|
|
27
|
+
/** Refuses unless the certificate is valid by both clocks, each read afresh. */
|
|
28
|
+
readonly checkCertificate: () => Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/** A write command's writes, returned once every refusal has been made; every failure in it follows a write. */
|
|
31
|
+
export type WriteStep = () => Promise<void>;
|
|
32
|
+
/** Asset keys, lowercased because iOS stores asset files on a case-insensitive disk, to the one SHA-256 each names. */
|
|
33
|
+
type KeyHashes = Map<string, {
|
|
34
|
+
readonly hash: string;
|
|
35
|
+
readonly source: string;
|
|
36
|
+
}>;
|
|
37
|
+
export declare const PUBLISH_USAGE = "usage: tide publish --from <dir> --channel <name>";
|
|
38
|
+
/**
|
|
39
|
+
* Publishes one update per platform to `--channel`. Returns 0 when every pointer is written, 2 when the publish is
|
|
40
|
+
* refused before any write, and 1 when it fails after writes began.
|
|
41
|
+
*/
|
|
42
|
+
export declare function publish(args: readonly string[], env: Env, options: PublishOptions): Promise<0 | 1 | 2>;
|
|
43
|
+
/** Runs a write command: 0 once its write step succeeds, 2 when `prepare` refuses, 1 when a write fails. */
|
|
44
|
+
export declare function execute(options: PublishOptions, prepare: () => Promise<WriteStep>): Promise<0 | 1 | 2>;
|
|
45
|
+
/** Prints a write command's own error and returns `code`; anything else is a bug and is rethrown. */
|
|
46
|
+
export declare function report<Code extends 1 | 2>(e: unknown, { err }: PublishOptions, code: Code): Code;
|
|
47
|
+
/**
|
|
48
|
+
* Reads each item's pointer, then the bucket's time, refusing a local clock more than a minute from it or a
|
|
49
|
+
* certificate invalid by either clock. Returns the pointers and the one local sample every new time comes from.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readTargets<T extends {
|
|
52
|
+
readonly key: string;
|
|
53
|
+
}>(writer: Writer, items: readonly T[], now: () => number): Promise<{
|
|
54
|
+
targets: (T & {
|
|
55
|
+
readonly previous: PointerState;
|
|
56
|
+
})[];
|
|
57
|
+
local: number;
|
|
58
|
+
checkCertificate: () => Promise<void>;
|
|
59
|
+
}>;
|
|
60
|
+
/**
|
|
61
|
+
* Ends a plan once everything is signed: builds `reply/no-update` if the bucket lacks one, then checks the
|
|
62
|
+
* certificate afresh, so an expiry before the first write is a refusal.
|
|
63
|
+
*/
|
|
64
|
+
export declare function plan(writer: Writer, checkCertificate: () => Promise<void>, records: Plan["records"], pointers: Plan["pointers"]): Promise<Plan>;
|
|
65
|
+
/**
|
|
66
|
+
* Writes `reply/no-update` if needed, the records, then each pointer. The certificate is re-checked before a pointer
|
|
67
|
+
* only once a PUT has been attempted since the plan's final check, so every failure here follows a write.
|
|
68
|
+
*/
|
|
69
|
+
export declare function commit({ writer, noUpdate, records, pointers, checkCertificate }: Plan, out: (line: string) => void): Promise<void>;
|
|
70
|
+
/** Returns `parse()`, turning parseArgs' own error into a usage refusal. */
|
|
71
|
+
export declare function parsed<T>(usage: string, parse: () => T): T;
|
|
72
|
+
/**
|
|
73
|
+
* Returns the asset keys every retained release record and registered build names, the release records stored at
|
|
74
|
+
* `wanted` keys, and the bases of each `targets` runtime and platform. A record that cannot be read refuses, since
|
|
75
|
+
* the key check cannot be proven without it.
|
|
76
|
+
*/
|
|
77
|
+
export declare function recordedKeys(bucket: Bucket, { wanted, targets, }?: {
|
|
78
|
+
readonly wanted?: readonly string[];
|
|
79
|
+
readonly targets?: readonly {
|
|
80
|
+
readonly runtime: string;
|
|
81
|
+
readonly platform: Platform;
|
|
82
|
+
}[];
|
|
83
|
+
}): Promise<{
|
|
84
|
+
keys: KeyHashes;
|
|
85
|
+
records: Map<string, ReleaseRecord>;
|
|
86
|
+
bases: PatchBase[];
|
|
87
|
+
}>;
|
|
88
|
+
/** Records that `key` names `hash`; a key seen with another hash is refused, whichever source came first. */
|
|
89
|
+
export declare function addKey(keys: KeyHashes, key: string, hash: string, source: string): void;
|
|
90
|
+
/** Builds and signs a new update from `draft` under a fresh ID: its records and its write to `target`. */
|
|
91
|
+
export declare function compose(writer: Writer, draft: DraftRecord, createdAtMs: number, { key, previous }: {
|
|
92
|
+
readonly key: string;
|
|
93
|
+
readonly previous: PointerState;
|
|
94
|
+
}): Promise<{
|
|
95
|
+
records: Plan["records"];
|
|
96
|
+
pointer: PointerWrite;
|
|
97
|
+
}>;
|
|
98
|
+
/** Returns every stored bundle and asset key, objects the server can serve, with its size. */
|
|
99
|
+
export declare function storedContent(bucket: Bucket): Promise<Map<string, number>>;
|
|
100
|
+
export {};
|