Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion How to write stream chunk by chunk with callbacks

Received: by 10.59.7.167 with SMTP id dd7mr1577564ved.26.1350148990467;
        Sat, 13 Oct 2012 10:23:10 -0700 (PDT)
X-BeenThere: nodejs@googlegroups.com
Received: by 10.52.20.7 with SMTP id j7ls2826289vde.0.gmail; Sat, 13 Oct 2012
 10:23:01 -0700 (PDT)
Received: by 10.52.93.229 with SMTP id cx5mr1281786vdb.19.1350148981182;
        Sat, 13 Oct 2012 10:23:01 -0700 (PDT)
Date: Sat, 13 Oct 2012 10:23:00 -0700 (PDT)
From: Bruno Jouhier <bjouh...@gmail.com>
To: nodejs@googlegroups.com
Message-Id: <c09f1ba7-b9b3-4dd6-bcdc-f44a1b9148ac@googlegroups.com>
In-Reply-To: <42eeb107-5160-4b41-9c77-3e3dc781585e@googlegroups.com>
References: <42eeb107-5160-4b41-9c77-3e3dc781585e@googlegroups.com>
Subject: Re: How to write stream chunk by chunk with callbacks
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1291_10690087.1350148980637"

------=_Part_1291_10690087.1350148980637
Content-Type: multipart/alternative; 
	boundary="----=_Part_1292_17875182.1350148980637"

------=_Part_1292_17875182.1350148980637
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

I wrote a post about plain callback APIs for streams: 
http://bjouhier.wordpress.com/2012/07/04/node-js-stream-api-events-or-callbacks/


On Saturday, October 13, 2012 11:17:13 AM UTC+2, Alexey Petrushin wrote:
>
> I don't quite understand how steam pause/resume works, or more exactly - 
> how to use it in simple manner. It's necessary to use it in situations when 
> the read stream produces data faster than the write stream can consume. 
>
> I need to write custom stream implementation and writing it with proper 
> handling of `pause/resume` functionality seems not a very easy task.
>
> Plain callbacks seems simpler to me, can streams be somehow wrapped into a 
> code like that ( code with highlighting https://gist.github.com/3883920 ) 
> ?
>
>     var copy = function(inputStream, outputStream, callback){
>       var copyNextChunk = function(){
>         inputStream.read(fuction(err, chunk){    
>           if(err) return callback(err)
>           // When chunk == null there's no data, copying is finished.
>           if(!chunk) return callback()
>           outputStream.write(chunk, function(err){
>             // Callback called only when chunk of data 
>             // delivered to the recipient and
>             // we can send another one.
>             if(err) return callback(err)
>             copyNextChunk()
>           })  
>         })
>       }
>     }
>

------=_Part_1292_17875182.1350148980637
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

I wrote a post about plain callback APIs for streams: http://bjouhier.wordp=
ress.com/2012/07/04/node-js-stream-api-events-or-callbacks/<br><br><br>On S=
aturday, October 13, 2012 11:17:13 AM UTC+2, Alexey Petrushin wrote:<blockq=
uote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-lef=
t: 1px #ccc solid;padding-left: 1ex;"><div>I don't quite understand how ste=
am pause/resume works, or more exactly - how to use it in&nbsp;simple manne=
r. It's necessary to use it in situations when the read stream produces dat=
a faster than the write stream can&nbsp;consume.&nbsp;</div><div><br></div>=
<div>I need to write custom stream implementation and writing it with prope=
r handling of `pause/resume` functionality seems not a very easy task.</div=
><div><br></div><div>Plain callbacks seems simpler to me, can streams be so=
mehow wrapped into a code like that ( code with highlighting <a href=3D"htt=
ps://gist.github.com/3883920" target=3D"_blank">https://gist.github.com/<wb=
r>3883920</a> ) ?</div><div><br></div><div>&nbsp; &nbsp; var copy =3D funct=
ion(inputStream, outputStream, callback){</div><div>&nbsp; &nbsp; &nbsp; va=
r copyNextChunk =3D function(){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; input=
Stream.read(fuction(err, chunk){ &nbsp; &nbsp;</div><div>&nbsp; &nbsp; &nbs=
p; &nbsp; &nbsp; if(err) return callback(err)</div><div>&nbsp; &nbsp; &nbsp=
; &nbsp; &nbsp; // When chunk =3D=3D null there's no data, copying is finis=
hed.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!chunk) return callbac=
k()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputStream.write(chunk, =
function(err){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Callb=
ack called only when chunk of data&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; // delivered to the recipient and</div><div>&nbsp; &nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp; // we can send another one.</div><div>&nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(err) return callback(err)</div><div>&n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copyNextChunk()</div><div>&nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; }) &nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }=
)</div><div>&nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div></blockqu=
ote>
------=_Part_1292_17875182.1350148980637--

------=_Part_1291_10690087.1350148980637--