better?sqlite3安裝node?gyp原生模塊編譯prebuild-install
關(guān)于node-gyp
node-gyp是一個(gè)用 Node.js 編寫的跨平臺(tái)命令行工具,用于為 Node.js 編譯本機(jī)插件模塊。它包含之前由 Chromium 團(tuán)隊(duì)使用的 gyp-next項(xiàng)目的供應(yīng)副本,擴(kuò)展以支持 Node.js 原生插件的開發(fā)。
node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It contains a vendored copy of the gyp-next project that was previously used by the Chromium team, extended to support the development of Node.js native addons.
node是跨平臺(tái)的,那么對(duì)于任何的node模塊理論也是應(yīng)該是跨平臺(tái)的。然而,有些node模塊直接或間接使用原生C/C++代碼,這些東西要跨平臺(tái),就需要使用源碼根據(jù)實(shí)際的操作平臺(tái)環(huán)境進(jìn)行原生模塊編譯。通常我們開發(fā)環(huán)境為macOS或Windows,而生產(chǎn)環(huán)境為Linux的各種發(fā)行版,這將導(dǎo)致我們的開發(fā)工作變得沉重不堪。那我們是否可以跳過node-gyp的編譯過程?
prebuild-install
node-gyp的編譯是讓人難受的過程,所以社區(qū)出現(xiàn)了node-pre-gyp和prebuild-install,它們都會(huì)優(yōu)先下載插件作者預(yù)編譯的二進(jìn)制文件,當(dāng)二進(jìn)制文件下載出現(xiàn)問題時(shí),再使用node-gyp進(jìn)行編譯兜底。但因?yàn)槲覀兙W(wǎng)絡(luò)環(huán)境的特殊性,這些二進(jìn)制文件我們大概率是不會(huì)下載成功的,接下來一起來看看在better-sqlite3的安裝過程中prebuild-install干了什么事。
關(guān)于node-pre-gyp參考姊妹文【Nodejs】關(guān)于原生模塊編譯node-gyp + node-pre-gyp (以安裝canvas為例)
better-sqlite3安裝過程追蹤
better-sqlite3就使用了prebuild-install來優(yōu)化構(gòu)建過程
1. 安裝 better-sqlite3
關(guān)于install我們需要了解一點(diǎn)東西, 通常基于表象我們都會(huì)認(rèn)為npm下載解壓并釋放到node_modules后就完成了安裝過程,但實(shí)際上npm還會(huì)檢查package中是否配置了install命令,存在就會(huì)立即執(zhí)行。
npm install better-sqlite3
2. better-sqlite3的package的命令腳本
可以看到better-sqlite3配置了install命令,所以npm下載better-sqlite3后立即執(zhí)行了prebuild-install
{
...,
"scripts": {
"install": "prebuild-install || npm run build-release",
"build-release": "node-gyp rebuild --release",
"build-debug": "node-gyp rebuild --debug",
"rebuild-release": "npm run lzz && npm run build-release",
"rebuild-debug": "npm run lzz && npm run build-debug",
"test": "mocha --exit --slow=75 --timeout=5000",
"benchmark": "node benchmark",
"download": "bash ./deps/download.sh",
"lzz": "lzz -hx hpp -sx cpp -k BETTER_SQLITE3 -d -hl -sl -e ./src/better_sqlite3.lzz"
},
}
3. prebuild-install的package.json
可以看到prebuild-install使用bin將prebuild-install命令鏈接到了bin.js
{
...,
"bin": "./bin.js",
...
}
4. 下載預(yù)構(gòu)建的二進(jìn)制文件
可以看到bin.js中核心函數(shù)就是startDownload
const startDownload = function (downloadUrl) {
download(downloadUrl, opts, function (err) {
if (err) {
log.warn('install', err.message)
return process.exit(1)
}
log.info('install', 'Successfully installed prebuilt binary!')
})
}
if (opts.token) {
asset(opts, function (err, assetId) {
if (err) {
log.warn('install', err.message)
return process.exit(1)
}
startDownload(util.getAssetUrl(opts, assetId))
})
} else {
startDownload(util.getDownloadUrl(opts))
}
其中opts的核心參數(shù)項(xiàng)如下。粘貼的代碼有刪減,完整的請(qǐng)自行查閱源碼
const rc = require('rc')('prebuild-install', {
target: pkgConf.target || env.npm_config_target || process.versions.node,
runtime: pkgConf.runtime || env.npm_config_runtime || 'node',
arch: pkgConf.arch || env.npm_config_arch || process.arch,
libc: libc,
platform: env.npm_config_platform || process.platform,
debug: env.npm_config_debug === 'true',
force: false,
verbose: env.npm_config_verbose === 'true',
buildFromSource: buildFromSource === pkg.name || buildFromSource === 'true',
path: '.',
proxy: env.npm_config_proxy || env.http_proxy || env.HTTP_PROXY,
'https-proxy': env.npm_config_https_proxy || env.https_proxy || env.HTTPS_PROXY,
'local-address': env.npm_config_local_address,
'local-prebuilds': 'prebuilds',
'tag-prefix': 'v',
download: env.npm_config_download
})
opts中并沒有發(fā)現(xiàn)token的蹤跡,那程序?qū)⑦M(jìn)入else分支,接下來我們進(jìn)入getDownloadUrl查看于構(gòu)建二進(jìn)制文件下載地址的拼接邏輯??梢钥吹结槍?duì)包名prebuild-install會(huì)移除包名以@組織開頭的部分
function getDownloadUrl (opts) {
const pkgName = opts.pkg.name.replace(/^@[a-zA-Z0-9_\-.~]+\//, '')
return expandTemplate(urlTemplate(opts), {
name: pkgName,
package_name: pkgName,
version: opts.pkg.version,
major: opts.pkg.version.split('.')[0],
minor: opts.pkg.version.split('.')[1],
patch: opts.pkg.version.split('.')[2],
prerelease: opts.pkg.version.split('-')[1],
build: opts.pkg.version.split('+')[1],
abi: opts.abi || process.versions.modules,
node_abi: process.versions.modules,
runtime: opts.runtime || 'node',
platform: opts.platform,
arch: opts.arch,
libc: opts.libc || '',
configuration: (opts.debug ? 'Debug' : 'Release'),
module_name: opts.pkg.binary && opts.pkg.binary.module_name,
tag_prefix: opts['tag-prefix']
})
}
接下來我們進(jìn)入urlTemplate函數(shù),函數(shù)內(nèi)又調(diào)用了其他的函數(shù),這里我們針對(duì)重要的代碼做說明不粘貼源碼
// 構(gòu)建了二進(jìn)制文件名的模板字符串, 在稍后`{}`中的內(nèi)容將會(huì)被替換掉
const packageName = '{name}-v{version}-{runtime}-v{abi}-{platform}{libc}-{arch}.tar.gz'
// 獲取npmrc中的鏡像配置{包名}_binary_host(或{包名}_binary_host_mirror)
const hostMirrorUrl = getHostMirrorUrl(opts);
// 當(dāng)hostMirrorUrl存在時(shí), 在hostMirrorUrl拼接上版本號(hào)和包信息返回
if (hostMirrorUrl) {
return hostMirrorUrl + '/{tag_prefix}{version}/' + packageName
}
// 當(dāng)hostMirrorUrl不存在時(shí), 將會(huì)檢查包中是否配置了binary以及binary.host, 存在時(shí)返回格式為
// '{binary.host}/{binary.remote_path}/{binary.package_name || packageName}'
// 當(dāng)binary以及binary.host均不存在時(shí), 將會(huì)返回二進(jìn)制文件在github上的托管地址
return github(opts.pkg) + '/releases/download/{tag_prefix}{version}/' + packageName
接下來就是expandTemplate函數(shù)對(duì)urlTemplate返回的下載地址中的占位符做替換處理生成真正的預(yù)構(gòu)建二進(jìn)制文件地址,至此prebuild-install完成了二進(jìn)制文件的下載,跳過了node-pyg的編譯過程
讓prebuild-install通過淘寶源下載預(yù)構(gòu)建文件
npm提供了.npmrc配置文件并注入到進(jìn)程的env環(huán)境變量中,從上面的urlTemplate源碼可知,prebuild-install會(huì)優(yōu)先讀取npm_config_{包名}_binary_host不存在時(shí)繼續(xù)讀取npm_config_{包名}_binary_host_mirror(.npmrc中的變量均會(huì)被npm添加npm_config_前綴, 所以我們配置時(shí)無需添加npm_config_前綴),另外需要值得注意的是npm會(huì)將.npmrc中的鍵以下劃線的方式組織且任何非數(shù)字和字母的字符將會(huì)被替換為_。所以以better-sqlite3舉例來說,以下配置均可被prebuild-install正常讀取。
better_sqlite3_binary_host=https://registry.npmmirror.com/-/binary/better-sqlite3 better_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/better-sqlite3 better-sqlite3_binary_host=https://registry.npmmirror.com/-/binary/better-sqlite3 better-sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/better-sqlite3
鑒于node-pre-gyp只讀取{包名}_binary_host_mirror,針對(duì)原生模塊我們?cè)?code>.npmrc中以{包名}_binary_host_mirror={mirror}的格式配置預(yù)構(gòu)建文件下載鏡像
以上就是better sqlite3安裝node gyp原生模塊編譯prebuild-install的詳細(xì)內(nèi)容,更多關(guān)于node 模塊編譯prebuild install的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
nodejs連接mysql數(shù)據(jù)庫及基本知識(shí)點(diǎn)詳解
這篇文章主要介紹了nodejs連接mysql數(shù)據(jù)庫,結(jié)合實(shí)例形式總結(jié)分析了nodejs連接與操作mysql數(shù)據(jù)庫的相關(guān)模板、配置及mysql數(shù)據(jù)庫查詢、添加數(shù)據(jù)等操作技巧,需要的朋友可以參考下2018-03-03
使用GruntJS構(gòu)建Web程序之Tasks(任務(wù))篇
任務(wù)(Tasks)是grunt的核心概念,你所做的很多工作比如資源合并(concat)、壓縮(uglify)都是在配置任務(wù)。 每次grunt運(yùn)行的時(shí)候,你指定的一個(gè)或多個(gè)任務(wù)也在運(yùn)行,如果你沒有指定任務(wù),那么一個(gè)默認(rèn)名為“default”的任務(wù)將自動(dòng)運(yùn)行。2014-06-06

