-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: account plan subscriptions and space usage API sugar #1171
Merged
+211
−72
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,99 @@ | ||
export * from '@web3-storage/access/space' | ||
import * as Usage from './capability/usage.js' | ||
import * as API from './types.js' | ||
|
||
export class Space { | ||
/** @type {import('./types.js').DID} */ | ||
#did | ||
/** | ||
* @typedef {object} Model | ||
* @property {API.SpaceDID} id | ||
* @property {{name?:string}} [meta] | ||
* @property {API.Agent} agent | ||
*/ | ||
|
||
/** @type {Record<string, any>} */ | ||
#meta | ||
export class Space { | ||
#model | ||
|
||
/** | ||
* @param {import('./types.js').DID} did | ||
* @param {Record<string, any>} meta | ||
* @param {Model} model | ||
*/ | ||
constructor(did, meta = {}) { | ||
this.#did = did | ||
this.#meta = meta | ||
constructor(model) { | ||
this.#model = model | ||
this.usage = new StorageUsage(model) | ||
} | ||
|
||
/** | ||
* The given space name. | ||
*/ | ||
get name() { | ||
/* c8 ignore next */ | ||
return String(this.#meta.name ?? '') | ||
return String(this.#model.meta?.name ?? '') | ||
} | ||
|
||
/** | ||
* The DID of the space. | ||
*/ | ||
did() { | ||
return this.#did | ||
return this.#model.id | ||
} | ||
|
||
/** | ||
* Whether the space has been registered with the service. | ||
* User defined space metadata. | ||
*/ | ||
registered() { | ||
return Boolean(this.#meta.isRegistered) | ||
meta() { | ||
return this.#model.meta | ||
} | ||
} | ||
|
||
export class StorageUsage { | ||
#model | ||
|
||
/** | ||
* User defined space metadata. | ||
* @param {Model} model | ||
*/ | ||
meta() { | ||
return this.#meta | ||
constructor(model) { | ||
this.#model = model | ||
} | ||
|
||
/** | ||
* Get the current usage in bytes. | ||
*/ | ||
async get() { | ||
const { agent } = this.#model | ||
const space = this.#model.id | ||
const now = new Date() | ||
const period = { | ||
// we may not have done a snapshot for this month _yet_, so get report | ||
// from last month -> now | ||
from: startOfLastMonth(now), | ||
to: now, | ||
} | ||
const result = await Usage.report({ agent }, { space, period }) | ||
/* c8 ignore next */ | ||
if (result.error) return result | ||
|
||
const provider = /** @type {API.ProviderDID} */ (agent.connection.id.did()) | ||
const report = result.ok[provider] | ||
|
||
return { | ||
/* c8 ignore next */ | ||
ok: report?.size.final == null ? undefined : BigInt(report.size.final), | ||
} | ||
} | ||
} | ||
|
||
/** @param {string|number|Date} now */ | ||
const startOfMonth = (now) => { | ||
const d = new Date(now) | ||
d.setUTCDate(1) | ||
d.setUTCHours(0) | ||
d.setUTCMinutes(0) | ||
d.setUTCSeconds(0) | ||
d.setUTCMilliseconds(0) | ||
return d | ||
} | ||
|
||
/** @param {string|number|Date} now */ | ||
const startOfLastMonth = (now) => { | ||
const d = startOfMonth(now) | ||
d.setUTCMonth(d.getUTCMonth() - 1) | ||
return d | ||
} |
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 |
---|---|---|
|
@@ -240,6 +240,29 @@ export const testAccount = { | |
assert.ok(plan?.product, 'did:web:free.web3.storage') | ||
}, | ||
|
||
'check account subscriptions': async ( | ||
assert, | ||
{ client, mail, grantAccess } | ||
) => { | ||
const space = await client.createSpace('test') | ||
|
||
const email = '[email protected]' | ||
const login = Account.login(client, email) | ||
const message = await mail.take() | ||
assert.deepEqual(message.to, email) | ||
await grantAccess(message) | ||
const account = Result.try(await login) | ||
|
||
Result.try(await account.provision(space.did())) | ||
|
||
const subs = Result.unwrap(await account.plan.subscriptions()) | ||
|
||
assert.equal(subs.results.length, 1) | ||
assert.equal(subs.results[0].provider, client.defaultProvider()) | ||
assert.deepEqual(subs.results[0].consumers, [space.did()]) | ||
assert.equal(typeof subs.results[0].subscription, 'string') | ||
}, | ||
|
||
'space.save': async (assert, { client, mail, grantAccess }) => { | ||
const space = await client.createSpace('test') | ||
assert.deepEqual(client.spaces(), []) | ||
|
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,16 +1,56 @@ | ||
import * as Signer from '@ucanto/principal/ed25519' | ||
import assert from 'assert' | ||
import * as StoreCapabilities from '@web3-storage/capabilities/store' | ||
import * as Test from './test.js' | ||
import { Space } from '../src/space.js' | ||
import * as Account from '../src/account.js' | ||
import * as Result from '../src/result.js' | ||
import { randomCAR } from './helpers/random.js' | ||
|
||
describe('spaces', () => { | ||
it('should get meta', async () => { | ||
/** | ||
* @type {Test.Suite} | ||
*/ | ||
export const testSpace = { | ||
'should get meta': async (assert, { client }) => { | ||
const signer = await Signer.generate() | ||
const name = `space-${Date.now()}` | ||
const isRegistered = true | ||
const space = new Space(signer.did(), { name, isRegistered }) | ||
const space = new Space({ | ||
id: signer.did(), | ||
meta: { name }, | ||
agent: client.agent, | ||
}) | ||
assert.equal(space.did(), signer.did()) | ||
assert.equal(space.name, name) | ||
assert.equal(space.registered(), isRegistered) | ||
assert.equal(space.meta().name, name) | ||
}) | ||
}) | ||
assert.equal(space.meta()?.name, name) | ||
}, | ||
|
||
'should get usage': async (assert, { client, grantAccess, mail }) => { | ||
const space = await client.createSpace('test') | ||
|
||
const email = '[email protected]' | ||
const login = Account.login(client, email) | ||
const message = await mail.take() | ||
assert.deepEqual(message.to, email) | ||
await grantAccess(message) | ||
const account = Result.try(await login) | ||
|
||
Result.try(await account.provision(space.did())) | ||
await space.save() | ||
|
||
const size = 1138 | ||
const archive = await randomCAR(size) | ||
await client.agent.invokeAndExecute(StoreCapabilities.add, { | ||
nb: { | ||
link: archive.cid, | ||
size, | ||
}, | ||
}) | ||
|
||
const found = client.spaces().find((s) => s.did() === space.did()) | ||
if (!found) return assert.fail('space not found') | ||
|
||
const usage = Result.unwrap(await found.usage.get()) | ||
assert.equal(usage, BigInt(size)) | ||
}, | ||
} | ||
|
||
Test.test({ Space: testSpace }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes API. Should we either:
Any is fine, just noting the API change here