You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.6 KiB

4 years ago
  1. # duplexify
  2. Turn a writeable and readable stream into a single streams2 duplex stream.
  3. Similar to [duplexer2](https://github.com/deoxxa/duplexer2) except it supports both streams2 and streams1 as input
  4. and it allows you to set the readable and writable part asynchronously using `setReadable(stream)` and `setWritable(stream)`
  5. ```
  6. npm install duplexify
  7. ```
  8. [![build status](http://img.shields.io/travis/mafintosh/duplexify.svg?style=flat)](http://travis-ci.org/mafintosh/duplexify)
  9. ## Usage
  10. Use `duplexify(writable, readable, streamOptions)` (or `duplexify.obj(writable, readable)` to create an object stream)
  11. ``` js
  12. var duplexify = require('duplexify')
  13. // turn writableStream and readableStream into a single duplex stream
  14. var dup = duplexify(writableStream, readableStream)
  15. dup.write('hello world') // will write to writableStream
  16. dup.on('data', function(data) {
  17. // will read from readableStream
  18. })
  19. ```
  20. You can also set the readable and writable parts asynchronously
  21. ``` js
  22. var dup = duplexify()
  23. dup.write('hello world') // write will buffer until the writable
  24. // part has been set
  25. // wait a bit ...
  26. dup.setReadable(readableStream)
  27. // maybe wait some more?
  28. dup.setWritable(writableStream)
  29. ```
  30. If you call `setReadable` or `setWritable` multiple times it will unregister the previous readable/writable stream.
  31. To disable the readable or writable part call `setReadable` or `setWritable` with `null`.
  32. If the readable or writable streams emits an error or close it will destroy both streams and bubble up the event.
  33. You can also explicitly destroy the streams by calling `dup.destroy()`. The `destroy` method optionally takes an
  34. error object as argument, in which case the error is emitted as part of the `error` event.
  35. ``` js
  36. dup.on('error', function(err) {
  37. console.log('readable or writable emitted an error - close will follow')
  38. })
  39. dup.on('close', function() {
  40. console.log('the duplex stream is destroyed')
  41. })
  42. dup.destroy() // calls destroy on the readable and writable part (if present)
  43. ```
  44. ## HTTP request example
  45. Turn a node core http request into a duplex stream is as easy as
  46. ``` js
  47. var duplexify = require('duplexify')
  48. var http = require('http')
  49. var request = function(opts) {
  50. var req = http.request(opts)
  51. var dup = duplexify(req)
  52. req.on('response', function(res) {
  53. dup.setReadable(res)
  54. })
  55. return dup
  56. }
  57. var req = request({
  58. method: 'GET',
  59. host: 'www.google.com',
  60. port: 80
  61. })
  62. req.end()
  63. req.pipe(process.stdout)
  64. ```
  65. ## License
  66. MIT
  67. ## Related
  68. `duplexify` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one.