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

Improve HTTP2 documentation. #16366

Closed
wants to merge 4 commits into from
Closed

Improve HTTP2 documentation. #16366

wants to merge 4 commits into from

Conversation

jsha
Copy link
Contributor

@jsha jsha commented Oct 21, 2017

Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example of https.
Add error handling, TLS, more efficient Buffer usage, and header printing to
client example.

Fixes #16345 /cc @apapirovski

Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example of `https`.
Add error handling, TLS, more efficient Buffer usage, and header printing to
client example.
@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. http2 Issues or PRs related to the http2 subsystem. labels Oct 21, 2017
Copy link
Member

@apapirovski apapirovski left a comment

Choose a reason for hiding this comment

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

Just some nits. Thanks for taking this on! 👍

doc/api/http2.md Outdated
req.on('data', (d) => data.push(d));
req.on('end', () => {
console.log();
console.log(Buffer.concat(data).toString('utf8'));
Copy link
Member

Choose a reason for hiding this comment

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

I feel like the example with setEncoding might've been a bit clearer but I'm ok with either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with setEncoding is that it turns each chunk into a string, meaning Buffer.concat won't work. We could join strings with Array.prototype.join, but to me, accumulating chunks in buffers is more natural. It's also more compatible with performing other transforms, like inflate or gunzip, on the resulting data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Making it more concrete: I actually spent a while fighting a subtle bug with this, trying to inflate data that had been decoded as if it were utf-8)

Copy link
Member

Choose a reason for hiding this comment

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

I kind of agree with @apapirovski here – you’re right, if you don’t want to receive text data, you shouldn’t use setEncoding() at all, but Buffer.concat() has the downside of having double the memory requirement, because the concatenated buffer needs to be a fresh allocation too…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding was that doing += on a string in a for loop has bad performance: it potentially causes an allocation each time plus O(n) work to copy into the newly allocated space. Does Node specially detect and optimize for this case, e.g. by predicting the final length of the string and preallocating for it?

Copy link
Member

Choose a reason for hiding this comment

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

Been a while since I read about it but pretty sure V8 has a special way of dealing with string concatenation. I believe it only gets truly concatenated when it actually gets used so if you're not also reading (or otherwise using) the string within your for loop then it'll always outperform arrays.

But anyway, it feels like this discussion is heading in the wrong direction. The main concern here should be the ease of understanding for somebody new to Node. To me, the setEncoding example is the only one that doesn't rely on understanding yet another part of Node and requires less lines to get the same point across.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it only gets truly concatenated when it actually gets used so if you're not also reading (or otherwise using) the string within your for loop then it'll always outperform arrays.

Aha, I did not know that! Thanks for updating my knowledge. I also checked the http docs, and they use the string concatenation style, so I've reverted to that.

The main concern here should be the ease of understanding for somebody new to Node.

True, although since example code so often forms the kernel of a new program, it's important not to recommend anti-patterns that a newbie might not know enough to see. Of course, in this case it turned out to not be an anti-pattern in modern environments, which is great!

doc/api/http2.md Outdated

const client = http2.connect('http://localhost:80');
var http2 = require('http2');
const client = http2.connect('https://localhost');
Copy link
Member

Choose a reason for hiding this comment

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

Since the default example is using a createSecureServer, maybe this could mention that one might need either rejectUnauthorized: false or to pass the ca in order for self-signed certs to work.

Also, it should probably connect on port 8443 to match the example above.

Also connect to port 8443 in client, and add require('fs')
doc/api/http2.md Outdated
To generate the certificate and key for this example, run:

```bash
openssl req -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -x509 -keyout localhost-privkey.pem -out localhost-cert.pem
Copy link
Member

Choose a reason for hiding this comment

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

tiny style nit: Can you wrap at 80 characters? You can use backslashes to escape end-of-lines in bash, i.e.

fooprog arg1 arg2 arg3 \
    arg4 arg5

is the same as fooprog arg1 arg2 arg3 arg4 arg5

doc/api/http2.md Outdated
req.on('data', (d) => data.push(d));
req.on('end', () => {
console.log();
console.log(Buffer.concat(data).toString('utf8'));
Copy link
Member

Choose a reason for hiding this comment

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

I kind of agree with @apapirovski here – you’re right, if you don’t want to receive text data, you shouldn’t use setEncoding() at all, but Buffer.concat() has the downside of having double the memory requirement, because the concatenated buffer needs to be a fresh allocation too…

doc/api/http2.md Outdated

const client = http2.connect('http://localhost:80');
var http2 = require('http2');
var fs = require('fs');
Copy link
Member

Choose a reason for hiding this comment

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

nit: const http2 = require('http2'); and const fs = require('fs');

doc/api/http2.md Outdated
console.log(headers[':status']);
console.log(headers['date']);
req.on('response', (headers, flags) => {
for (var name in headers) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: for (const name in headers) {

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

LGTM with the requested nits addressed.

doc/api/http2.md Outdated
req.on('data', (d) => data.push(d));
req.setEncoding('utf8');
let rawData = '';
req.on('data', (chunk) => { rawData += chunk; });
req.on('end', () => {
console.log();
Copy link
Member

@apapirovski apapirovski Oct 23, 2017

Choose a reason for hiding this comment

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

Could we remove this extra console.log — I think it might confuse people as to why it's there.

(The rest is good to go, I think.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to have a newline between the headers and the body, since that will make it clearer which is which. Would it be better to put the newline as part of the next line, e.g. console.log("\n" + ...)?

@apapirovski
Copy link
Member

apapirovski commented Oct 25, 2017

Landed in 3621889 (with fixes for the linter)

Thanks @jsha!

apapirovski pushed a commit that referenced this pull request Oct 25, 2017
Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example
of `https`. Add error handling, TLS, more efficient Buffer usage,
and header printing to client example.

PR-URL: #16366
Fixes: #16345
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
addaleax pushed a commit to ayojs/ayo that referenced this pull request Oct 26, 2017
Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example
of `https`. Add error handling, TLS, more efficient Buffer usage,
and header printing to client example.

PR-URL: nodejs/node#16366
Fixes: nodejs/node#16345
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
gibfahn pushed a commit that referenced this pull request Oct 30, 2017
Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example
of `https`. Add error handling, TLS, more efficient Buffer usage,
and header printing to client example.

PR-URL: #16366
Fixes: #16345
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
gibfahn pushed a commit that referenced this pull request Oct 30, 2017
Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example
of `https`. Add error handling, TLS, more efficient Buffer usage,
and header printing to client example.

PR-URL: #16366
Fixes: #16345
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
gibfahn pushed a commit that referenced this pull request Oct 31, 2017
Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example
of `https`. Add error handling, TLS, more efficient Buffer usage,
and header printing to client example.

PR-URL: #16366
Fixes: #16345
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
@gibfahn gibfahn mentioned this pull request Oct 31, 2017
addaleax pushed a commit to ayojs/ayo that referenced this pull request Dec 7, 2017
Provide section headings for server-side and client side examples.
Add error handling and TLS to server-side example, following example
of `https`. Add error handling, TLS, more efficient Buffer usage,
and header printing to client example.

PR-URL: nodejs/node#16366
Fixes: nodejs/node#16345
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. http2 Issues or PRs related to the http2 subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants