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

fix: inline origin resolution, drop original dependency #281

Merged
merged 1 commit into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/eventsource.js
@@ -1,4 +1,3 @@
var original = require('original')
var parse = require('url').parse
var events = require('events')
var https = require('https')
Expand Down Expand Up @@ -157,8 +156,8 @@ function EventSource (url, eventSourceInitDict) {
_emit('error', new Event('error', {status: res.statusCode, message: res.statusMessage}))
return
}
var prevOrigin = original(url)
var nextOrigin = original(location)
var prevOrigin = getOrigin(url)
var nextOrigin = getOrigin(location)
hasNewOrigin = prevOrigin !== nextOrigin
if (res.statusCode === 307) reconnectUrl = url
url = location
Expand Down Expand Up @@ -280,7 +279,7 @@ function EventSource (url, eventSourceInitDict) {
_emit(type, new MessageEvent(type, {
data: data.slice(0, -1), // remove trailing newline
lastEventId: lastEventId,
origin: original(url)
origin: getOrigin(url)
}))
data = ''
}
Expand Down Expand Up @@ -471,3 +470,16 @@ function removeUnsafeHeaders (headers) {

return safe
}

/**
* Transform an URL to a valid origin value.
*
* @param {String|Object} url URL to transform to it's origin.
* @returns {String} The origin.
* @api private
Copy link

Choose a reason for hiding this comment

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

should leave @link https://github.com/unshiftio/original/blob/507b362269c0ad4405d095aedc227c40aecaf68a/index.js as credit to orignal code.

*/
function getOrigin (url) {
if (typeof url === 'string') url = parse(url)
if (!url.protocol || !url.hostname) return 'null'
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO 'null' is a little confusing. It could be perceived as a bug? Maybe we should return 'invalid url'?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is confusing, but it is also in accordance with RFC6464, so I think I will leave it like this.

return (url.protocol + '//' + url.host).toLowerCase()
}