Socket
Socket
Sign inDemoInstall

@parcel/watcher

Package Overview
Dependencies
61
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0-alpha.2 to 2.0.0-alpha.3

src/binding.cc

37

index.js

@@ -1,1 +0,36 @@

module.exports = require('bindings')('fschanges.node');
const binding = require('bindings')('watcher.node');
const path = require('path');
function normalizeOptions(dir, opts = {}) {
if (Array.isArray(opts.ignore)) {
opts = Object.assign({}, opts, {
ignore: opts.ignore.map(ignore => path.resolve(dir, ignore))
});
}
return opts;
}
exports.writeSnapshot = (dir, snapshot, opts) => {
return binding.writeSnapshot(path.resolve(dir), path.resolve(snapshot), normalizeOptions(dir, opts));
};
exports.getEventsSince = (dir, snapshot, opts) => {
return binding.getEventsSince(path.resolve(dir), path.resolve(snapshot), normalizeOptions(dir, opts));
};
exports.subscribe = async (dir, fn, opts) => {
dir = path.resolve(dir);
opts = normalizeOptions(dir, opts);
await binding.subscribe(dir, fn, opts);
return {
unsubscribe() {
return binding.unsubscribe(dir, fn, opts);
}
};
};
exports.unsubscribe = (dir, fn, opts) => {
return binding.unsubscribe(path.resolve(dir), fn, normalizeOptions(dir, opts));
};

10

package.json
{
"name": "@parcel/watcher",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.3",
"main": "index.js",
"repository" : {
"type" : "git",
"url": "https://github.com/devongovett/fschanges.git"
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/watcher.git"
},
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
"install": "prebuild-install -r napi || node-gyp rebuild",
"prebuild": "prebuild -t 3 -r napi --strip",

@@ -12,0 +12,0 @@ "test": "mocha"

@@ -21,12 +21,15 @@ # @parcel/watcher

// Subscribe to events
watcher.subscribe(process.cwd(), (events) => {
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
console.log(events);
});
// later on...
await subscription.unsubscribe();
// Get events since some saved snapshot in the past
let snapshotPath = path.join(process.cwd(), 'snapshot.txt');
let events = watcher.getEventsSince(process.cwd(), snapshotPath);
let events = await watcher.getEventsSince(process.cwd(), snapshotPath);
// Save a snapshot for later
watcher.writeSnapshot(process.cwd(), snapshotPath);
await watcher.writeSnapshot(process.cwd(), snapshotPath);
```

@@ -42,2 +45,8 @@

```javascript
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
console.log(events);
});
```
Events have two properties:

@@ -48,2 +57,8 @@

To unsubscribe from change notifications, call the `unsubscribe` method on the returned subscription object.
```javascript
await subscription.unsubscribe();
```
`@parcel/watcher` has the following watcher backends, listed in priority order:

@@ -65,3 +80,3 @@

```javascript
watcher.writeSnapshot(dirPath, snapshotPath);
await watcher.writeSnapshot(dirPath, snapshotPath);
```

@@ -72,3 +87,3 @@

```javascript
let events = watcher.getEventsSince(dirPath, snapshotPath);
let events = await watcher.getEventsSince(dirPath, snapshotPath);
```

@@ -95,3 +110,3 @@

* `ignore` - an array of paths to ignore. They can be either files or directories. No events will be emitted about these files or directories or their children. They must be absolute paths.
* `ignore` - an array of paths to ignore. They can be either files or directories. No events will be emitted about these files or directories or their children.
* `backend` - the name of an explicitly chosen backend to use. Allowed options are `"fs-events"`, `"watchman"`, `"inotify"`, `"windows"`, or `"brute-force"` (only for querying). If the specified backend is not available on the current platform, the default backend will be used instead.

@@ -98,0 +113,0 @@

@@ -1,2 +0,2 @@

const fschanges = require('../');
const watcher = require('../');
const assert = require('assert');

@@ -45,3 +45,3 @@ const fs = require('fs-extra');

let f = getFilename();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
if (isSecondPrecision) {

@@ -53,3 +53,3 @@ await sleep(1000);

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -64,7 +64,7 @@ {type: 'create', path: f}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.writeFile(f, 'hi');
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -80,7 +80,7 @@ {type: 'update', path: f}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.rename(f1, f2);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -96,7 +96,7 @@ {type: 'delete', path: f1},

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.unlink(f);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -111,7 +111,7 @@ {type: 'delete', path: f}

let f1 = getFilename();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.mkdir(f1);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -127,7 +127,7 @@ {type: 'create', path: f1}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.rename(f1, f2);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});

@@ -144,7 +144,7 @@ assert.deepEqual(res, [

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.remove(f1);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});

@@ -163,3 +163,3 @@ assert.deepEqual(res, [

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
if (isSecondPrecision) {

@@ -171,3 +171,3 @@ await sleep(1000);

await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -184,7 +184,7 @@ {type: 'create', path: f2}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.writeFile(f2, 'hi');
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -202,7 +202,7 @@ {type: 'update', path: f2}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.rename(f2, f3);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -220,7 +220,7 @@ {type: 'delete', path: f2},

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.unlink(f2);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -238,7 +238,7 @@ {type: 'delete', path: f2}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.mkdir(f2);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -256,7 +256,7 @@ {type: 'create', path: f2}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.rename(f2, f3);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});

@@ -275,7 +275,7 @@ assert.deepEqual(res, [

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.remove(f1);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -294,7 +294,7 @@ {type: 'delete', path: f1},

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.symlink(f1, f2);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -311,7 +311,7 @@ {type: 'create', path: f2}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.writeFile(f2, 'hi');
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -329,7 +329,7 @@ {type: 'update', path: f1}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.rename(f2, f3);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -347,7 +347,7 @@ {type: 'delete', path: f2},

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.unlink(f2);
await sleep();
let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -366,3 +366,3 @@ {type: 'delete', path: f2}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.writeFile(f1, 'hello world');

@@ -373,3 +373,3 @@ await fs.writeFile(f2, 'hello world');

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -383,3 +383,3 @@ {type: 'create', path: f1}

let f1 = getFilename();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
if (isSecondPrecision) {

@@ -392,3 +392,3 @@ await sleep(1000);

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -403,3 +403,3 @@ {type: 'create', path: f1}

let f2 = getFilename();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.writeFile(f1, 'hello world');

@@ -409,3 +409,3 @@ await fs.rename(f1, f2);

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -421,3 +421,3 @@ {type: 'create', path: f2}

let f4 = getFilename();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
await fs.writeFile(f1, 'hello world');

@@ -429,3 +429,3 @@ await fs.rename(f1, f2);

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -441,3 +441,3 @@ {type: 'create', path: f4}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});

@@ -449,3 +449,3 @@ await fs.writeFile(f1, 'update');

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -460,3 +460,3 @@ {type: 'update', path:f1}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});

@@ -467,3 +467,3 @@ await fs.writeFile(f1, 'update');

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend});
assert.deepEqual(res, [

@@ -483,3 +483,3 @@ {type: 'delete', path: f1}

await sleep();
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend, ignore});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend, ignore});
if (isSecondPrecision) {

@@ -493,3 +493,3 @@ await sleep(1000);

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend, ignore});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend, ignore});
assert.deepEqual(res, [

@@ -504,3 +504,3 @@ {type: 'create', path: f1}

let ignore = [f2];
await fschanges.writeSnapshot(tmpDir, snapshotPath, {backend, ignore});
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend, ignore});
if (isSecondPrecision) {

@@ -514,3 +514,3 @@ await sleep(1000);

let res = await fschanges.getEventsSince(tmpDir, snapshotPath, {backend, ignore});
let res = await watcher.getEventsSince(tmpDir, snapshotPath, {backend, ignore});
assert.deepEqual(res, [

@@ -521,4 +521,38 @@ {type: 'create', path: f1}

});
describe('errors', () => {
it('should error if the watched directory does not exist', async () => {
let dir = path.join(fs.realpathSync(require('os').tmpdir()), Math.random().toString(31).slice(2));
let threw = false;
try {
await watcher.writeSnapshot(dir, snapshotPath, {backend});
} catch (err) {
threw = true;
}
assert(threw, 'did not throw');
});
it('should error if the watched path is not a directory', async () => {
if (backend === 'watchman' && process.platform === 'win32') {
// There is a bug in watchman on windows where the `watch` command hangs if the path is not a directory.
return;
}
let file = path.join(fs.realpathSync(require('os').tmpdir()), Math.random().toString(31).slice(2));
fs.writeFileSync(file, 'test');
let threw = false;
try {
await watcher.writeSnapshot(file, snapshotPath, {backend});
} catch (err) {
threw = true;
}
assert(threw, 'did not throw');
});
});
});
});
});

@@ -1,5 +0,6 @@

const fschanges = require('../');
const watcher = require('../');
const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');
const {execSync} = require('child_process');

@@ -27,3 +28,7 @@ let backends = [];

let fn = events => {
let fn = (err, events) => {
if (err) {
throw err;
}
setImmediate(() => {

@@ -40,3 +45,3 @@ for (let cb of cbs) {

const getFilename = (...dir) => path.join(tmpDir, ...dir, `test${c++}${Math.random().toString(31).slice(2)}`);
let ignoreDir, ignoreFile;
let ignoreDir, ignoreFile, sub;

@@ -49,7 +54,7 @@ before(async () => {

await new Promise(resolve => setTimeout(resolve, 100));
fschanges.subscribe(tmpDir, fn, {backend, ignore: [ignoreDir, ignoreFile]});
sub = await watcher.subscribe(tmpDir, fn, {backend, ignore: [ignoreDir, ignoreFile]});
});
after(async () => {
fschanges.unsubscribe(tmpDir, fn, {backend, ignore: [ignoreDir, ignoreFile]});
await sub.unsubscribe();
});

@@ -448,9 +453,7 @@

function listen() {
return new Promise(resolve => {
let fn = events => {
return new Promise(async resolve => {
let sub = await watcher.subscribe(dir, async (err, events) => {
setImmediate(() => resolve(events));
fschanges.unsubscribe(dir, fn, {backend});
};
fschanges.subscribe(dir, fn, {backend});
await sub.unsubscribe();
}, {backend});
});

@@ -478,9 +481,7 @@ }

function listen(ignore) {
return new Promise(resolve => {
let fn = events => {
return new Promise(async resolve => {
let sub = await watcher.subscribe(dir, async (err, events) => {
setImmediate(() => resolve(events));
fschanges.unsubscribe(dir, fn, {backend, ignore});
};
fschanges.subscribe(dir, fn, {backend, ignore});
await sub.unsubscribe();
}, {backend, ignore});
});

@@ -511,9 +512,7 @@ }

function listen(dir) {
return new Promise(resolve => {
let fn = events => {
return new Promise(async resolve => {
let sub = await watcher.subscribe(dir, async (err, events) => {
setImmediate(() => resolve(events));
fschanges.unsubscribe(dir, fn, {backend});
};
fschanges.subscribe(dir, fn, {backend});
await sub.unsubscribe();
}, {backend});
});

@@ -543,8 +542,6 @@ }

function listen(dir) {
return new Promise(resolve => {
let fn = events => {
setImmediate(() => resolve([events, fn]));
};
fschanges.subscribe(dir, fn, {backend});
return new Promise(async resolve => {
let sub = await watcher.subscribe(dir, (err, events) => {
setImmediate(() => resolve([events, sub]));
}, {backend});
});

@@ -559,3 +556,3 @@ }

await fschanges.writeSnapshot(dir, snapshot, {backend});
await watcher.writeSnapshot(dir, snapshot, {backend});
await new Promise(resolve => setTimeout(resolve, 1000));

@@ -566,3 +563,3 @@

let [watched, fn] = await l;
let [watched, sub] = await l;
assert.deepEqual(watched, [

@@ -572,3 +569,3 @@ {type: 'create', path: path.join(dir, 'test1.txt')}

let since = await fschanges.getEventsSince(dir, snapshot, {backend});
let since = await watcher.getEventsSince(dir, snapshot, {backend});
assert.deepEqual(since, [

@@ -578,7 +575,64 @@ {type: 'create', path: path.join(dir, 'test2.txt')}

fschanges.unsubscribe(dir, fn, {backend});
await sub.unsubscribe();
});
});
describe('errors', () => {
it('should error if the watched directory does not exist', async () => {
let dir = path.join(fs.realpathSync(require('os').tmpdir()), Math.random().toString(31).slice(2));
let threw = false;
try {
await watcher.subscribe(dir, (err, events) => {
assert(false, 'Should not get here');
}, {backend});
} catch (err) {
threw = true;
}
assert(threw, 'did not throw');
});
it('should error if the watched path is not a directory', async () => {
if (backend === 'watchman' && process.platform === 'win32') {
// There is a bug in watchman on windows where the `watch` command hangs if the path is not a directory.
return;
}
let file = path.join(fs.realpathSync(require('os').tmpdir()), Math.random().toString(31).slice(2));
fs.writeFileSync(file, 'test');
let threw = false;
try {
await watcher.subscribe(file, (err, events) => {
assert(false, 'Should not get here');
}, {backend});
} catch (err) {
threw = true;
}
assert(threw, 'did not throw');
});
});
});
});
describe('watchman errors', () => {
it('should emit an error when watchman dies', async () => {
let dir = path.join(fs.realpathSync(require('os').tmpdir()), Math.random().toString(31).slice(2));
fs.mkdirpSync(dir);
await new Promise(resolve => setTimeout(resolve, 100));
let p = new Promise(resolve => {
watcher.subscribe(dir, (err, events) => {
setImmediate(() => resolve(err));
}, {backend: 'watchman'});
});
execSync('watchman shutdown-server');
let err = await p;
assert(err, 'No error was emitted');
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc