mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* perf(mobile): let newer phone shells fetch the language catalog from the desktop instead of downloading all of them * fix(mobile): hash only the buildId fields so phone readers can recompute it * fix(mobile): use the catalog chunk when the generation still has it * fix(desktop): give catalog reads their own read-slot budget
202 lines
7.1 KiB
JavaScript
202 lines
7.1 KiB
JavaScript
import { createHash } from 'node:crypto'
|
|
import { readFile, mkdir, rm, writeFile } from 'node:fs/promises'
|
|
import { createRequire } from 'node:module'
|
|
import { join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { promisify } from 'node:util'
|
|
import { brotliCompress, constants as zlibConstants } from 'node:zlib'
|
|
|
|
const { darkBuildMarkers } = createRequire(import.meta.url)('../../../dark-build-switches.cjs')
|
|
|
|
const projectDir = fileURLToPath(new URL('../../../..', import.meta.url))
|
|
const brotliCompressAsync = promisify(brotliCompress)
|
|
|
|
export const MOBILE_WEB_BUNDLE_SCHEMA_VERSION = 1
|
|
export const MOBILE_WEB_BUNDLE_ENTRYPOINT = 'index.html'
|
|
|
|
const CONTENT_TYPE_BY_EXTENSION = {
|
|
css: 'text/css; charset=utf-8',
|
|
html: 'text/html; charset=utf-8',
|
|
js: 'text/javascript; charset=utf-8',
|
|
png: 'image/png',
|
|
// The app bundle emits images as same-origin assets rather than data: URLs, so each one is
|
|
// content-hashed and served from here, including the terminal's licensed symbol font.
|
|
jpg: 'image/jpeg',
|
|
jpeg: 'image/jpeg',
|
|
gif: 'image/gif',
|
|
webp: 'image/webp',
|
|
svg: 'image/svg+xml',
|
|
woff2: 'font/woff2'
|
|
}
|
|
|
|
// Keep canonical key/path order byte-identical to src/shared/mobile-web-bundle serializer; buildId must depend only on content.
|
|
export function serializeMobileWebBundleAssets(assets) {
|
|
return JSON.stringify(
|
|
[...assets]
|
|
.sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0))
|
|
.map(({ path, sha256, byteLength, contentType }) => ({
|
|
path,
|
|
sha256,
|
|
byteLength,
|
|
contentType
|
|
}))
|
|
)
|
|
}
|
|
|
|
export function computeMobileWebBundleBuildId(assets) {
|
|
return createHash('sha256').update(serializeMobileWebBundleAssets(assets), 'utf8').digest('hex')
|
|
}
|
|
|
|
export function sha256Hex(bytes) {
|
|
return createHash('sha256').update(bytes).digest('hex')
|
|
}
|
|
|
|
export function contentTypeForExtension(extension) {
|
|
const contentType = CONTENT_TYPE_BY_EXTENSION[extension]
|
|
if (!contentType) {
|
|
throw new Error(`[mobile-web-bundle-manifest] no content type registered for .${extension}`)
|
|
}
|
|
return contentType
|
|
}
|
|
|
|
export function hashedAsset(bytes, extension) {
|
|
const sha256 = sha256Hex(bytes)
|
|
return {
|
|
bytes,
|
|
path: `assets/${sha256}.${extension}`,
|
|
sha256,
|
|
byteLength: bytes.byteLength,
|
|
contentType: contentTypeForExtension(extension)
|
|
}
|
|
}
|
|
|
|
function readIntegerConstant(source, name, file = 'src/shared/protocol-version.ts') {
|
|
const match = new RegExp(`export const ${name} = (\\d+)`).exec(source)
|
|
if (!match) {
|
|
throw new Error(`[mobile-web-bundle-manifest] ${name} not found in ${file}`)
|
|
}
|
|
return Number.parseInt(match[1], 10)
|
|
}
|
|
|
|
// Parse TS protocol source: packaging runs bare Node before build outputs exist.
|
|
export async function readProtocolWindow() {
|
|
const source = await readFile(join(projectDir, 'src', 'shared', 'protocol-version.ts'), 'utf8')
|
|
return {
|
|
runtimeProtocolVersion: readIntegerConstant(source, 'RUNTIME_PROTOCOL_VERSION'),
|
|
minCompatibleRuntimeProtocolVersion: readIntegerConstant(
|
|
source,
|
|
'MIN_COMPATIBLE_RUNTIME_SERVER_VERSION'
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function readPageVersion() {
|
|
const file = join('src', 'shared', 'mobile-web-bundle', 'manifest-contract.ts')
|
|
const source = await readFile(join(projectDir, file), 'utf8')
|
|
return readIntegerConstant(source, 'MOBILE_WEB_PAGE_VERSION', file)
|
|
}
|
|
|
|
export async function readDesktopVersion() {
|
|
const packageJson = JSON.parse(await readFile(join(projectDir, 'package.json'), 'utf8'))
|
|
if (typeof packageJson.version !== 'string' || packageJson.version.length === 0) {
|
|
throw new Error('[mobile-web-bundle-manifest] root package.json has no version')
|
|
}
|
|
return packageJson.version
|
|
}
|
|
|
|
// Mirrors mobileWebBundleBrotliPath in src/shared/mobile-web-bundle/manifest-contract.ts.
|
|
export function mobileWebBundleBrotliPath(assetPath) {
|
|
return `${assetPath}.br`
|
|
}
|
|
|
|
// Largest first so the slowest file starts first on zlib's thread pool; keep only copies that shrink.
|
|
async function compressWithBrotli(written) {
|
|
const largestFirst = [...written].sort((left, right) => right.byteLength - left.byteLength)
|
|
const copies = await Promise.all(
|
|
largestFirst.map((asset) =>
|
|
brotliCompressAsync(asset.bytes, {
|
|
params: {
|
|
[zlibConstants.BROTLI_PARAM_QUALITY]: zlibConstants.BROTLI_MAX_QUALITY,
|
|
[zlibConstants.BROTLI_PARAM_SIZE_HINT]: asset.byteLength
|
|
}
|
|
})
|
|
)
|
|
)
|
|
return new Map(
|
|
largestFirst.flatMap((asset, index) =>
|
|
copies[index].byteLength < asset.byteLength ? [[asset.path, copies[index]]] : []
|
|
)
|
|
)
|
|
}
|
|
|
|
// Single manifest producer keeps builder/contract/packaging guard shape aligned.
|
|
export async function writeMobileWebBundleTree({
|
|
outDir,
|
|
written,
|
|
// Host-only JSON outside `assets` and buildId, served one at a time over RPC.
|
|
catalogs = [],
|
|
desktopVersion,
|
|
protocolWindow,
|
|
// Empty route list makes shell render every screen natively.
|
|
routes = [],
|
|
// Whole-file brotli copies for phones that inflate natively.
|
|
brotli = false,
|
|
env = process.env
|
|
}) {
|
|
const brotliCopies = brotli ? await compressWithBrotli(written) : new Map()
|
|
const assets = written
|
|
.map(({ path, sha256, byteLength, contentType, kind }) => {
|
|
const copy = brotliCopies.get(path)
|
|
return {
|
|
path,
|
|
sha256,
|
|
byteLength,
|
|
contentType,
|
|
...(copy === undefined ? {} : { brotliByteLength: copy.byteLength }),
|
|
...(kind === undefined ? {} : { kind })
|
|
}
|
|
})
|
|
.sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0))
|
|
const manifest = {
|
|
schemaVersion: MOBILE_WEB_BUNDLE_SCHEMA_VERSION,
|
|
buildId: computeMobileWebBundleBuildId(assets),
|
|
desktopVersion,
|
|
minCompatibleRuntimeProtocolVersion: protocolWindow.minCompatibleRuntimeProtocolVersion,
|
|
runtimeProtocolVersion: protocolWindow.runtimeProtocolVersion,
|
|
pageVersion: await readPageVersion(),
|
|
entrypoint: MOBILE_WEB_BUNDLE_ENTRYPOINT,
|
|
totalBytes: assets.reduce((total, asset) => total + asset.byteLength, 0),
|
|
assets,
|
|
routes,
|
|
...(catalogs.length === 0
|
|
? {}
|
|
: {
|
|
catalogs: catalogs.map(({ locale, path, sha256, byteLength }) => ({
|
|
locale,
|
|
path,
|
|
sha256,
|
|
byteLength
|
|
}))
|
|
})
|
|
}
|
|
|
|
// Clear entire output or stale unreferenced assets ship inside asar.
|
|
await rm(outDir, { recursive: true, force: true })
|
|
await mkdir(join(outDir, 'assets'), { recursive: true })
|
|
if (catalogs.length > 0) {
|
|
await mkdir(join(outDir, 'catalogs'), { recursive: true })
|
|
}
|
|
for (const file of [...written, ...catalogs]) {
|
|
await writeFile(join(outDir, file.path), file.bytes)
|
|
}
|
|
for (const [path, copy] of brotliCopies) {
|
|
await writeFile(join(outDir, mobileWebBundleBrotliPath(path)), copy)
|
|
}
|
|
await writeFile(join(outDir, 'manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`, 'utf8')
|
|
// Keep switch metadata outside manifest so compiled page bytes/buildId remain unchanged.
|
|
for (const marker of darkBuildMarkers(env)) {
|
|
await writeFile(join(outDir, marker.fileName), marker.text, 'utf8')
|
|
}
|
|
return { manifest, outDir }
|
|
}
|