mirror of
https://github.com/napi-rs/napi-rs.git
synced 2025-12-08 19:56:07 +00:00
feat(cli): validate the native binding versions (#2837)
This commit is contained in:
parent
5611735775
commit
310581a5f8
@ -999,6 +999,7 @@ export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array
|
||||
this.config.binaryName,
|
||||
this.config.packageName,
|
||||
idents,
|
||||
this.config.packageJson.version,
|
||||
)
|
||||
|
||||
try {
|
||||
|
||||
@ -2,12 +2,13 @@ export function createCjsBinding(
|
||||
localName: string,
|
||||
pkgName: string,
|
||||
idents: string[],
|
||||
packageVersion?: string,
|
||||
): string {
|
||||
return `${bindingHeader}
|
||||
const { createRequire } = require('node:module')
|
||||
require = createRequire(__filename)
|
||||
|
||||
${createCommonBinding(localName, pkgName)}
|
||||
${createCommonBinding(localName, pkgName, packageVersion)}
|
||||
module.exports = nativeBinding
|
||||
${idents
|
||||
.map((ident) => `module.exports.${ident} = nativeBinding.${ident}`)
|
||||
@ -19,13 +20,14 @@ export function createEsmBinding(
|
||||
localName: string,
|
||||
pkgName: string,
|
||||
idents: string[],
|
||||
packageVersion?: string,
|
||||
): string {
|
||||
return `${bindingHeader}
|
||||
import { createRequire } from 'node:module'
|
||||
const require = createRequire(import.meta.url)
|
||||
const __dirname = new URL('.', import.meta.url).pathname
|
||||
|
||||
${createCommonBinding(localName, pkgName)}
|
||||
${createCommonBinding(localName, pkgName, packageVersion)}
|
||||
const { ${idents.join(', ')} } = nativeBinding
|
||||
${idents.map((ident) => `export { ${ident} }`).join('\n')}
|
||||
`
|
||||
@ -37,20 +39,37 @@ const bindingHeader = `// prettier-ignore
|
||||
/* auto-generated by NAPI-RS */
|
||||
`
|
||||
|
||||
function createCommonBinding(localName: string, pkgName: string): string {
|
||||
function createCommonBinding(
|
||||
localName: string,
|
||||
pkgName: string,
|
||||
packageVersion?: string,
|
||||
): string {
|
||||
function requireTuple(tuple: string, identSize = 8) {
|
||||
const identLow = ' '.repeat(identSize - 2)
|
||||
const ident = ' '.repeat(identSize)
|
||||
return `try {
|
||||
${ident}return require('./${localName}.${tuple}.node')
|
||||
const versionCheck = packageVersion
|
||||
? `
|
||||
${identLow}try {
|
||||
${ident}const binding = require('${pkgName}-${tuple}')
|
||||
${ident}const bindingPackageVersion = require('${pkgName}-${tuple}/package.json').version
|
||||
${ident}if (bindingPackageVersion !== '${packageVersion}') {
|
||||
${ident} throw new Error(\`Native binding package version mismatch, expected ${packageVersion} but got \${bindingPackageVersion}. You can reinstall dependencies to fix this issue.\`)
|
||||
${ident}}
|
||||
${ident}return binding
|
||||
${identLow}} catch (e) {
|
||||
${ident}loadErrors.push(e)
|
||||
${identLow}}
|
||||
${identLow}}`
|
||||
: `
|
||||
${identLow}try {
|
||||
${ident}return require('${pkgName}-${tuple}')
|
||||
${identLow}} catch (e) {
|
||||
${ident}loadErrors.push(e)
|
||||
${identLow}}`
|
||||
return `try {
|
||||
${ident}return require('./${localName}.${tuple}.node')
|
||||
${identLow}} catch (e) {
|
||||
${ident}loadErrors.push(e)
|
||||
${identLow}}${versionCheck}`
|
||||
}
|
||||
|
||||
return `const { readFileSync } = require('node:fs')
|
||||
|
||||
@ -78,7 +78,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-android-arm64')
|
||||
const binding = require('@examples/napi-android-arm64')
|
||||
const bindingPackageVersion = require('@examples/napi-android-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -89,7 +94,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-android-arm-eabi')
|
||||
const binding = require('@examples/napi-android-arm-eabi')
|
||||
const bindingPackageVersion = require('@examples/napi-android-arm-eabi/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -104,7 +114,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-win32-x64-msvc')
|
||||
const binding = require('@examples/napi-win32-x64-msvc')
|
||||
const bindingPackageVersion = require('@examples/napi-win32-x64-msvc/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -115,7 +130,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-win32-ia32-msvc')
|
||||
const binding = require('@examples/napi-win32-ia32-msvc')
|
||||
const bindingPackageVersion = require('@examples/napi-win32-ia32-msvc/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -126,7 +146,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-win32-arm64-msvc')
|
||||
const binding = require('@examples/napi-win32-arm64-msvc')
|
||||
const bindingPackageVersion = require('@examples/napi-win32-arm64-msvc/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -140,7 +165,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-darwin-universal')
|
||||
const binding = require('@examples/napi-darwin-universal')
|
||||
const bindingPackageVersion = require('@examples/napi-darwin-universal/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -151,7 +181,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-darwin-x64')
|
||||
const binding = require('@examples/napi-darwin-x64')
|
||||
const bindingPackageVersion = require('@examples/napi-darwin-x64/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -162,7 +197,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-darwin-arm64')
|
||||
const binding = require('@examples/napi-darwin-arm64')
|
||||
const bindingPackageVersion = require('@examples/napi-darwin-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -177,7 +217,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-freebsd-x64')
|
||||
const binding = require('@examples/napi-freebsd-x64')
|
||||
const bindingPackageVersion = require('@examples/napi-freebsd-x64/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -188,7 +233,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-freebsd-arm64')
|
||||
const binding = require('@examples/napi-freebsd-arm64')
|
||||
const bindingPackageVersion = require('@examples/napi-freebsd-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -204,7 +254,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-x64-musl')
|
||||
const binding = require('@examples/napi-linux-x64-musl')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-x64-musl/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -215,7 +270,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-x64-gnu')
|
||||
const binding = require('@examples/napi-linux-x64-gnu')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-x64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -228,7 +288,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-arm64-musl')
|
||||
const binding = require('@examples/napi-linux-arm64-musl')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-arm64-musl/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -239,7 +304,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-arm64-gnu')
|
||||
const binding = require('@examples/napi-linux-arm64-gnu')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-arm64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -252,7 +322,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-arm-musleabihf')
|
||||
const binding = require('@examples/napi-linux-arm-musleabihf')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-arm-musleabihf/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -263,7 +338,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-arm-gnueabihf')
|
||||
const binding = require('@examples/napi-linux-arm-gnueabihf')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-arm-gnueabihf/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -276,7 +356,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-riscv64-musl')
|
||||
const binding = require('@examples/napi-linux-riscv64-musl')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-riscv64-musl/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -287,7 +372,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-riscv64-gnu')
|
||||
const binding = require('@examples/napi-linux-riscv64-gnu')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-riscv64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -299,7 +389,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-ppc64-gnu')
|
||||
const binding = require('@examples/napi-linux-ppc64-gnu')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-ppc64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -310,7 +405,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-s390x-gnu')
|
||||
const binding = require('@examples/napi-linux-s390x-gnu')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-s390x-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -325,7 +425,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-arm64-ohos')
|
||||
const binding = require('@examples/napi-linux-arm64-ohos')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-arm64-ohos/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -336,7 +441,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-x64-ohos')
|
||||
const binding = require('@examples/napi-linux-x64-ohos')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-x64-ohos/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
@ -347,7 +457,12 @@ function requireNative() {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
try {
|
||||
return require('@examples/napi-linux-arm-ohos')
|
||||
const binding = require('@examples/napi-linux-arm-ohos')
|
||||
const bindingPackageVersion = require('@examples/napi-linux-arm-ohos/package.json').version
|
||||
if (bindingPackageVersion !== '0.0.0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
loadErrors.push(e)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user