diff --git a/README.md b/README.md index 700b4db72..e2c0d0ba8 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ still a work in progress as its not yet complete). - [Async Operations](doc/async_operations.md) - [AsyncWorker](doc/async_worker.md) - [Promises](doc/promises.md) + - [Version management](doc/version_management.md) diff --git a/doc/version_management.md b/doc/version_management.md new file mode 100644 index 000000000..f81517a00 --- /dev/null +++ b/doc/version_management.md @@ -0,0 +1,44 @@ +# 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. + diff --git a/napi-inl.h b/napi-inl.h index 46ba2c0b9..bad4a94f9 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3235,6 +3235,24 @@ inline int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in return result; } +//////////////////////////////////////////////////////////////////////////////// +// Version Management class +//////////////////////////////////////////////////////////////////////////////// + +inline uint32_t VersionManagement::GetNapiVersion(Env env) { + uint32_t result; + napi_status status = napi_get_version(env, &result); + NAPI_THROW_IF_FAILED(env, status, 0); + return result; +} + +inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) { + const napi_node_version* result; + napi_status status = napi_get_node_version(env, &result); + NAPI_THROW_IF_FAILED(env, status, 0); + return result; +} + // These macros shouldn't be useful in user code. #undef NAPI_THROW #undef NAPI_THROW_IF_FAILED diff --git a/napi.h b/napi.h index f9852968b..4d0d524e9 100644 --- a/napi.h +++ b/napi.h @@ -1557,6 +1557,13 @@ namespace Napi { static int64_t AdjustExternalMemory(Env env, int64_t change_in_bytes); }; + // Version management + class VersionManagement { + public: + static uint32_t GetNapiVersion(Env env); + static const napi_node_version* GetNodeVersion(Env env); + }; + } // namespace Napi // Inline implementations of all the above class methods are included here. diff --git a/test/binding.cc b/test/binding.cc index 3b4bb7b0a..70006e90f 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -20,6 +20,7 @@ Object InitPromise(Env env); Object InitTypedArray(Env env); Object InitObjectWrap(Env env); Object InitObjectReference(Env env); +Object InitVersionManagement(Env env); Object Init(Env env, Object exports) { exports.Set("arraybuffer", InitArrayBuffer(env)); @@ -41,6 +42,7 @@ Object Init(Env env, Object exports) { exports.Set("typedarray", InitTypedArray(env)); exports.Set("objectwrap", InitObjectWrap(env)); exports.Set("objectreference", InitObjectReference(env)); + exports.Set("version_management", InitVersionManagement(env)); return exports; } diff --git a/test/binding.gyp b/test/binding.gyp index 698eb45b7..5666fae1c 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -25,6 +25,7 @@ 'typedarray.cc', 'objectwrap.cc', 'objectreference.cc', + 'version_management.cc' ], 'include_dirs': ["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; +} diff --git a/test/version_management.js b/test/version_management.js new file mode 100644 index 000000000..b1600c7b4 --- /dev/null +++ b/test/version_management.js @@ -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); + +}