Skip to content

The agent that grows with you

5.0 KB
Raw Blame
#!/usr/bin/env node
'use strict'
// mac-bundled-feed.mjs — build the loopback static update feed for the
// macOS packaged-app -> open-app-update E2E arm from the REAL signed NEW
// zip downloaded by the parent resolver. Dependency-free (runs on the mac
// CI runner with no npm install), pure where possible so tests-js can
// assert the generated bytes.
//
// Production contract (scripts/releases/darwin.py + apps/desktop/update-feed.cjs):
//   feed key   releases/darwin/<channel>/<channel>-mac.yml
//   merged url /releases/tag/<tag>/<artifact>.zip
// A release publishes the MERGED feed (both arches merged by
// scripts.releases.r2 finalize). electron-updater on arm64 requests
// <arch>-<channel>-mac.yml; on x64 it requests <channel>-mac.yml. The
// channel comes from the release TAG (canary tags serve canary feeds) —
// never hard-coded — and must be identical on both manifest sides
// (validated in bundle-manifest.cjs).

import { createHash } from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
import { createReadStream } from 'node:fs'

import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
// The one feed-layout contract, straight from the production source.
const { darwinFeed } = require('../../../apps/desktop/update-feed.cjs')

/** sha512 of a file, base64 — STREAMED: release zips are hundreds of MB
 * and must never be read whole into memory. */
export async function sha512Base64(filePath) {
  const hash = createHash('sha512')
  for await (const chunk of createReadStream(filePath)) hash.update(chunk)
  return hash.digest('base64')
}

/**
 * The electron-updater yml for one arch leg, in the shape electron-builder
 * writes per arch and scripts.releases.r2 merges: per-arch top-level path/sha512
 * plus files[] (url/sha512/size), releaseDate. Arch metadata names differ
 * by design; nothing here assumes they are equal across arches.
 */
export function buildMacFeedText({ version, tag, zipName, sha512, size, releaseDate }) {
  const lines = [
    `version: ${version}`,
    'files:',
    `  - url: /releases/tag/${tag}/${zipName}`,
    `    sha512: ${sha512}`,
    `    size: ${size}`,
    `path: /releases/tag/${tag}/${zipName}`,
    `sha512: ${sha512}`,
    `releaseDate: '${releaseDate}'`,
    ''
  ]
  return lines.join('\n')
}

/** The feed file names electron-updater asks for on this arch, on this
 * channel (and the un-prefixed name, served too so the merged-feed
 * contract holds). */
export function feedFileNames(arch, channel = 'stable') {
  const feed = darwinFeed(channel)
  const prefixed = arch === 'x64' ? feed.fileName : `${arch}-${feed.fileName}`
  return { feed, prefixed, plain: feed.fileName, directory: feed.directory }
}

/**
 * Materialize the loopback feed directory for the channel the NEW tag
 * publishes to:
 *   <out>/releases/darwin/<channel>/{arm64-,}<channel>-mac.yml
 *   <out>/releases/tag/<tag>/<zipName>          (copy of the real NEW zip)
 * Returns a receipt for the driver to assert.
 */
export async function materializeFeed({ outDir, zipPath, version, tag, arch, releaseDate }) {
  const zipStat = fs.statSync(zipPath)
  if (zipStat.size <= 0) throw new Error(`feed: empty zip at ${zipPath}`)
  const zipName = path.basename(zipPath)
  if (!zipName.endsWith('.zip')) {
    throw new Error(`feed: expected a .zip artifact, got ${zipName}`)
  }
  const channel = tag.includes('+canary.') ? 'canary' : 'stable'
  const sha512 = await sha512Base64(zipPath)
  const text = buildMacFeedText({ version, tag, zipName, sha512, size: zipStat.size, releaseDate })

  const { prefixed, plain, directory } = feedFileNames(arch, channel)
  const feedDir = path.join(outDir, directory)
  fs.mkdirSync(feedDir, { recursive: true })
  const written = []
  for (const name of [prefixed, plain]) {
    const file = path.join(feedDir, name)
    fs.writeFileSync(file, text)
    // URL-style keys (portable across the mac runner and CI logs)
    written.push(`${directory}/${name}`)
  }

  const artifactDir = path.join(outDir, 'releases', 'tag', tag)
  fs.mkdirSync(artifactDir, { recursive: true })
  const artifactDest = path.join(artifactDir, zipName)
  fs.copyFileSync(zipPath, artifactDest)

  return { feedKey: `${directory}/${prefixed}`, channel, written, artifactUrlPath: `/releases/tag/${tag}/${zipName}`, sha512, size: zipStat.size, version, tag }
}

// CLI entry: node mac-bundled-feed.mjs --out DIR --zip FILE --version V \
//   --tag vX.Y.Z --arch arm64|x64
if (process.argv.length > 2 && process.argv[1].endsWith('mac-bundled-feed.mjs')) {
  const { parseArgs } = await import('node:util')
  const { values } = parseArgs({
    options: {
      out: { type: 'string' },
      zip: { type: 'string' },
      version: { type: 'string' },
      tag: { type: 'string' },
      arch: { type: 'string' },
    },
  })
  const receipt = await materializeFeed({
    outDir: values.out,
    zipPath: values.zip,
    version: values.version,
    tag: values.tag,
    arch: values.arch,
    releaseDate: new Date().toISOString(),
  })
  console.log(JSON.stringify(receipt, null, 2))
}