5

I am would like to be able to filter the output. However, I am having a issue converting from buffer to string. console.log(JSON.stringify(obj.toString())); keeps giving me [object Object] which I can not use. How can I convert the buffer to string so I can filter out the contents to stdout?

//inject 'bower and javascript' files or just 'javascript' files
function injectStream(sourceStream, filesStream) {
    sourceStream
        .pipe(injector(filesStream, { ignorePath: 'app', addRootSlash: false }))
        .pipe(gulp.dest(INDEX_PATH_PARENT))
        .pipe(through2.obj(function(obj, enc, next) {
            console.log(JSON.stringify(obj.toString()));
            this.push(obj.contents);
            next();
    })).pipe(process.stdout)
}

1 Answer 1

4

through2.obj makes an object stream (or a stream in object mode). Through an object stream objects flow, not buffers. What you get is not a buffer, but an object obj. That is why its toString method gives [object Object]. Perhaps what you are looking for is in obj.contents?

Sign up to request clarification or add additional context in comments.

6 Comments

Strangely enough console.log(obj.contents) gives me <Buffer 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 65 6e 22 3e 0a 09 3c 68 65 61 64 3e 0a 09 09 3c 6d 65 74 61 20 63 68 61 72 73 65 74 3d 22 75 74 66 2d 38 22 20 2f 3e ...>
And calling toString on the buffer?
console.log(obj.contents.toString()); works. However, it gives me the whole contents at once. I was hoping for line by line since I thought it was iterating with next().
You should be aware of some issues with node's streams. There's also substack's stream handbook for an introduction.
It might be that there is a ditinction between the objects own properties and those that it inherits from its prototype. Check out obj.prototype or obj.__proto__
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.