fsevents
Advanced tools
Comparing version 2.0.1 to 2.0.2-pre-1
119
fsevents.js
@@ -13,102 +13,49 @@ /* | ||
const { stat } = require('fs'); | ||
const Native = require('./fsevents.node'); | ||
const { EventEmitter } = require('events'); | ||
const native = new WeakMap(); | ||
class FSEvents { | ||
constructor(path, handler) { | ||
if ('string' !== typeof path) throw new TypeError('path must be a string'); | ||
if ('function' !== typeof handler) throw new TypeError('function must be a function'); | ||
Object.defineProperties(this, { | ||
path: { value: path }, | ||
handler: { value: handler } | ||
}); | ||
} | ||
start() { | ||
if (native.has(this)) return; | ||
const instance = Native.start(this.path, this.handler); | ||
native.set(this, instance); | ||
return this; | ||
} | ||
stop() { | ||
const instance = native.get(this); | ||
if (!instance) return; | ||
Native.stop(instance); | ||
native.delete(this); | ||
return this; | ||
} | ||
} | ||
FSEvents.prototype[Symbol.toStringTag] = 'FSEvents'; | ||
function watch(path, handler) { | ||
if ('string' !== typeof path) throw new TypeError(`argument 1 must be a string and not a ${typeof path}`); | ||
if ('function' !== typeof handler) throw new TypeError(`argument 2 must be a function and not a ${typeof handler}`); | ||
const fse = Symbol('fsevents'); | ||
class Emitter extends EventEmitter { | ||
constructor(path) { | ||
super(); | ||
this[fse] = new FSEvents(path, (...args) => this.pushEvent(...args)); | ||
} | ||
start() { | ||
this[fse].start(); | ||
return this; | ||
} | ||
stop() { | ||
this[fse].stop(); | ||
return this; | ||
} | ||
pushEvent(path, flags, id) { | ||
this.emit('fsevent', path, flags, id); | ||
const info = getInfo(path, flags, id); | ||
if (info.event === 'moved') { | ||
stat(info.path, (err, stat) => { | ||
info.event = (err || !stat) ? 'moved-out' : 'moved-in'; | ||
this.emit('change', path, info); | ||
this.emit(info.event, path, info); | ||
}); | ||
} else { | ||
Promise.resolve().then(() => { | ||
this.emit('change', path, info); | ||
this.emit(info.event, path, info); | ||
}); | ||
} | ||
} | ||
let instance = Native.start(path, handler); | ||
return ()=>{ | ||
const result = instance ? Promise.resolve(instance).then(Native.stop) : null; | ||
instance = null; | ||
return result; | ||
}; | ||
} | ||
Emitter.prototype[Symbol.toStringTag] = 'FSEventsEmitter'; | ||
module.exports = function (path) { return new Emitter(path); } | ||
module.exports.getInfo = getInfo; | ||
module.exports.FSEvents = FSEvents; | ||
module.exports.Constants = Native.Constants; | ||
function getInfo(path, flags) { | ||
return { | ||
path, flags, | ||
event: getEventType(flags), | ||
type: getFileType(flags), | ||
changes: getFileChanges(flags) | ||
}; | ||
} | ||
function getFileType(flags) { | ||
if (Native.Constants.kFSEventStreamEventFlagItemIsFile & flags) return 'file'; | ||
if (Native.Constants.kFSEventStreamEventFlagItemIsDir & flags) return 'directory'; | ||
if (Native.Constants.kFSEventStreamEventFlagItemIsSymlink & flags) return 'symlink'; | ||
if (Native.constants.kFSEventStreamEventFlagItemIsFile & flags) return 'file'; | ||
if (Native.constants.kFSEventStreamEventFlagItemIsDir & flags) return 'directory'; | ||
if (Native.constants.kFSEventStreamEventFlagItemIsSymlink & flags) return 'symlink'; | ||
} | ||
function getEventType(flags) { | ||
if (Native.Constants.kFSEventStreamEventFlagItemRemoved & flags) return 'deleted'; | ||
if (Native.Constants.kFSEventStreamEventFlagItemRenamed & flags) return 'moved'; | ||
if (Native.Constants.kFSEventStreamEventFlagItemCreated & flags) return 'created'; | ||
if (Native.Constants.kFSEventStreamEventFlagItemModified & flags) return 'modified'; | ||
if (Native.Constants.kFSEventStreamEventFlagRootChanged & flags) return 'root-changed'; | ||
if (Native.constants.kFSEventStreamEventFlagItemRemoved & flags) return 'deleted'; | ||
if (Native.constants.kFSEventStreamEventFlagItemRenamed & flags) return 'moved'; | ||
if (Native.constants.kFSEventStreamEventFlagItemCreated & flags) return 'created'; | ||
if (Native.constants.kFSEventStreamEventFlagItemModified & flags) return 'modified'; | ||
if (Native.constants.kFSEventStreamEventFlagRootChanged & flags) return 'root-changed'; | ||
return 'unknown'; | ||
} | ||
function getFileChanges(flags) { | ||
return { | ||
inode: !!(Native.Constants.kFSEventStreamEventFlagItemInodeMetaMod & flags), | ||
finder: !!(Native.Constants.kFSEventStreamEventFlagItemFinderInfoMod & flags), | ||
access: !!(Native.Constants.kFSEventStreamEventFlagItemChangeOwner & flags), | ||
xattrs: !!(Native.Constants.kFSEventStreamEventFlagItemXattrMod & flags) | ||
inode: !!(Native.constants.kFSEventStreamEventFlagItemInodeMetaMod & flags), | ||
finder: !!(Native.constants.kFSEventStreamEventFlagItemFinderInfoMod & flags), | ||
access: !!(Native.constants.kFSEventStreamEventFlagItemChangeOwner & flags), | ||
xattrs: !!(Native.constants.kFSEventStreamEventFlagItemXattrMod & flags) | ||
}; | ||
} | ||
function getInfo(path, flags, id) { | ||
return { | ||
path, flags, id, | ||
event: getEventType(flags), | ||
type: getFileType(flags), | ||
changes: getFileChanges(flags) | ||
}; | ||
} | ||
exports.watch = watch; | ||
exports.getInfo = getInfo; | ||
exports.constants = Native.constants; |
{ | ||
"name": "fsevents", | ||
"version": "2.0.1", | ||
"version": "2.0.2-pre-1", | ||
"description": "Native Access to Mac OS-X FSEvents", | ||
@@ -10,3 +10,3 @@ "main": "fsevents.js", | ||
"engines": { | ||
"node": ">=6.0.0" | ||
"node": "^10.6.0 || >=11.0.0" | ||
}, | ||
@@ -16,3 +16,3 @@ "scripts": { | ||
"install": "[ -f fsevents.node ] || npm run prepare", | ||
"test": "tap ./test", | ||
"test": "/bin/bash ./test.sh 2>/dev/null", | ||
"prepare": "node-gyp rebuild && node-gyp clean" | ||
@@ -40,2 +40,6 @@ }, | ||
"email": "elan.shanker@gmail.com" | ||
}, | ||
{ | ||
"name": "Miroslav Bajtoš", | ||
"email": "mbajtoss@gmail.com" | ||
} | ||
@@ -48,5 +52,3 @@ ], | ||
"homepage": "https://github.com/strongloop/fsevents", | ||
"devDependencies": { | ||
"tap": "~12.0.1" | ||
} | ||
"devDependencies": {} | ||
} |
@@ -22,26 +22,28 @@ # fsevents [![NPM](https://nodei.co/npm/fsevents.png)](https://nodei.co/npm/fsevents/) | ||
```js | ||
var fsevents = require('fsevents'); | ||
var watcher = fsevents(__dirname); | ||
watcher.on('fsevent', function(path, flags, id) { }); // RAW Event as emitted by OS-X | ||
watcher.on('change', function(path, info) { }); // Common Event for all changes | ||
watcher.start() // To start observation | ||
watcher.stop() // To end observation | ||
const fsevents = require('fsevents'); | ||
let stop = fsevents.watch(__dirname, (path, flags, id)=>{ | ||
const info = fsevents.getInfo(path, flags, id); | ||
}); // To start observation | ||
stop(); // To end observation | ||
``` | ||
### Events | ||
### Callback | ||
* *fsevent* - RAW Event as emitted by OS-X | ||
* *change* - Common Event for all changes | ||
* *created* - A File-System-Item has been created | ||
* *deleted* - A File-System-Item has been deleted | ||
* *modified* - A File-System-Item has been modified | ||
* *moved-out* - A File-System-Item has been moved away from this location | ||
* *moved-in* - A File-System-Item has been moved into this location | ||
The callback passed as the second parameter to `.watch` get's called whenever the operating system detects a | ||
a change in the file system. It takes three arguments: | ||
All events except *fsevent* take an *info* object as the second parameter of the callback. The structure of this object is: | ||
* `path` - which is a string naming the path of the item in the filesystem that changed | ||
* `flags` - a numeric value describing what the change was | ||
* `id` - a unique-id identifying this specific event | ||
### `getInfo(path, flags, id) => {info-object}` | ||
The `getInfo` function takes the `path`, `flags` and `id` arguments and converts those parameters into a structure | ||
that is easier to digest to determine what the change was. | ||
The `info-object` has the following shape: | ||
```js | ||
{ | ||
"event": "<event-type>", | ||
"id": <eventi-id>, | ||
"event": "<deleted|moved|created|modified|root-changed|unknown>", | ||
"path": "<path-that-this-is-about>", | ||
@@ -48,0 +50,0 @@ "type": "<file|directory|symlink>", |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
59344
0
19
334
81
1
1
2