XuqmGroup-RNSDK/scripts/sync-server-signature-vector.mjs

68 行
2.1 KiB
JavaScript

#!/usr/bin/env node
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const serverRoot = path.resolve(
process.env.XUQM_SERVER_ROOT || path.join(root, '..', 'XuqmGroup-Server'),
)
const source = path.join(
serverRoot,
'update-service/src/test/resources/rn-release-signature-vector.json',
)
const target = path.join(
root,
'packages/common/tests/fixtures/rn-release-signature-vector.generated.json',
)
const mode = process.argv[2]
if ((mode !== '--check' && mode !== '--write') || process.argv.length !== 3) {
throw new Error('Usage: node scripts/sync-server-signature-vector.mjs --check|--write')
}
function parseVector(bytes, label) {
let vector
try {
vector = JSON.parse(bytes.toString('utf8'))
} catch {
throw new Error(`${label} is not valid UTF-8 JSON`)
}
for (const field of ['keyId', 'publicKeyRawBase64', 'canonicalJson', 'signatureBase64Url']) {
if (typeof vector[field] !== 'string' || !vector[field]) {
throw new Error(`${label} is missing ${field}`)
}
}
}
if (!existsSync(source)) {
if (mode === '--write') {
throw new Error(`Server signature vector does not exist: ${source}`)
}
console.log(`signature vector check skipped; Server checkout is unavailable: ${source}`)
process.exit(0)
}
const sourceBytes = readFileSync(source)
parseVector(sourceBytes, 'Server signature vector')
if (mode === '--write') {
mkdirSync(path.dirname(target), { recursive: true })
writeFileSync(target, sourceBytes)
console.log(`generated RN signature vector from Server: ${target}`)
process.exit(0)
}
if (!existsSync(target)) {
throw new Error(`Generated RN signature vector is missing; run with --write: ${target}`)
}
const targetBytes = readFileSync(target)
parseVector(targetBytes, 'Generated RN signature vector')
if (!sourceBytes.equals(targetBytes)) {
throw new Error(
'Generated RN signature vector differs from Server; run sync-server-signature-vector.mjs --write',
)
}
console.log('RN signature vector matches Server byte-for-byte')