feat: support `prebuild-install` for Node-API modules (#800)
Co-authored-by: Tim Fish <tim@timfish.uk>
This commit is contained in:
Родитель
72f21bf612
Коммит
818d98f7c7
|
@ -73,6 +73,7 @@
|
|||
"eslint": "^7.7.0",
|
||||
"eslint-plugin-mocha": "^9.0.0",
|
||||
"mocha": "^9.0.1",
|
||||
"node-api-version": "^0.1.3",
|
||||
"nyc": "^15.1.0",
|
||||
"semantic-release": "^17.1.1",
|
||||
"ts-node": "^10.0.0",
|
||||
|
|
|
@ -3,6 +3,7 @@ import * as detectLibc from 'detect-libc';
|
|||
import * as fs from 'fs-extra';
|
||||
import NodeGyp from 'node-gyp';
|
||||
import * as path from 'path';
|
||||
import { fromElectronVersion as napiVersionFromElectronVersion } from 'node-api-version';
|
||||
import { cacheModuleState } from './cache';
|
||||
import { promisify } from 'util';
|
||||
import { readPackageJson } from './read-package-json';
|
||||
|
@ -105,6 +106,54 @@ export class ModuleRebuilder {
|
|||
return args;
|
||||
}
|
||||
|
||||
async getSupportedNapiVersions(): Promise<number[] | undefined> {
|
||||
const binary = (await this.packageJSONFieldWithDefault(
|
||||
'binary',
|
||||
{}
|
||||
)) as Record<string, number[]>;
|
||||
|
||||
return binary?.napi_versions;
|
||||
}
|
||||
|
||||
async getPrebuildRuntimeArgs(): Promise<string[]> {
|
||||
const napiVersion = await this.getNapiVersion();
|
||||
if (napiVersion) {
|
||||
return [
|
||||
'--runtime=napi',
|
||||
`--target=${napiVersion}`,
|
||||
]
|
||||
} else {
|
||||
return [
|
||||
'--runtime=electron',
|
||||
`--target=${this.rebuilder.electronVersion}`,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
async getNapiVersion(): Promise<number | undefined> {
|
||||
const moduleNapiVersions = await this.getSupportedNapiVersions();
|
||||
|
||||
if (!moduleNapiVersions) {
|
||||
// This is not a Node-API module
|
||||
return;
|
||||
}
|
||||
|
||||
const electronNapiVersion = napiVersionFromElectronVersion(this.rebuilder.electronVersion);
|
||||
|
||||
if (!electronNapiVersion) {
|
||||
throw new Error(`Native module '${this.moduleName}' requires Node-API but Electron v${this.rebuilder.electronVersion} does not support Node-API`);
|
||||
}
|
||||
|
||||
// Filter out Node-API versions that are too high
|
||||
const filteredVersions = moduleNapiVersions.filter((v) => (v <= electronNapiVersion));
|
||||
|
||||
if (filteredVersions.length === 0) {
|
||||
throw new Error(`Native module '${this.moduleName}' supports Node-API versions ${moduleNapiVersions} but Electron v${this.rebuilder.electronVersion} only supports Node-API v${electronNapiVersion}`)
|
||||
}
|
||||
|
||||
return Math.max(...filteredVersions);
|
||||
}
|
||||
|
||||
async buildNodeGypArgsFromBinaryField(): Promise<string[]> {
|
||||
const binary = await this.packageJSONFieldWithDefault('binary', {}) as Record<string, string>;
|
||||
const flags = await Promise.all(Object.entries(binary).map(async ([binaryKey, binaryValue]) => {
|
||||
|
@ -258,6 +307,10 @@ export class ModuleRebuilder {
|
|||
success = true;
|
||||
} catch (err) {
|
||||
d('failed to use prebuild-install:', err);
|
||||
|
||||
if (err?.message?.includes('requires Node-API but Electron')) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
d('built:', this.moduleName);
|
||||
|
@ -304,9 +357,8 @@ export class ModuleRebuilder {
|
|||
prebuildInstallPath,
|
||||
`--arch=${this.rebuilder.arch}`,
|
||||
`--platform=${process.platform}`,
|
||||
'--runtime=electron',
|
||||
`--target=${this.rebuilder.electronVersion}`,
|
||||
`--tag-prefix=${this.rebuilder.prebuildTagPrefix}`
|
||||
`--tag-prefix=${this.rebuilder.prebuildTagPrefix}`,
|
||||
...await this.getPrebuildRuntimeArgs(),
|
||||
],
|
||||
{
|
||||
cwd: this.modulePath,
|
||||
|
|
|
@ -19,8 +19,9 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@newrelic/native-metrics": "5.3.0",
|
||||
"farmhash": "3.0.0",
|
||||
"farmhash": "3.2.1",
|
||||
"level": "6.0.0",
|
||||
"native-hello-world": "2.0.0",
|
||||
"ref-napi": "1.4.2"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
import * as crypto from 'crypto';
|
||||
import * as fs from 'fs-extra';
|
||||
import { promisify } from 'util';
|
||||
import * as stream from 'stream';
|
||||
|
||||
const pipeline = promisify(stream.pipeline);
|
||||
|
||||
export async function determineChecksum(filename: string): Promise<string> {
|
||||
let calculated = '';
|
||||
const file = fs.createReadStream(filename, { encoding: 'binary' });
|
||||
const hasher = crypto.createHash('sha256', { defaultEncoding: 'binary' });
|
||||
hasher.on('readable', () => {
|
||||
const data = hasher.read();
|
||||
if (data) {
|
||||
calculated = data.toString('hex');
|
||||
}
|
||||
});
|
||||
await pipeline(file, hasher);
|
||||
|
||||
return calculated;
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
import EventEmitter = require('events');
|
||||
import { expect } from 'chai';
|
||||
import * as fs from 'fs-extra';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import { spawn } from '@malept/cross-spawn-promise';
|
||||
|
||||
import { determineChecksum } from './helpers/checksum';
|
||||
import { expectNativeModuleToBeRebuilt, expectNativeModuleToNotBeRebuilt } from './helpers/rebuild';
|
||||
import { getExactElectronVersionSync } from './helpers/electron-version';
|
||||
import { rebuild } from '../src/rebuild';
|
||||
import { rebuild, Rebuilder } from '../src/rebuild';
|
||||
import { ModuleRebuilder } from '../src/module-rebuilder';
|
||||
|
||||
const MINUTES_IN_MILLISECONDS = 60 * 1000;
|
||||
const testElectronVersion = getExactElectronVersionSync();
|
||||
|
@ -89,6 +90,44 @@ describe('rebuilder', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('prebuild-install napi', function () {
|
||||
this.timeout(timeoutMinutes * MINUTES_IN_MILLISECONDS);
|
||||
|
||||
before(resetTestModule);
|
||||
after(cleanupTestModule);
|
||||
afterEach(resetMSVSVersion);
|
||||
|
||||
it('should find correct napi version and select napi args', async () => {
|
||||
const rebuilder = new Rebuilder({ buildPath: testModulePath, electronVersion: '8.0.0', arch: process.arch, lifecycle: new EventEmitter() });
|
||||
const modulePath = path.join(testModulePath, 'node_modules', 'farmhash');
|
||||
const modRebuilder = new ModuleRebuilder(rebuilder, modulePath);
|
||||
expect(await modRebuilder.getNapiVersion()).to.equal(3);
|
||||
expect(await modRebuilder.getPrebuildRuntimeArgs()).to.deep.equal([
|
||||
'--runtime=napi',
|
||||
`--target=3`,
|
||||
])
|
||||
});
|
||||
|
||||
it('should not fail running prebuild-install', async () => {
|
||||
process.env.ELECTRON_REBUILD_TESTS = 'true';
|
||||
|
||||
const rebuilder = new Rebuilder({ buildPath: testModulePath, electronVersion: '8.0.0', arch: process.arch, lifecycle: new EventEmitter() });
|
||||
const modulePath = path.join(testModulePath, 'node_modules', 'farmhash');
|
||||
const modRebuilder = new ModuleRebuilder(rebuilder, modulePath);
|
||||
expect(await modRebuilder.rebuildPrebuildModule('')).to.equal(true);
|
||||
});
|
||||
|
||||
it('should throw error with unsupported Electron version', async () => {
|
||||
try {
|
||||
await rebuild({ buildPath: testModulePath, electronVersion: '2.0.0', arch: process.arch });
|
||||
throw new Error('Expected error');
|
||||
} catch (error) {
|
||||
expect(error?.message).to.equal("Native module 'farmhash' requires Node-API but Electron v2.0.0 does not support Node-API");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('force rebuild', function() {
|
||||
this.timeout(timeoutMinutes * MINUTES_IN_MILLISECONDS);
|
||||
|
||||
|
@ -110,7 +149,7 @@ describe('rebuilder', () => {
|
|||
skipped++;
|
||||
});
|
||||
await rebuilder;
|
||||
expect(skipped).to.equal(5);
|
||||
expect(skipped).to.equal(6);
|
||||
});
|
||||
|
||||
it('should rebuild all modules again when disabled but the electron ABI changed', async () => {
|
||||
|
@ -148,21 +187,22 @@ describe('rebuilder', () => {
|
|||
afterEach(cleanupTestModule);
|
||||
|
||||
it('should rebuild only specified modules', async () => {
|
||||
const nativeModuleBinary = path.join(testModulePath, 'node_modules', 'farmhash', 'build', 'Release', 'farmhash.node');
|
||||
const nodeModuleChecksum = await determineChecksum(nativeModuleBinary);
|
||||
const nativeModuleBinary = path.join(testModulePath, 'node_modules', 'native-hello-world', 'build', 'Release', 'hello_world.node');
|
||||
expect(await fs.pathExists(nativeModuleBinary)).to.be.true;
|
||||
await fs.remove(nativeModuleBinary);
|
||||
expect(await fs.pathExists(nativeModuleBinary)).to.be.false;
|
||||
const rebuilder = rebuild({
|
||||
buildPath: testModulePath,
|
||||
electronVersion: testElectronVersion,
|
||||
arch: process.arch,
|
||||
onlyModules: ['farmhash'],
|
||||
onlyModules: ['native-hello-world'],
|
||||
force: true
|
||||
});
|
||||
let built = 0;
|
||||
rebuilder.lifecycle.on('module-done', () => built++);
|
||||
await rebuilder;
|
||||
expect(built).to.equal(1);
|
||||
const electronModuleChecksum = await determineChecksum(nativeModuleBinary);
|
||||
expect(electronModuleChecksum).to.not.equal(nodeModuleChecksum);
|
||||
expect(await fs.pathExists(nativeModuleBinary)).to.be.true;
|
||||
});
|
||||
|
||||
it('should rebuild multiple specified modules via --only option', async () => {
|
||||
|
|
43
yarn.lock
43
yarn.lock
|
@ -2077,7 +2077,7 @@ debug@^3.1.0:
|
|||
dependencies:
|
||||
ms "^2.1.1"
|
||||
|
||||
debuglog@*, debuglog@^1.0.1:
|
||||
debuglog@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
|
||||
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
|
||||
|
@ -3469,7 +3469,7 @@ import-lazy@^2.1.0:
|
|||
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
|
||||
integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM=
|
||||
|
||||
imurmurhash@*, imurmurhash@^0.1.4:
|
||||
imurmurhash@^0.1.4:
|
||||
version "0.1.4"
|
||||
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
|
||||
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
|
||||
|
@ -4413,11 +4413,6 @@ lockfile@^1.0.4:
|
|||
dependencies:
|
||||
signal-exit "^3.0.2"
|
||||
|
||||
lodash._baseindexof@*:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
|
||||
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
|
||||
|
||||
lodash._baseuniq@~4.6.0:
|
||||
version "4.6.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
|
||||
|
@ -4426,33 +4421,11 @@ lodash._baseuniq@~4.6.0:
|
|||
lodash._createset "~4.0.0"
|
||||
lodash._root "~3.0.0"
|
||||
|
||||
lodash._bindcallback@*:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
|
||||
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
|
||||
|
||||
lodash._cacheindexof@*:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
|
||||
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
|
||||
|
||||
lodash._createcache@*:
|
||||
version "3.1.2"
|
||||
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
|
||||
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
|
||||
dependencies:
|
||||
lodash._getnative "^3.0.0"
|
||||
|
||||
lodash._createset@~4.0.0:
|
||||
version "4.0.3"
|
||||
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
|
||||
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
|
||||
|
||||
lodash._getnative@*, lodash._getnative@^3.0.0:
|
||||
version "3.9.1"
|
||||
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
|
||||
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
|
||||
|
||||
lodash._root@~3.0.0:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
|
||||
|
@ -4498,11 +4471,6 @@ lodash.merge@^4.6.2:
|
|||
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
|
||||
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
|
||||
|
||||
lodash.restparam@*:
|
||||
version "3.6.1"
|
||||
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
|
||||
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
|
||||
|
||||
lodash.toarray@^4.4.0:
|
||||
version "4.4.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561"
|
||||
|
@ -5034,6 +5002,13 @@ node-addon-api@^3.1.0:
|
|||
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
|
||||
integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==
|
||||
|
||||
node-api-version@^0.1.3:
|
||||
version "0.1.4"
|
||||
resolved "https://registry.yarnpkg.com/node-api-version/-/node-api-version-0.1.4.tgz#1ed46a485e462d55d66b5aa1fe2821720dedf080"
|
||||
integrity sha512-KGXihXdUChwJAOHO53bv9/vXcLmdUsZ6jIptbvYvkpKfth+r7jw44JkVxQFA3kX5nQjzjmGu1uAu/xNNLNlI5g==
|
||||
dependencies:
|
||||
semver "^7.3.5"
|
||||
|
||||
node-emoji@^1.10.0:
|
||||
version "1.10.0"
|
||||
resolved "https://registry.yarnpkg.com/node-emoji/-/node-emoji-1.10.0.tgz#8886abd25d9c7bb61802a658523d1f8d2a89b2da"
|
||||
|
|
Загрузка…
Ссылка в новой задаче