1. You shouldn't override .write. if you do, you're all lost. Writable
handles backpressure logic etc. for you, if you override .write, all this
logic isn't used then. Both things are acceptable in your code, because you
simple do nothing with the data, but just printing. the _write is
considered private and is called by the Writable methods in some point of
the processing the data. this is where you can do with the data what you
want. thats where you get the encoding too.
2. In your code you don't see any decoding because oyu just print the
chunk. if the chunk is a buffer it's toString method decodes the bytes with
the default encoding (utf8) If you want to see a difference add this line:
console.log("is buffer? " + Buffer.isBuffer(chunk))
Am Donnerstag, 5. September 2013 21:57:43 UTC+2 schrieb [email protected]:
>
> Hi,
> I am trying to create a class that will be a writable stream. I got some
> unexpected behavior and was wondering if anyone might know why.
>
> -- I have two files, master.js and worker.js.
> -- Worker is a writable stream.
> -- Master reads from a Unix socket and pipes data to Worker.
>
> Following the documentation I override the _write(chunk, encoding,
> callback) method.
>
> *First Question*
> I initially misunderstood the documentation and I created a Worker.write()
> method instead of Worker._write().
>
> If I comment out the Worker._write() method the Sock.pipe(Worker) writes
> data to Worker.write(). But it does not send the encoding, or the callback
> arguments.
>
> If I comment out the Worker.write() method then Sock.pipe(Worker) writes
> data to Worker._write() but does send the encoding and callback arguments.
>
> Why are both Worker.write() and Worker._write() both acceptable and why do
> they accept different arguments.
>
> *Second Question
> *The docs say that when calling the Writeable constructor it will accept
> and options object as an argument. One of the options is "decodeStrings "
> which apparently defaults to true. However I dont see it getting decoded
> even I pass in an object like
>
> {"decodeStrings":true}
>
> Why is that?
>
>
>
> Here is some code for reference. Thanks for any insight.
>
> *Master.js*
> var Worker = require('./worker');
> var Net = require('net');
> var Byline = require('byline');
> var Fs = require('fs');
>
> var sockpath = '/tmp/steve.sock';
>
> if (Fs.existsSync(sockpath)) {
> Fs.unlinkSync(sockpath);
> console.log({
> 'log': 'info',
> 'msg': 'Found and removed sock file',
> 'sockpath': sockpath
> });
> }
>
>
> var W = new Worker();
>
> var Server = Net.createServer({allowHalfOpen: false});
>
> Server.on('error', function(err) {
> console.log({
> 'log': 'info',
> 'msg': 'Error creating socket',
> 'err': err
> });
> });
>
> Server.on('close', function(err) {
> console.log({
> 'log': 'info',
> 'msg': 'Server has closed the socket',
> 'err': err
> });
> });
>
> Server.on('listening', function() {
> console.log({
> 'log': 'info',
> 'msg': 'Server is listening'
> });
> });
>
> Server.on('connection', function(sock) {
> console.log({
> 'log': 'info',
> 'msg': 'Server has been connected to',
> });
>
> sock.setEncoding('utf8');
>
> sock.on('error', function(err) {
> console.log({
> 'log': 'info',
> 'msg': 'socket error',
> 'err': err
> });
> });
>
> // Wrap the socket stream in byline so that data events will
> // return exactly one line at a time.
> sock = Byline.createStream(sock);
> sock.pipe(W)
> /*
> sock.on('data', function(data) {
> console.log(data)
> });*/
> });
>
> /*
> * Finally we listen to the UNIX socket for data
> */
> Server.listen(sockpath);
>
>
>
> *Worker.js
> *var Writable = require('stream').Writable;
> var Inherits = require('util').inherits;
>
> Inherits(Worker, Writable);
>
> function Worker(opt){
> Writable.call(this, opt);
> };
>
> Worker.prototype._write = function(chunk, encoding, callback){
> // do something
> console.log(chunk);
> console.log(encoding);
> callback();
> };
> /*
> Worker.prototype.write = function(chunk){
> // do something
> console.log(chunk);
> };
> */
> module.exports = Worker;*
> *
>
--
--
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.