Files
orca/config/scripts/terminal-dependencies/serialize-addon/build-serialize-addon-at-ref.mjs
Neil 9bd6272fdf refactor(tooling): organize build, CI, and test owners (#38130)
* refactor(tooling): organize test runtime and maintenance owners

* test(tooling): preserve relocated runtime fixture dependency edges

* refactor(tooling): preserve xterm routing owner checks

* ci(tooling): include routing owner in sparse checkout

* test(tooling): preserve copied workflow dependency closures

* fix(tooling): normalize hashed JSON assets in release parity

* fix(tooling): distinguish asset aliases from literal references

* fix(tooling): frame normalized asset reference payloads

* fix(tooling): normalize decoded JSON asset references

* refactor(tooling): organize build, CI and release owners

* fix(tooling): align moved generator and workflow path contracts

* fix(tooling): update generated plugin bundle owner banners

* test(tooling): create relocated mobile verifier fixture directories

* test(tooling): copy headless detector dependency closure

* test(tooling): align native batch policy fixtures with owner path

* fix(tooling): link moved reliability command to historical evidence

* fix(tooling): preserve nested sandbox owners and canonical guide source

* fix(tooling): target release version owners in checkout fixture
2026-11-04 21:08:19 -08:00

138 lines
5.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// Materialize a ref’s patched serialize dist for serialize-grid.differential.fuzz.test.ts.
// Read version/patch from that ref; reverse installed patch or fetch matching pristine npm tarball, checking patch blob hashes.
// Usage: --ref <ref> --out-dir <dir>; pass printed path as ORCA_OLD_SERIALIZE_ADDON to the fuzz test.
import { execFileSync } from 'node:child_process'
import { createHash } from 'node:crypto'
import {
cpSync,
existsSync,
mkdirSync,
readFileSync,
realpathSync,
rmSync,
writeFileSync
} from 'node:fs'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { fetchPristinePackage } from '../xterm-patches/regenerate-xterm-patches.mjs'
const REPO_ROOT = path.resolve(import.meta.dirname, '../../..', '..')
const PACKAGE = '@xterm/addon-serialize'
const DIST_FILE = 'lib/addon-serialize.js'
const WORKSPACE_FILE = 'pnpm-workspace.yaml'
function parseArgs(argv) {
const args = {}
for (let i = 0; i < argv.length; i += 2) {
args[argv[i].replace(/^--/, '')] = argv[i + 1]
}
if (!args.ref || !args['out-dir']) {
throw new Error('usage: build-serialize-addon-at-ref.mjs --ref <git-ref> --out-dir <dir>')
}
return args
}
function git(args, options = {}) {
return execFileSync('git', args, { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, ...options })
}
function blobHash(content) {
return createHash('sha1').update(`blob ${content.length}\0`).update(content).digest('hex')
}
/** The serialize version and repo-relative patch path from a pnpm-workspace.yaml's patchedDependencies. */
function serializePatchEntry(workspaceYaml) {
const match = workspaceYaml.match(
/^\s+'?@xterm\/addon-serialize@([^'\s:]+)'?\s*:\s*['"]?([^'"\s]+)['"]?\s*$/m
)
if (!match) {
throw new Error(`no ${PACKAGE} entry under patchedDependencies in ${WORKSPACE_FILE}`)
}
return { version: match[1], patchPath: match[2] }
}
/** Reads the serialize patch pinned at `ref`, or in the working tree when `ref` is null. */
function readPatchAt(ref) {
const read = (file) =>
ref === null
? readFileSync(path.join(REPO_ROOT, file), 'utf8')
: git(['show', `${ref}:${file}`], { cwd: REPO_ROOT })
const entry = serializePatchEntry(read(WORKSPACE_FILE))
return { ...entry, text: read(entry.patchPath) }
}
/** Pre- and post-image blob hashes the patch records for the dist file. */
function distHashes(patchText) {
const match = patchText.match(
new RegExp(`diff --git a/${DIST_FILE} b/${DIST_FILE}\\nindex ([0-9a-f]+)\\.\\.([0-9a-f]+)`)
)
if (!match) {
throw new Error(`patch does not touch ${DIST_FILE}`)
}
return { pristine: match[1], patched: match[2] }
}
function applyPatch(packageDir, patchPath, reverse) {
// Ceiling stops git from discovering an enclosing repository, so paths stay package-relative.
git(['apply', ...(reverse ? ['-R'] : []), `--include=${DIST_FILE}`, patchPath], {
cwd: packageDir,
env: { ...process.env, GIT_CEILING_DIRECTORIES: path.dirname(packageDir) }
})
}
/** Recovers the pristine dist into packageDir by reverse-applying the installed copy's patch; false if it can't. */
function recoverPristineFromInstalled(packageDir, outDir, installedHash, pristineHash) {
const candidates = [readPatchAt(null), readPatchAt('HEAD')]
const source = candidates.find((patch) => distHashes(patch.text).patched === installedHash)
if (!source || distHashes(source.text).pristine !== pristineHash) {
return false
}
const sourcePath = path.join(outDir, 'installed.patch')
writeFileSync(sourcePath, source.text)
applyPatch(packageDir, sourcePath, true)
return true
}
function main() {
const args = parseArgs(process.argv.slice(2))
const outDir = path.resolve(args['out-dir'])
const refPatch = readPatchAt(args.ref)
const target = distHashes(refPatch.text)
const packageDir = path.join(outDir, 'package')
const distPath = path.join(packageDir, DIST_FILE)
rmSync(outDir, { recursive: true, force: true })
mkdirSync(path.join(packageDir, 'lib'), { recursive: true })
cpSync(path.join(REPO_ROOT, 'node_modules', PACKAGE, DIST_FILE), distPath)
const installedHash = blobHash(readFileSync(distPath))
if (installedHash !== target.patched) {
if (!recoverPristineFromInstalled(packageDir, outDir, installedHash, target.pristine)) {
const pristineDir = fetchPristinePackage({ name: PACKAGE, version: refPatch.version }, outDir)
cpSync(path.join(pristineDir, DIST_FILE), distPath)
}
const pristineHash = blobHash(readFileSync(distPath))
if (pristineHash !== target.pristine) {
throw new Error(
`pristine ${PACKAGE}@${refPatch.version} ${DIST_FILE} hashes to ${pristineHash}, the ref patch expects ${target.pristine}`
)
}
const refPatchPath = path.join(outDir, 'ref.patch')
writeFileSync(refPatchPath, refPatch.text)
applyPatch(packageDir, refPatchPath, false)
}
const finalHash = blobHash(readFileSync(distPath))
if (finalHash !== target.patched || !existsSync(distPath)) {
throw new Error(
`built ${DIST_FILE} hashes to ${finalHash}, the ref patch expects ${target.patched}`
)
}
process.stdout.write(`${distPath}\n`)
}
if (process.argv[1] && pathToFileURL(realpathSync(process.argv[1])).href === import.meta.url) {
main()
}