| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #!/usr/bin/env node
- /**
- * 引擎原生模块自检 / 补装。
- *
- * 为什么需要:npm 对 optionalDependencies 的安装失败是**静默忽略**的。
- * 镜像里 `node-addon-require-builtin` 的平台包没装上时,`docker build` 依然成功,
- * 直到运行时才炸 `Cannot find module .../require_builtin.node`。
- * sharp / koffi / node-pty 同理,只是崩得晚一点。
- *
- * 用法(在 engine 目录,即含 node_modules 的那层):
- * node verify-engine-natives.mjs 只检查,缺失则退出码 1
- * node verify-engine-natives.mjs --fix 缺失的用官方源补装后再查
- * node verify-engine-natives.mjs --engine=/app/engine --fix
- *
- * 判据是"真实 require 一次",不是看文件存不存在 —— 后者会被残缺包骗过。
- */
- import { createRequire } from 'node:module'
- import fs from 'node:fs'
- import path from 'node:path'
- import { execFileSync } from 'node:child_process'
- const args = process.argv.slice(2)
- const opt = (name, fallback = '') => {
- const hit = args.find(a => a.startsWith(`--${name}=`))
- return hit ? hit.slice(name.length + 3) : fallback
- }
- const FIX = args.includes('--fix')
- const ROOT = path.resolve(opt('engine', process.cwd()))
- const REGISTRY = opt('registry', 'https://registry.npmjs.org')
- const require = createRequire(path.join(ROOT, 'package.json'))
- // 依赖链上真正会把引擎搞崩的几个;顺序按加载先后
- const MODULES = [
- { pkg: 'node-addon-require-builtin', why: '引擎 loader 的底层 require 钩子', probe: m => typeof m.requireBuiltin === 'function' },
- { pkg: 'sharp', why: '图片处理', probe: m => typeof m === 'function' },
- { pkg: 'koffi', why: 'FFI', probe: m => !!m },
- { pkg: 'node-pty', why: '终端', probe: m => !!m }
- ]
- function libcTag () {
- if (process.platform !== 'linux') return ''
- try {
- const report = process.report?.getReport?.()
- return report?.header?.glibcVersionRuntime ? 'gnu' : 'musl'
- } catch {
- return 'gnu'
- }
- }
- /** 从主包的 optionalDependencies 里挑出当前平台的包名(版本跟随主包) */
- function platformPackageOf (pkgName) {
- let meta
- try {
- meta = JSON.parse(fs.readFileSync(path.join(ROOT, 'node_modules', pkgName, 'package.json'), 'utf8'))
- } catch {
- return null
- }
- const deps = meta.optionalDependencies || {}
- const stem = `${process.platform}-${process.arch}`
- const libc = libcTag()
- const candidates = Object.entries(deps).filter(([k]) => k.includes(stem))
- // linux 要区分 glibc / musl:musl 包名里带 linuxmusl 或 -musl
- const filtered = candidates.filter(([k]) => {
- if (process.platform !== 'linux') return true
- const isMusl = /musl/.test(k)
- return libc === 'musl' ? isMusl : !isMusl
- })
- const hit = filtered[0] || candidates[0]
- return hit ? { name: hit[0], version: hit[1] } : null
- }
- function tryLoad (pkg) {
- try {
- const mod = require(pkg)
- return { mod, error: null }
- } catch (error) {
- return { mod: null, error }
- }
- }
- let failed = []
- console.log(`引擎原生模块自检:${ROOT}`)
- console.log(`平台 ${process.platform}-${process.arch}${libcTag() ? `-${libcTag()}` : ''}|node ${process.versions.node}\n`)
- for (const item of MODULES) {
- const { mod, error } = tryLoad(item.pkg)
- if (mod && (!item.probe || item.probe(mod))) {
- console.log(` ✓ ${item.pkg.padEnd(30)} ${item.why}`)
- continue
- }
- const reason = error ? String(error.message).split('\n')[0] : '加载成功但导出不符合预期'
- console.log(` ✗ ${item.pkg.padEnd(30)} ${reason}`)
- failed.push({ ...item, error })
- }
- if (!failed.length) {
- console.log('\n全部就绪')
- process.exit(0)
- }
- console.log(`\n${failed.length} 个模块不可用`)
- if (!FIX) {
- console.log('加 --fix 可按平台补装缺失包(需要能访问 npm registry)')
- process.exit(1)
- }
- for (const item of failed) {
- const plat = platformPackageOf(item.pkg)
- if (!plat) {
- console.log(` ! ${item.pkg}:读不到 optionalDependencies,跳过自动补装`)
- continue
- }
- console.log(`\n补装 ${plat.name}@${plat.version}(registry ${REGISTRY})`)
- try {
- execFileSync('npm', [
- 'install', '--no-save', '--force', '--no-audit', '--no-fund',
- `--registry=${REGISTRY}`,
- `${plat.name}@${plat.version}`
- ], { cwd: ROOT, stdio: 'inherit' })
- } catch (err) {
- console.log(` ! 补装失败:${err.message.split('\n')[0]}`)
- }
- }
- // 补装后复查,只有真的能 require 才算过
- let stillBad = 0
- for (const item of MODULES) {
- const { mod } = tryLoad(item.pkg)
- const ok = mod && (!item.probe || item.probe(mod))
- console.log(` ${ok ? '✓' : '✗'} ${item.pkg}`)
- if (!ok) stillBad++
- }
- process.exit(stillBad ? 1 : 0)
|