-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypes.ts
301 lines (257 loc) · 8.9 KB
/
types.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import * as UCAN from '@ipld/dag-ucan'
import { DID, Delegation, Block, UCANLink, ByteView, ReceiptModel, DIDKey, Result, Failure, Unit } from '@ucanto/interface'
import { UnknownLink } from 'multiformats'
import { CID } from 'multiformats/cid'
import { Kinesis } from '@aws-sdk/client-kinesis'
import { AccountDID, ProviderDID, Service, SpaceDID, CarStoreBucket, AllocationsStorage, PlanCreateAdminSessionSuccess, PlanCreateAdminSessionFailure, AgentStore } from '@web3-storage/upload-api'
export type {
UnknownLink,
AgentStore,
AgentMessage,
UCANLink,
Capability,
Result,
WriteError,
Writer,
Unit,
Link,
Invocation,
StorageGetError,
ParsedAgentMessage,
AgentMessageIndexRecord,
Receipt,
ReceiptModel,
Accessor,
Variant,
ReceiptLink
} from '@web3-storage/upload-api'
export interface StoreOperationError extends Error {
name: 'StoreOperationFailed'
}
export interface UcanLogCtx {
basicAuth: string
agentStore: AgentStore
}
export interface UcanStreamCtx {
streamName: string
kinesisClient?: Kinesis
}
export interface WorkflowCtx extends UcanStreamCtx {
invocationBucket: InvocationBucket
workflowBucket: WorkflowBucket
}
export interface ReceiptBlockCtx extends UcanStreamCtx {
invocationBucket: InvocationBucket
taskBucket: TaskBucket
workflowBucket: WorkflowBucket
}
export interface MetricsStore {
incrementTotals: (metricsToUpdate: Record<string, number>) => Promise<void>
}
export interface MetricsCtx {
metricsStore: MetricsStore
carStore: CarStore
allocationsStorage: AllocationsStorage
}
export interface SpaceMetricsItem {
value: number
space: string
}
export interface SpaceMetricsStore {
incrementTotals: (metricsToUpdate: Record<string, SpaceMetricsItem[]>) => Promise<void>
}
export interface SpaceMetricsCtx {
metricsStore: SpaceMetricsStore
carStore: CarStore
allocationsStorage: AllocationsStorage
}
export interface CarStore extends CarStoreBucket {
getSize: (link: UnknownLink) => Promise<number>
}
export interface InvocationBucket {
putWorkflowLink: (cid: string, workflowCid: string) => Promise<void>
putReceipt: (cid: string, bytes: Uint8Array) => Promise<void>
putInLink: (cid: string, workflowCid: string) => Promise<void>
putOutLink: (cid: string, workflowCid: string) => Promise<void>
getInLink: (cid: string) => Promise<string|undefined>
getWorkflowLink: (cid: string) => Promise<string|undefined>
}
export interface TaskBucket {
putResult: (taskCid: string, bytes: Uint8Array) => Promise<void>
putInvocationLink: (taskCid: string, invocationCid: string) => Promise<void>
}
export interface WorkflowBucket {
put: (Cid: string, bytes: Uint8Array) => Promise<void>
get: (Cid: string) => Promise<Uint8Array|undefined>
}
export interface DelegationsBucket {
/** put a delegation into the delegations bucket */
put: (cid: CID, bytes: ByteView<Delegation>) => Promise<void>
/** get a delegation from the delegations bucket */
get: (cid: CID) => Promise<ByteView<Delegation>|undefined>
}
export interface MetricsTable {
/**
* Get all metrics from table.
*/
get: () => Promise<Array<Record<string, any>>>
}
export interface SpaceMetricsTable {
/**
* Return the total amount of storage a space has used.
*/
getAllocated: (consumer: DIDKey) => Promise<number>
}
/**
*
*/
export interface SubscriptionInput {
/** DID of the customer who maintains this subscription */
customer: DID,
/** DID of the provider who services this subscription */
provider: DID,
/** ID of this subscription - should be unique per-provider */
subscription: string,
/** CID of the invocation that created this subscription */
cause: UCANLink
}
export interface SubscriptionTable {
get: (provider: ProviderDID, subscription: string) =>
Promise<{ customer: DID } | null>
/** add a subscription - a relationship between a customer and a provider that will allow for provisioning of consumers */
add: (consumer: SubscriptionInput) => Promise<{}>
/** return the count of subscriptions in the system */
count: () => Promise<bigint>
/** return a list of the subscriptions a customer has with a provider */
findProviderSubscriptionsForCustomer: (customer: DID, provider: DID) =>
Promise<Array<{ subscription: string }>>
}
export interface ConsumerInput {
/** the DID of the consumer (eg, a space) for whom services are being provisioned */
consumer: DID,
/** DID of the customer who maintains the subscription for this consumer */
customer: DID,
/** the DID of the provider who will provide services for the consumer */
provider: DID,
/** the ID of the subscription representing the relationship between the consumer and provider */
subscription: string,
/** the CID of the UCAN invocation that created this record */
cause: UCANLink
}
export interface ConsumerRecord {
/** the ID of the subscription representing the relationship between the consumer and provider */
subscription: string,
/** the CID of the UCAN invocation that created this record */
cause: UCANLink
}
export interface ConsumerListRecord {
/** DID of the consumer (e.g. a space) for whom services have been provisioned. */
consumer: SpaceDID
/** DID of the provider who provides services for the consumer. */
provider: ProviderDID
/** ID of the subscription representing the relationship between the consumer and provider. */
subscription: string
/**
* CID of the UCAN invocation that created this record.
* Note: This became a required field after 2023-07-10T23:12:38.000Z.
*/
cause?: UCANLink
}
export interface ConsumerTable {
/** get a consumer record for a given provider */
get: (provider: ProviderDID, consumer: DIDKey) => Promise<{ subscription: string, customer: AccountDID } | null>
/** get a consumer record for a given subscription */
getBySubscription: (provider: ProviderDID, subscription: string) => Promise<{ consumer: DID } | null>
/** add a consumer - a relationship between a provider, subscription and consumer */
add: (consumer: ConsumerInput) => Promise<{}>
/** return the number of consumers */
count: () => Promise<bigint>
/** return a boolean indicating whether the given consumer has a storage provider */
hasStorageProvider: (consumer: DID) => Promise<boolean>
/** return a list of storage providers the given consumer has registered with */
getStorageProviders: (consumer: DID) => Promise<ProviderDID[]>
/** List consumers by customer account DID. */
listByCustomer: (customer: AccountDID) => Promise<{ results: ConsumerListRecord[] }>
}
// TODO: unify this with RecordNotFound in ../billing/tables/lib.js
export interface RecordNotFound<K> extends Failure {
name: 'RecordNotFound'
key: K
}
// TODO unify this with Customer in ../billing/lib/api.ts
export interface Customer {
product: string
updatedAt: string
}
// TODO unify this with CustomerStore in ../billing/lib/api.ts
export interface CustomerTable {
/** get a customer record */
get: (customer: DID<'mailto'>) => Promise<Result<Customer, RecordNotFound<DID<'mailto'>>>>
}
export type SpaceService = Pick<Service, "space">
export type UcanStreamInvocationType = 'workflow' | 'receipt'
export interface UcanStreamInvocation {
carCid: string
invocationCid: string
value: UcanInvocation
ts: number
type: UcanStreamInvocationType
out?: ReceiptResult
}
export interface UcanInvocation {
att: UCAN.Capabilities
aud: `did:${string}:${string}`
iss: `did:${string}:${string}`
cid: string
}
export interface Workflow {
cid: UnknownLink
bytes: Uint8Array
invocations: UcanInvocation[]
}
export interface ReceiptBlock extends Block<ReceiptModel> {
data: ReceiptModel
}
// TODO: Remove once in ucanto
/**
* Defines result type as per invocation spec
*
* @see https://github.com/ucan-wg/invocation/#6-result
*/
export type ReceiptResult<T = unknown, X extends {} = {}> = Result<T, X>
// would be generated by sst, but requires `sst build` to be run, which calls out to aws; not great for CI
declare module 'sst/node/config' {
export interface SecretResources {
PRIVATE_KEY: {
value: string
},
UCAN_INVOCATION_POST_BASIC_AUTH: {
value: string
},
STRIPE_SECRET_KEY: {
value: string
},
GITHUB_CLIENT_SECRET: {
value: string
}
}
}
export interface InvalidSubscriptionState extends Failure {
name: 'InvalidSubscriptionState'
}
export interface BillingProviderUpdateError extends Failure {
name: 'BillingProviderUpdateError'
}
type SetPlanFailure = InvalidSubscriptionState | BillingProviderUpdateError
export interface BillingProvider {
hasCustomer: (customer: AccountDID) => Promise<Result<boolean, Failure>>
setPlan: (customer: AccountDID, plan: DID) => Promise<Result<Unit, SetPlanFailure>>
createAdminSession: (customer: AccountDID, returnURL: string) => Promise<Result<PlanCreateAdminSessionSuccess, PlanCreateAdminSessionFailure>>
}
export interface Referral {
refcode: string
}
export interface ReferralsStore {
getReferredBy: (email: string) => Promise<Referral>
}
export {}