unchunk
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i unchunk

Weekly Downloads

60,416

Version

0.2.0

License

MIT

Unpacked Size

4.12 kB

Total Files

5

Last publish

Collaborators

  • deepsweet
  • fosimus
  • psxcode