-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupload.js
279 lines (262 loc) · 8.36 KB
/
upload.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
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
import {
UpdateItemCommand,
GetItemCommand,
DeleteItemCommand,
QueryCommand,
} from '@aws-sdk/client-dynamodb'
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'
import { CID } from 'multiformats/cid'
import { RecordNotFound } from './lib.js'
import { getDynamoClient } from '../../lib/aws/dynamo.js'
/**
* @typedef {import('@web3-storage/upload-api').UploadTable} UploadTable
* @typedef {import('@web3-storage/upload-api').UploadAddSuccess} UploadAddResult
* @typedef {import('@web3-storage/upload-api').UploadListItem} UploadListItem
*/
/**
* Abstraction layer to handle operations on Upload Table.
*
* @param {string} region
* @param {string} tableName
* @param {object} [options]
* @param {string} [options.endpoint]
* @returns {UploadTable}
*/
export function createUploadTable(region, tableName, options = {}) {
const dynamoDb = getDynamoClient({
region,
endpoint: options.endpoint,
})
return useUploadTable(dynamoDb, tableName)
}
/**
* @param {import('@aws-sdk/client-dynamodb').DynamoDBClient} dynamoDb
* @param {string} tableName
* @returns {UploadTable}
*/
export function useUploadTable(dynamoDb, tableName) {
return {
/**
* Fetch a single upload
*
* @param {import('@ucanto/interface').DID} space
* @param {import('@web3-storage/upload-api').UnknownLink} root
* @returns {ReturnType<UploadTable['get']>}
*/
get: async (space, root) => {
const cmd = new GetItemCommand({
TableName: tableName,
Key: marshall({
space,
root: root.toString(),
}),
AttributesToGet: ['space', 'root', 'shards', 'insertedAt', 'updatedAt'],
})
const res = await dynamoDb.send(cmd)
if (!res.Item) {
return { error: new RecordNotFound() }
}
return { ok: toUploadListItem(unmarshall(res.Item)) }
},
/**
* Check if the given data CID is bound to a space DID
*
* @param {import('@ucanto/interface').DID} space
* @param {import('@web3-storage/upload-api').UnknownLink} root
* @returns {ReturnType<UploadTable['exists']>}
*/
exists: async (space, root) => {
const cmd = new GetItemCommand({
TableName: tableName,
Key: marshall({
space,
root: root.toString(),
}),
AttributesToGet: ['space'],
})
try {
const response = await dynamoDb.send(cmd)
return { ok: Boolean(response.Item) }
} catch {
return { ok: false }
}
},
/**
* Link a root data CID to a car CID shard in a space DID.
*
* @typedef {import('@web3-storage/upload-api').UploadAddInput} UploadAddInput
*
* @param {UploadAddInput} item
* @returns {ReturnType<UploadTable['upsert']>}
*/
upsert: async ({ space, root, shards = [], issuer, invocation }) => {
const insertedAt = new Date().toISOString()
const shardSet = new Set(shards.map((s) => s.toString()))
const Key = {
space: { S: space.toString() },
root: { S: root.toString() },
}
// dynamo wont let us store an empty Set. We have to adjust the expression to only ADD it if it exists.
const ExpressionAttributeValues = {
':ia': { S: insertedAt },
':ua': { S: insertedAt },
...(shardSet.size > 0 && {
':sh': { SS: [...shardSet] }, // SS is "String Set"
}),
}
const shardExpression = shards.length ? 'ADD shards :sh' : ''
const UpdateExpression = `SET insertedAt=if_not_exists(insertedAt, :ia), updatedAt = :ua ${shardExpression}`
/**
* upsert!
* - Set updatedAt (space & root are set automatically from Key when creating a new item)
* - Set insertedAt when creating a new entry
* - Add shards to existing Set.
*/
const res = await dynamoDb.send(
new UpdateItemCommand({
TableName: tableName,
Key,
UpdateExpression,
ExpressionAttributeValues,
ReturnValues: 'ALL_NEW',
})
)
if (!res.Attributes) {
throw new Error('Missing `Attributes` property on DynamoDB response')
}
return { ok: toUploadAddResult(unmarshall(res.Attributes)) }
},
/**
* Remove an upload from an account
*
* @param {import('@ucanto/interface').DID} space
* @param {import('@web3-storage/upload-api').UnknownLink} root
* @returns {ReturnType<UploadTable['remove']>}
*/
remove: async (space, root) => {
const cmd = new DeleteItemCommand({
TableName: tableName,
Key: marshall({
space,
root: root.toString(),
}),
ConditionExpression: 'attribute_exists(#S) AND attribute_exists(#R)',
ExpressionAttributeNames: { '#S': 'space', '#R': 'root' },
ReturnValues: 'ALL_OLD',
})
try {
// return the removed object so caller may remove all shards
const res = await dynamoDb.send(cmd)
if (res.Attributes === undefined) {
throw new Error('missing return values')
}
const raw = unmarshall(res.Attributes)
return { ok: toUploadAddResult(raw) }
} catch (/** @type {any} */ err) {
if (err.name === 'ConditionalCheckFailedException') {
return { error: new RecordNotFound() }
}
throw err
}
},
/**
* List all CARs bound to an account
*
* @param {string} space
* @param {import('@web3-storage/upload-api').ListOptions} [options]
* @returns {ReturnType<UploadTable['list']>}
*/
list: async (space, options = {}) => {
const exclusiveStartKey = options.cursor
? marshall({
space,
root: options.cursor,
})
: undefined
const cmd = new QueryCommand({
TableName: tableName,
Limit: options.size || 20,
KeyConditions: {
space: {
ComparisonOperator: 'EQ',
AttributeValueList: [{ S: space }],
},
},
ScanIndexForward: !options.pre,
ExclusiveStartKey: exclusiveStartKey,
AttributesToGet: ['space', 'root', 'shards', 'insertedAt', 'updatedAt'],
})
const response = await dynamoDb.send(cmd)
const results = (response.Items ?? []).map((i) => toUploadListItem(unmarshall(i)))
const firstRootCID = results[0] ? results[0].root.toString() : undefined
// Get cursor of the item where list operation stopped (inclusive).
// This value can be used to start a new operation to continue listing.
const lastKey =
response.LastEvaluatedKey && unmarshall(response.LastEvaluatedKey)
const lastRootCID = lastKey ? lastKey.root : undefined
const before = options.pre ? lastRootCID : firstRootCID
const after = options.pre ? firstRootCID : lastRootCID
return {
ok: {
size: results.length,
before,
after,
cursor: after,
results: options.pre ? results.reverse() : results,
}
}
},
/**
* Get information about a CID.
*
* @param {import('@web3-storage/upload-api').UnknownLink} link
* @returns {ReturnType<UploadTable['inspect']>}
*/
inspect: async (link) => {
const response = await dynamoDb.send(new QueryCommand({
TableName: tableName,
IndexName: 'cid',
KeyConditionExpression: "root = :root",
ExpressionAttributeValues: {
':root': { S: link.toString() }
}
}))
return {
ok: {
spaces: (response.Items ?? []).map(i => {
const item = unmarshall(i)
return ({
did: item.space,
insertedAt: item.insertedAt
})
})
}
}
}
}
}
/**
* Convert from the db representation to an UploadAddInput
*
* @param {Record<string, any>} item
* @returns {UploadAddResult}
*/
export function toUploadAddResult({ root, shards }) {
return {
root: CID.parse(root),
shards: (shards ? [...shards] : []).map((s) => /** @type {import('@web3-storage/upload-api').CARLink} */ (CID.parse(s))),
}
}
/**
* Convert from the db representation to an UploadListItem
*
* @param {Record<string, any>} item
* @returns {UploadListItem & { insertedAt: string; updatedAt: string }}
*/
export function toUploadListItem({ insertedAt, updatedAt, ...rest }) {
return {
...toUploadAddResult(rest),
insertedAt,
updatedAt,
}
}