Skip to content
Closed
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
34 changes: 31 additions & 3 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,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}
Expand Down Expand Up @@ -1509,6 +1512,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
Expand All @@ -1517,13 +1524,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: '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as compareVersion is enumerable, it should be listed here.

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
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 @@ -35,6 +35,7 @@
setupGlobalVariables();

const _process = NativeModule.require('internal/process');
_process.setupCompareVersion();
_process.setupConfig(NativeModule._source);
_process.setupSignalHandlers();
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
Expand Down
44 changes: 43 additions & 1 deletion lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -293,5 +334,6 @@ module.exports = {
setupSignalHandlers,
setupChannel,
setupRawDebug,
setupUncaughtExceptionCapture
setupUncaughtExceptionCapture,
setupCompareVersion,
};
11 changes: 11 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
39 changes: 38 additions & 1 deletion test/parallel/test-process-release.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');

const assert = require('assert');
const versionParts = process.versions.node.split('.');
Expand All @@ -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',
});
}