first commit
This commit is contained in:
commit
817b7cf4bc
838 changed files with 730686 additions and 0 deletions
132
node_modules/@parcel/watcher/src/wasm/WasmBackend.cc
generated
vendored
Normal file
132
node_modules/@parcel/watcher/src/wasm/WasmBackend.cc
generated
vendored
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
#include <sys/stat.h>
|
||||
#include "WasmBackend.hh"
|
||||
|
||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
||||
|
||||
void WasmBackend::start() {
|
||||
notifyStarted();
|
||||
}
|
||||
|
||||
void WasmBackend::subscribe(WatcherRef watcher) {
|
||||
// Build a full directory tree recursively, and watch each directory.
|
||||
std::shared_ptr<DirTree> tree = getTree(watcher);
|
||||
|
||||
for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
|
||||
if (it->second.isDir) {
|
||||
watchDir(watcher, it->second.path, tree);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WasmBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
|
||||
int wd = wasm_backend_add_watch(path.c_str(), (void *)this);
|
||||
std::shared_ptr<WasmSubscription> sub = std::make_shared<WasmSubscription>();
|
||||
sub->tree = tree;
|
||||
sub->path = path;
|
||||
sub->watcher = watcher;
|
||||
mSubscriptions.emplace(wd, sub);
|
||||
}
|
||||
|
||||
extern "C" void wasm_backend_event_handler(void *backend, int wd, int type, char *filename) {
|
||||
WasmBackend *b = (WasmBackend *)(backend);
|
||||
b->handleEvent(wd, type, filename);
|
||||
}
|
||||
|
||||
void WasmBackend::handleEvent(int wd, int type, char *filename) {
|
||||
// Find the subscriptions for this watch descriptor
|
||||
auto range = mSubscriptions.equal_range(wd);
|
||||
std::unordered_set<std::shared_ptr<WasmSubscription>> set;
|
||||
for (auto it = range.first; it != range.second; it++) {
|
||||
set.insert(it->second);
|
||||
}
|
||||
|
||||
for (auto it = set.begin(); it != set.end(); it++) {
|
||||
if (handleSubscription(type, filename, *it)) {
|
||||
(*it)->watcher->notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WasmBackend::handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub) {
|
||||
// Build full path and check if its in our ignore list.
|
||||
WatcherRef watcher = sub->watcher;
|
||||
std::string path = std::string(sub->path);
|
||||
|
||||
if (filename[0] != '\0') {
|
||||
path += "/" + std::string(filename);
|
||||
}
|
||||
|
||||
if (watcher->isIgnored(path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == 1) {
|
||||
struct stat st;
|
||||
stat(path.c_str(), &st);
|
||||
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
||||
watcher->mEvents.update(path);
|
||||
} else if (type == 2) {
|
||||
// Determine if this is a create or delete depending on if the file exists or not.
|
||||
struct stat st;
|
||||
if (lstat(path.c_str(), &st)) {
|
||||
// If the entry being deleted/moved is a directory, remove it from the list of subscriptions
|
||||
DirEntry *entry = sub->tree->find(path);
|
||||
if (!entry) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entry->isDir) {
|
||||
std::string pathStart = path + DIR_SEP;
|
||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
||||
if (it->second->path == path || it->second->path.rfind(pathStart, 0) == 0) {
|
||||
wasm_backend_remove_watch(it->first);
|
||||
it = mSubscriptions.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all sub-entries
|
||||
for (auto it = sub->tree->entries.begin(); it != sub->tree->entries.end();) {
|
||||
if (it->first.rfind(pathStart, 0) == 0) {
|
||||
watcher->mEvents.remove(it->first);
|
||||
it = sub->tree->entries.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
watcher->mEvents.remove(path);
|
||||
sub->tree->remove(path);
|
||||
} else if (sub->tree->find(path)) {
|
||||
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
||||
watcher->mEvents.update(path);
|
||||
} else {
|
||||
watcher->mEvents.create(path);
|
||||
|
||||
// If this is a create, check if it's a directory and start watching if it is.
|
||||
DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
|
||||
if (entry->isDir) {
|
||||
watchDir(watcher, path, sub->tree);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WasmBackend::unsubscribe(WatcherRef watcher) {
|
||||
// Find any subscriptions pointing to this watcher, and remove them.
|
||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
||||
if (it->second->watcher.get() == watcher.get()) {
|
||||
if (mSubscriptions.count(it->first) == 1) {
|
||||
wasm_backend_remove_watch(it->first);
|
||||
}
|
||||
|
||||
it = mSubscriptions.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
34
node_modules/@parcel/watcher/src/wasm/WasmBackend.hh
generated
vendored
Normal file
34
node_modules/@parcel/watcher/src/wasm/WasmBackend.hh
generated
vendored
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef WASM_H
|
||||
#define WASM_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include "../shared/BruteForceBackend.hh"
|
||||
#include "../DirTree.hh"
|
||||
|
||||
extern "C" {
|
||||
int wasm_backend_add_watch(const char *filename, void *backend);
|
||||
void wasm_backend_remove_watch(int wd);
|
||||
void wasm_backend_event_handler(void *backend, int wd, int type, char *filename);
|
||||
};
|
||||
|
||||
struct WasmSubscription {
|
||||
std::shared_ptr<DirTree> tree;
|
||||
std::string path;
|
||||
WatcherRef watcher;
|
||||
};
|
||||
|
||||
class WasmBackend : public BruteForceBackend {
|
||||
public:
|
||||
void start() override;
|
||||
void subscribe(WatcherRef watcher) override;
|
||||
void unsubscribe(WatcherRef watcher) override;
|
||||
void handleEvent(int wd, int type, char *filename);
|
||||
private:
|
||||
int mWasm;
|
||||
std::unordered_multimap<int, std::shared_ptr<WasmSubscription>> mSubscriptions;
|
||||
|
||||
void watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree);
|
||||
bool handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub);
|
||||
};
|
||||
|
||||
#endif
|
||||
74
node_modules/@parcel/watcher/src/wasm/include.h
generated
vendored
Normal file
74
node_modules/@parcel/watcher/src/wasm/include.h
generated
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
Copyright Node.js contributors. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// Node does not include the headers for these functions when compiling for WASM, so add them here.
|
||||
#ifdef __wasm32__
|
||||
extern "C" {
|
||||
NAPI_EXTERN napi_status NAPI_CDECL
|
||||
napi_create_threadsafe_function(napi_env env,
|
||||
napi_value func,
|
||||
napi_value async_resource,
|
||||
napi_value async_resource_name,
|
||||
size_t max_queue_size,
|
||||
size_t initial_thread_count,
|
||||
void* thread_finalize_data,
|
||||
napi_finalize thread_finalize_cb,
|
||||
void* context,
|
||||
napi_threadsafe_function_call_js call_js_cb,
|
||||
napi_threadsafe_function* result);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL napi_get_threadsafe_function_context(
|
||||
napi_threadsafe_function func, void** result);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL
|
||||
napi_call_threadsafe_function(napi_threadsafe_function func,
|
||||
void* data,
|
||||
napi_threadsafe_function_call_mode is_blocking);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL
|
||||
napi_acquire_threadsafe_function(napi_threadsafe_function func);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL napi_release_threadsafe_function(
|
||||
napi_threadsafe_function func, napi_threadsafe_function_release_mode mode);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL
|
||||
napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL
|
||||
napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
||||
|
||||
NAPI_EXTERN napi_status NAPI_CDECL
|
||||
napi_create_async_work(napi_env env,
|
||||
napi_value async_resource,
|
||||
napi_value async_resource_name,
|
||||
napi_async_execute_callback execute,
|
||||
napi_async_complete_callback complete,
|
||||
void* data,
|
||||
napi_async_work* result);
|
||||
NAPI_EXTERN napi_status NAPI_CDECL napi_delete_async_work(napi_env env,
|
||||
napi_async_work work);
|
||||
NAPI_EXTERN napi_status NAPI_CDECL napi_queue_async_work(napi_env env,
|
||||
napi_async_work work);
|
||||
NAPI_EXTERN napi_status NAPI_CDECL napi_cancel_async_work(napi_env env,
|
||||
napi_async_work work);
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue