Files
orca/config/scripts/ci/unit-cache/ci-unit-module-cache-trailer.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

119 lines
3.6 KiB
JavaScript

const TRAILER = '\n//# vitestCache='
const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value)
const fail = (message) => {
throw new Error(`Unit module cache: ${message}`)
}
export function checkModuleTrailer(buffer) {
const text = buffer.toString('utf8')
if (!Buffer.from(text).equals(buffer)) {
fail('module is not valid UTF-8')
}
const marker = text.lastIndexOf(TRAILER)
if (marker === -1) {
fail('module trailer is missing')
}
const encoded = text.slice(marker + TRAILER.length)
const decoded = Buffer.from(encoded, 'base64')
if (!encoded || decoded.toString('base64') !== encoded) {
fail('module trailer is not canonical base64')
}
const json = decoded.toString('utf8')
if (!Buffer.from(json).equals(decoded)) {
fail('module metadata is not valid UTF-8')
}
const table = JSON.parse(json)
if (!Array.isArray(table) || !isRecord(table[0])) {
fail('module metadata is not a flatted object table')
}
const resolve = (value) => {
if (typeof value !== 'string') {
return value
}
if (
!/^(0|[1-9][0-9]*)$/.test(value) ||
!Number.isSafeInteger(Number(value)) ||
Number(value) >= table.length
) {
fail('invalid flatted reference')
}
return table[Number(value)]
}
// Check table references directly; the cache only needs the documented metadata fields.
for (const slot of table) {
if (typeof slot === 'string') {
continue
}
if (!slot || typeof slot !== 'object') {
fail('invalid flatted table slot')
}
if (Object.hasOwn(slot, '__proto__')) {
fail('unsafe flatted property')
}
for (const value of Object.values(slot)) {
if (value !== null && typeof value === 'object') {
fail('inline object in flatted table')
}
if (typeof value === 'number' && !Number.isFinite(value)) {
fail('invalid flatted number')
}
resolve(value)
}
}
const fields = Object.fromEntries(
Object.entries(table[0]).map(([name, value]) => [name, resolve(value)])
)
const strings = (value) =>
Array.isArray(value) && value.every((item) => typeof resolve(item) === 'string')
if (typeof fields.id !== 'string' || typeof fields.url !== 'string') {
fail('invalid module id or url')
}
if (fields.file != null && typeof fields.file !== 'string') {
fail('invalid module file')
}
if (typeof fields.mappings !== 'boolean') {
fail('invalid module mappings')
}
if (
fields.moduleType !== undefined &&
fields.moduleType !== 'cjs' &&
fields.moduleType !== 'esm'
) {
fail('invalid module type')
}
if (!isRecord(fields.imports)) {
fail('invalid module imports')
}
const urls = resolve(fields.imports.urls)
const ids = resolve(fields.imports.ids)
if (
!strings(urls) ||
!isRecord(ids) ||
!Object.values(ids).every((id) => typeof resolve(id) === 'string')
) {
fail('invalid module import references')
}
for (const name of ['deps', 'dynamicDeps']) {
if (fields[name] !== undefined && !strings(fields[name])) {
fail(`invalid module ${name}`)
}
}
if (fields.staticMocks != null) {
if (!Array.isArray(fields.staticMocks)) {
fail('invalid module static mocks')
}
for (const reference of fields.staticMocks) {
const mock = resolve(reference)
if (
!isRecord(mock) ||
typeof resolve(mock.method) !== 'string' ||
typeof resolve(mock.specifier) !== 'string' ||
typeof mock.hasFactory !== 'boolean' ||
typeof mock.factoryLoadsOriginal !== 'boolean'
) {
fail('invalid module static mock')
}
}
}
}