Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/run-npm-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run npm Tests

on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [22, 23]

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@

[![npm](https://img.shields.io/npm/v/napi-build-utils.svg)](https://www.npmjs.com/package/napi-build-utils)
![Node version](https://img.shields.io/node/v/prebuild.svg)
[![Build Status](https://travis-ci.org/inspiredware/napi-build-utils.svg?branch=master)](https://travis-ci.org/inspiredware/napi-build-utils)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Build Status](https://github.com/inspiredware/napi-build-utils/actions/workflows/run-npm-tests.yml/badge.svg)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A set of utilities to assist developers of tools that build [N-API](https://nodejs.org/api/n-api.html#n_api_n_api) native add-ons.
A set of utilities to assist developers of tools that build [Node-API](https://nodejs.org/api/n-api.html#n_api_n_api) native add-ons.

## Background

This module is targeted to developers creating tools that build N-API native add-ons.
This module is targeted to developers creating tools that build Node-API native add-ons.

It implements a set of functions that aid in determining the N-API version supported by the currently running Node instance and the set of N-API versions against which the N-API native add-on is designed to be built. Other functions determine whether a particular N-API version can be built and can issue console warnings for unsupported N-API versions.
It implements a set of functions that aid in determining the Node-API version supported by the currently running Node instance and the set of Node-API versions against which the Node-API native add-on is designed to be built. Other functions determine whether a particular Node-API version can be built and can issue console warnings for unsupported Node-API versions.

Unlike the modules this code is designed to facilitate building, this module is written entirely in JavaScript.
Unlike the modules this code is designed to facilitate building, this module is written entirely in JavaScript.

## Quick start

```bash
$ npm install napi-build-utils
npm install napi-build-utils
```

The module exports a set of functions documented [here](./index.md). For example:

```javascript
var napiBuildUtils = require('napi-build-utils');
var napiVersion = napiBuildUtils.getNapiVersion(); // N-API version supported by Node, or undefined.
var napiVersion = napiBuildUtils.getNapiVersion(); // Node-API version supported by Node, or undefined.
```

## Declaring supported N-API versions
## Declaring supported Node-API versions

Native modules that are designed to work with [N-API](https://nodejs.org/api/n-api.html#n_api_n_api) must explicitly declare the N-API version(s) against which they are coded to build. This is accomplished by including a `binary.napi_versions` property in the module's `package.json` file. For example:
Native modules that are designed to work with [Node-API](https://nodejs.org/api/n-api.html#n_api_n_api) must explicitly declare the Node-API version(s) against which they are coded to build. This is accomplished by including a `binary.napi_versions` property in the module's `package.json` file. For example:

```json
"binary": {
"napi_versions": [2,3]
}
```
```

In the absence of a need to compile against a specific Node-API version, the value `3` is a good choice as this is the Node-API version that was supported when Node-API left experimental status.

Modules that are built against a specific Node-API version will continue to operate indefinitely, even as later versions of Node-API are introduced.

In the absence of a need to compile against a specific N-API version, the value `3` is a good choice as this is the N-API version that was supported when N-API left experimental status.
## History

Modules that are built against a specific N-API version will continue to operate indefinitely, even as later versions of N-API are introduced.
**v2.0.0** This version was introduced to address a limitation when the Node-API version reached `10` in NodeJS `v23.6.0`. There was no change in the API, but a SemVer bump to `2.0.0` was made out of an abundance of caution.

## Support

If you run into problems or limitations, please file an issue and we'll take a look. Pull requests are also welcome.
If you run into problems or limitations, please file an issue and we'll take a look. Pull requests are also welcome.
42 changes: 0 additions & 42 deletions appveyor.yml

This file was deleted.

15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports.isSupportedVersion = function (napiVersion) {

/**
* Determines whether the specified N-API version is supported by the package.
* The N-API version must be preseent in the `package.json`
* The N-API version must be present in the `package.json`
* `binary.napi_versions` array.
*
* @param {number} napiVersion The N-API version to check.
Expand All @@ -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 @@ -134,7 +134,7 @@ exports.logMissingNapiVersions = function (target, prebuild, log) {
* Determines whether the specified N-API version exists in the prebuild
* configuration object.
*
* Note that this function is speicifc to the `prebuild` and `prebuild-install`
* Note that this function is specific to the `prebuild` and `prebuild-install`
* packages.
*
* @param {Object} prebuild A config object created by the `prebuild` package.
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "napi-build-utils",
"version": "1.0.2",
"version": "2.0.0",
"description": "A set of utilities to assist developers of tools that build N-API native add-ons",
"main": "index.js",
"scripts": {
Expand Down 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