@openziti/ziti-sdk-nodejs 0.24.2 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CMakeLists.txt CHANGED
@@ -35,6 +35,10 @@ target_include_directories(${PROJECT_NAME}
35
35
  PRIVATE ${CMAKE_JS_INC}
36
36
  )
37
37
 
38
+ target_compile_definitions(${PROJECT_NAME}
39
+ PRIVATE ZITI_LOG_MODULE="ziti-njs"
40
+ )
41
+
38
42
  if (WIN32)
39
43
  target_compile_definitions(${PROJECT_NAME} PRIVATE WIN32_LEAN_AND_MEAN)
40
44
  endif (WIN32)
package/lib/init.js CHANGED
@@ -19,17 +19,24 @@ limitations under the License.
19
19
 
20
20
  return new Promise((resolve, reject) => {
21
21
 
22
- let rc = ziti.ziti_init( identityPath, ( result ) => {
22
+ try {
23
+ ziti.ziti_init( identityPath, ( result ) => {
23
24
 
24
- return resolve( result );
25
-
26
- });
25
+ if (result instanceof Error) {
26
+ return reject(result);
27
+ }
27
28
 
28
- if (rc < 0) {
29
- return reject(`ziti.init() failed with return code ${rc}`);
29
+ return resolve( result );
30
+ });
31
+ } catch (e) {
32
+ reject(e);
30
33
  }
31
-
32
34
  });
33
35
  };
34
36
 
37
+ const shutdown = () => {
38
+ ziti.ziti_shutdown();
39
+ }
40
+
35
41
  exports.init = init;
42
+ exports.shutdown = shutdown;
@@ -26,4 +26,12 @@ const setLogLevel = ( lvl ) => {
26
26
 
27
27
  };
28
28
 
29
+ const setLogger = ( loggerFunc ) => {
30
+
31
+ ziti.ziti_set_logger( loggerFunc );
32
+
33
+ }
34
+
29
35
  exports.setLogLevel = setLogLevel;
36
+ exports.setLogger = setLogger;
37
+
package/lib/ziti.js CHANGED
@@ -21,27 +21,17 @@ limitations under the License.
21
21
  *
22
22
  */
23
23
 
24
- var binding;
25
-
26
- function importAll (r) {
27
- r.keys().forEach(key => {
28
- binding = r(key); // Load the addon
29
- });
24
+ const logger = require('./logger');
25
+
26
+ function load_addon() {
27
+ try {
28
+ return require('../build/Debug/ziti_sdk_nodejs');
29
+ } catch {
30
+ return require('../build/Release/ziti_sdk_nodejs');
31
+ }
30
32
  }
31
33
 
32
- if (typeof require.context == 'function') {
33
-
34
- importAll( require.context("../build/", true, /\.node$/) );
35
-
36
- } else {
37
-
38
- const binary = require('@mapbox/node-pre-gyp');
39
- const path = require('path')
40
- const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')), {debug: false});
41
-
42
- binding = require(binding_path);
43
-
44
- }
34
+ const binding = load_addon();
45
35
 
46
36
  ziti = module.exports = exports = binding;
47
37
 
@@ -188,7 +178,20 @@ exports.listen = require('./listen').listen;
188
178
  * @param {number} level - 0=NONE, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG, 5=TRACE
189
179
  * @returns {void} No return value.
190
180
  */
191
- exports.setLogLevel = require('./setLogLevel').setLogLevel;
181
+ exports.setLogLevel = logger.setLogLevel;
182
+
183
+ /**
184
+ * Set the logger function.
185
+ *
186
+ * **Note**: passing log messages to your own logger
187
+ * function will likely have a negative impact on performance. Use with caution.
188
+ *
189
+ * @function setLogger
190
+ * @param {function} loggerFunc - A function that accepts a log message object
191
+ * with the following properties: { level, source, message }
192
+ * @returns {undefined} No return value.
193
+ */
194
+ exports.setLogger = logger.setLogger;
192
195
 
193
196
  /**
194
197
  * Set the logging level.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openziti/ziti-sdk-nodejs",
3
3
  "description": "A NodeJS-based SDK for delivering secure applications over a Ziti Network",
4
- "version": "0.24.2",
4
+ "version": "0.26.0",
5
5
  "main": "./lib/ziti",
6
6
  "scripts": {
7
7
  "build": "npm run build:configure && npm run build:make",
@@ -11,7 +11,7 @@
11
11
  "build:make": "cmake-js build",
12
12
  "build:package": "node-pre-gyp package",
13
13
  "clean": "cmake-js clean",
14
- "test": "node tests/hello.js",
14
+ "test": "node --test --test-timeout=20000 tests/*.test.*js",
15
15
  "install": "node-pre-gyp install || npm run build",
16
16
  "docs": "jsdoc2md -t API_REFERENCE.hbs lib/*.js > API_REFERENCE.md"
17
17
  },
@@ -38,7 +38,7 @@
38
38
  "nodejs"
39
39
  ],
40
40
  "engines": {
41
- "node": ">=11.0.0"
41
+ "node": ">=20.0.0"
42
42
  },
43
43
  "author": {
44
44
  "name": "OpenZiti",
@@ -0,0 +1,23 @@
1
+ const ziti = require("../ziti.js");
2
+ const assert = require("node:assert");
3
+ const test = require("node:test");
4
+ const suite = test.suite;
5
+
6
+ suite("Ziti SDK Addon Tests", () => {
7
+ test("ziti.exports tests", () => {
8
+ assert(typeof ziti.ziti_sdk_version === "function", "ziti_sdk_version should be a function");
9
+ assert(typeof ziti.enroll === "function", "ziti_enroll should be a function");
10
+ assert(typeof ziti.setLogger === "function", "ziti_set_logger should be a function");
11
+
12
+ })
13
+ test("ziti_sdk_version test", () => {
14
+ const result = ziti.ziti_sdk_version();
15
+ console.log("ziti_sdk_version() result is: ", result);
16
+ assert(result !== "", "ziti_sdk_version should not return an empty string");
17
+ })
18
+ })
19
+
20
+
21
+
22
+
23
+
@@ -0,0 +1,91 @@
1
+ import ziti from "../ziti.js";
2
+ import assert from "node:assert";
3
+ import test from "node:test";
4
+ const suite = test.suite;
5
+
6
+ ziti.setLogLevel(5)
7
+
8
+ suite("Ziti SDK Context Tests", { timeout: 30000 }, () => {
9
+ test.afterEach(() => {
10
+ ziti.ziti_shutdown()
11
+ })
12
+
13
+ test("ziti.shutdown not fail", () => {
14
+ ziti.ziti_shutdown()
15
+ })
16
+
17
+ test("ziti.ziti_init with bad args", (done) => {
18
+ assert.throws(() => {
19
+ ziti.ziti_init()
20
+ }, {
21
+ message: "Too few arguments",
22
+ code: "EINVAL"
23
+ })
24
+ assert.throws(() => {
25
+ ziti.ziti_init("valid-config")
26
+ }, {
27
+ message: "Too few arguments",
28
+ code: "EINVAL"
29
+ })
30
+ })
31
+
32
+ test("ziti.ziti_init with invalid config", (done) => {
33
+ assert.throws(() => {
34
+ ziti.ziti_init("not-a-valid-config", () => {
35
+ assert.fail("should not called")
36
+ })
37
+ },
38
+ {
39
+ message: "configuration not found",
40
+ code: 'EINVAL'
41
+ }
42
+ )
43
+ ziti.init("not-a-valid-config").then(
44
+ () => {
45
+ assert.fail("should not resolve")
46
+ },
47
+ (err) => {
48
+ assert.strictEqual(err.message, 'configuration not found')
49
+ assert.strictEqual(err.code, 'EINVAL')
50
+ }
51
+ )
52
+ assert.rejects(() => ziti.init("not-a-valid-config"),
53
+ {
54
+ code: 'EINVAL',
55
+ message: 'configuration not found'
56
+ })
57
+ })
58
+
59
+ test("incomplete config", () => {
60
+ const cfg = {
61
+ id: {
62
+ cert: 'cert',
63
+ key: 'key',
64
+ ca: 'ca'
65
+ }
66
+ }
67
+
68
+ const cfgStr = JSON.stringify(cfg)
69
+ assert.throws(() => {
70
+ ziti.ziti_init(cfgStr, (err) => {
71
+ assert.fail("should not called")
72
+ })
73
+ })
74
+ assert.rejects(() => ziti.init(cfgStr), { message: 'configuration is invalid'})
75
+ })
76
+
77
+ test("complete but invalid config", async () => {
78
+ const cfg = {
79
+ ztAPI: "http://localhost:6262",
80
+ id: {
81
+ cert: 'cert',
82
+ key: 'key',
83
+ ca: 'ca'
84
+ }
85
+ }
86
+
87
+ const cfgStr = JSON.stringify(cfg)
88
+ await assert.rejects(() => ziti.init(cfgStr), { message: 'configuration is invalid'})
89
+ })
90
+
91
+ })
@@ -0,0 +1,24 @@
1
+ import ziti from "../ziti.js";
2
+ import test from "node:test";
3
+ import assert from "node:assert";
4
+ import { setTimeout as delay } from 'node:timers/promises';
5
+
6
+ test('setLogger test', async () => {
7
+ const messages = [];
8
+ ziti.setLogger(
9
+ (msg) => {
10
+ messages.push(msg);
11
+ }
12
+ );
13
+
14
+ ziti.setLogLevel(5);
15
+ await delay(1000);
16
+ assert(messages.length > 0, "Logger callback should have been called at least once");
17
+ assert(messages.some(msg => msg.message.includes("set log level")),
18
+ "Logger should contain log level set message"
19
+ );
20
+ })
21
+
22
+
23
+
24
+
@@ -0,0 +1,5 @@
1
+ {
2
+ "overlay-ports": [
3
+ "vcpkg-overlays"
4
+ ]
5
+ }
@@ -0,0 +1,5 @@
1
+ update llhttp to 9.3.0 to avoid conflicts with nodejs runtime
2
+
3
+ llhttp@9.3.0 changes the layout of http parser
4
+
5
+ this can be removed once https://github.com/microsoft/vcpkg/issues/49956 is fixed and released
@@ -0,0 +1,15 @@
1
+ diff --git a/CMakeLists.txt b/CMakeLists.txt
2
+ index bdef288..72555c6 100644
3
+ --- a/CMakeLists.txt
4
+ +++ b/CMakeLists.txt
5
+ @@ -77,6 +77,10 @@ function(config_library target)
6
+ NAMESPACE llhttp::
7
+ DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llhttp
8
+ )
9
+ + target_include_directories(${target}
10
+ + PRIVATE include ${CMAKE_CURRENT_BINARY_DIR}
11
+ + INTERFACE $<INSTALL_INTERFACE:include>
12
+ + )
13
+ endfunction(config_library target)
14
+
15
+ if(BUILD_SHARED_LIBS)
@@ -0,0 +1,31 @@
1
+ vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
2
+
3
+ vcpkg_from_github(
4
+ OUT_SOURCE_PATH SOURCE_PATH
5
+ REPO nodejs/llhttp
6
+ REF refs/tags/release/v${VERSION}
7
+ SHA512 6f659bbdc4e7efc431a506a1889d074cbde0c47dc5daca735f3ac4a31d670f62d29882575cabace832c9523c0dcf93192f8810b05f42ed00bd828bf709feee15
8
+ PATCHES
9
+ fix-usage.patch
10
+ )
11
+ string(COMPARE EQUAL "${VCPKG_LIBRARY_LINKAGE}" "static" LLHTTP_BUILD_STATIC)
12
+ string(COMPARE EQUAL "${VCPKG_LIBRARY_LINKAGE}" "dynamic" LLHTTP_BUILD_SHARED)
13
+
14
+ vcpkg_cmake_configure(
15
+ SOURCE_PATH "${SOURCE_PATH}"
16
+ DISABLE_PARALLEL_CONFIGURE
17
+ OPTIONS
18
+ -DBUILD_SHARED_LIBS=${LLHTTP_BUILD_SHARED}
19
+ -DBUILD_STATIC_LIBS=${LLHTTP_BUILD_STATIC}
20
+ )
21
+
22
+ vcpkg_cmake_install()
23
+ vcpkg_copy_pdbs()
24
+
25
+ vcpkg_cmake_config_fixup(
26
+ CONFIG_PATH "/lib/cmake/${PORT}"
27
+ )
28
+ file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
29
+ vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE-MIT")
30
+
31
+ vcpkg_fixup_pkgconfig()
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "llhttp",
3
+ "version": "9.3.0",
4
+ "description": "Port of http_parser to llparse.",
5
+ "homepage": "https://github.com/nodejs/llhttp",
6
+ "license": "MIT",
7
+ "dependencies": [
8
+ {
9
+ "name": "vcpkg-cmake",
10
+ "host": true
11
+ },
12
+ {
13
+ "name": "vcpkg-cmake-config",
14
+ "host": true
15
+ }
16
+ ]
17
+ }
package/tests/hello.js DELETED
@@ -1,18 +0,0 @@
1
-
2
- const bindings = require('bindings')('ziti_sdk_nodejs')
3
-
4
- console.log(bindings)
5
- const result = bindings.ziti_sdk_version();
6
- console.log("ziti_sdk_version() result is: ", result);
7
-
8
-
9
-
10
- const binary = require('@mapbox/node-pre-gyp');
11
- const path = require('path')
12
- // const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')), {debug: true});
13
- const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
14
- console.log("binding_path is: ", binding_path);
15
- const ziti = require(binding_path);
16
- console.log("ziti native addon is: \n", ziti);
17
- require('assert').notEqual(result,"");
18
- console.log("SUCCESS");