Skip to content
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
Next Next commit
process: add computedVersion
  • Loading branch information
devsnek committed Mar 21, 2018
commit 67f79a51653ff1ecc776b21d62b6a7d236d02b12
14 changes: 14 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,20 @@ The `process.version` property returns the Node.js version string.
console.log(`Version: ${process.version}`);
```

## process.computedVersion

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer dropping this off process.release also to avoid adding anything new to the process object.

<!-- YAML
added: REPLACEME
-->

* {number}

The `process.computedVersion` property is the result of
`(NODE_MAJOR_VERSION << 16) + (NODE_MINOR_VERSION << 8) + NODE_PATCH_VERSION`

This property is useful for inline version checks. Using the process above a
number such as `591872` (for version `9.8.0`) may be selected and easily used
for a version check (`process.computedVersion >= 591872`).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will the fact that the computed version does not incorporate the pre-release tag impact anything here? For instance, every version built on master will have the same computed version until the next major release.

@devsnek devsnek Mar 19, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

is process.computedVersion >= 591872 && process.prerelaseTag good enough?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, as long as folks know to check both. Perhaps an additional note in the docs?


## process.versions
<!-- YAML
added: v0.2.0
Expand Down
7 changes: 7 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,13 @@ void SetupProcessObject(Environment* env,
"version",
FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));

READONLY_PROPERTY(process,
"computedVersion",
Integer::New(env->isolate(),
(NODE_MAJOR_VERSION << 16) +
(NODE_MINOR_VERSION << 8) +
NODE_PATCH_VERSION));

// process.versions
Local<Object> versions = Object::New(env->isolate());
READONLY_PROPERTY(process, "versions", versions);
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-process-computed-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
require('../common');

const assert = require('assert');

// eslint-disable-next-line prefer-const
let [major, minor, patch] = process.version.split('.');
major = major.slice(1);
patch = patch.slice('-')[0];

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