@openziti/ziti-sdk-nodejs 0.8.0 → 0.9.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.
@@ -228,4 +228,4 @@ jobs:
228
228
  token: ${{ secrets.NPM_TOKEN }}
229
229
  access: public
230
230
  if: |
231
- matrix.config.os == 'ubuntu-18.04' && matrix.config.node == '14' && steps.extract_branch.outputs.branch == 'main'
231
+ matrix.config.os == 'ubuntu-18.04' && matrix.config.node == '14' && matrix.architecture == 'x64' && steps.extract_branch.outputs.branch == 'main'
package/lib/close.js ADDED
@@ -0,0 +1,29 @@
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
+
18
+ /**
19
+ * close()
20
+ *
21
+ * @param {*} conn
22
+ */
23
+ const close = ( conn, ) => {
24
+
25
+ ziti.ziti_close( conn );
26
+
27
+ };
28
+
29
+ exports.close = close;
@@ -16,6 +16,8 @@ limitations under the License.
16
16
 
17
17
  const zitiListen = require('./listen').listen;
18
18
  const EventEmitter = require('events');
19
+ const { ZitiSocket } = require('./ziti-socket');
20
+
19
21
 
20
22
  const normalizedArgsSymbol = Symbol('normalizedArgs');
21
23
 
@@ -30,8 +32,6 @@ var _servers = new Map();
30
32
  */
31
33
  Server.prototype.on_listen = ( status ) => {
32
34
 
33
- console.log('----------- Now inside on_listen callback ----------, status is: %o', status);
34
-
35
35
  };
36
36
 
37
37
  /**
@@ -41,8 +41,6 @@ Server.prototype.on_listen = ( status ) => {
41
41
  */
42
42
  Server.prototype.on_listen_client = ( obj ) => {
43
43
 
44
- console.log('----------- Now inside on_listen_client callback ----------, obj is: %o', obj);
45
-
46
44
  };
47
45
 
48
46
  /**
@@ -51,9 +49,7 @@ Server.prototype.on_listen = ( status ) => {
51
49
  * @param {*} obj
52
50
  */
53
51
  Server.prototype.on_client_write = ( obj ) => {
54
-
55
- console.log('----------- Now inside on_client_write callback ----------, obj is: %o', obj);
56
-
52
+
57
53
  };
58
54
 
59
55
  /**
@@ -63,16 +59,13 @@ Server.prototype.on_listen = ( status ) => {
63
59
  */
64
60
  Server.prototype.on_listen_client_connect = ( obj ) => {
65
61
 
66
- console.log('----------- Now inside on_listen_client_connect callback ----------, obj is: %o', obj);
67
-
68
62
  let self = _servers.get(obj.js_arb_data);
69
63
 
70
- // console.log('----------- Now inside on_listen_client_connect callback ----------, self is: %o', self);
64
+ const socket = new ZitiSocket({ client: obj.client });
71
65
 
72
- console.log('----------- Now inside on_listen_client_connect callback ----------, emitting `connection` event for client/socket: %o', obj.client);
73
- self.emit('connection', obj.client);
74
-
75
-
66
+ self._socket = socket;
67
+
68
+ self.emit('connection', socket);
76
69
  };
77
70
 
78
71
  /**
@@ -81,13 +74,11 @@ Server.prototype.on_listen = ( status ) => {
81
74
  * @param {*} obj
82
75
  */
83
76
  Server.prototype.on_listen_client_data = ( obj ) => {
84
-
85
- console.log('----------- Now inside on_listen_client_data callback ----------, obj is: %o', obj);
86
-
87
- if (obj.app_data) {
88
- console.log('----------- app_data ----------, app_data string is: \n%o', obj.app_data.toString());
89
- }
90
-
77
+
78
+ let self = _servers.get(obj.js_arb_data);
79
+ let socket = self._socket;
80
+
81
+ socket.captureData(obj.app_data);
91
82
  };
92
83
 
93
84
 
@@ -173,7 +164,6 @@ function Server(serviceName, options, connectionListener) {
173
164
  this.noDelay = Boolean(options.noDelay);
174
165
  this.keepAlive = Boolean(options.keepAlive);
175
166
  this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);
176
-
177
167
  }
178
168
  Object.setPrototypeOf(Server.prototype, EventEmitter.prototype);
179
169
  Object.setPrototypeOf(Server, EventEmitter);
@@ -181,11 +171,8 @@ Object.setPrototypeOf(Server, EventEmitter);
181
171
 
182
172
  Server.prototype.listen = function( serviceName, ...args ) {
183
173
 
184
- console.log('=======================> express-listener: Server.prototype.listen() entered: arguments: ', arguments);
185
-
186
174
  let normalized = normalizeArgs(args);
187
175
  normalized = normalizeArgs(normalized[0]);
188
- console.log('=======================> express-listener: Server.prototype.listen() normalized: ', normalized);
189
176
 
190
177
  // let options = normalized[0]; // we currently ignore options (a.k.a. `port`)
191
178
  let cb = normalized[1];
@@ -195,7 +182,6 @@ Server.prototype.listen = function( serviceName, ...args ) {
195
182
  _servers.set(index, this);
196
183
 
197
184
  zitiListen( serviceName, index, cb, this.on_listen_client, this.on_listen_client_connect, this.on_listen_client_data );
198
-
199
185
  };
200
186
 
201
187
  Server.prototype.address = function() {
@@ -263,5 +249,5 @@ Object.defineProperty(Server.prototype, 'listening', {
263
249
 
264
250
  module.exports = {
265
251
  Server,
266
- };
252
+ };
267
253
 
package/lib/express.js CHANGED
@@ -14,14 +14,18 @@ See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */
16
16
 
17
- const net = require('./express-listener');
18
-
17
+ const expressListener = require('./express-listener');
19
18
  const { Server } = require('_http_server'); // from NodeJS internals
20
19
 
21
20
 
22
21
 
23
-
24
- const getWrappedExpressApp = ( express, serviceName ) => {
22
+ /**
23
+ * express()
24
+ *
25
+ * @param {*} express
26
+ * @param {*} serviceName
27
+ */
28
+ const express = ( express, serviceName ) => {
25
29
 
26
30
  var wrappedExpressApp = express();
27
31
 
@@ -30,47 +34,19 @@ const getWrappedExpressApp = ( express, serviceName ) => {
30
34
  *
31
35
  * A node `http.Server` is returned, with this
32
36
  * application (which is a `Function`) as its
33
- * callback. If you wish to create both an HTTP
34
- * and HTTPS server you may do so with the "http"
35
- * and "https" modules as shown here:
36
- *
37
- * var http = require('http')
38
- * , https = require('https')
39
- * , express = require('express')
40
- * , app = express();
41
- *
42
- * http.createServer(app).listen(80);
43
- * https.createServer({ ... }, app).listen(443);
37
+ * callback.
44
38
  *
45
39
  * @return {http.Server}
46
40
  * @public
47
41
  */
48
42
  wrappedExpressApp.listen = function() {
49
43
 
50
- console.log('=======================> wrappedExpressApp.listen() entered: arguments: ', arguments);
51
-
52
- // var server = http.createServer(this);
53
- // console.log('=======================> wrappedExpressApp.listen() 1 server: ', server);
54
-
55
- Object.setPrototypeOf(Server.prototype, net.Server.prototype);
56
- Object.setPrototypeOf(Server, net.Server);
44
+ Object.setPrototypeOf(Server.prototype, expressListener.Server.prototype);
45
+ Object.setPrototypeOf(Server, expressListener.Server);
57
46
  var server = new Server(this);
58
47
 
48
+ expressListener.Server.call( server, serviceName, { } );
59
49
 
60
- net.Server.call(
61
- server,
62
- serviceName,
63
- {
64
- // allowHalfOpen: true,
65
- // noDelay: options.noDelay,
66
- // keepAlive: options.keepAlive,
67
- // keepAliveInitialDelay: options.keepAliveInitialDelay
68
- });
69
-
70
-
71
- // zitiListen( serviceName, on_listen, on_listen_client, on_listen_client_connect, on_listen_client_data );
72
-
73
- // return server.listen.apply(server, serviceName, arguments);
74
50
  return server.listen(serviceName, arguments);
75
51
 
76
52
  };
@@ -79,42 +55,4 @@ const getWrappedExpressApp = ( express, serviceName ) => {
79
55
 
80
56
  };
81
57
 
82
-
83
- /**
84
- * express()
85
- *
86
- * @param {*} express
87
- * @param {*} serviceName
88
- */
89
- const express = ( express, serviceName ) => {
90
-
91
- var app = getWrappedExpressApp( express, serviceName);
92
-
93
- // console.log('wrappedExpressApp: ', app);
94
-
95
- // const wrappedExpressResponse = Object.create( app.response, {
96
-
97
- // data: {
98
- // value: function(data) {
99
- // return this.status(200).json({status: true, data: data});
100
- // },
101
- // },
102
-
103
- // message: {
104
- // value: function(msg) {
105
- // return this.status(200).json({status: true, message: msg});
106
- // },
107
- // },
108
-
109
- // });
110
-
111
- // app.response = Object.create(wrappedExpressResponse);
112
-
113
- return app;
114
-
115
- };
116
-
117
58
  exports.express = express;
118
-
119
-
120
-
package/lib/write.js CHANGED
@@ -15,6 +15,15 @@ limitations under the License.
15
15
  */
16
16
 
17
17
 
18
+ /**
19
+ * on_write()
20
+ *
21
+ */
22
+ const on_write = ( status ) => {
23
+
24
+ };
25
+
26
+
18
27
  /**
19
28
  * write()
20
29
  *
@@ -22,9 +31,17 @@ limitations under the License.
22
31
  * @param {*} buf
23
32
  * @param {*} on_write callback
24
33
  */
25
- const write = ( conn, buf, on_write ) => {
34
+ const write = ( conn, buf, on_write_cb ) => {
35
+
36
+ let cb;
37
+
38
+ if (typeof on_write_cb === 'undefined') {
39
+ cb = on_write;
40
+ } else {
41
+ cb = on_write_cb;
42
+ }
26
43
 
27
- ziti.ziti_write( conn, buf, on_write );
44
+ ziti.ziti_write( conn, buf, cb );
28
45
 
29
46
  };
30
47
 
@@ -0,0 +1,119 @@
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 EventEmitter = require('events');
18
+ const stream = require('node:stream');
19
+ const zitiWrite = require('./write').write;
20
+
21
+
22
+
23
+ class ZitiSocket extends EventEmitter {
24
+
25
+ constructor(opts) {
26
+
27
+ super();
28
+
29
+ if (typeof opts !== 'undefined') {
30
+ if (typeof opts.client !== 'undefined') {
31
+ this.client = opts.client;
32
+ }
33
+ }
34
+
35
+ this._writableState = new stream.Writable.WritableState({}, this, true);
36
+
37
+ /**
38
+ * This stream is where we'll put any data returned from Ziti (see on_listen_client_data cb)
39
+ */
40
+ let self = this;
41
+ this.readableZitiStream = new ReadableStream({
42
+ start(controller) {
43
+ self.readableZitiStreamController = controller;
44
+ }
45
+ });
46
+ }
47
+
48
+
49
+ /**
50
+ *
51
+ */
52
+ captureData(data) {
53
+
54
+ if ((typeof data !== 'undefined') && (data.byteLength > 0)) {
55
+
56
+ this.readableZitiStreamController.enqueue(data);
57
+ this.emit('data', data);
58
+
59
+ } else {
60
+
61
+ this.emit('close');
62
+
63
+ }
64
+ }
65
+
66
+
67
+ /**
68
+ * Implements the writeable stream method `_write` by pushing the data onto the underlying Ziti connection.
69
+ */
70
+ async write(chunk, encoding, cb) {
71
+
72
+ let buffer;
73
+
74
+ if (typeof chunk === 'string' || chunk instanceof String) {
75
+ buffer = Buffer.from(chunk, 'utf8');
76
+ } else if (Buffer.isBuffer(chunk)) {
77
+ buffer = chunk;
78
+ } else if (chunk instanceof Uint8Array) {
79
+ buffer = Buffer.from(chunk, 'utf8');
80
+ } else {
81
+ throw new Error('chunk type of [' + typeof chunk + '] is not a supported type');
82
+ }
83
+
84
+ if (buffer.length > 0) {
85
+ zitiWrite(this.client, buffer);
86
+ }
87
+ if (cb) {
88
+ cb();
89
+ }
90
+ }
91
+
92
+ /**
93
+ *
94
+ */
95
+ _read() { /* NOP */ }
96
+ read() { /* NOP */ }
97
+ destroy() { /* NOP */ }
98
+ cork() { /* NOP */ }
99
+ uncork() { /* NOP */ }
100
+ pause() { /* NOP */ }
101
+ resume() { /* NOP */ }
102
+ destroy() { /* NOP */ }
103
+ end(data, encoding, callback) { /* NOP */ }
104
+ _final(cb) { cb(); }
105
+ setTimeout() { /* NOP */ }
106
+ setNoDelay() { /* NOP */ }
107
+ unshift(head) { /* NOP */ }
108
+ }
109
+
110
+ Object.defineProperty(ZitiSocket.prototype, 'writable', {
111
+ get() {
112
+ return (
113
+ true
114
+ );
115
+ }
116
+ });
117
+
118
+
119
+ exports.ZitiSocket = ZitiSocket;
package/lib/ziti.js CHANGED
@@ -18,7 +18,6 @@ var binding;
18
18
 
19
19
  function importAll (r) {
20
20
  r.keys().forEach(key => {
21
- console.log('importAll() addon key is: ', key);
22
21
  binding = r(key); // Load the addon
23
22
  });
24
23
  }
@@ -44,6 +43,7 @@ ziti = module.exports = exports = binding;
44
43
  /**
45
44
  * Attach the external, app-facing, API to the 'ziti' object
46
45
  */
46
+ exports.close = require('./close').close;
47
47
  exports.express = require('./express').express;
48
48
  exports.init = require('./init').init;
49
49
  exports.listen = require('./listen').listen;
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.8.0",
4
+ "version": "0.9.0",
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",
package/src/ziti_listen.c CHANGED
@@ -50,7 +50,7 @@ static void CallJs_on_listen_client_data(napi_env env, napi_value js_cb, void* c
50
50
  if (env != NULL) {
51
51
 
52
52
  // const obj = {}
53
- napi_value undefined, js_client_item, js_client, js_buffer;
53
+ napi_value undefined, js_client_item, js_client, js_buffer, js_arb_data;
54
54
  void* result_data;
55
55
 
56
56
  // Retrieve the JavaScript `undefined` value so we can use it as the `this`
@@ -65,6 +65,21 @@ static void CallJs_on_listen_client_data(napi_env env, napi_value js_cb, void* c
65
65
  napi_throw_error(env, "EINVAL", "failure to create object");
66
66
  }
67
67
 
68
+ // js_client_item.js_arb_data = js_arb_data
69
+ if (item->js_arb_data) {
70
+ rc = napi_create_int64(env, item->js_arb_data, &js_arb_data);
71
+ if (rc != napi_ok) {
72
+ napi_throw_error(env, "EINVAL", "failure to create obj.js_arb_data");
73
+ }
74
+ rc = napi_set_named_property(env, js_client_item, "js_arb_data", js_arb_data);
75
+ if (rc != napi_ok) {
76
+ napi_throw_error(env, "EINVAL", "failure to set named property status");
77
+ }
78
+ ZITI_NODEJS_LOG(DEBUG, "js_arb_data: %lld", item->js_arb_data);
79
+ } else {
80
+ rc = napi_set_named_property(env, js_client_item, "js_arb_data", undefined);
81
+ }
82
+
68
83
  // js_client_item.client = client
69
84
  napi_create_int64(env, (int64_t)item->client, &js_client);
70
85
  if (rc != napi_ok) {
@@ -472,13 +487,13 @@ void on_listen_client(ziti_connection serv, ziti_connection client, int status,
472
487
 
473
488
  const char *source_identity = clt_ctx->caller_id;
474
489
  if (source_identity != NULL) {
475
- ZITI_NODEJS_LOG(DEBUG, "on_listen_client: incoming connection from '%s'\n", source_identity );
490
+ ZITI_NODEJS_LOG(DEBUG, "on_listen_client: incoming connection from '%s'", source_identity );
476
491
  }
477
492
  else {
478
493
  ZITI_NODEJS_LOG(DEBUG, "on_listen_client: incoming connection from unidentified client" );
479
494
  }
480
495
  if (clt_ctx->app_data != NULL) {
481
- ZITI_NODEJS_LOG(DEBUG, "on_listen_client: got app data '%.*s'!\n", (int) clt_ctx->app_data_sz, clt_ctx->app_data );
496
+ ZITI_NODEJS_LOG(DEBUG, "on_listen_client: got app data '%.*s'!", (int) clt_ctx->app_data_sz, clt_ctx->app_data );
482
497
  }
483
498
 
484
499
  ziti_accept(client, on_listen_client_connect, on_listen_client_data);
@@ -715,7 +730,7 @@ napi_value _ziti_listen(napi_env env, const napi_callback_info info) {
715
730
  }
716
731
 
717
732
  // Start listening
718
- ZITI_NODEJS_LOG(DEBUG, "calling ziti_listen_with_options: %p", ztx);
733
+ ZITI_NODEJS_LOG(DEBUG, "calling ziti_listen_with_options: %p, addon_data: %p", ztx, addon_data);
719
734
  ziti_listen_opts listen_opts = {
720
735
  .bind_using_edge_identity = false,
721
736
  };
package/src/ziti_write.c CHANGED
@@ -32,6 +32,8 @@ typedef struct WriteItem {
32
32
  static void CallJs_on_write(napi_env env, napi_value js_cb, void* context, void* data) {
33
33
  napi_status status;
34
34
 
35
+ ZITI_NODEJS_LOG(DEBUG, "CallJs_on_write entered");
36
+
35
37
  // This parameter is not used.
36
38
  (void) context;
37
39
 
@@ -106,6 +108,8 @@ static void on_write(ziti_connection conn, ssize_t status, void *ctx) {
106
108
 
107
109
  ConnAddonData* addon_data = (ConnAddonData*) ziti_conn_data(conn);
108
110
 
111
+ ZITI_NODEJS_LOG(DEBUG, "on_write cb entered: addon_data: %p", addon_data);
112
+
109
113
  WriteItem* item = memset(malloc(sizeof(*item)), 0, sizeof(*item));
110
114
  item->conn = conn;
111
115
  item->status = status;
@@ -164,6 +168,8 @@ napi_value _ziti_write(napi_env env, const napi_callback_info info) {
164
168
 
165
169
  // Obtain ptr to JS 'write' callback function
166
170
  napi_value js_write_cb = args[2];
171
+ ZITI_NODEJS_LOG(DEBUG, "js_write_cb: %p", js_write_cb);
172
+
167
173
  napi_value work_name;
168
174
 
169
175
  // Create a string to describe this asynchronous operation.
@@ -195,7 +201,9 @@ napi_value _ziti_write(napi_env env, const napi_callback_info info) {
195
201
  }
196
202
 
197
203
  // Now, call the C-SDK to actually write the data over to the service
204
+ ZITI_NODEJS_LOG(DEBUG, "call ziti_write");
198
205
  ziti_write(conn, chunk, bufferLength, on_write, NULL);
206
+ ZITI_NODEJS_LOG(DEBUG, "back from ziti_write");
199
207
 
200
208
  return NULL;
201
209
  }
@@ -30,7 +30,9 @@ const Ziti_http_request = async (url, method, headers) => {
30
30
  // on_resp_data callback
31
31
  (obj) => {
32
32
  console.log('----------- Now inside Ziti_http_request on_resp_data callback ----------, obj is: \n%o', obj);
33
- console.log('----------- obj.body is: \n%o', obj.body.toString());
33
+ if (obj.body) {
34
+ console.log('----------- obj.body is: \n%o', obj.body.toString());
35
+ }
34
36
  },
35
37
  );
36
38
 
@@ -167,21 +169,21 @@ const sendChunk = (req) => {
167
169
  process.exit(-1);
168
170
  });
169
171
 
170
- console.log('inside JS main(), req is (%o)', req);
172
+ // console.log('inside JS main(), req is (%o)', req);
171
173
 
172
174
 
173
175
  // setTimeout( async () => {
174
176
 
175
- for (let i=0; i<3; i++ ) {
177
+ // for (let i=0; i<3; i++ ) {
176
178
 
177
- let req2 = await Ziti_http_request(url, method, []).catch((err) => {
178
- console.log('Ziti_http_request failed with error (%o)', err);
179
- process.exit(-1);
180
- });
179
+ // let req2 = await Ziti_http_request(url, method, []).catch((err) => {
180
+ // console.log('Ziti_http_request failed with error (%o)', err);
181
+ // process.exit(-1);
182
+ // });
181
183
 
182
- console.log('inside JS main() setTimeout(), req is (%o)', req2);
184
+ // console.log('inside JS main() setTimeout(), req is (%o)', req2);
183
185
 
184
- }
186
+ // }
185
187
 
186
188
  // }, 100);
187
189