How to destroy a thread in nodejs?

I'm trying to accomplish a fairly easy task, but I'm a little confused and stuck with using zlib in nodejs. I am creating functionality that includes downloading a file from aws S3 that is gziped, unzipping and reading it line by line. I want to accomplish all this using threads, as I believe this is possible in nodejs.

Here is my current code base:

//downloading zipped file from aws s3: //params are configured correctly to access my aws s3 bucket and file s3.getObject(params, function(err, data) { if (err) { console.log(err); } else { //trying to unzip received stream: //data.Body is a buffer from s3 zlib.gunzip(data.Body, function(err, unzippedStream) { if (err) { console.log(err); } else { //reading line by line unzziped stream: var lineReader = readline.createInterface({ input: unzippedStream }); lineReader.on('line', function(lines) { console.log(lines); }); } }); } }); 

I get an error message:

  readline.js:113 input.on('data', ondata); ^ TypeError: input.on is not a function 

I believe that the problem may lie in unpacking the process, but I'm not too sure what is wrong, any help would be appreciated.

+3
source share
1 answer

I don't have an S3 account for testing, but reading the documents suggests that s3.getObject() can return a stream, in which case I think this might work:

 var lineReader = readline.createInterface({ input: s3.getObject(params).pipe(zlib.createGunzip()) }); lineReader.on('line', function(lines) { console.log(lines); }); 

EDIT : It looks like the API may have changed, and now you need to instantiate the stream object manually before you can pass it through anything else:

 s3.getObject(params).createReadStream().pipe(...) 
+2
source

All Articles