Skip to content

Commit

Permalink
feat: add providers to space/info (#862)
Browse files Browse the repository at this point in the history
I believe we currently don't have any way for an agent to figure out
which providers a space is registered to - add a list of providers to
`space/info` to enable this in the CLI and w3console
  • Loading branch information
travis authored Aug 22, 2023
1 parent d76a6af commit ac72921
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
12 changes: 8 additions & 4 deletions packages/upload-api/src/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as API from './types.js'
/**
* @param {API.Input<Space.info>} input
* @param {API.SpaceServiceContext} ctx
* @returns {Promise<API.Result<{ did: API.SpaceDID }, API.Failure>>}
* @returns {Promise<API.Result<API.SpaceInfoSuccess, API.SpaceInfoFailure>>}
*/
export const info = async ({ capability }, ctx) => {
const { provisionsStorage: provisions } = ctx
Expand All @@ -23,10 +23,14 @@ export const info = async ({ capability }, ctx) => {
}
}

const result = await provisions.hasStorageProvider(spaceDid)
if (result.ok) {
const result = await provisions.getStorageProviders(spaceDid)
const providers = result.ok
if (providers && (providers.length > 0)) {
return {
ok: { did: spaceDid },
ok: {
did: spaceDid,
providers
},
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/upload-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import {
ProviderAddSuccess,
ProviderAddFailure,
SpaceInfo,
ProviderDID,
} from '@web3-storage/capabilities/types'
import * as Capabilities from '@web3-storage/capabilities'

Expand Down Expand Up @@ -173,7 +174,7 @@ export interface Service {
add: ServiceMethod<ProviderAdd, ProviderAddSuccess, ProviderAddFailure>
}
space: {
info: ServiceMethod<SpaceInfo, SpaceInfoResult, Failure | SpaceUnknown>
info: ServiceMethod<SpaceInfo, SpaceInfoSuccess, SpaceInfoFailure>
}
}

Expand Down Expand Up @@ -331,9 +332,11 @@ export interface UploadTable {
) => Promise<ListResponse<UploadListItem>>
}

export type SpaceInfoResult = {
export type SpaceInfoSuccess = {
did: SpaceDID
providers: ProviderDID[]
}
export type SpaceInfoFailure = Failure | SpaceUnknown

export interface UnknownProvider extends Failure {
name: 'UnknownProvider'
Expand Down
5 changes: 4 additions & 1 deletion packages/upload-api/src/types/provisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export interface Subscription {
export interface ProvisionsStorage<ProviderDID = Ucanto.DID<'web'>> {
services: ProviderDID[]
hasStorageProvider: (
consumer: Ucanto.DID<'key'>
consumer: Ucanto.DIDKey
) => Promise<Ucanto.Result<boolean, never>>
getStorageProviders: (
consumer: Ucanto.DIDKey
) => Promise<Ucanto.Result<ProviderDID[], never>>
/**
* ensure item is stored
*
Expand Down
31 changes: 25 additions & 6 deletions packages/upload-api/test/provisions-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,31 @@ export class ProvisionsStorage {
*
* @param {Types.DIDKey} consumer
*/
async getStorageProviders(consumer) {
return {
ok: Array.from(
Object.values(this.provisions)
.reduce((m, p) => {
if (p.consumer === consumer) {
m.add(p.provider)
}
return m
}, new Set())
)
}
}

/**
*
* @param {Types.DIDKey} consumer
*/
async hasStorageProvider(consumer) {
return {
ok: !!Object.values(this.provisions).find((i) => i.consumer === consumer),
ok: (await this.getStorageProviders(consumer)).ok.length > 0
}
}


/**
*
* @param {Types.Provision} item
Expand Down Expand Up @@ -79,11 +98,11 @@ export class ProvisionsStorage {
return exists
? { ok: { did: customer } }
: {
error: {
name: 'CustomerNotFound',
message: 'customer does not exist',
},
}
error: {
name: 'CustomerNotFound',
message: 'customer does not exist',
},
}
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/upload-api/test/space-info.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ describe('space/info', function () {
if (inv.out.error) {
assert.fail(inv.out.error.message)
} else {
assert.ok(
isSubset(inv.out.ok, {
did: space.did(),
})
)
assert.equal(inv.out.ok.did, space.did())
assert.deepEqual(inv.out.ok.providers, [service.did()])
}
})

Expand Down

0 comments on commit ac72921

Please sign in to comment.