Skip to content

Commit

Permalink
Partially revert "Merge pull request #1341 from bbc/upstream/tidying"…
Browse files Browse the repository at this point in the history
… (keep Meteor.absoluteUrl)

Because Meteor.absoluteUrl is used to determine the correct path for connecting the websocket for the client ddp-connection.
  • Loading branch information
nytamin committed Dec 12, 2024
1 parent 2eafb6a commit e6e9aa7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions meteor/__mocks__/meteor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ export namespace MeteorMock {
// but it'll do for now:
return callAsync(methodName, ...args)
}
export function absoluteUrl(path?: string): string {
return path + '' // todo
}
export function setTimeout(fcn: () => void | Promise<void>, time: number): number {
return $.setTimeout(() => {
Promise.resolve()
Expand Down
72 changes: 72 additions & 0 deletions packages/webui/src/meteor/meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,78 @@ Meteor.Error.prototype.clone = function () {
return new Meteor.Error(self.error, self.reason, self.details)
}

/**
* @summary Generate an absolute URL pointing to the application. The server reads from the `ROOT_URL` environment variable to determine where it is running. This is taken care of automatically for apps deployed to Galaxy, but must be provided when using `meteor build`.
* @locus Anywhere
* @param {String} [path] A path to append to the root URL. Do not include a leading "`/`".
* @param {Object} [options]
* @param {Boolean} options.secure Create an HTTPS URL.
* @param {Boolean} options.replaceLocalhost Replace localhost with 127.0.0.1. Useful for services that don't recognize localhost as a domain name.
* @param {String} options.rootUrl Override the default ROOT_URL from the server environment. For example: "`http://foo.example.com`"
*/
Meteor.absoluteUrl = function (path, options) {
// path is optional
if (!options && typeof path === 'object') {
options = path
path = undefined
}
// merge options with defaults
options = Object.assign({}, Meteor.absoluteUrl.defaultOptions, options || {})

var url = options.rootUrl
if (!url) throw new Error('Must pass options.rootUrl or set ROOT_URL in the server environment')

if (!/^http[s]?:\/\//i.test(url))
// url starts with 'http://' or 'https://'
url = 'http://' + url // we will later fix to https if options.secure is set

if (!url.endsWith('/')) {
url += '/'
}

if (path) {
// join url and path with a / separator
while (path.startsWith('/')) {
path = path.slice(1)
}
url += path
}

// turn http to https if secure option is set, and we're not talking
// to localhost.
if (
options.secure &&
/^http:/.test(url) && // url starts with 'http:'
!/http:\/\/localhost[:\/]/.test(url) && // doesn't match localhost
!/http:\/\/127\.0\.0\.1[:\/]/.test(url)
)
// or 127.0.0.1
url = url.replace(/^http:/, 'https:')

if (options.replaceLocalhost) url = url.replace(/^http:\/\/localhost([:\/].*)/, 'http://127.0.0.1$1')

return url
}

// allow later packages to override default options
var defaultOptions = (Meteor.absoluteUrl.defaultOptions = {})

// available only in a browser environment
var location = typeof window === 'object' && window.location

if (typeof window.__meteor_runtime_config__ === 'object' && window.__meteor_runtime_config__.ROOT_URL) {
defaultOptions.rootUrl = window.__meteor_runtime_config__.ROOT_URL
} else if (location && location.protocol && location.host) {
defaultOptions.rootUrl = location.protocol + '//' + location.host
}

// Make absolute URLs use HTTPS by default if the current window.location
// uses HTTPS. Since this is just a default, it can be overridden by
// passing { secure: false } if necessary.
if (location && location.protocol === 'https:') {
defaultOptions.secure = true
}

Meteor._relativeToSiteRootUrl = function (link) {
if (typeof window.__meteor_runtime_config__ === 'object' && link.substr(0, 1) === '/')
link = (window.__meteor_runtime_config__.ROOT_URL_PATH_PREFIX || '') + link
Expand Down
4 changes: 4 additions & 0 deletions packages/webui/src/meteor/socket-stream-client/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ function translateUrl(url, newSchemeBase, subPath) {
newSchemeBase = 'http';
}

if (subPath !== "sockjs" && url.startsWith("/")) {
url = Meteor.absoluteUrl(url.substr(1));
}

var ddpUrlMatch = url.match(/^ddp(i?)\+sockjs:\/\//);
var httpUrlMatch = url.match(/^http(s?):\/\//);
var newScheme;
Expand Down

0 comments on commit e6e9aa7

Please sign in to comment.