import { fromElectronVersion as napiVersionFromElectronVersion } from 'node-api-version' ; export class NodeAPI { private moduleName : string ; private electronVersion : string ; constructor ( moduleName , electronVersion ) { this . moduleName = moduleName ; this . electronVersion = electronVersion ; } ensureElectronSupport ( ) { this . getVersionForElectron ( ) ; } getVersionForElectron ( ) { 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 ) { const electronNapiVersion = this . getVersionForElectron ( ) ; 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 ) ; } }