@spiffcommerce/core 21.11.0-alpha.4 → 21.11.0-alpha.5
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/index.js +1 -1
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -503,7 +503,7 @@ class sa {
|
|
|
503
503
|
});
|
|
504
504
|
if (!((s = (i = n.data) == null ? void 0 : i.assetRemoveBackground) != null && s.key))
|
|
505
505
|
throw new Error("Failed to remove background from image");
|
|
506
|
-
return ae.add(n.data.assetRemoveBackground), K.add(t, n.data.assetRemoveBackground.key), n.data.assetRemoveBackground;
|
|
506
|
+
return ae.add(n.data.assetRemoveBackground), K.add(t, n.data.assetRemoveBackground.key), this.bgrmCache.delete(t), n.data.assetRemoveBackground;
|
|
507
507
|
})();
|
|
508
508
|
return this.bgrmCache.set(t, a), a;
|
|
509
509
|
}
|
package/dist/index.umd.cjs
CHANGED
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
...AssetFields
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
`,wt=async c=>(await N.getShadowGraphqlClient().query({query:MA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{keys:c}})).data.assets,PA=async c=>(await N.getShadowGraphqlClient().query({query:yA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{ids:c}})).data.materials,xA=async(c,e,t,A,n)=>{var i;return(i=(await N.getShadowGraphqlClient().mutate({mutation:SA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{name:c,type:e,mimeType:t,anonymous:A,temporary:n}})).data)==null?void 0:i.assetCreate};class NA{constructor(){this.cache=new Map,this.bgrmCache=new Map,this.materialCache=new Map,this.loadImageAsFileInfo=async e=>{const t=await e.arrayBuffer(),A=await l.getAttributesFromArrayBuffer(t),n=(a,i)=>{const o=atob(a.split(",")[1]),s=[];for(let r=0;r<o.length;r++)s.push(o.charCodeAt(r));return new Blob([new Uint8Array(s)],{type:i})};return{name:e.name.substring(e.name.lastIndexOf("/")+1),blob:n(A.dataUrl,e.type)}}}async getLocalOrFromServer(e){if(this.cache.has(e)){const n=this.cache.get(e);if(!n)throw new k("Failed to get asset from cache!");return n}const A=(async()=>(await wt([e]))[0])();return this.cache.set(e,A),A}keyFromURL(e){let t;try{t=new URL(e).pathname}catch{t=void 0}return t!==void 0&&t.startsWith("/")&&(t=t.replace("/","")),t==null?void 0:t.split("?")[0]}cacheAsset(e){if(!e.key)throw new k("Asset has no key!");this.cache.has(e.key)||this.cache.set(e.key,Promise.resolve(e))}cacheMaterial(e){if(!e.id)throw new k("Material has no id!");this.materialCache.has(e.id)||this.materialCache.set(e.id,Promise.resolve(e))}async getMaterialLocalOrFromServer(e){if(this.materialCache.has(e))return this.materialCache.get(e);const A=(async()=>(await PA([e]))[0])();return this.materialCache.set(e,A),A}async uploadAssetWithProgress(e,t,A,n,a){const i=await this.dispatchCreateAssetRequest(e,t,n,a);if(!i)throw new Error("Failed to create asset.");return await new Promise((o,s)=>{const r=new XMLHttpRequest;r.open("PUT",i.assetResponse.uploadUrl,!0),r.setRequestHeader("Content-Type",i.mimeType),r.setRequestHeader("Cache-Control","public,max-age=31536000,immutable"),r.upload.onprogress=g=>{g.lengthComputable&&A(g.loaded*100/g.total)},r.onload=()=>{const g=i.assetResponse.asset;_.add(i.assetResponse.asset),o(g)},r.onerror=s,r.send(e.blob)}),this.cacheAsset(i.assetResponse.asset),i.assetResponse.asset}async uploadAsset(e,t,A,n){return this.uploadAssetWithProgress(e,t,()=>{},A,n)}async uploadFile(e,t){const A=!(e.type==="image/svg+xml"||e.type==="application/pdf"),n=A?l.AssetType.Image:l.AssetType.Illustration;if(A){const a=await this.loadImageAsFileInfo(e);return await this.uploadAssetWithProgress(a,n,t,!0)}else{const a={name:e.name,blob:new Blob([e],{type:e.type})};return await this.uploadAssetWithProgress(a,n,t,!0)}}async removeBackgroundFromAsset(e){const t=e.key;if(this.bgrmCache.has(t))return this.bgrmCache.get(t);if(O.has(t)){const a=this.getLocalOrFromServer(O.get(t));return this.bgrmCache.set(t,a),a}const n=(async()=>{var i,o;const a=await N.getShadowGraphqlClient().mutate({mutation:YA,fetchPolicy:"no-cache",errorPolicy:"all",variables:{key:t}});if(!((o=(i=a.data)==null?void 0:i.assetRemoveBackground)!=null&&o.key))throw new Error("Failed to remove background from image");return _.add(a.data.assetRemoveBackground),O.add(t,a.data.assetRemoveBackground.key),a.data.assetRemoveBackground})();return this.bgrmCache.set(t,n),n}removePersistedAsset(e){_.remove(e),O.delete(e),O.deleteForBgRemovedKey(e)}getPersistedAssets(){return _.list()}registerPersistedAssetListener(e){_.addCallback(e)}unRegisterPersistedAssetListener(e){_.removeCallback(e)}async dispatchCreateAssetRequest(e,t,A,n){const a=e.blob.type?e.blob.type:this.guessMIME(e.name),i=await xA(e.name,t,a,A,n);if(i)return{assetResponse:i,mimeType:a}}guessMIME(e){const t=e.split(".").pop();switch(t){case"glb":return"model/gltf-binary";case"ttf":return"font/ttf";case"mkv":return"video/x-matroska";default:throw new k("Unexpected mimetype: "+t)}}}const de="persistentAssets",ht=class q{static add(e){if(!e.fileLink){console.error("Failed to find cdn link on asset, cannot persist!");return}const t=new Map;t.set(e.key||"",e.fileLink);const A=Y.getMap(de);A&&A.forEach((n,a)=>{t.set(a,n)}),Y.setMap(de,t),q.executeCallbacks()}static remove(e){const t=Y.getMap(de);if(!t)return;const A=Array.from(t.entries()).find(n=>n[0]===e);A&&(t.delete(A[0]),Y.setMap(de,t),q.executeCallbacks())}static list(){const e=Y.getMap(de);return e?Array.from(e.entries()).map(t=>({assetKey:t[0],src:t[1]})):[]}static addCallback(e){q.callbacks.push(e)}static removeCallback(e){q.callbacks=q.callbacks.filter(t=>t!==e)}static executeCallbacks(){q.callbacks.forEach(e=>e())}};ht.callbacks=[];let _=ht;const Et="bgrmPersistentAssets";class O{static has(e){return O.getMap().has(e)}static get(e){return O.getMap().get(e)}static keys(){return O.getMap().keys()}static values(){return O.getMap().values()}static add(e,t){const A=O.getMap();A.set(e,t),O.setMap(A)}static delete(e){const t=O.getMap();t.delete(e),O.setMap(t)}static deleteForBgRemovedKey(e){const t=O.getMap(),A=Array.from(t.keys()).find(n=>t.get(n)===e);A&&(t.delete(A),O.setMap(t))}static getMap(){return Y.getMap(Et)||new Map}static setMap(e){Y.setMap(Et,e)}}const G=new NA;class HA{constructor(){this.cache={},this.disabled=!1}get(e){if(this.disabled)return;const t=JSON.stringify(e);return this.cache[t]}set(e,t){if(this.disabled)return t;const A=JSON.stringify(e);return this.cache[A]=t,t}disable(e){this.disabled=e}}const ee=new HA,Ct=M.gql`
|
|
105
|
+
`,wt=async c=>(await N.getShadowGraphqlClient().query({query:MA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{keys:c}})).data.assets,PA=async c=>(await N.getShadowGraphqlClient().query({query:yA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{ids:c}})).data.materials,xA=async(c,e,t,A,n)=>{var i;return(i=(await N.getShadowGraphqlClient().mutate({mutation:SA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{name:c,type:e,mimeType:t,anonymous:A,temporary:n}})).data)==null?void 0:i.assetCreate};class NA{constructor(){this.cache=new Map,this.bgrmCache=new Map,this.materialCache=new Map,this.loadImageAsFileInfo=async e=>{const t=await e.arrayBuffer(),A=await l.getAttributesFromArrayBuffer(t),n=(a,i)=>{const o=atob(a.split(",")[1]),s=[];for(let r=0;r<o.length;r++)s.push(o.charCodeAt(r));return new Blob([new Uint8Array(s)],{type:i})};return{name:e.name.substring(e.name.lastIndexOf("/")+1),blob:n(A.dataUrl,e.type)}}}async getLocalOrFromServer(e){if(this.cache.has(e)){const n=this.cache.get(e);if(!n)throw new k("Failed to get asset from cache!");return n}const A=(async()=>(await wt([e]))[0])();return this.cache.set(e,A),A}keyFromURL(e){let t;try{t=new URL(e).pathname}catch{t=void 0}return t!==void 0&&t.startsWith("/")&&(t=t.replace("/","")),t==null?void 0:t.split("?")[0]}cacheAsset(e){if(!e.key)throw new k("Asset has no key!");this.cache.has(e.key)||this.cache.set(e.key,Promise.resolve(e))}cacheMaterial(e){if(!e.id)throw new k("Material has no id!");this.materialCache.has(e.id)||this.materialCache.set(e.id,Promise.resolve(e))}async getMaterialLocalOrFromServer(e){if(this.materialCache.has(e))return this.materialCache.get(e);const A=(async()=>(await PA([e]))[0])();return this.materialCache.set(e,A),A}async uploadAssetWithProgress(e,t,A,n,a){const i=await this.dispatchCreateAssetRequest(e,t,n,a);if(!i)throw new Error("Failed to create asset.");return await new Promise((o,s)=>{const r=new XMLHttpRequest;r.open("PUT",i.assetResponse.uploadUrl,!0),r.setRequestHeader("Content-Type",i.mimeType),r.setRequestHeader("Cache-Control","public,max-age=31536000,immutable"),r.upload.onprogress=g=>{g.lengthComputable&&A(g.loaded*100/g.total)},r.onload=()=>{const g=i.assetResponse.asset;_.add(i.assetResponse.asset),o(g)},r.onerror=s,r.send(e.blob)}),this.cacheAsset(i.assetResponse.asset),i.assetResponse.asset}async uploadAsset(e,t,A,n){return this.uploadAssetWithProgress(e,t,()=>{},A,n)}async uploadFile(e,t){const A=!(e.type==="image/svg+xml"||e.type==="application/pdf"),n=A?l.AssetType.Image:l.AssetType.Illustration;if(A){const a=await this.loadImageAsFileInfo(e);return await this.uploadAssetWithProgress(a,n,t,!0)}else{const a={name:e.name,blob:new Blob([e],{type:e.type})};return await this.uploadAssetWithProgress(a,n,t,!0)}}async removeBackgroundFromAsset(e){const t=e.key;if(this.bgrmCache.has(t))return this.bgrmCache.get(t);if(O.has(t)){const a=this.getLocalOrFromServer(O.get(t));return this.bgrmCache.set(t,a),a}const n=(async()=>{var i,o;const a=await N.getShadowGraphqlClient().mutate({mutation:YA,fetchPolicy:"no-cache",errorPolicy:"all",variables:{key:t}});if(!((o=(i=a.data)==null?void 0:i.assetRemoveBackground)!=null&&o.key))throw new Error("Failed to remove background from image");return _.add(a.data.assetRemoveBackground),O.add(t,a.data.assetRemoveBackground.key),this.bgrmCache.delete(t),a.data.assetRemoveBackground})();return this.bgrmCache.set(t,n),n}removePersistedAsset(e){_.remove(e),O.delete(e),O.deleteForBgRemovedKey(e)}getPersistedAssets(){return _.list()}registerPersistedAssetListener(e){_.addCallback(e)}unRegisterPersistedAssetListener(e){_.removeCallback(e)}async dispatchCreateAssetRequest(e,t,A,n){const a=e.blob.type?e.blob.type:this.guessMIME(e.name),i=await xA(e.name,t,a,A,n);if(i)return{assetResponse:i,mimeType:a}}guessMIME(e){const t=e.split(".").pop();switch(t){case"glb":return"model/gltf-binary";case"ttf":return"font/ttf";case"mkv":return"video/x-matroska";default:throw new k("Unexpected mimetype: "+t)}}}const de="persistentAssets",ht=class q{static add(e){if(!e.fileLink){console.error("Failed to find cdn link on asset, cannot persist!");return}const t=new Map;t.set(e.key||"",e.fileLink);const A=Y.getMap(de);A&&A.forEach((n,a)=>{t.set(a,n)}),Y.setMap(de,t),q.executeCallbacks()}static remove(e){const t=Y.getMap(de);if(!t)return;const A=Array.from(t.entries()).find(n=>n[0]===e);A&&(t.delete(A[0]),Y.setMap(de,t),q.executeCallbacks())}static list(){const e=Y.getMap(de);return e?Array.from(e.entries()).map(t=>({assetKey:t[0],src:t[1]})):[]}static addCallback(e){q.callbacks.push(e)}static removeCallback(e){q.callbacks=q.callbacks.filter(t=>t!==e)}static executeCallbacks(){q.callbacks.forEach(e=>e())}};ht.callbacks=[];let _=ht;const Et="bgrmPersistentAssets";class O{static has(e){return O.getMap().has(e)}static get(e){return O.getMap().get(e)}static keys(){return O.getMap().keys()}static values(){return O.getMap().values()}static add(e,t){const A=O.getMap();A.set(e,t),O.setMap(A)}static delete(e){const t=O.getMap();t.delete(e),O.setMap(t)}static deleteForBgRemovedKey(e){const t=O.getMap(),A=Array.from(t.keys()).find(n=>t.get(n)===e);A&&(t.delete(A),O.setMap(t))}static getMap(){return Y.getMap(Et)||new Map}static setMap(e){Y.setMap(Et,e)}}const G=new NA;class HA{constructor(){this.cache={},this.disabled=!1}get(e){if(this.disabled)return;const t=JSON.stringify(e);return this.cache[t]}set(e,t){if(this.disabled)return t;const A=JSON.stringify(e);return this.cache[A]=t,t}disable(e){this.disabled=e}}const ee=new HA,Ct=M.gql`
|
|
106
106
|
fragment OptionFields on Option {
|
|
107
107
|
id
|
|
108
108
|
name
|
package/package.json
CHANGED