gpt4 book ai didi

node.js - 如何确定已编译 native 模块的 ABI 版本(和其他详细信息)?

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:39 26 4
gpt4 key购买 nike

我正在协作开发一个使用 native 模块(串行端口)的 VSCode 扩展。为了使解决方案在所有平台上稳定运行,并且随着 VScode 更改 Electron 版本的时间的推移,我想包含预构建的 native 模块。

现在看来,这些预编译模块中的一些(不是全部)并不是他们声称的版本。

为了测试/验证,我想以编程方式确定 ABI 版本,如果可能的话,确定 install-prebuild 下载的每个 native 模块的运行时、平台(darwin、linux、win32)和体系结构

即当我尝试在 Electron 5.0.10 (ABI-70) 中加载模块时抛出以下错误:

Uncaught Error: The module '\\?\C:\develop\NodeJS\electron-serialport\node_modules\@serialport\bindings\lib\binding\node-v70-win32-x64\bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 47. This version of Node.js requires
NODE_MODULE_VERSION 70. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

因此它报告 ABI 47,而通过预构建安装下载为 ABI 70。注意:将 Electron 绑定(bind)存储在 Node 绑定(bind)位置,以允许“绑定(bind)”检测 ABI、平台和架构

## npx prebuild-install 
Download prebuild native binding for runtime electron : 5.0.5, abi: 70, win32, x64
prebuild-install info begin Prebuild-install version 5.3.0
prebuild-install info looking for cached prebuild @ C:\Users\josverl\AppData\Roaming\npm-cache\_prebuilds\bde028-bindings-v2.0.8-electron-v70-win32-x64.tar.gz
prebuild-install info found cached prebuild
prebuild-install info unpacking @ C:\Users\josverl\AppData\Roaming\npm-cache\_prebuilds\bde028-bindings-v2.0.8-electron-v70-win32-x64.tar.gz
prebuild-install info unpack resolved to C:\develop\NodeJS\electron-serialport\node_modules\@serialport\bindings\build\Release\bindings.node
prebuild-install info install Successfully installed prebuilt binary!

Copy to :
-> C:\develop\NodeJS\electron-serialport\noded_modules\@serialport\bindings\lib\binding\node-v70-win32-x64\bindings.node

最佳答案

找到了一个不错但部分的解决方案,因为我一直无法找到 API 或真正的跨平台解决方案。

我能得到的最接近的是直接使用 process.dlopen但这仍然只能给出成功或抛出错误。底层loader确实有信息 mp->nm_version,但仅通过错误报告。

所以到目前为止找到的唯一方法是解析该错误消息。限制是它只能在与当前运行时相同的平台和 CPU 架构上工作,但总比没有好。

下面将通过捕获错误消息并从中提取 ABI 来提取 native 模块的 ABI 版本。

// try to determine the ABI version for a native module
const getNativeABI = (filename) => {
var moduleVersion = 0
try {
var test = new Module(filename, null);
process.dlopen(module, filename) //,os.constants.dlopen.RTLD_NOW);
// if this works the node version is the same
moduleVersion = process.versions['modules']
// but now we need to unload it :-(
return moduleVersion
} catch (error) {
var match
var versionRegexp = /NODE_MODULE_VERSION (\d*)./gm
var platformRegexp = /(is not a valid Win32 application|invalid ELF header|wrong ELF class)/g
// check for ABI version mismatch
// Uncaught Error: The module '..\bindings.node'
// was compiled against a different Node.js version using
// NODE_MODULE_VERSION 47. This version of Node.js requires
// NODE_MODULE_VERSION 70. Please try re-compiling or re-installing
match = versionRegexp.exec(error.message)
if (match != null){
return match[1] // first version is that of the module
}
// not for valid on this win32 / linux
match = platformRegexp.exec(error.message)
if (match != null){
// todo: @linux : use error for elfclass to determine architecture :: wrong ELF class: ELFCLASS32
return 0 // can't validate cross platform
}
// other error
console.debug( error.message)
}
return moduleVersion // just in case
}

您需要传入一个虚拟模块结构。

/// dummy copy of  internal function
function Module(id, parent) {
this.id = id;
this.exports = {};
this.parent = parent;
// updateChildren(parent, this, false);
this.filename = null;
this.loaded = false;
this.children = [];
}

关于node.js - 如何确定已编译 native 模块的 ABI 版本(和其他详细信息)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57621585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com