@@ -16,18 +16,6 @@ limitations under the License.
1616
1717#include "ziti-nodejs.h"
1818
19- ziti_context nf ;
20- uv_loop_t * enroll_loop = NULL ;
21-
22- uv_loop_t * thread_loop ;
23- uv_thread_t thread ;
24- uv_async_t async ;
25-
26-
27- typedef struct {
28- napi_async_work work ;
29- napi_threadsafe_function tsfn ;
30- } AddonData ;
3119
3220// An item that will be generated here and passed into the JavaScript enroll callback
3321typedef struct EnrollItem {
@@ -45,6 +33,8 @@ typedef struct EnrollItem {
4533 */
4634static void CallJs_on_enroll (napi_env env , napi_value js_cb , void * context , void * data ) {
4735
36+ napi_status status ;
37+
4838 ZITI_NODEJS_LOG (DEBUG , "entered" );
4939
5040 // This parameter is not used.
@@ -70,38 +60,44 @@ static void CallJs_on_enroll(napi_env env, napi_value js_cb, void* context, void
7060
7161 // const obj = {}
7262 napi_value js_enroll_item , js_json_salvo , js_status , js_err ;
73- assert (napi_create_object (env , & js_enroll_item ) == napi_ok );
63+ status = napi_create_object (env , & js_enroll_item );
64+ if (status != napi_ok ) {
65+ napi_throw_error (env , NULL , "Unable to napi_create_object" );
66+ }
7467
7568 // obj.identity = identity
7669 if (NULL != item -> json_salvo ) {
77- assert (napi_create_string_utf8 (env , (const char * )item -> json_salvo , NAPI_AUTO_LENGTH , & js_json_salvo ) == napi_ok );
78- assert (napi_set_named_property (env , js_enroll_item , "identity" , js_json_salvo ) == napi_ok );
79- } else {
80- assert (napi_set_named_property (env , js_enroll_item , "identity" , undefined ) == napi_ok );
70+ napi_create_string_utf8 (env , (const char * )item -> json_salvo , NAPI_AUTO_LENGTH , & js_json_salvo );
71+ napi_set_named_property (env , js_enroll_item , "identity" , js_json_salvo );
8172 }
8273
8374 // obj.status = status
84- assert ( napi_create_int64 (env , (int64_t )item -> status , & js_status ) == napi_ok );
85- assert ( napi_set_named_property (env , js_enroll_item , "status" , js_status ) == napi_ok );
75+ napi_create_int64 (env , (int64_t )item -> status , & js_status );
76+ napi_set_named_property (env , js_enroll_item , "status" , js_status );
8677
8778 // obj.err = err
8879 if (NULL != item -> err ) {
89- assert (napi_create_string_utf8 (env , (const char * )item -> err , NAPI_AUTO_LENGTH , & js_err ) == napi_ok );
90- assert (napi_set_named_property (env , js_enroll_item , "err" , js_err ) == napi_ok );
91- } else {
92- assert (napi_set_named_property (env , js_enroll_item , "err" , undefined ) == napi_ok );
80+ napi_create_string_utf8 (env , (const char * )item -> err , NAPI_AUTO_LENGTH , & js_err );
81+ napi_set_named_property (env , js_enroll_item , "err" , js_err );
9382 }
9483
9584
96- // Call the JavaScript function and pass it the WriteItem
97- assert (
98- napi_call_function (
99- env ,
100- undefined ,
101- js_cb ,
102- 1 ,
103- & js_enroll_item ,
104- NULL ) == napi_ok );
85+ // Call the JavaScript function and pass it the EnrollItem
86+ napi_value global ;
87+ status = napi_get_global (env , & global );
88+
89+ status = napi_call_function (
90+ env ,
91+ global ,
92+ js_cb ,
93+ 1 ,
94+ & js_enroll_item ,
95+ NULL
96+ );
97+
98+ if (status != napi_ok ) {
99+ napi_throw_error (env , "EINVAL" , "failure to invoke JS callback" );
100+ }
105101
106102 }
107103}
@@ -110,11 +106,12 @@ static void CallJs_on_enroll(napi_env env, napi_value js_cb, void* context, void
110106/**
111107 *
112108 */
113- void on_ziti_enroll (const ziti_config * cfg , int status , const char * err , void * ctx ) {
109+ void on_ziti_enroll (ziti_config * cfg , int status , char * err , void * ctx ) {
110+ napi_status nstatus ;
114111
115112 ZITI_NODEJS_LOG (DEBUG , "\nstatus: %d, \nerr: %s,\nctx: %p" , status , err , ctx );
116113
117- AddonData * addon_data = (AddonData * )ctx ;
114+ EnrollAddonData * addon_data = (EnrollAddonData * )ctx ;
118115
119116 EnrollItem * item = memset (malloc (sizeof (* item )), 0 , sizeof (* item ));
120117
@@ -137,30 +134,28 @@ void on_ziti_enroll(const ziti_config *cfg, int status, const char *err, void *c
137134 // Initiate the call into the JavaScript callback.
138135 // The call into JavaScript will not have happened
139136 // when this function returns, but it will be queued.
140- assert (
141- napi_call_threadsafe_function (
142- addon_data -> tsfn ,
137+ nstatus = napi_call_threadsafe_function (
138+ addon_data -> tsfn_on_enroll ,
143139 item ,
144- napi_tsfn_blocking ) == napi_ok );
145- }
146-
140+ napi_tsfn_blocking );
141+ if (nstatus != napi_ok ) {
142+ ZITI_NODEJS_LOG (ERROR , "Unable to napi_call_threadsafe_function" );
143+ }
147144
145+ }
148146
149- void child_thread (void * data ){
150- uv_loop_t * thread_loop = (uv_loop_t * ) data ;
151147
152- //Start this loop
153- uv_run (thread_loop , UV_RUN_DEFAULT );
154- }
155148
156- static void consumer_notify (uv_async_t * handle , int status ) { }
157149
158150/**
159151 *
160152 */
161153napi_value _ziti_enroll (napi_env env , const napi_callback_info info ) {
162154 napi_status status ;
163155 napi_value jsRetval ;
156+ napi_valuetype js_cb_type ;
157+
158+ ziti_log_init (thread_loop , ZITI_LOG_DEFAULT_LEVEL , NULL );
164159
165160 ZITI_NODEJS_LOG (DEBUG , "entered" );
166161
@@ -172,6 +167,7 @@ napi_value _ziti_enroll(napi_env env, const napi_callback_info info) {
172167 }
173168
174169 if (argc < 2 ) {
170+ ZITI_NODEJS_LOG (DEBUG , "Too few arguments" );
175171 napi_throw_error (env , "EINVAL" , "Too few arguments" );
176172 return NULL ;
177173 }
@@ -182,9 +178,16 @@ napi_value _ziti_enroll(napi_env env, const napi_callback_info info) {
182178 status = napi_get_value_string_utf8 (env , args [0 ], JWTFileName , 256 , & result );
183179
184180 // Obtain ptr to JS callback function
185- napi_value js_cb = args [1 ];
181+ // napi_value js_cb = args[1];
182+ napi_typeof (env , args [1 ], & js_cb_type );
183+ if (js_cb_type != napi_function ) {
184+ ZITI_NODEJS_LOG (DEBUG , "args[1] is NOT a napi_function" );
185+ } else {
186+ ZITI_NODEJS_LOG (DEBUG , "args[1] IS a napi_function" );
187+ }
186188 napi_value work_name ;
187- AddonData * addon_data = malloc (sizeof (AddonData ));
189+
190+ EnrollAddonData * addon_data = memset (malloc (sizeof (* addon_data )), 0 , sizeof (* addon_data ));
188191
189192 // Create a string to describe this asynchronous operation.
190193 assert (napi_create_string_utf8 (
@@ -195,10 +198,9 @@ napi_value _ziti_enroll(napi_env env, const napi_callback_info info) {
195198
196199 // Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
197200 // which we can call from a worker thread.
198- assert (
199- napi_create_threadsafe_function (
201+ status = napi_create_threadsafe_function (
200202 env ,
201- js_cb ,
203+ args [ 1 ] ,
202204 NULL ,
203205 work_name ,
204206 0 ,
@@ -207,12 +209,11 @@ napi_value _ziti_enroll(napi_env env, const napi_callback_info info) {
207209 NULL ,
208210 NULL ,
209211 CallJs_on_enroll ,
210- & (addon_data -> tsfn )) == napi_ok );
212+ & (addon_data -> tsfn_on_enroll ));
213+ if (status != napi_ok ) {
214+ napi_throw_error (env , NULL , "Unable to napi_create_threadsafe_function" );
215+ }
211216
212- // Create and set up the consumer thread
213- thread_loop = uv_loop_new ();
214- uv_async_init (thread_loop , & async , (uv_async_cb )consumer_notify );
215- uv_thread_create (& thread , (uv_thread_cb )child_thread , thread_loop );
216217
217218 // Initiate the enrollment
218219 ziti_enroll_opts opts = {0 };
0 commit comments