Turn a writeable and readable stream into a single streams2 duplex stream.

Similar to \href{https://github.com/deoxxa/duplexer2}{\texttt{ duplexer2}} except it supports both streams2 and streams1 as input and it allows you to set the readable and writable part asynchronously using {\ttfamily set\+Readable(stream)} and {\ttfamily set\+Writable(stream)}


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install duplexify }

\end{DoxyCode}


\href{http://travis-ci.org/mafintosh/duplexify}{\texttt{ }}\hypertarget{md_sfcode_node_modules_duplexify_README_autotoc_md8287}{}\doxysection{Usage}\label{md_sfcode_node_modules_duplexify_README_autotoc_md8287}
Use {\ttfamily duplexify(writable, readable, stream\+Options)} (or {\ttfamily duplexify.\+obj(writable, readable)} to create an object stream)


\begin{DoxyCode}{0}
\DoxyCodeLine{var duplexify = require('duplexify')}
\DoxyCodeLine{}
\DoxyCodeLine{// turn writableStream and readableStream into a single duplex stream}
\DoxyCodeLine{var dup = duplexify(writableStream, readableStream)}
\DoxyCodeLine{}
\DoxyCodeLine{dup.write('hello world') // will write to writableStream}
\DoxyCodeLine{dup.on('data', function(data) \{}
\DoxyCodeLine{  // will read from readableStream}
\DoxyCodeLine{\}) }

\end{DoxyCode}


You can also set the readable and writable parts asynchronously


\begin{DoxyCode}{0}
\DoxyCodeLine{var dup = duplexify()}
\DoxyCodeLine{}
\DoxyCodeLine{dup.write('hello world') // write will buffer until the writable}
\DoxyCodeLine{                         // part has been set}
\DoxyCodeLine{}
\DoxyCodeLine{// wait a bit ...}
\DoxyCodeLine{dup.setReadable(readableStream)}
\DoxyCodeLine{}
\DoxyCodeLine{// maybe wait some more?}
\DoxyCodeLine{dup.setWritable(writableStream) }

\end{DoxyCode}


If you call {\ttfamily set\+Readable} or {\ttfamily set\+Writable} multiple times it will unregister the previous readable/writable stream. To disable the readable or writable part call {\ttfamily set\+Readable} or {\ttfamily set\+Writable} with {\ttfamily null}.

If the readable or writable streams emits an error or close it will destroy both streams and bubble up the event. You can also explicitly destroy the streams by calling {\ttfamily dup.\+destroy()}. The {\ttfamily destroy} method optionally takes an error object as argument, in which case the error is emitted as part of the {\ttfamily error} event.


\begin{DoxyCode}{0}
\DoxyCodeLine{dup.on('error', function(err) \{}
\DoxyCodeLine{  console.log('readable or writable emitted an error -\/ close will follow')}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{dup.on('close', function() \{}
\DoxyCodeLine{  console.log('the duplex stream is destroyed')}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{dup.destroy() // calls destroy on the readable and writable part (if present) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_duplexify_README_autotoc_md8288}{}\doxysection{HTTP request example}\label{md_sfcode_node_modules_duplexify_README_autotoc_md8288}
Turn a node core http request into a duplex stream is as easy as


\begin{DoxyCode}{0}
\DoxyCodeLine{var duplexify = require('duplexify')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{var request = function(opts) \{}
\DoxyCodeLine{  var req = http.request(opts)}
\DoxyCodeLine{  var dup = duplexify(req)}
\DoxyCodeLine{  req.on('response', function(res) \{}
\DoxyCodeLine{    dup.setReadable(res)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  return dup}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{var req = request(\{}
\DoxyCodeLine{  method: 'GET',}
\DoxyCodeLine{  host: 'www.google.com',}
\DoxyCodeLine{  port: 80}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{req.end()}
\DoxyCodeLine{req.pipe(process.stdout) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_duplexify_README_autotoc_md8289}{}\doxysection{License}\label{md_sfcode_node_modules_duplexify_README_autotoc_md8289}
MIT\hypertarget{md_sfcode_node_modules_duplexify_README_autotoc_md8290}{}\doxysection{Related}\label{md_sfcode_node_modules_duplexify_README_autotoc_md8290}
{\ttfamily duplexify} is part of the \href{https://github.com/maxogden/mississippi}{\texttt{ mississippi stream utility collection}} which includes more useful stream modules similar to this one. 