-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstore.js
194 lines (183 loc) · 5.33 KB
/
store.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
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
import { CAR } from '@ucanto/transport'
import * as StoreCapabilities from '@web3-storage/capabilities/store'
import retry, { AbortError } from 'p-retry'
import { servicePrincipal, connection } from './service.js'
import { REQUEST_RETRIES } from './constants.js'
/**
* Store a DAG encoded as a CAR file. The issuer needs the `store/add`
* delegated capability.
*
* Required delegated capability proofs: `store/add`
*
* @param {import('./types').InvocationConfig} conf Configuration
* for the UCAN invocation. An object with `issuer`, `with` and `proofs`.
*
* The `issuer` is the signing authority that is issuing the UCAN
* invocation(s). It is typically the user _agent_.
*
* The `with` is the resource the invocation applies to. It is typically the
* DID of a space.
*
* The `proofs` are a set of capability delegations that prove the issuer
* has the capability to perform the action.
*
* The issuer needs the `store/add` delegated capability.
* @param {Blob} car CAR file data.
* @param {import('./types').RequestOptions} [options]
* @returns {Promise<import('./types').CARLink>}
*/
export async function add(
{ issuer, with: resource, proofs, audience },
car,
options = {}
) {
// TODO: validate blob contains CAR data
const bytes = new Uint8Array(await car.arrayBuffer())
const link = await CAR.codec.link(bytes)
/* c8 ignore next */
const conn = options.connection ?? connection
const result = await retry(
async () => {
return await StoreCapabilities.add
.invoke({
issuer,
/* c8 ignore next */
audience: audience ?? servicePrincipal,
with: resource,
nb: { link, size: car.size },
proofs,
})
.execute(conn)
},
{
onFailedAttempt: console.warn,
retries: options.retries ?? REQUEST_RETRIES,
}
)
if (result.error) {
throw new Error(`failed ${StoreCapabilities.add.can} invocation`, {
cause: result,
})
}
// Return early if it was already uploaded.
if (result.status === 'done') {
return link
}
const res = await retry(
async () => {
try {
const res = await fetch(result.url, {
method: 'PUT',
mode: 'cors',
body: car,
headers: result.headers,
signal: options.signal,
})
if (res.status >= 400 && res.status < 500) {
throw new AbortError(`upload failed: ${res.status}`)
}
return res
} catch (err) {
if (options.signal?.aborted === true) {
throw new AbortError('upload aborted')
}
throw err
}
},
{
onFailedAttempt: console.warn,
retries: options.retries ?? REQUEST_RETRIES,
}
)
if (!res.ok) {
throw new Error(`upload failed: ${res.status}`)
}
return link
}
/**
* List CAR files stored by the issuer.
*
* @param {import('./types').InvocationConfig} conf Configuration
* for the UCAN invocation. An object with `issuer`, `with` and `proofs`.
*
* The `issuer` is the signing authority that is issuing the UCAN
* invocation(s). It is typically the user _agent_.
*
* The `with` is the resource the invocation applies to. It is typically the
* DID of a space.
*
* The `proofs` are a set of capability delegations that prove the issuer
* has the capability to perform the action.
*
* The issuer needs the `store/list` delegated capability.
* @param {import('./types').ListRequestOptions} [options]
* @returns {Promise<import('./types').ListResponse<import('./types').StoreListResult>>}
*/
export async function list(
{ issuer, with: resource, proofs, audience },
options = {}
) {
/* c8 ignore next */
const conn = options.connection ?? connection
const result = await StoreCapabilities.list
.invoke({
issuer,
/* c8 ignore next */
audience: audience ?? servicePrincipal,
with: resource,
proofs,
nb: {
cursor: options.cursor,
size: options.size,
},
})
.execute(conn)
if (result.error) {
throw new Error(`failed ${StoreCapabilities.list.can} invocation`, {
cause: result,
})
}
return result
}
/**
* Remove a stored CAR file by CAR CID.
*
* @param {import('./types').InvocationConfig} conf Configuration
* for the UCAN invocation. An object with `issuer`, `with` and `proofs`.
*
* The `issuer` is the signing authority that is issuing the UCAN
* invocation(s). It is typically the user _agent_.
*
* The `with` is the resource the invocation applies to. It is typically the
* DID of a space.
*
* The `proofs` are a set of capability delegations that prove the issuer
* has the capability to perform the action.
*
* The issuer needs the `store/remove` delegated capability.
* @param {import('./types').CARLink} link CID of CAR file to remove.
* @param {import('./types').RequestOptions} [options]
*/
export async function remove(
{ issuer, with: resource, proofs, audience },
link,
options = {}
) {
/* c8 ignore next */
const conn = options.connection ?? connection
const result = await StoreCapabilities.remove
.invoke({
issuer,
/* c8 ignore next */
audience: audience ?? servicePrincipal,
with: resource,
nb: { link },
proofs,
})
.execute(conn)
if (result?.error) {
throw new Error(`failed ${StoreCapabilities.remove.can} invocation`, {
cause: result,
})
}
}