Skip to content

Commit

Permalink
fix: inline origin resolution, drop original dependency (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored Jun 8, 2022
1 parent aa7a408 commit 9494642
Show file tree
Hide file tree
Showing 3 changed files with 11,685 additions and 923 deletions.
20 changes: 16 additions & 4 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
@@ -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
*/
function getOrigin (url) {
if (typeof url === 'string') url = parse(url)
if (!url.protocol || !url.hostname) return 'null'
return (url.protocol + '//' + url.host).toLowerCase()
}
Loading

0 comments on commit 9494642

Please sign in to comment.