@openziti/ziti-sdk-nodejs 0.6.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.
@@ -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
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -18,14 +18,13 @@ 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
  }
25
24
 
26
25
  if (typeof require.context == 'function') {
27
26
 
28
- importAll( require.context("../build/", true, /\.node$/) );
27
+ importAll( require.context("../build/", true, /\.node$/) );
29
28
 
30
29
  } else {
31
30
 
@@ -37,4 +36,15 @@ if (typeof require.context == 'function') {
37
36
 
38
37
  }
39
38
 
40
- var ziti = module.exports = exports = binding;
39
+ ziti = module.exports = exports = binding;
40
+
41
+
42
+
43
+ /**
44
+ * Attach the external, app-facing, API to the 'ziti' object
45
+ */
46
+ exports.close = require('./close').close;
47
+ exports.express = require('./express').express;
48
+ exports.init = require('./init').init;
49
+ exports.listen = require('./listen').listen;
50
+ exports.write = require('./write').write;
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.6.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",
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ static void CallJs_on_req_body(napi_env env, napi_value js_cb, void* context, vo
73
73
  ZITI_NODEJS_LOG(DEBUG, "status: %zd", item->status);
74
74
 
75
75
  // obj.body = body
76
- rc = napi_create_int32(env, (int32_t)item->body, &js_body);
76
+ rc = napi_create_int32(env, (int64_t)item->body, &js_body);
77
77
  if (rc != napi_ok) {
78
78
  napi_throw_error(env, "EINVAL", "failure to create resp.body");
79
79
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/src/utils.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 NetFoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/src/utils.h CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 NetFoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/src/ziti-add-on.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -71,6 +71,7 @@ napi_value Init(napi_env env, napi_value exports) {
71
71
  expose_ziti_enroll(env, exports);
72
72
  expose_ziti_hello(env, exports);
73
73
  expose_ziti_init(env, exports);
74
+ expose_ziti_listen(env, exports);
74
75
  expose_ziti_service_available(env, exports);
75
76
  expose_ziti_shutdown(env, exports);
76
77
  expose_ziti_write(env, exports);
package/src/ziti-nodejs.h CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -93,6 +93,20 @@ typedef struct {
93
93
  napi_threadsafe_function tsfn_on_service_available;
94
94
  } ConnAddonData;
95
95
 
96
+ /**
97
+ *
98
+ */
99
+ typedef struct {
100
+ char *service_name;
101
+ int64_t js_arb_data;
102
+ ziti_connection server;
103
+ napi_async_work work;
104
+ napi_threadsafe_function tsfn_on_listen;
105
+ napi_threadsafe_function tsfn_on_listen_client;
106
+ napi_threadsafe_function tsfn_on_listen_client_connect;
107
+ napi_threadsafe_function tsfn_on_listen_client_data;
108
+ } ListenAddonData;
109
+
96
110
  /**
97
111
  *
98
112
  */
@@ -189,6 +203,7 @@ extern void expose_ziti_dial(napi_env env, napi_value exports);
189
203
  extern void expose_ziti_enroll(napi_env env, napi_value exports);
190
204
  extern void expose_ziti_hello(napi_env env, napi_value exports);
191
205
  extern void expose_ziti_init(napi_env env, napi_value exports);
206
+ extern void expose_ziti_listen(napi_env env, napi_value exports);
192
207
  extern void expose_ziti_service_available(napi_env env, napi_value exports);
193
208
  extern void expose_ziti_shutdown(napi_env env, napi_value exports);
194
209
  extern void expose_ziti_write(napi_env env, napi_value exports);
package/src/ziti_close.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/src/ziti_dial.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@ limitations under the License.
20
20
  // An item that will be generated here and passed into the JavaScript on_data callback
21
21
  typedef struct OnDataItem {
22
22
 
23
- uint8_t *buf;
23
+ const unsigned char *buf;
24
24
  int len;
25
25
 
26
26
  } OnDataItem;
@@ -139,11 +139,10 @@ static void CallJs_on_data(napi_env env, napi_value js_cb, void* context, void*
139
139
 
140
140
 
141
141
 
142
-
143
142
  /**
144
143
  * This function is the callback invoked by the C-SDK when data arrives on the connection.
145
144
  */
146
- ssize_t on_data(ziti_connection conn, uint8_t *buf, ssize_t len) {
145
+ long on_data(struct ziti_conn *conn, const unsigned char *buf, long len) {
147
146
  napi_status status;
148
147
 
149
148
  ConnAddonData* addon_data = (ConnAddonData*) ziti_conn_data(conn);
@@ -168,7 +167,7 @@ ssize_t on_data(ziti_connection conn, uint8_t *buf, ssize_t len) {
168
167
  OnDataItem* item = memset(malloc(sizeof(*item)), 0, sizeof(*item));
169
168
  item->buf = buf;
170
169
  item->buf = calloc(1, len);
171
- memcpy(item->buf, buf, len);
170
+ memcpy((void*)item->buf, buf, len);
172
171
  item->len = len;
173
172
 
174
173
  // if (addon_data->isWebsocket) {
package/src/ziti_enroll.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ static void CallJs_on_enroll(napi_env env, napi_value js_cb, void* context, void
110
110
  /**
111
111
  *
112
112
  */
113
- void on_ziti_enroll(ziti_config *cfg, int status, char *err, void *ctx) {
113
+ void on_ziti_enroll(const ziti_config *cfg, int status, const char *err, void *ctx) {
114
114
 
115
115
  ZITI_NODEJS_LOG(DEBUG, "\nstatus: %d, \nerr: %s,\nctx: %p", status, err, ctx);
116
116
 
package/src/ziti_hello.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/src/ziti_init.c CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2019-2020 Netfoundry, Inc.
2
+ Copyright Netfoundry, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ static void CallJs(napi_env env, napi_value js_cb, void* context, void* data) {
56
56
  status = napi_get_undefined(env, &undefined);
57
57
 
58
58
  // Retrieve the rc created by the worker thread.
59
- int rc = (int)data;
59
+ int64_t rc = (int64_t)data;
60
60
  status = napi_create_int64(env, (int64_t)rc, &js_rc);
61
61
  if (status != napi_ok) {
62
62
  napi_throw_error(env, NULL, "Failed to napi_create_int64");
@@ -269,7 +269,6 @@ napi_value _ziti_init(napi_env env, const napi_callback_info info) {
269
269
 
270
270
  // Create and set up the consumer thread
271
271
  if (NULL == thread_loop) { // Spawn the loop only once
272
- ZITI_NODEJS_LOG(DEBUG, "calling uv_loop_new()");
273
272
  thread_loop = uv_loop_new();
274
273
  uv_async_init(thread_loop, &async, (uv_async_cb)consumer_notify);
275
274
  uv_thread_create(&thread, (uv_thread_cb)child_thread, thread_loop);
@@ -289,6 +288,8 @@ napi_value _ziti_init(napi_env env, const napi_callback_info info) {
289
288
 
290
289
  int rc = ziti_init_opts(opts, thread_loop);
291
290
 
291
+ ZITI_NODEJS_LOG(DEBUG, "ziti_init_opts rc: %d", rc);
292
+
292
293
  status = napi_create_int32(env, rc, &jsRetval);
293
294
  if (status != napi_ok) {
294
295
  napi_throw_error(env, NULL, "Unable to create return value");