Skip to content

Commit

Permalink
Allow node patch versions to be different on runtime
Browse files Browse the repository at this point in the history
This adjusts the runtime node version check to only compare major and
minor version requirements. Systems that don't use the bundled NodeJS
might update their node due to security patches, which would currently
break OpenSearch Dashboards unecessarily.

```shell
% grep 14.18 ../../package.json
    "node": "14.18.2",
% nvm use 12
Now using node v12.22.8 (npm v6.14.15)
% node node_version_validator.js
OpenSearch Dashboards does not support the current Node.js version v12.22.8. Please use Node.js ~v14.18.
% nvm use 14
Now using node v14.18.3 (npm v6.14.15)
% node node_version_validator.js
%
```

Signed-off-by: Justin Kromlinger <[email protected]>
  • Loading branch information
hashworks committed Jan 28, 2022
1 parent 8777eb3 commit b40dbb3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/setup_node_env/node_version_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ var pkg = require('../../package.json');
var currentVersion = (process && process.version) || null;
var rawRequiredVersion = (pkg && pkg.engines && pkg.engines.node) || null;
var requiredVersion = rawRequiredVersion ? 'v' + rawRequiredVersion : rawRequiredVersion;
var isVersionValid = !!currentVersion && !!requiredVersion && currentVersion === requiredVersion;
var requiredVersionMajorMinor = requiredVersion.match(/^v(\d+\.\d+)/)[1]
var isVersionValid = requiredVersionMajorMinor === currentVersion.match(/^v(\d+\.\d+)/)[1]

// Validates current the NodeJS version compatibility when OpenSearch Dashboards starts.
if (!isVersionValid) {
var errorMessage =
'OpenSearch Dashboards does not support the current Node.js version ' +
currentVersion +
'. Please use Node.js ' +
requiredVersion +
'. Please use Node.js ~v' +
requiredVersionMajorMinor +
'.';

// Actions to apply when validation fails: error report + exit.
Expand Down
21 changes: 21 additions & 0 deletions src/setup_node_env/node_version_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,25 @@ describe('NodeVersionValidator', function () {
done();
});
});

it('should run the script WITHOUT error', function (done) {
var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/)
let major = matches[1]
let minor = matches[2]
let patch = parseInt(matches[3]) + 1 // change patch version to be higher than required

var processVersionOverwrite =
"Object.defineProperty(process, 'version', { value: '" +
"v" + major + "." + minor + "." + patch +
"', writable: true });";
var command =
'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\"";

exec(command, { cwd: __dirname }, function (error, stdout, stderr) {
expect(error).toBeNull();
expect(stderr).toBeDefined();
expect(stderr).toHaveLength(0);
done();
});
});
});

0 comments on commit b40dbb3

Please sign in to comment.