Skip to content

Commit

Permalink
Update code for Node-API v10
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
jschlight committed Jan 16, 2025
1 parent 99ff9a5 commit 1bfd1c4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ exports.isSupportedVersion = function (napiVersion) {
*/
exports.packageSupportsVersion = function (napiVersion) {
if (pkg.binary && pkg.binary.napi_versions &&
pkg.binary.napi_versions instanceof Array) {
pkg.binary.napi_versions instanceof Array) { // integer array
for (var i = 0; i < pkg.binary.napi_versions.length; i++) {
if (pkg.binary.napi_versions[i] === napiVersion) return true
};
Expand Down Expand Up @@ -165,10 +165,11 @@ var prebuildExists = function (prebuild, napiVersion) {
*/
exports.getBestNapiBuildVersion = function () {
var bestNapiBuildVersion = 0
var napiBuildVersions = exports.getNapiBuildVersions(pkg)
var napiBuildVersions = exports.getNapiBuildVersions(pkg) // array of integer strings
if (napiBuildVersions) {
var ourNapiVersion = exports.getNapiVersion()
napiBuildVersions.forEach(function (napiBuildVersion) {
napiBuildVersions.forEach(function (napiBuildVersionStr) {
var napiBuildVersion = parseInt(napiBuildVersionStr, 10)
if (napiBuildVersion > bestNapiBuildVersion &&
napiBuildVersion <= ourNapiVersion) {
bestNapiBuildVersion = napiBuildVersion
Expand All @@ -181,7 +182,7 @@ exports.getBestNapiBuildVersion = function () {
/**
* Returns an array of N-API versions supported by the package.
*
* @returns {Array<string>}
* @returns {Array<string>|undefined}
*/
exports.getNapiBuildVersions = function () {
var napiBuildVersions = []
Expand All @@ -204,7 +205,7 @@ exports.getNapiBuildVersions = function () {
* @returns {string|undefined}
*/
exports.getNapiVersion = function () {
var version = process.versions.napi // string, can be undefined
var version = process.versions.napi // integer string, can be undefined
if (!version) { // this code should never need to be updated
if (versionArray[0] === 9 && versionArray[1] >= 3) version = '2' // 9.3.0+
else if (versionArray[0] === 8) version = '1' // 8.0.0+
Expand Down
4 changes: 2 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The main repository can be found
* [.isSupportedVersion(napiVersion)](#module_napi-build-utils.isSupportedVersion) ⇒ <code>boolean</code>
* [.logUnsupportedVersion(napiVersion, log)](#module_napi-build-utils.logUnsupportedVersion)
* [.getBestNapiBuildVersion()](#module_napi-build-utils.getBestNapiBuildVersion) ⇒ <code>number</code> \| <code>undefined</code>
* [.getNapiBuildVersions()](#module_napi-build-utils.getNapiBuildVersions) ⇒ <code>Array.&lt;string&gt;</code>
* [.getNapiBuildVersions()](#module_napi-build-utils.getNapiBuildVersions) ⇒ <code>[ &#x27;Array&#x27; ].&lt;string&gt;</code>
* [.getNapiVersion()](#module_napi-build-utils.getNapiVersion) ⇒ <code>string</code> \| <code>undefined</code>

<a name="module_napi-build-utils.isNapiRuntime"></a>
Expand Down Expand Up @@ -68,7 +68,7 @@ supported by the current Node instance.
**Kind**: static method of [<code>napi-build-utils</code>](#module_napi-build-utils)
<a name="module_napi-build-utils.getNapiBuildVersions"></a>

### napi-build-utils.getNapiBuildVersions() ⇒ <code>Array.&lt;string&gt;</code>
### napi-build-utils.getNapiBuildVersions() ⇒ <code>[ &#x27;Array&#x27; ].&lt;string&gt;</code>
Returns an array of N-API versions supported by the package.

**Kind**: static method of [<code>napi-build-utils</code>](#module_napi-build-utils)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"napi_versions": [
2,
2,
3
3,
10
]
}
}
7 changes: 5 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('napi-build-utils', function () {
manifest.binary.should.have.property('note')
manifest.binary.should.have.property('napi_versions')
manifest.binary.napi_versions.should.be.instanceof(Array)
manifest.binary.napi_versions.length.should.equal(3)
manifest.binary.napi_versions.length.should.equal(4)
})
it('isNapiRuntime', () => {
let isNapi = utils.isNapiRuntime('napi')
Expand All @@ -40,9 +40,10 @@ describe('napi-build-utils', function () {
it('getNapiBuildVersions', function () {
let buildVersions = utils.getNapiBuildVersions()
buildVersions.should.be.instanceof(Array)
buildVersions.length.should.equal(2)
buildVersions.length.should.equal(3)
buildVersions[0].should.equal('2')
buildVersions[1].should.equal('3')
buildVersions[2].should.equal('10')
})
it('packageSupportsVersion', function () {
utils.packageSupportsVersion(1).should.equal(false)
Expand Down Expand Up @@ -74,6 +75,8 @@ describe('napi-build-utils', function () {
bestBuildVersion.should.equal(2)
} else if (napiVersion === 3) {
bestBuildVersion.should.equal(3)
} else if (napiVersion >= 10) {
bestBuildVersion.should.equal(10)
} else {
bestBuildVersion.should.equal(3)
}
Expand Down

0 comments on commit 1bfd1c4

Please sign in to comment.