Skip to content

Commit 982e3bd

Browse files
committed
process: add more version properties to release
PR-URL: #19438 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent cef9097 commit 982e3bd

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

doc/api/process.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,21 @@ tarball.
15031503
- `'Argon'` for the 4.x LTS line beginning with 4.2.0.
15041504
- `'Boron'` for the 6.x LTS line beginning with 6.9.0.
15051505
- `'Carbon'` for the 8.x LTS line beginning with 8.9.1.
1506+
* `majorVersion` {number} The major version of Node.js.
1507+
* `minorVersion` {number} The minor version of Node.js.
1508+
* `patchVersion` {number} The patch version of Node.js.
1509+
* `prereleaseTag` {string} The SemVer pre-release tag for Node.js.
1510+
* `computedVersion` {number} A number representing the current version, created
1511+
using the following method:
1512+
`(majorVersion << 16) + (minorVersion << 8) + patchVersion`
1513+
* `compareVersion` {function} Perform a SemVer comparison to the release
1514+
version.
1515+
* `major`
1516+
* `minor`
1517+
* `patch`
1518+
* Returns: {number} `-1` if the given version is lower than the release version,
1519+
`0` if the given version matches the process version, and `1` if the given
1520+
version is greater than the release version.
15061521

15071522
<!-- eslint-skip -->
15081523
```js
@@ -1511,7 +1526,12 @@ tarball.
15111526
lts: 'Argon',
15121527
sourceUrl: 'https://nodejs.org/download/release/v4.4.5/node-v4.4.5.tar.gz',
15131528
headersUrl: 'https://nodejs.org/download/release/v4.4.5/node-v4.4.5-headers.tar.gz',
1514-
libUrl: 'https://nodejs.org/download/release/v4.4.5/win-x64/node.lib'
1529+
libUrl: 'https://nodejs.org/download/release/v4.4.5/win-x64/node.lib',
1530+
majorVersion: 4,
1531+
minorVersion: 4,
1532+
patchVersion: 5,
1533+
prereleaseTag: '',
1534+
computedVersion: 263173,
15151535
}
15161536
```
15171537

lib/internal/bootstrap/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
_process.setupConfig(NativeModule._source);
3939
_process.setupSignalHandlers();
4040
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
41+
_process.setupCompareVersion();
4142
NativeModule.require('internal/process/warning').setup();
4243
NativeModule.require('internal/process/next_tick').setup();
4344
NativeModule.require('internal/process/stdio').setup();

lib/internal/process.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ function setupUncaughtExceptionCapture(exceptionHandlerState) {
269269
};
270270
}
271271

272+
function setupCompareVersion() {
273+
const { computedVersion } = process.release;
274+
process.release.compareVersion = (major, minor, patch) => {
275+
const comp = (major << 16) + (minor << 8) + patch;
276+
if (comp === computedVersion)
277+
return 0;
278+
return comp > computedVersion ? 1 : -1;
279+
};
280+
}
281+
272282
module.exports = {
273283
setup_performance,
274284
setup_cpuUsage,
@@ -279,5 +289,6 @@ module.exports = {
279289
setupSignalHandlers,
280290
setupChannel,
281291
setupRawDebug,
282-
setupUncaughtExceptionCapture
292+
setupUncaughtExceptionCapture,
293+
setupCompareVersion,
283294
};

src/node.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,23 @@ void SetupProcessObject(Environment* env,
30203020
READONLY_PROPERTY(release, "name",
30213021
OneByteString(env->isolate(), NODE_RELEASE));
30223022

3023+
READONLY_PROPERTY(release, "majorVersion",
3024+
Integer::New(env->isolate(), NODE_MAJOR_VERSION));
3025+
READONLY_PROPERTY(release, "minorVersion",
3026+
Integer::New(env->isolate(), NODE_MINOR_VERSION));
3027+
READONLY_PROPERTY(release, "patchVersion",
3028+
Integer::New(env->isolate(), NODE_PATCH_VERSION));
3029+
3030+
READONLY_PROPERTY(release, "prereleaseTag",
3031+
OneByteString(env->isolate(), NODE_TAG));
3032+
3033+
READONLY_PROPERTY(release,
3034+
"computedVersion",
3035+
Integer::New(env->isolate(),
3036+
(NODE_MAJOR_VERSION << 16) +
3037+
(NODE_MINOR_VERSION << 8) +
3038+
NODE_PATCH_VERSION));
3039+
30233040
#if NODE_VERSION_IS_LTS
30243041
READONLY_PROPERTY(release, "lts",
30253042
OneByteString(env->isolate(), NODE_VERSION_LTS_CODENAME));

test/parallel/test-process-release.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ if (versionParts[0] === '4' && versionParts[1] >= 2) {
1818
} else {
1919
assert.strictEqual(process.release.lts, undefined);
2020
}
21+
22+
const {
23+
majorVersion: major,
24+
minorVersion: minor,
25+
patchVersion: patch,
26+
computedVersion,
27+
compareVersion,
28+
} = process.release;
29+
30+
assert.strictEqual(
31+
(major << 16) + (minor << 8) + patch, computedVersion);
32+
33+
assert.strictEqual(0, compareVersion(major, minor, patch));
34+
assert.strictEqual(1, compareVersion(major, minor, patch + 1));
35+
assert.strictEqual(-1, compareVersion(major - 1, minor, patch));

0 commit comments

Comments
 (0)