Skip to content
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

implement toNodeStream() #644

Merged
merged 4 commits into from
Mar 11, 2018
Merged

implement toNodeStream() #644

merged 4 commits into from
Mar 11, 2018

Conversation

NotBobTheBuilder
Copy link
Contributor

There had been some discussion in #279 - To Nodejs Stream of a toNodeStream method, which I've implemented here.

Inclusion of the stream-browserify package means this should build seamlessly for the purposes of non-node environments and use a shim in those cases.

@NotBobTheBuilder NotBobTheBuilder changed the base branch from master to 2.x March 10, 2018 09:23
package.json Outdated
@@ -43,6 +43,7 @@
"test": "eslint Gruntfile.js lib test/test.js && nodeunit test/test.js"
},
"dependencies": {
"stream-browserify": "^2.0.1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary. According to the node-browserify npm page, we'll get this automatically when using browserify.

lib/index.js Outdated

Stream.prototype.toNodeStream = function () {
var self = this;
return new Readable().wrap(self);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this Readable be marked as objectMode?

@vqvu
Copy link
Collaborator

vqvu commented Mar 10, 2018

Thanks for this.

I've added a few minor comments. Also, can you add some tests. Doesn't need to be in-depth. Just verify that the output is a Readable and it emits the correct values (including errors).

@NotBobTheBuilder
Copy link
Contributor Author

Happy to help! I'll put some tests together now.

With regards to the objectMode question, I was originally testing with a Highland wrapper around a file stream, so this code worked as-is. I now realise that object streams wouldn't work.

My first impression was to add a parameter to toNodeStream for specifying custom options, of which {objectMode: true} could be one. that options object would then just be relayed straight into the Readable constructor.

Does that sound like a reasonable strategy?

@NotBobTheBuilder
Copy link
Contributor Author

For additional context, the reason I didn't just set {objectMode: true} by default is because I was using this method in conjunction with Hapi, which cannot use an object mode stream as a http response

@vqvu
Copy link
Collaborator

vqvu commented Mar 11, 2018

I think having a options object that's passed directly to the Readable constructor would work. Make sure to add a note to the method docs to remind people that they need to set objectMode to true. Highland streams work with objects, so I can see people assuming that we would set objectMode to true by default.

@NotBobTheBuilder
Copy link
Contributor Author

That makes sense, thanks. Given there's room for confusion do you think it would be worthwhile to require a choice (possibly with a shorthand for those who don't need to use the full options object).

E.g:

if (options === true) {
  options = {objectMode: true};
} else if (options === false) { 
  options = {objectMode: false};
} else if (!options || (typeof options.objectMode === 'undefined')) {
  throw new Error('toNodeStream requires explicitly specifying objectMode true or false');
}

@vqvu
Copy link
Collaborator

vqvu commented Mar 11, 2018 via email

@NotBobTheBuilder
Copy link
Contributor Author

Thanks! I've now added tests, docs & the options parameter.

@vqvu
Copy link
Collaborator

vqvu commented Mar 11, 2018

The build fails on older versions of Node. I know that some of these are pretty old, since we haven't updated the list of versions to test in a while, but can you fix the error? It looks like maybe you just need to use the Buffer constructor directly rather than Buffer.from()?

@vqvu
Copy link
Collaborator

vqvu commented Mar 11, 2018

Also, can you add an entry to the CHANGELOG.md under 2.12.0?

lib/index.js Outdated
* @param {Object} options - (optional) [`Readable` constructor](http://nodejs.org/api/stream.html#stream_class_stream_readable) options
* @api public
*
* _(fs.createReadStream('./abc')).toNodeStream(false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toNodeStream(false) and .toNodeStream(true) are not valid code anymore.

@NotBobTheBuilder
Copy link
Contributor Author

Thanks - all updated!

@NotBobTheBuilder
Copy link
Contributor Author

Looks like it still failed in versions 4 & 5, I'll fix that too.

Would it be worth me adding some newer versions also to the Travis build matrix too? It looks like 7 is the most recent specified (& 6 is the most recent executing)

@vqvu
Copy link
Collaborator

vqvu commented Mar 11, 2018

Node 7 is only on the master branch, which is the test matrix for the 3.x beta. That's why it's not running. I think we should add 8 and 9, remove 0.12 and 5, and change "4.0" to "4", but not in this PR. 0.10 is helpful as a kind of proxy for older browsers, so I'd like to leave it in.

Edit: CL -> PR

@vqvu vqvu merged commit 89211d3 into caolan:2.x Mar 11, 2018
@NotBobTheBuilder
Copy link
Contributor Author

Thanks!

@NotBobTheBuilder NotBobTheBuilder deleted the to-node-stream branch March 11, 2018 23:12
@vqvu
Copy link
Collaborator

vqvu commented Mar 12, 2018

I've released this change in 2.12.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants