feat(ota): add GitHub release-based OTA delivery
This commit is contained in:
@@ -0,0 +1,516 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn, spawnSync } from "node:child_process"
|
||||
import { createHash } from "node:crypto"
|
||||
import { existsSync } from "node:fs"
|
||||
import { readFile, writeFile } from "node:fs/promises"
|
||||
import { fileURLToPath } from "node:url"
|
||||
|
||||
import { dirname, extname, join, resolve } from "pathe"
|
||||
|
||||
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))
|
||||
const REPO_ROOT = resolve(SCRIPT_DIR, "..", "..")
|
||||
const DEFAULT_MOBILE_PROJECT_DIR = join(REPO_ROOT, "apps", "mobile")
|
||||
const OTA_PLATFORMS = ["ios", "android"]
|
||||
const CONTENT_TYPES = new Map([
|
||||
[".aac", "audio/aac"],
|
||||
[".bmp", "image/bmp"],
|
||||
[".bundle", "application/javascript"],
|
||||
[".css", "text/css"],
|
||||
[".gif", "image/gif"],
|
||||
[".hbc", "application/javascript"],
|
||||
[".html", "text/html"],
|
||||
[".jpeg", "image/jpeg"],
|
||||
[".jpg", "image/jpeg"],
|
||||
[".js", "application/javascript"],
|
||||
[".json", "application/json"],
|
||||
[".m4a", "audio/mp4"],
|
||||
[".mp3", "audio/mpeg"],
|
||||
[".mp4", "video/mp4"],
|
||||
[".otf", "font/otf"],
|
||||
[".pdf", "application/pdf"],
|
||||
[".png", "image/png"],
|
||||
[".svg", "image/svg+xml"],
|
||||
[".ttf", "font/ttf"],
|
||||
[".txt", "text/plain; charset=utf-8"],
|
||||
[".wav", "audio/wav"],
|
||||
[".webm", "video/webm"],
|
||||
[".webp", "image/webp"],
|
||||
[".woff", "font/woff"],
|
||||
[".woff2", "font/woff2"],
|
||||
[".xml", "application/xml"],
|
||||
])
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* path: string
|
||||
* ext?: string
|
||||
* }} ExportAssetMetadata
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* path: string
|
||||
* sha256: string
|
||||
* contentType: string
|
||||
* }} OtaAsset
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* bundle: string
|
||||
* assets?: unknown[]
|
||||
* }} ExportPlatformMetadata
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* fileMetadata?: Partial<Record<"ios" | "android", ExportPlatformMetadata>>
|
||||
* }} ExportMetadata
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* product: string
|
||||
* channel: string
|
||||
* releaseVersion: string
|
||||
* releaseKind: string
|
||||
* runtimeVersion: string
|
||||
* gitTag: string
|
||||
* gitCommit: string
|
||||
* publishedAt: string
|
||||
* policy: {
|
||||
* storeRequired: boolean
|
||||
* minSupportedBinaryVersion: string
|
||||
* message: string | null
|
||||
* }
|
||||
* metadata: ExportMetadata
|
||||
* resolveAsset: (asset: ExportAssetMetadata) => Promise<OtaAsset>
|
||||
* }} BuildOtaMetadataInput
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {BuildOtaMetadataInput} input
|
||||
*/
|
||||
export async function buildOtaMetadata(input) {
|
||||
/** @type {Record<string, { launchAsset: OtaAsset, assets: OtaAsset[] }>} */
|
||||
const platforms = {}
|
||||
const fileMetadata = resolveExportFileMetadata(input.metadata)
|
||||
|
||||
for (const platform of OTA_PLATFORMS) {
|
||||
const platformMetadata = resolvePlatformMetadata(fileMetadata, platform)
|
||||
const launchAsset = { path: normalizeAssetPath(platformMetadata.bundle) }
|
||||
const assets = collectAssets(platformMetadata.assets, platform)
|
||||
|
||||
platforms[platform] = {
|
||||
launchAsset: await input.resolveAsset(launchAsset),
|
||||
assets: await Promise.all(assets.map((asset) => input.resolveAsset(asset))),
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
releaseKind: input.releaseKind,
|
||||
runtimeVersion: input.runtimeVersion,
|
||||
publishedAt: input.publishedAt,
|
||||
git: {
|
||||
tag: input.gitTag,
|
||||
commit: input.gitCommit,
|
||||
},
|
||||
policy: input.policy,
|
||||
platforms,
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
const metadataPath = join(distDir, "metadata.json")
|
||||
|
||||
if (!existsSync(metadataPath)) {
|
||||
throw new Error(
|
||||
`Missing Expo export metadata at ${metadataPath}. Run "pnpm --dir apps/mobile run update:export" first.`,
|
||||
)
|
||||
}
|
||||
|
||||
const packageJson = await readJson(join(projectDir, "package.json"))
|
||||
const product = process.env.OTA_PRODUCT ?? "mobile"
|
||||
const channel = process.env.OTA_CHANNEL ?? "production"
|
||||
const releaseVersion = process.env.OTA_RELEASE_VERSION ?? packageJson.version
|
||||
const releaseKind = process.env.OTA_RELEASE_KIND ?? "ota"
|
||||
const runtimeVersion = process.env.OTA_RUNTIME_VERSION ?? packageJson.version
|
||||
const gitTag = process.env.OTA_GIT_TAG ?? `${product}/v${releaseVersion}`
|
||||
const gitCommit = process.env.OTA_GIT_COMMIT ?? execGit(["rev-parse", "HEAD"], REPO_ROOT)
|
||||
const publishedAt = process.env.OTA_PUBLISHED_AT ?? new Date().toISOString()
|
||||
const metadata = await readJson(metadataPath)
|
||||
|
||||
const otaMetadata = await buildOtaMetadata({
|
||||
product,
|
||||
channel,
|
||||
releaseVersion,
|
||||
releaseKind,
|
||||
runtimeVersion,
|
||||
gitTag,
|
||||
gitCommit,
|
||||
publishedAt,
|
||||
policy: {
|
||||
storeRequired: parseBooleanEnv("OTA_STORE_REQUIRED", false),
|
||||
minSupportedBinaryVersion: process.env.OTA_MIN_SUPPORTED_BINARY_VERSION ?? runtimeVersion,
|
||||
message: parseNullableEnv("OTA_POLICY_MESSAGE"),
|
||||
},
|
||||
metadata,
|
||||
resolveAsset: async (asset) => {
|
||||
const assetFilePath = join(distDir, asset.path)
|
||||
let assetBuffer
|
||||
|
||||
try {
|
||||
assetBuffer = await readFile(assetFilePath)
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error)
|
||||
throw new Error(
|
||||
`Failed to read exported asset "${asset.path}" at ${assetFilePath}: ${reason}`,
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
path: asset.path,
|
||||
sha256: createHash("sha256").update(assetBuffer).digest("hex"),
|
||||
contentType: resolveContentType(asset),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const outputPath = join(distDir, "ota-release.json")
|
||||
const archivePath = join(projectDir, "dist.tar.zst")
|
||||
|
||||
await writeFile(outputPath, `${JSON.stringify(otaMetadata, null, 2)}\n`, "utf8")
|
||||
await createTarZstArchive({ distDir, archivePath })
|
||||
|
||||
return {
|
||||
projectDir,
|
||||
distDir,
|
||||
metadataPath,
|
||||
outputPath,
|
||||
archivePath,
|
||||
otaMetadata,
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const result = await buildReleaseAssets()
|
||||
|
||||
console.info(`Wrote OTA metadata: ${result.outputPath}`)
|
||||
console.info(`Wrote OTA archive: ${result.archivePath}`)
|
||||
} catch (error) {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
process.exitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {unknown} value
|
||||
*/
|
||||
function resolveExportFileMetadata(metadata) {
|
||||
if (!metadata || typeof metadata !== "object") {
|
||||
throw new Error("Expo export metadata is missing fileMetadata")
|
||||
}
|
||||
|
||||
if (!metadata.fileMetadata || typeof metadata.fileMetadata !== "object") {
|
||||
throw new Error("Expo export metadata is missing fileMetadata")
|
||||
}
|
||||
|
||||
return metadata.fileMetadata
|
||||
}
|
||||
|
||||
function resolvePlatformMetadata(fileMetadata, platform) {
|
||||
const platformMetadata = fileMetadata[platform]
|
||||
|
||||
if (!platformMetadata || typeof platformMetadata !== "object") {
|
||||
throw new Error(`Expo export metadata is missing ${platform} platform metadata`)
|
||||
}
|
||||
|
||||
if (typeof platformMetadata.bundle !== "string" || platformMetadata.bundle.length === 0) {
|
||||
throw new Error(`Expo export metadata is missing ${platform} bundle metadata`)
|
||||
}
|
||||
|
||||
return platformMetadata
|
||||
}
|
||||
|
||||
function collectAssets(value, platform) {
|
||||
if (value == null) {
|
||||
throw new TypeError(`Expo export metadata is missing ${platform} assets metadata`)
|
||||
}
|
||||
|
||||
if (!Array.isArray(value)) {
|
||||
throw new TypeError(`Expo export metadata has invalid ${platform} assets metadata`)
|
||||
}
|
||||
|
||||
/** @type {Map<string, ExportAssetMetadata>} */
|
||||
const dedupedAssets = new Map()
|
||||
|
||||
for (const asset of value) {
|
||||
const normalizedAsset = normalizeExportAsset(asset)
|
||||
const existingAsset = dedupedAssets.get(normalizedAsset.path)
|
||||
|
||||
if (!existingAsset || (!existingAsset.ext && normalizedAsset.ext)) {
|
||||
dedupedAssets.set(normalizedAsset.path, normalizedAsset)
|
||||
continue
|
||||
}
|
||||
|
||||
if (existingAsset.ext && normalizedAsset.ext && existingAsset.ext !== normalizedAsset.ext) {
|
||||
throw new Error(
|
||||
`Expo export metadata has conflicting ext values for asset "${normalizedAsset.path}"`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return [...dedupedAssets.values()]
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {unknown} asset
|
||||
*/
|
||||
function normalizeExportAsset(asset) {
|
||||
if (typeof asset === "string") {
|
||||
return { path: normalizeAssetPath(asset) }
|
||||
}
|
||||
|
||||
if (!asset || typeof asset !== "object") {
|
||||
throw new Error(`Unsupported Expo asset metadata entry: ${JSON.stringify(asset)}`)
|
||||
}
|
||||
|
||||
const assetPath =
|
||||
"path" in asset && typeof asset.path === "string"
|
||||
? asset.path
|
||||
: "file" in asset && typeof asset.file === "string"
|
||||
? asset.file
|
||||
: null
|
||||
|
||||
if (!assetPath) {
|
||||
throw new Error(`Unsupported Expo asset metadata entry: ${JSON.stringify(asset)}`)
|
||||
}
|
||||
|
||||
return {
|
||||
path: normalizeAssetPath(assetPath),
|
||||
...("ext" in asset && asset.ext != null ? { ext: normalizeAssetExtension(asset.ext) } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeAssetExtension(ext) {
|
||||
if (typeof ext !== "string") {
|
||||
throw new TypeError(`Unsupported Expo asset ext metadata: ${JSON.stringify(ext)}`)
|
||||
}
|
||||
|
||||
const normalizedExt = ext.trim().replace(/^\.+/, "").toLowerCase()
|
||||
|
||||
if (!normalizedExt) {
|
||||
throw new Error(`Invalid exported asset ext "${ext}"`)
|
||||
}
|
||||
|
||||
return normalizedExt
|
||||
}
|
||||
|
||||
function resolveMobileProjectDir(projectDir) {
|
||||
if (projectDir) {
|
||||
return resolve(projectDir)
|
||||
}
|
||||
|
||||
const cwd = resolve(process.cwd())
|
||||
|
||||
if (existsSync(join(cwd, "package.json")) && existsSync(join(cwd, "scripts", "expo-update.ts"))) {
|
||||
return cwd
|
||||
}
|
||||
|
||||
if (existsSync(join(cwd, "apps", "mobile", "package.json"))) {
|
||||
return join(cwd, "apps", "mobile")
|
||||
}
|
||||
|
||||
return DEFAULT_MOBILE_PROJECT_DIR
|
||||
}
|
||||
|
||||
function normalizeAssetPath(assetPath) {
|
||||
const normalized = assetPath
|
||||
.replaceAll("\\", "/")
|
||||
.replace(/^\/+/, "")
|
||||
.replace(/^(\.\/)+/, "")
|
||||
.replaceAll(/\/{2,}/g, "/")
|
||||
|
||||
if (!normalized || normalized.split("/").includes("..")) {
|
||||
throw new Error(`Invalid exported asset path "${assetPath}"`)
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
function resolveContentType(asset) {
|
||||
const pathExtension = extname(asset.path).toLowerCase()
|
||||
const metadataExtension = asset.ext ? `.${asset.ext}` : ""
|
||||
const extension = pathExtension || metadataExtension
|
||||
|
||||
return CONTENT_TYPES.get(extension) ?? "application/octet-stream"
|
||||
}
|
||||
|
||||
async function createTarZstArchive({ distDir, archivePath }) {
|
||||
const zstdBinary = resolveZstdBinary()
|
||||
|
||||
await new Promise((resolvePromise, rejectPromise) => {
|
||||
let tarExited = false
|
||||
let zstdExited = false
|
||||
|
||||
const tarProcess = spawn("tar", ["-cf", "-", "-C", distDir, "."], {
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
})
|
||||
const zstdProcess = spawn(zstdBinary, ["-q", "-f", "-o", archivePath], {
|
||||
stdio: ["pipe", "inherit", "inherit"],
|
||||
})
|
||||
|
||||
const rejectOnce = once((error) => rejectPromise(error))
|
||||
const resolveIfComplete = () => {
|
||||
if (tarExited && zstdExited) {
|
||||
resolvePromise(void 0)
|
||||
}
|
||||
}
|
||||
|
||||
tarProcess.on("error", (error) => {
|
||||
rejectOnce(new Error(`Failed to run tar: ${error.message}`))
|
||||
})
|
||||
zstdProcess.on("error", (error) => {
|
||||
rejectOnce(new Error(`Failed to run zstd: ${error.message}`))
|
||||
})
|
||||
|
||||
tarProcess.stdout.on("error", (error) => {
|
||||
rejectOnce(new Error(`Failed to stream tar output: ${error.message}`))
|
||||
})
|
||||
zstdProcess.stdin.on("error", (error) => {
|
||||
rejectOnce(new Error(`Failed to stream zstd input: ${error.message}`))
|
||||
})
|
||||
|
||||
tarProcess.stdout.pipe(zstdProcess.stdin)
|
||||
|
||||
tarProcess.on("close", (code) => {
|
||||
if (code !== 0) {
|
||||
rejectOnce(new Error(`tar exited with code ${code}`))
|
||||
return
|
||||
}
|
||||
|
||||
tarExited = true
|
||||
resolveIfComplete()
|
||||
})
|
||||
|
||||
zstdProcess.on("close", (code) => {
|
||||
if (code !== 0) {
|
||||
rejectOnce(new Error(`zstd exited with code ${code}`))
|
||||
return
|
||||
}
|
||||
|
||||
zstdExited = true
|
||||
resolveIfComplete()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function resolveZstdBinary() {
|
||||
const candidates = [process.env.ZSTD_BIN, "zstd", "/opt/homebrew/bin/zstd"].filter(Boolean)
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const args = candidate === "/opt/homebrew/bin/zstd" ? ["--version"] : ["--version"]
|
||||
const result = spawnSync(candidate, args, { stdio: "ignore" })
|
||||
|
||||
if (result.status === 0) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Unable to find "zstd". Set ZSTD_BIN or install zstd in PATH.')
|
||||
}
|
||||
|
||||
function execGit(args, cwd) {
|
||||
const result = spawnSync("git", args, {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
})
|
||||
|
||||
if (result.status !== 0) {
|
||||
throw new Error(result.stderr?.trim() || `git ${args.join(" ")} failed`)
|
||||
}
|
||||
|
||||
return result.stdout.trim()
|
||||
}
|
||||
|
||||
async function readJson(path) {
|
||||
try {
|
||||
return JSON.parse(await readFile(path, "utf8"))
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error)
|
||||
throw new Error(`Failed to read JSON at ${path}: ${reason}`)
|
||||
}
|
||||
}
|
||||
|
||||
function parseBooleanEnv(name, defaultValue) {
|
||||
const value = process.env[name]
|
||||
|
||||
if (value == null) {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
if (value === "1" || value.toLowerCase() === "true") {
|
||||
return true
|
||||
}
|
||||
|
||||
if (value === "0" || value.toLowerCase() === "false") {
|
||||
return false
|
||||
}
|
||||
|
||||
throw new Error(`${name} must be "true", "false", "1", or "0"`)
|
||||
}
|
||||
|
||||
function parseNullableEnv(name) {
|
||||
const value = process.env[name]
|
||||
|
||||
if (value == null || value.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
function once(callback) {
|
||||
let called = false
|
||||
|
||||
return (...args) => {
|
||||
if (called) {
|
||||
return
|
||||
}
|
||||
|
||||
called = true
|
||||
callback(...args)
|
||||
}
|
||||
}
|
||||
|
||||
if (process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1])) {
|
||||
await main()
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"
|
||||
import { tmpdir } from "node:os"
|
||||
|
||||
import { join } from "pathe"
|
||||
import { afterEach, describe, expect, it } from "vitest"
|
||||
|
||||
type InputAsset = string | { path: string; ext?: string }
|
||||
|
||||
const tempDirs: string[] = []
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
tempDirs.splice(0).map((directory) => rm(directory, { recursive: true, force: true })),
|
||||
)
|
||||
})
|
||||
|
||||
describe("buildOtaMetadata", () => {
|
||||
it("preserves releaseVersion, runtimeVersion, and scoped git tag", async () => {
|
||||
const { buildOtaMetadata } = await import("./build-ota-release.mjs")
|
||||
|
||||
const result = await buildOtaMetadata({
|
||||
product: "mobile",
|
||||
channel: "production",
|
||||
releaseVersion: "0.4.2",
|
||||
releaseKind: "ota",
|
||||
runtimeVersion: "0.4.1",
|
||||
gitTag: "mobile/v0.4.2",
|
||||
gitCommit: "abcdef1234567890",
|
||||
publishedAt: "2026-04-10T12:00:00.000Z",
|
||||
policy: {
|
||||
storeRequired: false,
|
||||
minSupportedBinaryVersion: "0.4.1",
|
||||
message: null,
|
||||
},
|
||||
metadata: {
|
||||
fileMetadata: {
|
||||
ios: {
|
||||
bundle: "_expo/static/js/ios/main.hbc",
|
||||
assets: [],
|
||||
},
|
||||
android: {
|
||||
bundle: "_expo/static/js/android/main.hbc",
|
||||
assets: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
resolveAsset: async (assetPath: string) => ({
|
||||
path: assetPath,
|
||||
sha256: `${assetPath}-sha256`.padEnd(64, "0").slice(0, 64),
|
||||
contentType: "application/octet-stream",
|
||||
}),
|
||||
})
|
||||
|
||||
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 () => {
|
||||
const { buildOtaMetadata } = await import("./build-ota-release.mjs")
|
||||
|
||||
const result = await buildOtaMetadata({
|
||||
product: "mobile",
|
||||
channel: "production",
|
||||
releaseVersion: "0.4.2",
|
||||
releaseKind: "ota",
|
||||
runtimeVersion: "0.4.1",
|
||||
gitTag: "mobile/v0.4.2",
|
||||
gitCommit: "abcdef1234567890",
|
||||
publishedAt: "2026-04-10T12:00:00.000Z",
|
||||
policy: {
|
||||
storeRequired: false,
|
||||
minSupportedBinaryVersion: "0.4.1",
|
||||
message: null,
|
||||
},
|
||||
metadata: {
|
||||
fileMetadata: {
|
||||
ios: {
|
||||
bundle: "_expo/static/js/ios/main.hbc",
|
||||
assets: [{ path: "assets/splash", ext: "png" }],
|
||||
},
|
||||
android: {
|
||||
bundle: "_expo/static/js/android/main.hbc",
|
||||
assets: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
resolveAsset: async (asset: InputAsset) => {
|
||||
const assetPath = typeof asset === "string" ? asset : asset.path
|
||||
const ext = typeof asset === "string" ? undefined : asset.ext
|
||||
|
||||
return {
|
||||
path: assetPath,
|
||||
sha256: `${assetPath}-sha256`.padEnd(64, "0").slice(0, 64),
|
||||
contentType: ext === "png" ? "image/png" : "application/octet-stream",
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.platforms.ios.assets).toEqual([
|
||||
expect.objectContaining({
|
||||
path: "assets/splash",
|
||||
contentType: "image/png",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("fails when a platform bundle is missing from export metadata", async () => {
|
||||
const { buildOtaMetadata } = await import("./build-ota-release.mjs")
|
||||
|
||||
await expect(
|
||||
buildOtaMetadata({
|
||||
product: "mobile",
|
||||
channel: "production",
|
||||
releaseVersion: "0.4.2",
|
||||
releaseKind: "ota",
|
||||
runtimeVersion: "0.4.1",
|
||||
gitTag: "mobile/v0.4.2",
|
||||
gitCommit: "abcdef1234567890",
|
||||
publishedAt: "2026-04-10T12:00:00.000Z",
|
||||
policy: {
|
||||
storeRequired: false,
|
||||
minSupportedBinaryVersion: "0.4.1",
|
||||
message: null,
|
||||
},
|
||||
metadata: {
|
||||
fileMetadata: {
|
||||
ios: {
|
||||
bundle: "_expo/static/js/ios/main.hbc",
|
||||
assets: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
resolveAsset: async (assetPath: string) => ({
|
||||
path: assetPath,
|
||||
sha256: `${assetPath}-sha256`.padEnd(64, "0").slice(0, 64),
|
||||
contentType: "application/octet-stream",
|
||||
}),
|
||||
}),
|
||||
).rejects.toThrow(/android/i)
|
||||
})
|
||||
|
||||
it("fails when a platform assets array is missing from export metadata", async () => {
|
||||
const { buildOtaMetadata } = await import("./build-ota-release.mjs")
|
||||
|
||||
await expect(
|
||||
buildOtaMetadata({
|
||||
product: "mobile",
|
||||
channel: "production",
|
||||
releaseVersion: "0.4.2",
|
||||
releaseKind: "ota",
|
||||
runtimeVersion: "0.4.1",
|
||||
gitTag: "mobile/v0.4.2",
|
||||
gitCommit: "abcdef1234567890",
|
||||
publishedAt: "2026-04-10T12:00:00.000Z",
|
||||
policy: {
|
||||
storeRequired: false,
|
||||
minSupportedBinaryVersion: "0.4.1",
|
||||
message: null,
|
||||
},
|
||||
metadata: {
|
||||
fileMetadata: {
|
||||
ios: {
|
||||
bundle: "_expo/static/js/ios/main.hbc",
|
||||
},
|
||||
android: {
|
||||
bundle: "_expo/static/js/android/main.hbc",
|
||||
assets: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
resolveAsset: async (assetPath: string) => ({
|
||||
path: assetPath,
|
||||
sha256: `${assetPath}-sha256`.padEnd(64, "0").slice(0, 64),
|
||||
contentType: "application/octet-stream",
|
||||
}),
|
||||
}),
|
||||
).rejects.toThrow(/ios.*assets/i)
|
||||
})
|
||||
|
||||
it("deduplicates repeated asset entries after path normalization", async () => {
|
||||
const { buildOtaMetadata } = await import("./build-ota-release.mjs")
|
||||
|
||||
const resolvedAssets: InputAsset[] = []
|
||||
|
||||
const result = await buildOtaMetadata({
|
||||
product: "mobile",
|
||||
channel: "production",
|
||||
releaseVersion: "0.4.2",
|
||||
releaseKind: "ota",
|
||||
runtimeVersion: "0.4.1",
|
||||
gitTag: "mobile/v0.4.2",
|
||||
gitCommit: "abcdef1234567890",
|
||||
publishedAt: "2026-04-10T12:00:00.000Z",
|
||||
policy: {
|
||||
storeRequired: false,
|
||||
minSupportedBinaryVersion: "0.4.1",
|
||||
message: null,
|
||||
},
|
||||
metadata: {
|
||||
fileMetadata: {
|
||||
ios: {
|
||||
bundle: "_expo/static/js/ios/main.hbc",
|
||||
assets: ["./assets/icon.png", { path: "assets/icon.png" }],
|
||||
},
|
||||
android: {
|
||||
bundle: "_expo/static/js/android/main.hbc",
|
||||
assets: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
resolveAsset: async (asset: InputAsset) => {
|
||||
resolvedAssets.push(asset)
|
||||
const assetPath = typeof asset === "string" ? asset : asset.path
|
||||
|
||||
return {
|
||||
path: assetPath,
|
||||
sha256: `${assetPath}-sha256`.padEnd(64, "0").slice(0, 64),
|
||||
contentType: "image/png",
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.platforms.ios.assets).toHaveLength(1)
|
||||
expect(
|
||||
resolvedAssets.filter((asset) => {
|
||||
const assetPath = typeof asset === "string" ? asset : asset.path
|
||||
|
||||
return assetPath === "assets/icon.png"
|
||||
}),
|
||||
).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("buildReleaseAssets", () => {
|
||||
it("includes the JSON file path when export metadata cannot be parsed", async () => {
|
||||
const { buildReleaseAssets } = await import("./build-ota-release.mjs")
|
||||
|
||||
const projectDir = await mkdtemp(join(tmpdir(), "build-ota-release-test-"))
|
||||
const distDir = join(projectDir, "dist")
|
||||
const metadataPath = join(distDir, "metadata.json")
|
||||
tempDirs.push(projectDir)
|
||||
|
||||
await mkdir(distDir, { recursive: true })
|
||||
await writeFile(
|
||||
join(projectDir, "package.json"),
|
||||
JSON.stringify({ name: "@follow/mobile-test", version: "0.4.2" }),
|
||||
"utf8",
|
||||
)
|
||||
await writeFile(metadataPath, "{invalid", "utf8")
|
||||
|
||||
await expect(buildReleaseAssets({ projectDir })).rejects.toThrow(metadataPath)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { pathToFileURL } from "node:url"
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* baseUrl: string
|
||||
* token: string
|
||||
* headerName: string
|
||||
* timeoutMs?: number
|
||||
* }} TriggerOtaSyncOptions
|
||||
*/
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 10_000
|
||||
|
||||
/**
|
||||
* @param {TriggerOtaSyncOptions} options
|
||||
*/
|
||||
export async function triggerOtaSync({
|
||||
baseUrl,
|
||||
token,
|
||||
headerName,
|
||||
timeoutMs = DEFAULT_TIMEOUT_MS,
|
||||
}) {
|
||||
if (!baseUrl) {
|
||||
throw new TypeError("OTA base URL is required")
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
throw new TypeError("OTA sync token is required")
|
||||
}
|
||||
|
||||
if (!headerName) {
|
||||
throw new TypeError("OTA sync header name is required")
|
||||
}
|
||||
|
||||
if (!Number.isInteger(timeoutMs) || timeoutMs <= 0) {
|
||||
throw new TypeError("OTA sync timeout must be a positive integer")
|
||||
}
|
||||
|
||||
const normalizedBaseUrl = baseUrl.replace(/\/+$/, "")
|
||||
const controller = new AbortController()
|
||||
const timeoutError = new Error(`Timed out while triggering OTA sync after ${timeoutMs}ms`)
|
||||
const timeoutId = setTimeout(() => {
|
||||
controller.abort(timeoutError)
|
||||
}, timeoutMs)
|
||||
|
||||
try {
|
||||
const response = await fetch(`${normalizedBaseUrl}/internal/sync`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
[headerName]: token,
|
||||
},
|
||||
signal: controller.signal,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const responseText = await response.text()
|
||||
const detail = responseText ? `: ${responseText}` : ""
|
||||
|
||||
throw new Error(
|
||||
`Failed to trigger OTA sync (${response.status} ${response.statusText})${detail}`,
|
||||
)
|
||||
}
|
||||
|
||||
return response
|
||||
} catch (error) {
|
||||
if (controller.signal.aborted && controller.signal.reason === timeoutError) {
|
||||
throw timeoutError
|
||||
}
|
||||
|
||||
throw error
|
||||
} finally {
|
||||
clearTimeout(timeoutId)
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
await triggerOtaSync({
|
||||
baseUrl: process.env.OTA_BASE_URL ?? "",
|
||||
token: process.env.OTA_SYNC_TOKEN ?? "",
|
||||
headerName: process.env.OTA_SYNC_TOKEN_HEADER ?? "",
|
||||
})
|
||||
|
||||
console.info("Triggered OTA sync successfully")
|
||||
} catch (error) {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
process.exitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
await main()
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
import { createServer } from "node:http"
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest"
|
||||
|
||||
const activeServers = new Set<ReturnType<typeof createServer>>()
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
Array.from(
|
||||
activeServers,
|
||||
(server) =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
return
|
||||
}
|
||||
|
||||
resolve()
|
||||
})
|
||||
}),
|
||||
),
|
||||
)
|
||||
activeServers.clear()
|
||||
})
|
||||
|
||||
describe("triggerOtaSync", () => {
|
||||
it("POSTs to /internal/sync with the configured auth header", async () => {
|
||||
const requests: Array<{ method?: string; url?: string; headerValue?: string }> = []
|
||||
const headerName = "x-ota-sync-token"
|
||||
const token = "sync-token-value"
|
||||
|
||||
const server = createServer((request, response) => {
|
||||
requests.push({
|
||||
method: request.method,
|
||||
url: request.url,
|
||||
headerValue: request.headers[headerName],
|
||||
})
|
||||
|
||||
response.writeHead(204)
|
||||
response.end()
|
||||
})
|
||||
|
||||
activeServers.add(server)
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once("error", reject)
|
||||
server.listen(0, "127.0.0.1", resolve)
|
||||
})
|
||||
|
||||
const address = server.address()
|
||||
if (!address || typeof address === "string") {
|
||||
throw new TypeError("Expected the test server to bind to an ephemeral TCP port")
|
||||
}
|
||||
|
||||
const { triggerOtaSync } = await import("./trigger-ota-sync.mjs")
|
||||
|
||||
await triggerOtaSync({
|
||||
baseUrl: `http://127.0.0.1:${address.port}`,
|
||||
token,
|
||||
headerName,
|
||||
})
|
||||
|
||||
expect(requests).toEqual([
|
||||
{
|
||||
method: "POST",
|
||||
url: "/internal/sync",
|
||||
headerValue: token,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("throws when the sync endpoint responds with a non-OK status", async () => {
|
||||
const headerName = "x-ota-sync-token"
|
||||
const token = "sync-token-value"
|
||||
|
||||
const server = createServer((_request, response) => {
|
||||
response.writeHead(503, { "content-type": "text/plain" })
|
||||
response.end("service unavailable")
|
||||
})
|
||||
|
||||
activeServers.add(server)
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once("error", reject)
|
||||
server.listen(0, "127.0.0.1", resolve)
|
||||
})
|
||||
|
||||
const address = server.address()
|
||||
if (!address || typeof address === "string") {
|
||||
throw new TypeError("Expected the test server to bind to an ephemeral TCP port")
|
||||
}
|
||||
|
||||
const { triggerOtaSync } = await import("./trigger-ota-sync.mjs")
|
||||
|
||||
await expect(
|
||||
triggerOtaSync({
|
||||
baseUrl: `http://127.0.0.1:${address.port}`,
|
||||
token,
|
||||
headerName,
|
||||
}),
|
||||
).rejects.toThrow("Failed to trigger OTA sync (503 Service Unavailable): service unavailable")
|
||||
})
|
||||
|
||||
it("aborts the request when the timeout is exceeded", async () => {
|
||||
const headerName = "x-ota-sync-token"
|
||||
const token = "sync-token-value"
|
||||
|
||||
const server = createServer((_request, _response) => {
|
||||
// Intentionally never respond so the client must rely on the timeout.
|
||||
})
|
||||
|
||||
activeServers.add(server)
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once("error", reject)
|
||||
server.listen(0, "127.0.0.1", resolve)
|
||||
})
|
||||
|
||||
const address = server.address()
|
||||
if (!address || typeof address === "string") {
|
||||
throw new TypeError("Expected the test server to bind to an ephemeral TCP port")
|
||||
}
|
||||
|
||||
const { triggerOtaSync } = await import("./trigger-ota-sync.mjs")
|
||||
|
||||
await expect(
|
||||
triggerOtaSync({
|
||||
baseUrl: `http://127.0.0.1:${address.port}`,
|
||||
token,
|
||||
headerName,
|
||||
timeoutMs: 25,
|
||||
}),
|
||||
).rejects.toThrow("Timed out while triggering OTA sync after 25ms")
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user