# through2-spy

> A through2 wrapper to for simple stream.PassThrough spies.

Latest version **2.0.0** (published 2015-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install through2-spy
pnpm add through2-spy
yarn add through2-spy
bun add through2-spy
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2015-06-17 |
| First published | 2013-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21 |
| Author | Bryce B. Baril |
| Maintainers | bryce |
| Keywords | streams, through, through2, passthrough, spy |

## Links

- npm: https://www.npmjs.com/package/through2-spy
- Repository: git@github.com:brycebaril/through2-spy
- Homepage: https://github.com/brycebaril/through2-spy
- Issues: https://github.com/brycebaril/through2-spy/issues
- npm.io page: https://npm.io/package/through2-spy

## Dependencies (2)

- [xtend](https://npm.io/package/xtend.md) ~4.0.0
- [through2](https://npm.io/package/through2.md) ~2.0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2015-06-17
- 1.2.0 — 2014-06-20
- 1.1.1 — 2014-01-31
- 1.1.0 — 2013-09-20
- 1.0.1 — 2013-08-07
- 1.0.0 — 2013-08-07

## README

through2-spy
============

[![NPM](https://nodei.co/npm/through2-spy.png)](https://nodei.co/npm/through2-spy/)

This is a super thin wrapper around [through2](http://npm.im/through2) for creating simple stream.PassThrough spies.

Saves you a tiny bit of boilerplate compared to `through2` for writing stream spies.

Note you will **NOT** be able to do anything but spy and abort the stream pipeline. To do any filtering or transformations you should consider `through2` `through2-filter` or `through2-map`.

Pass a function to run as each chunk goes through your stream pipeline. Return an Error to abort the pipeline.

```js

var spy = require("through2-spy")

var count = 0
var countChunks = spy(function (chunk) {
  count++
})

// vs. with through2:
var countChunks = through2(function (chunk, encoding, callback) {
  count++
  this.push(chunk)
  return callback()
})

// Then use your spy:
source.pipe(countChunks).pipe(sink)

// Additionally accepts `wantStrings` argument to conver buffers into strings
var nsaregex = /(open source)|(foss)|(node\.js)/i
var prizm = spy({wantStrings: true}, function (str) {
  var wiretap = str.match(nsaregex)
  if (wiretap) this.emit("OMGTERRIST", wiretap[0], str)
})

prizm.on("OMGTERRIST", sendDrone(/* ... */))

internet.pipe(prizm).pipe(internet)

// Return an Error to abort the pipeline
var Meter = spy.ctor({maxBytes: 1024, bytes: 0}, function (chunk) {
  this.options.bytes += chunk.length
  if (this.options.bytes >= this.options.maxBytes) return new Error("Over 1024 byte limit!")
})

var meter = new Meter()

```

API
---

`require("through2-spy")([options], fn)`
---

Create a `through2-spy` instance that will call `fn(chunk)` and then silently pass through data downstream.

`require("through2-spy").ctor([options], fn)`
---

Create a `through2-spy` Type that can be instantiated via `new Type()` or `Type()` to create reusable spies.

`require("through2-spy").obj([options], fn)`
---

Create a `through2-spy` that defaults to `objectMode = true`.

`require("through2-spy").objCtor([options], fn)`
---

Create a `through2-spy` Type that defaults to `objectMode = true`.

Options
-------

  * wantStrings: Automatically call chunk.toString() for the super lazy.
  * all other through2 options

LICENSE
=======

MIT

---
_Source: https://npm.io/package/through2-spy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
