Skip to content

Commit

Permalink
implemented suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Dec 9, 2024
1 parent 14a69bc commit 36a99a0
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/middleware/withDelegationStubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const withDelegationStubs = (handler) => async (request, env, ctx) => {
return handler(request, env, {
...ctx,
delegationsStorage: {
find: async () => ({ ok: stubDelegations[0] }),
find: async () => ({ ok: stubDelegations }),
store: async () => ({ ok: {} })
},
locator:
Expand Down
3 changes: 1 addition & 2 deletions src/middleware/withUcanInvocationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export function withUcanInvocationHandler (handler) {

const { headers, body, status } = await server.request({
body: new Uint8Array(await request.arrayBuffer()),
// @ts-expect-error: TODO: fix the .entries() type
headers: Object.fromEntries(request.headers.entries())
headers: Object.fromEntries(request.headers)
})

return new Response(body, { headers, status: status ?? 200 })
Expand Down
10 changes: 8 additions & 2 deletions src/server/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export function createService (ctx) {
capability: AccessCapabilities.delegate,
audience: Schema.did({ method: 'web' }),
handler: async ({ capability, invocation, context }) => {
const result = extractContentServeDelegations(ctx.gatewayIdentity, capability, invocation.proofs)
const result = extractContentServeDelegations(capability, invocation.proofs)
if (result.error) {
console.error('error while extracting delegation', result.error)
return result
}

const delegations = result.ok
for (const delegation of delegations) {
const validationResults = await Promise.all(delegations.map(async (delegation) => {
const validationResult = await claim(
SpaceCapabilities.contentServe,
[delegation],
Expand All @@ -43,7 +43,13 @@ export function createService (ctx) {

const space = capability.with
return ctx.delegationsStorage.store(space, delegation)
}))

const errorResult = validationResults.find(result => result.error)
if (errorResult) {
return errorResult
}

return ok({})
}

Expand Down
8 changes: 3 additions & 5 deletions src/server/utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Access as AccessCapabilities, Space as SpaceCapabilities } from '@web3-storage/capabilities'
import { InvalidDelegation } from '../middleware/withDelegationsStorage.js'
import { Delegation } from '@storacha/client/delegation'

/**
* Checks if the space/content/serve/* delegation is for the gateway and it is not expired.
*
* @param {import('@ucanto/interface').Signer} gatewayIdentity - The signer of the gateway identity
* @param {import('@ucanto/interface').InferInvokedCapability<typeof AccessCapabilities.delegate>} capability - The capability to validate
* @param {import('@ucanto/interface').Proof[]} proofs - The proofs to validate
*/
export const extractContentServeDelegations = (gatewayIdentity, capability, proofs) => {
export const extractContentServeDelegations = (capability, proofs) => {
const nbDelegations = new Set(Object.values(capability.nb.delegations))
if (nbDelegations.size === 0) {
return { error: new InvalidDelegation('nb.delegations can not be empty') }
if (nbDelegations.size !== 1) {
return { error: new InvalidDelegation('nb.delegations has more than one delegation') }
}
const delegations = []
for (const delegationLink of nbDelegations) {
Expand Down
13 changes: 3 additions & 10 deletions test/unit/middleware/withDelegationsStorage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`no-unused-expressions` doesn't understand that several of Chai's assertions
are implemented as getters rather than explicit function calls; it thinks
the assertions are unused expressions. */
import { describe, it, afterEach, before } from 'mocha'
import { describe, it, afterEach } from 'mocha'
import { expect } from 'chai'
import sinon from 'sinon'
import { ed25519 } from '@ucanto/principal'
Expand All @@ -22,13 +22,6 @@ const kvStoreMock = {
* @typedef {import('../../../src/middleware/withDelegationsStorage.types.js').DelegationsStorageContext} DelegationsStorageContext
*/

const env =
/** @satisfies {DelegationsStorageEnvironment} */
({
FF_DELEGATIONS_STORAGE_ENABLED: 'true',
CONTENT_SERVE_DELEGATIONS_STORE: kvStoreMock
})

const gatewaySigner = (await ed25519.Signer.generate()).signer
const gatewayIdentity = gatewaySigner.withDID('did:web:test.w3s.link')

Expand Down Expand Up @@ -57,7 +50,7 @@ describe('withDelegationsStorage', async () => {

describe('-> Successful Requests', () => {
it('should set delegationsStorage in context when FF_DELEGATIONS_STORAGE_ENABLED is true', async () => {
const mockHandler = sinon.stub().callsFake((request, env, ctx) => ctx)
const mockHandler = sinon.fake((request, env, ctx) => ctx)
const request = new Request('http://example.com/')
const env = {
FF_DELEGATIONS_STORAGE_ENABLED: 'true',
Expand All @@ -76,7 +69,7 @@ describe('withDelegationsStorage', async () => {
})

it('should not set delegationsStorage in context when FF_DELEGATIONS_STORAGE_ENABLED is not true', async () => {
const mockHandler = sinon.stub().callsFake((request, env, ctx) => ctx)
const mockHandler = sinon.fake((request, env, ctx) => ctx)
const request = new Request('http://example.com/')
const env = {
FF_DELEGATIONS_STORAGE_ENABLED: 'false',
Expand Down
2 changes: 0 additions & 2 deletions test/unit/middleware/withEgressTracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ const EgressClient = () => {
}
const gatewaySigner = (await ed25519.Signer.generate()).signer
const gatewayIdentity = gatewaySigner.withDID('did:web:test.w3s.link')
/** @type {import('@ucanto/interface').Delegation<import('@ucanto/interface').Capabilities>[]} */
const stubDelegations = []

const ctx =
/** @satisfies {EgressTrackerContext} */
Expand Down
2 changes: 1 addition & 1 deletion test/unit/middleware/withUcanInvocationHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`no-unused-expressions` doesn't understand that several of Chai's assertions
are implemented as getters rather than explicit function calls; it thinks
the assertions are unused expressions. */
import { describe, it } from 'mocha'
import { describe, it, afterEach } from 'mocha'
import { expect } from 'chai'
import sinon from 'sinon'
import { ed25519 } from '@ucanto/principal'
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"moduleResolution": "NodeNext",
"skipLibCheck": true,
"resolveJsonModule": true,
"lib": ["ES2022", "DOM"],
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"target": "ES2022",
"sourceMap": true
}
Expand Down

0 comments on commit 36a99a0

Please sign in to comment.