-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
168 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,13 @@ | ||
import * as Ucanto from '@ucanto/interface' | ||
import { Context as MiddlewareContext } from '@web3-storage/gateway-lib' | ||
import { GatewayIdentityContext as GatewayIdentityContext } from './withGatewayIdentity.types.js' | ||
|
||
export interface DelegationsStorageContext | ||
extends MiddlewareContext, | ||
GatewayIdentityContext { | ||
delegationsStorage: DelegationsStorage | ||
/** | ||
* The delegation proofs to use for the egress record | ||
* The proofs must be valid for the space and the owner of the space | ||
* must have delegated the right to the Gateway to serve content and record egress traffic. | ||
* The `space/content/serve/*` capability must be granted to the Gateway Web DID. | ||
*/ | ||
delegationProofs: Ucanto.Delegation[] | ||
} | ||
import { SpaceDID } from '@web3-storage/capabilities/types' | ||
|
||
export interface SpaceContext extends MiddlewareContext { | ||
space: Ucanto.DID | null | ||
space?: SpaceDID | ||
} | ||
|
||
// TEMP: https://github.com/storacha/blob-fetcher/pull/13/files | ||
declare module '@web3-storage/blob-fetcher' { | ||
interface Site { | ||
space?: Ucanto.DID | ||
space?: SpaceDID | ||
} | ||
} | ||
|
||
// TEMP | ||
|
||
export interface Query { | ||
audience?: Ucanto.DID | ||
can: string | ||
with: Ucanto.Resource | ||
} | ||
|
||
export interface DelegationsStorage { | ||
/** | ||
* find all items that match the query | ||
*/ | ||
find: ( | ||
query: Query | ||
) => Promise<Ucanto.Result<Ucanto.Delegation[], Ucanto.Failure>> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Delegation } from '@ucanto/core' | ||
import { DelegationFailure } from './withDelegationsStorage.types.js' | ||
|
||
/** | ||
* @import * as Ucanto from '@ucanto/interface' | ||
* @import { | ||
* Middleware, | ||
* } from '@web3-storage/gateway-lib' | ||
* @import { DelegationsStorageContext, Environment } from './withDelegationsStorage.types.js' | ||
*/ | ||
|
||
/** | ||
* Provides a delegations storage in the application context | ||
* | ||
* @type {( | ||
* Middleware<DelegationsStorageContext, DelegationsStorageContext, Environment> | ||
* )} | ||
*/ | ||
export const withDelegationsStorage = (handler) => async (request, env, ctx) => { | ||
return handler(request, env, { | ||
...ctx, | ||
delegationsStorage: createStorage(env), | ||
delegationProofs: [], // Delegation proofs are set by withAuthorizedSpace handler | ||
locator: ctx.locator | ||
}) | ||
} | ||
|
||
/** | ||
* @param {Environment} env | ||
* @returns {import('./withDelegationsStorage.types.js').DelegationsStorage} | ||
*/ | ||
function createStorage(env) { | ||
return { | ||
/** | ||
* Finds the delegation proofs for the given space | ||
* | ||
* @param {import('@web3-storage/capabilities/types').SpaceDID} space | ||
* @returns {Promise<Ucanto.Result<Ucanto.Delegation<Ucanto.Capabilities>, Ucanto.Failure>>} | ||
*/ | ||
find: async (space) => { | ||
if (!space) return { error: { name: 'MissingSpace', message: 'No space provided' } } | ||
const delegation = await env.CONTENT_SERVE_DELEGATIONS_STORE.get(space, 'arrayBuffer') | ||
if (!delegation) return { error: { name: 'DelegationNotFound', message: `No delegation found for space ${space}` } } | ||
const res = await Delegation.extract(new Uint8Array(delegation)) | ||
if (res.error) return res | ||
return { ok: res.ok } | ||
}, | ||
|
||
/** | ||
* Stores the delegation proofs for the given space. | ||
* If the delegation has an expiration, it will be stored with an expiration time in seconds since unix epoch. | ||
* | ||
* @param {import('@web3-storage/capabilities/types').SpaceDID} space | ||
* @param {Ucanto.Delegation<Ucanto.Capabilities>} delegation | ||
* @returns {Promise<Ucanto.Result<Ucanto.Unit, Ucanto.Failure>>} | ||
*/ | ||
store: async (space, delegation) => { | ||
try { | ||
let options = {} | ||
if (delegation.expiration && delegation.expiration > 0 && delegation.expiration !== Infinity) { | ||
// expire the key-value pair when the delegation expires (seconds since epoch) | ||
options = { expiration: delegation.expiration } | ||
} | ||
|
||
const value = await delegation.archive() | ||
if (value.error) return value | ||
|
||
await env.CONTENT_SERVE_DELEGATIONS_STORE.put(space, value.ok.buffer, options) | ||
return { ok: {} } | ||
} catch (error) { | ||
const message = `error while storing delegation for space ${space}` | ||
console.error(message, error) | ||
return { | ||
error: new DelegationFailure(message) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as Ucanto from '@ucanto/interface' | ||
import { Environment as MiddlewareEnvironment, Context as MiddlewareContext } from '@web3-storage/gateway-lib' | ||
import { KVNamespace } from '@cloudflare/workers-types' | ||
import { SpaceDID } from '@web3-storage/capabilities/types' | ||
import { Failure } from '@ucanto/core' | ||
import { GatewayIdentityContext } from './withGatewayIdentity.types.js' | ||
import { LocatorContext } from './withLocator.types.js' | ||
|
||
export interface Environment extends MiddlewareEnvironment { | ||
CONTENT_SERVE_DELEGATIONS_STORE: KVNamespace | ||
} | ||
|
||
export class DelegationFailure extends Failure { | ||
get name() { | ||
return /** @type {const} */ ('DelegationFailure') | ||
} | ||
} | ||
|
||
export interface DelegationsStorageContext | ||
extends MiddlewareContext, | ||
LocatorContext, | ||
GatewayIdentityContext { | ||
delegationsStorage: DelegationsStorage | ||
/** | ||
* The delegation proofs to use for the egress record | ||
* The proofs must be valid for the space and the owner of the space | ||
* must have delegated the right to the Gateway to serve content and record egress traffic. | ||
* The `space/content/serve/*` capability must be granted to the Gateway Web DID. | ||
*/ | ||
delegationProofs: Ucanto.Delegation[] | ||
} | ||
|
||
export interface DelegationsStorage { | ||
/** | ||
* Finds the delegation proofs for the given space | ||
* | ||
* @param {import('@web3-storage/capabilities/types').SpaceDID} space | ||
* @returns {Promise<Ucanto.Result<Ucanto.Delegation<Ucanto.Capabilities>, Ucanto.Failure>>} | ||
*/ | ||
find: ( | ||
space: SpaceDID | ||
) => Promise<Ucanto.Result<Ucanto.Delegation<Ucanto.Capabilities>, Ucanto.Failure>> | ||
|
||
/** | ||
* Stores the delegation proofs for the given space | ||
* | ||
* @param {import('@web3-storage/capabilities/types').SpaceDID} space | ||
* @param {Ucanto.Delegation<Ucanto.Capabilities>} delegation | ||
* @returns {Promise<Ucanto.Result<Ucanto.Unit, Ucanto.Failure>>} | ||
*/ | ||
store: ( | ||
space: SpaceDID, | ||
delegation: Ucanto.Delegation<Ucanto.Capabilities> | ||
) => Promise<Ucanto.Result<Ucanto.Unit, Ucanto.Failure>> | ||
} |
Oops, something went wrong.