Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The watchpack npm package is a wrapper around the file-watching functionality of Node.js and chokidar. It provides a higher-level API for watching file changes in a specified directory or set of directories. It is often used in build tools and development servers to watch for file changes and trigger rebuilds or reloads.
Watching Files and Directories
This feature allows you to watch for changes in specific files and directories. You can listen for 'change' events on individual files and 'aggregated' events for batch changes.
const Watchpack = require('watchpack');
const wp = new Watchpack({
// options
});
wp.watch({
files: ['file1.js', 'file2.js'],
directories: ['dir1', 'dir2'],
missing: ['file3.js', 'file4.js']
});
wp.on('change', (filePath, mtime, explanation) => {
console.log(`${filePath} changed`, mtime, explanation);
});
wp.on('aggregated', (changes, removals) => {
console.log('Changes:', changes);
console.log('Removals:', removals);
});
Pausing and Resuming Watch
This feature allows you to temporarily pause the watching process and resume it later. This can be useful when performing batch operations that should not trigger watch events.
const wp = new Watchpack({});
// Start watching
wp.watch({ files: ['file1.js'] });
// Pause watching
wp.pause();
// After some time, resume watching
wp.watch({ files: ['file1.js'] });
Getting Watched Items
This feature allows you to retrieve a list of all files that are currently being watched. This can be useful for debugging or logging purposes.
const wp = new Watchpack({});
wp.watch({ files: ['file1.js'] });
const watchedFiles = wp.getWatchedFiles();
console.log(watchedFiles); // Outputs: ['file1.js']
Chokidar is a more low-level file watching library that watchpack uses under the hood. It provides more granular control over file watching and events but requires more setup and configuration compared to watchpack.
Gaze is another file watching library that offers similar functionality to watchpack. It allows you to watch files and directories for changes and supports patterns for file matching. It differs in its API and the way it handles events and patterns.
Nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected. While it also watches files for changes, its primary use case is different as it focuses on restarting applications rather than providing an API for developers to hook into file changes.
Wrapper library for directory and file watching.
watchpack high level API doesn't map directly to watchers. Instead a three level architecture ensures that for each directory only a single watcher exists.
DirectoryWatchers
from a WatcherManager
, which ensures that only a single DirectoryWatcher
per directory is created.Watcher
can be obtained from a DirectoryWatcher
and provides a filtered view on the DirectoryWatcher
.DirectoryWatcher
and Watcher
to decide when to close them.DirectoryWatcher
.var Watchpack = require("watchpack");
var wp = new Watchpack({
// options:
aggregateTimeout: 1000
// fire "aggregated" event when after a change for 1000ms no additional change occurred
// aggregated defaults to undefined, which doesn't fire an "aggregated" event
poll: true
// poll: true - use polling with the default interval
// poll: 10000 - use polling with an interval of 10s
// poll defaults to undefined, which prefer native watching methods
// Note: enable polling when watching on a network path
// When WATCHPACK_POLLING environment variable is set it will override this option
ignored: "**/.git",
// ignored: "string" - a glob pattern for files or folders that should not be watched
// ignored: ["string", "string"] - multiple glob patterns that should be ignored
// All subdirectories are ignored too
});
// Watchpack.prototype.watch(files: string[], directories: string[], startTime?: number)
wp.watch(listOfFiles, listOfDirectories, Date.now() - 10000);
// starts watching these files and directories
// calling this again will override the files and directories
wp.on("change", function(filePath, mtime) {
// filePath: the changed file
// mtime: last modified time for the changed file (null if file was removed)
// for folders it's a time before that all changes in the directory happened
});
wp.on("aggregated", function(changes, removals) {
// changes: a Set of all changed files
// removals: a Set of all removed files
});
// Watchpack.prototype.pause()
wp.pause();
// stops emitting events, but keeps watchers open
// next "watch" call can reuse the watchers
// Watchpack.prototype.close()
wp.close();
// stops emitting events and closes all watchers
// Watchpack.prototype.getTimeInfoEntries()
var fileTimes = wp.getTimeInfoEntries();
// returns a Map with all known time info objects for files and directories
// this include info from files not directly watched
// key: absolute path, value: object with { safeTime, timestamp }
// safeTime: the time before that all changes happened
// timestamp: only for files, the mtime timestamp of the file
// (deprecated)
// Watchpack.prototype.getTimes()
var fileTimes = wp.getTimes();
// returns an object with all known change times for files
// this include timestamps from files not directly watched
// key: absolute path, value: timestamp as number
FAQs
Wrapper library for directory and file watching.
The npm package watchpack receives a total of 10,657,899 weekly downloads. As such, watchpack popularity was classified as popular.
We found that watchpack demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.