index.js
7.04 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict'
// Copyright (c) 2018 inspiredware
var path = require('path')
var pkg = require(path.resolve('package.json'))
var versionArray = process.version
.substr(1)
.replace(/-.*$/, '')
.split('.')
.map(function (item) {
return +item
})
/**
*
* 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.
*
* The main repository can be found
* [here](https://github.com/inspiredware/napi-build-utils#napi-build-utils).
*
* @module napi-build-utils
*/
/**
* Implements a consistent name of `napi` for N-API runtimes.
*
* @param {string} runtime The runtime string.
* @returns {boolean}
*/
exports.isNapiRuntime = function (runtime) {
return runtime === 'napi'
}
/**
* Determines whether the specified N-API version is supported
* by both the currently running Node instance and the package.
*
* @param {string} napiVersion The N-API version to check.
* @returns {boolean}
*/
exports.isSupportedVersion = function (napiVersion) {
var version = parseInt(napiVersion, 10)
return version <= exports.getNapiVersion() && exports.packageSupportsVersion(version)
}
/**
* Determines whether the specified N-API version is supported by the package.
* The N-API version must be preseent in the `package.json`
* `binary.napi_versions` array.
*
* @param {number} napiVersion The N-API version to check.
* @returns {boolean}
* @private
*/
exports.packageSupportsVersion = function (napiVersion) {
if (pkg.binary && pkg.binary.napi_versions &&
pkg.binary.napi_versions instanceof Array) {
for (var i = 0; i < pkg.binary.napi_versions.length; i++) {
if (pkg.binary.napi_versions[i] === napiVersion) return true
};
};
return false
}
/**
* Issues a warning to the supplied log if the N-API version is not supported
* by the current Node instance or if the N-API version is not supported
* by the package.
*
* @param {string} napiVersion The N-API version to check.
* @param {Object} log The log object to which the warnings are to be issued.
* Must implement the `warn` method.
*/
exports.logUnsupportedVersion = function (napiVersion, log) {
if (!exports.isSupportedVersion(napiVersion)) {
if (exports.packageSupportsVersion(napiVersion)) {
log.warn('This Node instance does not support N-API version ' + napiVersion)
} else {
log.warn('This package does not support N-API version ' + napiVersion)
}
}
}
/**
* Issues warnings to the supplied log for those N-API versions not supported
* by the N-API runtime or the package.
*
* Note that this function is specific to the
* [`prebuild`](https://github.com/prebuild/prebuild#prebuild) package.
*
* `target` is the list of targets to be built and is determined in one of
* three ways from the command line arguments:
* (1) `--target` specifies a specific target to build.
* (2) `--all` specifies all N-API versions supported by the package.
* (3) Neither of these specifies to build the single "best version available."
*
* `prebuild` is an array of objects in the form `{runtime: 'napi', target: '2'}`.
* The array contains the list of N-API versions that are supported by both the
* package being built and the currently running Node instance.
*
* The objective of this function is to issue a warning for those items that appear
* in the `target` argument but not in the `prebuild` argument.
* If a specific target is supported by the package (`packageSupportsVersion`) but
* but note in `prebuild`, the assumption is that the target is not supported by
* Node.
*
* @param {(Array<string>|string)} target The N-API version(s) to check. Target is
* @param {Array<Object>} prebuild A config object created by the `prebuild` package.
* @param {Object} log The log object to which the warnings are to be issued.
* Must implement the `warn` method.
* @private
*/
exports.logMissingNapiVersions = function (target, prebuild, log) {
if (exports.getNapiBuildVersions()) {
var targets = [].concat(target)
targets.forEach(function (napiVersion) {
if (!prebuildExists(prebuild, napiVersion)) {
if (exports.packageSupportsVersion(parseInt(napiVersion, 10))) {
log.warn('This Node instance does not support N-API version ' + napiVersion)
} else {
log.warn('This package does not support N-API version ' + napiVersion)
}
}
})
} else {
log.error('Builds with runtime \'napi\' require a binary.napi_versions ' +
'property on the package.json file')
}
}
/**
* 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`
* packages.
*
* @param {Object} prebuild A config object created by the `prebuild` package.
* @param {string} napiVersion The N-APi version to be checked.
* @return {boolean}
* @private
*/
var prebuildExists = function (prebuild, napiVersion) {
if (prebuild) {
for (var i = 0; i < prebuild.length; i++) {
if (prebuild[i].target === napiVersion) return true
}
}
return false
}
/**
* Returns the best N-API version to build given the highest N-API
* version supported by the current Node instance and the N-API versions
* supported by the package, or undefined if a suitable N-API version
* cannot be determined.
*
* The best build version is the greatest N-API version supported by
* the package that is less than or equal to the highest N-API version
* supported by the current Node instance.
*
* @returns {number|undefined}
*/
exports.getBestNapiBuildVersion = function () {
var bestNapiBuildVersion = 0
var napiBuildVersions = exports.getNapiBuildVersions(pkg)
if (napiBuildVersions) {
var ourNapiVersion = exports.getNapiVersion()
napiBuildVersions.forEach(function (napiBuildVersion) {
if (napiBuildVersion > bestNapiBuildVersion &&
napiBuildVersion <= ourNapiVersion) {
bestNapiBuildVersion = napiBuildVersion
}
})
}
return bestNapiBuildVersion === 0 ? undefined : bestNapiBuildVersion
}
/**
* Returns an array of N-API versions supported by the package.
*
* @returns {Array<string>}
*/
exports.getNapiBuildVersions = function () {
var napiBuildVersions = []
// remove duplicates, convert to text
if (pkg.binary && pkg.binary.napi_versions) {
pkg.binary.napi_versions.forEach(function (napiVersion) {
var duplicated = napiBuildVersions.indexOf('' + napiVersion) !== -1
if (!duplicated) {
napiBuildVersions.push('' + napiVersion)
}
})
}
return napiBuildVersions.length ? napiBuildVersions : undefined
}
/**
* Returns the highest N-API version supported by the current node instance
* or undefined if N-API is not supported.
*
* @returns {string|undefined}
*/
exports.getNapiVersion = function () {
var version = process.versions.napi // 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+
}
return version
}