Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: add lots of version properties #19438

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
compareVersion
  • Loading branch information
devsnek committed Mar 21, 2018
commit 85cbf8d1b035548041071d3efd6b1cf0ddbf0e61
10 changes: 9 additions & 1 deletion doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1507,9 +1507,17 @@ tarball.
* `minorVersion` {number} The minor version of Node.js.
* `patchVersion` {number} The patch version of Node.js.
* `prereleaseTag` {string} The SemVer pre-release tag for Node.js.
* `computedVersion` {number} a number representing the current version, created
* `computedVersion` {number} A number representing the current version, created
using the following method:
`(majorVersion << 16) + (minorVersion << 8) + patchVersion`
* `compareVersion` {function} Perform a SemVer comparison to the release
version.
* `major`
* `minor`
* `patch`
* Returns: {number} `-1` if the given version is lower than the release version,
`0` if the given version matches the process version, and `1` if the given
version is greater than the release version.

<!-- eslint-skip -->
```js
Expand Down
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
_process.setupConfig(NativeModule._source);
_process.setupSignalHandlers();
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
_process.setupCompareVersion();
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/stdio').setup();
Expand Down
13 changes: 12 additions & 1 deletion lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ function setupUncaughtExceptionCapture(exceptionHandlerState) {
};
}

function setupCompareVersion() {
const { computedVersion } = process.release;
process.release.compareVersion = (major, minor, patch) => {
const comp = (major << 16) + (minor << 8) + patch;
if (comp === computedVersion)
return 0;
return comp > computedVersion ? 1 : -1;
};
}

module.exports = {
setup_performance,
setup_cpuUsage,
Expand All @@ -279,5 +289,6 @@ module.exports = {
setupSignalHandlers,
setupChannel,
setupRawDebug,
setupUncaughtExceptionCapture
setupUncaughtExceptionCapture,
setupCompareVersion,
};
5 changes: 5 additions & 0 deletions test/parallel/test-process-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ const {
minorVersion: minor,
patchVersion: patch,
computedVersion,
compareVersion,
} = process.release;

assert.strictEqual(
(major << 16) + (minor << 8) + patch, computedVersion);

assert.strictEqual(0, compareVersion(major, minor, patch));
assert.strictEqual(1, compareVersion(major, minor, patch + 1));
assert.strictEqual(-1, compareVersion(major - 1, minor, patch));