@openziti/ziti-sdk-nodejs 0.13.0 → 0.13.2

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  <p align="center" width="100%">
2
2
  OpenZiti is a free and open source project focused on bringing zero trust to any application.
3
+ <br>
4
+ The project provides all the pieces required to implement or integrate zero trust into your solutions.
3
5
  <br/>
4
6
  <br/>
5
7
  Please star us.
@@ -7,8 +9,6 @@ Please star us.
7
9
  <a href="https://github.com/openziti/ziti/stargazers"><img src="https://img.shields.io/github/stars/openziti/ziti?style=flat" ></a>
8
10
  <br/>
9
11
  <br>
10
- The project provides all the pieces required to implement or integrate zero trust into your solutions.
11
- <br>
12
12
  </p>
13
13
 
14
14
  <p align="center" width="100%">
@@ -21,7 +21,7 @@ The project provides all the pieces required to implement or integrate zero trus
21
21
  <br>
22
22
  <br>
23
23
  <b>
24
- A NodeJS-based SDK for delivering secure applications over a <a href="https://openziti.io">Ziti Network</a>
24
+ This repo hosts the OpenZiti SDK for NodeJS, and is designed to help you deliver secure applications over a <a href="https://openziti.io">OpenZiti Network</a>
25
25
  <br>
26
26
  <br>
27
27
  <b>Part of the <a href="https://openziti.io/about">OpenZiti</a> ecosystem</b>
@@ -46,6 +46,9 @@ The project provides all the pieces required to implement or integrate zero trus
46
46
 
47
47
  ---
48
48
 
49
+ # Associated Article(s)
50
+ For more context on this SDK, you may be interested in this
51
+ [article concerning how to secure NodeJS applications](https://openziti.io/securing-nodejs-applications)
49
52
 
50
53
 
51
54
 
@@ -64,9 +67,14 @@ Binaries for most Node versions and platforms are provided by default via [@mapb
64
67
 
65
68
  # Installing
66
69
 
70
+ NPM
67
71
  ``` js
68
72
  npm i @openziti/ziti-sdk-nodejs
69
73
  ```
74
+ or Yarn
75
+ ``` js
76
+ yarn add @openziti/ziti-sdk-nodejs
77
+ ```
70
78
 
71
79
  Special note on previous package:
72
80
 
@@ -81,6 +89,59 @@ npm install @openziti/ziti-sdk-nodejs --save
81
89
 
82
90
  **Note:** the module must be [installed](#installing) before use.
83
91
 
92
+ ESM example (client-side)
93
+ ``` js
94
+ import ziti from '@openziti/ziti-sdk-nodejs';
95
+
96
+ // Somehow provide path to identity file, e.g. via env var
97
+ const zitiIdentityFile = process.env.ZITI_IDENTITY_FILE;
98
+ // Authenticate ourselves onto the Ziti network
99
+ await ziti.init( zitiIdentityFile ).catch(( err ) => { /* probably exit */ });
100
+
101
+ const on_resp_data = ( obj ) => {
102
+ console.log(`response is: ${obj.body.toString('utf8')}`);
103
+ };
104
+
105
+ // Perform an HTTP GET request to a dark OpenZiti web service
106
+ ziti.httpRequest(
107
+ 'myDarkWebService', // OpenZiti Service name or HTTP origin part of the URL
108
+ 'GET',
109
+ '/', // path part of the URL including query params
110
+ ['Accept: application/json' ], // headers
111
+ undefined, // optional on_req cb
112
+ undefined, // optional on_req_data cb
113
+ on_resp_data // optional on_resp_data cb
114
+ );
115
+
116
+ ```
117
+
118
+ ESM example (server-side ExpressJS)
119
+ ``` js
120
+ import ziti from '@openziti/ziti-sdk-nodejs';
121
+ import express from 'express';
122
+ let app = ziti.express( express, zitiServiceName );
123
+ app.listen(ignored, function() { ... }
124
+
125
+ /**
126
+
127
+ That's right.
128
+
129
+ With only a single-line code change (the ziti.express call), your web server is now capable
130
+ of being invisible to malicious attackers on the internet, and only accessible to your
131
+ trusted remote users.
132
+
133
+ Nothing else in your existing ExpressJS web server code needs to change!
134
+
135
+ Existing routing, middleware, etc., all operates the same as it always did...
136
+ but now you enjoy the comfort of knowing that if a connection comes in, it is from
137
+ a trusted identity on the client side.
138
+
139
+ No malicious actors can see your dark web server, and thus, no malicious actors can attack it.
140
+
141
+ */
142
+ ```
143
+
144
+ CJS example (client-side)
84
145
  ``` js
85
146
  var ziti = require('@openziti/ziti-sdk-nodejs');
86
147
 
@@ -147,6 +208,9 @@ const ziti_write = (conn, data) => {
147
208
 
148
209
  # Ziti NodeJS SDK - Setup for Development
149
210
 
211
+ NOTE: You don't need to compile this SDK in order to use it (we publish pre-built binaries for your OS, NodeJS version, and CPU architecture).
212
+ The following information applies only if you are doing development on the SDK itself.
213
+
150
214
  The following steps should get your NodeJS SDK for Ziti building. The Ziti NodeJS SDK is a native addon for Node JS,
151
215
  and is written in C. C development is specific to your operating system and tool chain used. These steps should work
152
216
  properly for you but if your OS has variations you may need to adapt these steps accordingly.
@@ -172,11 +236,10 @@ $ npm run build
172
236
 
173
237
  Getting Help
174
238
  ------------
175
- Please use these community resources for getting help. We use GitHub [issues](https://github.com/NetFoundry/ziti-sdk-nodejs/issues)
239
+ Please use these community resources for getting help. We use GitHub [issues](https://github.com/openziti/ziti-sdk-nodejs/issues)
176
240
  for tracking bugs and feature requests and have limited bandwidth to address them.
177
241
 
178
- - Read the [docs](https://netfoundry.github.io/ziti-doc/ziti/overview.html)
179
- - Join our [Developer Community](https://developer.netfoundry.io)
242
+ - Read the [docs](https://openziti.github.io/ziti/overview.html)
180
243
  - Participate in discussion on [Discourse](https://openziti.discourse.group/)
181
244
 
182
245
 
@@ -48,7 +48,7 @@ const on_resp_data = ( obj ) => {
48
48
 
49
49
  const httpRequest = ( url, method, path, headers, on_req_cb, on_resp_cb, on_resp_data_cb ) => {
50
50
 
51
- console.log('httpRequest entered: ', url, method, headers);
51
+ console.log(`httpRequest entered: httpOrigin|serviceName: "${url}", method: "${method}", path: "${path}", headers: "${headers}"`);
52
52
 
53
53
  let _on_req_cb;
54
54
  let _on_resp_cb;
package/lib/init.js CHANGED
@@ -27,11 +27,8 @@ const init = ( identityPath ) => {
27
27
 
28
28
  let rc = ziti.ziti_init( identityPath, ( result ) => {
29
29
 
30
- setTimeout( () => {
31
-
32
30
  return resolve( result );
33
31
 
34
- }, 1000);
35
32
  });
36
33
 
37
34
  if (rc < 0) {
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.13.0",
4
+ "version": "0.13.2",
5
5
  "main": "./lib/ziti",
6
6
  "scripts": {
7
7
  "build": "npm run build:init; npm run build:c-sdk; npm install --build-from-source --clang=1",
@@ -726,9 +726,9 @@ void on_client(uv_work_t* req, int status) {
726
726
  /**
727
727
  * Initiate an HTTPS request
728
728
  *
729
- * @param {string} [0] serviceName | URL
729
+ * @param {string} [0] serviceName | url Ziti service name or HTTP origin
730
730
  * @param {string} [1] method
731
- * @param {string} [2] path
731
+ * @param {string} [2] path path part of the URL including query params
732
732
  * @param {string[]} [3] headers; Array of strings of the form "name:value"
733
733
  * @param {func} [4] JS on_req callback; This is invoked from 'on_client' function above
734
734
  * @param {func} [5] JS on_resp callback; This is invoked from 'on_resp' function above
package/src/ziti_init.c CHANGED
@@ -21,6 +21,8 @@ ziti_context ztx = NULL;
21
21
  typedef struct {
22
22
  napi_async_work work;
23
23
  napi_threadsafe_function tsfn;
24
+ int zitiContextEventStatus;
25
+ bool on_init_cb_invoked;
24
26
  } AddonData;
25
27
 
26
28
 
@@ -129,16 +131,8 @@ static void on_ziti_event(ziti_context _ztx, const ziti_event_t *event) {
129
131
  ZITI_NODEJS_LOG(ERROR, "Failed to connect to controller: %s", event->event.ctx.err);
130
132
  }
131
133
 
132
- // Initiate the call into the JavaScript callback.
133
- // The call into JavaScript will not have happened
134
- // when this function returns, but it will be queued.
135
- nstatus = napi_call_threadsafe_function(
136
- addon_data->tsfn,
137
- (void*) (long) event->event.ctx.ctrl_status,
138
- napi_tsfn_blocking);
139
- if (nstatus != napi_ok) {
140
- ZITI_NODEJS_LOG(ERROR, "Unable to napi_call_threadsafe_function");
141
- }
134
+ addon_data->zitiContextEventStatus = event->event.ctx.ctrl_status;
135
+
142
136
  break;
143
137
 
144
138
  case ZitiServiceEvent:
@@ -223,6 +217,15 @@ static void on_ziti_event(ziti_context _ztx, const ziti_event_t *event) {
223
217
  free(intercept);
224
218
  }
225
219
 
220
+ // Initiate the call into the JavaScript 'on_init' callback, now that we know about all the services
221
+ ZITI_NODEJS_LOG(DEBUG, "addon_data->on_init_cb_invoked %o", addon_data->on_init_cb_invoked);
222
+ if (!addon_data->on_init_cb_invoked) {
223
+ nstatus = napi_call_threadsafe_function(
224
+ addon_data->tsfn,
225
+ (void*) (long) addon_data->zitiContextEventStatus,
226
+ napi_tsfn_blocking);
227
+ addon_data->on_init_cb_invoked = true;
228
+ }
226
229
 
227
230
  break;
228
231
 
@@ -293,7 +296,7 @@ napi_value _ziti_init(napi_env env, const napi_callback_info info) {
293
296
  // Obtain ptr to JS callback function
294
297
  napi_value js_cb = args[1];
295
298
  napi_value work_name;
296
- AddonData* addon_data = malloc(sizeof(AddonData));
299
+ AddonData* addon_data = calloc(1, sizeof(AddonData));
297
300
 
298
301
  // Create a string to describe this asynchronous operation.
299
302
  status = napi_create_string_utf8(