-
Notifications
You must be signed in to change notification settings - Fork 22
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
Alan Shaw
committed
Nov 7, 2023
1 parent
6418b4b
commit 7f740f9
Showing
24 changed files
with
221 additions
and
15 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
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
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,9 +1,11 @@ | ||
import * as Types from './types.js' | ||
import * as Get from './subscription/get.js' | ||
import * as List from './subscription/list.js' | ||
|
||
/** | ||
* @param {Types.SubscriptionServiceContext} context | ||
*/ | ||
export const createService = (context) => ({ | ||
get: Get.provide(context), | ||
list: List.provide(context), | ||
}) |
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,17 @@ | ||
import * as API from '../types.js' | ||
import * as Server from '@ucanto/server' | ||
import { Subscription } from '@web3-storage/capabilities' | ||
|
||
/** | ||
* @param {API.SubscriptionServiceContext} context | ||
*/ | ||
export const provide = (context) => | ||
Server.provide(Subscription.list, (input) => list(input, context)) | ||
|
||
/** | ||
* @param {API.Input<Subscription.list>} input | ||
* @param {API.SubscriptionServiceContext} context | ||
* @returns {Promise<API.Result<API.SubscriptionListSuccess, API.SubscriptionListFailure>>} | ||
*/ | ||
const list = async ({ capability }, context) => | ||
context.subscriptionsStorage.list(capability.with) |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Result } from '@ucanto/interface' | ||
import { | ||
AccountDID, | ||
SubscriptionListSuccess, | ||
SubscriptionListFailure | ||
} from '@web3-storage/capabilities/types' | ||
|
||
export interface SubscriptionsStorage { | ||
list: ( | ||
account: AccountDID | ||
) => Promise<Result<SubscriptionListSuccess, SubscriptionListFailure>> | ||
} |
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,43 @@ | ||
import { Subscription } from '@web3-storage/capabilities' | ||
import * as API from '../../src/types.js' | ||
import { createServer, connect } from '../../src/lib.js' | ||
import { alice, registerSpace } from '../util.js' | ||
import { createAuthorization } from '../helpers/utils.js' | ||
|
||
/** @type {API.Tests} */ | ||
export const test = { | ||
'subscription/list retrieves subscriptions for account': async (assert, context) => { | ||
const spaces = await Promise.all([ | ||
registerSpace(alice, context, 'alic_e'), | ||
registerSpace(alice, context, 'alic_e') | ||
]) | ||
const connection = connect({ | ||
id: context.id, | ||
channel: createServer(context), | ||
}) | ||
|
||
const subListRes = await Subscription.list | ||
.invoke({ | ||
issuer: alice, | ||
audience: context.id, | ||
with: spaces[0].account.did(), | ||
nb: {}, | ||
proofs: await createAuthorization({ | ||
agent: alice, | ||
account: spaces[0].account, | ||
service: context.service, | ||
}), | ||
}) | ||
.execute(connection) | ||
|
||
assert.ok(subListRes.out.ok) | ||
|
||
const results = subListRes.out.ok?.results | ||
const totalConsumers = results?.reduce((total, s) => total + s.consumers.length, 0) | ||
assert.equal(totalConsumers, spaces.length) | ||
|
||
for (const space of spaces) { | ||
assert.ok(results?.some(s => s.consumers[0] === space.spaceDid)) | ||
} | ||
}, | ||
} |
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,3 @@ | ||
import * as Subscription from './subscription.js' | ||
import { test } from '../test.js' | ||
test({ 'subscription/*': Subscription.test }) |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @typedef {import('../../src/types/subscriptions.js').SubscriptionsStorage} SubscriptionsStore | ||
*/ | ||
|
||
/** | ||
* @implements {SubscriptionsStore} | ||
*/ | ||
export class SubscriptionsStorage { | ||
/** @param {import('./provisions-storage.js').ProvisionsStorage} provisions */ | ||
constructor (provisions) { | ||
this.provisionsStore = provisions | ||
} | ||
|
||
/** @param {import('../types.js').AccountDID} account */ | ||
async list(account) { | ||
/** @type {import('../types.js').SubscriptionListItem[]} */ | ||
const results = [] | ||
const entries = Object.entries(this.provisionsStore.provisions) | ||
for (const [subscription, { customer, provider, consumer }] of entries) { | ||
if (customer !== account) continue | ||
results.push({ | ||
subscription, | ||
provider, | ||
consumers: [consumer] | ||
}) | ||
} | ||
return { ok: { results } } | ||
} | ||
} |
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
Oops, something went wrong.