@@ -15,15 +15,16 @@ import { ok, error, Failure } from '@ucanto/server'
1515 * Middleware<DelegationsStorageContext, DelegationsStorageContext, DelegationsStorageEnvironment>
1616 * )}
1717 */
18- export const withDelegationsStorage = ( handler ) => async ( request , env , ctx ) => {
19- if ( env . FF_DELEGATIONS_STORAGE_ENABLED !== 'true' ) {
20- return handler ( request , env , ctx )
18+ export const withDelegationsStorage =
19+ ( handler ) => async ( request , env , ctx ) => {
20+ if ( env . FF_DELEGATIONS_STORAGE_ENABLED !== 'true' ) {
21+ return handler ( request , env , ctx )
22+ }
23+ return handler ( request , env , {
24+ ...ctx ,
25+ delegationsStorage : createStorage ( env )
26+ } )
2127 }
22- return handler ( request , env , {
23- ...ctx ,
24- delegationsStorage : createStorage ( env )
25- } )
26- }
2728
2829/**
2930 * @param {DelegationsStorageEnvironment } env
@@ -34,35 +35,46 @@ function createStorage (env) {
3435 /**
3536 * Finds the delegation proofs for the given space
3637 *
37- * @param {import('@web3-storage /capabilities/types').SpaceDID } space
38+ * @param {import('@storacha /capabilities/types').SpaceDID } space
3839 * @returns {Promise<Ucanto.Result<Ucanto.Delegation<Ucanto.Capabilities>[], DelegationNotFound | Ucanto.Failure>> }
3940 */
4041 find : async ( space ) => {
4142 /** @type {Ucanto.Delegation<Ucanto.Capabilities>[] } */
4243 const delegations = [ ]
43- const result = await env . CONTENT_SERVE_DELEGATIONS_STORE . list ( { prefix : space } )
44- await Promise . all ( result . keys . map ( async ( key ) => {
45- const delegation = await env . CONTENT_SERVE_DELEGATIONS_STORE . get ( key . name , 'arrayBuffer' )
46- if ( delegation ) {
47- const d = await Delegation . extract ( new Uint8Array ( delegation ) )
48- if ( d . ok ) delegations . push ( d . ok )
49- else console . error ( 'error while extracting delegation' , d . error )
50- }
51- } ) )
44+ const result = await env . CONTENT_SERVE_DELEGATIONS_STORE . list ( {
45+ prefix : space
46+ } )
47+ await Promise . all (
48+ result . keys . map ( async ( key ) => {
49+ const delegation = await env . CONTENT_SERVE_DELEGATIONS_STORE . get (
50+ key . name ,
51+ 'arrayBuffer'
52+ )
53+ if ( delegation ) {
54+ const d = await Delegation . extract ( new Uint8Array ( delegation ) )
55+ if ( d . ok ) delegations . push ( d . ok )
56+ else console . error ( 'error while extracting delegation' , d . error )
57+ }
58+ } )
59+ )
5260 return ok ( delegations )
5361 } ,
5462
5563 /**
5664 * Stores the delegation proofs for the given space.
5765 * If the delegation has an expiration, it will be stored with an expiration time in seconds since unix epoch.
5866 *
59- * @param {import('@web3-storage /capabilities/types').SpaceDID } space
67+ * @param {import('@storacha /capabilities/types').SpaceDID } space
6068 * @param {Ucanto.Delegation<Ucanto.Capabilities> } delegation
6169 * @returns {Promise<Ucanto.Result<Ucanto.Unit, StoreOperationFailed | Ucanto.Failure>> }
6270 */
6371 store : async ( space , delegation ) => {
6472 let options = { }
65- if ( delegation . expiration && delegation . expiration > 0 && delegation . expiration !== Infinity ) {
73+ if (
74+ delegation . expiration &&
75+ delegation . expiration > 0 &&
76+ delegation . expiration !== Infinity
77+ ) {
6678 // expire the key-value pair when the delegation expires (seconds since epoch)
6779 options = { expiration : delegation . expiration }
6880 }
@@ -74,7 +86,11 @@ function createStorage (env) {
7486 }
7587
7688 try {
77- await env . CONTENT_SERVE_DELEGATIONS_STORE . put ( `${ space } :${ delegation . cid . toString ( ) } ` , value . ok . buffer , options )
89+ await env . CONTENT_SERVE_DELEGATIONS_STORE . put (
90+ `${ space } :${ delegation . cid . toString ( ) } ` ,
91+ value . ok . buffer ,
92+ options
93+ )
7894 return ok ( { } )
7995 } catch ( /** @type {any } */ err ) {
8096 const message = `error while storing delegation for space ${ space } `
0 commit comments