@openziti/ziti-sdk-nodejs 0.28.0 → 0.28.1

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 CHANGED
@@ -15,6 +15,7 @@ limitations under the License.
15
15
  */
16
16
 
17
17
  const http = require('node:http');
18
+ const https = require('node:https');
18
19
  const net = require('node:net');
19
20
 
20
21
  const connect = (dialInfo, cb) => {
@@ -34,18 +35,47 @@ function getDialInfo(protocol, host, port) {
34
35
  return ziti.get_ziti_service(protocol, host, port);
35
36
  }
36
37
 
38
+ function doConnect(options, callback) {
39
+ let dialInfo;
40
+ try {
41
+ dialInfo = getDialInfo('tcp', options.host, options.port);
42
+ } catch (e) {
43
+ // construct forwarding data just in case hosting side needs it
44
+ const data = {
45
+ dst_protocol: "tcp",
46
+ dst_hostname: options.host,
47
+ dst_port: options.port.toString(),
48
+ }
49
+ dialInfo = {
50
+ service: options.host,
51
+ dial_data: JSON.stringify(data),
52
+ }
53
+ }
54
+
55
+ connect(dialInfo, callback)
56
+ return undefined
57
+ }
58
+
59
+
37
60
  class HttpAgent extends http.Agent {
61
+ constructor(options) {
62
+ super(options);
63
+ }
38
64
  createConnection(options, callback) {
39
- let dialInfo;
40
- try {
41
- dialInfo = getDialInfo('tcp', options.host, options.port);
42
- } catch (e) {
43
- dialInfo = {
44
- service: options.host,
65
+ return doConnect(options, (err, sock) => {
66
+ if (err) {
67
+ console.error('Error from ziti.ziti_connect:', err);
68
+ return super.createConnection(options, callback);
45
69
  }
46
- }
47
70
 
48
- connect(dialInfo, (err, sock) => {
71
+ callback(undefined, sock);
72
+ });
73
+ }
74
+ }
75
+
76
+ class HttpsAgent extends https.Agent {
77
+ createConnection(options, callback) {
78
+ return doConnect(options, (err, sock) => {
49
79
  if (err) {
50
80
  console.error('Error from ziti.ziti_connect:', err);
51
81
  // fallback to default connect
@@ -53,13 +83,15 @@ class HttpAgent extends http.Agent {
53
83
  }
54
84
 
55
85
  callback(undefined, sock);
56
- })
57
- return undefined
86
+ });
58
87
  }
59
88
  }
60
89
 
61
- const mkAgent = () => {
62
- return new HttpAgent();
90
+ function mkAgent(url) {
91
+ if (typeof url === 'string') {
92
+ return url.startsWith('http://') ? new HttpAgent() : new HttpsAgent();
93
+ }
94
+ return url === http ? new HttpAgent() : new HttpsAgent();
63
95
  }
64
96
 
65
97
  exports.connect = connect;
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.28.0",
4
+ "version": "0.28.1",
5
5
  "main": "./lib/ziti",
6
6
  "scripts": {
7
7
  "build": "npm run build:configure && npm run build:make",
@@ -1,4 +1,5 @@
1
1
  import ziti from '../ziti.js';
2
+ import https from 'node:https';
2
3
  import http from 'node:http';
3
4
 
4
5
  // Usage node http-get.mjs <identity.json> <url>
@@ -15,8 +16,10 @@ console.log('Initializing...');
15
16
  await ziti.init(IDENTITY_FILE);
16
17
  console.log('Init done');
17
18
 
18
- http.get(URL,
19
- { agent: ziti.httpAgent() },
19
+ const client = URL.startsWith('https://') ? https : http;
20
+
21
+ client.get(URL,
22
+ { agent: ziti.httpAgent(client) },
20
23
  (res) => {
21
24
  console.log(`HTTP status code: ${res.statusCode} ${res.statusMessage}`);
22
25
  for (const k in res.headers) {