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

doc: document res.connection and res.socket #13617

Closed
wants to merge 1 commit into from

Conversation

JustinBeckwith
Copy link
Contributor

@JustinBeckwith JustinBeckwith commented Jun 11, 2017

Adds documentation for the connection and socket properties
available on the http.serverResponse object.

Fixes: #12617

Checklist
Affected core subsystem(s)
  • doc

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem. labels Jun 11, 2017
Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

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

LGTM. (As always, there's room for some more information to be added, but this is certainly better than not having it documented at all.)

@Trott
Copy link
Member

Trott commented Jun 11, 2017

I wonder if it might be possible to indicate a use case where a user might want to access these properties. Otherwise, readers may wonder about the statements that users usually won't want to access them.

Trott
Trott previously approved these changes Jun 11, 2017
@mscdex
Copy link
Contributor

mscdex commented Jun 11, 2017

I think we should just document one of the two. At some point it'd be nice to remove one of them to have less duplication.

Since core itself uses .socket much more than .connection, I'd say we document .socket.

@JustinBeckwith
Copy link
Contributor Author

That's a great point. I was mostly just trying to address #12617, but to be honest I am having trouble thinking of an example where accessing the raw socket is required or advisable. Followed up on the issue to learn more.

@Trott Trott dismissed their stale review June 11, 2017 21:09

dismissing my review until mscdex suggestion sorted; I wouldn't stop anyone else from approving if they like this as-is, though

@JustinBeckwith
Copy link
Contributor Author

JustinBeckwith commented Jun 12, 2017

I went ahead and added the same documentation for request.socket and request.connection, since their use is probably more common than the same properties on the response.

@mscdex I agree that having both of these isn't ideal. I did some searching, and mostly found folks using req.connection as opposed to req.socket, even in the express docs:

https://www.google.com/search?q=%22req.connection%22&oq=%22req.connection&aqs=chrome.0.69i59j0j69i57j0l3.3608j0j7&sourceid=chrome&ie=UTF-8

For now, I went ahead and documented both, with connection just linking out to socket. I'm happy to file a follow up issue to make the API breaking part of the change. Thoughts?

@JustinBeckwith
Copy link
Contributor Author

Also, I added an example :)

doc/api/http.md Outdated
added: v0.3.0
-->

* `connection` {net.Socket}
Copy link
Member

@lpinca lpinca Jun 12, 2017

Choose a reason for hiding this comment

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

Nit: I think the name (`connection`) is not needed here as this is property not a function argument.

doc/api/http.md Outdated
```js
const http = require('http');
http.createServer((req, res) => {
const clientIp = req.socket.remoteAddress;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can you please use two spaces for indentation?

doc/api/http.md Outdated
```js
const http = require('http');
http.createServer((req, res) => {
const clientIp = res.socket.remoteAddress;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

doc/api/http.md Outdated

* `socket` {net.Socket}

Reference to the underlying socket. `socket` is an object of type
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to duplicate the type in a separate sentence if it is already specified above.

@JustinBeckwith
Copy link
Contributor Author

Thanks for the awesome feedback folks. All addressed.

Copy link
Contributor

@aqrln aqrln 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 a couple of tiny nits. Thanks!

doc/api/http.md Outdated

Reference to the underlying socket. Usually users will not want to access
this property. In particular, the socket will not emit `'readable'` events
because of how the protocol parser attaches to the socket. After `response.end()`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please wrap this line and the next one it at 80 characters?

doc/api/http.md Outdated

Reference to the underlying socket. Usually users will not want to access
this property. In particular, the socket will not emit `'readable'` events
because of how the protocol parser attaches to the socket. After `response.end()`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

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 nits addressed

@silverwind
Copy link
Contributor

I think worthy cases to document would be req.connection.remoteAddress and req.connection.remotePort, which hold the remote adress and port, something which is useful for logging and access control.

@JustinBeckwith
Copy link
Contributor Author

Addressed all the things and rebased into one commit!

doc/api/http.md Outdated
@@ -497,6 +497,15 @@ If a request has been aborted, this value is the time when the request was
aborted, in milliseconds since 1 January 1970 00:00:00 UTC.

### request.end([data[, encoding]][, callback])
### request.connection
Copy link
Member

Choose a reason for hiding this comment

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

Are these supposed to be directly after each other?

doc/api/http.md Outdated
const http = require('http');
http.createServer((req, res) => {
const clientIp = req.socket.remoteAddress;
res.end(`Your IP is ${clientIp}`);
Copy link
Contributor

@silverwind silverwind Jun 18, 2017

Choose a reason for hiding this comment

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

Sorry to nitpick, but "IP address" would be more correct, as IP is just the protocol. Also, I'd add a second print for the port. Same goes for the res example below.

Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

Fixes: nodejs#12617
@JustinBeckwith
Copy link
Contributor Author

One more try 😂

@silverwind
Copy link
Contributor

Thanks! Landed in d3d66a5.

I also took the liberty to change IP to IP address 😉

@silverwind silverwind closed this Jun 19, 2017
silverwind pushed a commit that referenced this pull request Jun 19, 2017
Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

PR-URL: #13617
Fixes: #12617
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
@refack
Copy link
Contributor

refack commented Jun 19, 2017

@silverwind code does not lint 😢 Probably new lint rules added in #13645

@refack
Copy link
Contributor

refack commented Jun 19, 2017

@silverwind code does not lint 😢 Probably new lint rules added in #13645

P.S. we lint the JS in the snippets

@silverwind
Copy link
Contributor

Argh, I forgot that we lint the docs.

I was able to force-push a fix as the commit was still on HEAD so this has now landed on 24ecc33.

silverwind pushed a commit that referenced this pull request Jun 19, 2017
Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

PR-URL: #13617
Fixes: #12617
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
@jasnell
Copy link
Member

jasnell commented Jun 19, 2017

Ugh. Per the collab guidelines: However, you are only allowed to force push to any Node.js branch within 10 minutes from your original push. If someone else pushes to the branch or the 10 minute period passes, consider the commit final.

This force push happened 2 hours later. Please do not do that as it causes quite a bit of hassle for other devs.

@silverwind
Copy link
Contributor

Yes, I'm aware of that rule. Just thought it's easier to backport as one commit later. What real harm does it cause when the replaced commit is still on HEAD?

@addaleax
Copy link
Member

What real harm does it cause when the replaced commit is still on HEAD?

@silverwind The question is not really where the commit is in the history; the problem is more that people who update their clones from upstream now have a bogus master after force-pushing. For example, I think new PRs based on that old master would appear to include the commits that were force-pushed out of the way.

@jasnell
Copy link
Member

jasnell commented Jun 19, 2017

I have four local branches and one open PR branch that I had rebased off master when I started today that I ended up having to go back and correct after the force push. Forcing outside of the 10 minute window ends up meaning other collaborators end up having to do extra work they really shouldn't have to do.

addaleax pushed a commit that referenced this pull request Jun 24, 2017
Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

PR-URL: #13617
Fixes: #12617
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
@addaleax addaleax mentioned this pull request Jun 24, 2017
addaleax pushed a commit that referenced this pull request Jun 29, 2017
Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

PR-URL: #13617
Fixes: #12617
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
addaleax pushed a commit that referenced this pull request Jul 11, 2017
Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

PR-URL: #13617
Fixes: #12617
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
addaleax pushed a commit that referenced this pull request Jul 18, 2017
Adds documentation and samples for the `connection` and
`socket` properties available on the `http.serverResponse`
and `http.clientRequest` objects.

PR-URL: #13617
Fixes: #12617
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
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. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.