Skip to content

Commit e6e9aa7

Browse files
committed
Partially revert "Merge pull request #1341 from bbc/upstream/tidying" (keep Meteor.absoluteUrl)
Because Meteor.absoluteUrl is used to determine the correct path for connecting the websocket for the client ddp-connection.
1 parent 2eafb6a commit e6e9aa7

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

meteor/__mocks__/meteor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ export namespace MeteorMock {
195195
// but it'll do for now:
196196
return callAsync(methodName, ...args)
197197
}
198+
export function absoluteUrl(path?: string): string {
199+
return path + '' // todo
200+
}
198201
export function setTimeout(fcn: () => void | Promise<void>, time: number): number {
199202
return $.setTimeout(() => {
200203
Promise.resolve()

packages/webui/src/meteor/meteor.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,78 @@ Meteor.Error.prototype.clone = function () {
234234
return new Meteor.Error(self.error, self.reason, self.details)
235235
}
236236

237+
/**
238+
* @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`.
239+
* @locus Anywhere
240+
* @param {String} [path] A path to append to the root URL. Do not include a leading "`/`".
241+
* @param {Object} [options]
242+
* @param {Boolean} options.secure Create an HTTPS URL.
243+
* @param {Boolean} options.replaceLocalhost Replace localhost with 127.0.0.1. Useful for services that don't recognize localhost as a domain name.
244+
* @param {String} options.rootUrl Override the default ROOT_URL from the server environment. For example: "`http://foo.example.com`"
245+
*/
246+
Meteor.absoluteUrl = function (path, options) {
247+
// path is optional
248+
if (!options && typeof path === 'object') {
249+
options = path
250+
path = undefined
251+
}
252+
// merge options with defaults
253+
options = Object.assign({}, Meteor.absoluteUrl.defaultOptions, options || {})
254+
255+
var url = options.rootUrl
256+
if (!url) throw new Error('Must pass options.rootUrl or set ROOT_URL in the server environment')
257+
258+
if (!/^http[s]?:\/\//i.test(url))
259+
// url starts with 'http://' or 'https://'
260+
url = 'http://' + url // we will later fix to https if options.secure is set
261+
262+
if (!url.endsWith('/')) {
263+
url += '/'
264+
}
265+
266+
if (path) {
267+
// join url and path with a / separator
268+
while (path.startsWith('/')) {
269+
path = path.slice(1)
270+
}
271+
url += path
272+
}
273+
274+
// turn http to https if secure option is set, and we're not talking
275+
// to localhost.
276+
if (
277+
options.secure &&
278+
/^http:/.test(url) && // url starts with 'http:'
279+
!/http:\/\/localhost[:\/]/.test(url) && // doesn't match localhost
280+
!/http:\/\/127\.0\.0\.1[:\/]/.test(url)
281+
)
282+
// or 127.0.0.1
283+
url = url.replace(/^http:/, 'https:')
284+
285+
if (options.replaceLocalhost) url = url.replace(/^http:\/\/localhost([:\/].*)/, 'http://127.0.0.1$1')
286+
287+
return url
288+
}
289+
290+
// allow later packages to override default options
291+
var defaultOptions = (Meteor.absoluteUrl.defaultOptions = {})
292+
293+
// available only in a browser environment
294+
var location = typeof window === 'object' && window.location
295+
296+
if (typeof window.__meteor_runtime_config__ === 'object' && window.__meteor_runtime_config__.ROOT_URL) {
297+
defaultOptions.rootUrl = window.__meteor_runtime_config__.ROOT_URL
298+
} else if (location && location.protocol && location.host) {
299+
defaultOptions.rootUrl = location.protocol + '//' + location.host
300+
}
301+
302+
// Make absolute URLs use HTTPS by default if the current window.location
303+
// uses HTTPS. Since this is just a default, it can be overridden by
304+
// passing { secure: false } if necessary.
305+
if (location && location.protocol === 'https:') {
306+
defaultOptions.secure = true
307+
}
308+
237309
Meteor._relativeToSiteRootUrl = function (link) {
238310
if (typeof window.__meteor_runtime_config__ === 'object' && link.substr(0, 1) === '/')
239311
link = (window.__meteor_runtime_config__.ROOT_URL_PATH_PREFIX || '') + link

packages/webui/src/meteor/socket-stream-client/urls.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function translateUrl(url, newSchemeBase, subPath) {
1212
newSchemeBase = 'http';
1313
}
1414

15+
if (subPath !== "sockjs" && url.startsWith("/")) {
16+
url = Meteor.absoluteUrl(url.substr(1));
17+
}
18+
1519
var ddpUrlMatch = url.match(/^ddp(i?)\+sockjs:\/\//);
1620
var httpUrlMatch = url.match(/^http(s?):\/\//);
1721
var newScheme;

0 commit comments

Comments
 (0)