From 05a789129a3295ece056b856d55000cb84684f67 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 10 Apr 2026 23:56:48 +0800 Subject: [PATCH] fix(ota): emit expo-compatible update identifiers --- .github/scripts/build-ota-release.mjs | 20 ++++++ .github/scripts/build-ota-release.test.ts | 3 + apps/ota/src/__tests__/manifest.test.ts | 6 ++ apps/ota/src/lib/manifest.ts | 86 ++++++++++++++++++++++- apps/ota/src/lib/schema.ts | 1 + 5 files changed, 114 insertions(+), 2 deletions(-) diff --git a/.github/scripts/build-ota-release.mjs b/.github/scripts/build-ota-release.mjs index 5fe06392e..210273035 100644 --- a/.github/scripts/build-ota-release.mjs +++ b/.github/scripts/build-ota-release.mjs @@ -110,6 +110,9 @@ export async function buildOtaMetadata(input) { return { schemaVersion: 1, + updateId: createDeterministicUuid( + `${input.product}:${input.channel}:${input.releaseVersion}:${input.runtimeVersion}:${input.gitTag}:${input.gitCommit}`, + ), product: input.product, channel: input.channel, releaseVersion: input.releaseVersion, @@ -125,6 +128,23 @@ export async function buildOtaMetadata(input) { } } +function createDeterministicUuid(seed) { + const digest = createHash("sha1").update(seed).digest() + const bytes = Buffer.from(digest.subarray(0, 16)) + + bytes[6] = (bytes[6] & 0x0f) | 0x50 + bytes[8] = (bytes[8] & 0x3f) | 0x80 + + const hex = bytes.toString("hex") + return [ + hex.slice(0, 8), + hex.slice(8, 12), + hex.slice(12, 16), + hex.slice(16, 20), + hex.slice(20, 32), + ].join("-") +} + export async function buildReleaseAssets(options = {}) { const projectDir = resolveMobileProjectDir(options.projectDir) const distDir = join(projectDir, "dist") diff --git a/.github/scripts/build-ota-release.test.ts b/.github/scripts/build-ota-release.test.ts index 663034763..4ee98eb85 100644 --- a/.github/scripts/build-ota-release.test.ts +++ b/.github/scripts/build-ota-release.test.ts @@ -54,6 +54,9 @@ describe("buildOtaMetadata", () => { expect(result.releaseVersion).toBe("0.4.2") expect(result.runtimeVersion).toBe("0.4.1") expect(result.git.tag).toBe("mobile/v0.4.2") + expect(result.updateId).toMatch( + /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, + ) }) it("uses asset ext metadata when exported asset paths have no suffix", async () => { diff --git a/apps/ota/src/__tests__/manifest.test.ts b/apps/ota/src/__tests__/manifest.test.ts index bb33cc860..e5a4e2a05 100644 --- a/apps/ota/src/__tests__/manifest.test.ts +++ b/apps/ota/src/__tests__/manifest.test.ts @@ -51,9 +51,15 @@ describe("buildManifest", () => { expect(manifest.launchAsset.url).toBe( "https://ota.folo.is/assets/mobile/production/0.4.1/0.4.2/ios/bundles/ios-main.js", ) + expect(manifest.launchAsset.key).toBe("ios-main") + expect(manifest.launchAsset.hash).toBe("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo") + expect(manifest.launchAsset.fileExtension).toBe(".js") expect(manifest.assets[0]?.url).toBe( "https://ota.folo.is/assets/mobile/production/0.4.1/0.4.2/ios/assets/one.png", ) + expect(manifest.assets[0]?.key).toBe("one") + expect(manifest.assets[0]?.hash).toBe("u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7s") + expect(manifest.assets[0]?.fileExtension).toBe(".png") }) }) diff --git a/apps/ota/src/lib/manifest.ts b/apps/ota/src/lib/manifest.ts index 0327b211f..9f461ac25 100644 --- a/apps/ota/src/lib/manifest.ts +++ b/apps/ota/src/lib/manifest.ts @@ -6,6 +6,8 @@ type PlatformAsset = PlatformPayload["launchAsset"] | PlatformPayload["assets"][ export interface ManifestAsset { key: string + hash: string + fileExtension?: string contentType: string url: string } @@ -41,7 +43,7 @@ export function buildManifest( } return { - id: `${release.product}-${release.channel}-${release.releaseVersion}-${input.platform}`, + id: release.updateId ?? crypto.randomUUID(), createdAt: release.publishedAt, runtimeVersion: release.runtimeVersion, launchAsset: toManifestAsset( @@ -69,8 +71,12 @@ function toManifestAsset( platform: OtaPlatform, asset: PlatformAsset, ): ManifestAsset { + const { key, fileExtension } = toExpoAssetIdentity(asset.path, asset.contentType) + return { - key: asset.path, + key, + hash: toExpoManifestHash(asset.sha256), + fileExtension, contentType: asset.contentType, url: new URL( `/assets/${buildMirroredAssetKey(release, platform, asset.path)}`, @@ -78,3 +84,79 @@ function toManifestAsset( ).toString(), } } + +function toExpoManifestHash(sha256Hex: string) { + const bytes = hexToBytes(sha256Hex) + return Buffer.from(bytes) + .toString("base64") + .replaceAll("+", "-") + .replaceAll("/", "_") + .replaceAll(/=+$/g, "") +} + +function hexToBytes(sha256Hex: string) { + const normalized = sha256Hex.trim().toLowerCase() + + if (!/^[a-f0-9]{64}$/.test(normalized)) { + return new Uint8Array() + } + + const bytes = new Uint8Array(normalized.length / 2) + + for (let index = 0; index < normalized.length; index += 2) { + bytes[index / 2] = Number.parseInt(normalized.slice(index, index + 2), 16) + } + + return bytes +} + +function toExpoAssetIdentity(path: string, contentType: string) { + const fileName = path.split("/").at(-1) ?? path + const extensionMatch = fileName.match(/(\.[^.]+)$/) + + if (extensionMatch?.[1]) { + const fileExtension = extensionMatch[1] + return { + key: fileName.slice(0, -fileExtension.length), + fileExtension, + } + } + + const inferredExtension = inferExtensionFromContentType(contentType) + return { + key: fileName, + fileExtension: inferredExtension, + } +} + +function inferExtensionFromContentType(contentType: string) { + switch (contentType) { + case "application/javascript": { + return ".bundle" + } + case "image/png": { + return ".png" + } + case "image/jpeg": { + return ".jpg" + } + case "image/webp": { + return ".webp" + } + case "font/otf": { + return ".otf" + } + case "font/ttf": { + return ".ttf" + } + case "font/woff": { + return ".woff" + } + case "font/woff2": { + return ".woff2" + } + default: { + return + } + } +} diff --git a/apps/ota/src/lib/schema.ts b/apps/ota/src/lib/schema.ts index 385053040..dc6018056 100644 --- a/apps/ota/src/lib/schema.ts +++ b/apps/ota/src/lib/schema.ts @@ -29,6 +29,7 @@ const platformsSchema = z export const otaReleaseSchema = z.object({ schemaVersion: z.literal(1), + updateId: z.string().uuid().optional(), product: z.enum(["mobile", "desktop"]), channel: z.string().min(1), releaseVersion: semver,