Skip to content

Commit

Permalink
Merge branch 'master' into matttyson-master
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Jun 13, 2024
2 parents d11468a + 99cf43b commit 955abbc
Show file tree
Hide file tree
Showing 27 changed files with 227 additions and 226 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
include:
- os: windows-latest
arch: x64
node: 16
node: 18
- os: windows-latest
arch: arm64
node: 20
Expand All @@ -21,10 +21,10 @@ jobs:
node: 16
- os: macos-latest
arch: x64
node: 16
node: 18
- os: macos-latest
arch: arm64
node: 16
node: 18
- os: ubuntu-latest
arch: x64
libc: glibc
Expand All @@ -34,26 +34,26 @@ jobs:
arch: x64
libc: musl
image: ghcr.io/prebuild/alpine:2.1.1
node: 16
node: 18
- os: ubuntu-latest
arch: arm64
libc: glibc
image: ghcr.io/prebuild/linux-arm64-lts:2.1.1
node: 16
node: 18
- os: ubuntu-latest
arch: arm64
libc: musl
image: ghcr.io/prebuild/linux-arm64-musl:2.1.1
node: 16
node: 18
- os: ubuntu-latest
arch: arm
libc: glibc
image: ghcr.io/prebuild/linux-armv7:2.1.1
node: 16
node: 18
- os: ubuntu-latest
arch: arm64
image: ghcr.io/prebuild/android-arm64:2.1.1
node: 16
node: 18
runs-on: ${{matrix.os}}
container:
image: ${{matrix.image}}
Expand Down Expand Up @@ -93,7 +93,7 @@ jobs:
run: |
yarn --frozen-lockfile --ignore-scripts
npm install node-gyp -g
yarn prebuild --arch x64 -t 16.0.0
yarn prebuild --arch x64 -t 18.0.0
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -130,7 +130,7 @@ jobs:
- uses: actions/setup-node@v3
with:
cache: yarn
node-version: 16
node-version: 18
- run: yarn --frozen-lockfile --ignore-scripts
- name: Download artifacts
uses: actions/download-artifact@v3
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parcel/watcher",
"version": "2.4.1",
"version": "2.4.2-alpha.0",
"main": "index.js",
"types": "index.d.ts",
"repository": {
Expand Down Expand Up @@ -58,7 +58,7 @@
"lint-staged": "^11.1.2",
"mocha": "^9.1.1",
"napi-wasm": "^1.1.0",
"prebuildify": "^5.0.1",
"prebuildify": "^6.0.1",
"prettier": "^2.3.2"
},
"binary": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function buildNode(triple, t) {
fs.mkdirSync(dir + '/npm/' + t);
} catch (err) { }
fs.writeFileSync(`${dir}/npm/${t}/package.json`, JSON.stringify(pkg2, false, 2) + '\n');
fs.copyFileSync(`${dir}/prebuilds/${triple.platform}-${triple.arch}/node.napi.${triple.libc || 'glibc'}.node`, `${dir}/npm/${t}/watcher.node`);
fs.copyFileSync(`${dir}/prebuilds/${triple.platform}-${triple.arch}/@parcel+watcher.${triple.libc || 'glibc'}.node`, `${dir}/npm/${t}/watcher.node`);
fs.writeFileSync(`${dir}/npm/${t}/README.md`, `This is the ${t} build of @parcel/watcher. See https://github.com/parcel-bundler/watcher for details.`);
fs.copyFileSync(`${dir}/LICENSE`, `${dir}/npm/${t}/LICENSE`);
}
12 changes: 6 additions & 6 deletions src/Backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,23 @@ Backend::~Backend() {
#endif
}

void Backend::watch(Watcher &watcher) {
void Backend::watch(WatcherRef watcher) {
std::unique_lock<std::mutex> lock(mMutex);
auto res = mSubscriptions.find(&watcher);
auto res = mSubscriptions.find(watcher);
if (res == mSubscriptions.end()) {
try {
this->subscribe(watcher);
mSubscriptions.insert(&watcher);
mSubscriptions.insert(watcher);
} catch (std::exception &err) {
unref();
throw;
}
}
}

void Backend::unwatch(Watcher &watcher) {
void Backend::unwatch(WatcherRef watcher) {
std::unique_lock<std::mutex> lock(mMutex);
size_t deleted = mSubscriptions.erase(&watcher);
size_t deleted = mSubscriptions.erase(watcher);
if (deleted > 0) {
this->unsubscribe(watcher);
unref();
Expand All @@ -168,7 +168,7 @@ void Backend::unref() {
}

void Backend::handleWatcherError(WatcherError &err) {
unwatch(*err.mWatcher);
unwatch(err.mWatcher);
err.mWatcher->notifyError(err);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Backend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public:
void notifyStarted();

virtual void start();
virtual void writeSnapshot(Watcher &watcher, std::string *snapshotPath) = 0;
virtual void getEventsSince(Watcher &watcher, std::string *snapshotPath) = 0;
virtual void subscribe(Watcher &watcher) = 0;
virtual void unsubscribe(Watcher &watcher) = 0;
virtual void writeSnapshot(WatcherRef watcher, std::string *snapshotPath) = 0;
virtual void getEventsSince(WatcherRef watcher, std::string *snapshotPath) = 0;
virtual void subscribe(WatcherRef watcher) = 0;
virtual void unsubscribe(WatcherRef watcher) = 0;

static std::shared_ptr<Backend> getShared(std::string backend);

void watch(Watcher &watcher);
void unwatch(Watcher &watcher);
void watch(WatcherRef watcher);
void unwatch(WatcherRef watcher);
void unref();
void handleWatcherError(WatcherError &err);

std::mutex mMutex;
std::thread mThread;
private:
std::unordered_set<Watcher *> mSubscriptions;
std::unordered_set<WatcherRef> mSubscriptions;
Signal mStartedSignal;

void handleError(std::exception &err);
Expand Down
10 changes: 5 additions & 5 deletions src/Watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
using namespace Napi;

struct WatcherHash {
std::size_t operator() (std::shared_ptr<Watcher> const &k) const {
std::size_t operator() (WatcherRef const &k) const {
return std::hash<std::string>()(k->mDir);
}
};

struct WatcherCompare {
size_t operator() (std::shared_ptr<Watcher> const &a, std::shared_ptr<Watcher> const &b) const {
size_t operator() (WatcherRef const &a, WatcherRef const &b) const {
return *a == *b;
}
};

static std::unordered_set<std::shared_ptr<Watcher>, WatcherHash, WatcherCompare> sharedWatchers;
static std::unordered_set<WatcherRef , WatcherHash, WatcherCompare> sharedWatchers;

std::shared_ptr<Watcher> Watcher::getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs) {
std::shared_ptr<Watcher> watcher = std::make_shared<Watcher>(dir, ignorePaths, ignoreGlobs);
WatcherRef Watcher::getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs) {
WatcherRef watcher = std::make_shared<Watcher>(dir, ignorePaths, ignoreGlobs);
auto found = sharedWatchers.find(watcher);
if (found != sharedWatchers.end()) {
return *found;
Expand Down
18 changes: 13 additions & 5 deletions src/Watcher.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@

using namespace Napi;

struct Watcher;
using WatcherRef = std::shared_ptr<Watcher>;

struct Callback {
Napi::ThreadSafeFunction tsfn;
Napi::FunctionReference ref;
std::thread::id threadId;
};

class WatcherState {
public:
virtual ~WatcherState() = default;
};

struct Watcher {
std::string mDir;
std::unordered_set<std::string> mIgnorePaths;
std::unordered_set<Glob> mIgnoreGlobs;
EventList mEvents;
void *state;
std::shared_ptr<WatcherState> state;

Watcher(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
~Watcher();
Expand All @@ -42,7 +50,7 @@ struct Watcher {
bool isIgnored(std::string path);
void destroy();

static std::shared_ptr<Watcher> getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
static WatcherRef getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);

private:
std::mutex mMutex;
Expand All @@ -57,9 +65,9 @@ private:

class WatcherError : public std::runtime_error {
public:
Watcher *mWatcher;
WatcherError(std::string msg, Watcher *watcher) : std::runtime_error(msg), mWatcher(watcher) {}
WatcherError(const char *msg, Watcher *watcher) : std::runtime_error(msg), mWatcher(watcher) {}
WatcherRef mWatcher;
WatcherError(std::string msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
WatcherError(const char *msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
};

#endif
16 changes: 8 additions & 8 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ class WriteSnapshotRunner : public PromiseRunner {
}
private:
std::shared_ptr<Backend> backend;
std::shared_ptr<Watcher> watcher;
WatcherRef watcher;
std::string snapshotPath;

void execute() override {
backend->writeSnapshot(*watcher, &snapshotPath);
backend->writeSnapshot(watcher, &snapshotPath);
}
};

Expand All @@ -107,11 +107,11 @@ class GetEventsSinceRunner : public PromiseRunner {
}
private:
std::shared_ptr<Backend> backend;
std::shared_ptr<Watcher> watcher;
WatcherRef watcher;
std::string snapshotPath;

void execute() override {
backend->getEventsSince(*watcher, &snapshotPath);
backend->getEventsSince(watcher, &snapshotPath);
}

Value getResult() override {
Expand Down Expand Up @@ -169,13 +169,13 @@ class SubscribeRunner : public PromiseRunner {
}

private:
std::shared_ptr<Watcher> watcher;
WatcherRef watcher;
std::shared_ptr<Backend> backend;
FunctionReference callback;

void execute() override {
try {
backend->watch(*watcher);
backend->watch(watcher);
} catch (std::exception &err) {
watcher->destroy();
throw;
Expand All @@ -197,13 +197,13 @@ class UnsubscribeRunner : public PromiseRunner {
}

private:
std::shared_ptr<Watcher> watcher;
WatcherRef watcher;
std::shared_ptr<Backend> backend;
bool shouldUnwatch;

void execute() override {
if (shouldUnwatch) {
backend->unwatch(*watcher);
backend->unwatch(watcher);
}
}
};
Expand Down
20 changes: 10 additions & 10 deletions src/kqueue/KqueueBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void KqueueBackend::start() {
}

// Track all of the watchers that are touched so we can notify them at the end of the events.
std::unordered_set<Watcher *> watchers;
std::unordered_set<WatcherRef> watchers;

for (int i = 0; i < event_count; i++) {
int flags = events[i].fflags;
Expand Down Expand Up @@ -118,20 +118,20 @@ KqueueBackend::~KqueueBackend() {
mEndedSignal.wait();
}

void KqueueBackend::subscribe(Watcher &watcher) {
void KqueueBackend::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++) {
bool success = watchDir(watcher, it->second.path, tree);
if (!success) {
throw WatcherError(std::string("error watching " + watcher.mDir + ": " + strerror(errno)), &watcher);
throw WatcherError(std::string("error watching " + watcher->mDir + ": " + strerror(errno)), watcher);
}
}
}

bool KqueueBackend::watchDir(Watcher &watcher, std::string path, std::shared_ptr<DirTree> tree) {
if (watcher.isIgnored(path)) {
bool KqueueBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
if (watcher->isIgnored(path)) {
return false;
}

Expand All @@ -141,7 +141,7 @@ bool KqueueBackend::watchDir(Watcher &watcher, std::string path, std::shared_ptr
}

KqueueSubscription sub = {
.watcher = &watcher,
.watcher = watcher,
.path = path,
.tree = tree
};
Expand Down Expand Up @@ -189,7 +189,7 @@ std::vector<KqueueSubscription *> KqueueBackend::findSubscriptions(std::string &
return subs;
}

bool KqueueBackend::compareDir(int fd, std::string &path, std::unordered_set<Watcher *> &watchers) {
bool KqueueBackend::compareDir(int fd, std::string &path, std::unordered_set<WatcherRef> &watchers) {
// macOS doesn't support fdclosedir, so we have to duplicate the file descriptor
// to ensure the closedir doesn't also stop watching.
#if __APPLE__
Expand Down Expand Up @@ -240,7 +240,7 @@ bool KqueueBackend::compareDir(int fd, std::string &path, std::unordered_set<Wat
sub->watcher->mEvents.create(fullpath);
watchers.emplace(sub->watcher);

bool success = watchDir(*sub->watcher, fullpath, sub->tree);
bool success = watchDir(sub->watcher, fullpath, sub->tree);
if (!success) {
sub->tree->remove(fullpath);
return false;
Expand Down Expand Up @@ -289,10 +289,10 @@ bool KqueueBackend::compareDir(int fd, std::string &path, std::unordered_set<Wat
return true;
}

void KqueueBackend::unsubscribe(Watcher &watcher) {
void KqueueBackend::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 == &watcher) {
if (it->second.watcher.get() == watcher.get()) {
if (mSubscriptions.count(it->first) == 1) {
// Closing the file descriptor automatically unwatches it in the kqueue.
close(it->second.fd);
Expand Down
Loading

0 comments on commit 955abbc

Please sign in to comment.