-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassert.js
42 lines (40 loc) · 1.62 KB
/
assert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as Server from '@ucanto/server'
import * as Digest from 'multiformats/hashes/digest'
import * as Assert from '../../capability/assert.js'
/**
* @param {import('./api.js').AssertServiceContext} context
* @returns {import('./api.js').AssertService}
*/
export function createService (context) {
return {
inclusion: Server.provide(Assert.inclusion, input => handler(input, context)),
index: Server.provide(Assert.index, input => handler(input, context)),
location: Server.provide(Assert.location, input => handler(input, context)),
partition: Server.provide(Assert.partition, input => handler(input, context)),
relation: Server.provide(Assert.relation, input => handler(input, context)),
equals: Server.provide(Assert.equals, input => handler(input, context))
}
}
/**
* @param {object} config
* @param {import('./api.js').AnyAssertCap} config.capability
* @param {import('@ucanto/interface').Invocation} config.invocation
* @param {import('./api.js').AssertServiceContext} context
* @returns {Promise<import('@ucanto/server').Result<{}, import('@ucanto/server').Failure>>}
*/
export const handler = async ({ capability, invocation }, { claimStore }) => {
const { content } = capability.nb
const archive = await invocation.archive()
if (archive.error) throw new Error('failed invocation archive', { cause: archive.error })
const claim = {
claim: invocation.cid,
bytes: archive.ok,
content: 'digest' in content
? Digest.decode(content.digest)
: content.multihash,
expiration: invocation.expiration,
value: capability
}
await claimStore.put(claim)
return { ok: {} }
}