forked from electron/rebuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-api.ts
38 lines (28 loc) · 1.26 KB
/
node-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { fromElectronVersion as napiVersionFromElectronVersion } from 'node-api-version';
export class NodeAPI {
private moduleName: string;
private electronVersion: string;
constructor(moduleName: string, electronVersion: string) {
this.moduleName = moduleName;
this.electronVersion = electronVersion;
}
ensureElectronSupport(): void {
this.getVersionForElectron();
}
getVersionForElectron(): number {
const electronNapiVersion = napiVersionFromElectronVersion(this.electronVersion);
if (!electronNapiVersion) {
throw new Error(`Native module '${this.moduleName}' requires Node-API but Electron v${this.electronVersion} does not support Node-API`);
}
return electronNapiVersion;
}
getNapiVersion(moduleNapiVersions: number[]): number {
const electronNapiVersion = this.getVersionForElectron();
// 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.electronVersion} only supports Node-API v${electronNapiVersion}`)
}
return Math.max(...filteredVersions);
}
}