npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fast-walk-fs

v0.0.2

Published

A library for efficiently walking large file system structures.

Downloads

5

Readme

fast-walk-fs

npm version license

A library for efficiently walking large file system structures.

Highlights

  • Concurrently performs io operations for speed.
  • Proper backpressure handling for minimum memory & cpu usage.
  • Built-in filtering.
  • Flexible error handling.

js-standard-style

  • Installation
  • Usage
  • Examples
  • Licence & copyright

Install

npm i fast-walk-fs --save

Usage

const walk = require('fast-walk-fs');

walk(directory, [options]) returns a Readable stream. Every walked entry has following properties:

  • path : absolute path.
  • name : name of the entry within its parent.
  • depth : Depth within the folder structure.
  • stats : An instance of fs.Stats class.

fast-walk-fs will not stop iterating when an error is encountered, you have to listen to events error-readdir and error-stat to handle errors. Optionally fs.walk can be stopped by calling end explicitely.

directory

  • Required: true

The path of the directory to walk.

options

  • Type: object
  • Required: false
  • Default: undefined

visit

  • Type: function
  • Required: false
  • Default: undefined

A function executed to determine if an entry should be walked or not.

This function must return true if the entry has to be walked.

maxConcurrency

  • Type: number
  • Required: false
  • Default: 10

The maximum number of concurrent IO operations allowed to be performed.

fast-walk-fs self adapts the number of concurrent operations. For example if consuming entries is slow, fast-walk-fs internally decreases the number of concurrent IO operations. For example, will not cause excessive memory usage:

const walk = require('fast-walk-fs');

function pause(timeMs) {
    return new Promise(resolve => {
        setTimeout(() => resolve(true), timeMs);
    })
}

const entries = walk('./someLargeDir', { maxConcurrency });
for await (const entry of entries) {
  await pause(100);
}

Examples

Stream (push)

const walk = require('fast-walk-fs');

walk('.').on('data', console.log)

Streams (pull)

const walk = require('fast-walk-fs');

walk('.').on('readable', () => {
  let entry;
  while ((entry = this.read()) !== null) {
    console.log(entry);
  }
})

Async iteration (pull)

const walk = require('fast-walk-fs');

for await (const entry in walk('.')) {
  console.log(entry);
}

Error handling

const walk = require('fast-walk-fs');

const entries = walk('.')
entries.on('error-readdir' (error, entry) => {
  console.log('error while reading directory contents', error, entry);
  // optionally, end to walk
  entries.end();
})
entries.on('error-stat' (error, entry) => {
  console.log('error when stat', error, entry);
})
for await (const entry in entries) {
  console.log(entry);
}

Filtering out everything at depth > 3

const walk = require('fast-walk-fs');
const path = require('path');

const visit = (entry) => {
  return !entry.depth > 3;
}

let totalSize = 0
for await (const entry in walk('.', {visit})) {
  console.log(entry);
}

Totaling size of txt files

const walk = require('fast-walk-fs');
const path = require('path');

const visit = (entry) => {
  if (entry.stats.isFile()) {
    return path.extname(entry.name) === '.txt';
  }
  return true;
}

let totalSize = 0;
for await (const entry in walk('.', {visit})) {
  totalSize += entry.stats.size;
}
console.log(totalSize);

License

ISC