# unchunk

> Unchunk readable stream into Promise

Latest version **0.2.0** (published 2020-08-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install unchunk
pnpm add unchunk
yarn add unchunk
bun add unchunk
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-08-14 |
| First published | 2020-02-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.13.0 |
| Dependencies | 0 |
| Unpacked size | 4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 81 |
| Maintainers | deepsweet, fosimus, psxcode |
| Keywords | stream, data, chunk |

## Links

- npm: https://www.npmjs.com/package/unchunk
- Repository: https://github.com/nextools/metarepo
- Homepage: https://github.com/nextools/metarepo#readme
- Issues: https://github.com/nextools/metarepo/issues
- npm.io page: https://npm.io/package/unchunk

## 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

- 0.2.0 (latest) — 2020-08-14
- 0.1.0 — 2020-03-14
- 0.0.0 — 2020-02-26

## README

# unchunk ![npm](https://flat.badgen.net/npm/v/unchunk)

Unchunk readable stream into Promise. Works with HTTP server requests, HTTP request responses or any other event emitters where `data` event receives `Buffer`-chunks and `end` means it's done.

## Install

```sh
$ yarn add unchunk
```

## Usage

```ts
const unchunkBuffer: (emitter: EventEmitter) => Promise<Buffer>
```

```ts
const unchunkString: (emitter: EventEmitter) => Promise<string>
```

```ts
type TAnyObject = {
  [key: string]: any
}

const unchunkJson: <T = TAnyObject>(emitter: EventEmitter) => Promise<T>
```

```ts
import http from 'http'
import { unchunkJson } from 'unchunk'

const server = http.createServer(async (req, res) => {
  const body = await unchunkJson(req)

  console.log(body)
  // { "ping": true }

  res.end(JSON.stringify({ pong: true }))
})

server.listen(3000, 'localhost')

const request = http.request('http://localhost:3000/', { method: 'POST' }, async (res) => {
  const body = await unchunkJson(res)

  console.log(body)
  // { "pong": true }
})

request.write(JSON.stringify({ ping: true }))
request.end()
```

```ts
import { Readable } from 'stream'
import { unchunkString } from 'unchunk'

let i = 0

const readable = new Readable({
  read() {
    if (i === 0) {
      this.push('he')
    } else if (i === 1) {
      this.push('l')
    } else {
      this.push('lo')
      this.push(null)
    }

    i++
  }
})

const result = await unchunkString(readable)

console.log(result)
// hello
```

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