{"version":3,"sources":["local-storage-compat.ts"],"sourcesContent":["// custom localstorage class that handles browser and node local storage and temporary (session) storage\n\nimport { logger } from \"../../utils/global_values.ts\";\nimport { client_type } from \"../../utils/constants.ts\";\nimport { ptr_cache_path } from \"../../runtime/cache_path.ts\";\nimport { normalizePath } from \"../../utils/normalize-path.ts\";\n\nclass LocalStorage implements Storage {\n\t[name: string]: any;\n\tget length() {return Object.getOwnPropertyNames(this).length}\n\n\t#cache_file?: URL\n\n\t#initialized = false;\n\n\t#init() {\n\t\tif (this.#initialized) return;\n\t\tthis.#initialized = true;\n\n\t\tthis.clear();\n\n\t\t// create cache file\n\t\tthis.#createCacheFileIfNotExisting();\n\n\t\t// try to parse JSON\n\t\ttry {\n\t\t\tconst serialized = Deno.readFileSync(this.#cache_file);\n\t\t\tconst data = JSON.parse(new TextDecoder().decode(serialized));\n\t\t\tObject.assign(this, data);\n\t\t}\n\t\tcatch {\n\t\t\tlogger.warn(\"Could not read localStorage file\")\n\t\t} // ignore\n\n\t}\n\n\t#createCacheFileIfNotExisting(){\n\t\tconst name = '@@local' // Runtime.endpoint.toString(); - not working, not yet initialized at this time\n\n\t\t// file setup\n\t\tthis.#cache_file = new URL(name, ptr_cache_path);\n\t\ttry {\n\t\t\ttry {\n\t\t\t\tconst file = Deno.openSync(normalizePath(ptr_cache_path));\n\t\t\t\tfile.close()\n\t\t\t} catch {\n\t\t\t\tDeno.mkdirSync(normalizePath(ptr_cache_path), {recursive:true});\n\t\t\t}\n\t\n\t\t\ttry {\n\t\t\t\tconst file = Deno.openSync(normalizePath(this.#cache_file));\n\t\t\t\tfile.close()\n\t\t\t} catch {\n\t\t\t\tDeno.writeTextFileSync(normalizePath(this.#cache_file), '{}');\n\t\t\t}\n\t\t}\n\t\tcatch {\n\t\t\tlogger.error(\"Cannot save local storage cache file\")\n\t\t}\n\t}\n\n\n\tsaveFile(){\n\t\tif (!this.#initialized) return;\n\t\tthis.#createCacheFileIfNotExisting();\n\t\tif (this.#cache_file) Deno.writeTextFileSync(normalizePath(this.#cache_file), JSON.stringify(this));\n\t}\n\n\n\tclear(): void {\n\t\tthis.#init();\n\t\tfor (const key of Object.getOwnPropertyNames(this)) {\n\t\t\tdelete this[key];\n\t\t}\n\t}\n\tgetItem(key: string): string {\n\t\tthis.#init();\n\t\treturn this[key];\n\t}\n\tkey(index: number): string {\n\t\tthis.#init();\n\t\treturn Object.getOwnPropertyNames(this)[index];\n\t}\n\tremoveItem(key: string): void {\n\t\tthis.#init();\n\t\tdelete this[key];\n\t}\n\tsetItem(key: string, value: string): void {\n\t\tthis.#init();\n\t\tthis[key] = String(value);\n\t}\n\n}\n\n// old deno worker problems\n// if (client_type !== \"deno\" && client_type !== \"worker\" && !globalThis.localStorage) {\n// \tthrow \"no localStorage available (are you using the latest Deno version?)\"\n// }\n\n// export const localStorage = client_type == \"deno\" ? new LocalStorage() : globalThis.localStorage;\n// if (client_type == \"deno\") globalThis.localStorage = localStorage;\n\n\n// migrate from compat localStorage to deno localStorage\n// TODO: remove this at some point\nif (client_type == \"deno\" && !globalThis.NO_INIT) {\n\ttry {\n\t\tconst cache_file = new URL('@@local', ptr_cache_path);\n\t\tconst serialized = Deno.readFileSync(cache_file);\n\t\tconst data = JSON.parse(new TextDecoder().decode(serialized));\n\t\tconst entries = Object.entries(data);\n\t\tfor (const [key, value] of entries as [string, string][]) {\n\t\t\tglobalThis.localStorage.setItem(key, value);\n\t\t}\n\t\tawait Deno.rename(cache_file, new URL('@@local.backup', ptr_cache_path));\n\t\tlogger.success(\"Migrated \"+entries.length+\" items from compat pointer storage to deno localStorage\")\n\t}\n\tcatch {}\n}\n\nexport const localStorage = globalThis.localStorage"],"names":["logger","client_type","ptr_cache_path","globalThis","NO_INIT","cache_file","URL","serialized","Deno","readFileSync","data","JSON","parse","TextDecoder","decode","entries","Object","key","value","localStorage","setItem","rename","success","length"],"mappings":"AAEA,SAASA,MAAM,QAAQ,+BAA+B;AACtD,SAASC,WAAW,QAAQ,2BAA2B;AACvD,SAASC,cAAc,QAAQ,8BAA8B;AAC7D,OAA8B,gCAAgC;AAoG9D,IAAID,AAAe,UAAfA,eAAyB,CAACE,WAAWC,OAAO,EAC/C,IAAI;IACH,IAAMC,aAAa,IAAIC,IAAI,WAAWJ,iBAChCK,aAAaC,KAAKC,YAAY,CAACJ,aAC/BK,OAAOC,KAAKC,KAAK,CAAC,IAAIC,cAAcC,MAAM,CAACP,cAC3CQ,UAAUC,OAAOD,OAAO,CAACL;IAC/B,KAAK,IAAM,CAACO,KAAKC,MAAM,IAAIH,QAC1BZ,WAAWgB,YAAY,CAACC,OAAO,CAACH,KAAKC;IAEtC,MAAMV,KAAKa,MAAM,CAAChB,YAAY,IAAIC,IAAI,kBAAkBJ,kBACxDF,OAAOsB,OAAO,CAAC,cAAYP,QAAQQ,MAAM,GAAC;AAC3C,EACA,OAAM,CAAC;AAGR,OAAO,MAAMJ,eAAehB,WAAWgB,YAAY,CAAA","sourceRoot":"/lib/datex-core-js-legacy-0.3.15/storage/storage-locations/","file":"local-storage-compat.ts"}