0.2.0 • Published 4 years ago

unchunk v0.2.0

Weekly downloads
130
License
MIT
Repository
github
Last release
4 years ago

unchunk npm

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

$ yarn add unchunk

Usage

const unchunkBuffer: (emitter: EventEmitter) => Promise<Buffer>
const unchunkString: (emitter: EventEmitter) => Promise<string>
type TAnyObject = {
  [key: string]: any
}

const unchunkJson: <T = TAnyObject>(emitter: EventEmitter) => Promise<T>
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()
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