@learncard/chapi-plugin 1.0.3 → 1.0.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/chapi-plugin.cjs.development.js +38 -108
- package/dist/chapi-plugin.cjs.development.js.map +4 -4
- package/dist/chapi-plugin.cjs.production.min.js +3 -3
- package/dist/chapi-plugin.cjs.production.min.js.map +4 -4
- package/dist/chapi-plugin.esm.js +38 -107
- package/dist/chapi-plugin.esm.js.map +4 -4
- package/package.json +4 -4
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
var W=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var re=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var o=(i,e)=>W(i,"name",{value:e,configurable:!0});var se=(i,e)=>{for(var t in e)W(i,t,{get:e[t],enumerable:!0})},ae=(i,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of re(e))!oe.call(i,r)&&r!==t&&W(i,r,{get:()=>e[r],enumerable:!(n=ne(e,r))||n.enumerable});return i};var ce=i=>ae(W({},"__esModule",{value:!0}),i);var xe={};se(xe,{getCHAPIPlugin:()=>ie});module.exports=ce(xe);var S={ParseError:{message:"Parse error",code:-32700},InvalidRequest:{message:"Invalid Request",code:-32600},MethodNotFound:{message:"Method not found",code:-32601},InvalidParams:{message:"Invalid params",code:-32602},InternalError:{message:"Internal Error",code:-32603},ServerError:{message:"Server error",code:-32e3}};function f(i,e){if(e===void 0&&(e=window.location.href),typeof URL=="function")return new URL(i,e);if(typeof i!="string")throw new TypeError('"url" must be a string.');i.includes(":")||(e.startsWith("http")&&!i.startsWith("/")?i=e+"/"+i:i=e+i);let t=document.createElement("a");t.href=i;let n=(t.protocol||window.location.protocol)+"//";t.host?t.protocol==="http:"&&t.port==="80"||t.protocol==="https:"&&t.port==="443"?n+=t.hostname:n+=t.host:n+=window.location.host;let r=t.pathname;return r.startsWith("/")||(r="/"+r),{host:t.host||window.location.host,hostname:t.hostname,origin:n,protocol:t.protocol,pathname:r}}o(f,"parseUrl");function z(i,e){for(e=i="";i++<36;e+=i*51&52?(i^15?8^Math.random()*(i^20?16:4):4).toString(16):"-");return e}o(z,"uuidv4");function F(i){return i&&typeof i=="object"&&i.jsonrpc==="2.0"&&i.id&&typeof i.id=="string"}o(F,"isValidMessage");function de(i){return F(i)&&Array.isArray(i.params)}o(de,"isValidRequest");function le(i){return F(i)&&!!("result"in i^"error"in i)&&(!("error"in i)||he(i.error))}o(le,"isValidResponse");function he(i){return i&&typeof i=="object"&&typeof i.code=="number"&&typeof i.message=="string"}o(he,"isValidError");function K(i){let e={message:i.message};return i.constructor.name!=="Error"&&(e.constructor=i.constructor.name),"name"in i&&(e.name=i.name),"code"in i?e.code=i.code:e.code=S.ServerError.code,"details"in i&&(e.details=i.details),e}o(K,"serializeError");function X(i){let e;return i.constructor==="DOMException"?e=new DOMException(i.message,i.name):(e=new Error(i.message),"code"in i&&(e.code=i.code)),i.details&&(e.details=i.details),e}o(X,"deserializeError");function D({listener:i,origin:e,handle:t,expectRequest:n}){if(b(t)){let r=t;t=!1,r.then(s=>t=s)}return r=>{!(r.source===t&&r.origin===e&&(n&&de(r.data)||!n&&le(r.data)))||i(r.data,r)}}o(D,"createMessageListener");function Y(i){let[e,...t]=i.split("."),n=t.pop();return e=[e,...t].join("."),{name:e,method:n}}o(Y,"destructureMethodName");function b(i){try{return typeof i.then=="function"}catch{}return!1}o(b,"isHandlePromise");var pe=3e4,m=class{constructor(){this.origin=null,this._handle=null,this._listener=null,this._pending=new Map}async connect(e,t){if(this._listener)throw new Error("Already connected.");t=t||{};let n=this;n.origin=f(e).origin,n._handle=t.handle||window.opener||window.parent;let r=n._pending;return n._listener=D({origin:n.origin,handle:n._handle,expectRequest:!1,listener:s=>{if(!r.has(s.id))return;let{resolve:d,reject:a,cancelTimeout:c}=r.get(s.id);if(c(),"result"in s)return d(s.result);a(X(s.error))}}),window.addEventListener("message",n._listener),new q(n)}async send(e,t,{timeout:n=pe}){if(!this._listener)throw new Error("RPC client not connected.");let r=this,s={jsonrpc:"2.0",id:z(),method:e,params:t};return b(r._handle)?(await r._handle).postMessage(s,r.origin):r._handle.postMessage(s,r.origin),new Promise((d,a)=>{let c=r._pending,l;if(n>0){let h=setTimeout(()=>{c.delete(s.id),a(new Error("RPC call timed out."))},n);l=o(()=>{c.delete(s.id),clearTimeout(h)},"cancelTimeout")}else l=o(()=>{c.delete(s.id)},"cancelTimeout");c.set(s.id,{resolve:d,reject:a,cancelTimeout:l})})}close(){if(this._listener){window.removeEventListener("message",this._listener),this._handle=this.origin=this._listener=null;for(let e of this._pending.values())e.reject(new Error("RPC client closed."));this._pending=new Map}}};o(m,"Client");var q=class{constructor(e){this.client=e,this._apis=new Map}define(e,t){if(!(e&&typeof e=="string"))throw new TypeError("`name` must be a non-empty string.");if(!(t&&typeof t=="object"&&Array.isArray(t.functions)))throw new TypeError("`definition.function` must be an array of function names or function definition objects to be defined.");let n=this,r={};return t.functions.forEach(s=>{typeof s=="string"&&(s={name:s,options:{}}),r[s.name]=async function(){return n.client.send(e+"."+s.name,[...arguments],s.options)}}),n._apis[e]=r,r}get(e,t){if(!this._apis[e]){if(t)return this.define(e,t);throw new Error(`API "${e}" has not been defined.`)}return this._apis[e]}};o(q,"Injector");var x=class{constructor({deserialize:e=o(n=>n,"deserialize"),waitUntil:t=o(async()=>{},"waitUntil")}={}){this._listeners=[],this._deserialize=e,this._waitUntil=t}async emit(e){return e=this._deserialize(e),(this._listeners[e.type]||[]).forEach(t=>t(e)),this._waitUntil(e)}addEventListener(e,t){this._listeners[e]?this._listeners[e].push(t):this._listeners[e]=[t]}removeEventListener(e,t){let n=this._listeners[e];if(!n)return;let r=n.indexOf(t);r!==-1&&n.splice(r,1)}};o(x,"EventEmitter");var g=class{constructor(){this.origin=null,this._handle=null,this._apis=new Map}define(e,t){if(!(e&&typeof e=="string"))throw new TypeError("`name` must be a non-empty string.");if(!(t&&t!=="object"))throw new TypeError("`api` must be an object.");if(e in this._apis)throw new Error(`The "${e}" API is already defined.`);this._apis[e]=t}async listen(e,t){if(this._listener)throw new Error("Already listening.");t=t||{};let n=this;n.origin=f(e).origin,n._handle=t.handle||window.opener||window.parent;let r=t.ignoreUnknownApi==="true"||!1;n._listener=D({origin:n.origin,handle:n._handle,expectRequest:!0,listener:s=>{let{name:d,method:a}=Y(s.method),c=n._apis[d];if(a&&a.startsWith("_"))return G(n._handle,n.origin,s);if(!c&&r)return;if(!c||typeof c[a]!="function")return G(n._handle,n.origin,s);let l=c[a];(async()=>{let h={jsonrpc:"2.0",id:s.id};try{h.result=await l.apply(c,s.params)}catch(p){h.error=K(p)}n._handle&&(b(n._handle)?n._handle.then(p=>p.postMessage(h,n.origin)):n._handle.postMessage(h,n.origin))})()}}),window.addEventListener("message",n._listener)}close(){this._listener&&(window.removeEventListener("message",this._listener),this._handle=this.origin=this._listener=null)}};o(g,"Server");function G(i,e,t){let n={jsonrpc:"2.0",id:t.id,error:Object.assign({},S.MethodNotFound)};return b(i)?i.then(r=>r.postMessage(n,e)):i.postMessage(n,e)}o(G,"sendMethodNotFound");var y=class{constructor(e){this.relyingOrigin=f(e).origin,this.client=null,this.injector=null,this.client=new m,this.server=new g,this._control=null,this._connected=!1}async connect(){return this.injector=await this.client.connect(this.relyingOrigin),this._connected=!0,this._control=this.injector.define("core.control",{functions:["ready","show","hide"]}),this.server.listen(this.relyingOrigin),this.injector}async ready(){if(!this._connected)throw new Error('WebApp not connected. Did you call ".connect()"?');return await this._control.ready(),this}close(){this._connected&&(this.server.close(),this.client.close(),this._connected=!1)}async show(){if(!this._connected)throw new Error('Cannot "show" yet; not connected. Did you call ".connect()"?');return this._control.show()}async hide(){if(!this._connected)throw new Error('Cannot "hide" yet; not connected. Did you call ".connect()?"');return this._control.hide()}};o(y,"WebApp");var E=class{constructor(){this._closeEventListeners=new Set}addEventListener(e,t){if(e!=="close")throw new Error(`Unknown event "${e}".`);if(typeof t!="function")throw new TypeError('"listener" must be a function.');this._closeEventListeners.add(t)}removeEventListener(e,t){if(e!=="close")throw new Error(`Unknown event "${e}".`);if(typeof t!="function")throw new TypeError('"listener" must be a function.');this._closeEventListeners.delete(t)}show(){}close(){for(let e of this._closeEventListeners)e({})}destroy(){this._closeEventListeners.clear()}};o(E,"WebAppWindowDialog");var T=class extends E{constructor({url:e,handle:t,className:n}){super(),this.url=e,this.handle=t,this.dialog=document.createElement("dialog"),V(this.dialog,{position:"fixed",top:0,left:0,width:"100%",height:"100%","max-width":"100%","max-height":"100%",display:"none",margin:0,padding:0,border:"none",background:"transparent",color:"black","box-sizing":"border-box",overflow:"hidden","user-select":"none","z-index":1e6}),this.dialog.className="web-app-window",typeof n=="string"&&(this.dialog.className=this.dialog.className+" "+n);let r=document.createElement("style");r.appendChild(document.createTextNode(`dialog.web-app-window::backdrop {
|
2
2
|
background-color: transparent;
|
3
|
-
}`)),this.container=document.createElement("div"),V(this.container,{position:"relative",width:"100%",height:"100%",margin:0,padding:0,display:"flex","flex-direction":"column"}),this.container.className="web-app-window-backdrop",this.iframe=document.createElement("iframe"),this.iframe.src=e,this.iframe.scrolling="auto",V(this.iframe,{position:"fixed",top:0,left:0,width:"100%",height:"100%",border:"none",background:"transparent",overflow:"hidden",margin:0,padding:0,"flex-grow":1,"user-select":"none"}),this.dialog.appendChild(r),this.container.appendChild(this.iframe),this.dialog.appendChild(this.container),this.dialog.addEventListener("cancel",s=>{s.preventDefault(),this.hide()}),document.body.appendChild(this.dialog),this.handle=this.iframe.contentWindow}show(){this.dialog.style.display="block",this.dialog.showModal&&this.dialog.showModal();try{this.dialog.style.pointerEvents="none"}catch{}setTimeout(()=>{try{this.dialog.style.pointerEvents=""}catch{}},32)}close(){if(this.dialog.style.display="none",this.dialog.close)try{this.dialog.close()}catch(e){console.error(e)}super.close()}destroy(){this.dialog.parentNode.removeChild(this.dialog),super.destroy()}};o(T,"WebAppWindowInlineDialog");function V(i,e){for(let t in e)i.style[t]=e[t]}o(V,"applyStyle");var P=class extends E{constructor({url:e,handle:t,bounds:n={width:500,height:400}}){super(),this.url=e,this.handle=t,this._locationChanging=!1,t||this._openWindow({url:e,name:"web-app-window",bounds:n}),this.destroyed=!1,this._removeListeners=()=>{}}show(){}close(){this.destroy()}destroy(){this.handle&&!this.destroyed&&(this.handle.close(),super.close(),this.handle=null,this.destroyed=!0,this._removeListeners(),super.destroy())}isClosed(){return!this.handle||this.handle.closed}_openWindow({url:e,name:t,bounds:n}){let{x:r,y:s}=n,{width:d=500,height:a=400}=n;d=Math.min(d,window.innerWidth),a=Math.min(a,window.innerHeight);let c=Math.floor(r!==void 0?r:window.screenX+(window.innerWidth-d)/2),l=Math.floor(s!==void 0?s:window.screenY+(window.innerHeight-a)/2),h=`popup=yes,menubar=no,location=no,resizable=no,scrollbars=no,status=no,width=${d},height=${a},left=${c},top=${l}`;this._locationChanging=!0,this.handle=window.open(e,t,h),this._addListeners()}setLocation(e){this.url=e,this._locationChanging=!0,this.handle.location.replace(e)}_addListeners(){let e=o(()=>this.destroy(),"destroyDialog"),t=o(()=>{this._locationChanging=!1},"loadDialog"),n=o(()=>{this._locationChanging||this.destroy()},"unloadDialog");this.handle.addEventListener("unload",n),this.handle.addEventListener("load",t),window.addEventListener("beforeUnload",e,{once:!0});let r=setInterval(()=>{this.isClosed()&&(this.destroy(),clearInterval(r))},250);this._removeListeners=()=>{clearInterval(r),this.handle.removeListener("unload",n),this.handle.removeListener("load",t),window.removeEventListener("beforeUnload",e)}}};o(P,"WebAppWindowPopupDialog");var ue=6e4,u=class{constructor(e,{timeout:t=ue,dialog:n=null,handle:r,popup:s=!1,className:d=null,customize:a=null,bounds:c}={}){if(this.visible=!1,this.dialog=n,this.handle=null,this.popup=s,this.windowControl=null,this._destroyed=!1,this._ready=!1,this._private={},this._timeoutId=null,r&&r._dialog&&(this.dialog=n=r._dialog),this._private._readyPromise=new Promise((l,h)=>{this._timeoutId=setTimeout(()=>h(new DOMException("Loading Web application window timed out.","TimeoutError")),t),this._private._resolveReady=p=>{clearTimeout(this.timeoutId),this._timeoutId=null,l(p)},this._private._rejectReady=p=>{clearTimeout(this.timeoutId),this._timeoutId=null,h(p)}}),this._private.isReady=async()=>this._private._readyPromise,this._private.destroy=()=>{this._timeoutId&&this._private._rejectReady(new DOMException("Web application window closed before ready.","AbortError")),this._destroyed||(this.dialog.destroy(),this.dialog=null,this._destroyed=!0)},a&&!typeof a==="function")throw new TypeError("`options.customize` must be a function.");if(this.dialog||(this.popup?this.dialog=new P({url:e,handle:r,bounds:c}):this.dialog=new T({url:e,handle:r,className:d})),this.popup&&c){let{x:l,y:h,width:p=500,height:j=400}=c;p=Math.min(p,window.innerWidth),j=Math.min(j+30,window.innerHeight),l=Math.floor(l!==void 0?l:window.screenX+(window.innerWidth-p)/2),h=Math.floor(h!==void 0?h:window.screenY+(window.innerHeight-j)/2+15),this.dialog.handle.resizeTo(p,j),this.dialog.handle.moveTo(l,h)}if(this.handle=this.dialog.handle,a)try{a({dialog:this.dialog.dialog,container:this.dialog.container,iframe:this.dialog.iframe,webAppWindow:this})}catch(l){console.error(l)}}ready(){this._ready=!0,this._private._resolveReady(!0)}show(){if(!this.visible){this.visible=!0;let e=document.querySelector("body");this._bodyOverflowStyle=e.style.overflow,e.style.overflow="hidden",this._destroyed?this.windowControl.show&&this.windowControl.show():this.dialog.show()}}hide(){if(this.visible){this.visible=!1;let e=document.querySelector("body");this._bodyOverflowStyle?e.style.overflow=this._bodyOverflowStyle:e.style.overflow="",this._destroyed?this.windowControl.hide&&this.windowControl.hide():this.dialog.close()}}};o(u,"WebAppWindow");var we=1e4,_=class{constructor(){this.client=new m,this.server=new g,this.injector=null,this.control=null,this.loaded=!1,this.closed=!1}async createWindow(e,{timeout:t=we,iframe:n,dialog:r=null,popup:s=!1,handle:d,windowControl:a,className:c,customize:l,bounds:h}={}){if(this.loaded)throw new Error("AppContext already loaded.");this.loaded=!0,this.control=new u(e,{timeout:t,dialog:r,iframe:n,popup:s,handle:d,windowControl:a,className:c,customize:l,bounds:h}),window.addEventListener("pagehide",()=>this.close(),{once:!0}),this.server.define("core.control",this.control);let p=f(e).origin;return this.server.listen(p,{handle:this.control.handle,ignoreUnknownApi:!0}),await this.control._private.isReady(),this.injector=await this.client.connect(p,{handle:this.control.handle}),this.injector}close(){this.closed||(this.closed=!0,this.control._private.destroy(),this.server.close(),this.client.close())}};o(_,"WebAppContext");var M=class{constructor({credentialHandler:e,credentialRequestOrigin:t,credentialRequestOptions:n,hintKey:r}){this.type="credentialrequest",this._credentialHandler=e,this.credentialRequestOrigin=t,this.credentialRequestOptions=n,this.hintKey=r}async openWindow(e){await this._credentialHandler.show();let t=new u(e,{className:"credential-handler"});return t.ready(),t.show(),t.handle._dialog=t.dialog,t.handle}respondWith(e){this._promise=e}};o(M,"CredentialRequestEvent");var I=class{constructor({credentialHandler:e,credentialRequestOrigin:t,credential:n,hintKey:r}){this.type="credentialstore",this._credentialHandler=e,this.credentialRequestOrigin=t,this.credential=n,this.hintKey=r}async openWindow(e){await this._credentialHandler.show();let t=new u(e);return t.ready(),t.show(),t.handle._dialog=t.dialog,t.handle}respondWith(e){this._promise=e}};o(I,"CredentialStoreEvent");var O=class{constructor(e){this._credentialHandler=e}async request(e){return await this._credentialHandler._emitter.emit(new M(Object.assign({credentialHandler:this._credentialHandler},e)))}async store(e){return await this._credentialHandler._emitter.emit(new I(Object.assign({credentialHandler:this._credentialHandler},e)))}};o(O,"CredentialHandlerService");var Q=["credentialrequest","credentialstore"],R=class extends y{constructor(e,t=!1){if(typeof e!="string")throw new TypeError('"mediatorOrigin" must be a string.');super(e,t),this._emitter=new x({async waitUntil(n){return n._promise||Promise.reject(new DOMException('No "credentialrequest" event handler found.',"NotFoundError"))}})}async connect(){let e=await super.connect();return this.server.define("credentialHandler",new O(this)),await this.ready(),e}addEventListener(e,t){if(!Q.includes(e))throw new DOMException(`Unsupported event type "${e}"`,"NotSupportedError");return this._emitter.addEventListener(e,t)}removeEventListener(e,t){if(!Q.includes(e))throw new DOMException(`Unsupported event type "${e}"`,"NotSupportedError");return this._emitter.removeEventListener(e,t)}};o(R,"CredentialHandler");var L=class{constructor(e,t){let n=t.get("credentialHints",{functions:["delete","get","keys","has","set","clear"]});for(let r in n)if(r!=="set"){let s=n[r].bind(this,e);this[r]=function(...d){return this._deprecateNotice(),s(...d)}}this._remoteSet=n.set.bind(this,e)}async set(e,t){this._deprecateNotice(),t.icons=t.icons||[];let n=t.icons.map(r=>fe(r.src).then(s=>{r.fetchedImage=s}));return await Promise.all(n),this._remoteSet(e,t)}_deprecateNotice(){console.warn("Credential hints are deprecated and no longer used.")}};o(L,"CredentialHints");function fe(i){return new Promise(e=>{let t=new Image;t.crossOrigin="Anonymous",t.onload=()=>{let n=document.createElement("canvas"),r=n.getContext("2d");n.height=t.height,n.width=t.width,r.drawImage(t,0,0);let s=n.toDataURL();e(s),n=null},t.onerror=()=>e(null),t.src=i})}o(fe,"imageToDataUrl");var v=class{constructor(e,t){if(!(e&&typeof e=="string"))throw new TypeError('"url" must be a non-empty string.');this.hints=new L(e,t)}static async requestPermission(){return(await navigator.credentialsPolyfill.permissions.request({name:"credentialhandler"})).state}};o(v,"CredentialManager");var C=class{constructor(e,t){if(!(e&&typeof e=="string"))throw new TypeError('"url" must be a non-empty string.');this.credentialManager=new v(e,t)}};o(C,"CredentialHandlerRegistration");var A=class{constructor(e){this._init=(async()=>{this._injector=await e,this._remote=this._injector.get("credentialHandlers",{functions:["register","unregister","getRegistration","hasRegistration"]})})()}async register(e){return this._deprecateNotice(),await this._init,e=await this._remote.register("credential",e),new C(e,this._injector)}async unregister(e){return this._deprecateNotice(),await this._init,this._remote.unregister("credential",e)}async getRegistration(e){return this._deprecateNotice(),await this._init,e=await this._remote.getRegistration("credential",e),e?new C(e,this._injector):null}async hasRegistration(e){return this._deprecateNotice(),await this._init,await this._remote.hasRegistration("credential",e)}_deprecateNotice(){console.warn('Credential handler registration APIs are deprecated. The credential handler specified in "manifest.json" is now automatically registered when a user grants permission to install a credential handler via "CredentialManager.requestPermission()".')}};o(A,"CredentialHandlers");var w=class{constructor(e,t,{recommendedHandlerOrigins:n=[]}={}){if(typeof e!="string")throw new TypeError('"dataType" must be a string.');this.type="web",this.dataType=e,this.data=t,this.options={recommendedHandlerOrigins:n}}};o(w,"WebCredential");var me=0,ge=0,N=class{constructor(e){this._nativeCredentialsContainer={get:navigator.credentials&&navigator.credentials.get&&navigator.credentials.get.bind(navigator.credentials),store:navigator.credentials&&navigator.credentials.store&&navigator.credentials.store.bind(navigator.credentials)},this._init=(async()=>{this._remote=(await e).get("credentialsContainer",{functions:[{name:"get",options:{timeout:me}},{name:"store",options:{timeout:ge}}]})})()}async get(e={}){if(e.web){await this._init;let t=await this._remote.get(e);return t?new w(t.dataType,t.data):null}if(this._nativeCredentialsContainer.get)return this._nativeCredentialsContainer.get(e);throw new DOMException("Not implemented.","NotSupportedError")}async store(e){if(e instanceof w){await this._init;let t=await this._remote.store(e);return t?new w(t.dataType,t.data):null}if(this._nativeCredentialsContainer.store)return this._nativeCredentialsContainer.store(e);throw new DOMException("Not implemented.","NotSupportedError")}};o(N,"CredentialsContainer");var H=class{constructor(e){this._init=(async()=>{this._remote=(await e).get("permissionManager",{functions:["query",{name:"request",options:{timeout:0}},"revoke"]})})()}async query(e){return await this._init,await this._remote.query(e)}async request(e){return await this._init,await this._remote.request(e)}async revoke(e){return await this._init,await this._remote.revoke(e)}};o(H,"PermissionManager");var ye="https://authn.io";var $;async function B(i){return $||($=!0,_e(i))}o(B,"loadOnce");async function _e(i={mediatorOrigin:ye}){ve();let e;if(typeof i=="string")e=i;else if(i&&typeof i=="object"&&typeof i.mediatorOrigin=="string")e=`${i.mediatorOrigin}/mediator`;else throw new Error('"options.mediatorOrigin" must be a string expressing the origin of the mediator.');let n=new _().createWindow(e,{className:"credential-mediator",timeout:3e4}),r=document.createElement("style");r.appendChild(document.createTextNode(`dialog.web-app-window.credential-mediator > .web-app-window-backdrop {
|
3
|
+
}`)),this.container=document.createElement("div"),V(this.container,{position:"relative",width:"100%",height:"100%",margin:0,padding:0,display:"flex","flex-direction":"column"}),this.container.className="web-app-window-backdrop",this.iframe=document.createElement("iframe"),this.iframe.src=e,this.iframe.scrolling="auto",V(this.iframe,{position:"fixed",top:0,left:0,width:"100%",height:"100%",border:"none",background:"transparent",overflow:"hidden",margin:0,padding:0,"flex-grow":1,"user-select":"none"}),this.dialog.appendChild(r),this.container.appendChild(this.iframe),this.dialog.appendChild(this.container),this.dialog.addEventListener("cancel",s=>{s.preventDefault(),this.hide()}),document.body.appendChild(this.dialog),this.handle=this.iframe.contentWindow}show(){this.dialog.style.display="block",this.dialog.showModal&&this.dialog.showModal();try{this.dialog.style.pointerEvents="none"}catch{}setTimeout(()=>{try{this.dialog.style.pointerEvents=""}catch{}},32)}close(){if(this.dialog.style.display="none",this.dialog.close)try{this.dialog.close()}catch(e){console.error(e)}super.close()}destroy(){this.dialog.parentNode.removeChild(this.dialog),super.destroy()}};o(T,"WebAppWindowInlineDialog");function V(i,e){for(let t in e)i.style[t]=e[t]}o(V,"applyStyle");var P=class extends E{constructor({url:e,handle:t,bounds:n={width:500,height:400}}){super(),this.url=e,this.handle=t,this._locationChanging=!1,t||this._openWindow({url:e,name:"web-app-window",bounds:n}),this.destroyed=!1,this._removeListeners=()=>{}}show(){}close(){this.destroy()}destroy(){this.handle&&!this.destroyed&&(this.handle.close(),super.close(),this.handle=null,this.destroyed=!0,this._removeListeners(),super.destroy())}isClosed(){return!this.handle||this.handle.closed}_openWindow({url:e,name:t,bounds:n}){let{x:r,y:s}=n,{width:d=500,height:a=400}=n;d=Math.min(d,window.innerWidth),a=Math.min(a,window.innerHeight);let c=Math.floor(r!==void 0?r:window.screenX+(window.innerWidth-d)/2),l=Math.floor(s!==void 0?s:window.screenY+(window.innerHeight-a)/2),h=`popup=yes,menubar=no,location=no,resizable=no,scrollbars=no,status=no,width=${d},height=${a},left=${c},top=${l}`;this._locationChanging=!0,this.handle=window.open(e,t,h),this._addListeners()}setLocation(e){this.url=e,this._locationChanging=!0,this.handle.location.replace(e)}_addListeners(){let e=o(()=>this.destroy(),"destroyDialog"),t=o(()=>{this._locationChanging=!1},"loadDialog"),n=o(()=>{this._locationChanging||this.destroy()},"unloadDialog");this.handle.addEventListener("unload",n),this.handle.addEventListener("load",t),window.addEventListener("beforeUnload",e,{once:!0});let r=setInterval(()=>{this.isClosed()&&(this.destroy(),clearInterval(r))},250);this._removeListeners=()=>{clearInterval(r),this.handle.removeListener("unload",n),this.handle.removeListener("load",t),window.removeEventListener("beforeUnload",e)}}};o(P,"WebAppWindowPopupDialog");var ue=6e4,u=class{constructor(e,{timeout:t=ue,dialog:n=null,handle:r,popup:s=!1,className:d=null,customize:a=null,bounds:c}={}){if(this.visible=!1,this.dialog=n,this.handle=null,this.popup=s,this.windowControl=null,this._destroyed=!1,this._ready=!1,this._private={},this._timeoutId=null,r&&r._dialog&&(this.dialog=n=r._dialog),this._private._readyPromise=new Promise((l,h)=>{this._timeoutId=setTimeout(()=>h(new DOMException("Loading Web application window timed out.","TimeoutError")),t),this._private._resolveReady=p=>{clearTimeout(this.timeoutId),this._timeoutId=null,l(p)},this._private._rejectReady=p=>{clearTimeout(this.timeoutId),this._timeoutId=null,h(p)}}),this._private.isReady=async()=>this._private._readyPromise,this._private.destroy=()=>{this._timeoutId&&this._private._rejectReady(new DOMException("Web application window closed before ready.","AbortError")),this._destroyed||(this.dialog.destroy(),this.dialog=null,this._destroyed=!0)},a&&!typeof a==="function")throw new TypeError("`options.customize` must be a function.");if(this.dialog||(this.popup?this.dialog=new P({url:e,handle:r,bounds:c}):this.dialog=new T({url:e,handle:r,className:d})),this.popup&&c){let{x:l,y:h,width:p=500,height:j=400}=c;p=Math.min(p,window.innerWidth),j=Math.min(j+30,window.innerHeight),l=Math.floor(l!==void 0?l:window.screenX+(window.innerWidth-p)/2),h=Math.floor(h!==void 0?h:window.screenY+(window.innerHeight-j)/2+15),this.dialog.handle.resizeTo(p,j),this.dialog.handle.moveTo(l,h)}if(this.handle=this.dialog.handle,a)try{a({dialog:this.dialog.dialog,container:this.dialog.container,iframe:this.dialog.iframe,webAppWindow:this})}catch(l){console.error(l)}}ready(){this._ready=!0,this._private._resolveReady(!0)}show(){if(!this.visible){this.visible=!0;let e=document.querySelector("body");this._bodyOverflowStyle=e.style.overflow,e.style.overflow="hidden",this._destroyed?this.windowControl.show&&this.windowControl.show():this.dialog.show()}}hide(){if(this.visible){this.visible=!1;let e=document.querySelector("body");this._bodyOverflowStyle?e.style.overflow=this._bodyOverflowStyle:e.style.overflow="",this._destroyed?this.windowControl.hide&&this.windowControl.hide():this.dialog.close()}}};o(u,"WebAppWindow");var we=1e4,_=class{constructor(){this.client=new m,this.server=new g,this.injector=null,this.control=null,this.loaded=!1,this.closed=!1}async createWindow(e,{timeout:t=we,iframe:n,dialog:r=null,popup:s=!1,handle:d,windowControl:a,className:c,customize:l,bounds:h}={}){if(this.loaded)throw new Error("AppContext already loaded.");this.loaded=!0,this.control=new u(e,{timeout:t,dialog:r,iframe:n,popup:s,handle:d,windowControl:a,className:c,customize:l,bounds:h}),window.addEventListener("pagehide",()=>this.close(),{once:!0}),this.server.define("core.control",this.control);let p=f(e).origin;return this.server.listen(p,{handle:this.control.handle,ignoreUnknownApi:!0}),await this.control._private.isReady(),this.injector=await this.client.connect(p,{handle:this.control.handle}),this.injector}close(){this.closed||(this.closed=!0,this.control._private.destroy(),this.server.close(),this.client.close())}};o(_,"WebAppContext");var M=class{constructor({credentialHandler:e,credentialRequestOrigin:t,credentialRequestOptions:n,hintKey:r}){this.type="credentialrequest",this._credentialHandler=e,this.credentialRequestOrigin=t,this.credentialRequestOptions=n,this.hintKey=r}async openWindow(e){await this._credentialHandler.show();let t=new u(e,{className:"credential-handler"});return t.ready(),t.show(),t.handle._dialog=t.dialog,t.handle}respondWith(e){this._promise=e}};o(M,"CredentialRequestEvent");var I=class{constructor({credentialHandler:e,credentialRequestOrigin:t,credential:n,hintKey:r}){this.type="credentialstore",this._credentialHandler=e,this.credentialRequestOrigin=t,this.credential=n,this.hintKey=r}async openWindow(e){await this._credentialHandler.show();let t=new u(e);return t.ready(),t.show(),t.handle._dialog=t.dialog,t.handle}respondWith(e){this._promise=e}};o(I,"CredentialStoreEvent");var O=class{constructor(e){this._credentialHandler=e}async request(e){return await this._credentialHandler._emitter.emit(new M(Object.assign({credentialHandler:this._credentialHandler},e)))}async store(e){return await this._credentialHandler._emitter.emit(new I(Object.assign({credentialHandler:this._credentialHandler},e)))}};o(O,"CredentialHandlerService");var Q=["credentialrequest","credentialstore"],R=class extends y{constructor(e,t=!1){if(typeof e!="string")throw new TypeError('"mediatorOrigin" must be a string.');super(e,t),this._emitter=new x({async waitUntil(n){return n._promise||Promise.reject(new DOMException('No "credentialrequest" event handler found.',"NotFoundError"))}})}async connect(){let e=await super.connect();return this.server.define("credentialHandler",new O(this)),await this.ready(),e}addEventListener(e,t){if(!Q.includes(e))throw new DOMException(`Unsupported event type "${e}"`,"NotSupportedError");return this._emitter.addEventListener(e,t)}removeEventListener(e,t){if(!Q.includes(e))throw new DOMException(`Unsupported event type "${e}"`,"NotSupportedError");return this._emitter.removeEventListener(e,t)}};o(R,"CredentialHandler");var L=class{constructor(e,t){let n=t.get("credentialHints",{functions:["delete","get","keys","has","set","clear"]});for(let r in n)if(r!=="set"){let s=n[r].bind(this,e);this[r]=function(...d){return this._deprecateNotice(),s(...d)}}this._remoteSet=n.set.bind(this,e)}async set(e,t){this._deprecateNotice(),t.icons=t.icons||[];let n=t.icons.map(r=>fe(r.src).then(s=>{r.fetchedImage=s}));return await Promise.all(n),this._remoteSet(e,t)}_deprecateNotice(){console.warn("Credential hints are deprecated and no longer used.")}};o(L,"CredentialHints");function fe(i){return new Promise(e=>{let t=new Image;t.crossOrigin="Anonymous",t.onload=()=>{let n=document.createElement("canvas"),r=n.getContext("2d");n.height=t.height,n.width=t.width,r.drawImage(t,0,0);let s=n.toDataURL();e(s),n=null},t.onerror=()=>e(null),t.src=i})}o(fe,"imageToDataUrl");var v=class{constructor(e,t){if(!(e&&typeof e=="string"))throw new TypeError('"url" must be a non-empty string.');this.hints=new L(e,t)}static async requestPermission(){return(await navigator.credentialsPolyfill.permissions.request({name:"credentialhandler"})).state}};o(v,"CredentialManager");var C=class{constructor(e,t){if(!(e&&typeof e=="string"))throw new TypeError('"url" must be a non-empty string.');this.credentialManager=new v(e,t)}};o(C,"CredentialHandlerRegistration");var A=class{constructor(e){this._init=(async()=>{this._injector=await e,this._remote=this._injector.get("credentialHandlers",{functions:["register","unregister","getRegistration","hasRegistration"]})})()}async register(e){return this._deprecateNotice(),await this._init,e=await this._remote.register("credential",e),new C(e,this._injector)}async unregister(e){return this._deprecateNotice(),await this._init,this._remote.unregister("credential",e)}async getRegistration(e){return this._deprecateNotice(),await this._init,e=await this._remote.getRegistration("credential",e),e?new C(e,this._injector):null}async hasRegistration(e){return this._deprecateNotice(),await this._init,await this._remote.hasRegistration("credential",e)}_deprecateNotice(){console.warn('Credential handler registration APIs are deprecated. The credential handler specified in "manifest.json" is now automatically registered when a user grants permission to install a credential handler via "CredentialManager.requestPermission()".')}};o(A,"CredentialHandlers");var w=class{constructor(e,t,{recommendedHandlerOrigins:n=[]}={}){if(typeof e!="string")throw new TypeError('"dataType" must be a string.');this.type="web",this.dataType=e,this.data=t,this.options={recommendedHandlerOrigins:n}}};o(w,"WebCredential");var me=0,ge=0,N=class{constructor(e){this._nativeCredentialsContainer={get:navigator.credentials&&navigator.credentials.get&&navigator.credentials.get.bind(navigator.credentials),store:navigator.credentials&&navigator.credentials.store&&navigator.credentials.store.bind(navigator.credentials)},this._init=(async()=>{this._remote=(await e).get("credentialsContainer",{functions:[{name:"get",options:{timeout:me}},{name:"store",options:{timeout:ge}}]})})()}async get(e={}){if(e.web){await this._init;let t=await this._remote.get(e);return t?new w(t.dataType,t.data):null}if(this._nativeCredentialsContainer.get)return this._nativeCredentialsContainer.get(e);throw new DOMException("Not implemented.","NotSupportedError")}async store(e){if(e instanceof w){await this._init;let t=await this._remote.store(e);return t?new w(t.dataType,t.data):null}if(this._nativeCredentialsContainer.store)return this._nativeCredentialsContainer.store(e);throw new DOMException("Not implemented.","NotSupportedError")}};o(N,"CredentialsContainer");var H=class{constructor(e){this._init=(async()=>{this._remote=(await e).get("permissionManager",{functions:["query",{name:"request",options:{timeout:0}},"revoke"]})})()}async query(e){return await this._init,await this._remote.query(e)}async request(e){return await this._init,await this._remote.request(e)}async revoke(e){return await this._init,await this._remote.revoke(e)}};o(H,"PermissionManager");var ye="https://authn.io";var $;async function B(i){return $||($=!0,_e(i))}o(B,"loadOnce");async function _e(i={mediatorOrigin:ye}){let e;if(typeof i=="string")e=i;else if(i&&typeof i=="object"&&typeof i.mediatorOrigin=="string")e=`${i.mediatorOrigin}/mediator`;else throw new Error('"options.mediatorOrigin" must be a string expressing the origin of the mediator.');let n=new _().createWindow(e,{className:"credential-mediator",timeout:3e4}),r=document.createElement("style");r.appendChild(document.createTextNode(`dialog.web-app-window.credential-mediator > .web-app-window-backdrop {
|
4
4
|
background-color: rgba(0, 0, 0, 0.25);
|
5
|
-
}`)),document.body.appendChild(r);let s={};return s.permissions=new H(n),s.CredentialHandlers=new A(n),s.CredentialHandler=R,s.CredentialManager=v,s.credentials=new N(n),s.WebCredential=w,navigator.credentialsPolyfill=s,"credentials"in navigator?(navigator.credentials.get=s.credentials.get.bind(s.credentials),navigator.credentials.store=s.credentials.store.bind(s.credentials)):navigator.credentials=s.credentials,window.CredentialManager=v,window.WebCredential=w,s}o(_e,"load");
|
5
|
+
}`)),document.body.appendChild(r);let s={};return s.permissions=new H(n),s.CredentialHandlers=new A(n),s.CredentialHandler=R,s.CredentialManager=v,s.credentials=new N(n),s.WebCredential=w,navigator.credentialsPolyfill=s,"credentials"in navigator?(navigator.credentials.get=s.credentials.get.bind(s.credentials),navigator.credentials.store=s.credentials.store.bind(s.credentials)):navigator.credentials=s.credentials,window.CredentialManager=v,window.WebCredential=w,s}o(_e,"load");var ve=6e4,U=class extends y{constructor(){super(window.location.origin)}async receive(){let e=this;return await e.connect(),new Promise((t,n)=>{let r=setTimeout(()=>{n(new Error("Timed out waiting to receive event."))},ve);e.server.define("credentialEventProxy",{async send(s){return t(s),clearTimeout(r),new Promise((d,a)=>{s.respondWith=c=>{try{d(c)}catch(l){a(l)}}})}}),e.ready()})}};o(U,"CredentialEventProxy");var Ee="https://authn.io";async function Z(){if(await navigator.credentialsPolyfill.CredentialManager.requestPermission()!=="granted")throw new Error("Permission denied.")}o(Z,"installHandler");async function ee({mediatorOrigin:i=Ee,get:e,store:t}){if(!(e||t))throw new Error('"get" or "store" function(s) must be specified.');let n=navigator.credentialsPolyfill.CredentialHandler,r=new n(i);if(e){if(typeof e!="function")throw new TypeError('"get" must be a function.');r.addEventListener("credentialrequest",s=>J({event:s,get:e}))}if(t){if(typeof t!="function")throw new TypeError('"store" must be a function.');r.addEventListener("credentialstore",s=>J({event:s,store:t}))}await r.connect()}o(ee,"activateHandler");async function te(){return new U().receive()}o(te,"receiveCredentialEvent");function J({event:i,get:e,store:t}){i.respondWith(be({event:i,get:e,store:t}))}o(J,"listener");async function be({event:i,get:e,store:t}){let n=await(e||t)({event:i});if(!(n&&typeof n=="object"))throw new TypeError('Return value of "get" or "store" hook must be an object.');if(n.type==="response")return{dataType:n.dataType,data:n.data};if(n.type==="redirect"){let r=new _,s=await i.openWindow(n.url);return(await r.createWindow(n.url,{handle:s,popup:!1,timeout:6e5})).get("credentialEventProxy",{functions:[{name:"send",options:{timeout:0}}]}).send({type:i.type,credentialRequestOptions:i.credentialRequestOptions,credentialRequestOrigin:i.credentialRequestOrigin,credential:i.credential,hintKey:i.hintKey})}throw new Error('Return value of "get" or "store" must have a type of "response" or "redirect".')}o(be,"createResponse");var ie=o(async()=>typeof window>"u"?{name:"CHAPI",methods:{installChapiHandler:async()=>{throw new Error("CHAPI is only available inside of a browser!")},activateChapiHandler:async()=>{throw new Error("CHAPI is only available inside of a browser!")},receiveChapiEvent:async()=>{throw new Error("CHAPI is only available inside of a browser!")},storeCredentialViaChapiDidAuth:async()=>{throw new Error("CHAPI is only available inside of a browser!")},storePresentationViaChapi:async()=>{throw new Error("CHAPI is only available inside of a browser!")}}}:(await B(),{name:"CHAPI",displayName:"CHAPI",description:"Credential Handler API. Allows sending/retrieving credentials across wallets and issuers",methods:{installChapiHandler:async()=>Z(),activateChapiHandler:async(i,{mediatorOrigin:e=`https://authn.io/mediator?${encodeURIComponent(window.location.origin)}`,get:t,store:n})=>ee({mediatorOrigin:e,get:t,store:n}),receiveChapiEvent:async()=>te(),storeCredentialViaChapiDidAuth:async(i,e)=>{let t=crypto.randomUUID(),n=window.location.origin,r={web:{VerifiablePresentation:{query:{type:"DIDAuthentication"},challenge:t,domain:n}}},s=await navigator.credentials.get(r);if(!s)return{success:!1,reason:"did not auth"};let d=await i.invoke.verifyPresentation(s.data,{challenge:t,domain:n,proofPurpose:"authentication"});if(d.warnings.length>0||d.errors.length>0)return{success:!1,reason:"auth failed verification"};let a=s.data?.proof?.verificationMethod?.split("#")[0];Array.isArray(e.credentialSubject)||(e.credentialSubject.id=a);let c=await i.invoke.getTestVp(await i.invoke.issueCredential(e));return await i.invoke.storePresentationViaChapi(c)?{success:!0}:{success:!1,reason:"did not store"}},storePresentationViaChapi:async(i,e)=>{let t=new WebCredential("VerifiablePresentation",e);return window.navigator.credentials.store(t)}}}),"getCHAPIPlugin");
|
6
6
|
/*!
|
7
7
|
* A CredentialHandlerRegistration provides a CredentialManager to enable Web
|
8
8
|
* apps to register Profiles that can be presented to websites.
|