|
| 1 | +#define NAPI_VERSION 8 |
| 2 | +#include "wtr/watcher-cabi.h" |
| 3 | +#include <node_api.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#define WITH_TSFN_NONBLOCKING 1 |
| 9 | + |
| 10 | +/* Owns: |
| 11 | + - A "handle" to a watch object, |
| 12 | + which we can give back to the watcher |
| 13 | + when we want to close it. |
| 14 | + - A (thread-safe) function, storing the |
| 15 | + user's callback, which we can call when |
| 16 | + an event is ready. |
| 17 | + - A reference to the user's callback, which |
| 18 | + we release when the watcher is closed. */ |
| 19 | +struct WatcherWrapper { |
| 20 | + napi_ref js_callback_ref = NULL; |
| 21 | + napi_threadsafe_function tsfn = NULL; |
| 22 | + void* watcher = NULL; |
| 23 | +}; |
| 24 | + |
| 25 | +/* Called by the callback bridge when an event is ready. |
| 26 | + Forwards event to js-land for the user. |
| 27 | + Expects an "owned" event, which will be freed in this scope. */ |
| 28 | +static void callback_js_receiver(napi_env env, napi_value js_callback, void* _unused, void* ctx) |
| 29 | +{ |
| 30 | + wtr_watcher_event* event = (wtr_watcher_event*)ctx; |
| 31 | + napi_value event_obj; |
| 32 | + napi_create_object(env, &event_obj); |
| 33 | + napi_value path_name, effect_type, path_type, effect_time, associated_path_name; |
| 34 | + napi_create_string_utf8(env, event->path_name, NAPI_AUTO_LENGTH, &path_name); |
| 35 | + napi_create_int32(env, event->effect_type, &effect_type); |
| 36 | + napi_create_int32(env, event->path_type, &path_type); |
| 37 | + napi_create_bigint_int64(env, event->effect_time, &effect_time); |
| 38 | + napi_set_named_property(env, event_obj, "pathName", path_name); |
| 39 | + napi_set_named_property(env, event_obj, "effectType", effect_type); |
| 40 | + napi_set_named_property(env, event_obj, "pathType", path_type); |
| 41 | + napi_set_named_property(env, event_obj, "effectTime", effect_time); |
| 42 | + if (event->associated_path_name) { |
| 43 | + napi_create_string_utf8(env, event->associated_path_name, NAPI_AUTO_LENGTH, &associated_path_name); |
| 44 | + napi_set_named_property(env, event_obj, "associatedPathName", associated_path_name); |
| 45 | + } |
| 46 | + napi_value global; |
| 47 | + napi_get_global(env, &global); |
| 48 | + napi_value result; |
| 49 | + napi_call_function(env, global, js_callback, 1, &event_obj, &result); |
| 50 | +#if WITH_TSFN_NONBLOCKING |
| 51 | + free((void*)event->path_name); |
| 52 | + free((void*)event->associated_path_name); |
| 53 | + free(event); |
| 54 | +#endif |
| 55 | +} |
| 56 | + |
| 57 | +/* Called by the watcher when an event is ready. |
| 58 | + Passes a newly allocated event to the wrapper's stored function. |
| 59 | + Expects the event it to be freed in the wrapper's stored function. */ |
| 60 | +static void callback_bridge(struct wtr_watcher_event event_view, void* ctx) |
| 61 | +{ |
| 62 | + WatcherWrapper* wrapper = (WatcherWrapper*)ctx; |
| 63 | +#if WITH_TSFN_NONBLOCKING |
| 64 | + wtr_watcher_event* event_owned = (wtr_watcher_event*)malloc(sizeof(wtr_watcher_event)); |
| 65 | + event_owned->path_name = event_view.path_name ? strdup(event_view.path_name) : NULL; |
| 66 | + event_owned->effect_type = event_view.effect_type; |
| 67 | + event_owned->path_type = event_view.path_type; |
| 68 | + event_owned->effect_time = event_view.effect_time; |
| 69 | + event_owned->associated_path_name = event_view.associated_path_name ? strdup(event_view.associated_path_name) : NULL; |
| 70 | + if (wrapper->tsfn) { |
| 71 | + napi_call_threadsafe_function(wrapper->tsfn, event_owned, napi_tsfn_nonblocking); |
| 72 | +#else |
| 73 | + napi_call_threadsafe_function(wrapper->tsfn, &event_view, napi_tsfn_blocking); |
| 74 | +#endif |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/* Should be called by the user when they are done with the watcher. |
| 79 | + Ends event processing and releases any owned resources. */ |
| 80 | +static napi_value close(napi_env env, napi_callback_info func_arg_info) |
| 81 | +{ |
| 82 | + void* ctx; |
| 83 | + napi_get_cb_info(env, func_arg_info, NULL, NULL, NULL, &ctx); |
| 84 | + WatcherWrapper* wrapper = (WatcherWrapper*)ctx; |
| 85 | + if (wrapper->tsfn) { |
| 86 | + napi_release_threadsafe_function(wrapper->tsfn, napi_tsfn_release); |
| 87 | + wrapper->tsfn = NULL; |
| 88 | + } |
| 89 | + if (wrapper->js_callback_ref) { |
| 90 | + napi_delete_reference(env, wrapper->js_callback_ref); |
| 91 | + wrapper->js_callback_ref = NULL; |
| 92 | + } |
| 93 | + bool closed_ok = wtr_watcher_close(wrapper->watcher); |
| 94 | + free(wrapper); |
| 95 | + wrapper->watcher = NULL; |
| 96 | + napi_value result; |
| 97 | + napi_get_boolean(env, closed_ok, &result); |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +/* Opens a watcher on a path (and any children). |
| 102 | + Calls the provided callback when events happen. |
| 103 | + Accepts two arguments, a path and a callback. |
| 104 | + Returns an object with a single method: close. |
| 105 | + Call `.close()` when you don't want to watch |
| 106 | + things anymore. */ |
| 107 | +static napi_value watch(napi_env env, napi_callback_info func_arg_info) |
| 108 | +{ |
| 109 | + size_t argc = 2; |
| 110 | + napi_value args[2]; |
| 111 | + napi_get_cb_info(env, func_arg_info, &argc, args, NULL, NULL); |
| 112 | + if (argc != 2) { |
| 113 | + napi_throw_error(env, NULL, "Wrong number of arguments"); |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + char path[4096]; |
| 117 | + size_t path_len; |
| 118 | + napi_get_value_string_utf8(env, args[0], path, sizeof(path), &path_len); |
| 119 | + WatcherWrapper* wrapper = (WatcherWrapper*)malloc(sizeof(WatcherWrapper)); |
| 120 | + napi_value js_callback = args[1]; |
| 121 | + napi_create_reference(env, js_callback, 1, &wrapper->js_callback_ref); |
| 122 | + napi_value work_name; |
| 123 | + napi_create_string_utf8(env, "Watcher", NAPI_AUTO_LENGTH, &work_name); |
| 124 | + size_t work_queue_size = 1024; |
| 125 | + napi_create_threadsafe_function( |
| 126 | + env, |
| 127 | + js_callback, |
| 128 | + NULL, |
| 129 | + work_name, |
| 130 | + work_queue_size, |
| 131 | + 1, |
| 132 | + wrapper, |
| 133 | + NULL, |
| 134 | + NULL, |
| 135 | + callback_js_receiver, |
| 136 | + &wrapper->tsfn); |
| 137 | + wrapper->watcher = wtr_watcher_open(path, callback_bridge, wrapper); |
| 138 | + if (wrapper->watcher == NULL) { |
| 139 | + napi_throw_error(env, NULL, "Failed to open watcher"); |
| 140 | + return NULL; |
| 141 | + } |
| 142 | + napi_value watcher_obj; |
| 143 | + napi_create_object(env, &watcher_obj); |
| 144 | + napi_value close_func; |
| 145 | + napi_create_function(env, "close", NAPI_AUTO_LENGTH, close, wrapper, &close_func); |
| 146 | + napi_set_named_property(env, watcher_obj, "close", close_func); |
| 147 | + napi_wrap(env, watcher_obj, wrapper, NULL, NULL, NULL); |
| 148 | + return watcher_obj; |
| 149 | +} |
| 150 | + |
| 151 | +/* Module initialization. (Does this work as a non-CommonJS module?) */ |
| 152 | +static napi_value mod_init(napi_env env, napi_value exports) |
| 153 | +{ |
| 154 | + napi_value watch_func; |
| 155 | + napi_create_function(env, NULL, 0, watch, NULL, &watch_func); |
| 156 | + napi_set_named_property(env, exports, "watch", watch_func); |
| 157 | + return exports; |
| 158 | +} |
| 159 | + |
| 160 | +NAPI_MODULE(NODE_GYP_MODULE_NAME, mod_init) |
0 commit comments