On Jun 13, 2013, at 2:45 PM, Isaac Schlueter <[email protected]> wrote:
> All the arguments are optional.  Only the order matters.  So,
> actually, you can just do:
> 
> stream.end(function() { 'finished' })
> 
> That function is just added as a listener on the "finish" event.
> 


Thank you Isaac. I noticed I have a typo below (I wrote “final” instead of 
“finish”). The above is good to know.


> If you find a bug in the `bytesWritten` field, then please provide a test 
> case.



With regard to my code fragment below, it turns out bytesWritten is in fact the 
correct value of the bytes on disk! So something else is going on:

I see:

Sending x1 bytes to be written…
Sending x2 bytes to be written…
Sending x3 bytes to be written…

Each of these log messages is followed up by a stream.write() so the data is 
being pushed to the buffer. x1 + x2 + x3 equals the bytes sent from the client. 
So far so good.

I call stream.end() (at some point) and the callback/listener prints that Y 
bytes were written to disk. And indeed the size of the file on disk is Y bytes.

However, Y < x1 + x2 + x3.

The only way I can explain this is that stream.end() causes some part of 
pending stream.write()s to be discarded and not written to disk.

Or I am doing something very wrong.

I will write a test case.

        —ravi



> 
> On Thu, Jun 13, 2013 at 10:40 AM, // ravi <[email protected]> wrote:
>> 
>> Howdy all,
>> 
>> humour me another round. My question today:
>> 
>> The writable.end() call takes three arguments: (1) chunk to write if any, 
>> (2) encoding, (3) callback for when all writing is done.
>> 
>> Assuming there is nothing left to write, I am hoping it is okay to pass 
>> null, null to the first two arguments.
>> 
>> And I am guessing it is also safe to assume that the end() will be queued up 
>> after any pending write()s.
>> 
>> With regard to the callback argument, the docs say:
>> 
>>> callback Function Optional. Called when the final chunk is successfully 
>>> written.
>> 
>> My question is what does “successfully written” really mean? In particular, 
>> can the callback rely on fs’s file.bytesWritten to have the correct value? 
>> The doc for the latter says:
>> 
>>> The number of bytes written so far. Does not include data that is still 
>>> queued for writing.
>> 
>> These two bits suggest the below is safe to do:
>> 
>> req.on
>> (
>>    ‘data’,
>>    function(chunk)
>>    {
>>         console.log(“Sending “ + chunk.length + “ bytes to be written.”);
>>         write_stream.write(chunk);
>>    }
>> );
>> 
>> req.on
>> (
>>    ‘end’,
>>    write_stream.end
>>    (
>>         null, null,
>>         function()
>>         {
>>             console.log(“Yay, the file is written. Size is “ + 
>> write_stream.bytesWritten);
>>         }
>>    );
>> );
>> 
>> But in my experience, it does not seem to be. If I send 1000 bytes, the 
>> first log prints, say, 4 times with 250 byte writes, as expected. But the 
>> bytesWritten value is much smaller than the 1000 bytes sent (all numbers are 
>> made up).
>> 
>> I can post more fleshed out sample code if/once you confirm my assumptions 
>> are correct. I realise there are other ways to do this (e.g: pipe, or listen 
>> to the ‘final’ event for the writeable, etc), but I am curious if the above 
>> is even expected behaviour.
>> 
>> Thank you,
>> 
>>        —ravi
>> 
>> 
>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines: 
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>> 
>> ---
>> You received this message because you are subscribed to the Google Groups 
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>> 
>> 
> 
> -- 
> -- 
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
> 
> --- 
> You received this message because you are subscribed to the Google Groups 
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to