-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Use node 7+ WHATWG parser for hostname, fixes #1002 #1004
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* Module dependencies. | ||
*/ | ||
|
||
const URL = require('url').URL; | ||
const net = require('net'); | ||
const contentType = require('content-type'); | ||
const stringify = require('url').format; | ||
|
@@ -244,9 +245,28 @@ module.exports = { | |
get hostname() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs some adjusting - the code path is weird and for some reason I'm not using |
||
const host = this.host; | ||
if (!host) return ''; | ||
if ('[' == host[0]) return this.URL.hostname; // IPv6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return host.split(':')[0]; | ||
}, | ||
|
||
/** | ||
* Get WHATWG parsed URL. | ||
* Lazily memoized. | ||
* | ||
* @return {URL} | ||
* @api public | ||
*/ | ||
|
||
get URL() { | ||
if (!this.memoizedURL) { | ||
const protocol = this.protocol; | ||
const host = this.host; | ||
const originalUrl = this.originalUrl || ''; // avoid undefined in template string | ||
this.memoizedURL = new URL(`${protocol}://${host}${originalUrl}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. URL will throw TypeError if something's off. Maybe wrap this in a try-catch and memoize empty object so that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A try-catch shouldn't deopt since node 7.x (v8/v8@9aac80f) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
+1 |
||
} | ||
return this.memoizedURL; | ||
}, | ||
|
||
/** | ||
* Check if the request is fresh, aka | ||
* Last-Modified and/or the ETag | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought about destructing here (
const { format: stringify, URL } = require('url')
),though I'm not seeing it elsewhere so assumed it was taboo in koajs/koa.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fl0w i think we didn't add it for node v4 support previously