Skip to content

Commit

Permalink
feat: access-api handling store/info for space not in db returns fail…
Browse files Browse the repository at this point in the history
…ure with name (#391)

Motivation:
* implement #382
* tl;dr `space/info` now has explicit failure type for case where the
ucans are valid, but the space DID is not in the db. that way when
upload-api invokes `space/info` as part of its `verifyInvocation` logic,
it can distinguish between an invalid invocation and a not-found space
id

Unblocks:
* this test passing gobengo/w3protocol-test#1
* rest of
storacha/w3infra#134 (comment)
  • Loading branch information
gobengo authored Jan 30, 2023
1 parent ec39443 commit 665dac9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/access-api/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export function service(ctx) {
info: Server.provide(Space.info, async ({ capability, invocation }) => {
const results = await ctx.models.spaces.get(capability.with)
if (!results) {
return new Failure('Space not found.')
/** @type {import('@web3-storage/access/types').SpaceUnknown} */
const spaceUnknownFailure = {
error: true,
name: 'SpaceUnknown',
message: `Space not found.`,
}
return spaceUnknownFailure
}
return results
}),
Expand Down
6 changes: 6 additions & 0 deletions packages/access-api/test/space-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('space/info', function () {

if (inv?.error) {
assert.deepEqual(inv.message, `Space not found.`)
const expectedErrorName = 'SpaceUnknown'
assert.deepEqual(
'name' in inv && inv.name,
expectedErrorName,
`error result has name ${expectedErrorName}`
)
} else {
assert.fail()
}
Expand Down
12 changes: 12 additions & 0 deletions packages/access-client/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Ucanto from '@ucanto/interface'

/**
* Indicates failure executing ability that requires access to a space that is not well-known enough to be handled.
* e.g. it's a space that's never been seen before,
* or it's a seen space that hasn't been fully registered such that the service can serve info about the space.
*/
export interface SpaceUnknown extends Ucanto.Failure {
error: true
message: string
name: 'SpaceUnknown'
}
8 changes: 7 additions & 1 deletion packages/access-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ import type {
import type { SetRequired } from 'type-fest'
import { Driver } from './drivers/types.js'
import type { ColumnType, Selectable } from 'kysely'
import { SpaceUnknown } from './errors.js'

// export other types
export * from '@web3-storage/capabilities/types'
export * from './errors.js'

/**
* D1 Types
Expand Down Expand Up @@ -73,7 +75,11 @@ export interface Service {
redeem: ServiceMethod<VoucherRedeem, void, Failure>
}
space: {
info: ServiceMethod<SpaceInfo, Selectable<SpaceTable>, Failure>
info: ServiceMethod<
SpaceInfo,
Selectable<SpaceTable>,
Failure | SpaceUnknown
>
'recover-validation': ServiceMethod<
SpaceRecoverValidation,
EncodedDelegation<[SpaceRecover]> | undefined,
Expand Down

0 comments on commit 665dac9

Please sign in to comment.