@enbox/api 0.5.11 → 0.6.0

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/browser.mjs CHANGED
@@ -2076,7 +2076,7 @@ zoo`.split(`
2076
2076
  `),e+=` Deleted: ${this.deleted}
2077
2077
  `,e+=` Created: ${this.dateCreated}
2078
2078
  `,e+=` Timestamp: ${this.timestamp}
2079
- `,e+="}",e}async paginationCursor(e){return ZI(Te.RecordsWrite,this.rawMessage)?zz(this.rawMessage,e):void 0}async update({timestamp:e,data:r,encryption:n,protocolRole:i,store:a=!0,...s}){if(this.deleted)throw new Error("Record: Cannot revive a deleted record.");let o=n??this._encryption!==void 0,{parentId:d,...u}=this._recordsWriteDescriptor,c=d?this._contextId.split("/").slice(0,-1).join("/"):void 0,l={...u,...s,parentContextId:c,protocolRole:i??this._protocolRole,messageTimestamp:e,recordId:this._recordId};(b3(l.tags)||l.tags===null)&&delete l.tags;let f;r!==void 0&&(delete l.dataCid,delete l.dataSize,{dataBlob:f}=P0(r,l.dataFormat));let h=new Set(["data","dataCid","dataFormat","dataSize","datePublished","messageTimestamp","published","tags"]);ol.verifyPermittedMutation(Object.keys(s),h),s.published===!1&&l.datePublished!==void 0&&delete l.datePublished;let p={author:this._connectedDid,dataStream:f,messageParams:{...l},messageType:Te.RecordsWrite,target:this._connectedDid,store:a,encryption:o||void 0};await this.applyDelegateGrant(p);let m=await this._agent.processDwnRequest(p),{message:g,reply:{status:_}}=m;if(!(200<=_.code&&_.code<=299))return{status:_,record:this};let v=this._initialWrite??{...this.rawMessage},E=new ol(this._agent,{author:this._author,connectedDid:this._connectedDid,delegateDid:this._delegateDid,remoteOrigin:this._remoteOrigin,protocolRole:i??this._protocolRole,initialWrite:v,encodedData:r!==void 0?f:this._encodedData,...g},this._permissionsApi),T=g;return this._descriptor=T.descriptor,this._attestation=T.attestation,this._authorization=T.authorization,this._encryption=T.encryption,this._contextId=T.contextId,this._initialWrite=v,this._protocolRole=i??this._protocolRole,this._encodedData=r!==void 0?f:this._encodedData,this._readableStream=void 0,this._rawMessageDirty=!0,{status:_,record:E}}async delete(e){let{store:r=!0,signAsOwner:n,timestamp:i,prune:a=!1}=e||{},s=n&&this._delegateDid===void 0,o=n&&this._delegateDid!==void 0;if(this.deleted&&!this._initialWrite)throw new Error("Record: Record is in an invalid state, initial write is missing.");this._initialWrite||(this._initialWrite={...this.rawMessage}),await this.processInitialWriteIfNeeded({store:r,signAsOwner:n});let d={messageType:Te.RecordsDelete,author:this._connectedDid,target:this._connectedDid,signAsOwner:s,signAsOwnerDelegate:o,store:r},u=e?.protocolRole?Lz(this.rawMessage)!==e.protocolRole:!1;this.deleted&&!u?d.rawMessage=this.rawMessage:d.messageParams={prune:a,recordId:this._recordId,messageTimestamp:i,protocolRole:e?.protocolRole??this._protocolRole},await this.applyDelegateGrant(d);let c=await this._agent.processDwnRequest(d),{message:l,reply:{status:f}}=c;if(f.code!==202)return{status:f,record:this};let h=this._initialWrite,p=new ol(this._agent,{author:Wo(l),connectedDid:this._connectedDid,delegateDid:this._delegateDid,remoteOrigin:this._remoteOrigin,protocolRole:e?.protocolRole??this._protocolRole,initialWrite:h,...l},this._permissionsApi),m=l;return this._descriptor=m.descriptor,this._authorization=m.authorization,this._protocolRole=e?.protocolRole??this._protocolRole,this._encodedData=void 0,this._readableStream=void 0,this._rawMessageDirty=!0,{status:f,record:p}}async processInitialWriteIfNeeded({store:e,signAsOwner:r}){if(this.initialWrite&&(r&&!this._initialWriteSigned||e&&!this._initialWriteStored)){let n=r&&this._delegateDid===void 0,i=r&&this._delegateDid!==void 0,a={messageType:Te.RecordsWrite,rawMessage:this.initialWrite,author:this._connectedDid,target:this._connectedDid,signAsOwner:n,signAsOwnerDelegate:i,store:e};await this.applyDelegateGrant(a);let s=await this._agent.processDwnRequest(a),{message:o,reply:{status:d}}=s,u=o;200<=d.code&&d.code<=299&&(e&&(this._initialWriteStored=!0),r&&(this._initialWriteSigned=!0,this.initialWrite.authorization=u.authorization))}}async processRecord({store:e,signAsOwner:r}){let n=r&&this._delegateDid===void 0,i=r&&this._delegateDid!==void 0;await this.processInitialWriteIfNeeded({store:e,signAsOwner:r});let a;this.deleted?a={messageType:Te.RecordsDelete,rawMessage:this.rawMessage,author:this._connectedDid,target:this._connectedDid,signAsOwner:n,signAsOwnerDelegate:i,store:e}:a={messageType:Te.RecordsWrite,rawMessage:this.rawMessage,author:this._connectedDid,target:this._connectedDid,dataStream:await this.data.blob(),signAsOwner:n,signAsOwnerDelegate:i,store:e},await this.applyDelegateGrant(a);let s=await this._agent.processDwnRequest(a),{message:o,reply:{status:d}}=s,u=o;return 200<=d.code&&d.code<=299&&r&&(this._authorization=u.authorization,this._rawMessageDirty=!0),{status:d}}async readRecordData({target:e,isRemote:r}){let n={author:this._connectedDid,messageParams:{filter:{recordId:this.id},protocolRole:this._protocolRole},messageType:Te.RecordsRead,target:e,...this._encryption?{encryption:!0}:{}};if(this._delegateDid)try{await this.applyDelegateGrant(n)}catch{n.author=this._delegateDid}let i=r?this._agent.sendDwnRequest(n):this._agent.processDwnRequest(n);try{let{reply:{status:a,entry:s}}=await i;if(a.code!==200)throw new Error(`${a.code}: ${a.detail}`);return s.data}catch(a){throw new Error(`Error encountered while attempting to read data: ${a.message}`)}}async applyDelegateGrant(e){if(!this._delegateDid)return;let{message:r}=await this._permissionsApi.getPermissionForRequest({connectedDid:this._connectedDid,delegateDid:this._delegateDid,protocol:this.protocol,delegate:!0,cached:!0,messageType:e.messageType});e.messageParams={...e.messageParams,delegatedGrant:r},e.granteeDid=this._delegateDid}static verifyPermittedMutation(e,r){for(let n of e)if(!r.has(n))throw new Error(`${n} is an immutable property. Its value cannot be changed.`)}isRecordsDeleteDescriptor(e){return e.interface+e.method===Te.RecordsDelete}};ol._sendCache=x0;var jd=ol;var iR=class extends CustomEvent{constructor(e){super(e.type,{detail:e})}},xP=class extends EventTarget{constructor(r){super();this._closed=!1;this._connected=!0;let{agent:n,connectedDid:i,cursor:a,delegateDid:s,protocolRole:o,remoteOrigin:d,permissionsApi:u,initialEntries:c,subscription:l}=r;this._subscription=l,this.cursor=a,this.records=c.map(f=>new jd(n,{author:Wo(f),connectedDid:i,remoteOrigin:d,delegateDid:s,protocolRole:o,...f},u)),this._knownRecords=new Map;for(let f of this.records)this._knownRecords.set(f.id,f.timestamp)}get isConnected(){return this._connected}handleEvent(r){if(this._closed)return;let n;if(r.deleted)n="delete",this._knownRecords.delete(r.id);else{let a=this._knownRecords.get(r.id);if(a!==void 0){if(r.timestamp<=a)return;n="update"}else n="create";this._knownRecords.set(r.id,r.timestamp)}let i={type:n,record:r};this.dispatchEvent(new iR(i)),this.dispatchEvent(new CustomEvent("change",{detail:i}))}handleLifecycleEvent(r,n){this._closed||(r==="disconnected"?this._connected=!1:r==="reconnected"&&(this._connected=!0),this.dispatchEvent(new CustomEvent(r,{detail:n})))}on(r,n){let i=a=>{let s=a.detail;r==="change"?n(s):r==="create"||r==="update"||r==="delete"?n(s.record):r==="reconnecting"?n(s):n()};return this.addEventListener(r,i),()=>{this.removeEventListener(r,i)}}async close(){this._closed||(this._closed=!0,await this._subscription.close())}};y();Kt();y();Kt();var PP=class t{constructor(e,r){this._permissions=e,this._connectedDid=r.connectedDid,this._message=r.message}get author(){return Wo(this._message)}static async parse({connectedDid:e,agent:r,message:n}){let i=new fi({agent:r});return new t(i,{connectedDid:e,message:n})}get agent(){return this._permissions.agent}get rawMessage(){return this._message}async send(e){e??=this._connectedDid;let{encodedData:r,...n}=this._message,i=new Blob([Ue.base64Url(r).toUint8Array()]),a={messageType:Te.RecordsWrite,author:this._connectedDid,target:e,dataStream:i,rawMessage:n},{reply:s}=await this.agent.sendDwnRequest(a);return s}async store(e){let{encodedData:r,...n}=this.rawMessage,i=new Blob([Ue.base64Url(r).toUint8Array()]),{reply:a,message:s}=await this.agent.processDwnRequest({author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,signAsOwner:e,rawMessage:n,dataStream:i});return this._message={...s,encodedData:r},{status:a.status}}};var np=class t{constructor({api:e,connectedDid:r,message:n,grant:i}){this._permissions=e,this._connectedDid=r,this._message=n,this._grant=i}static parse(e){let r=dr.parse(e.message),n=new fi({agent:e.agent});return new t({...e,grant:r,api:n})}get agent(){return this._permissions.agent}get id(){return this._grant.id}get grantor(){return this._grant.grantor}get grantee(){return this._grant.grantee}get dateGranted(){return this._grant.dateGranted}get description(){return this._grant.description}get requestId(){return this._grant.requestId}get dateExpires(){return this._grant.dateExpires}get delegated(){return this._grant.delegated}get scope(){return this._grant.scope}get conditions(){return this._grant.conditions}get rawMessage(){return this._message}async send(e){e??=this._connectedDid;let{encodedData:r,...n}=this._message,i=new Blob([Ue.base64Url(r).toUint8Array()]),a={messageType:Te.RecordsWrite,author:this._connectedDid,target:e,dataStream:i,rawMessage:n},{reply:s}=await this.agent.sendDwnRequest(a);return s}async store(e=!1){let{encodedData:r,...n}=this.rawMessage,i=new Blob([Ue.base64Url(r).toUint8Array()]),{reply:a,message:s}=await this.agent.processDwnRequest({store:!0,author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,signAsOwner:e,rawMessage:n,dataStream:i});return this._message={...s,encodedData:r},{status:a.status}}async import(e=!1){let{encodedData:r,...n}=this.rawMessage,i=new Blob([Ue.base64Url(r).toUint8Array()]),{reply:a,message:s}=await this.agent.processDwnRequest({store:e,author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,signAsOwner:!0,rawMessage:n,dataStream:i});return this._message={...s,encodedData:r},{status:a.status}}async revoke(e=!0){let r=await this._permissions.createRevocation({store:e,author:this._connectedDid,grant:this._grant});return PP.parse({connectedDid:this._connectedDid,agent:this.agent,message:r.message})}isRevoked(e=!1){return this._permissions.isGrantRevoked({author:this._connectedDid,target:this.grantor,grantRecordId:this.id,remote:e})}toJSON(){return{id:this.id,grantor:this.grantor,grantee:this.grantee,dateGranted:this.dateGranted,description:this.description,requestId:this.requestId,dateExpires:this.dateExpires,delegated:this.delegated,scope:this.scope,conditions:this.conditions}}};y();Kt();var _0=class t{constructor({api:e,connectedDid:r,message:n,request:i}){this._permissions=e,this._connectedDid=r,this._request=i,this._message=n}static parse({connectedDid:e,agent:r,message:n}){let i=Ia.parse(n),a=new fi({agent:r});return new t({api:a,connectedDid:e,message:n,request:i})}get agent(){return this._permissions.agent}get id(){return this._request.id}get requester(){return this._request.requester}get description(){return this._request.description}get delegated(){return this._request.delegated}get scope(){return this._request.scope}get conditions(){return this._request.conditions}get rawMessage(){return this._message}async send(e){e??=this._connectedDid;let{encodedData:r,...n}=this._message,i=new Blob([Ue.base64Url(r).toUint8Array()]),a={messageType:Te.RecordsWrite,author:this._connectedDid,target:e,dataStream:i,rawMessage:n},{reply:s}=await this.agent.sendDwnRequest(a);return s}async store(){let{encodedData:e,...r}=this.rawMessage,n=new Blob([Ue.base64Url(e).toUint8Array()]),{reply:i,message:a}=await this.agent.processDwnRequest({author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,rawMessage:r,dataStream:n});return this._message={...a,encodedData:e},{status:i.status}}async grant(e,r=!0){let{message:n}=await this._permissions.createGrant({requestId:this.id,grantedTo:this.requester,scope:this.scope,delegated:this.delegated,author:this._connectedDid,store:r,dateExpires:e});return np.parse({connectedDid:this._connectedDid,agent:this.agent,message:n})}toJSON(){return this._request}};y();var E0=class{constructor(e,r,n){this._agent=e,this._metadata=n,this._protocolsConfigureMessage=r}get definition(){return this._protocolsConfigureMessage.descriptor.definition}toJSON(){return this._protocolsConfigureMessage}async send(e){let{reply:r}=await this._agent.sendDwnRequest({author:this._metadata.author,messageCid:this._metadata.messageCid,messageType:Te.ProtocolsConfigure,target:e});return{status:r.status}}};var _P=class{constructor(e){this.agent=e.agent,this.connectedDid=e.connectedDid,this.delegateDid=e.delegateDid,this.permissionsApi=new fi({agent:this.agent})}get isDelegate(){return this.delegateDid!==void 0}get permissions(){return{request:async e=>{let{message:r}=await this.permissionsApi.createRequest({...e,author:this.delegateDid??this.connectedDid}),n={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:r};return _0.parse(n)},grant:async e=>{let{message:r}=await this.permissionsApi.createGrant({...e,author:this.delegateDid??this.connectedDid}),n={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:r};return np.parse(n)},queryRequests:async(e={})=>{let{from:r,...n}=e,i=await this.permissionsApi.fetchRequests({...n,author:this.delegateDid??this.connectedDid,target:r??this.delegateDid??this.connectedDid,remote:r!==void 0}),a=[];for(let s of i){let o={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:s.message};a.push(_0.parse(o))}return a},queryGrants:async(e={})=>{let{from:r,...n}=e,i=r!==void 0,a=this.delegateDid??this.connectedDid,s=r??this.delegateDid??this.connectedDid,o=await this.permissionsApi.fetchGrants({...n,author:a,target:s,remote:i}),d=[];for(let u of o){let c={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:u.message};d.push(np.parse(c))}return d}}}get protocols(){return{configure:async e=>{let{encryption:r,...n}=e,i={author:this.connectedDid,messageParams:n,messageType:Te.ProtocolsConfigure,target:this.connectedDid,encryption:r};if(this.delegateDid){let{message:c}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n.definition.protocol,delegate:!0,cached:!0,messageType:i.messageType});i.messageParams={...i.messageParams,delegatedGrant:c},i.granteeDid=this.delegateDid}let a=await this.agent.processDwnRequest(i),{message:s,messageCid:o,reply:{status:d}}=a,u={status:d};if(d.code<300){let c={author:this.connectedDid,messageCid:o};u.protocol=new E0(this.agent,s,c)}return u},query:async e=>{let{from:r,...n}=e,i={author:this.connectedDid,messageParams:n,messageType:Te.ProtocolsQuery,target:r||this.connectedDid};if(this.delegateDid)try{let{grant:{id:c}}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n.filter.protocol,cached:!0,messageType:i.messageType});i.messageParams={...i.messageParams,permissionGrantId:c},i.granteeDid=this.delegateDid}catch{i.author=this.delegateDid}let a;r?a=await this.agent.sendDwnRequest(i):a=await this.agent.processDwnRequest(i);let s=a.reply,{entries:o=[],status:d}=s;return{protocols:o.map(c=>{let l={author:this.connectedDid};return new E0(this.agent,c,l)}),status:d}}}}get records(){return{delete:async e=>{let{from:r,protocol:n,...i}=e,a={author:this.connectedDid,messageParams:i,messageType:Te.RecordsDelete,target:r||this.connectedDid};if(this.delegateDid){let{message:d}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n,delegate:!0,cached:!0,messageType:a.messageType});a.messageParams={...a.messageParams,delegatedGrant:d},a.granteeDid=this.delegateDid}let s;r?s=await this.agent.sendDwnRequest(a):s=await this.agent.processDwnRequest(a);let{reply:{status:o}}=s;return{status:o}},query:async e=>{let{from:r,encryption:n,...i}=e,a={author:this.connectedDid,messageParams:i,messageType:Te.RecordsQuery,target:r||this.connectedDid,encryption:n};if(this.delegateDid)try{let{message:f}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:i.filter?.protocol,delegate:!0,cached:!0,messageType:a.messageType});a.messageParams={...a.messageParams,delegatedGrant:f},a.granteeDid=this.delegateDid}catch{a.author=this.delegateDid}let s;r?s=await this.agent.sendDwnRequest(a):s=await this.agent.processDwnRequest(a);let o=s.reply,{entries:d=[],status:u,cursor:c}=o;return{records:d.map(f=>{let h={author:Wo(f),connectedDid:this.connectedDid,remoteOrigin:r,delegateDid:this.delegateDid,protocolRole:a.messageParams.protocolRole,...f};return new jd(this.agent,h,this.permissionsApi)}),status:u,cursor:c}},read:async e=>{let{from:r,protocol:n,encryption:i,...a}=e,s={author:this.connectedDid,messageParams:a,messageType:Te.RecordsRead,target:r||this.connectedDid,encryption:i};if(this.delegateDid)try{let{message:l}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n,delegate:!0,cached:!0,messageType:s.messageType});s.messageParams={...s.messageParams,delegatedGrant:l},s.granteeDid=this.delegateDid}catch{s.author=this.delegateDid}let o;r?o=await this.agent.sendDwnRequest(s):o=await this.agent.processDwnRequest(s);let{reply:{entry:d,status:u}}=o,c;if(200<=u.code&&u.code<=299){let l={author:Wo(d.recordsWrite),connectedDid:this.connectedDid,remoteOrigin:r,delegateDid:this.delegateDid,data:d.data,initialWrite:d.initialWrite,...d.recordsWrite};c=new jd(this.agent,l,this.permissionsApi)}return{record:c,status:u}},subscribe:async e=>{let{from:r,...n}=e,i,a=r,s=n.protocolRole,o=m=>{if(m.type==="eose"){i?.handleLifecycleEvent("eose");return}if(m.type==="disconnected"){i?.handleLifecycleEvent("disconnected");return}if(m.type==="reconnected"){i?.handleLifecycleEvent("reconnected");return}if(m.type==="reconnecting"){i?.handleLifecycleEvent("reconnecting",{attempt:m.attempt});return}let{message:g,initialWrite:_}=m.event,v=new jd(this.agent,{...g,author:Wo(g),connectedDid:this.connectedDid,remoteOrigin:a,initialWrite:_,protocolRole:s,delegateDid:this.delegateDid},this.permissionsApi);i?.handleEvent(v)},d={author:this.connectedDid,messageParams:n,messageType:Te.RecordsSubscribe,target:r||this.connectedDid,subscriptionHandler:o};if(this.delegateDid)try{let{message:m}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n.filter?.protocol,delegate:!0,cached:!0,messageType:d.messageType});d.messageParams={...d.messageParams,delegatedGrant:m},d.granteeDid=this.delegateDid}catch{d.author=this.delegateDid}let u;r?u=await this.agent.sendDwnRequest(d):u=await this.agent.processDwnRequest(d);let c=u.reply,{status:l,subscription:f,entries:h=[],cursor:p}=c;return f&&(i=new xP({agent:this.agent,connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocolRole:s,remoteOrigin:a,permissionsApi:this.permissionsApi,initialEntries:h,cursor:p,subscription:f})),{status:l,liveQuery:i}},write:async e=>{let{data:r,store:n,encryption:i,...a}=e,{dataBlob:s,dataFormat:o}=P0(r,a.dataFormat),d={...a,dataFormat:o},u={store:n,messageType:Te.RecordsWrite,messageParams:d,author:this.connectedDid,target:this.connectedDid,dataStream:s,encryption:i};if(this.delegateDid){let{message:p}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:d.protocol,delegate:!0,cached:!0,messageType:u.messageType});u.messageParams={...u.messageParams,delegatedGrant:p},u.granteeDid=this.delegateDid}let c=await this.agent.processDwnRequest(u),{message:l,reply:{status:f}}=c,h;if(200<=f.code&&f.code<=299){let p={author:this.connectedDid,connectedDid:this.connectedDid,encodedData:s,delegateDid:this.delegateDid,...l};h=new jd(this.agent,p,this.permissionsApi)}return{record:h,status:f}}}}};y();y();y();var $c=class t{constructor(e){this._record=e}get rawRecord(){return this._record}get data(){let e=this._record.data;return{blob:()=>e.blob(),bytes:()=>e.bytes(),json:()=>e.json(),text:()=>e.text(),stream:()=>e.stream(),then:e.then.bind(e),catch:e.catch.bind(e)}}async update(e){let{status:r,record:n}=await this._record.update(e);return{status:r,record:new t(n)}}async delete(e){let{status:r,record:n}=await this._record.delete(e);return{status:r,record:new t(n)}}async store(e=!1){return this._record.store(e)}async import(e=!0){return this._record.import(e)}async send(e){return this._record.send(e)}toJSON(){return this._record.toJSON()}toString(){return this._record.toString()}async paginationCursor(e){return this._record.paginationCursor(e)}get id(){return this._record.id}get contextId(){return this._record.contextId}get dateCreated(){return this._record.dateCreated}get parentId(){return this._record.parentId}get protocol(){return this._record.protocol}get protocolPath(){return this._record.protocolPath}get recipient(){return this._record.recipient}get schema(){return this._record.schema}get dataFormat(){return this._record.dataFormat}get dataCid(){return this._record.dataCid}get dataSize(){return this._record.dataSize}get datePublished(){return this._record.datePublished}get published(){return this._record.published}get tags(){return this._record.tags}get author(){return this._record.author}get creator(){return this._record.creator}get timestamp(){return this._record.timestamp}get encryption(){return this._record.encryption}get authorization(){return this._record.authorization}get attestation(){return this._record.attestation}get protocolRole(){return this._record.protocolRole}get deleted(){return this._record.deleted}get initialWrite(){return this._record.initialWrite}get rawMessage(){return this._record.rawMessage}};var EP=class{constructor(e){this._liveQuery=e}get rawLiveQuery(){return this._liveQuery}get records(){return this._typedRecords||(this._typedRecords=this._liveQuery.records.map(e=>new $c(e))),this._typedRecords}get cursor(){return this._liveQuery.cursor}get isConnected(){return this._liveQuery.isConnected}on(e,r){return e==="disconnected"?this._liveQuery.on("disconnected",r):e==="reconnected"?this._liveQuery.on("reconnected",r):e==="eose"?this._liveQuery.on("eose",r):e==="reconnecting"?this._liveQuery.on("reconnecting",r):e==="change"?this._liveQuery.on("change",n=>{r({type:n.type,record:new $c(n.record)})}):this._liveQuery.on(e,n=>{r(new $c(n))})}async close(){return this._liveQuery.close()}};var TP=class{constructor(e,r){this._configured=!1;this._ensureReadyPromise=null;this._dwn=e,this._definition=r.definition,this._validPaths=y7(this._definition.structure)}get protocol(){return this._definition.protocol}get definition(){return this._definition}async configure(e){let{protocols:r}=await this._dwn.protocols.query({filter:{protocol:this._definition.protocol}});if(r.length>0){let a=r[0];if(m7(a.definition,this._definition))return this._configured=!0,{status:{code:200,detail:"OK"},protocol:a}}let n=!this._dwn.isDelegate&&Object.values(this._definition.types).some(a=>a?.encryptionRequired===!0),i=await this._dwn.protocols.configure({definition:this._definition,encryption:e?.encryption??(n||void 0)});return i.status.code===202&&(this._configured=!0),i}get isConfigured(){return this._configured}_assertValidPath(e){if(!this._validPaths.has(e))throw new Error(`TypedEnbox: invalid protocol path '${e}'. Valid paths are: ${[...this._validPaths].join(", ")}.`)}async _ensureReady(e){if(this._configured){this._assertValidPath(e);return}this._ensureReadyPromise===null&&(this._ensureReadyPromise=this._autoConfigureOnce()),await this._ensureReadyPromise,this._assertValidPath(e)}async _autoConfigureOnce(){let{protocols:e}=await this._dwn.protocols.query({filter:{protocol:this._definition.protocol}});if(e.length>0){let i=e[0];if(m7(i.definition,this._definition)){this._configured=!0;return}console.warn(`TypedEnbox: installed protocol '${this._definition.protocol}' differs from the provided definition. Call configure() to update it.`),this._configured=!0;return}let r=!this._dwn.isDelegate&&Object.values(this._definition.types).some(i=>i?.encryptionRequired===!0);(await this._dwn.protocols.configure({definition:this._definition,encryption:r||void 0})).status.code===202&&(this._configured=!0)}get records(){if(this._records!==void 0)return this._records;let e={create:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=!this._dwn.isDelegate&&s?.encryptionRequired===!0?!0:void 0,{status:d,record:u}=await this._dwn.records.write({data:n.data,store:n.store,encryption:n.encryption??o,parentContextId:n.parentContextId,published:n.published,datePublished:n.datePublished,recipient:n.recipient,protocolRole:n.protocolRole,tags:n.tags,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{},dataFormat:n.dataFormat??s?.dataFormats?.[0]});return{status:d,record:u?new $c(u):void 0}},query:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=oR(n?.filter),d=!this._dwn.isDelegate&&s?.encryptionRequired===!0?!0:void 0,{status:u,records:c,cursor:l}=await this._dwn.records.query({from:n?.from,encryption:n?.encryption??d,filter:{...o,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{}},dateSort:n?.dateSort,pagination:n?.pagination,protocolRole:n?.protocolRole});return{status:u,records:c.map(f=>new $c(f)),cursor:l}},read:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=oR(n.filter),d=!this._dwn.isDelegate&&s?.encryptionRequired===!0?!0:void 0,{status:u,record:c}=await this._dwn.records.read({from:n.from,encryption:n.encryption??d,protocol:this._definition.protocol,filter:{...o,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{}}});return{status:u,record:c?new $c(c):void 0}},delete:async(r,n)=>(await this._ensureReady(S0(r)),this._dwn.records.delete({from:n.from,protocol:this._definition.protocol,recordId:n.recordId})),subscribe:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=oR(n?.filter),{status:d,liveQuery:u}=await this._dwn.records.subscribe({from:n?.from,filter:{...o,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{}},protocolRole:n?.protocolRole});return{status:d,liveQuery:u?new EP(u):void 0}}};return this._records=e,e}};function oR(t){if(!t)return;let{parentContextId:e,...r}=t;return e!==void 0&&r.parentId===void 0&&(r.parentId=e),r}function m7(t,e){return kP(t)===kP(e)}function S0(t){return t.replace(/^\/+|\/+$/g,"")}function SP(t){let e=t.split("/");return e[e.length-1]}function y7(t,e=""){let r=new Set;for(let n of Object.keys(t)){if(n.startsWith("$"))continue;let i=e?`${e}/${n}`:n;r.add(i);let a=t[n];if(a!==null&&typeof a=="object")for(let s of y7(a,i))r.add(s)}return r}function kP(t){return t==null||typeof t!="object"?JSON.stringify(t):Array.isArray(t)?"["+t.map(n=>kP(n)).join(",")+"]":"{"+Object.keys(t).sort().map(n=>JSON.stringify(n)+":"+kP(t[n])).join(",")+"}"}y();var AP=class{constructor(e){this.agent=e.agent,this.connectedDid=e.connectedDid}async create(){throw new Error("Not implemented.")}};var g7=class t{constructor({agent:e,connectedDid:r,delegateDid:n}){this._typedInstances=new Map;this.agent=e,this.did=new vP({agent:e,connectedDid:r}),this._dwn=new _P({agent:e,connectedDid:r,delegateDid:n}),this.vc=new AP({agent:e,connectedDid:r})}using(e){let r=e.definition.protocol,n=this._typedInstances.get(r);if(n)return n;let i=new TP(this._dwn,e);return this._typedInstances.set(r,i),i}async disconnect(e){"sync"in this.agent&&typeof this.agent.sync?.stopSync=="function"&&await this.agent.sync.stopSync(e),this._typedInstances.clear()}static anonymous(e){let r=new s0({didResolvers:e?.didResolvers??[tl,Jf,qx,kI],cache:new Lx}),n=new Rm,i=new lP({didResolver:r,rpcClient:n});return{dwn:new bP(i)}}static connect(e){if("session"in e){let{session:r}=e;return new t({agent:r.agent,connectedDid:r.did,delegateDid:r.delegateDid})}return new t(e)}};y();y();y();function w7(t){return t.code===400&&t.detail.includes("record limit")}function Ppe(t,e){let r=e.split("/"),n=t.structure;for(let a of r){if(!n||typeof n!="object")return!1;n=n[a]}if(!n||typeof n!="object")return!1;let i=n.$recordLimit;return i!==void 0&&typeof i=="object"&&i!==null&&i.max===1}function _pe(t,e){let r=e.split("/"),n=t.structure;for(let i of r){if(!n||typeof n!="object")return[];n=n[i]}return!n||typeof n!="object"?[]:Object.keys(n).filter(i=>!i.startsWith("$"))}function Epe(t,e){return{async create(r){let{status:n,record:i}=await t.records.create(e,r);return{status:n,record:i}},async query(r){let{status:n,records:i,cursor:a}=await t.records.query(e,r);return{status:n,records:i,cursor:a}},async get(r){let{record:n}=await t.records.read(e,{filter:{recordId:r}});return n},async delete(r){return t.records.delete(e,{recordId:r})},async subscribe(r){let{liveQuery:n}=await t.records.subscribe(e,r);return n}}}function Spe(t,e){return{async set(r){let n=await t.records.create(e,r);if(w7(n.status)){let{records:i}=await t.records.query(e);if(i.length>0){let{status:a,record:s}=await i[0].update({data:r.data,...r.tags!==void 0?{tags:r.tags}:{}});return{status:a,record:s}}}return{status:n.status,record:n.record}},async get(){let{records:r}=await t.records.query(e);return r.length>0?r[0]:void 0},async delete(r){return t.records.delete(e,{recordId:r})}}}function Tpe(t,e){return{async create(r,n){let{status:i,record:a}=await t.records.create(e,{...n,parentContextId:r});return{status:i,record:a}},async query(r,n){let{status:i,records:a,cursor:s}=await t.records.query(e,{...n,filter:{...n?.filter,contextId:r}});return{status:i,records:a,cursor:s}},async get(r){let{record:n}=await t.records.read(e,{filter:{recordId:r}});return n},async delete(r){return t.records.delete(e,{recordId:r})},async subscribe(r,n){let{liveQuery:i}=await t.records.subscribe(e,{...n,filter:{...n?.filter,contextId:r}});return i}}}function kpe(t,e){return{async set(r,n){let i=await t.records.create(e,{...n,parentContextId:r});if(w7(i.status)){let{records:a}=await t.records.query(e,{filter:{contextId:r}});if(a.length>0){let{status:s,record:o}=await a[0].update({data:n.data,...n.tags!==void 0?{tags:n.tags}:{}});return{status:s,record:o}}}return{status:i.status,record:i.record}},async get(r){let{records:n}=await t.records.query(e,{filter:{contextId:r}});return n.length>0?n[0]:void 0},async delete(r){return t.records.delete(e,{recordId:r})}}}function v7(t,e,r,n){let i=Ppe(e,r),a;n?a=i?kpe(t,r):Tpe(t,r):a=i?Spe(t,r):Epe(t,r);let s=_pe(e,r),o={};return new Proxy(a,{get(d,u){if(typeof u=="string"){if(u in d)return d[u];if(s.includes(u))return u in o||(o[u]=v7(t,e,`${r}/${u}`,!0)),o[u]}}})}function MJe(t){let e=t.definition,r=Object.keys(e.structure).filter(a=>!a.startsWith("$")),n={};return new Proxy({},{get(a,s){if(typeof s=="string"){if(s==="configure")return async o=>await t.configure(o);if(r.includes(s))return s in n||(n[s]=v7(t,e,s,!1)),n[s]}}})}y();export{vP as DidApi,bP as DwnReaderApi,g7 as Enbox,xP as LiveQuery,np as PermissionGrant,PP as PermissionGrantRevocation,_0 as PermissionRequest,E0 as Protocol,b0 as ReadOnlyRecord,jd as Record,iR as RecordChangeEvent,TP as TypedEnbox,EP as TypedLiveQuery,$c as TypedRecord,AP as VcApi,h7 as createRecordData,Npe as defineProtocol,f7 as isOk,MJe as repository,p7 as utils};
2079
+ `,e+="}",e}async paginationCursor(e){return ZI(Te.RecordsWrite,this.rawMessage)?zz(this.rawMessage,e):void 0}async update({timestamp:e,data:r,encryption:n,protocolRole:i,store:a=!0,...s}){if(this.deleted)throw new Error("Record: Cannot revive a deleted record.");let o=n??this._encryption!==void 0,{parentId:d,...u}=this._recordsWriteDescriptor,c=d?this._contextId.split("/").slice(0,-1).join("/"):void 0,l={...u,...s,parentContextId:c,protocolRole:i??this._protocolRole,messageTimestamp:e,recordId:this._recordId};(b3(l.tags)||l.tags===null)&&delete l.tags;let f;r!==void 0&&(delete l.dataCid,delete l.dataSize,{dataBlob:f}=P0(r,l.dataFormat));let h=new Set(["data","dataCid","dataFormat","dataSize","datePublished","messageTimestamp","published","tags"]);ol.verifyPermittedMutation(Object.keys(s),h),s.published===!1&&l.datePublished!==void 0&&delete l.datePublished;let p={author:this._connectedDid,dataStream:f,messageParams:{...l},messageType:Te.RecordsWrite,target:this._connectedDid,store:a,encryption:o||void 0};await this.applyDelegateGrant(p);let m=await this._agent.processDwnRequest(p),{message:g,reply:{status:_}}=m;if(!(200<=_.code&&_.code<=299))return{status:_,record:this};let v=this._initialWrite??{...this.rawMessage},E=new ol(this._agent,{author:this._author,connectedDid:this._connectedDid,delegateDid:this._delegateDid,remoteOrigin:this._remoteOrigin,protocolRole:i??this._protocolRole,initialWrite:v,encodedData:r!==void 0?f:this._encodedData,...g},this._permissionsApi),T=g;return this._descriptor=T.descriptor,this._attestation=T.attestation,this._authorization=T.authorization,this._encryption=T.encryption,this._contextId=T.contextId,this._initialWrite=v,this._protocolRole=i??this._protocolRole,this._encodedData=r!==void 0?f:this._encodedData,this._readableStream=void 0,this._rawMessageDirty=!0,{status:_,record:E}}async delete(e){let{store:r=!0,signAsOwner:n,timestamp:i,prune:a=!1}=e||{},s=n&&this._delegateDid===void 0,o=n&&this._delegateDid!==void 0;if(this.deleted&&!this._initialWrite)throw new Error("Record: Record is in an invalid state, initial write is missing.");this._initialWrite||(this._initialWrite={...this.rawMessage}),await this.processInitialWriteIfNeeded({store:r,signAsOwner:n});let d={messageType:Te.RecordsDelete,author:this._connectedDid,target:this._connectedDid,signAsOwner:s,signAsOwnerDelegate:o,store:r},u=e?.protocolRole?Lz(this.rawMessage)!==e.protocolRole:!1;this.deleted&&!u?d.rawMessage=this.rawMessage:d.messageParams={prune:a,recordId:this._recordId,messageTimestamp:i,protocolRole:e?.protocolRole??this._protocolRole},await this.applyDelegateGrant(d);let c=await this._agent.processDwnRequest(d),{message:l,reply:{status:f}}=c;if(f.code!==202)return{status:f,record:this};let h=this._initialWrite,p=new ol(this._agent,{author:Wo(l),connectedDid:this._connectedDid,delegateDid:this._delegateDid,remoteOrigin:this._remoteOrigin,protocolRole:e?.protocolRole??this._protocolRole,initialWrite:h,...l},this._permissionsApi),m=l;return this._descriptor=m.descriptor,this._authorization=m.authorization,this._protocolRole=e?.protocolRole??this._protocolRole,this._encodedData=void 0,this._readableStream=void 0,this._rawMessageDirty=!0,{status:f,record:p}}async processInitialWriteIfNeeded({store:e,signAsOwner:r}){if(this.initialWrite&&(r&&!this._initialWriteSigned||e&&!this._initialWriteStored)){let n=r&&this._delegateDid===void 0,i=r&&this._delegateDid!==void 0,a={messageType:Te.RecordsWrite,rawMessage:this.initialWrite,author:this._connectedDid,target:this._connectedDid,signAsOwner:n,signAsOwnerDelegate:i,store:e};await this.applyDelegateGrant(a);let s=await this._agent.processDwnRequest(a),{message:o,reply:{status:d}}=s,u=o;200<=d.code&&d.code<=299&&(e&&(this._initialWriteStored=!0),r&&(this._initialWriteSigned=!0,this.initialWrite.authorization=u.authorization))}}async processRecord({store:e,signAsOwner:r}){let n=r&&this._delegateDid===void 0,i=r&&this._delegateDid!==void 0;await this.processInitialWriteIfNeeded({store:e,signAsOwner:r});let a;this.deleted?a={messageType:Te.RecordsDelete,rawMessage:this.rawMessage,author:this._connectedDid,target:this._connectedDid,signAsOwner:n,signAsOwnerDelegate:i,store:e}:a={messageType:Te.RecordsWrite,rawMessage:this.rawMessage,author:this._connectedDid,target:this._connectedDid,dataStream:await this.data.blob(),signAsOwner:n,signAsOwnerDelegate:i,store:e},await this.applyDelegateGrant(a);let s=await this._agent.processDwnRequest(a),{message:o,reply:{status:d}}=s,u=o;return 200<=d.code&&d.code<=299&&r&&(this._authorization=u.authorization,this._rawMessageDirty=!0),{status:d}}async readRecordData({target:e,isRemote:r}){let n={author:this._connectedDid,messageParams:{filter:{recordId:this.id},protocolRole:this._protocolRole},messageType:Te.RecordsRead,target:e,...this._encryption?{encryption:!0}:{}};if(this._delegateDid)try{await this.applyDelegateGrant(n)}catch{n.author=this._delegateDid}let i=r?this._agent.sendDwnRequest(n):this._agent.processDwnRequest(n);try{let{reply:{status:a,entry:s}}=await i;if(a.code!==200)throw new Error(`${a.code}: ${a.detail}`);return s.data}catch(a){throw new Error(`Error encountered while attempting to read data: ${a.message}`)}}async applyDelegateGrant(e){if(!this._delegateDid)return;let{message:r}=await this._permissionsApi.getPermissionForRequest({connectedDid:this._connectedDid,delegateDid:this._delegateDid,protocol:this.protocol,delegate:!0,cached:!0,messageType:e.messageType});e.messageParams={...e.messageParams,delegatedGrant:r},e.granteeDid=this._delegateDid}static verifyPermittedMutation(e,r){for(let n of e)if(!r.has(n))throw new Error(`${n} is an immutable property. Its value cannot be changed.`)}isRecordsDeleteDescriptor(e){return e.interface+e.method===Te.RecordsDelete}};ol._sendCache=x0;var jd=ol;var iR=class extends CustomEvent{constructor(e){super(e.type,{detail:e})}},xP=class extends EventTarget{constructor(r){super();this._closed=!1;this._connected=!0;let{agent:n,connectedDid:i,cursor:a,delegateDid:s,protocolRole:o,remoteOrigin:d,permissionsApi:u,initialEntries:c,subscription:l}=r;this._subscription=l,this.cursor=a,this.records=c.map(f=>new jd(n,{author:Wo(f),connectedDid:i,remoteOrigin:d,delegateDid:s,protocolRole:o,...f},u)),this._knownRecords=new Map;for(let f of this.records)this._knownRecords.set(f.id,f.timestamp)}get isConnected(){return this._connected}handleEvent(r){if(this._closed)return;let n;if(r.deleted)n="delete",this._knownRecords.delete(r.id);else{let a=this._knownRecords.get(r.id);if(a!==void 0){if(r.timestamp<=a)return;n="update"}else n="create";this._knownRecords.set(r.id,r.timestamp)}let i={type:n,record:r};this.dispatchEvent(new iR(i)),this.dispatchEvent(new CustomEvent("change",{detail:i}))}handleLifecycleEvent(r,n){this._closed||(r==="disconnected"?this._connected=!1:r==="reconnected"&&(this._connected=!0),this.dispatchEvent(new CustomEvent(r,{detail:n})))}on(r,n){let i=a=>{let s=a.detail;r==="change"?n(s):r==="create"||r==="update"||r==="delete"?n(s.record):r==="reconnecting"?n(s):n()};return this.addEventListener(r,i),()=>{this.removeEventListener(r,i)}}async close(){this._closed||(this._closed=!0,await this._subscription.close())}};y();Kt();y();Kt();var PP=class t{constructor(e,r){this._permissions=e,this._connectedDid=r.connectedDid,this._message=r.message}get author(){return Wo(this._message)}static async parse({connectedDid:e,agent:r,message:n}){let i=new fi({agent:r});return new t(i,{connectedDid:e,message:n})}get agent(){return this._permissions.agent}get rawMessage(){return this._message}async send(e){e??=this._connectedDid;let{encodedData:r,...n}=this._message,i=new Blob([Ue.base64Url(r).toUint8Array()]),a={messageType:Te.RecordsWrite,author:this._connectedDid,target:e,dataStream:i,rawMessage:n},{reply:s}=await this.agent.sendDwnRequest(a);return s}async store(e){let{encodedData:r,...n}=this.rawMessage,i=new Blob([Ue.base64Url(r).toUint8Array()]),{reply:a,message:s}=await this.agent.processDwnRequest({author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,signAsOwner:e,rawMessage:n,dataStream:i});return this._message={...s,encodedData:r},{status:a.status}}};var np=class t{constructor({api:e,connectedDid:r,message:n,grant:i}){this._permissions=e,this._connectedDid=r,this._message=n,this._grant=i}static parse(e){let r=dr.parse(e.message),n=new fi({agent:e.agent});return new t({...e,grant:r,api:n})}get agent(){return this._permissions.agent}get id(){return this._grant.id}get grantor(){return this._grant.grantor}get grantee(){return this._grant.grantee}get dateGranted(){return this._grant.dateGranted}get description(){return this._grant.description}get requestId(){return this._grant.requestId}get dateExpires(){return this._grant.dateExpires}get delegated(){return this._grant.delegated}get scope(){return this._grant.scope}get conditions(){return this._grant.conditions}get rawMessage(){return this._message}async send(e){e??=this._connectedDid;let{encodedData:r,...n}=this._message,i=new Blob([Ue.base64Url(r).toUint8Array()]),a={messageType:Te.RecordsWrite,author:this._connectedDid,target:e,dataStream:i,rawMessage:n},{reply:s}=await this.agent.sendDwnRequest(a);return s}async store(e=!1){let{encodedData:r,...n}=this.rawMessage,i=new Blob([Ue.base64Url(r).toUint8Array()]),{reply:a,message:s}=await this.agent.processDwnRequest({store:!0,author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,signAsOwner:e,rawMessage:n,dataStream:i});return this._message={...s,encodedData:r},{status:a.status}}async import(e=!1){let{encodedData:r,...n}=this.rawMessage,i=new Blob([Ue.base64Url(r).toUint8Array()]),{reply:a,message:s}=await this.agent.processDwnRequest({store:e,author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,signAsOwner:!0,rawMessage:n,dataStream:i});return this._message={...s,encodedData:r},{status:a.status}}async revoke(e=!0){let r=await this._permissions.createRevocation({store:e,author:this._connectedDid,grant:this._grant});return PP.parse({connectedDid:this._connectedDid,agent:this.agent,message:r.message})}isRevoked(e=!1){return this._permissions.isGrantRevoked({author:this._connectedDid,target:this.grantor,grantRecordId:this.id,remote:e})}toJSON(){return{id:this.id,grantor:this.grantor,grantee:this.grantee,dateGranted:this.dateGranted,description:this.description,requestId:this.requestId,dateExpires:this.dateExpires,delegated:this.delegated,scope:this.scope,conditions:this.conditions}}};y();Kt();var _0=class t{constructor({api:e,connectedDid:r,message:n,request:i}){this._permissions=e,this._connectedDid=r,this._request=i,this._message=n}static parse({connectedDid:e,agent:r,message:n}){let i=Ia.parse(n),a=new fi({agent:r});return new t({api:a,connectedDid:e,message:n,request:i})}get agent(){return this._permissions.agent}get id(){return this._request.id}get requester(){return this._request.requester}get description(){return this._request.description}get delegated(){return this._request.delegated}get scope(){return this._request.scope}get conditions(){return this._request.conditions}get rawMessage(){return this._message}async send(e){e??=this._connectedDid;let{encodedData:r,...n}=this._message,i=new Blob([Ue.base64Url(r).toUint8Array()]),a={messageType:Te.RecordsWrite,author:this._connectedDid,target:e,dataStream:i,rawMessage:n},{reply:s}=await this.agent.sendDwnRequest(a);return s}async store(){let{encodedData:e,...r}=this.rawMessage,n=new Blob([Ue.base64Url(e).toUint8Array()]),{reply:i,message:a}=await this.agent.processDwnRequest({author:this._connectedDid,target:this._connectedDid,messageType:Te.RecordsWrite,rawMessage:r,dataStream:n});return this._message={...a,encodedData:e},{status:i.status}}async grant(e,r=!0){let{message:n}=await this._permissions.createGrant({requestId:this.id,grantedTo:this.requester,scope:this.scope,delegated:this.delegated,author:this._connectedDid,store:r,dateExpires:e});return np.parse({connectedDid:this._connectedDid,agent:this.agent,message:n})}toJSON(){return this._request}};y();var E0=class{constructor(e,r,n){this._agent=e,this._metadata=n,this._protocolsConfigureMessage=r}get definition(){return this._protocolsConfigureMessage.descriptor.definition}toJSON(){return this._protocolsConfigureMessage}async send(e){let{reply:r}=await this._agent.sendDwnRequest({author:this._metadata.author,messageCid:this._metadata.messageCid,messageType:Te.ProtocolsConfigure,target:e});return{status:r.status}}};var _P=class{constructor(e){this.agent=e.agent,this.connectedDid=e.connectedDid,this.delegateDid=e.delegateDid,this.permissionsApi=new fi({agent:this.agent})}get isDelegate(){return this.delegateDid!==void 0}get permissions(){return{request:async e=>{let{message:r}=await this.permissionsApi.createRequest({...e,author:this.delegateDid??this.connectedDid}),n={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:r};return _0.parse(n)},grant:async e=>{let{message:r}=await this.permissionsApi.createGrant({...e,author:this.delegateDid??this.connectedDid}),n={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:r};return np.parse(n)},queryRequests:async(e={})=>{let{from:r,...n}=e,i=await this.permissionsApi.fetchRequests({...n,author:this.delegateDid??this.connectedDid,target:r??this.delegateDid??this.connectedDid,remote:r!==void 0}),a=[];for(let s of i){let o={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:s.message};a.push(_0.parse(o))}return a},queryGrants:async(e={})=>{let{from:r,...n}=e,i=r!==void 0,a=this.delegateDid??this.connectedDid,s=r??this.delegateDid??this.connectedDid,o=await this.permissionsApi.fetchGrants({...n,author:a,target:s,remote:i}),d=[];for(let u of o){let c={connectedDid:this.delegateDid??this.connectedDid,agent:this.agent,message:u.message};d.push(np.parse(c))}return d}}}get protocols(){return{configure:async e=>{let{encryption:r,...n}=e,i={author:this.connectedDid,messageParams:n,messageType:Te.ProtocolsConfigure,target:this.connectedDid,encryption:r};if(this.delegateDid){let{message:c}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n.definition.protocol,delegate:!0,cached:!0,messageType:i.messageType});i.messageParams={...i.messageParams,delegatedGrant:c},i.granteeDid=this.delegateDid}let a=await this.agent.processDwnRequest(i),{message:s,messageCid:o,reply:{status:d}}=a,u={status:d};if(d.code<300){let c={author:this.connectedDid,messageCid:o};u.protocol=new E0(this.agent,s,c)}return u},query:async e=>{let{from:r,...n}=e,i={author:this.connectedDid,messageParams:n,messageType:Te.ProtocolsQuery,target:r||this.connectedDid};if(this.delegateDid)try{let{grant:{id:c}}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n.filter.protocol,cached:!0,messageType:i.messageType});i.messageParams={...i.messageParams,permissionGrantId:c},i.granteeDid=this.delegateDid}catch{i.author=this.delegateDid}let a;r?a=await this.agent.sendDwnRequest(i):a=await this.agent.processDwnRequest(i);let s=a.reply,{entries:o=[],status:d}=s;return{protocols:o.map(c=>{let l={author:this.connectedDid};return new E0(this.agent,c,l)}),status:d}}}}get records(){return{delete:async e=>{let{from:r,protocol:n,...i}=e,a={author:this.connectedDid,messageParams:i,messageType:Te.RecordsDelete,target:r||this.connectedDid};if(this.delegateDid){let{message:d}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n,delegate:!0,cached:!0,messageType:a.messageType});a.messageParams={...a.messageParams,delegatedGrant:d},a.granteeDid=this.delegateDid}let s;r?s=await this.agent.sendDwnRequest(a):s=await this.agent.processDwnRequest(a);let{reply:{status:o}}=s;return{status:o}},query:async e=>{let{from:r,encryption:n,...i}=e,a={author:this.connectedDid,messageParams:i,messageType:Te.RecordsQuery,target:r||this.connectedDid,encryption:n};if(this.delegateDid)try{let{message:f}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:i.filter?.protocol,delegate:!0,cached:!0,messageType:a.messageType});a.messageParams={...a.messageParams,delegatedGrant:f},a.granteeDid=this.delegateDid}catch{a.author=this.delegateDid}let s;r?s=await this.agent.sendDwnRequest(a):s=await this.agent.processDwnRequest(a);let o=s.reply,{entries:d=[],status:u,cursor:c}=o;return{records:d.map(f=>{let h={author:Wo(f),connectedDid:this.connectedDid,remoteOrigin:r,delegateDid:this.delegateDid,protocolRole:a.messageParams.protocolRole,...f};return new jd(this.agent,h,this.permissionsApi)}),status:u,cursor:c}},read:async e=>{let{from:r,protocol:n,encryption:i,...a}=e,s={author:this.connectedDid,messageParams:a,messageType:Te.RecordsRead,target:r||this.connectedDid,encryption:i};if(this.delegateDid)try{let{message:l}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n,delegate:!0,cached:!0,messageType:s.messageType});s.messageParams={...s.messageParams,delegatedGrant:l},s.granteeDid=this.delegateDid}catch{s.author=this.delegateDid}let o;r?o=await this.agent.sendDwnRequest(s):o=await this.agent.processDwnRequest(s);let{reply:{entry:d,status:u}}=o,c;if(200<=u.code&&u.code<=299){let l={author:Wo(d.recordsWrite),connectedDid:this.connectedDid,remoteOrigin:r,delegateDid:this.delegateDid,data:d.data,initialWrite:d.initialWrite,...d.recordsWrite};c=new jd(this.agent,l,this.permissionsApi)}return{record:c,status:u}},subscribe:async e=>{let{from:r,...n}=e,i,a=r,s=n.protocolRole,o=m=>{if(m.type==="eose"){i?.handleLifecycleEvent("eose");return}if(m.type==="disconnected"){i?.handleLifecycleEvent("disconnected");return}if(m.type==="reconnected"){i?.handleLifecycleEvent("reconnected");return}if(m.type==="reconnecting"){i?.handleLifecycleEvent("reconnecting",{attempt:m.attempt});return}let{message:g,initialWrite:_}=m.event,v=new jd(this.agent,{...g,author:Wo(g),connectedDid:this.connectedDid,remoteOrigin:a,initialWrite:_,protocolRole:s,delegateDid:this.delegateDid},this.permissionsApi);i?.handleEvent(v)},d={author:this.connectedDid,messageParams:n,messageType:Te.RecordsSubscribe,target:r||this.connectedDid,subscriptionHandler:o};if(this.delegateDid)try{let{message:m}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:n.filter?.protocol,delegate:!0,cached:!0,messageType:d.messageType});d.messageParams={...d.messageParams,delegatedGrant:m},d.granteeDid=this.delegateDid}catch{d.author=this.delegateDid}let u;r?u=await this.agent.sendDwnRequest(d):u=await this.agent.processDwnRequest(d);let c=u.reply,{status:l,subscription:f,entries:h=[],cursor:p}=c;return f&&(i=new xP({agent:this.agent,connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocolRole:s,remoteOrigin:a,permissionsApi:this.permissionsApi,initialEntries:h,cursor:p,subscription:f})),{status:l,liveQuery:i}},write:async e=>{let{data:r,store:n,encryption:i,...a}=e,{dataBlob:s,dataFormat:o}=P0(r,a.dataFormat),d={...a,dataFormat:o},u={store:n,messageType:Te.RecordsWrite,messageParams:d,author:this.connectedDid,target:this.connectedDid,dataStream:s,encryption:i};if(this.delegateDid){let{message:p}=await this.permissionsApi.getPermissionForRequest({connectedDid:this.connectedDid,delegateDid:this.delegateDid,protocol:d.protocol,delegate:!0,cached:!0,messageType:u.messageType});u.messageParams={...u.messageParams,delegatedGrant:p},u.granteeDid=this.delegateDid}let c=await this.agent.processDwnRequest(u),{message:l,reply:{status:f}}=c,h;if(200<=f.code&&f.code<=299){let p={author:this.connectedDid,connectedDid:this.connectedDid,encodedData:s,delegateDid:this.delegateDid,...l};h=new jd(this.agent,p,this.permissionsApi)}return{record:h,status:f}}}}};y();y();y();var $c=class t{constructor(e){this._record=e}get rawRecord(){return this._record}get data(){let e=this._record.data;return{blob:()=>e.blob(),bytes:()=>e.bytes(),json:()=>e.json(),text:()=>e.text(),stream:()=>e.stream(),then:e.then.bind(e),catch:e.catch.bind(e)}}async update(e){let{status:r,record:n}=await this._record.update(e);return{status:r,record:new t(n)}}async delete(e){let{status:r,record:n}=await this._record.delete(e);return{status:r,record:new t(n)}}async store(e=!1){return this._record.store(e)}async import(e=!0){return this._record.import(e)}async send(e){return this._record.send(e)}toJSON(){return this._record.toJSON()}toString(){return this._record.toString()}async paginationCursor(e){return this._record.paginationCursor(e)}get id(){return this._record.id}get contextId(){return this._record.contextId}get dateCreated(){return this._record.dateCreated}get parentId(){return this._record.parentId}get protocol(){return this._record.protocol}get protocolPath(){return this._record.protocolPath}get recipient(){return this._record.recipient}get schema(){return this._record.schema}get dataFormat(){return this._record.dataFormat}get dataCid(){return this._record.dataCid}get dataSize(){return this._record.dataSize}get datePublished(){return this._record.datePublished}get published(){return this._record.published}get tags(){return this._record.tags}get author(){return this._record.author}get creator(){return this._record.creator}get timestamp(){return this._record.timestamp}get encryption(){return this._record.encryption}get authorization(){return this._record.authorization}get attestation(){return this._record.attestation}get protocolRole(){return this._record.protocolRole}get deleted(){return this._record.deleted}get initialWrite(){return this._record.initialWrite}get rawMessage(){return this._record.rawMessage}};var EP=class{constructor(e){this._liveQuery=e}get rawLiveQuery(){return this._liveQuery}get records(){return this._typedRecords||(this._typedRecords=this._liveQuery.records.map(e=>new $c(e))),this._typedRecords}get cursor(){return this._liveQuery.cursor}get isConnected(){return this._liveQuery.isConnected}on(e,r){return e==="disconnected"?this._liveQuery.on("disconnected",r):e==="reconnected"?this._liveQuery.on("reconnected",r):e==="eose"?this._liveQuery.on("eose",r):e==="reconnecting"?this._liveQuery.on("reconnecting",r):e==="change"?this._liveQuery.on("change",n=>{r({type:n.type,record:new $c(n.record)})}):this._liveQuery.on(e,n=>{r(new $c(n))})}async close(){return this._liveQuery.close()}};var TP=class{constructor(e,r){this._configured=!1;this._ensureReadyPromise=null;this._dwn=e,this._definition=r.definition,this._validPaths=y7(this._definition.structure)}get protocol(){return this._definition.protocol}get definition(){return this._definition}async configure(e){let{protocols:r}=await this._dwn.protocols.query({filter:{protocol:this._definition.protocol}});if(r.length>0){let a=r[0];if(m7(a.definition,this._definition))return this._configured=!0,{status:{code:200,detail:"OK"},protocol:a}}let n=!this._dwn.isDelegate&&Object.values(this._definition.types).some(a=>a?.encryptionRequired===!0),i=await this._dwn.protocols.configure({definition:this._definition,encryption:e?.encryption??(n||void 0)});return i.status.code===202&&(this._configured=!0),i}get isConfigured(){return this._configured}async subscribe(e){await this._autoConfigureOnce();let{liveQuery:r}=await this._dwn.records.subscribe({from:e?.from,filter:{protocol:this._definition.protocol}});return r}_assertValidPath(e){if(!this._validPaths.has(e))throw new Error(`TypedEnbox: invalid protocol path '${e}'. Valid paths are: ${[...this._validPaths].join(", ")}.`)}async _ensureReady(e){if(this._configured){this._assertValidPath(e);return}this._ensureReadyPromise===null&&(this._ensureReadyPromise=this._autoConfigureOnce()),await this._ensureReadyPromise,this._assertValidPath(e)}async _autoConfigureOnce(){let{protocols:e}=await this._dwn.protocols.query({filter:{protocol:this._definition.protocol}});if(e.length>0){let i=e[0];if(m7(i.definition,this._definition)){this._configured=!0;return}console.warn(`TypedEnbox: installed protocol '${this._definition.protocol}' differs from the provided definition. Call configure() to update it.`),this._configured=!0;return}let r=!this._dwn.isDelegate&&Object.values(this._definition.types).some(i=>i?.encryptionRequired===!0);(await this._dwn.protocols.configure({definition:this._definition,encryption:r||void 0})).status.code===202&&(this._configured=!0)}get records(){if(this._records!==void 0)return this._records;let e={create:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=!this._dwn.isDelegate&&s?.encryptionRequired===!0?!0:void 0,{status:d,record:u}=await this._dwn.records.write({data:n.data,store:n.store,encryption:n.encryption??o,parentContextId:n.parentContextId,published:n.published,datePublished:n.datePublished,recipient:n.recipient,protocolRole:n.protocolRole,tags:n.tags,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{},dataFormat:n.dataFormat??s?.dataFormats?.[0]});return{status:d,record:u?new $c(u):void 0}},query:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=oR(n?.filter),d=!this._dwn.isDelegate&&s?.encryptionRequired===!0?!0:void 0,{status:u,records:c,cursor:l}=await this._dwn.records.query({from:n?.from,encryption:n?.encryption??d,filter:{...o,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{}},dateSort:n?.dateSort,pagination:n?.pagination,protocolRole:n?.protocolRole});return{status:u,records:c.map(f=>new $c(f)),cursor:l}},read:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=oR(n.filter),d=!this._dwn.isDelegate&&s?.encryptionRequired===!0?!0:void 0,{status:u,record:c}=await this._dwn.records.read({from:n.from,encryption:n.encryption??d,protocol:this._definition.protocol,filter:{...o,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{}}});return{status:u,record:c?new $c(c):void 0}},delete:async(r,n)=>(await this._ensureReady(S0(r)),this._dwn.records.delete({from:n.from,protocol:this._definition.protocol,recordId:n.recordId})),subscribe:async(r,n)=>{let i=S0(r);await this._ensureReady(i);let a=SP(i),s=this._definition.types[a],o=oR(n?.filter),{status:d,liveQuery:u}=await this._dwn.records.subscribe({from:n?.from,filter:{...o,protocol:this._definition.protocol,protocolPath:i,...s?.schema!==void 0?{schema:s.schema}:{}},protocolRole:n?.protocolRole});return{status:d,liveQuery:u?new EP(u):void 0}}};return this._records=e,e}};function oR(t){if(!t)return;let{parentContextId:e,...r}=t;return e!==void 0&&r.parentId===void 0&&(r.parentId=e),r}function m7(t,e){return kP(t)===kP(e)}function S0(t){return t.replace(/^\/+|\/+$/g,"")}function SP(t){let e=t.split("/");return e[e.length-1]}function y7(t,e=""){let r=new Set;for(let n of Object.keys(t)){if(n.startsWith("$"))continue;let i=e?`${e}/${n}`:n;r.add(i);let a=t[n];if(a!==null&&typeof a=="object")for(let s of y7(a,i))r.add(s)}return r}function kP(t){return t==null||typeof t!="object"?JSON.stringify(t):Array.isArray(t)?"["+t.map(n=>kP(n)).join(",")+"]":"{"+Object.keys(t).sort().map(n=>JSON.stringify(n)+":"+kP(t[n])).join(",")+"}"}y();var AP=class{constructor(e){this.agent=e.agent,this.connectedDid=e.connectedDid}async create(){throw new Error("Not implemented.")}};var g7=class t{constructor({agent:e,connectedDid:r,delegateDid:n}){this._typedInstances=new Map;this.agent=e,this.did=new vP({agent:e,connectedDid:r}),this._dwn=new _P({agent:e,connectedDid:r,delegateDid:n}),this.vc=new AP({agent:e,connectedDid:r})}using(e){let r=e.definition.protocol,n=this._typedInstances.get(r);if(n)return n;let i=new TP(this._dwn,e);return this._typedInstances.set(r,i),i}async disconnect(e){"sync"in this.agent&&typeof this.agent.sync?.stopSync=="function"&&await this.agent.sync.stopSync(e),this._typedInstances.clear()}static anonymous(e){let r=new s0({didResolvers:e?.didResolvers??[tl,Jf,qx,kI],cache:new Lx}),n=new Rm,i=new lP({didResolver:r,rpcClient:n});return{dwn:new bP(i)}}static connect(e){if("session"in e){let{session:r}=e;return new t({agent:r.agent,connectedDid:r.did,delegateDid:r.delegateDid})}return new t(e)}};y();y();y();function w7(t){return t.code===400&&t.detail.includes("record limit")}function Ppe(t,e){let r=e.split("/"),n=t.structure;for(let a of r){if(!n||typeof n!="object")return!1;n=n[a]}if(!n||typeof n!="object")return!1;let i=n.$recordLimit;return i!==void 0&&typeof i=="object"&&i!==null&&i.max===1}function _pe(t,e){let r=e.split("/"),n=t.structure;for(let i of r){if(!n||typeof n!="object")return[];n=n[i]}return!n||typeof n!="object"?[]:Object.keys(n).filter(i=>!i.startsWith("$"))}function Epe(t,e){return{async create(r){let{status:n,record:i}=await t.records.create(e,r);return{status:n,record:i}},async query(r){let{status:n,records:i,cursor:a}=await t.records.query(e,r);return{status:n,records:i,cursor:a}},async get(r){let{record:n}=await t.records.read(e,{filter:{recordId:r}});return n},async delete(r){return t.records.delete(e,{recordId:r})},async subscribe(r){let{liveQuery:n}=await t.records.subscribe(e,r);return n}}}function Spe(t,e){return{async set(r){let n=await t.records.create(e,r);if(w7(n.status)){let{records:i}=await t.records.query(e);if(i.length>0){let{status:a,record:s}=await i[0].update({data:r.data,...r.tags!==void 0?{tags:r.tags}:{}});return{status:a,record:s}}}return{status:n.status,record:n.record}},async get(){let{records:r}=await t.records.query(e);return r.length>0?r[0]:void 0},async delete(r){return t.records.delete(e,{recordId:r})}}}function Tpe(t,e){return{async create(r,n){let{status:i,record:a}=await t.records.create(e,{...n,parentContextId:r});return{status:i,record:a}},async query(r,n){let{status:i,records:a,cursor:s}=await t.records.query(e,{...n,filter:{...n?.filter,contextId:r}});return{status:i,records:a,cursor:s}},async get(r){let{record:n}=await t.records.read(e,{filter:{recordId:r}});return n},async delete(r){return t.records.delete(e,{recordId:r})},async subscribe(r,n){let{liveQuery:i}=await t.records.subscribe(e,{...n,filter:{...n?.filter,contextId:r}});return i}}}function kpe(t,e){return{async set(r,n){let i=await t.records.create(e,{...n,parentContextId:r});if(w7(i.status)){let{records:a}=await t.records.query(e,{filter:{contextId:r}});if(a.length>0){let{status:s,record:o}=await a[0].update({data:n.data,...n.tags!==void 0?{tags:n.tags}:{}});return{status:s,record:o}}}return{status:i.status,record:i.record}},async get(r){let{records:n}=await t.records.query(e,{filter:{contextId:r}});return n.length>0?n[0]:void 0},async delete(r){return t.records.delete(e,{recordId:r})}}}function v7(t,e,r,n){let i=Ppe(e,r),a;n?a=i?kpe(t,r):Tpe(t,r):a=i?Spe(t,r):Epe(t,r);let s=_pe(e,r),o={};return new Proxy(a,{get(d,u){if(typeof u=="string"){if(u in d)return d[u];if(s.includes(u))return u in o||(o[u]=v7(t,e,`${r}/${u}`,!0)),o[u]}}})}function MJe(t){let e=t.definition,r=Object.keys(e.structure).filter(a=>!a.startsWith("$")),n={};return new Proxy({},{get(a,s){if(typeof s=="string"){if(s==="configure")return async o=>await t.configure(o);if(r.includes(s))return s in n||(n[s]=v7(t,e,s,!1)),n[s]}}})}y();export{vP as DidApi,bP as DwnReaderApi,g7 as Enbox,xP as LiveQuery,np as PermissionGrant,PP as PermissionGrantRevocation,_0 as PermissionRequest,E0 as Protocol,b0 as ReadOnlyRecord,jd as Record,iR as RecordChangeEvent,TP as TypedEnbox,EP as TypedLiveQuery,$c as TypedRecord,AP as VcApi,h7 as createRecordData,Npe as defineProtocol,f7 as isOk,MJe as repository,p7 as utils};
2080
2080
  /*! Bundled license information:
2081
2081
 
2082
2082
  ieee754/index.js: