Hello
I try combine few streams in one and then parse its in separated streams 
And now for me not clear how to pause of transform.
When i call method "done" (indicate stream continue transform) in Readable 
stream - throw exception "Error: no writecb in Transform class".

Thanks for help!

var fs = require('fs');
var Readable = require('stream').Readable;
var Transform = require('stream').Transform;

//function of streams separate
function split(options) {
    var idx = 0;
    var streams = [];

    function Divider(options) {
        Transform.call(this, options);
    }
    Divider.prototype = Object.create(
        Transform.prototype, { constructor: { value: Divider }});

    Divider.prototype._transform = function(chunk, encoding, done) {
        var length = chunk.length,
            split = -1;

        for (var i = 0; i < length; i++) {
            if (chunk[i] === 10) { // '\n'
                if ((i + 2) < length && chunk[i+1] === 10 && chunk[i+2] == 
'93') {
                    split = i;
                    break;
                }
            }
        }

        var rs;
        if (!streams[idx]) {
            rs = new Readable;
            rs._read = function() {
                if (chunk === null)
                    return rs.push('');

                if (split == -1) {
                    rs.push(chunk);
                } else {
                    rs.push(chunk.slice(0, split));
                    rs.push(null);
                }
                done();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Throw Error Error: 
no writecb in Transform class
            };

            this.emit('stream', rs, idx);
            streams.push(rs);
        } else {
            rs = streams[idx];
        }
        rs.read(0);
        if (split != -1) {
            idx++;
            this.unshift(chunk.slice(split + 2));
        }
    };
    return new Divider(options);
}


//function for combine few streams in one
function combine(streams) {
    var idx = 0;
    var rs = new Readable();

    rs._read = function(size) {
        var chunk = streams[idx].read(size);
        if (chunk === null)
            return rs.push('');
        return rs.push(chunk);
    };

    (function read() {
        var stream = streams[idx];
        stream.on('readable', function() {
            rs.read(0);
        });
        stream.on('end', function() {
            idx++;
            if (idx == streams.length) {
                rs.push("\n\n]");
                rs.push(null);
                return;
            }
            rs.push("\n\n]");
            return read();
        });
    })();
    return rs;
}


combine([fs.createReadStream('./files/img1.jpg'), 
fs.createReadStream('./files/img2.jpg')])
    .pipe(split()).on('stream', function(rs, index) {
        rs.pipe(fs.createWriteStream('./files/after' + index));
    });

-- 
-- 
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