X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0412302971e705a539805711496bc24e374b6f50..ba15fa5da21f4bafd3f90a8d259ea2aae764c77e:/apps/workbench/app/assets/javascripts/models/session_db.js diff --git a/apps/workbench/app/assets/javascripts/models/session_db.js b/apps/workbench/app/assets/javascripts/models/session_db.js index 13409d9048..a43cd79545 100644 --- a/apps/workbench/app/assets/javascripts/models/session_db.js +++ b/apps/workbench/app/assets/javascripts/models/session_db.js @@ -5,6 +5,7 @@ window.SessionDB = function() { var db = this Object.assign(db, { + discoveryCache: {}, loadFromLocalStorage: function() { try { return JSON.parse(window.localStorage.getItem('sessions')) || {} @@ -27,6 +28,17 @@ window.SessionDB = function() { }) return sessions }, + loadLocal: function() { + var sessions = db.loadActive() + var s = false + Object.values(sessions).forEach(function(session) { + if (session.isFromRails) { + s = session + return + } + }) + return s + }, save: function(k, v) { var sessions = db.loadAll() sessions[k] = v @@ -84,7 +96,7 @@ window.SessionDB = function() { // If there's a token and baseURL in the location bar (i.e., // we just landed here after a successful login), save it and // scrub the location bar. - if (!document.location.search.startsWith('?')) + if (document.location.search[0] != '?') return var params = {} document.location.search.slice(1).split('&').map(function(kv) { @@ -120,6 +132,36 @@ window.SessionDB = function() { }) }) }, + // Return the Workbench base URL advertised by the session's + // API server, or a reasonable guess, or (if neither strategy + // works out) null. + workbenchBaseURL: function(session) { + var dd = db.discoveryDoc(session)() + if (!dd) + // Don't fall back to guessing until we receive the discovery doc + return null + if (dd.workbenchUrl) + return dd.workbenchUrl + // Guess workbench.{apihostport} is a Workbench... unless + // the host part of apihostport is an IPv4 or [IPv6] + // address. + if (!session.baseURL.match('://(\\[|\\d+\\.\\d+\\.\\d+\\.\\d+[:/])')) { + var wbUrl = session.baseURL.replace('://', '://workbench.') + // Remove the trailing slash, if it's there. + return wbUrl.slice(-1) == '/' ? wbUrl.slice(0, -1) : wbUrl + } + return null + }, + // Return a m.stream that will get fulfilled with the + // discovery doc from a session's API server. + discoveryDoc: function(session) { + var cache = db.discoveryCache[session.baseURL] + if (!cache) { + db.discoveryCache[session.baseURL] = cache = m.stream() + m.request(session.baseURL+'discovery/v1/apis/arvados/v1/rest').then(cache) + } + return cache + }, request: function(session, path, opts) { opts = opts || {} opts.headers = opts.headers || {}