-
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.
feat: usage/record capability definition
- Loading branch information
Showing
10 changed files
with
210 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import { provide } from './usage/report.js' | ||
import { provide as provideReport } from './usage/report.js' | ||
import { provide as provideRecord } from './usage/record.js' | ||
|
||
/** @param {import('./types.js').UsageServiceContext} context */ | ||
export const createService = (context) => ({ report: provide(context) }) | ||
export const createService = (context) => ({ | ||
report: provideReport(context), | ||
record: provideRecord(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,24 @@ | ||
import * as API from '../types.js' | ||
import * as Provider from '@ucanto/server' | ||
import { Usage } from '@web3-storage/capabilities' | ||
|
||
/** @param {API.UsageServiceContext} context */ | ||
export const provide = (context) => | ||
Provider.provide(Usage.record, (input) => record(input, context)) | ||
|
||
/** | ||
* @param {API.Input<Usage.record>} input | ||
* @param {API.UsageServiceContext} context | ||
* @returns {Promise<API.Result<API.EgressRecordSuccess, API.EgressRecordFailure>>} | ||
*/ | ||
const record = async ({ capability }, context) => { | ||
const res = await context.usageStorage.record( | ||
capability.nb.customer, | ||
capability.nb.resourceCID, | ||
capability.nb.bytes, | ||
new Date(capability.nb.servedAt * 1000) | ||
) | ||
if (res.error) return res | ||
|
||
return res | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import { AgentData } from '@web3-storage/access/agent' | |
import { Client } from '../../src/client.js' | ||
import * as Test from '../test.js' | ||
import { receiptsEndpoint } from '../helpers/utils.js' | ||
import { randomCAR } from '../helpers/random.js' | ||
|
||
export const UsageClient = Test.withContext({ | ||
report: { | ||
|
@@ -68,6 +69,46 @@ export const UsageClient = Test.withContext({ | |
assert.deepEqual(report, {}) | ||
}, | ||
}, | ||
record: { | ||
'should record egress': async ( | ||
assert, | ||
{ connection, provisionsStorage } | ||
) => { | ||
const alice = new Client(await AgentData.create(), { | ||
// @ts-ignore | ||
serviceConf: { | ||
access: connection, | ||
upload: connection, | ||
}, | ||
}) | ||
|
||
const space = await alice.createSpace('test') | ||
const auth = await space.createAuthorization(alice) | ||
await alice.addSpace(auth) | ||
|
||
// Then we setup a billing for this account | ||
await provisionsStorage.put({ | ||
// @ts-expect-error | ||
provider: connection.id.did(), | ||
account: alice.agent.did(), | ||
consumer: space.did(), | ||
}) | ||
|
||
const car = await randomCAR(128) | ||
const resourceCID = car.cid | ||
await alice.capability.upload.add(car.roots[0], [resourceCID]) | ||
|
||
const result = await alice.capability.upload.get(car.roots[0]) | ||
|
||
const record = await alice.capability.usage.record(space.did(), { | ||
customer: 'did:mailto:[email protected]', | ||
resourceCID: resourceCID.link().toString(), | ||
bytes: result.root.byteLength, | ||
servedAt: new Date().toISOString(), | ||
}) | ||
assert.ok(record) | ||
}, | ||
}, | ||
}) | ||
|
||
Test.test({ UsageClient }) |