@openziti/ziti-sdk-nodejs 0.6.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/.github/workflows/build.yml +220 -0
- package/.github/workflows/codeql-analysis.yml +71 -0
- package/.github/workflows/mattermost-ziti-webhook.yml +26 -0
- package/.gitmodules +4 -0
- package/.travis.yml-obsolete +99 -0
- package/CODE_OF_CONDUCT.md +17 -0
- package/CONTRIBUTING.md +6 -0
- package/LICENSE +201 -0
- package/README.md +155 -0
- package/appveyor.yml-obsolete +32 -0
- package/binding.gyp +227 -0
- package/lib/index.js +17 -0
- package/lib/ziti.js +40 -0
- package/package.json +56 -0
- package/scripts/build-appveyor.bat +198 -0
- package/scripts/install_node.sh +99 -0
- package/scripts/validate_tag.sh +24 -0
- package/src/Ziti_https_request.c +960 -0
- package/src/Ziti_https_request_data.c +250 -0
- package/src/Ziti_https_request_end.c +79 -0
- package/src/stack_traces.c +334 -0
- package/src/utils.c +108 -0
- package/src/utils.h +85 -0
- package/src/ziti-add-on.c +88 -0
- package/src/ziti-nodejs.h +209 -0
- package/src/ziti_close.c +79 -0
- package/src/ziti_dial.c +375 -0
- package/src/ziti_enroll.c +245 -0
- package/src/ziti_hello.c +52 -0
- package/src/ziti_init.c +315 -0
- package/src/ziti_service_available.c +222 -0
- package/src/ziti_shutdown.c +47 -0
- package/src/ziti_websocket_connect.c +458 -0
- package/src/ziti_websocket_write.c +235 -0
- package/src/ziti_write.c +223 -0
- package/tests/enroll-test.js +38 -0
- package/tests/hello.js +12 -0
- package/tests/https-test.js +206 -0
- package/tests/mattermost-test.js +124 -0
- package/tests/websocket-test.js +119 -0
- package/ziti.js +1 -0
- package/ziti.png +0 -0
package/src/ziti_write.c
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019-2020 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
|
+
#include "ziti-nodejs.h"
|
|
18
|
+
#include <string.h>
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// An item that will be generated here and passed into the JavaScript write callback
|
|
22
|
+
typedef struct WriteItem {
|
|
23
|
+
ziti_connection conn;
|
|
24
|
+
ssize_t status;
|
|
25
|
+
} WriteItem;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* This function is responsible for calling the JavaScript 'write' callback function
|
|
30
|
+
* that was specified when the ziti_write(...) was called from JavaScript.
|
|
31
|
+
*/
|
|
32
|
+
static void CallJs_on_write(napi_env env, napi_value js_cb, void* context, void* data) {
|
|
33
|
+
napi_status status;
|
|
34
|
+
|
|
35
|
+
// This parameter is not used.
|
|
36
|
+
(void) context;
|
|
37
|
+
|
|
38
|
+
// Retrieve the WriteItem created by the worker thread.
|
|
39
|
+
WriteItem* item = (WriteItem*)data;
|
|
40
|
+
|
|
41
|
+
// env and js_cb may both be NULL if Node.js is in its cleanup phase, and
|
|
42
|
+
// items are left over from earlier thread-safe calls from the worker thread.
|
|
43
|
+
// When env is NULL, we simply skip over the call into Javascript and free the
|
|
44
|
+
// items.
|
|
45
|
+
if (env != NULL) {
|
|
46
|
+
|
|
47
|
+
napi_value undefined;
|
|
48
|
+
|
|
49
|
+
// const obj = {}
|
|
50
|
+
napi_value js_write_item, js_conn, js_status;
|
|
51
|
+
status = napi_create_object(env, &js_write_item);
|
|
52
|
+
if (status != napi_ok) {
|
|
53
|
+
napi_throw_error(env, NULL, "Unable to napi_create_object");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// obj.conn = conn
|
|
57
|
+
ZITI_NODEJS_LOG(DEBUG, "conn=%p", item->conn);
|
|
58
|
+
status = napi_create_int64(env, (int64_t)item->conn, &js_conn);
|
|
59
|
+
if (status != napi_ok) {
|
|
60
|
+
napi_throw_error(env, NULL, "Unable to napi_create_int64");
|
|
61
|
+
}
|
|
62
|
+
status = napi_set_named_property(env, js_write_item, "conn", js_conn);
|
|
63
|
+
if (status != napi_ok) {
|
|
64
|
+
napi_throw_error(env, NULL, "Unable to napi_set_named_property");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// obj.status = status
|
|
68
|
+
ZITI_NODEJS_LOG(DEBUG, "status=%zo", item->status);
|
|
69
|
+
status = napi_create_int64(env, (int64_t)item->status, &js_status);
|
|
70
|
+
if (status != napi_ok) {
|
|
71
|
+
napi_throw_error(env, NULL, "Unable to napi_create_int64");
|
|
72
|
+
}
|
|
73
|
+
status = napi_set_named_property(env, js_write_item, "status", js_status);
|
|
74
|
+
if (status != napi_ok) {
|
|
75
|
+
napi_throw_error(env, NULL, "Unable to napi_set_named_property");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Retrieve the JavaScript `undefined` value so we can use it as the `this`
|
|
79
|
+
// value of the JavaScript function call.
|
|
80
|
+
status = napi_get_undefined(env, &undefined);
|
|
81
|
+
if (status != napi_ok) {
|
|
82
|
+
napi_throw_error(env, NULL, "Unable to napi_get_undefined (5)");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Call the JavaScript function and pass it the WriteItem
|
|
86
|
+
status = napi_call_function(
|
|
87
|
+
env,
|
|
88
|
+
undefined,
|
|
89
|
+
js_cb,
|
|
90
|
+
1,
|
|
91
|
+
&js_write_item,
|
|
92
|
+
NULL);
|
|
93
|
+
if (status != napi_ok) {
|
|
94
|
+
napi_throw_error(env, NULL, "Unable to napi_call_function");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
free(item);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
*/
|
|
105
|
+
static void on_write(ziti_connection conn, ssize_t status, void *ctx) {
|
|
106
|
+
|
|
107
|
+
ConnAddonData* addon_data = (ConnAddonData*) ziti_conn_data(conn);
|
|
108
|
+
|
|
109
|
+
WriteItem* item = memset(malloc(sizeof(*item)), 0, sizeof(*item));
|
|
110
|
+
item->conn = conn;
|
|
111
|
+
item->status = status;
|
|
112
|
+
|
|
113
|
+
// Initiate the call into the JavaScript callback.
|
|
114
|
+
// The call into JavaScript will not have happened
|
|
115
|
+
// when this function returns, but it will be queued.
|
|
116
|
+
napi_status nstatus = napi_call_threadsafe_function(
|
|
117
|
+
addon_data->tsfn_on_write,
|
|
118
|
+
item,
|
|
119
|
+
napi_tsfn_blocking);
|
|
120
|
+
if (nstatus != napi_ok) {
|
|
121
|
+
ZITI_NODEJS_LOG(ERROR, "Unable to napi_call_threadsafe_function");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
*
|
|
128
|
+
*/
|
|
129
|
+
napi_value _ziti_write(napi_env env, const napi_callback_info info) {
|
|
130
|
+
napi_status status;
|
|
131
|
+
size_t argc = 3;
|
|
132
|
+
napi_value args[3];
|
|
133
|
+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
|
|
134
|
+
if (status != napi_ok) {
|
|
135
|
+
napi_throw_error(env, NULL, "Failed to parse arguments");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (argc < 3) {
|
|
139
|
+
napi_throw_error(env, "EINVAL", "Too few arguments");
|
|
140
|
+
return NULL;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Obtain ziti_connection
|
|
144
|
+
int64_t js_conn;
|
|
145
|
+
status = napi_get_value_int64(env, args[0], &js_conn);
|
|
146
|
+
if (status != napi_ok) {
|
|
147
|
+
napi_throw_error(env, NULL, "Failed to get Conn");
|
|
148
|
+
}
|
|
149
|
+
ziti_connection conn = (ziti_connection)js_conn;
|
|
150
|
+
|
|
151
|
+
ConnAddonData* addon_data = (ConnAddonData*) ziti_conn_data(conn);
|
|
152
|
+
|
|
153
|
+
// Obtain data to write (we expect a Buffer)
|
|
154
|
+
void* buffer;
|
|
155
|
+
size_t bufferLength;
|
|
156
|
+
status = napi_get_buffer_info(env, args[1], &buffer, &bufferLength);
|
|
157
|
+
if (status != napi_ok) {
|
|
158
|
+
napi_throw_error(env, NULL, "Failed to get Buffer info");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Since the underlying Buffer's lifetime is not guaranteed if it's managed by the VM, we will copy the chunk into our heap
|
|
162
|
+
void* chunk = memset(malloc(bufferLength), 0, bufferLength);
|
|
163
|
+
memcpy(chunk, buffer, bufferLength);
|
|
164
|
+
|
|
165
|
+
// Obtain ptr to JS 'write' callback function
|
|
166
|
+
napi_value js_write_cb = args[2];
|
|
167
|
+
napi_value work_name;
|
|
168
|
+
|
|
169
|
+
// Create a string to describe this asynchronous operation.
|
|
170
|
+
status = napi_create_string_utf8(
|
|
171
|
+
env,
|
|
172
|
+
"N-API on_write",
|
|
173
|
+
NAPI_AUTO_LENGTH,
|
|
174
|
+
&work_name);
|
|
175
|
+
if (status != napi_ok) {
|
|
176
|
+
napi_throw_error(env, NULL, "Failed to napi_create_string_utf8");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
|
|
180
|
+
// which we can call from a worker thread.
|
|
181
|
+
status = napi_create_threadsafe_function(
|
|
182
|
+
env,
|
|
183
|
+
js_write_cb,
|
|
184
|
+
NULL,
|
|
185
|
+
work_name,
|
|
186
|
+
0,
|
|
187
|
+
1,
|
|
188
|
+
NULL,
|
|
189
|
+
NULL,
|
|
190
|
+
NULL,
|
|
191
|
+
CallJs_on_write,
|
|
192
|
+
&(addon_data->tsfn_on_write));
|
|
193
|
+
if (status != napi_ok) {
|
|
194
|
+
napi_throw_error(env, NULL, "Failed to napi_create_threadsafe_function");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Now, call the C-SDK to actually write the data over to the service
|
|
198
|
+
ziti_write(conn, chunk, bufferLength, on_write, NULL);
|
|
199
|
+
|
|
200
|
+
return NULL;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
*
|
|
207
|
+
*/
|
|
208
|
+
void expose_ziti_write(napi_env env, napi_value exports) {
|
|
209
|
+
napi_status status;
|
|
210
|
+
napi_value fn;
|
|
211
|
+
|
|
212
|
+
status = napi_create_function(env, NULL, 0, _ziti_write, NULL, &fn);
|
|
213
|
+
if (status != napi_ok) {
|
|
214
|
+
napi_throw_error(env, NULL, "Unable to wrap native function '_ziti_write");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
status = napi_set_named_property(env, exports, "ziti_write", fn);
|
|
218
|
+
if (status != napi_ok) {
|
|
219
|
+
napi_throw_error(env, NULL, "Unable to populate exports for 'ziti_write");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
}
|
|
223
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
var binary = require('@mapbox/node-pre-gyp');
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')), {debug: true});
|
|
5
|
+
// var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
|
|
6
|
+
var ziti = require(binding_path);
|
|
7
|
+
require('assert').equal(ziti.ziti_hello(),"ziti");
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const ziti_enroll = async (jwt_path) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
let rc = ziti.ziti_enroll(jwt_path, (data) => {
|
|
14
|
+
if (data.identity) {
|
|
15
|
+
resolve(data);
|
|
16
|
+
} else {
|
|
17
|
+
reject(data);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
(async () => {
|
|
25
|
+
|
|
26
|
+
let jwt_path = process.argv[2];
|
|
27
|
+
|
|
28
|
+
let data = await ziti_enroll(jwt_path).catch((data) => {
|
|
29
|
+
console.log('NF_enroll failed with error code (%o)', data.len);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (data && data.identity) {
|
|
33
|
+
console.log("data.identity is:\n\n%s", data.identity);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
process.exit(0);
|
|
37
|
+
|
|
38
|
+
})();
|
package/tests/hello.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
const binary = require('@mapbox/node-pre-gyp');
|
|
3
|
+
const path = require('path')
|
|
4
|
+
// const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')), {debug: true});
|
|
5
|
+
const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
|
|
6
|
+
console.log("binding_path is: ", binding_path);
|
|
7
|
+
const ziti = require(binding_path);
|
|
8
|
+
console.log("ziti native addon is: \n", ziti);
|
|
9
|
+
const result = ziti.ziti_hello();
|
|
10
|
+
console.log("ziti_hello() result is: ", result);
|
|
11
|
+
require('assert').equal(result,"ziti");
|
|
12
|
+
console.log("SUCCESS");
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
|
|
2
|
+
var binary = require('@mapbox/node-pre-gyp');
|
|
3
|
+
var path = require('path')
|
|
4
|
+
// var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')), {debug: true});
|
|
5
|
+
var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
|
|
6
|
+
var ziti = require(binding_path);
|
|
7
|
+
require('assert').equal(ziti.ziti_hello(),"ziti");
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const Ziti_http_request = async (url, method, headers) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
try {
|
|
14
|
+
|
|
15
|
+
console.log('headers (%o)', headers);
|
|
16
|
+
|
|
17
|
+
let req = ziti.Ziti_http_request(
|
|
18
|
+
url,
|
|
19
|
+
method,
|
|
20
|
+
headers,
|
|
21
|
+
// on_req callback
|
|
22
|
+
(obj) => {
|
|
23
|
+
console.log('----------- Now inside Ziti_http_request on_req callback ----------, obj is: \n%o', obj);
|
|
24
|
+
},
|
|
25
|
+
// on_resp callback
|
|
26
|
+
(obj) => {
|
|
27
|
+
console.log('----------- Now inside Ziti_http_request on_resp callback ----------, obj is: \n%o', obj);
|
|
28
|
+
// resolve(obj);
|
|
29
|
+
},
|
|
30
|
+
// on_resp_data callback
|
|
31
|
+
(obj) => {
|
|
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());
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
console.log('inside JS Ziti_http_request(), req is (%o)', req);
|
|
38
|
+
resolve(req);
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const Ziti_http_request_data = async (req, buffer) => {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
ziti.Ziti_http_request_data(
|
|
50
|
+
req,
|
|
51
|
+
buffer,
|
|
52
|
+
// on_req_data callback
|
|
53
|
+
(obj) => {
|
|
54
|
+
console.log('----------- Now inside Ziti_http_request_data on_req_data callback ----------, obj is: \n%o', obj);
|
|
55
|
+
resolve(obj);
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const NF_init = async () => {
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
ziti.ziti_init(process.argv[2], () => {
|
|
64
|
+
resolve();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
let ctr = 0;
|
|
70
|
+
const spin = () => {
|
|
71
|
+
setTimeout( () => {
|
|
72
|
+
ctr++;
|
|
73
|
+
console.log("1-sec wait, ctr=%d", ctr);
|
|
74
|
+
if (ctr < 60) {
|
|
75
|
+
spin();
|
|
76
|
+
} else {
|
|
77
|
+
process.exit(0);
|
|
78
|
+
}
|
|
79
|
+
}, 1000);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// let chunkctr = 0;
|
|
83
|
+
let chunkBody = process.argv[5];
|
|
84
|
+
|
|
85
|
+
// const sendChunk = (req) => {
|
|
86
|
+
// setTimeout( (req) => {
|
|
87
|
+
// chunkctr++;
|
|
88
|
+
// console.log("chunkctr=%d", chunkctr);
|
|
89
|
+
// if (chunkctr < 10) {
|
|
90
|
+
|
|
91
|
+
// buffer = Buffer.from(chunkBody + "-" + chunkctr);
|
|
92
|
+
|
|
93
|
+
// console.log("sending chunk %d", chunkctr);
|
|
94
|
+
|
|
95
|
+
// Ziti_http_request_data(req, buffer);
|
|
96
|
+
|
|
97
|
+
// sendChunk(req);
|
|
98
|
+
|
|
99
|
+
// } else {
|
|
100
|
+
// console.log("======== calling Ziti_http_request_end");
|
|
101
|
+
// ziti.Ziti_http_request_end( req );
|
|
102
|
+
// }
|
|
103
|
+
// }, 500, req);
|
|
104
|
+
// }
|
|
105
|
+
|
|
106
|
+
const sendChunk = (req) => {
|
|
107
|
+
setTimeout( (req) => {
|
|
108
|
+
buffer = Buffer.from(chunkBody);
|
|
109
|
+
console.log("sending chunk");
|
|
110
|
+
Ziti_http_request_data(req, buffer);
|
|
111
|
+
}, 500, req);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
(async () => {
|
|
117
|
+
|
|
118
|
+
let url = process.argv[3];
|
|
119
|
+
let method = process.argv[4];
|
|
120
|
+
let body = process.argv[5];
|
|
121
|
+
let buffer;
|
|
122
|
+
let results;
|
|
123
|
+
let headersArray = [
|
|
124
|
+
"Content-Length:1",
|
|
125
|
+
"Transfer-Encoding:chunked",
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
await NF_init();
|
|
130
|
+
|
|
131
|
+
if (typeof body !== 'undefined') {
|
|
132
|
+
|
|
133
|
+
buffer = Buffer.from("1");
|
|
134
|
+
|
|
135
|
+
console.log("Sending 'body' of: %o", buffer);
|
|
136
|
+
|
|
137
|
+
let req = await Ziti_http_request(url, method, headersArray).catch((err) => {
|
|
138
|
+
console.log('Ziti_http_request failed with error (%o)', err);
|
|
139
|
+
process.exit(-1);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
console.log("Ziti_http_request results is:\n\n%o", req);
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < 10; i++) {
|
|
146
|
+
console.log("queueing chunk %d", i);
|
|
147
|
+
|
|
148
|
+
results = await Ziti_http_request_data(req, buffer).catch((err) => {
|
|
149
|
+
console.log('Ziti_http_request_data failed with error (%o)', err);
|
|
150
|
+
process.exit(-1);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
console.log('========================================================================================================');
|
|
156
|
+
console.log('======================================== Ziti_http_request_end called ==================================');
|
|
157
|
+
ziti.Ziti_http_request_end( req );
|
|
158
|
+
console.log('========================================================================================================');
|
|
159
|
+
|
|
160
|
+
} else {
|
|
161
|
+
|
|
162
|
+
console.log("No 'body' will be sent");
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
let req = await Ziti_http_request(url, method, []).catch((err) => {
|
|
166
|
+
console.log('Ziti_http_request failed with error (%o)', err);
|
|
167
|
+
process.exit(-1);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
console.log('inside JS main(), req is (%o)', req);
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
// setTimeout( async () => {
|
|
174
|
+
|
|
175
|
+
for (let i=0; i<3; i++ ) {
|
|
176
|
+
|
|
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
|
+
});
|
|
181
|
+
|
|
182
|
+
console.log('inside JS main() setTimeout(), req is (%o)', req2);
|
|
183
|
+
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// }, 100);
|
|
187
|
+
|
|
188
|
+
// let req2 = await Ziti_http_request(url, method, []).catch((err) => {
|
|
189
|
+
// console.log('Ziti_http_request failed with error (%o)', err);
|
|
190
|
+
// process.exit(-1);
|
|
191
|
+
// });
|
|
192
|
+
|
|
193
|
+
// console.log("Ziti_http_request results is:\n\n%o", req2);
|
|
194
|
+
|
|
195
|
+
// console.log('========================================================================================================');
|
|
196
|
+
// console.log('======================================== Ziti_http_request_end called ==================================');
|
|
197
|
+
// ziti.Ziti_http_request_end( req );
|
|
198
|
+
// console.log('========================================================================================================');
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
spin();
|
|
205
|
+
|
|
206
|
+
})();
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
|
|
2
|
+
var binary = require('@mapbox/node-pre-gyp');
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
|
|
5
|
+
var ziti = require(binding_path);
|
|
6
|
+
require('assert').equal(ziti.ziti_hello(),"ziti");
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
function NF_dial(service) {
|
|
11
|
+
console.log('----------- inside NF_dial() ---------- service is: ', service);
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
ziti.ziti_dial(
|
|
14
|
+
service,
|
|
15
|
+
false, // NOT a wabsocket
|
|
16
|
+
(conn) => {
|
|
17
|
+
console.log('----------- Now inside NF_dial connect callback ----------, conn is: ' + conn);
|
|
18
|
+
resolve(conn);
|
|
19
|
+
},
|
|
20
|
+
(data) => {
|
|
21
|
+
console.log('----------- Now inside NF_dial data callback ----------, data is: [===\n%s\n===]', data);
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const rand = max => Math.floor(Math.random() * max);
|
|
28
|
+
const delay = (ms, value) => new Promise(resolve => setTimeout(resolve, ms, value));
|
|
29
|
+
|
|
30
|
+
const NF_init = async () => {
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
ziti.ziti_init(process.argv[2], () => {
|
|
33
|
+
resolve();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const NF_service_available = (service) => {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
ziti.ziti_service_available(service, (status) => {
|
|
41
|
+
resolve(status);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const NF_write = (conn, data) => {
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
ziti.ziti_write(conn, data, () => {
|
|
49
|
+
resolve();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
let ctr = 0;
|
|
55
|
+
const spin = () => {
|
|
56
|
+
setTimeout( () => {
|
|
57
|
+
ctr++;
|
|
58
|
+
console.log("1-sec wait, ctr=%d", ctr);
|
|
59
|
+
if (ctr < 5) {
|
|
60
|
+
spin();
|
|
61
|
+
} else {
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
}, 1000);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
function NF_write_callback(item) {
|
|
69
|
+
if (item.status < 0) {
|
|
70
|
+
// console.log("NF_write_callback(): request performed on conn '%o' failed to submit status '%o'", item.conn, item.status);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// console.log("NF_write_callback(): request performed on conn '%o' successful: '%o' bytes sent", item.conn, item.status);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function NF_dial_connect_callback(conn) {
|
|
78
|
+
|
|
79
|
+
console.log("NF_dial_connect_callback(): received connection '%o'; now initiating Write to service", conn);
|
|
80
|
+
|
|
81
|
+
let data =
|
|
82
|
+
"GET / " +
|
|
83
|
+
"HTTP/1.1\r\n" +
|
|
84
|
+
"Accept: */*\r\n" +
|
|
85
|
+
"Connection: keep-alive\r\n" +
|
|
86
|
+
"Host: mattermost.ziti.netfoundry.io\r\n" +
|
|
87
|
+
"User-Agent: curl/7.54.0\r\n" +
|
|
88
|
+
"\r\n";
|
|
89
|
+
|
|
90
|
+
ziti.ziti_write(
|
|
91
|
+
conn,
|
|
92
|
+
data,
|
|
93
|
+
NF_write_callback
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function NF_dial_data_callback(data) {
|
|
98
|
+
console.log("NF_dial_data_callback(): received the following data: \n\n", data);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
(async () => {
|
|
102
|
+
|
|
103
|
+
await NF_init();
|
|
104
|
+
|
|
105
|
+
let status = await NF_service_available('mattermost.ziti.netfoundry.io');
|
|
106
|
+
|
|
107
|
+
let conn = await NF_dial('mattermost.ziti.netfoundry.io');
|
|
108
|
+
|
|
109
|
+
let data =
|
|
110
|
+
"GET / " +
|
|
111
|
+
"HTTP/1.1\r\n" +
|
|
112
|
+
"Accept: */*\r\n" +
|
|
113
|
+
"Connection: keep-alive\r\n" +
|
|
114
|
+
"Host: mattermost.ziti.netfoundry.io\r\n" +
|
|
115
|
+
"User-Agent: curl/7.54.0\r\n" +
|
|
116
|
+
"\r\n";
|
|
117
|
+
|
|
118
|
+
let buffer = Buffer.from(data);
|
|
119
|
+
|
|
120
|
+
await NF_write(conn, buffer);
|
|
121
|
+
|
|
122
|
+
spin();
|
|
123
|
+
|
|
124
|
+
})();
|