Socket
Socket
Sign inDemoInstall

co-pipe

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    co-pipe

Wait the completion of piping.


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Install size
14.7 kB
Created
Weekly downloads
 

Readme

Source

co-pipe

Build Status Coverage Status JavaScript Style Guide dependencies Status devDependencies Status

Wait the completion of piping.

Install

$ npm install co-pipe

Usage

Vanilla Promise

const fs = require('fs')
const pipe = require('co-pipe')

const reader = fs.createReadStream('foo.txt')
const writer = fs.createWriteStream('bar.txt')

pipe(reader, writer).then(
  () => console.log('File successfully copied.'),
  (error) => console.log(`Something was wrong with ${
    error.stream === reader ? 'reader' : 'writer'
  }`)
)

Use with co

const fs = require('fs')
const co = require('co')
const pipe = require('co-pipe')

co(function * () {
  const reader = fs.createReadStream('foo.txt')
  const writer = fs.createWriteStream('bar.txt')

  try {
    yield pipe(reader, writer)
    console.log('File successfully copied.')
  } catch (error) {
    console.log(`Something was wrong with ${
      error.stream === reader ? 'reader' : 'writer'
    }`)
  }
})

Use with koa

const fs = require('fs')
const koa = require('koa')
const pipe = require('co-pipe')

const app = koa()

app.use(function * () {
  const writer = fs.createWriteStream('foo.txt')
  yield pipe(this.req, writer)
  this.body = 'Request successfully written to foo.txt'
})

Multiple Streams

const fs = require('fs')
const zlib = require('zlib')
const pipe = require('co-pipe')

const reader = fs.createReadStream('foo.txt')
const gzip = zlib.createGzip()
const writer = fs.createWriteStream('bar.txt.gz')

pipe(reader, gzip, writer).then(
  () => console.log('File successfully gzipped.'),
  (error) => console.log(`Something was wrong with ${
    error.stream === reader ? 'reader' :
      (error.stream === gzip ? 'gzip' : 'writer')
  }`)
)

License

MIT

Keywords

FAQs

Last updated on 27 Oct 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc