|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Silicon Labs |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { v4 as uuidv4 } from 'uuid' |
| 19 | +import { parse as querystringParse } from 'querystring' |
| 20 | + |
| 21 | +// Session handling constants |
| 22 | +const SESSION_KEY = 'session_uuid' |
| 23 | +const APP_ID_KEY = 'stsApplicationId' |
| 24 | +const DEFAULT_APP_ID = 'defaultAppId' |
| 25 | + |
| 26 | +/** |
| 27 | + * Loads a session map from the session storage. |
| 28 | + * Session map is a string=>string key/value map, |
| 29 | + * which contains "appId" as a key, and the generated UUID as a value. |
| 30 | + * If appId is not present (as in standalone), then a default value |
| 31 | + * is used. |
| 32 | + * |
| 33 | + * @param {*} window |
| 34 | + * @returns |
| 35 | + */ |
| 36 | +function loadSessionMap(window) { |
| 37 | + let json = window.sessionStorage.getItem(SESSION_KEY) |
| 38 | + if (json == null) { |
| 39 | + // Nothing there, let's just create an empty map. |
| 40 | + return new Map() |
| 41 | + } else { |
| 42 | + // Found it, so we deserialize it. |
| 43 | + const parsedArray = JSON.parse(json) |
| 44 | + return new Map(parsedArray) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Saves a session map into the session storage. |
| 50 | + * @param {*} window |
| 51 | + * @param {*} map |
| 52 | + */ |
| 53 | +function saveSessionMap(window, map) { |
| 54 | + const mapArray = Array.from(map) |
| 55 | + const json = JSON.stringify(mapArray) |
| 56 | + window.sessionStorage.setItem(SESSION_KEY, json) |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Determine application id. If there is actually an stsApplicationId passed |
| 61 | + * in, then what we get is that. Otherwise, it uses |
| 62 | + * |
| 63 | + * @param {*} window |
| 64 | + * @returns |
| 65 | + */ |
| 66 | +function retrieveApplicationId(window) { |
| 67 | + // Check if it has already been put on the dom. |
| 68 | + if (window.zapAppId) { |
| 69 | + return window.zapAppId |
| 70 | + } |
| 71 | + |
| 72 | + // If it's not there, get it from the seach query.... |
| 73 | + let search = window.location.search |
| 74 | + if (search[0] === '?') { |
| 75 | + search = search.substring(1) |
| 76 | + } |
| 77 | + let query = querystringParse(search) |
| 78 | + |
| 79 | + let appId |
| 80 | + if (query[APP_ID_KEY]) { |
| 81 | + appId = query[APP_ID_KEY] |
| 82 | + } else { |
| 83 | + appId = DEFAULT_APP_ID |
| 84 | + } |
| 85 | + |
| 86 | + // Store it onto the window object for quicker access. |
| 87 | + window.zapAppId = appId |
| 88 | + |
| 89 | + return appId |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Returns the session id after it's been created. This method WILL NOT |
| 94 | + * create one if it hasn't been created yet. Only the |
| 95 | + * initializeSessionIdInBrowser method will create a new session id. |
| 96 | + * @param {*} window |
| 97 | + * @returns |
| 98 | + */ |
| 99 | +export function sessionId(window) { |
| 100 | + let appId = retrieveApplicationId(window) |
| 101 | + let sessionMap = loadSessionMap(window) |
| 102 | + return sessionMap.get(appId) |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * This is the entry point for the boot file to handle the session object. |
| 107 | + * It mainly needs to figure out if it needs to create a new UUID or not. |
| 108 | + * @param {*} window |
| 109 | + */ |
| 110 | +export function initializeSessionIdInBrowser(window) { |
| 111 | + let sessionMap = loadSessionMap(window) |
| 112 | + let appId = retrieveApplicationId(window) |
| 113 | + |
| 114 | + // Get the session object, that is keyed by the appId |
| 115 | + let sessionUuid = sessionMap.get(appId) |
| 116 | + if (sessionUuid == null) { |
| 117 | + // This is a whole new appId. Let's create a session object for it. |
| 118 | + sessionMap.set(appId, uuidv4()) |
| 119 | + saveSessionMap(window, sessionMap) |
| 120 | + } |
| 121 | +} |
0 commit comments