Debian Codegraph @

node/@parcel/watcher/src/binding.cc

raw
1 #include <unordered_set>
2 #include <node_api.h>
3 #include "wasm/include.h"
4 #include <napi.h>
5 #include "Glob.hh"
6 #include "Event.hh"
7 #include "Backend.hh"
8 #include "Watcher.hh"
9 #include "PromiseRunner.hh"
10
11 using namespace Napi;
12
13 std::unordered_set<std::string> getIgnorePaths(Env env, Value opts) {
14 std::unordered_set<std::string> result;
15
16 if (opts.IsObject()) {
17 Value v = opts.As<Object>().Get(String::New(env, "ignorePaths"));
18 if (v.IsArray()) {
19 Array items = v.As<Array>();
20 for (size_t i = 0; i < items.Length(); i++) {
21 Value item = items.Get(Number::New(env, static_cast<double>(i)));
22 if (item.IsString()) {
23 result.insert(std::string(item.As<String>().Utf8Value().c_str()));
24 }
25 }
26 }
27 }
28
29 return result;
30 }
31
32 std::unordered_set<Glob> getIgnoreGlobs(Env env, Value opts) {
33 std::unordered_set<Glob> result;
34
35 if (opts.IsObject()) {
36 Value v = opts.As<Object>().Get(String::New(env, "ignoreGlobs"));
37 if (v.IsArray()) {
38 Array items = v.As<Array>();
39 for (size_t i = 0; i < items.Length(); i++) {
40 Value item = items.Get(Number::New(env, static_cast<double>(i)));
41 if (item.IsString()) {
42 auto key = item.As<String>().Utf8Value();
43 try {
44 result.emplace(key);
45 } catch (const std::regex_error& e) {
46 Error::New(env, e.what()).ThrowAsJavaScriptException();
47 }
48 }
49 }
50 }
51 }
52
53 return result;
54 }
55
56 std::shared_ptr<Backend> getBackend(Env env, Value opts) {
57 Value b = opts.As<Object>().Get(String::New(env, "backend"));
58 std::string backendName;
59 if (b.IsString()) {
60 backendName = std::string(b.As<String>().Utf8Value().c_str());
61 }
62
63 return Backend::getShared(backendName);
64 }
65
66 class WriteSnapshotRunner : public PromiseRunner {
67 public:
68 WriteSnapshotRunner(Env env, Value dir, Value snap, Value opts)
69 : PromiseRunner(env),
70 snapshotPath(std::string(snap.As<String>().Utf8Value().c_str())) {
71 watcher = Watcher::getShared(
72 std::string(dir.As<String>().Utf8Value().c_str()),
73 getIgnorePaths(env, opts),
74 getIgnoreGlobs(env, opts)
75 );
76
77 backend = getBackend(env, opts);
78 }
79
80 ~WriteSnapshotRunner() {
81 watcher->unref();
82 backend->unref();
83 }
84 private:
85 std::shared_ptr<Backend> backend;
86 WatcherRef watcher;
87 std::string snapshotPath;
88
89 void execute() override {
90 backend->writeSnapshot(watcher, &snapshotPath);
91 }
92 };
93
94 class GetEventsSinceRunner : public PromiseRunner {
95 public:
96 GetEventsSinceRunner(Env env, Value dir, Value snap, Value opts)
97 : PromiseRunner(env),
98 snapshotPath(std::string(snap.As<String>().Utf8Value().c_str())) {
99 watcher = std::make_shared<Watcher>(
100 std::string(dir.As<String>().Utf8Value().c_str()),
101 getIgnorePaths(env, opts),
102 getIgnoreGlobs(env, opts)
103 );
104
105 backend = getBackend(env, opts);
106 }
107
108 ~GetEventsSinceRunner() {
109 watcher->unref();
110 backend->unref();
111 }
112 private:
113 std::shared_ptr<Backend> backend;
114 WatcherRef watcher;
115 std::string snapshotPath;
116
117 void execute() override {
118 backend->getEventsSince(watcher, &snapshotPath);
119 if (watcher->mEvents.hasError()) {
120 throw std::runtime_error(watcher->mEvents.getError());
121 }
122 }
123
124 Value getResult() override {
125 std::vector<Event> events = watcher->mEvents.getEvents();
126 Array eventsArray = Array::New(env, events.size());
127 uint32_t i = 0;
128 for (auto it = events.begin(); it != events.end(); it++) {
129 eventsArray.Set(i++, it->toJS(env));
130 }
131 return eventsArray;
132 }
133 };
134
135 template<class Runner>
136 Value queueSnapshotWork(const CallbackInfo& info) {
137 Env env = info.Env();
138 if (info.Length() < 1 || !info[0].IsString()) {
139 TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
140 return env.Null();
141 }
142
143 if (info.Length() < 2 || !info[1].IsString()) {
144 TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
145 return env.Null();
146 }
147
148 if (info.Length() >= 3 && !info[2].IsObject()) {
149 TypeError::New(env, "Expected an object").ThrowAsJavaScriptException();
150 return env.Null();
151 }
152
153 Runner *runner = new Runner(info.Env(), info[0], info[1], info[2]);
154 return runner->queue();
155 }
156
157 Value writeSnapshot(const CallbackInfo& info) {
158 return queueSnapshotWork<WriteSnapshotRunner>(info);
159 }
160
161 Value getEventsSince(const CallbackInfo& info) {
162 return queueSnapshotWork<GetEventsSinceRunner>(info);
163 }
164
165 class SubscribeRunner : public PromiseRunner {
166 public:
167 SubscribeRunner(Env env, Value dir, Value fn, Value opts) : PromiseRunner(env) {
168 watcher = Watcher::getShared(
169 std::string(dir.As<String>().Utf8Value().c_str()),
170 getIgnorePaths(env, opts),
171 getIgnoreGlobs(env, opts)
172 );
173
174 backend = getBackend(env, opts);
175 watcher->watch(fn.As<Function>());
176 }
177
178 private:
179 WatcherRef watcher;
180 std::shared_ptr<Backend> backend;
181 FunctionReference callback;
182
183 void execute() override {
184 try {
185 backend->watch(watcher);
186 } catch (std::exception&) {
187 watcher->destroy();
188 throw;
189 }
190 }
191 };
192
193 class UnsubscribeRunner : public PromiseRunner {
194 public:
195 UnsubscribeRunner(Env env, Value dir, Value fn, Value opts) : PromiseRunner(env) {
196 watcher = Watcher::getShared(
197 std::string(dir.As<String>().Utf8Value().c_str()),
198 getIgnorePaths(env, opts),
199 getIgnoreGlobs(env, opts)
200 );
201
202 backend = getBackend(env, opts);
203 shouldUnwatch = watcher->unwatch(fn.As<Function>());
204 }
205
206 private:
207 WatcherRef watcher;
208 std::shared_ptr<Backend> backend;
209 bool shouldUnwatch;
210
211 void execute() override {
212 if (shouldUnwatch) {
213 backend->unwatch(watcher);
214 }
215 }
216 };
217
218 template<class Runner>
219 Value queueSubscriptionWork(const CallbackInfo& info) {
220 Env env = info.Env();
221 if (info.Length() < 1 || !info[0].IsString()) {
222 TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
223 return env.Null();
224 }
225
226 if (info.Length() < 2 || !info[1].IsFunction()) {
227 TypeError::New(env, "Expected a function").ThrowAsJavaScriptException();
228 return env.Null();
229 }
230
231 if (info.Length() >= 3 && !info[2].IsObject()) {
232 TypeError::New(env, "Expected an object").ThrowAsJavaScriptException();
233 return env.Null();
234 }
235
236 Runner *runner = new Runner(info.Env(), info[0], info[1], info[2]);
237 return runner->queue();
238 }
239
240 Value subscribe(const CallbackInfo& info) {
241 return queueSubscriptionWork<SubscribeRunner>(info);
242 }
243
244 Value unsubscribe(const CallbackInfo& info) {
245 return queueSubscriptionWork<UnsubscribeRunner>(info);
246 }
247
248 Object Init(Env env, Object exports) {
249 exports.Set(
250 String::New(env, "writeSnapshot"),
251 Function::New(env, writeSnapshot)
252 );
253 exports.Set(
254 String::New(env, "getEventsSince"),
255 Function::New(env, getEventsSince)
256 );
257 exports.Set(
258 String::New(env, "subscribe"),
259 Function::New(env, subscribe)
260 );
261 exports.Set(
262 String::New(env, "unsubscribe"),
263 Function::New(env, unsubscribe)
264 );
265 return exports;
266 }
267
268 NODE_API_MODULE(watcher, Init)