-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: first pass on adding version management apis
PR-URL: nodejs/node-addon-api#325 Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
c0008bb
commit 0e39dd7
Showing
9 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# VersionManagement | ||
|
||
The `Napi::VersionManagement` class contains methods that allow information | ||
to be retrieved about the version of N-API and Node.js. In some cases it is | ||
important to make decisions based on different versions of the system. | ||
|
||
## Methods | ||
|
||
### GetNapiVersion | ||
|
||
Retrieves the highest N-API version supported by Node.js runtime. | ||
|
||
```cpp | ||
static uint32_t GetNapiVersion(Env env); | ||
``` | ||
- `[in] env`: The environment in which the API is invoked under. | ||
Returns the highest N-API version supported by Node.js runtime. | ||
### GetNodeVersion | ||
Retrives information about Node.js version present on the system. All the | ||
information is stored in the `napi_node_version` structrue that is defined as | ||
shown below: | ||
```cpp | ||
typedef struct { | ||
uint32_t major; | ||
uint32_t minor; | ||
uint32_t patch; | ||
const char* release; | ||
} napi_node_version; | ||
```` | ||
```cpp | ||
static const napi_node_version* GetNodeVersion(Env env); | ||
``` | ||
|
||
- `[in] env`: The environment in which the API is invoked under. | ||
|
||
Returns the structure a pointer to the structure `napi_node_version` populated by | ||
the version information of Node.js runtime. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Value getNapiVersion(const CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
uint32_t napi_version = VersionManagement::GetNapiVersion(env); | ||
return Number::New(env, napi_version); | ||
} | ||
|
||
Value getNodeVersion(const CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
const napi_node_version* node_version = VersionManagement::GetNodeVersion(env); | ||
Object version = Object::New(env); | ||
version.Set("major", Number::New(env, node_version->major)); | ||
version.Set("minor", Number::New(env, node_version->minor)); | ||
version.Set("patch", Number::New(env, node_version->patch)); | ||
version.Set("release", String::New(env, node_version->release)); | ||
return version; | ||
} | ||
|
||
Object InitVersionManagement(Env env) { | ||
Object exports = Object::New(env); | ||
exports["getNapiVersion"] = Function::New(env, getNapiVersion); | ||
exports["getNodeVersion"] = Function::New(env, getNodeVersion); | ||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function parseVersion() { | ||
const expected = {}; | ||
expected.napi = parseInt(process.versions.napi); | ||
expected.release = process.release.name; | ||
const nodeVersion = process.versions.node.split('.'); | ||
expected.major = parseInt(nodeVersion[0]); | ||
expected.minor = parseInt(nodeVersion[1]); | ||
expected.patch = parseInt(nodeVersion[2]); | ||
return expected; | ||
} | ||
|
||
function test(binding) { | ||
|
||
const expected = parseVersion(); | ||
|
||
const napiVersion = binding.version_management.getNapiVersion(); | ||
assert.strictEqual(napiVersion, expected.napi); | ||
|
||
const nodeVersion = binding.version_management.getNodeVersion(); | ||
assert.strictEqual(nodeVersion.major, expected.major); | ||
assert.strictEqual(nodeVersion.minor, expected.minor); | ||
assert.strictEqual(nodeVersion.patch, expected.patch); | ||
assert.strictEqual(nodeVersion.release, expected.release); | ||
|
||
} |