(
{ target, buildDir, outputDir, version, revision },
{ host = process, load = loadAddon } = {},
)
| 28 | } |
| 29 | |
| 30 | export function packageRuntime( |
| 31 | { target, buildDir, outputDir, version, revision }, |
| 32 | { host = process, load = loadAddon } = {}, |
| 33 | ) { |
| 34 | const spec = Object.hasOwn(targets, target) ? targets[target] : undefined; |
| 35 | if (!spec) throw new Error(`Unsupported Node runtime target: ${target}`); |
| 36 | if (host.platform !== spec.platform || host.arch !== spec.arch) { |
| 37 | throw new Error(`Runtime smoke test requires ${spec.platform}/${spec.arch}, not ${host.platform}/${host.arch}`); |
| 38 | } |
| 39 | if (Number(host.versions.napi) < 8 || !host.versions.napi) { |
| 40 | throw new Error('libatomic requires Node-API 8 or newer'); |
| 41 | } |
| 42 | if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(version)) { |
| 43 | throw new Error('A release version is required'); |
| 44 | } |
| 45 | if (!/^[0-9a-f]{40}$/.test(revision)) throw new Error('A full Git source revision is required'); |
| 46 | |
| 47 | const source = resolve(buildDir, spec.source); |
| 48 | // Read before creating the destination so a missing producer is a hard failure. |
| 49 | readFileSync(source); |
| 50 | mkdirSync(outputDir, { recursive: true }); |
| 51 | const library = resolve(outputDir, spec.library); |
| 52 | // Never overwrite a runtime that a process might already have mapped. |
| 53 | copyFileSync(source, library, constants.COPYFILE_EXCL); |
| 54 | const addon = load(library); |
| 55 | if (typeof addon.call !== 'function' || typeof addon.descriptor !== 'function') { |
| 56 | throw new Error('Library must be built with --features node (missing call/descriptor exports)'); |
| 57 | } |
| 58 | const descriptor = addon.descriptor(); |
| 59 | if (!Buffer.isBuffer(descriptor) || descriptor.length === 0) { |
| 60 | throw new Error('Library has no protobuf descriptor'); |
| 61 | } |
| 62 | const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex'); |
| 63 | const metadata = { |
| 64 | schemaVersion: 1, |
| 65 | version, |
| 66 | revision, |
| 67 | target, |
| 68 | platform: spec.platform, |
| 69 | arch: spec.arch, |
| 70 | nodeApiVersion: 8, |
| 71 | library: spec.library, |
| 72 | librarySha256: sha256(readFileSync(library)), |
| 73 | descriptorSha256: sha256(descriptor), |
| 74 | }; |
| 75 | writeFileSync(resolve(outputDir, 'libatomic-runtime.json'), `${JSON.stringify(metadata, null, 2)}\n`, { flag: 'wx' }); |
| 76 | return metadata; |
| 77 | } |
| 78 | |
| 79 | if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 80 | const { values } = parseArgs({ options: { |
no test coverage detected