Comparing version 0.1.3 to 0.1.4
55
index.js
var FileWatcher = require('./build/Release/FileWatcher.node'); | ||
var path = require("path"); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var nsfw = module.exports = function(watchPath, callback, pInterval) { | ||
var nsfw = module.exports = function(watchPath, pollCallback, errorCallback, pInterval) { | ||
if (!errorCallback || !(errorCallback instanceof Function)) { | ||
throw new Error('Must provide an error callback'); | ||
} | ||
this.errorCallback = errorCallback; | ||
this.poll; | ||
watchPath = path.resolve(watchPath); | ||
try { | ||
fs.statSync(watchPath); | ||
} catch(error) { | ||
throw error; | ||
} | ||
this.interval = pInterval || 1000; | ||
this.watcher = new FileWatcher.NSFW(watchPath, callback); | ||
this.watcher = new FileWatcher.NSFW(watchPath, pollCallback); | ||
} | ||
@@ -13,17 +26,29 @@ | ||
nsfw.prototype.start = function() { | ||
this.watcher.start(); | ||
var that = this; | ||
this.poll = setInterval(function() { | ||
try { | ||
that.watcher.poll(); | ||
} catch(error) { | ||
clearInterval(that.poll); | ||
throw error; | ||
} | ||
}, this.interval); | ||
try { | ||
this.watcher.start(); | ||
var that = this; | ||
this.poll = setInterval(function() { | ||
try { | ||
that.watcher.poll(); | ||
} catch(error) { | ||
clearInterval(that.poll); | ||
that.errorCallback(error); | ||
} | ||
}, this.interval); | ||
return true; | ||
} catch(error) { | ||
this.errorCallback(error); | ||
return false; | ||
} | ||
}; | ||
nsfw.prototype.stop = function(callback) { | ||
clearInterval(this.poll); | ||
this.watcher.stop(callback); | ||
try { | ||
clearInterval(this.poll); | ||
this.watcher.stop(callback); | ||
return true; | ||
} catch(error) { | ||
this.errorCallback(error); | ||
return false; | ||
} | ||
}; |
{ | ||
"name": "nsfw", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "A simple file watcher for Node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,8 +27,21 @@ # node-sentinel-file-watcher | ||
var simple = require('node-sentinel-file-watcher'); | ||
var watcherOne = new nsfw("dir1", function(events) { | ||
var watcherOne = new nsfw( | ||
"dir1", | ||
function(events) { | ||
// handle events | ||
}); | ||
var watcherTwo = new nsfw("dir2", function(events) { | ||
}, | ||
function(error) { | ||
// handle errors | ||
} | ||
); | ||
var watcherTwo = new nsfw( | ||
"dir2", | ||
function(events) { | ||
// handles other events | ||
}, 5000); // every 5 seconds | ||
}, | ||
function(error) { | ||
// handle errors | ||
} | ||
5000 | ||
); // every 5 seconds | ||
@@ -35,0 +48,0 @@ ``` |
132
test/test.js
@@ -39,2 +39,3 @@ var nsfw = require("../index.js"); | ||
var deleteEvents = 0; | ||
var errors = false; | ||
@@ -62,5 +63,11 @@ function findEvents(element, index, array) { | ||
var watch = new nsfw(filePath, function(events) { | ||
events.forEach(findEvents); | ||
}); | ||
var watch = new nsfw( | ||
filePath, | ||
function(events) { | ||
events.forEach(findEvents); | ||
}, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
watch.start(); | ||
@@ -95,2 +102,3 @@ | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert(changeEvents >= 1, "NSFW did not hear the change event."); | ||
@@ -113,2 +121,3 @@ assert(createEvents == 2, "NSFW did not hear the create event."); | ||
var inPath = path.resolve(workDir, "test2", "folder2"); | ||
var errors = false; | ||
@@ -123,5 +132,11 @@ function findCreateEvent(element, index, array) { | ||
} | ||
var watch = new nsfw("./mockfs", function(events) { | ||
events.forEach(findCreateEvent); | ||
}); | ||
var watch = new nsfw( | ||
"./mockfs", | ||
function(events) { | ||
events.forEach(findCreateEvent); | ||
}, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
watch.start(); | ||
@@ -138,2 +153,3 @@ | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert.equal(createEventFound, true, "NSFW did not hear the create event."); | ||
@@ -155,2 +171,3 @@ return watch.stop(); | ||
var inPath = path.resolve(workDir, "test3"); | ||
var errors = false; | ||
@@ -165,5 +182,11 @@ function findDeleteEvent(element, index, array) { | ||
} | ||
var watch = new nsfw("./mockfs", function(events) { | ||
events.forEach(findDeleteEvent); | ||
}); | ||
var watch = new nsfw( | ||
"./mockfs", | ||
function(events) { | ||
events.forEach(findDeleteEvent); | ||
}, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
watch.start(); | ||
@@ -178,2 +201,3 @@ | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert.equal(deleteEventFound, true, "NSFW did not hear the delete event."); | ||
@@ -196,2 +220,3 @@ return watch.stop(); | ||
var inPath = path.resolve(workDir, "test0"); | ||
var errors = false; | ||
@@ -206,5 +231,11 @@ function findChangeEvent(element, index, array) { | ||
} | ||
var watch = new nsfw("./mockfs", function(events) { | ||
events.forEach(findChangeEvent); | ||
}); | ||
var watch = new nsfw( | ||
"./mockfs", | ||
function(events) { | ||
events.forEach(findChangeEvent); | ||
}, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
watch.start(); | ||
@@ -221,2 +252,3 @@ | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert.equal(changeEventFound, true, "NSFW did not hear the change event."); | ||
@@ -240,2 +272,3 @@ return watch.stop(); | ||
var fileB = "testing0.file"; | ||
var errors = false; | ||
@@ -256,4 +289,16 @@ function registerCreateEvents(element, index, array) { | ||
var watchA = new nsfw(dirA, eventHandler); | ||
var watchB = new nsfw(dirB, eventHandler); | ||
var watchA = new nsfw( | ||
dirA, | ||
eventHandler, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
var watchB = new nsfw( | ||
dirB, | ||
eventHandler, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
@@ -276,2 +321,3 @@ watchA.start(); | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert.equal(deleteEvents, 2, "Failed to hear both delete events."); | ||
@@ -287,4 +333,7 @@ return watchA.stop(); | ||
.then(function() { | ||
return watchB.stop() | ||
return watchB.stop(); | ||
}) | ||
.catch(function() { | ||
return watchB.stop(); | ||
}) | ||
.then(function() { | ||
@@ -302,2 +351,3 @@ Promise.reject(error); | ||
var createdCount = 0; | ||
var errors = false; | ||
@@ -318,5 +368,11 @@ function findCreateEvent(element, index, array) { | ||
} | ||
var watch = new nsfw("./mockfs", function(events) { | ||
events.forEach(findCreateEvent); | ||
}); | ||
var watch = new nsfw( | ||
"./mockfs", | ||
function(events) { | ||
events.forEach(findCreateEvent); | ||
}, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
watch.start(); | ||
@@ -331,2 +387,3 @@ | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert.equal(createdCount, 3, "NSFW did not hear all 3 delete events."); | ||
@@ -347,2 +404,3 @@ return watch.stop(); | ||
var deletionCount = 0; | ||
var errors = false; | ||
@@ -363,5 +421,11 @@ function findDeleteEvent(element, index, array) { | ||
} | ||
var watch = new nsfw("./mockfs", function(events) { | ||
events.forEach(findDeleteEvent); | ||
}); | ||
var watch = new nsfw( | ||
"./mockfs", | ||
function(events) { | ||
events.forEach(findDeleteEvent); | ||
}, | ||
function() { | ||
errors = true; | ||
} | ||
); | ||
watch.start(); | ||
@@ -376,2 +440,3 @@ | ||
.then(function() { | ||
assert(errors == false, "NSFW received an error."); | ||
assert.equal(deletionCount, 3, "NSFW did not hear all 3 delete events."); | ||
@@ -393,15 +458,14 @@ return watch.stop(); | ||
var inPath = path.resolve(workDir, "test4"); | ||
var watch = new nsfw(inPath, function(){}); | ||
var errorFound = false; | ||
var exceptionHandler = process.listeners('uncaughtException').pop(); | ||
var newExceptionHandler = function(error) { | ||
if (error.message === "Access is denied") { | ||
process.listeners('uncaughtException').push(exceptionHandler); | ||
errorFound = true; | ||
var watch = new nsfw( | ||
inPath, | ||
function(){}, | ||
function(error) { | ||
if (error.message === "Access is denied") { | ||
errorFound = true; | ||
} | ||
} | ||
}; | ||
); | ||
process.removeListener('uncaughtException', exceptionHandler); | ||
process.once('uncaughtException', newExceptionHandler); | ||
watch.start(); | ||
@@ -417,7 +481,3 @@ | ||
assert.equal(errorFound, true, "NSFW did not throw an exception when the watch folder was deleted."); | ||
}) | ||
.catch(function(error) { | ||
process.listeners('uncaughtException').push(exceptionHandler); | ||
return Promise.reject(error); | ||
}) | ||
}); | ||
}); | ||
@@ -427,3 +487,3 @@ | ||
this.timeout(21000); | ||
var watch = new nsfw(workDir, function(){}); | ||
var watch = new nsfw(workDir, function(){}, function(e) { throw e; }); | ||
@@ -430,0 +490,0 @@ var counter = 10; |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
498
77
73051
2