-
Notifications
You must be signed in to change notification settings - Fork 7
/
provisions.js
140 lines (129 loc) · 4.5 KB
/
provisions.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ConflictError as ConsumerConflictError } from '../tables/consumer.js'
import { ConflictError as SubscriptionConflictError } from '../tables/subscription.js'
import { CBOR, Failure } from '@ucanto/server'
/**
* Create a subscription ID for a given provision. Currently
* uses a CID generated from `consumer` which ensures a space
* can be provisioned at most once.
*
* @param {import('@web3-storage/upload-api').Provision} item
* @returns string
*/
export const createProvisionSubscriptionId = async ({ customer, consumer }) =>
(await CBOR.write({ consumer })).cid.toString()
/**
* @param {import('../types').SubscriptionTable} subscriptionTable
* @param {import('../types').ConsumerTable} consumerTable
* @param {import('../types').SpaceMetricsTable} spaceMetricsTable
* @param {import('@ucanto/interface').DID<'web'>[]} services
* @returns {import('@web3-storage/upload-api').ProvisionsStorage}
*/
export function useProvisionStore (subscriptionTable, consumerTable, spaceMetricsTable, services) {
return {
services,
hasStorageProvider: async (consumer) => (
{ ok: await consumerTable.hasStorageProvider(consumer) }
),
getStorageProviders: async (consumer) => (
{ ok: await consumerTable.getStorageProviders(consumer) }
),
put: async (item) => {
const { cause, consumer, customer, provider } = item
const subscription = await createProvisionSubscriptionId(item)
try {
await subscriptionTable.add({
cause: cause.cid,
provider,
customer,
subscription
})
} catch (error) {
// if we got a conflict error, ignore - it means the subscription already exists and
// can be used to create a consumer/provider relationship below
if (!(error instanceof SubscriptionConflictError)) {
return {
error: new Failure('Unknown error adding subscription', {
cause: error
})
}
}
}
try {
await consumerTable.add({
cause: cause.cid,
provider,
consumer,
customer,
subscription
})
return { ok: { id: subscription } }
} catch (error) {
return (error instanceof ConsumerConflictError) ? (
{
error
}
) : (
{
error: new Failure('Unknown error adding consumer', {
cause: error
})
}
)
}
},
/**
* get number of stored items
*/
count: async () => {
return consumerTable.count()
},
getCustomer: async (provider, customer) => {
const subscriptions = await subscriptionTable.findProviderSubscriptionsForCustomer(customer, provider)
// if we don't have any subscriptions for a customer
if (subscriptions.length === 0) {
return { error: { name: 'CustomerNotFound', message: `could not find ${customer}` } }
}
return {
ok: {
did: customer,
subscriptions: subscriptions.map(s => s.subscription)
}
}
},
getConsumer: async (provider, consumer) => {
const [consumerRecord, allocated] = await Promise.all([
consumerTable.get(provider, consumer),
spaceMetricsTable.getAllocated(consumer)
])
return consumerRecord ? ({
ok: {
did: consumer,
allocated,
limit: 1_000_000_000, // set to an arbitrarily high number because we currently don't enforce any limits
subscription: consumerRecord.subscription,
customer: consumerRecord.customer
}
}) : (
{ error: { name: 'ConsumerNotFound', message: `could not find ${consumer}` } }
)
},
getSubscription: async (provider, subscription) => {
const [subscriptionRecord, consumerRecord] = await Promise.all([
subscriptionTable.get(provider, subscription),
consumerTable.getBySubscription(provider, subscription)
])
if (subscriptionRecord) {
/** @type {import('@web3-storage/upload-api/dist/src/types/provisions').Subscription} */
const result = {
customer: /** @type {import('@web3-storage/upload-api').AccountDID} */(subscriptionRecord.customer)
}
if (consumerRecord) {
result.consumer = /** @type {import('@ucanto/interface').DIDKey} */(consumerRecord.consumer)
}
return { ok: result }
} else {
return { error: { name: 'SubscriptionNotFound', message: 'unimplemented' } }
}
}
}
}