# peek-stream

> Transform stream that lets you peek the first line before deciding how to parse it

Latest version **1.1.3** (published 2018-03-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install peek-stream
pnpm add peek-stream
yarn add peek-stream
bun add peek-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.1.3 |
| Published | 2018-03-21 |
| First published | 2014-07-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 58 |
| Author | Mathias Buus |
| Maintainers | mafintosh |
| Keywords | peek, stream, parse, swap |

## Links

- npm: https://www.npmjs.com/package/peek-stream
- Repository: https://github.com/mafintosh/peek-stream
- Issues: https://github.com/mafintosh/peek-stream/issues
- npm.io page: https://npm.io/package/peek-stream

## Dependencies (3)

- [through2](https://npm.io/package/through2.md) ^2.0.3
- [duplexify](https://npm.io/package/duplexify.md) ^3.5.0
- [buffer-from](https://npm.io/package/buffer-from.md) ^1.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2018-03-21
- 1.1.2 — 2017-04-08
- 1.1.1 — 2014-07-25
- 1.1.0 — 2014-07-23
- 1.0.0 — 2014-07-23

## README

# peek-stream

Transform stream that lets you peek the first line before deciding how to parse it

```
npm install peek-stream
```

[![build status](http://img.shields.io/travis/mafintosh/peek-stream.svg?style=flat)](http://travis-ci.org/mafintosh/peek-stream)
![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat)

## Usage

``` js
var peek = require('peek-stream')
var ldjson = require('ldjson-stream')
var csv = require('csv-parser')

var isCSV = function(data) {
  return data.toString().indexOf(',') > -1
}

var isJSON = function(data) {
  try {
    JSON.parse(data)
    return true
  } catch (err) {
    return false
  }
}

// call parser to create a new parser
var parser = function() {
  return peek(function(data, swap) {
    // maybe it is JSON?
    if (isJSON(data)) return swap(null, ldjson())

    // maybe it is CSV?
    if (isCSV(data)) return swap(null, csv())

    // we do not know - bail
    swap(new Error('No parser available'))
  })
}
```

The above parser will be able to parse both line delimited JSON and CSV

``` js
var parse = parser()

parse.write('{"hello":"world"}\n{"hello":"another"}\n')
parse.on('data', function(data) {
  console.log(data) // prints {hello:'world'} and {hello:'another'}
})
```

Or

``` js
var parse = parser()

parse.write('test,header\nvalue-1,value-2\n')
parse.on('data', function(data) {
  console.log(data) // prints {test:'value-1', header:'value-2'}
})
```

Per default `data` is the first line (or the first `65535` bytes if no newline is found).
To change the max buffer pass an options map to the constructor

``` js
var parse = peek({
  maxBuffer: 10000
}, function(data, swap) {
  ...
})
```

If you want to emit an error if no newline is found set `strict: true` as well.


## License

MIT

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