UNPKG

thebe-core

Version:

Typescript based core functionality for Thebe

93 lines 4.02 kB
import { __awaiter } from "tslib"; import { KernelAPI, ServerConnection } from '@jupyterlab/services'; export function removeServerInfo(storageKey) { window.localStorage.removeItem(storageKey); } export 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() }))); } export 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); } } export function getExistingServer(savedSessionOptions, storageKey) { return __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 KernelAPI.listRunning(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; }); } /** * 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 */ export 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)); } /** * 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 */ export function clearSavedSession(storageKey) { console.debug(`thebe:clearSavedSession - removing ${storageKey}`); window.localStorage.removeItem(storageKey); } //# sourceMappingURL=sessions.js.map