summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/api/process.md34
-rw-r--r--lib/internal/bootstrap/node.js1
-rw-r--r--lib/internal/process.js44
-rw-r--r--src/node.cc11
-rw-r--r--test/parallel/test-process-release.js39
5 files changed, 124 insertions, 5 deletions
diff --git a/doc/api/process.md b/doc/api/process.md
index 202725cb51..060d741804 100644
--- a/doc/api/process.md
+++ b/doc/api/process.md
@@ -1482,6 +1482,9 @@ changes:
- version: v4.2.0
pr-url: https://github.com/nodejs/node/pull/3212
description: The `lts` property is now supported.
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/19587
+ description: Add SemVer properties.
-->
* {Object}
@@ -1510,6 +1513,10 @@ tarball.
- `'Argon'` for the 4.x LTS line beginning with 4.2.0.
- `'Boron'` for the 6.x LTS line beginning with 6.9.0.
- `'Carbon'` for the 8.x LTS line beginning with 8.9.1.
+* `majorVersion` {number} The major version of this release.
+* `minorVersion` {number} The minor version of this release.
+* `patchVersion` {number} The patch version of this release.
+* `prereleaseTag` {string} The SemVer pre-release tag for Node.js.
<!-- eslint-skip -->
```js
@@ -1518,13 +1525,34 @@ tarball.
lts: 'Argon',
sourceUrl: 'https://nodejs.org/download/release/v4.4.5/node-v4.4.5.tar.gz',
headersUrl: 'https://nodejs.org/download/release/v4.4.5/node-v4.4.5-headers.tar.gz',
- libUrl: 'https://nodejs.org/download/release/v4.4.5/win-x64/node.lib'
+ libUrl: 'https://nodejs.org/download/release/v4.4.5/win-x64/node.lib',
+ majorVersion: 4,
+ minorVersion: 4,
+ patchVersion: 5,
+ prereleaseTag: '',
+ compareVersion: [Function: compareVersion]
}
```
+
In custom builds from non-release versions of the source tree, only the
-`name` property may be present. The additional properties should not be
-relied upon to exist.
+`name` property and SemVer properties may be present. The additional properties
+should not be relied upon to exist.
+
+## process.release.compareVersion(major, minor, patch[, tag])
+<!-- YAML
+added: REPLACEME
+-->
+
+Perform a SemVer comparison to the release version.
+
+* `major` {number} The major version to compare.
+* `minor` {number} The minor version to compare.
+* `patch` {number} The patch version to compare.
+* `tag` {string} The pre-release tag to compare.
+* Returns: {number} `1` if the given version is lower than the current release
+ version, `0` if the given version matches the process version, and `-1`
+ if the given version is greater than the release version.
## process.send(message[, sendHandle[, options]][, callback])
<!-- YAML
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
index 7762aac846..b74c194d07 100644
--- a/lib/internal/bootstrap/node.js
+++ b/lib/internal/bootstrap/node.js
@@ -35,6 +35,7 @@
setupGlobalVariables();
const _process = NativeModule.require('internal/process');
+ _process.setupCompareVersion();
_process.setupConfig(NativeModule._source);
_process.setupSignalHandlers();
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
diff --git a/lib/internal/process.js b/lib/internal/process.js
index f72043264f..027657991a 100644
--- a/lib/internal/process.js
+++ b/lib/internal/process.js
@@ -283,6 +283,47 @@ function setupUncaughtExceptionCapture(exceptionHandlerState) {
};
}
+function setupCompareVersion() {
+ const {
+ majorVersion,
+ minorVersion,
+ patchVersion,
+ prereleaseTag,
+ } = process.release;
+
+ process.release.compareVersion = (major, minor, patch, tag) => {
+ if (typeof major !== 'number')
+ throw new ERR_INVALID_ARG_TYPE('major', 'number', major);
+ if (typeof minor !== 'number')
+ throw new ERR_INVALID_ARG_TYPE('minor', 'number', minor);
+ if (typeof patch !== 'number')
+ throw new ERR_INVALID_ARG_TYPE('patch', 'number', patch);
+ if (tag !== undefined && typeof tag !== 'string')
+ throw new ERR_INVALID_ARG_TYPE('tag', 'string', tag);
+
+ if (major > majorVersion)
+ return -1;
+ if (major < majorVersion)
+ return 1;
+
+ if (minor > minorVersion)
+ return -1;
+ if (minor < minorVersion)
+ return 1;
+
+ if (patch > patchVersion)
+ return -1;
+ if (patch < patchVersion)
+ return 1;
+ if (prereleaseTag)
+ return prereleaseTag === tag ? 0 : 1;
+ if (tag)
+ return -1;
+
+ return 0;
+ };
+}
+
module.exports = {
setup_performance,
setup_cpuUsage,
@@ -293,5 +334,6 @@ module.exports = {
setupSignalHandlers,
setupChannel,
setupRawDebug,
- setupUncaughtExceptionCapture
+ setupUncaughtExceptionCapture,
+ setupCompareVersion,
};
diff --git a/src/node.cc b/src/node.cc
index 335764c302..6f946ab3ad 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -3015,6 +3015,17 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(release, "name",
OneByteString(env->isolate(), NODE_RELEASE));
+ READONLY_PROPERTY(release, "majorVersion",
+ Integer::New(env->isolate(), NODE_MAJOR_VERSION));
+ READONLY_PROPERTY(release, "minorVersion",
+ Integer::New(env->isolate(), NODE_MINOR_VERSION));
+ READONLY_PROPERTY(release, "patchVersion",
+ Integer::New(env->isolate(), NODE_PATCH_VERSION));
+ std::string node_tag(NODE_TAG);
+ READONLY_PROPERTY(release, "prereleaseTag",
+ OneByteString(env->isolate(), node_tag.size() > 0 ?
+ node_tag.substr(1).c_str() : ""));
+
#if NODE_VERSION_IS_LTS
READONLY_PROPERTY(release, "lts",
OneByteString(env->isolate(), NODE_VERSION_LTS_CODENAME));
diff --git a/test/parallel/test-process-release.js b/test/parallel/test-process-release.js
index 8b6bca9141..a7c8e8fa09 100644
--- a/test/parallel/test-process-release.js
+++ b/test/parallel/test-process-release.js
@@ -1,6 +1,6 @@
'use strict';
-require('../common');
+const common = require('../common');
const assert = require('assert');
const versionParts = process.versions.node.split('.');
@@ -18,3 +18,40 @@ if (versionParts[0] === '4' && versionParts[1] >= 2) {
} else {
assert.strictEqual(process.release.lts, undefined);
}
+
+const {
+ majorVersion: major,
+ minorVersion: minor,
+ patchVersion: patch,
+ prereleaseTag: tag,
+ compareVersion,
+} = process.release;
+
+assert.strictEqual(compareVersion(major, minor, patch, tag), 0);
+
+assert.strictEqual(compareVersion(major + 1, minor, patch, tag), -1);
+assert.strictEqual(compareVersion(major - 1, minor, patch, tag), 1);
+
+assert.strictEqual(compareVersion(major, minor + 1, patch, tag), -1);
+assert.strictEqual(compareVersion(major, minor - 1, patch, tag), 1);
+
+assert.strictEqual(compareVersion(major, minor, patch + 1, tag), -1);
+assert.strictEqual(compareVersion(major, minor, patch - 1, tag), 1);
+
+if (tag)
+ assert.strictEqual(compareVersion(major, minor, patch), 1);
+else
+ assert.strictEqual(compareVersion(major, minor, patch, 'notrealtag'), -1);
+
+for (const args of [
+ ['', 0, 0, ''],
+ [0, '', 0, ''],
+ [0, 0, '', ''],
+ [0, 0, 0, 0],
+]) {
+ common.expectsError(() => {
+ compareVersion(...args);
+ }, {
+ code: 'ERR_INVALID_ARG_TYPE',
+ });
+}