# discard-stream

> a transform stream that buffers up to N items, discarding on further writes

Latest version **1.0.1** (published 2015-08-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install discard-stream
pnpm add discard-stream
yarn add discard-stream
bun add discard-stream
```

## 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 | 1.0.1 |
| Published | 2015-08-29 |
| First published | 2015-08-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Chris Dickinson |
| Maintainers | chrisdickinson |
| Keywords | discard-stream, readable, stream, log, metered, ratelimit |

## Links

- npm: https://www.npmjs.com/package/discard-stream
- Repository: https://github.com/chrisdickinson/discard-stream
- Homepage: https://github.com/chrisdickinson/discard-stream#readme
- Issues: https://github.com/chrisdickinson/discard-stream/issues
- npm.io page: https://npm.io/package/discard-stream

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-08-29
- 1.0.0 — 2015-08-28

## README

# discard-stream

A transform stream that passes through writes by default, but only buffers up
to a certain number of pending writes when backpressure is applied. New writes
will push old writes out of the buffer, so no more than N writes can be pending
at a time.

This is especially useful for logging or metrics situations, where:

1. the data generated is of less value than keeping the process healthy,
2. and it is difficult or meaningless to apply backpressure to the generation of the data.

```javascript
var discardStream = require('discard-stream')

var myLogs = discardStream({
  maxBacklog: 100,
  objectMode: true
})
myLogs.pipe(fs.createWriteStream('some/slow/logs'))

myHighPerformanceWebserver.on('request', function (req, res) {
  myLogs.write(metrics)
})
```

```
// or use it as a transform:
someVeryFastMetricsStream()
  .pipe(discardStream(10))
  .pipe(someSlowTCPConnection())
```

## API

#### `discardStream(options) → DiscardStream`

Create a discard stream using the provided [stream options][stream-opts].
An additional options key, `maxBacklog`, is required. `maxBacklog` determines
the maximum number of written values to be retained during backpressure — any
writes _over_ `maxBacklog` will result in an old buffered value being discarded.

`options.highWaterMark` will be ignored — it is always set to 0.

**`DiscardStream`'s will never apply backpressure upstream.** They act as
infinite sinks for data.

#### `discardStream(number) → DiscardStream`

A shorthand for creating a discard stream with the following options:

```
discardStream({
  maxBacklog: number,
  highWaterMark: 0,
  objectMode: true
})
```

#### `DiscardStream#backlog`

Get the backlog of queued writes as an array.

## Events

#### `"discard"` (data)

Emitted when an old write is discarded in favor of a more recent write.
This is the last chance the user has to do something with `data`.

## License

ISC

[stream-opts]: https://iojs.org/api/stream.html#stream_new_stream_readable_options

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