@openziti/ziti-sdk-nodejs 0.25.0 → 0.27.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/lib/connect.js ADDED
@@ -0,0 +1,55 @@
1
+ /*
2
+ Copyright NetFoundry Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ const http = require('node:http');
18
+ const net = require('node:net');
19
+
20
+ const connect = (service, cb) => {
21
+ ziti.ziti_connect(service, (err, sock) => {
22
+ if (err) {
23
+ return cb(err);
24
+ }
25
+
26
+ cb(undefined, new net.Socket({fd: sock}));
27
+ });
28
+ }
29
+
30
+ function getService(protocol, host, port) {
31
+ return ziti.get_ziti_service(protocol, host, port);
32
+ }
33
+
34
+ class HttpAgent extends http.Agent {
35
+ createConnection(options, callback) {
36
+ const service = getService('tcp', options.host, options.port) || options.host;
37
+ connect(service, (err, sock) => {
38
+ if (err) {
39
+ console.error('Error from ziti.ziti_connect:', err);
40
+ // fallback to default connect
41
+ return super.createConnection(options, callback);
42
+ }
43
+
44
+ callback(undefined, sock);
45
+ })
46
+ return undefined
47
+ }
48
+ }
49
+
50
+ const mkAgent = () => {
51
+ return new HttpAgent();
52
+ }
53
+
54
+ exports.connect = connect;
55
+ exports.httpAgent = mkAgent;
package/lib/enroll.js CHANGED
@@ -15,35 +15,43 @@ limitations under the License.
15
15
  */
16
16
 
17
17
 
18
-
19
18
  /**
20
- * on_enroll()
21
- *
22
- */
23
- const on_enroll = ( status ) => {
24
-
25
- };
26
-
27
-
28
- /**
29
- * enroll()
30
- *
19
+ * enroll Enrolls a Ziti Identity using the provided JWT file path.
20
+ * The callback receives an error (if any) or the Ziti configuration JSON upon successful enrollment.
21
+ *
22
+ * if callback is not provided, returns a Promise that resolves with the Ziti configuration JSON or rejects with an error.
23
+ *
31
24
  * @param {*} jwt_path
32
- * @param {*} on_enroll_cb callback
25
+ * @param {*} on_enroll_cb callback (err, cfg) => {} where cfg is the Ziti configuration object
26
+ * @return {Promise|undefined} Returns a Promise if no callback is provided, otherwise returns undefined.
33
27
  */
34
28
  const enroll = ( jwt_path, on_enroll_cb ) => {
35
29
 
36
- let enroll_cb;
37
-
38
- if (typeof on_enroll_cb === 'undefined') {
39
- enroll_cb = on_enroll;
40
- } else {
41
- enroll_cb = on_enroll_cb;
30
+ if (on_enroll_cb) {
31
+ ziti.ziti_enroll(jwt_path, (err, cfg) => {
32
+ if (err) {
33
+ on_enroll_cb(err);
34
+ } else {
35
+ on_enroll_cb(undefined, JSON.parse(cfg));
36
+ }
37
+ });
38
+ return undefined;
42
39
  }
43
40
 
44
- ziti.ziti_enroll(jwt_path, enroll_cb);
45
-
46
- };
47
-
41
+ return new Promise((resolve, reject) => {
42
+ try {
43
+ ziti.ziti_enroll(jwt_path, (err, cfg) => {
44
+ if (err) {
45
+ reject(err);
46
+ } else {
47
+ resolve(JSON.parse(cfg));
48
+ }
49
+ });
50
+ } catch (e) {
51
+ reject(e);
52
+ }
53
+ });
54
+
55
+ }
48
56
 
49
57
  exports.enroll = enroll;
package/lib/ziti.js CHANGED
@@ -225,3 +225,7 @@ exports.serviceAvailable = require('./serviceAvailable').serviceAvailable;
225
225
  * @returns {void} No return value.
226
226
  */
227
227
  exports.write = require('./write').write;
228
+
229
+ const connect = require('./connect')
230
+ exports.connect = connect.connect
231
+ exports.httpAgent = connect.httpAgent
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.25.0",
4
+ "version": "0.27.0",
5
5
  "main": "./lib/ziti",
6
6
  "scripts": {
7
7
  "build": "npm run build:configure && npm run build:make",
@@ -0,0 +1,18 @@
1
+ // import ziti from '@openziti/ziti-sdk-nodejs';
2
+ import ziti from '../ziti.js';
3
+
4
+ const jwt_path = process.argv[2];
5
+
6
+ if (!jwt_path) {
7
+ console.error('Usage: node enroll.mjs <jwt_path>');
8
+ process.exit(1);
9
+ }
10
+
11
+ console.log("JS ziti_Enroll() entered ")
12
+ const identity = await ziti.enroll(jwt_path).catch((err) => {
13
+ console.log('ziti enrollments failed with error: %o', err);
14
+ });
15
+
16
+ // Note: identity is the Ziti configuration JSON object that can be used for ziti.init()
17
+ // or saved to a file for later use.
18
+ console.log(identity);
@@ -0,0 +1,29 @@
1
+ import ziti from '../ziti.js';
2
+ import http from 'node:http';
3
+
4
+ // Usage node http-get.mjs <identity.json> <url>
5
+ // <url> can be a Ziti service intercept (e.g. http://myserver.ziti:8080) or http://<service-name> (port does not matter)
6
+ const IDENTITY_FILE = process.argv[2];
7
+ const URL = process.argv[3];
8
+
9
+ if (!IDENTITY_FILE) {
10
+ console.error('Set ZITI_IDENTITY_FILE to your identity JSON path');
11
+ process.exit(1);
12
+ }
13
+
14
+ console.log('Initializing...');
15
+ await ziti.init(IDENTITY_FILE);
16
+ console.log('Init done');
17
+
18
+ http.get(URL,
19
+ { agent: ziti.httpAgent() },
20
+ (res) => {
21
+ console.log(`HTTP status code: ${res.statusCode} ${res.statusMessage}`);
22
+ for (const k in res.headers) {
23
+ const header = res.headers[k];
24
+ console.log(` ${k}: ${header}`);
25
+ }
26
+ res.on('data', (chunk) => {
27
+ console.log('Received body chunk:', chunk.toString());
28
+ }).on('end', ziti.ziti_shutdown)
29
+ });
@@ -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
+ }