@openziti/ziti-sdk-nodejs 0.26.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.26.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
+ });