dome-embedded-app-sdk 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/card-sdk.d.ts +13 -1
- package/dist/dome-sdk.d.ts +5 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/card-sdk.d.ts
CHANGED
|
@@ -54,6 +54,8 @@ export declare class CardSdk extends DomeEmbeddedAppSdk {
|
|
|
54
54
|
private static readonly FS_RESPONSE_TIMEOUT_MS;
|
|
55
55
|
private static readonly FS_READ_RETENTION_MS;
|
|
56
56
|
private static readonly CARD_FS_ACK_TIMEOUT_MS;
|
|
57
|
+
private static readonly MAX_CONSOLE_ARG_CHARS;
|
|
58
|
+
private static readonly MAX_CONSOLE_LOG_ENTRIES;
|
|
57
59
|
private static readonly DEFAULT_ROLE_PERMISSIONS;
|
|
58
60
|
dataStore: any;
|
|
59
61
|
private accessToken;
|
|
@@ -75,7 +77,11 @@ export declare class CardSdk extends DomeEmbeddedAppSdk {
|
|
|
75
77
|
private containerIuid?;
|
|
76
78
|
private pendingAcks;
|
|
77
79
|
private consoleCaptureInstalled;
|
|
78
|
-
private
|
|
80
|
+
private consoleCaptureEnabled;
|
|
81
|
+
private consoleCaptureMinLevel;
|
|
82
|
+
private consoleLogBuffer;
|
|
83
|
+
private consoleLogWriteIndex;
|
|
84
|
+
private consoleLogSize;
|
|
79
85
|
private buildCardFsMessagePayload;
|
|
80
86
|
private formatFolderPath;
|
|
81
87
|
private assertValidReadHandler;
|
|
@@ -145,8 +151,14 @@ export declare class CardSdk extends DomeEmbeddedAppSdk {
|
|
|
145
151
|
private clearFsReadRequest;
|
|
146
152
|
private sendConsoleLogs;
|
|
147
153
|
private setupConsoleCapture;
|
|
154
|
+
private applyLoggingConfig;
|
|
155
|
+
private isConsoleLogLevel;
|
|
156
|
+
private shouldCaptureLevel;
|
|
148
157
|
private recordConsoleEntry;
|
|
158
|
+
private clearConsoleLogs;
|
|
159
|
+
private getConsoleLogs;
|
|
149
160
|
private serializeConsoleArg;
|
|
161
|
+
private truncateSerializedArg;
|
|
150
162
|
private failFsReadRequest;
|
|
151
163
|
private clearFsWriteRequest;
|
|
152
164
|
private failFsWriteRequest;
|
package/dist/dome-sdk.d.ts
CHANGED
|
@@ -24,7 +24,10 @@ export declare enum ClientMessageType {
|
|
|
24
24
|
AF1_DATA_TOKEN_ACK = "AF1_DATA_TOKEN_ACK",
|
|
25
25
|
ERROR = "ERROR",
|
|
26
26
|
REFRESH = "REFRESH",
|
|
27
|
-
|
|
27
|
+
LOGGING_ENABLE = "LOGGING_ENABLE",
|
|
28
|
+
LOGGING_DISABLE = "LOGGING_DISABLE",
|
|
29
|
+
LOGGING_CLEAR = "LOGGING_CLEAR",
|
|
30
|
+
LOGGING_GET = "LOGGING_GET"
|
|
28
31
|
}
|
|
29
32
|
export declare enum CardMessageType {
|
|
30
33
|
APP_READY = "APP_READY",
|
|
@@ -36,7 +39,7 @@ export declare enum CardMessageType {
|
|
|
36
39
|
FILE_DIRTY = "FILE_DIRTY",
|
|
37
40
|
OPEN_DEEPLINK = "OPEN_DEEPLINK",
|
|
38
41
|
AF1_DATA_TOKEN = "AF1_DATA_TOKEN",
|
|
39
|
-
|
|
42
|
+
LOGGING_GET_RET = "LOGGING_GET_RET"
|
|
40
43
|
}
|
|
41
44
|
export interface HostInfo {
|
|
42
45
|
type: string;
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.domeSdk=t():e.domeSdk=t()}(this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var s in r)e.o(r,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:r[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{CardMessageType:()=>o,CardPermission:()=>h,CardSdk:()=>m,ClientMessageType:()=>i,CryptoA01:()=>l,DomeEmbeddedAppSdk:()=>c,ViewerMessageType:()=>a,ViewerSdk:()=>d,cardPermission:()=>f,getKeyFromBlob:()=>u});const r="0.2.7",s=new Set(["dome","intouchapp"]);var a,n,i,o;!function(e){e.CONNECTION_SUCCESS="CONNECTION_SUCCESS",e.INIT="INIT",e.REQUEST_SAVE="REQUEST_SAVE",e.REQUEST_CLOSE="REQUEST_CLOSE",e.REQUEST_INITIAL_DATA="REQUEST_INITIAL_DATA",e.SET_DIRTY="SET_DIRTY",e.SEND_CLOSE="SEND_CLOSE",e.SEND_EXCEPTION="SEND_EXCEPTION"}(a||(a={})),function(e){e.INIT_MESSAGE_CHANNEL="INIT_MESSAGE_CHANNEL"}(n||(n={})),function(e){e.CONNECT="CONNECT",e.REQUEST_CLOSE="REQUEST_CLOSE",e.REQUEST_SAVE="REQUEST_SAVE",e.SAVE_ERROR="SAVE_ERROR",e.SAVE_SUCCESS="SAVE_SUCCESS",e.DATA_CHANGE="DATA_CHANGE",e.FILE_DATA="FILE_DATA",e.WRITE_FILE_ACK="WRITE_FILE_ACK",e.READ_FILE_ACK="READ_FILE_ACK",e.DELETE_FILE_ACK="DELETE_FILE_ACK",e.LIST_FILES_ACK="LIST_FILES_ACK",e.INIT_ACK="INIT_ACK",e.AF1_DATA_TOKEN_ACK="AF1_DATA_TOKEN_ACK",e.ERROR="ERROR",e.REFRESH="REFRESH",e.GET_LOGS="GET_LOGS"}(i||(i={})),function(e){e.APP_READY="APP_READY",e.INIT="INIT",e.READ_FILE="READ_FILE",e.WRITE_FILE="WRITE_FILE",e.DELETE_FILE="DELETE_FILE",e.LIST_FILES="LIST_FILES",e.FILE_DIRTY="FILE_DIRTY",e.OPEN_DEEPLINK="OPEN_DEEPLINK",e.AF1_DATA_TOKEN="AF1_DATA_TOKEN",e.APP_LOGS="APP_LOGS"}(o||(o={}));class c{constructor(){this.targetOrigin="*",this.isAppReady=!1,this.port2=null,this.runtimeHost="unknown",this.parentHostDetails=null,this.parentCapabilities=null,this.handleDeepLinkClick=e=>{if("webapp"!==this.runtimeHost)return;if(!(e.target instanceof Element))return;const t=e.target.closest("a[href]");if(!t)return;const r=t.getAttribute("href")??"";this.emitDeepLink(r)&&e.preventDefault()},console.info(`Initializing Dome Embedded App SDK v${r}`),this.detectHost(),this.setupDeepLinkInterception()}detectHost(){void 0!==window.AndroidBridge?this.runtimeHost="android":void 0!==window.webkit?this.runtimeHost="ios":this.runtimeHost="webapp",this.runtimeHost}setupDeepLinkInterception(){"webapp"===this.runtimeHost&&"undefined"!=typeof document&&document.addEventListener("click",this.handleDeepLinkClick,!0)}emitDeepLink(e){if("string"!=typeof e||""===e.trim())return console.warn("emitDeepLink called without a valid href"),!1;const t=e.split(":")[0]?.toLowerCase();return!(!t||!s.has(t))&&(this.sendMessage(o.OPEN_DEEPLINK,{url:e}),!0)}updateParentContext(e,t){if(!e&&!t)return;const r={...this.parentHostDetails??{type:this.runtimeHost},...e??{}};r.type||(r.type=this.runtimeHost);const s=t??this.parentCapabilities??r.capabilities;t&&(this.parentCapabilities=t),s?r.capabilities=s:delete r.capabilities,this.parentHostDetails=r,this.parentCapabilities=s??null,s?console.info("Host capabilities detected",{host_type:r.type,capabilities:s}):console.info("Host capabilities not found",{hostType:r.type})}getHost(){return this.parentHostDetails??{type:this.runtimeHost}}sendMessage(e,t){const r={type:e,data:t??null};switch(this.runtimeHost){case"android":window.AndroidBridge?.sendMessage(JSON.stringify(r));break;case"ios":window?.webkit?.messageHandlers?window.webkit?.messageHandlers.appHandler.postMessage(JSON.stringify(r)):console.error("webkit.messageHandlers not found");break;case"webapp":this.port2?this.port2.postMessage(r):console.error("Web connection is not established.");break;default:console.error("Unsupported host, cannot send message.")}this.runtimeHost}sendAppInit(){this.isAppReady||(this.isAppReady=!0,this.sendMessage(a.INIT,{sdk:{ver:r}}))}safeInvoke(e,t,r){const s=t?.[e];"function"==typeof s?s(r):console.warn(`Handler for '${String(e)}' is not defined.`)}setupParentConnection(){return new Promise(((e,t)=>{switch(this.runtimeHost){case"android":window.receiveFromAndroid=e=>{this.handleMessage(e.type,e.data)},e();break;case"ios":window.receiveFromIOS=e=>{this.handleMessage(e.type,e.data)},e();break;case"webapp":if(this.port2)return console.warn("Connection already established. Skipping reinitialization."),void e();const s=t=>{const{type:r}=t.data||{};r===i.CONNECT&&t.ports&&t.ports.length>0&&(this.port2=t.ports[0],this.port2.onmessage=e=>this.handlePortMessage(e),window.removeEventListener("message",s),this.notifyConnectionSuccess(),e())};window.addEventListener("message",s),window.parent?window.parent.postMessage({type:n.INIT_MESSAGE_CHANNEL,data:{sdk:{ver:r}}},this.targetOrigin):console.error("Parent window not available to initialize message channel.");break;default:console.error("Unknown host."),t("Unknown host")}}))}notifyConnectionSuccess(){this.sendMessage(a.CONNECTION_SUCCESS)}handlePortMessage(e){const{type:t,data:r}=e.data||{};t&&this.handleMessage(t,r)}handleMessage(e,t){throw new Error("Subclasses must implement handleMessage.")}}class d extends c{constructor(){super(),this.handler=null,this.pendingRequests=new Map,this.pendingInitAck=null}static init(e){return d.initialized?(console.warn("ViewerSdk is already initialized. Skipping initialization."),d.instance):(d.instance||(d.instance=new d,d.instance.setupParentConnection().then((()=>{try{d.instance.initializeViewerSdk()}catch(e){console.error("Error in initializeViewerSdk:",e)}})).catch((e=>{console.error("init: Error setting up parent connection!",e),console.trace("called from:")}))),e&&d.instance.setHandler(e),d.initialized=!0,d.instance)}setHandler(e){this.handler=e,this.pendingInitAck&&(this.safeInvoke("onInitialData",this.handler,this.pendingInitAck),this.pendingInitAck=null)}canRead(e){return!!e?.includes("r")}canWrite(e){return!!e?.includes("w")||!!e?.includes("*")}initializeViewerSdk(){this.sendAppInit()}requestInitialData(){this.sendMessage(a.REQUEST_INITIAL_DATA)}requestSave(e,t){const r=function(){if("undefined"!=typeof window&&window.crypto&&window.crypto.randomUUID)return window.crypto.randomUUID();if("undefined"!=typeof crypto&&crypto.randomUUID)return crypto.randomUUID();throw new Error("UUID generation is not supported in this environment")}();return this.sendMessage(a.REQUEST_SAVE,{doc:e,isDataDirty:t,requestId:r}),new Promise(((e,t)=>{this.pendingRequests.set(r,e),this.pendingRequests.set(r+"_reject",t),setTimeout((()=>{this.pendingRequests.has(r)&&(this.pendingRequests.delete(r),this.pendingRequests.delete(r+"_reject"))}),3e4)}))}handleOnSave(e){const{requestId:t,status:r,message:s}=e,a=this.pendingRequests.get(t),n=this.pendingRequests.get(t+"_reject");a&&("error"===r?n?.({status:r,message:s}):a({status:r,message:s}),this.pendingRequests.delete(t),this.pendingRequests.delete(t+"_reject"))}setDirty(e){this.sendMessage(a.SET_DIRTY,e)}sendClose(e,t){this.sendMessage(a.SEND_CLOSE,{doc:e,isDataDirty:t})}sendException(e){this.sendMessage(a.SEND_EXCEPTION,e)}handleMessage(e,t){if(e===i.INIT_ACK&&this.updateParentContext(t?.host,t?.capabilities),this.handler)switch(e){case i.INIT_ACK:this.safeInvoke("onInitialData",this.handler,t);break;case i.DATA_CHANGE:this.safeInvoke("onDataChange",this.handler,t);break;case i.REQUEST_CLOSE:this.safeInvoke("onCloseRequest",this.handler);break;case i.REQUEST_SAVE:this.safeInvoke("onSaveRequest",this.handler);break;case i.SAVE_SUCCESS:case i.SAVE_ERROR:this.handleOnSave(t);break;default:console.warn(`No handler found for message type: ${e}`)}else e===i.INIT_ACK?(console.warn("Handler not set. Storing INIT_ACK message for later processing."),this.pendingInitAck=t):console.error("Message handler not found for type:",e)}}d.initialized=!1;class l{constructor(){if(this.subtleCrypto=window.crypto?.subtle,!this.subtleCrypto)throw new Error("SubtleCrypto API is not available in this environment.")}async decrypt(e,t,r){try{if(!e)throw new Error("Invalid token");const s=this.base64UrlDecode(e);if(128!==s[0])throw new Error("Invalid version");s.slice(1,9);const a=s.slice(9,25),n=s.slice(25,-32),i=s.slice(-32),o=await this.deriveKey(t,r),{hmacKey:c,aesKey:d}=await this.splitKey(o),l=s.slice(0,-32);if(!new Uint8Array(await this.subtleCrypto.sign("HMAC",c,l)).every(((e,t)=>e===i[t])))throw new Error("Invalid HMAC. Token has been tampered with!");const u=await this.subtleCrypto.decrypt({name:"AES-CBC",iv:a},d,n);return(new TextDecoder).decode(u)}catch(e){throw console.log("Error in decrypt:",e),e}}async deriveKey(e,t,r=1e4){const s=new TextEncoder,a=await this.subtleCrypto.importKey("raw",s.encode(e),"PBKDF2",!1,["deriveKey"]);return this.subtleCrypto.deriveKey({name:"PBKDF2",hash:"SHA-256",salt:s.encode(t),iterations:r},a,{name:"AES-CBC",length:256},!0,["encrypt","decrypt"])}async splitKey(e){const t=new Uint8Array(await this.subtleCrypto.exportKey("raw",e));return{hmacKey:await this.subtleCrypto.importKey("raw",t.slice(0,16),{name:"HMAC",hash:"SHA-256"},!1,["sign","verify"]),aesKey:await this.subtleCrypto.importKey("raw",t.slice(16),{name:"AES-CBC"},!1,["encrypt","decrypt"])}}base64UrlDecode(e){const t=e.replace(/-/g,"+").replace(/_/g,"/"),r=atob(t);return new Uint8Array([...r].map((e=>e.charCodeAt(0))))}}function u(e){let t;if(1!==e.v)throw new Error(`Unsupported key blob version: ${e.v}`);return t=function(e){const{seed:t,obf:r}=e,s=new Uint8Array(r.length);for(let e=0;e<r.length;e++)s[e]=r[e]^t+17*e&255;return s}(e),function(e){if("undefined"!=typeof TextDecoder)return(new TextDecoder).decode(e);let t="";for(let r=0;r<e.length;r++)t+=String.fromCharCode(e[r]);return t}(t)}var h;!function(e){e.READ="r",e.WRITE="w",e.FORWARD="f",e.SHARE="s",e.DOWNLOAD="d"}(h||(h={}));const f=h;class p{constructor(e,t){this.cardIuid=e,this.containerIuid=t,this.openPromise=null}isSupported(){return"undefined"!=typeof indexedDB}async openDb(){return this.isSupported()?(this.openPromise||(this.openPromise=new Promise((e=>{this.openWithVersion(p.DB_VERSION,e)}))),this.openPromise):null}openWithVersion(e,t){let r="ensure";const s=indexedDB.open(this.cardIuid,e);s.onupgradeneeded=e=>{r=0===(e.oldVersion||0)?"create":"ensure",this.configureStores(s.result)},s.onsuccess=()=>{const e=s.result;if(!this.hasRequiredStores(e)){const r=e.version+1;return e.close(),void this.openWithVersion(r,t)}this.updateStoresMetadata(e,"ensure"===r).catch((()=>{})).finally((()=>t(e)))},s.onerror=()=>t(null)}configureStores(e){e.objectStoreNames.contains(p.DATA_STORE)||e.createObjectStore(p.DATA_STORE),e.objectStoreNames.contains(p.NAME_LOOKUP_STORE)||e.createObjectStore(p.NAME_LOOKUP_STORE),e.objectStoreNames.contains(p.INFO_STORE)||e.createObjectStore(p.INFO_STORE)}hasRequiredStores(e){const t=e.objectStoreNames;return t.contains(p.DATA_STORE)&&t.contains(p.NAME_LOOKUP_STORE)&&t.contains(p.INFO_STORE)}static nowMicros(){return Math.trunc(1e3*Date.now())}updateStoresMetadata(e,t){return Promise.all([this.ensureStoreVersion(e,p.DATA_STORE),this.ensureStoreVersion(e,p.NAME_LOOKUP_STORE),this.updateCardInfo(e,t)]).then((()=>{}))}ensureStoreVersion(e,t){return e.objectStoreNames.contains(t)?new Promise((r=>{try{const s=e.transaction(t,"readwrite"),a=s.objectStore(t),n=()=>r();s.oncomplete=n,s.onerror=n,s.onabort=n,a.put("1.0.0",p.STORE_VERSION_KEY)}catch{r()}})):Promise.resolve()}async updateCardInfo(e,t){if(!e.objectStoreNames.contains(p.INFO_STORE))return;const r=await this.readInfoValue(e,"container_iuid"),s=t?await this.readInfoValue(e,"ts_c"):void 0,a=this.containerIuid??r??null,n=t&&s?s:p.nowMicros();await Promise.all([this.writeInfoValue(e,"container_iuid",a),this.writeInfoValue(e,"ts_c",n)]).catch((()=>{}))}readInfoValue(e,t){return new Promise((r=>{try{const s=e.transaction(p.INFO_STORE,"readonly"),a=s.objectStore(p.INFO_STORE).get(t);a.onsuccess=()=>r(a.result),a.onerror=()=>r(void 0)}catch{r(void 0)}}))}writeInfoValue(e,t,r){return new Promise((s=>{try{const a=e.transaction(p.INFO_STORE,"readwrite"),n=a.objectStore(p.INFO_STORE),i=()=>s();a.oncomplete=i,a.onerror=i,a.onabort=i,n.put(r,t)}catch{s()}}))}keyFor(e){return e}static normalizeName(e){if("string"!=typeof e)return null;const t=e.trim();return t||null}async put(e,t){const r=await this.openDb();r&&await new Promise((s=>{const a=r.transaction(p.DATA_STORE,"readwrite");a.objectStore(p.DATA_STORE).put(t,e).onsuccess=()=>s(),a.oncomplete=()=>s(),a.onerror=()=>s(),a.onabort=()=>s()}))}async get(e){const t=await this.openDb();if(t)return new Promise((r=>{const s=t.transaction(p.DATA_STORE,"readonly").objectStore(p.DATA_STORE).get(e);s.onsuccess=()=>r(s.result),s.onerror=()=>r(void 0)}))}async delete(e){const t=await this.openDb();t&&await new Promise((r=>{const s=t.transaction(p.DATA_STORE,"readwrite");s.objectStore(p.DATA_STORE).delete(e).onsuccess=()=>r(),s.oncomplete=()=>r(),s.onerror=()=>r(),s.onabort=()=>r()}))}static async hashName(e){const t="undefined"!=typeof globalThis?globalThis.crypto:void 0;if(!t?.subtle?.digest)throw new Error("CardFS name hashing requires SubtleCrypto support");const r=p.encodeText(e),s=await t.subtle.digest("SHA-256",r);return p.bufferToBase64(s)}static encodeText(e){if("undefined"!=typeof TextEncoder)return(new TextEncoder).encode(e).buffer;const t=new Uint8Array(e.length);for(let r=0;r<e.length;r++)t[r]=e.charCodeAt(r);return t.buffer}static bufferToBase64(e){let t="";const r=new Uint8Array(e);for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return btoa(t)}async getIuidForName(e){const t=p.normalizeName(e);if(!t)return;const r=await this.openDb();if(!r)return;const s=await p.hashName(t);return new Promise((e=>{const t=r.transaction(p.NAME_LOOKUP_STORE,"readonly").objectStore(p.NAME_LOOKUP_STORE).get(s);t.onsuccess=()=>e(t.result),t.onerror=()=>e(void 0)}))}async upsertNameLookup(e,t){const r=p.normalizeName(e);if(!r)return;const s=await this.openDb();if(!s)return;const a=await p.hashName(r);await new Promise((e=>{const r=s.transaction(p.NAME_LOOKUP_STORE,"readwrite"),n=r.objectStore(p.NAME_LOOKUP_STORE);n.put(t,a);n.openCursor().onsuccess=e=>{const r=e.target.result;r&&(r.key!==a&&r.value===t&&r.delete(),r.continue())},r.oncomplete=()=>e(),r.onerror=()=>e(),r.onabort=()=>e()}))}async deleteNameLookupByName(e){const t=p.normalizeName(e);if(!t)return;const r=await this.openDb();if(!r)return;const s=await p.hashName(t);await new Promise((e=>{const t=r.transaction(p.NAME_LOOKUP_STORE,"readwrite");t.objectStore(p.NAME_LOOKUP_STORE).delete(s),t.oncomplete=()=>e(),t.onerror=()=>e(),t.onabort=()=>e()}))}async deleteNameLookupByIuid(e){if(!e)return;const t=await this.openDb();t&&await new Promise((r=>{const s=t.transaction(p.NAME_LOOKUP_STORE,"readwrite");s.objectStore(p.NAME_LOOKUP_STORE).openCursor().onsuccess=t=>{const r=t.target.result;r&&(r.value===e&&r.delete(),r.continue())},s.oncomplete=()=>r(),s.onerror=()=>r(),s.onabort=()=>r()}))}static estimateSize(e){if(null==e)return 0;if("undefined"!=typeof Blob&&e instanceof Blob)return e.size;if(e instanceof ArrayBuffer)return e.byteLength;if(ArrayBuffer.isView(e))return e.byteLength;if("string"==typeof e)return new Blob([e]).size;try{return new Blob([JSON.stringify(e)]).size}catch{return 0}}async cacheDocument(e,t){if(!e?.iuid)return;await this.put(this.keyFor(`${e.iuid}_object`),e);const r=p.normalizeName(e.name);if(r&&await this.upsertNameLookup(r,e.iuid),void 0===t)return void await this.delete(this.keyFor(`${e.iuid}_data`));p.estimateSize(t)<=p.MAX_DATA_BYTES?await this.put(this.keyFor(`${e.iuid}_data`),t):await this.delete(this.keyFor(`${e.iuid}_data`))}async getByName(e){const t=p.normalizeName(e);if(!t)return null;const r=await this.getIuidForName(t);if(!r)return null;const s=await this.get(this.keyFor(`${r}_object`));if(!s)return null;return{iuid:r,object:s,data:await this.get(this.keyFor(`${r}_data`))}}async getByIuid(e){if(!e)return null;const t=await this.get(this.keyFor(`${e}_object`));if(!t)return null;return{iuid:e,object:t,data:await this.get(this.keyFor(`${e}_data`))}}async deleteByName(e){const t=await this.getByName(e);await this.deleteNameLookupByName(e),t&&(await this.delete(this.keyFor(`${t.iuid}_object`)),await this.delete(this.keyFor(`${t.iuid}_data`)))}async deleteByIuid(e,t){t&&await this.deleteNameLookupByName(t),await this.deleteNameLookupByIuid(e),await this.delete(this.keyFor(`${e}_object`)),await this.delete(this.keyFor(`${e}_data`))}async getAllCachedObjects(){const e=await this.openDb();return e?new Promise((t=>{const r=[],s=e.transaction(p.DATA_STORE,"readonly").objectStore(p.DATA_STORE).openCursor();s.onsuccess=e=>{const s=e.target.result;if(!s)return void t(r);const a=s.key;"string"==typeof a&&a.endsWith("_object")&&r.push(s.value),s.continue()},s.onerror=()=>t(r)})):[]}}p.DB_VERSION=2,p.DATA_STORE="cardfs",p.NAME_LOOKUP_STORE="cardfs_name_iuid_lookup",p.INFO_STORE="info",p.STORE_VERSION_KEY="ver",p.MAX_DATA_BYTES=5242880;class m extends c{buildCardFsMessagePayload(e,t){return{...e.name?{name:e.name}:{},...e.iuid?{iuid:e.iuid}:{},...t??{}}}formatFolderPath(e){if("string"!=typeof e)return;let t=e.trim();return t&&(t=t.replace(/^\/+/,""),t)?(t.endsWith("/")||(t+="/"),t):void 0}assertValidReadHandler(e){if(!e||"function"!=typeof e.next)throw new Error("cardFS.read requires a handler with a next() function");return e}invokeReadHandlerNext(e,t){try{e.next(t)}catch(e){console.error("cardFS.read handler next() threw",e)}}invokeReadHandlerError(e,t){if(e.error)try{e.error(t)}catch(e){console.error("cardFS.read handler error() threw",e)}}sanitizeReadData(e,t){if(null==e)return{value:e,stripped:!1};try{if(p.estimateSize(e)>p.MAX_DATA_BYTES){const e=t?` for ${t}`:"";return console.warn(`cardFS.read payload data exceeded ${p.MAX_DATA_BYTES} bytes${e}; omitting data`),{value:void 0,stripped:!0}}}catch(e){console.warn("cardFS.read payload size estimation failed",e)}return{value:e,stripped:!1}}getCardIuid(){return this.dataStore?.iuid||null}ensureCardFsCache(){if(!this.shouldUseCardFsFallback())return null;if(!this.cardFsCache){const e=this.getCardIuid();e&&(this.cardFsCache=new p(e,this.containerIuid))}return this.cardFsCache}supportsParentCardFs(){return!!this.parentCapabilities?.card_fs}shouldUseCardFsFallback(){return!this.supportsParentCardFs()}constructor(){super(),this.handler=null,this.accessToken="",this.devMode=!1,this.permission=null,this.userRole=null,this.fsReadRequests=new Map,this.fsWriteRequests=new Map,this.cardFsCache=null,this.pendingAcks=new Map,this.consoleCaptureInstalled=!1,this.consoleLogHistory=[],this.handleMessage=(e,t)=>{e===i.INIT_ACK&&this.updateParentContext(t?.host,t?.capabilities);const r=t?.messageId;if(r&&this.pendingAcks.has(r)){const{resolve:e,timeout:s}=this.pendingAcks.get(r);return clearTimeout(s),this.pendingAcks.delete(r),void e(t)}if(r||!this.resolvePendingAckByType(e,t)){if(!this.handler)throw new Error("Message handler not found!");switch(e){case i.INIT_ACK:this.dataStore.kw1=t.key_wa1,this.dataStore.iuid=t.iuid,"boolean"==typeof t?.dev_mode&&(this.devMode=t.dev_mode,this.dataStore.dev_mode=t.dev_mode),t.iuid?this.shouldUseCardFsFallback()?this.cardFsCache=new p(t.iuid,this.containerIuid):this.cardFsCache=null:this.shouldUseCardFsFallback()||(this.cardFsCache=null);try{this.cryptoA01.decrypt(this.dataStore.denc,t.key_wa1+this.dataStore.kw2,t.iuid).then((e=>{const r=JSON.parse(e),s=r?.container?.iuid;s&&(this.containerIuid=s,this.shouldUseCardFsFallback()&&this.dataStore?.iuid&&(this.cardFsCache=new p(this.dataStore.iuid,this.containerIuid)));const a="string"==typeof r?.api_token?r.api_token.trim():"";a?this.accessToken=a:console.warn("CardSdk: INIT_ACK payload missing api_token; fallback APIs may fail without auth"),this.permission=r?.perms_v2??null,this.userRole=r?.role??null,this.handler&&this.safeInvoke("onInit",this.handler,{...r,ui:t.ui}),delete this.dataStore.denc})).catch((e=>{throw console.error("Final decrypt error",e),e}))}catch(e){console.error("Decryption failed!",e),this.sendEventError("dec2_failed",e.message)}break;case i.FILE_DATA:{const e=t?.messageId;if(e&&this.fsReadRequests.has(e)){const{messageId:r,...s}=t||{};return void this.handleFsReadDataMessage(e,s)}console.warn("CardSdk: FILE_DATA received but no matching fsRead request",t?.messageId??t?.name);break}case i.DATA_CHANGE:this.safeInvoke("onFileChange",this.handler,t);case i.SAVE_SUCCESS:{const e=t?.messageId;if(e&&this.fsWriteRequests.has(e))return void this.handleFsWriteSuccess(e,t);console.warn("CardSdk: SAVE_SUCCESS received but no matching fsWrite request",t?.messageId??t?.name);break}case i.SAVE_ERROR:{const e=t?.messageId;if(e&&this.fsWriteRequests.has(e))return void this.handleFsWriteError(e,t);console.warn("CardSdk: SAVE_ERROR received but no matching fsWrite request",t?.messageId??t?.name);break}case i.ERROR:{const e=t?.messageId;if(e){const r=t?.message||"Unknown error";if(this.fsReadRequests.has(e))return void this.failFsReadRequest(e,new Error(r));if(this.fsWriteRequests.has(e))return void this.failFsWriteRequest(e,new Error(r))}this.safeInvoke("onError",this.handler,t);break}case i.REFRESH:this.safeInvoke("onRefreshRequest",this.handler,t);break;case i.AF1_DATA_TOKEN_ACK:break;case i.GET_LOGS:this.sendConsoleLogs();break;case i.READ_FILE_ACK:case i.WRITE_FILE_ACK:case i.DELETE_FILE_ACK:case i.LIST_FILES_ACK:break;default:console.warn(`No handler found for message type: ${e}`)}}},this.setupConsoleCapture(),this.cryptoA01=new l,this.cardFS={read:(e,t,r=!0)=>{this.cardsFsRead(e,t,r)},readById:(e,t,r=!0)=>{this.cardsFsReadById(e,t,r)},write:(e,t,r)=>this.cardsFsWrite(e,t,r),writeById:(e,t,r)=>this.cardsFsWriteById(e,t,r),delete:(e,t)=>this.cardsFsDelete(e,t),deleteById:(e,t)=>this.cardsFsDeleteById(e,t),list:e=>this.cardsFsList(e)}}static async init(e,t,r){try{return m.instance||(m.instance=new m,m.instance.setupParentConnection().then((()=>{m.instance.initializeCardSdk(e)})).catch((e=>{console.error(e)})),t&&m.instance.setHandler(t)),m.instance}catch(e){throw console.error("CardSdk: Unrecoverable error in init",e),e}}setHandler(e){this.handler=e}openDeepLink(e){this.emitDeepLink(e)||console.warn("openDeepLink ignored; provide a dome:// or intouchapp:// href")}hasPerm(e){if(!e)return!1;const t=this.userRole?.abbr;if(!t)return!0;if("o"===t)return!0;const r=this.permission?.[t];return"string"==typeof r?!!r.includes("*")||r.includes(e):m.DEFAULT_ROLE_PERMISSIONS.includes(e)}canRead(){return this.hasPerm(h.READ)}canWrite(){return this.hasPerm(h.WRITE)}deriveSecretSeed(e){const t=e.split("").reverse().join("").substring(4,25);if(!t)throw new Error("Cannot decrypt (1)");return t}getCardIuidEnc(e){try{const t=e.pathname.split("/wa/").filter(Boolean);if(t.length<2)return null;return(t[1]?.split("/")[0]??null)||null}catch(e){return console.warn("CardSdk: Failed to extract seed token",e),null}}async requestAf1DataFromParent(e){const t=await this.sendMessageWithAck(o.AF1_DATA_TOKEN,{sdk:{ver:r}},i.AF1_DATA_TOKEN_ACK),{messageId:s,...a}=t||{},{cie:n,ck:c,dev_token:d}=a;if(!n||!c||!d)throw new Error("Invalid AF1 data token payload");const l=await this.fetchAf1Data(n,c,d,e),u=l?.data_af1??l?.data?.data_af1;if(!u)throw new Error("AF1 data fetch returned empty payload");return window.IT_DATA_AF1=u,l?.ver&&(window.IT_VERSION=l.ver),l?.env&&(window.IT_ENV=l.env),{dataAf1:u,cie:n}}async fetchAf1Data(e,t,r,s){if(!(e&&t&&r&&s))throw new Error("Invalid AF1 data fetch parameters");const a=new URL(`https://dome.so/api/v1/cards/e/${encodeURIComponent(e)}/data_af1/`);a.searchParams.set("dt",r),a.searchParams.set("rt",s),a.searchParams.set("ck",t);const n=await fetch(a.toString(),{method:"GET"});if(!n.ok){const e=await n.text().catch((()=>n.statusText));throw new Error(`AF1 data fetch failed: ${n.status} ${e}`)}return n.json()}async initializeCardSdk(e){let t="CardSdk::initializeCardSdk:";try{if(!e)throw new Error("Invalid secret");const r=window.location.href,s=new URL(r),a=s.searchParams.get("rt");if(!a)throw new Error("Missing request token");let n=this.getCardIuidEnc(s),i=window.IT_DATA_AF1;if(!i){const{dataAf1:e,cie:t}=await this.requestAf1DataFromParent(a);i=e,n=t||n}if(!i)throw console.error(t,"No data"),new Error("No data");if(!n)throw new Error("Cannot decrypt (missing seed)");const o=this.deriveSecretSeed(n),c=await this.cryptoA01.decrypt(i,e,o);try{const e=JSON.parse(c);if(!e.ite)throw new Error("Invalid data");this.dataStore={denc:e.d,kw2:e.kw2},m.instance.sendInit(e.ite)}catch(e){throw console.error("Initial Decryption failed (2):",e),e}}catch(e){console.error(t,"Init failed:",e),this.sendEventError("init_failed",e.message)}}async sendInit(e){const t={token:e,sdk:{ver:r}},s=this.getWebappDetails();s&&(t.wa=s),this.sendMessage(o.INIT,t)}getWebappDetails(){if("undefined"==typeof window)return null;let e={};const t=window.IT_VERSION,r=window.IT_ENV;return t&&(e.ver=String(t)),r&&(e.env=String(r)),Object.keys(e).length?e:null}sendMessageWithAck(e,t,r,s=15e3,a){const n=crypto.randomUUID();if(a)try{a(n)}catch(e){return Promise.reject(e)}return new Promise(((a,i)=>{const o=setTimeout((()=>{this.pendingAcks.delete(n),i(new Error(`${r} not received in time`))}),s);this.pendingAcks.set(n,{resolve:a,reject:i,timeout:o,ackType:r}),this.sendMessage(e,{...t,messageId:n})}))}resolvePendingAckByType(e,t){for(const[r,s]of this.pendingAcks.entries())if(s.ackType===e)return clearTimeout(s.timeout),this.pendingAcks.delete(r),s.resolve(t),!0;return!1}cardsFsRead(e,t,r=!0){const s={name:e},a=this.assertValidReadHandler(t);this.cardsFsReadInternal(s,a,r,"read")}cardsFsReadById(e,t,r=!0){const s={iuid:e},a=this.assertValidReadHandler(t);this.cardsFsReadInternal(s,a,r,"readById")}cardsFsReadInternal(e,t,r,s){if(this.shouldUseCardFsFallback())return void this.cardsFsReadFallback(e,void 0,t,r).catch((e=>{const r=e instanceof Error?e:new Error(String(e));this.invokeReadHandlerError(t,r)}));let a=null;const n=e.name?`"${e.name}"`:e.iuid?`iuid ${e.iuid}`:"document";this.sendMessageWithAck(o.READ_FILE,this.buildCardFsMessagePayload(e,{allow_stale:r}),i.READ_FILE_ACK,m.CARD_FS_ACK_TIMEOUT_MS,(e=>{a=e;const i={name:n,handler:t,allowStale:r};i.noResponseTimeout=setTimeout((()=>{this.failFsReadRequest(e,new Error(`cardFS.${s} timed out for ${n}`))}),m.FS_RESPONSE_TIMEOUT_MS),this.fsReadRequests.set(e,i)})).catch((e=>{const r=e instanceof Error?e:new Error(String(e));a?this.failFsReadRequest(a,r):this.invokeReadHandlerError(t,r)}))}cardsFsWrite(e,t,r){const s={name:e};return this.cardsFsWriteInternal(s,t,r,"write")}cardsFsWriteById(e,t,r){const s={iuid:e};return this.cardsFsWriteInternal(s,t,r,"writeById")}cardsFsWriteInternal(e,t,r,s){return this.shouldUseCardFsFallback()?this.cardsFsWriteFallback(e,t).then((e=>{try{r?.(e)}catch(e){console.error(`cardFS.${s} fallback callback threw`,e)}return e})):new Promise(((a,n)=>{let c=null;const d=e.name?`"${e.name}"`:e.iuid?`iuid ${e.iuid}`:"document";this.sendMessageWithAck(o.WRITE_FILE,this.buildCardFsMessagePayload(e,{data:t}),i.WRITE_FILE_ACK,m.CARD_FS_ACK_TIMEOUT_MS,(e=>{c=e;const t={name:d,onResult:r,resolve:a,reject:n,timeout:setTimeout((()=>{this.failFsWriteRequest(e,new Error(`cardFS.${s} timed out for ${d}`))}),m.FS_RESPONSE_TIMEOUT_MS)};this.fsWriteRequests.set(e,t)})).catch((e=>{const t=e instanceof Error?e:new Error(String(e));c?this.failFsWriteRequest(c,t):n(t)}))}))}cardsFsDelete(e,t){const r={name:e};return this.cardsFsDeleteInternal(r,t,"delete")}cardsFsDeleteById(e,t){const r={iuid:e};return this.cardsFsDeleteInternal(r,t,"deleteById")}cardsFsDeleteInternal(e,t,r){return this.shouldUseCardFsFallback()?this.cardsFsDeleteFallback(e).then((e=>{try{t?.(e)}catch(e){console.error(`cardFS.${r} fallback callback threw`,e)}return e})):new Promise(((r,s)=>{this.sendMessageWithAck(o.DELETE_FILE,this.buildCardFsMessagePayload(e),i.DELETE_FILE_ACK,m.CARD_FS_ACK_TIMEOUT_MS).then((e=>{const{messageId:s,...a}=e||{};t?.(a),r(a)})).catch((e=>{s(e instanceof Error?e:new Error(String(e)))}))}))}cardsFsList(e){const t=this.formatFolderPath(e);return this.shouldUseCardFsFallback()?this.cardsFsListFallback(t).then((e=>e)):new Promise(((e,r)=>{const s=t?{name:t}:{};this.sendMessageWithAck(o.LIST_FILES,s,i.LIST_FILES_ACK,m.CARD_FS_ACK_TIMEOUT_MS).then((t=>{const{messageId:r,...s}=t||{};e(s)})).catch((e=>{r(e instanceof Error?e:new Error(String(e)))}))}))}handleFsReadDataMessage(e,t){const r=this.fsReadRequests.get(e);if(!r)return;r.noResponseTimeout&&(clearTimeout(r.noResponseTimeout),r.noResponseTimeout=void 0);const s=this.sanitizeReadData(t?.data,r.name),a=Boolean(t?.from_cache??t?.is_stale),n={name:t?.name,iuid:t?.iuid??null,object:t?.object,data:s.value,is_stale:Boolean(t?.is_stale??t?.from_cache),is_complete:"boolean"==typeof t?.is_complete?t.is_complete:!r.allowStale||!a};this.invokeReadHandlerNext(r.handler,n),r.cleanupTimeout&&clearTimeout(r.cleanupTimeout),r.cleanupTimeout=setTimeout((()=>{this.clearFsReadRequest(e)}),m.FS_READ_RETENTION_MS)}clearFsReadRequest(e){const t=this.fsReadRequests.get(e);if(t)return t.noResponseTimeout&&clearTimeout(t.noResponseTimeout),t.cleanupTimeout&&clearTimeout(t.cleanupTimeout),this.fsReadRequests.delete(e),t}sendConsoleLogs(){const e={entries:this.consoleLogHistory.map((e=>({level:e.level,timestamp:e.timestamp,args:e.args})))};this.sendMessage(o.APP_LOGS,e)}setupConsoleCapture(){if(this.consoleCaptureInstalled||"undefined"==typeof console)return;["log","info","warn","error","debug"].forEach((e=>{const t=console[e];if("function"!=typeof t)return;const r=t.bind(console);console[e]=(...t)=>{try{this.recordConsoleEntry(e,t)}catch{}r(...t)}})),this.consoleCaptureInstalled=!0}recordConsoleEntry(e,t){const r={level:e,timestamp:(new Date).toISOString(),args:t.map((e=>this.serializeConsoleArg(e)))};this.consoleLogHistory.push(r)}serializeConsoleArg(e){if(null===e||"string"==typeof e||"number"==typeof e||"boolean"==typeof e)return e;if(void 0===e)return{type:"undefined"};if("bigint"==typeof e)return{type:"bigint",value:e.toString()};if("symbol"==typeof e)return{type:"symbol",value:e.toString()};if("function"==typeof e)return{type:"function",name:e.name||null};if(e instanceof Error)return{type:"error",name:e.name,message:e.message,stack:e.stack??null};try{return JSON.parse(JSON.stringify(e))}catch{try{return{type:"object",value:String(e)}}catch{return{type:"object",value:"[unserializable]"}}}}failFsReadRequest(e,t){const r=this.clearFsReadRequest(e);r&&this.invokeReadHandlerError(r.handler,t)}clearFsWriteRequest(e){const t=this.fsWriteRequests.get(e);if(t)return clearTimeout(t.timeout),this.fsWriteRequests.delete(e),t}failFsWriteRequest(e,t){const r=this.clearFsWriteRequest(e);r&&r.reject(t)}handleFsWriteSuccess(e,t){const r=this.clearFsWriteRequest(e);if(!r)return;const{messageId:s,...a}=t||{};if("function"==typeof r.onResult)try{r.onResult(a)}catch(e){console.error("cardFS.write callback threw",e)}r.resolve(a)}handleFsWriteError(e,t){const r=this.clearFsWriteRequest(e);if(!r)return;const s=t instanceof Error?t:new Error(t?.message||"Unknown write error");r.reject(s)}async cardsFsReadFallback(e,t,r,s=!0){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.read fallback is disabled because parent supports card_fs");const a=this.getCardIuid();if(!a)throw new Error("cardFS.read fallback failed: card not initialized");const n=this.ensureCardFsCache();let i=null;const o=e.name?`"${e.name}"`:e.iuid?`iuid ${e.iuid}`:"document",c=(s,a,n,o,c,d)=>{if(!s)return null;const l=c??e.name??s?.name??e.iuid??"document",u=d??s?.iuid??e.iuid??null,{value:h}=this.sanitizeReadData(a,l),f={name:l,iuid:u,object:s,data:h,is_stale:n,is_complete:o};return t&&this.fsReadRequests.has(t)?this.handleFsReadDataMessage(t,f):this.invokeReadHandlerNext(r,f),i||(i=f),f};let d=null;if(n)try{if(s){let t=null;e.iuid&&(t=await n.getByIuid(e.iuid)),!t&&e.name&&(t=await n.getByName(e.name)),t?.object&&(d=c(t.object,t.data,!0,!1,t.object?.name??e.name,t.iuid))}}catch(e){console.warn("cardFS.read cache lookup failed",e)}try{const t=await this.fetchDocumentPayload(a,e);t?(await(n?.cacheDocument(t.object,t.data)),c(t.object,t.data,!1,!0,t.object?.name,t.object?.iuid)):s&&d&&c(d.object,d.data,!0,!0,d.name,d.iuid??void 0)}catch(e){if(!i){throw e instanceof Error?e:new Error(String(e))}console.warn("cardFS.read fresh fetch failed",e),s&&d&&c(d.object,d.data,!0,!0,d.name,d.iuid??void 0)}if(!i)throw new Error(`cardFS.read fallback could not locate ${o}`)}async cardsFsWriteFallback(e,t,r){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.write fallback is disabled because parent supports card_fs");const s=this.getCardIuid();if(!s)throw new Error("cardFS.write fallback failed: card not initialized");await this.upsertDocumentViaApi(s,e,t);const a=await this.fetchDocumentPayload(s,e);await(this.ensureCardFsCache()?.cacheDocument(a.object,a.data));const n={name:a.object?.name??e.name??null,iuid:a.object?.iuid??e.iuid??null,object:a.object,data:a.data};return r&&this.fsWriteRequests.has(r)&&this.handleFsWriteSuccess(r,n),n}async cardsFsDeleteFallback(e){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.delete fallback is disabled because parent supports card_fs");const t=this.getCardIuid();if(!t)throw new Error("cardFS.delete fallback failed: card not initialized");const r=this.ensureCardFsCache(),s=await this.fetchDocumentMetadataByTarget(t,e).catch((()=>null)),a=s?.iuid??e.iuid??null,n=s?.name??e.name??null;if(!a)return e.name&&await(r?.deleteByName(e.name)),{name:n,iuid:null,deleted:!1};const i=await fetch(`/api/v1/documents/${a}/`,{method:"DELETE",headers:this.authHeader()});if(!i.ok&&404!==i.status){const e=await i.text().catch((()=>"delete failed"));throw new Error(`cardFS.delete fallback failed: ${e}`)}await(r?.deleteByIuid(a,n??void 0));return{name:n??a,iuid:a,deleted:404!==i.status}}async cardsFsListFallback(e){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.list fallback is disabled because parent supports card_fs");const t=this.getCardIuid();if(!t)throw new Error("cardFS.list fallback failed: card not initialized");const r=this.ensureCardFsCache();try{const s=await this.fetchAllDocuments(t,e);if(r)for(const e of s)await r.cacheDocument(e,void 0);return{documents:s,from_cache:!1}}catch(e){const t=await(r?.getAllCachedObjects());if(t&&t.length>0)return{documents:t,from_cache:!0};throw e}}authHeader(){return this.accessToken?{Authorization:`Bearer ${this.accessToken}`}:{}}async fetchDocumentMetadataByTarget(e,t){return t.iuid?this.fetchDocumentMetadataByIuid(t.iuid):t.name?this.fetchDocumentMetadataByName(e,t.name):null}async fetchDocumentMetadataByIuid(e){const t=await fetch(`/api/v1/documents/${e}/`,{method:"GET",headers:this.authHeader()});if(404===t.status)return null;if(!t.ok){const e=await t.text().catch((()=>"metadata lookup failed"));throw new Error(`cardFS metadata fetch failed: ${e}`)}return t.json().catch((()=>null))}normalizeDocumentsResponse(e){return Array.isArray(e?.results)?e.results:Array.isArray(e)?e:e?[e]:[]}async fetchDocumentMetadataByName(e,t){if(!t)return null;const r=await fetch(`/api/v1/documents/with_card/${e}/?name=${encodeURIComponent(t)}`,{method:"GET",headers:this.authHeader()});if(!r.ok){if(404===r.status)return null;const e=await r.text().catch((()=>"metadata lookup failed"));throw new Error(`cardFS metadata fetch failed: ${e}`)}const s=await r.json().catch((()=>null)),a=this.normalizeDocumentsResponse(s);return a.find((e=>e?.name===t))??a[0]??null}async fetchAllDocuments(e,t){const r=t?`?name=${encodeURIComponent(t)}`:"",s=await fetch(`/api/v1/documents/with_card/${e}/${r}`,{method:"GET",headers:this.authHeader()});if(!s.ok){const e=await s.text().catch((()=>"list failed"));throw new Error(`cardFS.list failed: ${e}`)}const a=await s.json().catch((()=>null));return this.normalizeDocumentsResponse(a)}async fetchDocumentPayload(e,t){const r="string"==typeof t?{name:t}:t,s=await this.fetchDocumentMetadataByTarget(e,r);if(!s){const e=r.name?`"${r.name}"`:r.iuid?`iuid ${r.iuid}`:"document";throw new Error(`cardFS document not found: ${e}`)}return{object:s,data:await this.fetchDocumentData(s)}}async fetchDocumentData(e){if(void 0!==e?.data)return e.data;const t=e?.orig?.url||e?.url?.original||e?.url;if(!t||"string"!=typeof t)return null;const r=await fetch(t);if(!r.ok){const e=await r.text().catch((()=>"download failed"));throw new Error(`cardFS data fetch failed: ${e}`)}const s=r.headers.get("Content-Type")||"";return s.includes("application/json")?r.json():s.startsWith("text/")?r.text():r.arrayBuffer()}async buildFileFormData(e,t){let r;const s=e=>{if(e instanceof ArrayBuffer)return new Blob([e.slice(0)],{type:"application/octet-stream"});if("undefined"!=typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer){const t=new Uint8Array(e),r=new Uint8Array(t.length);return r.set(t),new Blob([r.buffer],{type:"application/octet-stream"})}if(ArrayBuffer.isView(e)){const t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),r=new Uint8Array(t.length);return r.set(t),new Blob([r.buffer],{type:"application/octet-stream"})}const t=new Uint8Array(e),r=new Uint8Array(t.length);return r.set(t),new Blob([r.buffer],{type:"application/octet-stream"})};if(t instanceof File)r=t;else if("undefined"!=typeof Blob&&t instanceof Blob)r=new File([t],e,{type:t.type||"application/octet-stream"});else if("undefined"!=typeof SharedArrayBuffer&&t instanceof SharedArrayBuffer){const a=s(t);r=new File([a],e,{type:a.type})}else if(t instanceof ArrayBuffer){const a=s(t);r=new File([a],e,{type:a.type})}else if(ArrayBuffer.isView(t)){const a=s(t);r=new File([a],e,{type:a.type})}else if("string"==typeof t){const s=new Blob([t],{type:"text/plain"});r=new File([s],e,{type:s.type})}else{const s=JSON.stringify(t??{}),a=new Blob([s],{type:"application/json"});r=new File([a],e,{type:a.type})}const a=new FormData;return a.append("file",r),a}async upsertDocumentViaApi(e,t,r){const s=await this.buildFileFormData(t.name??t.iuid??"document",r);if(t.iuid){const e=await fetch(`/api/v1/documents/${t.iuid}/`,{method:"PUT",headers:this.authHeader(),body:s});if(!e.ok){const t=await e.text().catch((()=>"update failed"));throw new Error(`cardFS.write update failed: ${t}`)}return}const a=t.name?await this.fetchDocumentMetadataByName(e,t.name).catch((()=>null)):null;if(a?.iuid){const e=await fetch(`/api/v1/documents/${a.iuid}/`,{method:"PUT",headers:this.authHeader(),body:s});if(!e.ok){const t=await e.text().catch((()=>"update failed"));throw new Error(`cardFS.write update failed: ${t}`)}return}if(s.append("parent_iuid",e),s.append("attached_to_iuid",e),!t.name)throw new Error('cardFS.write failed: "name" missing');s.append("filename",t.name);const n=await fetch("/api/v1/documents/",{method:"POST",headers:this.authHeader(),body:s});if(!n.ok){const e=await n.text().catch((()=>"upload failed"));throw new Error(`cardFS.write upload failed: ${e}`)}}formatErrorMessage(e){if("string"==typeof e&&e.trim())return e;if(e instanceof Error)return e.message||e.toString();if(e&&"object"==typeof e){if("string"==typeof e.message)return e.message;try{return JSON.stringify(e)}catch{return Object.prototype.toString.call(e)}}return null!=e?String(e):"Unknown error"}sendEventError(e,t,r=void 0){const s={message:this.formatErrorMessage(t),error_code:e,data:r??null};this.handler&&this.safeInvoke("onError",this.handler,s)}setFileDirty(e){this.sendMessage(o.FILE_DIRTY,e)}getUsername(e){const t=e?.name;return t&&Object.values(t).some((e=>e))?function(e){const{prefix:t="",given:r="",middle:s="",family:a="",suffix:n=""}=e||{};return[t,r,s,a,n].filter((e=>e)).join(" ").trim()}(t):""}}return m.FS_RESPONSE_TIMEOUT_MS=3e4,m.FS_READ_RETENTION_MS=5e3,m.CARD_FS_ACK_TIMEOUT_MS=5e3,m.DEFAULT_ROLE_PERMISSIONS="r",t})()));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.domeSdk=t():e.domeSdk=t()}(this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var s in r)e.o(r,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:r[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{CardMessageType:()=>o,CardPermission:()=>h,CardSdk:()=>E,ClientMessageType:()=>i,CryptoA01:()=>l,DomeEmbeddedAppSdk:()=>c,ViewerMessageType:()=>n,ViewerSdk:()=>d,cardPermission:()=>f,getKeyFromBlob:()=>u});const r="0.2.8",s=new Set(["dome","intouchapp"]);var n,a,i,o;!function(e){e.CONNECTION_SUCCESS="CONNECTION_SUCCESS",e.INIT="INIT",e.REQUEST_SAVE="REQUEST_SAVE",e.REQUEST_CLOSE="REQUEST_CLOSE",e.REQUEST_INITIAL_DATA="REQUEST_INITIAL_DATA",e.SET_DIRTY="SET_DIRTY",e.SEND_CLOSE="SEND_CLOSE",e.SEND_EXCEPTION="SEND_EXCEPTION"}(n||(n={})),function(e){e.INIT_MESSAGE_CHANNEL="INIT_MESSAGE_CHANNEL"}(a||(a={})),function(e){e.CONNECT="CONNECT",e.REQUEST_CLOSE="REQUEST_CLOSE",e.REQUEST_SAVE="REQUEST_SAVE",e.SAVE_ERROR="SAVE_ERROR",e.SAVE_SUCCESS="SAVE_SUCCESS",e.DATA_CHANGE="DATA_CHANGE",e.FILE_DATA="FILE_DATA",e.WRITE_FILE_ACK="WRITE_FILE_ACK",e.READ_FILE_ACK="READ_FILE_ACK",e.DELETE_FILE_ACK="DELETE_FILE_ACK",e.LIST_FILES_ACK="LIST_FILES_ACK",e.INIT_ACK="INIT_ACK",e.AF1_DATA_TOKEN_ACK="AF1_DATA_TOKEN_ACK",e.ERROR="ERROR",e.REFRESH="REFRESH",e.LOGGING_ENABLE="LOGGING_ENABLE",e.LOGGING_DISABLE="LOGGING_DISABLE",e.LOGGING_CLEAR="LOGGING_CLEAR",e.LOGGING_GET="LOGGING_GET"}(i||(i={})),function(e){e.APP_READY="APP_READY",e.INIT="INIT",e.READ_FILE="READ_FILE",e.WRITE_FILE="WRITE_FILE",e.DELETE_FILE="DELETE_FILE",e.LIST_FILES="LIST_FILES",e.FILE_DIRTY="FILE_DIRTY",e.OPEN_DEEPLINK="OPEN_DEEPLINK",e.AF1_DATA_TOKEN="AF1_DATA_TOKEN",e.LOGGING_GET_RET="LOGGING_GET_RET"}(o||(o={}));class c{constructor(){this.targetOrigin="*",this.isAppReady=!1,this.port2=null,this.runtimeHost="unknown",this.parentHostDetails=null,this.parentCapabilities=null,this.handleDeepLinkClick=e=>{if("webapp"!==this.runtimeHost)return;if(!(e.target instanceof Element))return;const t=e.target.closest("a[href]");if(!t)return;const r=t.getAttribute("href")??"";this.emitDeepLink(r)&&e.preventDefault()},console.info(`Initializing Dome Embedded App SDK v${r}`),this.detectHost(),this.setupDeepLinkInterception()}detectHost(){void 0!==window.AndroidBridge?this.runtimeHost="android":void 0!==window.webkit?this.runtimeHost="ios":this.runtimeHost="webapp",this.runtimeHost}setupDeepLinkInterception(){"webapp"===this.runtimeHost&&"undefined"!=typeof document&&document.addEventListener("click",this.handleDeepLinkClick,!0)}emitDeepLink(e){if("string"!=typeof e||""===e.trim())return console.warn("emitDeepLink called without a valid href"),!1;const t=e.split(":")[0]?.toLowerCase();return!(!t||!s.has(t))&&(this.sendMessage(o.OPEN_DEEPLINK,{url:e}),!0)}updateParentContext(e,t){if(!e&&!t)return;const r={...this.parentHostDetails??{type:this.runtimeHost},...e??{}};r.type||(r.type=this.runtimeHost);const s=t??this.parentCapabilities??r.capabilities;t&&(this.parentCapabilities=t),s?r.capabilities=s:delete r.capabilities,this.parentHostDetails=r,this.parentCapabilities=s??null,s?console.info("Host capabilities detected",{host_type:r.type,capabilities:s}):console.info("Host capabilities not found",{hostType:r.type})}getHost(){return this.parentHostDetails??{type:this.runtimeHost}}sendMessage(e,t){const r={type:e,data:t??null};switch(this.runtimeHost){case"android":window.AndroidBridge?.sendMessage(JSON.stringify(r));break;case"ios":window?.webkit?.messageHandlers?window.webkit?.messageHandlers.appHandler.postMessage(JSON.stringify(r)):console.error("webkit.messageHandlers not found");break;case"webapp":this.port2?this.port2.postMessage(r):console.error("Web connection is not established.");break;default:console.error("Unsupported host, cannot send message.")}this.runtimeHost}sendAppInit(){this.isAppReady||(this.isAppReady=!0,this.sendMessage(n.INIT,{sdk:{ver:r}}))}safeInvoke(e,t,r){const s=t?.[e];"function"==typeof s?s(r):console.warn(`Handler for '${String(e)}' is not defined.`)}setupParentConnection(){return new Promise(((e,t)=>{switch(this.runtimeHost){case"android":window.receiveFromAndroid=e=>{this.handleMessage(e.type,e.data)},e();break;case"ios":window.receiveFromIOS=e=>{this.handleMessage(e.type,e.data)},e();break;case"webapp":if(this.port2)return console.warn("Connection already established. Skipping reinitialization."),void e();const s=t=>{const{type:r}=t.data||{};r===i.CONNECT&&t.ports&&t.ports.length>0&&(this.port2=t.ports[0],this.port2.onmessage=e=>this.handlePortMessage(e),window.removeEventListener("message",s),this.notifyConnectionSuccess(),e())};window.addEventListener("message",s),window.parent?window.parent.postMessage({type:a.INIT_MESSAGE_CHANNEL,data:{sdk:{ver:r}}},this.targetOrigin):console.error("Parent window not available to initialize message channel.");break;default:console.error("Unknown host."),t("Unknown host")}}))}notifyConnectionSuccess(){this.sendMessage(n.CONNECTION_SUCCESS)}handlePortMessage(e){const{type:t,data:r}=e.data||{};t&&this.handleMessage(t,r)}handleMessage(e,t){throw new Error("Subclasses must implement handleMessage.")}}class d extends c{constructor(){super(),this.handler=null,this.pendingRequests=new Map,this.pendingInitAck=null}static init(e){return d.initialized?(console.warn("ViewerSdk is already initialized. Skipping initialization."),d.instance):(d.instance||(d.instance=new d,d.instance.setupParentConnection().then((()=>{try{d.instance.initializeViewerSdk()}catch(e){console.error("Error in initializeViewerSdk:",e)}})).catch((e=>{console.error("init: Error setting up parent connection!",e),console.trace("called from:")}))),e&&d.instance.setHandler(e),d.initialized=!0,d.instance)}setHandler(e){this.handler=e,this.pendingInitAck&&(this.safeInvoke("onInitialData",this.handler,this.pendingInitAck),this.pendingInitAck=null)}canRead(e){return!!e?.includes("r")}canWrite(e){return!!e?.includes("w")||!!e?.includes("*")}initializeViewerSdk(){this.sendAppInit()}requestInitialData(){this.sendMessage(n.REQUEST_INITIAL_DATA)}requestSave(e,t){const r=function(){if("undefined"!=typeof window&&window.crypto&&window.crypto.randomUUID)return window.crypto.randomUUID();if("undefined"!=typeof crypto&&crypto.randomUUID)return crypto.randomUUID();throw new Error("UUID generation is not supported in this environment")}();return this.sendMessage(n.REQUEST_SAVE,{doc:e,isDataDirty:t,requestId:r}),new Promise(((e,t)=>{this.pendingRequests.set(r,e),this.pendingRequests.set(r+"_reject",t),setTimeout((()=>{this.pendingRequests.has(r)&&(this.pendingRequests.delete(r),this.pendingRequests.delete(r+"_reject"))}),3e4)}))}handleOnSave(e){const{requestId:t,status:r,message:s}=e,n=this.pendingRequests.get(t),a=this.pendingRequests.get(t+"_reject");n&&("error"===r?a?.({status:r,message:s}):n({status:r,message:s}),this.pendingRequests.delete(t),this.pendingRequests.delete(t+"_reject"))}setDirty(e){this.sendMessage(n.SET_DIRTY,e)}sendClose(e,t){this.sendMessage(n.SEND_CLOSE,{doc:e,isDataDirty:t})}sendException(e){this.sendMessage(n.SEND_EXCEPTION,e)}handleMessage(e,t){if(e===i.INIT_ACK&&this.updateParentContext(t?.host,t?.capabilities),this.handler)switch(e){case i.INIT_ACK:this.safeInvoke("onInitialData",this.handler,t);break;case i.DATA_CHANGE:this.safeInvoke("onDataChange",this.handler,t);break;case i.REQUEST_CLOSE:this.safeInvoke("onCloseRequest",this.handler);break;case i.REQUEST_SAVE:this.safeInvoke("onSaveRequest",this.handler);break;case i.SAVE_SUCCESS:case i.SAVE_ERROR:this.handleOnSave(t);break;default:console.warn(`No handler found for message type: ${e}`)}else e===i.INIT_ACK?(console.warn("Handler not set. Storing INIT_ACK message for later processing."),this.pendingInitAck=t):console.error("Message handler not found for type:",e)}}d.initialized=!1;class l{constructor(){if(this.subtleCrypto=window.crypto?.subtle,!this.subtleCrypto)throw new Error("SubtleCrypto API is not available in this environment.")}async decrypt(e,t,r){try{if(!e)throw new Error("Invalid token");const s=this.base64UrlDecode(e);if(128!==s[0])throw new Error("Invalid version");s.slice(1,9);const n=s.slice(9,25),a=s.slice(25,-32),i=s.slice(-32),o=await this.deriveKey(t,r),{hmacKey:c,aesKey:d}=await this.splitKey(o),l=s.slice(0,-32);if(!new Uint8Array(await this.subtleCrypto.sign("HMAC",c,l)).every(((e,t)=>e===i[t])))throw new Error("Invalid HMAC. Token has been tampered with!");const u=await this.subtleCrypto.decrypt({name:"AES-CBC",iv:n},d,a);return(new TextDecoder).decode(u)}catch(e){throw console.log("Error in decrypt:",e),e}}async deriveKey(e,t,r=1e4){const s=new TextEncoder,n=await this.subtleCrypto.importKey("raw",s.encode(e),"PBKDF2",!1,["deriveKey"]);return this.subtleCrypto.deriveKey({name:"PBKDF2",hash:"SHA-256",salt:s.encode(t),iterations:r},n,{name:"AES-CBC",length:256},!0,["encrypt","decrypt"])}async splitKey(e){const t=new Uint8Array(await this.subtleCrypto.exportKey("raw",e));return{hmacKey:await this.subtleCrypto.importKey("raw",t.slice(0,16),{name:"HMAC",hash:"SHA-256"},!1,["sign","verify"]),aesKey:await this.subtleCrypto.importKey("raw",t.slice(16),{name:"AES-CBC"},!1,["encrypt","decrypt"])}}base64UrlDecode(e){const t=e.replace(/-/g,"+").replace(/_/g,"/"),r=atob(t);return new Uint8Array([...r].map((e=>e.charCodeAt(0))))}}function u(e){let t;if(1!==e.v)throw new Error(`Unsupported key blob version: ${e.v}`);return t=function(e){const{seed:t,obf:r}=e,s=new Uint8Array(r.length);for(let e=0;e<r.length;e++)s[e]=r[e]^t+17*e&255;return s}(e),function(e){if("undefined"!=typeof TextDecoder)return(new TextDecoder).decode(e);let t="";for(let r=0;r<e.length;r++)t+=String.fromCharCode(e[r]);return t}(t)}var h;!function(e){e.READ="r",e.WRITE="w",e.FORWARD="f",e.SHARE="s",e.DOWNLOAD="d"}(h||(h={}));const f=h;class p{constructor(e,t){this.cardIuid=e,this.containerIuid=t,this.openPromise=null}isSupported(){return"undefined"!=typeof indexedDB}async openDb(){return this.isSupported()?(this.openPromise||(this.openPromise=new Promise((e=>{this.openWithVersion(p.DB_VERSION,e)}))),this.openPromise):null}openWithVersion(e,t){let r="ensure";const s=indexedDB.open(this.cardIuid,e);s.onupgradeneeded=e=>{r=0===(e.oldVersion||0)?"create":"ensure",this.configureStores(s.result)},s.onsuccess=()=>{const e=s.result;if(!this.hasRequiredStores(e)){const r=e.version+1;return e.close(),void this.openWithVersion(r,t)}this.updateStoresMetadata(e,"ensure"===r).catch((()=>{})).finally((()=>t(e)))},s.onerror=()=>t(null)}configureStores(e){e.objectStoreNames.contains(p.DATA_STORE)||e.createObjectStore(p.DATA_STORE),e.objectStoreNames.contains(p.NAME_LOOKUP_STORE)||e.createObjectStore(p.NAME_LOOKUP_STORE),e.objectStoreNames.contains(p.INFO_STORE)||e.createObjectStore(p.INFO_STORE)}hasRequiredStores(e){const t=e.objectStoreNames;return t.contains(p.DATA_STORE)&&t.contains(p.NAME_LOOKUP_STORE)&&t.contains(p.INFO_STORE)}static nowMicros(){return Math.trunc(1e3*Date.now())}updateStoresMetadata(e,t){return Promise.all([this.ensureStoreVersion(e,p.DATA_STORE),this.ensureStoreVersion(e,p.NAME_LOOKUP_STORE),this.updateCardInfo(e,t)]).then((()=>{}))}ensureStoreVersion(e,t){return e.objectStoreNames.contains(t)?new Promise((r=>{try{const s=e.transaction(t,"readwrite"),n=s.objectStore(t),a=()=>r();s.oncomplete=a,s.onerror=a,s.onabort=a,n.put("1.0.0",p.STORE_VERSION_KEY)}catch{r()}})):Promise.resolve()}async updateCardInfo(e,t){if(!e.objectStoreNames.contains(p.INFO_STORE))return;const r=await this.readInfoValue(e,"container_iuid"),s=t?await this.readInfoValue(e,"ts_c"):void 0,n=this.containerIuid??r??null,a=t&&s?s:p.nowMicros();await Promise.all([this.writeInfoValue(e,"container_iuid",n),this.writeInfoValue(e,"ts_c",a)]).catch((()=>{}))}readInfoValue(e,t){return new Promise((r=>{try{const s=e.transaction(p.INFO_STORE,"readonly"),n=s.objectStore(p.INFO_STORE).get(t);n.onsuccess=()=>r(n.result),n.onerror=()=>r(void 0)}catch{r(void 0)}}))}writeInfoValue(e,t,r){return new Promise((s=>{try{const n=e.transaction(p.INFO_STORE,"readwrite"),a=n.objectStore(p.INFO_STORE),i=()=>s();n.oncomplete=i,n.onerror=i,n.onabort=i,a.put(r,t)}catch{s()}}))}keyFor(e){return e}static normalizeName(e){if("string"!=typeof e)return null;const t=e.trim();return t||null}async put(e,t){const r=await this.openDb();r&&await new Promise((s=>{const n=r.transaction(p.DATA_STORE,"readwrite");n.objectStore(p.DATA_STORE).put(t,e).onsuccess=()=>s(),n.oncomplete=()=>s(),n.onerror=()=>s(),n.onabort=()=>s()}))}async get(e){const t=await this.openDb();if(t)return new Promise((r=>{const s=t.transaction(p.DATA_STORE,"readonly").objectStore(p.DATA_STORE).get(e);s.onsuccess=()=>r(s.result),s.onerror=()=>r(void 0)}))}async delete(e){const t=await this.openDb();t&&await new Promise((r=>{const s=t.transaction(p.DATA_STORE,"readwrite");s.objectStore(p.DATA_STORE).delete(e).onsuccess=()=>r(),s.oncomplete=()=>r(),s.onerror=()=>r(),s.onabort=()=>r()}))}static async hashName(e){const t="undefined"!=typeof globalThis?globalThis.crypto:void 0;if(!t?.subtle?.digest)throw new Error("CardFS name hashing requires SubtleCrypto support");const r=p.encodeText(e),s=await t.subtle.digest("SHA-256",r);return p.bufferToBase64(s)}static encodeText(e){if("undefined"!=typeof TextEncoder)return(new TextEncoder).encode(e).buffer;const t=new Uint8Array(e.length);for(let r=0;r<e.length;r++)t[r]=e.charCodeAt(r);return t.buffer}static bufferToBase64(e){let t="";const r=new Uint8Array(e);for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return btoa(t)}async getIuidForName(e){const t=p.normalizeName(e);if(!t)return;const r=await this.openDb();if(!r)return;const s=await p.hashName(t);return new Promise((e=>{const t=r.transaction(p.NAME_LOOKUP_STORE,"readonly").objectStore(p.NAME_LOOKUP_STORE).get(s);t.onsuccess=()=>e(t.result),t.onerror=()=>e(void 0)}))}async upsertNameLookup(e,t){const r=p.normalizeName(e);if(!r)return;const s=await this.openDb();if(!s)return;const n=await p.hashName(r);await new Promise((e=>{const r=s.transaction(p.NAME_LOOKUP_STORE,"readwrite"),a=r.objectStore(p.NAME_LOOKUP_STORE);a.put(t,n);a.openCursor().onsuccess=e=>{const r=e.target.result;r&&(r.key!==n&&r.value===t&&r.delete(),r.continue())},r.oncomplete=()=>e(),r.onerror=()=>e(),r.onabort=()=>e()}))}async deleteNameLookupByName(e){const t=p.normalizeName(e);if(!t)return;const r=await this.openDb();if(!r)return;const s=await p.hashName(t);await new Promise((e=>{const t=r.transaction(p.NAME_LOOKUP_STORE,"readwrite");t.objectStore(p.NAME_LOOKUP_STORE).delete(s),t.oncomplete=()=>e(),t.onerror=()=>e(),t.onabort=()=>e()}))}async deleteNameLookupByIuid(e){if(!e)return;const t=await this.openDb();t&&await new Promise((r=>{const s=t.transaction(p.NAME_LOOKUP_STORE,"readwrite");s.objectStore(p.NAME_LOOKUP_STORE).openCursor().onsuccess=t=>{const r=t.target.result;r&&(r.value===e&&r.delete(),r.continue())},s.oncomplete=()=>r(),s.onerror=()=>r(),s.onabort=()=>r()}))}static estimateSize(e){if(null==e)return 0;if("undefined"!=typeof Blob&&e instanceof Blob)return e.size;if(e instanceof ArrayBuffer)return e.byteLength;if(ArrayBuffer.isView(e))return e.byteLength;if("string"==typeof e)return new Blob([e]).size;try{return new Blob([JSON.stringify(e)]).size}catch{return 0}}async cacheDocument(e,t){if(!e?.iuid)return;await this.put(this.keyFor(`${e.iuid}_object`),e);const r=p.normalizeName(e.name);if(r&&await this.upsertNameLookup(r,e.iuid),void 0===t)return void await this.delete(this.keyFor(`${e.iuid}_data`));p.estimateSize(t)<=p.MAX_DATA_BYTES?await this.put(this.keyFor(`${e.iuid}_data`),t):await this.delete(this.keyFor(`${e.iuid}_data`))}async getByName(e){const t=p.normalizeName(e);if(!t)return null;const r=await this.getIuidForName(t);if(!r)return null;const s=await this.get(this.keyFor(`${r}_object`));if(!s)return null;return{iuid:r,object:s,data:await this.get(this.keyFor(`${r}_data`))}}async getByIuid(e){if(!e)return null;const t=await this.get(this.keyFor(`${e}_object`));if(!t)return null;return{iuid:e,object:t,data:await this.get(this.keyFor(`${e}_data`))}}async deleteByName(e){const t=await this.getByName(e);await this.deleteNameLookupByName(e),t&&(await this.delete(this.keyFor(`${t.iuid}_object`)),await this.delete(this.keyFor(`${t.iuid}_data`)))}async deleteByIuid(e,t){t&&await this.deleteNameLookupByName(t),await this.deleteNameLookupByIuid(e),await this.delete(this.keyFor(`${e}_object`)),await this.delete(this.keyFor(`${e}_data`))}async getAllCachedObjects(){const e=await this.openDb();return e?new Promise((t=>{const r=[],s=e.transaction(p.DATA_STORE,"readonly").objectStore(p.DATA_STORE).openCursor();s.onsuccess=e=>{const s=e.target.result;if(!s)return void t(r);const n=s.key;"string"==typeof n&&n.endsWith("_object")&&r.push(s.value),s.continue()},s.onerror=()=>t(r)})):[]}}p.DB_VERSION=2,p.DATA_STORE="cardfs",p.NAME_LOOKUP_STORE="cardfs_name_iuid_lookup",p.INFO_STORE="info",p.STORE_VERSION_KEY="ver",p.MAX_DATA_BYTES=5242880;class E extends c{buildCardFsMessagePayload(e,t){return{...e.name?{name:e.name}:{},...e.iuid?{iuid:e.iuid}:{},...t??{}}}formatFolderPath(e){if("string"!=typeof e)return;let t=e.trim();return t&&(t=t.replace(/^\/+/,""),t)?(t.endsWith("/")||(t+="/"),t):void 0}assertValidReadHandler(e){if(!e||"function"!=typeof e.next)throw new Error("cardFS.read requires a handler with a next() function");return e}invokeReadHandlerNext(e,t){try{e.next(t)}catch(e){console.error("cardFS.read handler next() threw",e)}}invokeReadHandlerError(e,t){if(e.error)try{e.error(t)}catch(e){console.error("cardFS.read handler error() threw",e)}}sanitizeReadData(e,t){if(null==e)return{value:e,stripped:!1};try{if(p.estimateSize(e)>p.MAX_DATA_BYTES){const e=t?` for ${t}`:"";return console.warn(`cardFS.read payload data exceeded ${p.MAX_DATA_BYTES} bytes${e}; omitting data`),{value:void 0,stripped:!0}}}catch(e){console.warn("cardFS.read payload size estimation failed",e)}return{value:e,stripped:!1}}getCardIuid(){return this.dataStore?.iuid||null}ensureCardFsCache(){if(!this.shouldUseCardFsFallback())return null;if(!this.cardFsCache){const e=this.getCardIuid();e&&(this.cardFsCache=new p(e,this.containerIuid))}return this.cardFsCache}supportsParentCardFs(){return!!this.parentCapabilities?.card_fs}shouldUseCardFsFallback(){return!this.supportsParentCardFs()}constructor(){super(),this.handler=null,this.accessToken="",this.devMode=!1,this.permission=null,this.userRole=null,this.fsReadRequests=new Map,this.fsWriteRequests=new Map,this.cardFsCache=null,this.pendingAcks=new Map,this.consoleCaptureInstalled=!1,this.consoleCaptureEnabled=!1,this.consoleCaptureMinLevel=null,this.consoleLogBuffer=new Array(E.MAX_CONSOLE_LOG_ENTRIES).fill(null),this.consoleLogWriteIndex=0,this.consoleLogSize=0,this.handleMessage=(e,t)=>{e===i.INIT_ACK&&this.updateParentContext(t?.host,t?.capabilities);const r=t?.messageId;if(r&&this.pendingAcks.has(r)){const{resolve:e,timeout:s}=this.pendingAcks.get(r);return clearTimeout(s),this.pendingAcks.delete(r),void e(t)}if(r||!this.resolvePendingAckByType(e,t)){if(!this.handler)throw new Error("Message handler not found!");switch(e){case i.INIT_ACK:this.applyLoggingConfig(t?.logging),this.dataStore.kw1=t.key_wa1,this.dataStore.iuid=t.iuid,"boolean"==typeof t?.dev_mode&&(this.devMode=t.dev_mode,this.dataStore.dev_mode=t.dev_mode),t.iuid?this.shouldUseCardFsFallback()?this.cardFsCache=new p(t.iuid,this.containerIuid):this.cardFsCache=null:this.shouldUseCardFsFallback()||(this.cardFsCache=null);try{this.cryptoA01.decrypt(this.dataStore.denc,t.key_wa1+this.dataStore.kw2,t.iuid).then((e=>{const r=JSON.parse(e),s=r?.container?.iuid;s&&(this.containerIuid=s,this.shouldUseCardFsFallback()&&this.dataStore?.iuid&&(this.cardFsCache=new p(this.dataStore.iuid,this.containerIuid)));const n="string"==typeof r?.api_token?r.api_token.trim():"";n?this.accessToken=n:console.warn("CardSdk: INIT_ACK payload missing api_token; fallback APIs may fail without auth"),this.permission=r?.perms_v2??null,this.userRole=r?.role??null,this.handler&&this.safeInvoke("onInit",this.handler,{...r,ui:t.ui}),delete this.dataStore.denc})).catch((e=>{throw console.error("Final decrypt error",e),e}))}catch(e){console.error("Decryption failed!",e),this.sendEventError("dec2_failed",e.message)}break;case i.FILE_DATA:{const e=t?.messageId;if(e&&this.fsReadRequests.has(e)){const{messageId:r,...s}=t||{};return void this.handleFsReadDataMessage(e,s)}console.warn("CardSdk: FILE_DATA received but no matching fsRead request",t?.messageId??t?.name);break}case i.DATA_CHANGE:this.safeInvoke("onFileChange",this.handler,t);case i.SAVE_SUCCESS:{const e=t?.messageId;if(e&&this.fsWriteRequests.has(e))return void this.handleFsWriteSuccess(e,t);console.warn("CardSdk: SAVE_SUCCESS received but no matching fsWrite request",t?.messageId??t?.name);break}case i.SAVE_ERROR:{const e=t?.messageId;if(e&&this.fsWriteRequests.has(e))return void this.handleFsWriteError(e,t);console.warn("CardSdk: SAVE_ERROR received but no matching fsWrite request",t?.messageId??t?.name);break}case i.ERROR:{const e=t?.messageId;if(e){const r=t?.message||"Unknown error";if(this.fsReadRequests.has(e))return void this.failFsReadRequest(e,new Error(r));if(this.fsWriteRequests.has(e))return void this.failFsWriteRequest(e,new Error(r))}this.safeInvoke("onError",this.handler,t);break}case i.REFRESH:this.safeInvoke("onRefreshRequest",this.handler,t);break;case i.AF1_DATA_TOKEN_ACK:break;case i.LOGGING_ENABLE:this.applyLoggingConfig({status:"enabled",level:t?.level??t?.logging?.level});break;case i.LOGGING_DISABLE:this.consoleCaptureEnabled=!1;break;case i.LOGGING_CLEAR:this.clearConsoleLogs();break;case i.LOGGING_GET:this.sendConsoleLogs();break;case i.READ_FILE_ACK:case i.WRITE_FILE_ACK:case i.DELETE_FILE_ACK:case i.LIST_FILES_ACK:break;default:console.warn(`No handler found for message type: ${e}`)}}},this.cryptoA01=new l,this.cardFS={read:(e,t,r=!0)=>{this.cardsFsRead(e,t,r)},readById:(e,t,r=!0)=>{this.cardsFsReadById(e,t,r)},write:(e,t,r)=>this.cardsFsWrite(e,t,r),writeById:(e,t,r)=>this.cardsFsWriteById(e,t,r),delete:(e,t)=>this.cardsFsDelete(e,t),deleteById:(e,t)=>this.cardsFsDeleteById(e,t),list:e=>this.cardsFsList(e)}}static async init(e,t,r){try{return E.instance||(E.instance=new E,E.instance.setupParentConnection().then((()=>{E.instance.initializeCardSdk(e)})).catch((e=>{console.error(e)})),t&&E.instance.setHandler(t)),E.instance}catch(e){throw console.error("CardSdk: Unrecoverable error in init",e),e}}setHandler(e){this.handler=e}openDeepLink(e){this.emitDeepLink(e)||console.warn("openDeepLink ignored; provide a dome:// or intouchapp:// href")}hasPerm(e){if(!e)return!1;const t=this.userRole?.abbr;if(!t)return!0;if("o"===t)return!0;const r=this.permission?.[t];return"string"==typeof r?!!r.includes("*")||r.includes(e):E.DEFAULT_ROLE_PERMISSIONS.includes(e)}canRead(){return this.hasPerm(h.READ)}canWrite(){return this.hasPerm(h.WRITE)}deriveSecretSeed(e){const t=e.split("").reverse().join("").substring(4,25);if(!t)throw new Error("Cannot decrypt (1)");return t}getCardIuidEnc(e){try{const t=e.pathname.split("/wa/").filter(Boolean);if(t.length<2)return null;return(t[1]?.split("/")[0]??null)||null}catch(e){return console.warn("CardSdk: Failed to extract seed token",e),null}}async requestAf1DataFromParent(e){const t=await this.sendMessageWithAck(o.AF1_DATA_TOKEN,{sdk:{ver:r}},i.AF1_DATA_TOKEN_ACK),{messageId:s,...n}=t||{},{cie:a,ck:c,dev_token:d}=n;if(!a||!c||!d)throw new Error("Invalid AF1 data token payload");const l=await this.fetchAf1Data(a,c,d,e),u=l?.data_af1??l?.data?.data_af1;if(!u)throw new Error("AF1 data fetch returned empty payload");return window.IT_DATA_AF1=u,l?.ver&&(window.IT_VERSION=l.ver),l?.env&&(window.IT_ENV=l.env),{dataAf1:u,cie:a}}async fetchAf1Data(e,t,r,s){if(!(e&&t&&r&&s))throw new Error("Invalid AF1 data fetch parameters");const n=new URL(`https://dome.so/api/v1/cards/e/${encodeURIComponent(e)}/data_af1/`);n.searchParams.set("dt",r),n.searchParams.set("rt",s),n.searchParams.set("ck",t);const a=await fetch(n.toString(),{method:"GET"});if(!a.ok){const e=await a.text().catch((()=>a.statusText));throw new Error(`AF1 data fetch failed: ${a.status} ${e}`)}return a.json()}async initializeCardSdk(e){let t="CardSdk::initializeCardSdk:";try{if(!e)throw new Error("Invalid secret");const r=window.location.href,s=new URL(r),n=s.searchParams.get("rt");if(!n)throw new Error("Missing request token");let a=this.getCardIuidEnc(s),i=window.IT_DATA_AF1;if(!i){const{dataAf1:e,cie:t}=await this.requestAf1DataFromParent(n);i=e,a=t||a}if(!i)throw console.error(t,"No data"),new Error("No data");if(!a)throw new Error("Cannot decrypt (missing seed)");const o=this.deriveSecretSeed(a),c=await this.cryptoA01.decrypt(i,e,o);try{const e=JSON.parse(c);if(!e.ite)throw new Error("Invalid data");this.dataStore={denc:e.d,kw2:e.kw2},E.instance.sendInit(e.ite)}catch(e){throw console.error("Initial Decryption failed (2):",e),e}}catch(e){console.error(t,"Init failed:",e),this.sendEventError("init_failed",e.message)}}async sendInit(e){const t={token:e,sdk:{ver:r}},s=this.getWebappDetails();s&&(t.wa=s),this.sendMessage(o.INIT,t)}getWebappDetails(){if("undefined"==typeof window)return null;let e={};const t=window.IT_VERSION,r=window.IT_ENV;return t&&(e.ver=String(t)),r&&(e.env=String(r)),Object.keys(e).length?e:null}sendMessageWithAck(e,t,r,s=15e3,n){const a=crypto.randomUUID();if(n)try{n(a)}catch(e){return Promise.reject(e)}return new Promise(((n,i)=>{const o=setTimeout((()=>{this.pendingAcks.delete(a),i(new Error(`${r} not received in time`))}),s);this.pendingAcks.set(a,{resolve:n,reject:i,timeout:o,ackType:r}),this.sendMessage(e,{...t,messageId:a})}))}resolvePendingAckByType(e,t){for(const[r,s]of this.pendingAcks.entries())if(s.ackType===e)return clearTimeout(s.timeout),this.pendingAcks.delete(r),s.resolve(t),!0;return!1}cardsFsRead(e,t,r=!0){const s={name:e},n=this.assertValidReadHandler(t);this.cardsFsReadInternal(s,n,r,"read")}cardsFsReadById(e,t,r=!0){const s={iuid:e},n=this.assertValidReadHandler(t);this.cardsFsReadInternal(s,n,r,"readById")}cardsFsReadInternal(e,t,r,s){if(this.shouldUseCardFsFallback())return void this.cardsFsReadFallback(e,void 0,t,r).catch((e=>{const r=e instanceof Error?e:new Error(String(e));this.invokeReadHandlerError(t,r)}));let n=null;const a=e.name?`"${e.name}"`:e.iuid?`iuid ${e.iuid}`:"document";this.sendMessageWithAck(o.READ_FILE,this.buildCardFsMessagePayload(e,{allow_stale:r}),i.READ_FILE_ACK,E.CARD_FS_ACK_TIMEOUT_MS,(e=>{n=e;const i={name:a,handler:t,allowStale:r};i.noResponseTimeout=setTimeout((()=>{this.failFsReadRequest(e,new Error(`cardFS.${s} timed out for ${a}`))}),E.FS_RESPONSE_TIMEOUT_MS),this.fsReadRequests.set(e,i)})).catch((e=>{const r=e instanceof Error?e:new Error(String(e));n?this.failFsReadRequest(n,r):this.invokeReadHandlerError(t,r)}))}cardsFsWrite(e,t,r){const s={name:e};return this.cardsFsWriteInternal(s,t,r,"write")}cardsFsWriteById(e,t,r){const s={iuid:e};return this.cardsFsWriteInternal(s,t,r,"writeById")}cardsFsWriteInternal(e,t,r,s){return this.shouldUseCardFsFallback()?this.cardsFsWriteFallback(e,t).then((e=>{try{r?.(e)}catch(e){console.error(`cardFS.${s} fallback callback threw`,e)}return e})):new Promise(((n,a)=>{let c=null;const d=e.name?`"${e.name}"`:e.iuid?`iuid ${e.iuid}`:"document";this.sendMessageWithAck(o.WRITE_FILE,this.buildCardFsMessagePayload(e,{data:t}),i.WRITE_FILE_ACK,E.CARD_FS_ACK_TIMEOUT_MS,(e=>{c=e;const t={name:d,onResult:r,resolve:n,reject:a,timeout:setTimeout((()=>{this.failFsWriteRequest(e,new Error(`cardFS.${s} timed out for ${d}`))}),E.FS_RESPONSE_TIMEOUT_MS)};this.fsWriteRequests.set(e,t)})).catch((e=>{const t=e instanceof Error?e:new Error(String(e));c?this.failFsWriteRequest(c,t):a(t)}))}))}cardsFsDelete(e,t){const r={name:e};return this.cardsFsDeleteInternal(r,t,"delete")}cardsFsDeleteById(e,t){const r={iuid:e};return this.cardsFsDeleteInternal(r,t,"deleteById")}cardsFsDeleteInternal(e,t,r){return this.shouldUseCardFsFallback()?this.cardsFsDeleteFallback(e).then((e=>{try{t?.(e)}catch(e){console.error(`cardFS.${r} fallback callback threw`,e)}return e})):new Promise(((r,s)=>{this.sendMessageWithAck(o.DELETE_FILE,this.buildCardFsMessagePayload(e),i.DELETE_FILE_ACK,E.CARD_FS_ACK_TIMEOUT_MS).then((e=>{const{messageId:s,...n}=e||{};t?.(n),r(n)})).catch((e=>{s(e instanceof Error?e:new Error(String(e)))}))}))}cardsFsList(e){const t=this.formatFolderPath(e);return this.shouldUseCardFsFallback()?this.cardsFsListFallback(t).then((e=>e)):new Promise(((e,r)=>{const s=t?{name:t}:{};this.sendMessageWithAck(o.LIST_FILES,s,i.LIST_FILES_ACK,E.CARD_FS_ACK_TIMEOUT_MS).then((t=>{const{messageId:r,...s}=t||{};e(s)})).catch((e=>{r(e instanceof Error?e:new Error(String(e)))}))}))}handleFsReadDataMessage(e,t){const r=this.fsReadRequests.get(e);if(!r)return;r.noResponseTimeout&&(clearTimeout(r.noResponseTimeout),r.noResponseTimeout=void 0);const s=this.sanitizeReadData(t?.data,r.name),n=Boolean(t?.from_cache??t?.is_stale),a={name:t?.name,iuid:t?.iuid??null,object:t?.object,data:s.value,is_stale:Boolean(t?.is_stale??t?.from_cache),is_complete:"boolean"==typeof t?.is_complete?t.is_complete:!r.allowStale||!n};this.invokeReadHandlerNext(r.handler,a),r.cleanupTimeout&&clearTimeout(r.cleanupTimeout),r.cleanupTimeout=setTimeout((()=>{this.clearFsReadRequest(e)}),E.FS_READ_RETENTION_MS)}clearFsReadRequest(e){const t=this.fsReadRequests.get(e);if(t)return t.noResponseTimeout&&clearTimeout(t.noResponseTimeout),t.cleanupTimeout&&clearTimeout(t.cleanupTimeout),this.fsReadRequests.delete(e),t}sendConsoleLogs(){const e={logs:this.getConsoleLogs().map((e=>({l:e.level,ts:e.timestamp,args:e.args}))),ver:"1.0.0"};this.sendMessage(o.LOGGING_GET_RET,e)}setupConsoleCapture(){if(this.consoleCaptureInstalled||"undefined"==typeof console)return;["log","info","warn","error","debug"].forEach((e=>{const t=console[e];if("function"!=typeof t)return;const r=t.bind(console);console[e]=(...t)=>{try{this.recordConsoleEntry(e,t)}catch{}r(...t)}})),this.consoleCaptureInstalled=!0}applyLoggingConfig(e){if(!e)return;"enabled"===e.status?(this.consoleCaptureEnabled=!0,this.setupConsoleCapture()):"disabled"===e.status&&(this.consoleCaptureEnabled=!1);const t="string"==typeof e.level?e.level.toLowerCase():null;t&&this.isConsoleLogLevel(t)&&(this.consoleCaptureMinLevel=t)}isConsoleLogLevel(e){return"log"===e||"info"===e||"warn"===e||"error"===e||"debug"===e}shouldCaptureLevel(e){if(!this.consoleCaptureEnabled)return!1;if(!this.consoleCaptureMinLevel)return!0;const t=["debug","log","info","warn","error"];return t.indexOf(e)>=t.indexOf(this.consoleCaptureMinLevel)}recordConsoleEntry(e,t){if(!this.shouldCaptureLevel(e))return;const r={level:e,timestamp:(new Date).toISOString(),args:t.map((e=>this.serializeConsoleArg(e)))};this.consoleLogBuffer[this.consoleLogWriteIndex]=r,this.consoleLogWriteIndex=(this.consoleLogWriteIndex+1)%E.MAX_CONSOLE_LOG_ENTRIES,this.consoleLogSize=Math.min(this.consoleLogSize+1,E.MAX_CONSOLE_LOG_ENTRIES)}clearConsoleLogs(){this.consoleLogBuffer.fill(null),this.consoleLogWriteIndex=0,this.consoleLogSize=0}getConsoleLogs(){if(0===this.consoleLogSize)return[];if(this.consoleLogSize<E.MAX_CONSOLE_LOG_ENTRIES)return this.consoleLogBuffer.slice(0,this.consoleLogSize).filter((e=>null!==e));const e=[];for(let t=0;t<E.MAX_CONSOLE_LOG_ENTRIES;t+=1){const r=(this.consoleLogWriteIndex+t)%E.MAX_CONSOLE_LOG_ENTRIES,s=this.consoleLogBuffer[r];s&&e.push(s)}return e}serializeConsoleArg(e){if(null===e||"string"==typeof e||"number"==typeof e||"boolean"==typeof e)return this.truncateSerializedArg(e);if(void 0===e)return this.truncateSerializedArg({type:"undefined"});if("bigint"==typeof e)return this.truncateSerializedArg({type:"bigint",value:e.toString()});if("symbol"==typeof e)return this.truncateSerializedArg({type:"symbol",value:e.toString()});if("function"==typeof e)return this.truncateSerializedArg({type:"function",name:e.name||null});if(e instanceof Error)return this.truncateSerializedArg({type:"error",name:e.name,message:e.message,stack:e.stack??null});try{return this.truncateSerializedArg(JSON.parse(JSON.stringify(e)))}catch{try{return this.truncateSerializedArg({type:"object",value:String(e)})}catch{return this.truncateSerializedArg({type:"object",value:"[unserializable]"})}}}truncateSerializedArg(e){try{const t=JSON.stringify(e);if("string"==typeof t&&t.length>E.MAX_CONSOLE_ARG_CHARS)return{type:"truncated",preview:t.slice(0,E.MAX_CONSOLE_ARG_CHARS),original_length:t.length}}catch{}return e}failFsReadRequest(e,t){const r=this.clearFsReadRequest(e);r&&this.invokeReadHandlerError(r.handler,t)}clearFsWriteRequest(e){const t=this.fsWriteRequests.get(e);if(t)return clearTimeout(t.timeout),this.fsWriteRequests.delete(e),t}failFsWriteRequest(e,t){const r=this.clearFsWriteRequest(e);r&&r.reject(t)}handleFsWriteSuccess(e,t){const r=this.clearFsWriteRequest(e);if(!r)return;const{messageId:s,...n}=t||{};if("function"==typeof r.onResult)try{r.onResult(n)}catch(e){console.error("cardFS.write callback threw",e)}r.resolve(n)}handleFsWriteError(e,t){const r=this.clearFsWriteRequest(e);if(!r)return;const s=t instanceof Error?t:new Error(t?.message||"Unknown write error");r.reject(s)}async cardsFsReadFallback(e,t,r,s=!0){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.read fallback is disabled because parent supports card_fs");const n=this.getCardIuid();if(!n)throw new Error("cardFS.read fallback failed: card not initialized");const a=this.ensureCardFsCache();let i=null;const o=e.name?`"${e.name}"`:e.iuid?`iuid ${e.iuid}`:"document",c=(s,n,a,o,c,d)=>{if(!s)return null;const l=c??e.name??s?.name??e.iuid??"document",u=d??s?.iuid??e.iuid??null,{value:h}=this.sanitizeReadData(n,l),f={name:l,iuid:u,object:s,data:h,is_stale:a,is_complete:o};return t&&this.fsReadRequests.has(t)?this.handleFsReadDataMessage(t,f):this.invokeReadHandlerNext(r,f),i||(i=f),f};let d=null;if(a)try{if(s){let t=null;e.iuid&&(t=await a.getByIuid(e.iuid)),!t&&e.name&&(t=await a.getByName(e.name)),t?.object&&(d=c(t.object,t.data,!0,!1,t.object?.name??e.name,t.iuid))}}catch(e){console.warn("cardFS.read cache lookup failed",e)}try{const t=await this.fetchDocumentPayload(n,e);t?(await(a?.cacheDocument(t.object,t.data)),c(t.object,t.data,!1,!0,t.object?.name,t.object?.iuid)):s&&d&&c(d.object,d.data,!0,!0,d.name,d.iuid??void 0)}catch(e){if(!i){throw e instanceof Error?e:new Error(String(e))}console.warn("cardFS.read fresh fetch failed",e),s&&d&&c(d.object,d.data,!0,!0,d.name,d.iuid??void 0)}if(!i)throw new Error(`cardFS.read fallback could not locate ${o}`)}async cardsFsWriteFallback(e,t,r){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.write fallback is disabled because parent supports card_fs");const s=this.getCardIuid();if(!s)throw new Error("cardFS.write fallback failed: card not initialized");await this.upsertDocumentViaApi(s,e,t);const n=await this.fetchDocumentPayload(s,e);await(this.ensureCardFsCache()?.cacheDocument(n.object,n.data));const a={name:n.object?.name??e.name??null,iuid:n.object?.iuid??e.iuid??null,object:n.object,data:n.data};return r&&this.fsWriteRequests.has(r)&&this.handleFsWriteSuccess(r,a),a}async cardsFsDeleteFallback(e){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.delete fallback is disabled because parent supports card_fs");const t=this.getCardIuid();if(!t)throw new Error("cardFS.delete fallback failed: card not initialized");const r=this.ensureCardFsCache(),s=await this.fetchDocumentMetadataByTarget(t,e).catch((()=>null)),n=s?.iuid??e.iuid??null,a=s?.name??e.name??null;if(!n)return e.name&&await(r?.deleteByName(e.name)),{name:a,iuid:null,deleted:!1};const i=await fetch(`/api/v1/documents/${n}/`,{method:"DELETE",headers:this.authHeader()});if(!i.ok&&404!==i.status){const e=await i.text().catch((()=>"delete failed"));throw new Error(`cardFS.delete fallback failed: ${e}`)}await(r?.deleteByIuid(n,a??void 0));return{name:a??n,iuid:n,deleted:404!==i.status}}async cardsFsListFallback(e){if(!this.shouldUseCardFsFallback())throw new Error("cardFS.list fallback is disabled because parent supports card_fs");const t=this.getCardIuid();if(!t)throw new Error("cardFS.list fallback failed: card not initialized");const r=this.ensureCardFsCache();try{const s=await this.fetchAllDocuments(t,e);if(r)for(const e of s)await r.cacheDocument(e,void 0);return{documents:s,from_cache:!1}}catch(e){const t=await(r?.getAllCachedObjects());if(t&&t.length>0)return{documents:t,from_cache:!0};throw e}}authHeader(){return this.accessToken?{Authorization:`Bearer ${this.accessToken}`}:{}}async fetchDocumentMetadataByTarget(e,t){return t.iuid?this.fetchDocumentMetadataByIuid(t.iuid):t.name?this.fetchDocumentMetadataByName(e,t.name):null}async fetchDocumentMetadataByIuid(e){const t=await fetch(`/api/v1/documents/${e}/`,{method:"GET",headers:this.authHeader()});if(404===t.status)return null;if(!t.ok){const e=await t.text().catch((()=>"metadata lookup failed"));throw new Error(`cardFS metadata fetch failed: ${e}`)}return t.json().catch((()=>null))}normalizeDocumentsResponse(e){return Array.isArray(e?.results)?e.results:Array.isArray(e)?e:e?[e]:[]}async fetchDocumentMetadataByName(e,t){if(!t)return null;const r=await fetch(`/api/v1/documents/with_card/${e}/?name=${encodeURIComponent(t)}`,{method:"GET",headers:this.authHeader()});if(!r.ok){if(404===r.status)return null;const e=await r.text().catch((()=>"metadata lookup failed"));throw new Error(`cardFS metadata fetch failed: ${e}`)}const s=await r.json().catch((()=>null)),n=this.normalizeDocumentsResponse(s);return n.find((e=>e?.name===t))??n[0]??null}async fetchAllDocuments(e,t){const r=t?`?name=${encodeURIComponent(t)}`:"",s=await fetch(`/api/v1/documents/with_card/${e}/${r}`,{method:"GET",headers:this.authHeader()});if(!s.ok){const e=await s.text().catch((()=>"list failed"));throw new Error(`cardFS.list failed: ${e}`)}const n=await s.json().catch((()=>null));return this.normalizeDocumentsResponse(n)}async fetchDocumentPayload(e,t){const r="string"==typeof t?{name:t}:t,s=await this.fetchDocumentMetadataByTarget(e,r);if(!s){const e=r.name?`"${r.name}"`:r.iuid?`iuid ${r.iuid}`:"document";throw new Error(`cardFS document not found: ${e}`)}return{object:s,data:await this.fetchDocumentData(s)}}async fetchDocumentData(e){if(void 0!==e?.data)return e.data;const t=e?.orig?.url||e?.url?.original||e?.url;if(!t||"string"!=typeof t)return null;const r=await fetch(t);if(!r.ok){const e=await r.text().catch((()=>"download failed"));throw new Error(`cardFS data fetch failed: ${e}`)}const s=r.headers.get("Content-Type")||"";return s.includes("application/json")?r.json():s.startsWith("text/")?r.text():r.arrayBuffer()}async buildFileFormData(e,t){let r;const s=e=>{if(e instanceof ArrayBuffer)return new Blob([e.slice(0)],{type:"application/octet-stream"});if("undefined"!=typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer){const t=new Uint8Array(e),r=new Uint8Array(t.length);return r.set(t),new Blob([r.buffer],{type:"application/octet-stream"})}if(ArrayBuffer.isView(e)){const t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),r=new Uint8Array(t.length);return r.set(t),new Blob([r.buffer],{type:"application/octet-stream"})}const t=new Uint8Array(e),r=new Uint8Array(t.length);return r.set(t),new Blob([r.buffer],{type:"application/octet-stream"})};if(t instanceof File)r=t;else if("undefined"!=typeof Blob&&t instanceof Blob)r=new File([t],e,{type:t.type||"application/octet-stream"});else if("undefined"!=typeof SharedArrayBuffer&&t instanceof SharedArrayBuffer){const n=s(t);r=new File([n],e,{type:n.type})}else if(t instanceof ArrayBuffer){const n=s(t);r=new File([n],e,{type:n.type})}else if(ArrayBuffer.isView(t)){const n=s(t);r=new File([n],e,{type:n.type})}else if("string"==typeof t){const s=new Blob([t],{type:"text/plain"});r=new File([s],e,{type:s.type})}else{const s=JSON.stringify(t??{}),n=new Blob([s],{type:"application/json"});r=new File([n],e,{type:n.type})}const n=new FormData;return n.append("file",r),n}async upsertDocumentViaApi(e,t,r){const s=await this.buildFileFormData(t.name??t.iuid??"document",r);if(t.iuid){const e=await fetch(`/api/v1/documents/${t.iuid}/`,{method:"PUT",headers:this.authHeader(),body:s});if(!e.ok){const t=await e.text().catch((()=>"update failed"));throw new Error(`cardFS.write update failed: ${t}`)}return}const n=t.name?await this.fetchDocumentMetadataByName(e,t.name).catch((()=>null)):null;if(n?.iuid){const e=await fetch(`/api/v1/documents/${n.iuid}/`,{method:"PUT",headers:this.authHeader(),body:s});if(!e.ok){const t=await e.text().catch((()=>"update failed"));throw new Error(`cardFS.write update failed: ${t}`)}return}if(s.append("parent_iuid",e),s.append("attached_to_iuid",e),!t.name)throw new Error('cardFS.write failed: "name" missing');s.append("filename",t.name);const a=await fetch("/api/v1/documents/",{method:"POST",headers:this.authHeader(),body:s});if(!a.ok){const e=await a.text().catch((()=>"upload failed"));throw new Error(`cardFS.write upload failed: ${e}`)}}formatErrorMessage(e){if("string"==typeof e&&e.trim())return e;if(e instanceof Error)return e.message||e.toString();if(e&&"object"==typeof e){if("string"==typeof e.message)return e.message;try{return JSON.stringify(e)}catch{return Object.prototype.toString.call(e)}}return null!=e?String(e):"Unknown error"}sendEventError(e,t,r=void 0){const s={message:this.formatErrorMessage(t),error_code:e,data:r??null};this.handler&&this.safeInvoke("onError",this.handler,s)}setFileDirty(e){this.sendMessage(o.FILE_DIRTY,e)}getUsername(e){const t=e?.name;return t&&Object.values(t).some((e=>e))?function(e){const{prefix:t="",given:r="",middle:s="",family:n="",suffix:a=""}=e||{};return[t,r,s,n,a].filter((e=>e)).join(" ").trim()}(t):""}}return E.FS_RESPONSE_TIMEOUT_MS=3e4,E.FS_READ_RETENTION_MS=5e3,E.CARD_FS_ACK_TIMEOUT_MS=5e3,E.MAX_CONSOLE_ARG_CHARS=1e3,E.MAX_CONSOLE_LOG_ENTRIES=100,E.DEFAULT_ROLE_PERMISSIONS="r",t})()));
|
|
2
2
|
//# sourceMappingURL=index.js.map
|