New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

klaw

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

klaw - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

6

CHANGELOG.md

@@ -0,3 +1,9 @@

1.0.0 / 2015-10-25
------------------
- removed unused `filter` param
- bugfix: always set `streamOptions` to `objectMode`
- simplified, converted from push mode (streams 1) to proper pull mode (streams 3)
0.1.0 / 2015-10-25
------------------
- initial release

55

index.js

@@ -7,61 +7,34 @@ var fs = require('fs')

function Walker (dir, filter, streamOptions) {
Readable.call(this, assign({ objectMode: true }, streamOptions))
this.path = path.resolve(dir)
this.filter = filter
this.pending = 0
this.start()
function Walker (dir, streamOptions) {
Readable.call(this, assign({}, streamOptions, { objectMode: true }))
this.root = path.resolve(dir)
this.paths = [this.root]
}
util.inherits(Walker, Readable)
Walker.prototype.start = function () {
this.visit(this.path)
return this
}
Walker.prototype.visit = function (item) {
this.pending++
Walker.prototype._read = function () {
if (this.paths.length === 0) return this.push(null)
var self = this
var item = this.paths.shift()
fs.lstat(item, function (err, stats) {
if (err) {
self.emit('error', err, {path: item, stats: stats})
return self.finishItem()
}
if (err) return self.emit('error', err, { path: item, stats: stats })
if (!stats.isDirectory()) return self.push({ path: item, stats: stats })
if (self.filter && !self.filter({path: item, stats: stats})) return self.finishItem()
if (!stats.isDirectory()) {
self.push({ path: item, stats: stats })
return self.finishItem()
}
fs.readdir(item, function (err, items) {
if (err) {
self.emit('error', err, {path: item, stats: stats})
return self.finishItem()
}
if (err) return self.emit('error', err, { path: item, stats: stats })
self.push({path: item, stats: stats})
items.forEach(function (part) {
self.visit(path.join(item, part))
self.paths.push(path.join(item, part))
})
self.finishItem()
self.push({ path: item, stats: stats })
})
})
return this
}
Walker.prototype.finishItem = function () {
this.pending -= 1
if (this.pending === 0) this.push(null)
return this
}
Walker.prototype._read = function () { }
function walk (path) {
return new Walker(path)// .start()
return new Walker(path)
}
module.exports = walk
{
"name": "klaw",
"version": "0.1.0",
"version": "1.0.0",
"description": "File system walker with Readable stream interface.",

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

@@ -33,5 +33,5 @@ Node.js - klaw

- `directory`: The directory to recursively walk. Type `string`.
- `options`: Right now it's just Readable stream options.
- `options`: Right now it's just [Readable stream options](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
Streams 1 (push) example:
**Streams 1 (push) example:**

@@ -51,3 +51,3 @@ ```js

Streams 2 & 3 (pull) example:
**Streams 2 & 3 (pull) example:**

@@ -100,21 +100,24 @@ ```js

### Filtering
### Aggregation / Filtering / Executing Actions (Through Streams)
On many occasions you may want to filter files based upon size, extension, etc.
Or you may want to aggregate stats on certain file types. Or maybe you want to
perform an action on certain file types.
You should use the module [`through2`](https://www.npmjs.com/package/through2) to easily
accomplish this.
Example (skipping directories):
Install `through2`:
first:
npm i --save through2
**Example (skipping directories):**
```js
var fs = require('fs-extra')
var klaw = require('klaw')
var through2 = require('through2')
var excludeDirFilter = through2.obj(function (item, enc, next) {
if (!item.stat.isDirectory()) this.push(item)
if (!item.stats.isDirectory()) this.push(item)
next()

@@ -124,3 +127,3 @@ })

var items = [] // files, directories, symlinks, etc
klaw(TEST_DIR)
klaw('/some/dir')
.pipe(excludeDirFilter)

@@ -137,2 +140,75 @@ .on('data', function (item) {

**Example (totaling size of PNG files):**
```js
var klaw = require('klaw')
var path = require('path')
var through2 = require('through2')
var totalPngsInBytes = 0
var aggregatePngSize = through2.obj(function (item, enc, next) {
if (path.extname(item.path) === 'png') {
totalPngsInBytes += item.stats.size
}
this.push(item)
next()
})
klaw('/some/dir')
.pipe(excludeDirFilter)
.on('data', function (item) {
items.push(item.path)
})
.on('end', function () {
console.dir(totalPngsInBytes) // => total of all pngs (bytes)
})
```
**Example (deleting all .tmp files):**
```js
var fs = require('fs')
var klaw = require('klaw')
var through2 = require('through2')
var deleteAction = through2.obj(function (item, enc, next) {
this.push(item)
if (path.extname(item.path) === 'tmp') {
item.deleted = true
fs.unklink(item.path, next)
} else {
item.deleted = false
next()
}
})
var deletedFiles = []
klaw('/some/dir')
.pipe(deleteAction)
.on('data', function (item) {
if (!item.deleted) return
deletedFiles.push(item.path)
})
.on('end', function () {
console.dir(deletedFiles) // => all deleted files
})
```
You can even chain a bunch of these filters and aggregators together. By using
multiple pipes.
**Example (using multiple filters / aggregators):**
```js
klaw('/some/dir')
.pipe(filterCertainFiles)
.pipe(deleteSomeOtherFiles)
.on('end', function () {
console.log('all done!')
})
```
License

@@ -139,0 +215,0 @@ -------

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