UNPKG

thebe-core

Version:

Typescript based core functionality for Thebe

102 lines 4.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clearSavedSession = exports.clearAllSavedSessions = exports.getExistingServer = exports.saveServerInfo = exports.updateLastUsedTimestamp = exports.removeServerInfo = void 0; const tslib_1 = require("tslib"); const services_1 = require("@jupyterlab/services"); function removeServerInfo(storageKey) { window.localStorage.removeItem(storageKey); } exports.removeServerInfo = removeServerInfo; function updateLastUsedTimestamp(storageKey) { const saved = window.localStorage.getItem(storageKey); if (!saved) return; const obj = JSON.parse(saved); window.localStorage.setItem(storageKey, JSON.stringify(Object.assign(Object.assign({}, obj), { lastUsed: new Date() }))); } exports.updateLastUsedTimestamp = updateLastUsedTimestamp; function saveServerInfo(storageKey, id, serverSettings) { try { // save the current connection url+token to reuse later const { baseUrl, token, wsUrl } = serverSettings; window.localStorage.setItem(storageKey, JSON.stringify({ id, baseUrl, token, wsUrl, lastUsed: new Date(), })); } catch (e) { // storage quota full, gently ignore nonfatal error console.warn("Couldn't save thebe binder connection info to local storage", e); } } exports.saveServerInfo = saveServerInfo; function getExistingServer(savedSessionOptions, storageKey) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!savedSessionOptions.enabled) return null; const storedInfoJSON = window.localStorage.getItem(storageKey); if (storedInfoJSON == null) { console.debug('thebe:getExistingServer No session saved in ', storageKey); return null; } console.debug('thebe:getExistingServer Saved binder session found'); const existingSettings = JSON.parse(storedInfoJSON !== null && storedInfoJSON !== void 0 ? storedInfoJSON : ''); const lastUsed = new Date(existingSettings.lastUsed); const now = new Date(); const ageSeconds = (now.getTime() - lastUsed.getTime()) / 1000; if (ageSeconds > savedSessionOptions.maxAge) { console.debug(`thebe:getExistingServer Not using expired binder session for ${existingSettings.baseUrl} from ${lastUsed}`); window.localStorage.removeItem(storageKey); return null; } try { yield services_1.KernelAPI.listRunning(services_1.ServerConnection.makeSettings(existingSettings)); } catch (err) { console.debug('thebe:getExistingServer Saved binder connection appears to be invalid, requesting new session', err); window.localStorage.removeItem(storageKey); return null; } // refresh lastUsed timestamp in stored info updateLastUsedTimestamp(storageKey); console.debug(`thebe:getExistingServer Saved binder session is valid and will be reused ${existingSettings.baseUrl}`); return existingSettings; }); } exports.getExistingServer = getExistingServer; /** * Remove all saved sessions items from local storage based on the storagePrefix provided. * The appropriate (default) storage prefix will be available in the SavedSessionOptions object * in the Config object. * * @param storagePrefix */ function clearAllSavedSessions(storagePrefix = 'thebe-binder') { const keysToRemove = []; for (let i = 0; i < window.localStorage.length; i++) { const key = window.localStorage.key(i); if (key === null || key === void 0 ? void 0 : key.startsWith(storagePrefix)) { keysToRemove.push(key); } } console.debug(`thebe:clearAllSavedSessions - removing ${keysToRemove.length} saved sessions`, keysToRemove.join(',')); keysToRemove.forEach((key) => window.localStorage.removeItem(key)); } exports.clearAllSavedSessions = clearAllSavedSessions; /** * Remove all saved sessions items from local storage based on the storagePrefix provided. * The appropriate (default) storage prefix will be available in the SavedSessionOptions object * in the Config object. * * @param storagePrefix * @param url */ function clearSavedSession(storageKey) { console.debug(`thebe:clearSavedSession - removing ${storageKey}`); window.localStorage.removeItem(storageKey); } exports.clearSavedSession = clearSavedSession; //# sourceMappingURL=sessions.js.map