Skip to content

Commit

Permalink
make on_req & on_resp callbacks truely optional for Ziti_http_request (
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect authored Nov 8, 2023
1 parent fbf68f4 commit 64b5afd
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 74 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,20 @@ For more context on this SDK, you may be interested in this
# Supported platforms

The `@openziti/ziti-sdk-nodejs` module works with the following Node.js versions:
- v12.x
- v13.x
- v14.x
- v15.x
- v16.x
- v17.x
- v18.x
- v19.x
- v20.x

The `@openziti/ziti-sdk-nodejs` module works with the following architectures:
- amd64
- arm64

The `@openziti/ziti-sdk-nodejs` module works with the following Operating Systems:
- macos
- linux
- windows

Binaries for most Node versions and platforms are provided by default via [@mapbox/node-pre-gyp](https://github.com/mapbox/node-pre-gyp).

# Installing

Expand Down Expand Up @@ -107,6 +112,7 @@ const on_resp_data = ( obj ) => {
// Perform an HTTP GET request to a dark OpenZiti web service
ziti.httpRequest(
'myDarkWebService', // OpenZiti Service name or HTTP origin part of the URL
undefined, // schemeHostPort parm is mutually-exclusive with serviceName parm
'GET',
'/', // path part of the URL including query params
['Accept: application/json' ], // headers
Expand Down Expand Up @@ -221,4 +227,18 @@ for tracking bugs and feature requests and have limited bandwidth to address the
- Participate in discussion on [Discourse](https://openziti.discourse.group/)
# Building from source on MacOS
``` js
git clone https://github.com/microsoft/vcpkg.git
./vcpkg/bootstrap-vcpkg.sh
export VCPKG_ROOT=`pwd`/vcpkg
brew install cmake
brew install ninja
brew install pkg-config
git clone https://github.com/openziti/ziti-sdk-nodejs.git
cd ziti-sdk-nodejs
npm run build
```
Copyright© NetFoundry, Inc.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@openziti/ziti-sdk-nodejs",
"description": "A NodeJS-based SDK for delivering secure applications over a Ziti Network",
"version": "0.14.0",
"version": "0.14.1",
"main": "./lib/ziti",
"scripts": {
"build": "npm run build:init && npm run build:configure && npm run build:make",
Expand Down
150 changes: 85 additions & 65 deletions src/Ziti_https_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,14 @@ void on_resp(tlsuv_http_resp_t *resp, void *data) {
}

// Initiate the call into the JavaScript callback. The call into JavaScript will not have happened when this function returns, but it will be queued.
int rc = napi_call_threadsafe_function(
addon_data->tsfn_on_resp,
item,
napi_tsfn_blocking);
if (rc != napi_ok) {
napi_throw_error(addon_data->env, "EINVAL", "failure to invoke JS callback");
if (addon_data->tsfn_on_resp != NULL) {
int rc = napi_call_threadsafe_function(
addon_data->tsfn_on_resp,
item,
napi_tsfn_blocking);
if (rc != napi_ok) {
napi_throw_error(addon_data->env, "EINVAL", "failure to invoke JS callback");
}
}

if (UV_EOF != resp->code) {
Expand Down Expand Up @@ -711,12 +713,14 @@ void on_client(uv_work_t* req, int status) {
}

// Initiate the call into the JavaScript callback. The call into JavaScript will not have happened when this function returns, but it will be queued.
int rc = napi_call_threadsafe_function(
addon_data->tsfn_on_req,
addon_data,
napi_tsfn_blocking);
if (rc != napi_ok) {
napi_throw_error(addon_data->env, "EINVAL", "failure to invoke JS callback");
if (addon_data->tsfn_on_req != NULL) {
int rc = napi_call_threadsafe_function(
addon_data->tsfn_on_req,
addon_data,
napi_tsfn_blocking);
if (rc != napi_ok) {
napi_throw_error(addon_data->env, "EINVAL", "failure to invoke JS callback");
}
}
}

Expand Down Expand Up @@ -908,73 +912,89 @@ napi_value _Ziti_http_request(napi_env env, const napi_callback_info info) {

ZITI_NODEJS_LOG(DEBUG, "path: %s", addon_data->path);

HttpsReq* httpsReq = calloc(1, sizeof(HttpsReq));
addon_data->httpsReq = httpsReq;
httpsReq->addon_data = addon_data;

// Obtain ptr to JS on_req callback function
napi_value js_cb = args[5];
napi_value work_name;

status = napi_typeof(env, args[5], &valuetype);
if (valuetype != napi_undefined) {
ZITI_NODEJS_LOG(DEBUG, "on_req callback is specified");

HttpsReq* httpsReq = calloc(1, sizeof(HttpsReq));
addon_data->httpsReq = httpsReq;
httpsReq->addon_data = addon_data;
// Create a string to describe this asynchronous operation.
rc = napi_create_string_utf8(env, "on_req", NAPI_AUTO_LENGTH, &work_name);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create string");
return NULL;
}

// Create a string to describe this asynchronous operation.
rc = napi_create_string_utf8(env, "on_req", NAPI_AUTO_LENGTH, &work_name);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create string");
return NULL;
// Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
// which we can call from a worker thread.
rc = napi_create_threadsafe_function(
env,
js_cb,
NULL,
work_name,
0,
1,
NULL,
NULL,
NULL,
CallJs_on_req,
&(addon_data->tsfn_on_req)
);
ZITI_NODEJS_LOG(DEBUG, "2: %d", rc);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create threadsafe_function");
return NULL;
}
ZITI_NODEJS_LOG(DEBUG, "napi_create_threadsafe_function addon_data->tsfn_on_req() : %p", addon_data->tsfn_on_req);
}

// Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
// which we can call from a worker thread.
rc = napi_create_threadsafe_function(
env,
js_cb,
NULL,
work_name,
0,
1,
NULL,
NULL,
NULL,
CallJs_on_req,
&(addon_data->tsfn_on_req)
);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create threadsafe_function");
return NULL;
else {
ZITI_NODEJS_LOG(DEBUG, "on_req callback is NOT specified");
}
ZITI_NODEJS_LOG(DEBUG, "napi_create_threadsafe_function addon_data->tsfn_on_req() : %p", addon_data->tsfn_on_req);

// Obtain ptr to JS on_resp callback function
napi_value js_cb2 = args[6];

// Create a string to describe this asynchronous operation.
rc = napi_create_string_utf8(env, "on_resp", NAPI_AUTO_LENGTH, &work_name);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create string");
return NULL;
}
status = napi_typeof(env, args[6], &valuetype);
if (valuetype != napi_undefined) {
ZITI_NODEJS_LOG(DEBUG, "on_resp callback is specified");

// Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
// which we can call from a worker thread.
rc = napi_create_threadsafe_function(
env,
js_cb2,
NULL,
work_name,
0,
1,
NULL,
NULL,
NULL,
CallJs_on_resp,
&(addon_data->tsfn_on_resp)
);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create threadsafe_function");
return NULL;
// Create a string to describe this asynchronous operation.
rc = napi_create_string_utf8(env, "on_resp", NAPI_AUTO_LENGTH, &work_name);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create string");
return NULL;
}

// Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
// which we can call from a worker thread.
rc = napi_create_threadsafe_function(
env,
js_cb2,
NULL,
work_name,
0,
1,
NULL,
NULL,
NULL,
CallJs_on_resp,
&(addon_data->tsfn_on_resp)
);
if (rc != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to create threadsafe_function");
return NULL;
}
ZITI_NODEJS_LOG(DEBUG, "napi_create_threadsafe_function addon_data->tsfn_on_resp() : %p", addon_data->tsfn_on_resp);
}
else {
ZITI_NODEJS_LOG(DEBUG, "on_resp callback is NOT specified");
}
ZITI_NODEJS_LOG(DEBUG, "napi_create_threadsafe_function addon_data->tsfn_on_resp() : %p", addon_data->tsfn_on_resp);


// Obtain ptr to JS on_resp_data callback function
Expand Down
2 changes: 2 additions & 0 deletions src/ziti_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ napi_value _ziti_init(napi_env env, const napi_callback_info info) {
napi_throw_error(env, NULL, "Failed to napi_create_threadsafe_function");
}

ziti_log_init(thread_loop, ZITI_LOG_DEFAULT_LEVEL, NULL);

ziti_config cfg = {0};

int rc = ziti_load_config(&cfg, config_file_name);
Expand Down

0 comments on commit 64b5afd

Please sign in to comment.