New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toString() failed #110

Closed
dlarr opened this Issue May 3, 2018 · 1 comment

Comments

Projects
None yet
1 participant
@dlarr

dlarr commented May 3, 2018

Hello,

I am trying to use this library to read from a buffer line by line, below the code I use :

const es = require('event-stream');
const stream = require('stream');

let bufferStream = new stream.PassThrough();
bufferStream.end(myBuffer);

bufferStream
	.pipe(es.split())
	.pipe(es.mapSync(function(line){

			// pause the readstream
			bufferStream.pause();

			// DO SOME PROCESSING OF THIS LINE

			// resume the readstream, possibly from a callback
			bufferStream.resume();

		}).on('error', function(err){
			console.log('Error while reading file.' + err);
		}).on('end', function(){
			console.log('end event !');
		}).on('close', function(){
			console.log('close event !');
		})
	);

It seems awsome, but the problem is that I get an error - feels like a noob error :(

buffer.js:556
      if (encoding === 'utf8') return buf.utf8Slice(start, end);
                                          ^

Error: "toString()" failed
    at stringSlice (buffer.js:556:43)
    at Buffer.toString (buffer.js:629:10)
    at StringDecoder.utf8Text [as text] (string_decoder.js:201:16)
    at StringDecoder.write (string_decoder.js:85:46)
    at Stream.<anonymous> (...\node_modules\split\index.js:53:24)
    at Stream.stream.write (...\node_modules\through\index.js:26:11)
    at PassThrough.ondata (_stream_readable.js:628:20)
    at emitOne (events.js:115:13)
    at PassThrough.emit (events.js:210:7)
    at PassThrough.Readable.read (_stream_readable.js:464:10)
    at flow (_stream_readable.js:835:34)
    at resume_ (_stream_readable.js:817:3)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Do you know what's happening ?

Thank for the lib anyway !

Regards.

@dlarr

This comment has been minimized.

dlarr commented Jun 11, 2018

Hello, it has nothing to do with your lib. Nevertheless, I found a way to get rid of this error :
Not using this lib anymore sorry :(

class Splitter extends Transform {
    constructor(options){
        super(options);
        this.splitSize = options.splitSize;
        this.buffer = Buffer.alloc(0);
        this.continueThis = true;
    }
    stopIt() {
        this.continueThis = false;
    }

    _transform(chunk, encoding, cb){

        this.buffer = Buffer.concat([this.buffer, chunk]);

        while ((this.buffer.length > this.splitSize || this.buffer.length === 1) && this.continueThis){
            try {
                let chunk = this.buffer.slice(0, this.splitSize);

                this.push(chunk);
                this.buffer = this.buffer.slice(this.splitSize);
                if (this.buffer[0] === 26){
                    console.log('EOF : ' + this.buffer[0]);
                }
            } catch (err) {
                console.log('ERR OCCURED => ', err);
                break;
            }
        }
        console.log('WHILE FINISHED');
        cb();
    }
}

The to read your stuff simply do

let bufferStream = new stream.PassThrough();
bufferStream.end(hugeBuffer);
let splitter = new Splitter({splitSize : 170}); // In my case I have 170 length lines, so I want to process them line by line
let lineNr = 0;
bufferStream
      .pipe(splitter)
      .on('data', async function(line){

          line = line.toString().trim();

          splitter.pause(); // pause stream so you can perform long time processing with await
          lineNr++;
          
         if (lineNr === 1){
              // DO stuff with 1st line
              
         } else {
              splitter.stopIt(); // Break the stream and stop reading so we just read 1st line
         }

         splitter.resume() // resumestream so you can process next chunk
    }).on('error', function(err){
            console.log('Error while reading file.' + err);
            // whatever
     }).on('end', async function(){
           console.log('end event');

           // Stream has ended, do whatever...
           
    });

@dlarr dlarr closed this Jun 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment