New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 2.0.1 to 3.0.0

readme.md

69

index.js

@@ -6,8 +6,9 @@ 'use strict'

const TOLERANCE = 200
const PLATFORMS = ['win32', 'darwin']
const INTERVAL = 300
// OS watcher.
let watchFolder = (workingDir, recursive, callback) => {
let watchFolder = (workingDir, recursive, tolerance, callback) => {
let options = { persistent: true, recursive: recursive }
let last = { filePath: null, timestamp: 0 }

@@ -18,3 +19,21 @@ let w = fs.watch(workingDir, options, (event, fileName) => {

let filePath = fileName ? path.join(workingDir, fileName) : workingDir
callback(filePath)
if (!tolerance) {
return callback(filePath)
}
fs.stat(filePath, (err, stat) => {
// If error, the file was likely deleted.
let timestamp = err ? 0 : (new Date(stat.mtime)).getTime()
let ready = err || timestamp - last.timestamp >= tolerance
let fileMatches = filePath === last.filePath
last.filePath = filePath
last.timestamp = timestamp
if (fileMatches && !ready) {
return
}
callback(filePath)
})
})

@@ -27,4 +46,3 @@

// Recursive fallback handler.
let watchFolderFallback = (parent, callback) => {
let watchFolderFallback = (parent, tolerance, callback) => {
// This code is synchronous to be able to tell when it actually finishes.

@@ -37,3 +55,3 @@ try {

watchFolder(parent, false, callback)
watchFolder(parent, false, tolerance, callback)

@@ -43,3 +61,3 @@ // Iterate over list of children.

child = path.resolve(parent, child)
watchFolderFallback(child, callback)
watchFolderFallback(child, tolerance, callback)
})

@@ -51,8 +69,13 @@ } catch (err) {

let watch = (workingDir, callback) => {
let watch = (workingDir, callback, tolerance) => {
workingDir = path.resolve(workingDir)
// Set the default tolerance value.
tolerance = tolerance === undefined ? TOLERANCE : tolerance
// 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)
}

@@ -62,3 +85,3 @@

let cache = {}
watchFolderFallback(workingDir, (localPath) => {
watchFolderFallback(workingDir, tolerance, (localPath) => {
fs.stat(localPath, (err, stat) => {

@@ -74,3 +97,3 @@ // Delete cache entry.

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

@@ -83,24 +106,8 @@ })

let main = (workingDir, callback, interval) => {
interval = interval !== undefined ? interval : INTERVAL
// Enqueue items on change.
let queue = []
watch(workingDir, (filePath) => {
queue.push(filePath)
if (require.main === module) {
watch(process.argv[2], (fileName) => {
console.log(`${fileName}`)
})
// Start the interval.
setInterval(() => {
// Dequeue paths and store them as dictionary keys.
let dict = {}
while (queue.length) {
dict[queue.pop()] = true
}
// Run callback with unique paths.
let unique = Object.keys(dict)
unique.length && callback(unique)
}, interval)
}
module.exports = main
module.exports = watch
{
"name": "simple-watcher",
"version": "2.0.1",
"version": "3.0.0",
"description": "\"A simple recursive directory watcher.\"",

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

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