Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-watcher

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-watcher - npm Package Compare versions

Comparing version 0.1.2 to 1.0.0

33

index.js

@@ -6,13 +6,26 @@ 'use strict'

const TOLERANCE = 200
const PLATFORMS = ['win32', 'darwin']
// OS watcher.
let watchFolder = (workingDir, recursive, callback) => {
let watchFolder = (workingDir, recursive, tolerance, callback) => {
let options = { persistent: true, recursive: recursive }
let last = { filePath: null, timestamp: null }
let w = fs.watch(workingDir, options, (event, fileName) => {
// On Windows it may actually be empty.
if (fileName) {
callback(path.join(workingDir, fileName))
// On Windows fileName may actually be empty.
let filePath = fileName ? path.join(workingDir, fileName) : workingDir
// Eliminate double reporting.
if (tolerance) {
let now = Date.now()
if (filePath === last.filePath && now - last.timestamp < tolerance) {
return
}
last.filePath = filePath
last.timestamp = now
}
callback(filePath)
})

@@ -45,11 +58,15 @@

let watch = (workingDir, callback) => {
let watch = (workingDir, callback, tolerance = TOLERANCE) => {
workingDir = path.resolve(workingDir)
let cache = {}
// Enable tolerance only for Windows.
tolerance = process.platform === 'win32' ? tolerance : 0
// Use recursive flag if natively available.
if (PLATFORMS.indexOf(process.platform) !== -1) {
return watchFolder(workingDir, true, callback)
return watchFolder(workingDir, true, tolerance, callback)
}
// Attach handlers for each folder recursively.
let cache = {}
watchFolderFallback(workingDir, (localPath) => {

@@ -66,3 +83,3 @@ fs.stat(localPath, (err, stat) => {

cache[localPath] = true
watchFolder(localPath, false, callback)
watchFolder(localPath, false, tolerance, callback)
}

@@ -69,0 +86,0 @@ })

{
"name": "simple-watcher",
"version": "0.1.2",
"version": "1.0.0",
"description": "\"A simple recursive directory watcher.\"",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -13,12 +13,16 @@ # Simple Watcher

* Leverages the `recursive` options on OS X and Windows; uses a fallback for other platforms.
* Does not care about Windows reporting multiple changes for one file (a simple workaround for this below).
* Takes care of WinAPI's `ReadDirectoryChangesW` [double reporting](http://stackoverflow.com/questions/14036449/c-winapi-readdirectorychangesw-receiving-double-notifications).
## Usage
Basic example:
```JavaScript
const watch = require('simple-watcher')
/**
* Recursively watches for directory changes.
* @param {string} workingDir - Directory to watch.
* @param {function} callback - Triggered on change.
* @param {number} tolerance - Interval in which multiple changes to the same file
* on Windows will be treated as one; default is 200ms.
*/
watch('/path/to/directory', (filePath) => {

@@ -28,25 +32,1 @@ console.log(`Changed: ${filePath}`)

```
WinAPI's `ReadDirectoryChangesW` double reporting fix:
```JavaScript
const watch = require('simple-watcher')
let last = { filePath: null, timestamp: null }
let delta = 300 // Adjust if needed.
watch('/path/to/directory', (filePath) => {
// Skip if the last change within the delta time was the same.
let now = Date.now()
if (filePath === last.filePath && now - last.timestamp < delta) {
return
}
// Save the change.
last.filePath = filePath
last.timestamp = now
console.log(`Changed: ${filePath}`)
})
```
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc