UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines 1.96 kB
{"version":3,"file":"injectStorageShim.mjs","names":[],"sources":["../../src/HtmlPreview/injectStorageShim.ts"],"sourcesContent":["/**\n * Why: When iframe sandbox does not include `allow-same-origin`, accessing\n * `window.localStorage` / `window.sessionStorage` throws a SecurityError.\n * Many LLM-generated demos use these APIs as a convenience even when they\n * don't need persistence — letting them throw kills the whole demo.\n *\n * The shim defines per-frame in-memory Storage objects that match the\n * Storage interface, so naive `localStorage.setItem(...)` calls succeed.\n * State does not survive a reload (acceptable — sandbox is throwaway).\n */\nexport const STORAGE_SHIM_SCRIPT = `\n(function () {\n function createStorage() {\n var store = Object.create(null);\n return {\n get length() {\n return Object.keys(store).length;\n },\n key: function (i) {\n var keys = Object.keys(store);\n return i >= 0 && i < keys.length ? keys[i] : null;\n },\n getItem: function (k) {\n return Object.prototype.hasOwnProperty.call(store, k) ? store[k] : null;\n },\n setItem: function (k, v) {\n store[String(k)] = String(v);\n },\n removeItem: function (k) {\n delete store[k];\n },\n clear: function () {\n store = Object.create(null);\n },\n };\n }\n\n function tryShim(name) {\n try {\n // Accessing the property in a sandboxed (no allow-same-origin) frame\n // throws synchronously — that's the signal to install the shim.\n // eslint-disable-next-line no-unused-expressions\n window[name];\n return;\n } catch (_) {}\n try {\n Object.defineProperty(window, name, {\n configurable: true,\n value: createStorage(),\n });\n } catch (_) {}\n }\n\n tryShim('localStorage');\n tryShim('sessionStorage');\n})();\n`;\n"],"mappings":";;;;;;;;;;;AAUA,MAAa,sBAAsB"}