-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
350 lines (304 loc) · 12.6 KB
/
index.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import fs from 'node:fs'
import http from 'node:http'
import { Writable } from 'node:stream'
import { authorize } from '@web3-storage/upload-api/validate'
import * as DIDMailto from '@web3-storage/did-mailto'
import { walkClaims } from '@web3-storage/content-claims/server'
import * as Digest from 'multiformats/hashes/digest'
import * as raw from 'multiformats/codecs/raw'
import * as Link from 'multiformats/link'
import { base58btc } from 'multiformats/bases/base58'
import { CARReaderStream, CARWriterStream } from 'carstream'
import { MultihashIndexSortedWriter } from 'cardex'
import { Store } from './stores/transactional.js'
import { StoreStore } from './stores/store.js'
import { UploadAddEvent, UploadStore } from './stores/upload.js'
import { ProvisionsStore } from './stores/provisions.js'
import { DelegationsStore } from './stores/delegations.js'
import { RateLimitsStore } from './stores/rate-limits.js'
import { PlansStore } from './stores/plans.js'
import { SubscriptionsStore } from './stores/subscriptions.js'
import { RevocationsStore } from './stores/revocations.js'
import { UsageStore } from './stores/usage.js'
import { BlobStore } from './stores/blob.js'
import { CARPutEvent, CARStore } from './stores/car.js'
import { PartsStore } from './stores/parts.js'
import { ClaimStore } from './stores/claims.js'
import { parseRange } from './http.js'
import { config } from './config.js'
import { createServer } from './server.js'
import { createInclusionClaim, createLocationClaim, createPartitionClaim } from './claims.js'
import { concatStream } from './util.js'
const store = new Store(config.dataDir)
const storeStore = store.partition('store/')
const uploadStore = store.partition('upload/')
const subscriptionStore = store.partition('subscription/')
const consumerStore = store.partition('consumer/')
const spaceMetricsStore = store.partition('space-metric/')
const delegationsStore = store.partition('delegation/')
const rateLimitsStore = store.partition('rate-limit/')
const customerStore = store.partition('customer/')
const revocationsStore = store.partition('revocation/')
const spaceDiffStore = store.partition('space-diff/')
const spaceSnapshotStore = store.partition('space-snapshot/')
const blobStore = store.partition('blob/', { codec: raw })
const partsStore = store.partition('part/')
const claimStore = store.partition('claim/')
const blobStorage = new BlobStore(blobStore, config.signer, config.publicUploadURL)
const context = {
// Ucanto config
id: config.signer,
signer: config.signer,
errorReporter: { catch: err => console.error(err) },
// Access service config
email: { sendValidation: async input => console.log('Sending email:', input) },
url: config.publicApiURL, // URL used in validation emails
// store/add config
maxUploadSize: config.maxUploadSize,
// Stores
storeTable: new StoreStore(storeStore),
uploadTable: new UploadStore(uploadStore),
provisionsStorage: new ProvisionsStore(subscriptionStore, consumerStore, spaceMetricsStore, [config.signer.did()]),
delegationsStorage: new DelegationsStore(delegationsStore),
rateLimitsStorage: new RateLimitsStore(rateLimitsStore),
plansStorage: new PlansStore(customerStore),
subscriptionsStorage: new SubscriptionsStore(consumerStore),
revocationsStorage: new RevocationsStore(revocationsStore),
usageStorage: new UsageStore(spaceDiffStore, spaceSnapshotStore),
// used on store/add to determine if status = 'upload' or status = 'done' in response [X]
carStoreBucket: new CARStore(blobStorage),
// on upload/add we write root => CAR(s) mapping [X]
dudewhereBucket: new PartsStore(partsStore),
// filecoin storefront
// taskStore, // [X]
// receiptStore, // [X]
// aggregatorId, // aggregator service DID, will be replaced with aggregator service config [X]
// pieceStore, // [X]
// filecoinSubmitQueue, // [X]
// pieceOfferQueue, // [X]
// dealTrackerService, // connection and invocation config for deal tracker service
// Content Claims
claimStore: new ClaimStore(claimStore)
}
// Create a location claim when a CAR is added
context.carStoreBucket.addEventListener('put', async e => {
if (e instanceof CARPutEvent) {
await createAndStoreLocationClaim(e.link)
}
})
// Create CAR index when CAR is added
context.carStoreBucket.addEventListener('put', async e => {
if (e instanceof CARPutEvent) {
const carStream = await blobStorage.stream(e.link.multihash)
if (!carStream.ok) return console.error('failed to stream CAR from blob store', carStream.error)
const { readable, writable } = new TransformStream()
const writer = MultihashIndexSortedWriter.createWriter({ writer: writable.getWriter() })
const [, bytes] = await Promise.all([
carStream.ok
.pipeThrough(new CARReaderStream())
.pipeTo(new WritableStream({
write: async block => { await writer.add(block.cid, block.offset) },
close: async () => { await writer.close() }
})),
concatStream(readable)
])
const putResult = await blobStorage.put(bytes)
if (!putResult.ok) return console.error('failed to store CAR index', putResult.error)
const { digest } = putResult.ok
const includes = Link.create(MultihashIndexSortedWriter.codec, digest)
console.log(`Indexed CAR: ${e.link} => ${includes}`)
await createAndStoreLocationClaim(includes)
const claim = await createInclusionClaim({
issuer: config.signer,
audience: config.signer,
proofs: []
}, e.link, includes)
const archive = await claim.archive()
if (archive.error) return console.error('failed to archive delegation', archive.error)
await context.claimStore.put({
claim: claim.cid,
bytes: archive.ok,
content: e.link.multihash,
// @ts-expect-error
value: claim.capabilities[0]
})
console.log(`Content inclusion claimed: ${e.link} includes: ${includes}`)
}
})
// Create a partition claim when `upload/add` is invoked
context.uploadTable.addEventListener('add', async e => {
if (e instanceof UploadAddEvent) {
const claim = await createPartitionClaim({
issuer: config.signer,
audience: config.signer,
proofs: []
}, e.root, e.shards)
const archive = await claim.archive()
if (archive.error) return console.error('failed to archive delegation', archive.error)
await context.claimStore.put({
claim: claim.cid,
bytes: archive.ok,
content: e.root.multihash,
// @ts-expect-error
value: claim.capabilities[0]
})
console.log(`Content partition claimed: ${e.root} parts: ${e.shards.map(s => String(s))}`)
}
})
/**
* @param {import('multiformats').UnknownLink} content
*/
const createAndStoreLocationClaim = async (content) => {
const location = new URL(`/blob/${base58btc.encode(content.multihash.bytes)}`, config.publicUploadURL)
const claim = await createLocationClaim({
issuer: config.signer,
audience: config.signer,
proofs: []
}, content, location)
const archive = await claim.archive()
if (archive.error) return console.error('failed to archive delegation', archive.error)
await context.claimStore.put({
claim: claim.cid,
bytes: archive.ok,
content: content.multihash,
// @ts-expect-error
value: claim.capabilities[0]
})
console.log(`Content location claimed: ${content} location: ${location}`)
}
const server = createServer(context)
console.log(config.banner)
console.log(`Service DID: ${config.signer.did()} (${config.signer.toDIDKey()})`)
const httpServer = http.createServer(async (req, res) => {
if (req.url !== '/') console.log(`${req.method} ${req.url}`)
// GET /validate-email //////////////////////////////////////////////////////
if (req.method === 'GET' && req.url === '/version') {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify({
name: config.pkg.name,
version: config.pkg.version,
did: config.signer.did(),
publicKey: config.signer.toDIDKey()
}))
return res.end()
}
if (req.method === 'GET' && req.url?.startsWith('/validate-email?')) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.write(await fs.promises.readFile(`${import.meta.dirname}/validate-email.html`))
return res.end()
}
// POST /validate-email /////////////////////////////////////////////////////
if (req.method === 'POST' && req.url?.startsWith('/validate-email?')) {
const { searchParams } = new URL(req.url, config.publicApiURL)
const authResult = await authorize(searchParams.get('ucan') ?? '', context)
if (authResult.error) {
console.error(new Error('failed authorization', { cause: authResult.error }))
res.statusCode = 500
res.setHeader('Content-Type', 'text/html')
res.write(`Oops something went wrong: ${authResult.error.message}`)
return res.end()
}
const customer = DIDMailto.fromEmail(authResult.ok.email)
const account = `placeholder:acc-${Date.now()}`
const product = 'did:web:starter.local.web3.storage'
console.log(`Skipping payment flow and initializing ${customer} with plan ${product}`)
const initResult = await context.plansStorage.initialize(customer, account, product)
if (initResult.error && initResult.error.name !== 'CustomerExists') {
console.error(new Error('failed customer initialization', { cause: initResult.error }))
res.statusCode = 500
res.setHeader('Content-Type', 'text/html')
res.write(`Oops something went wrong: ${initResult.error.message}`)
return res.end()
}
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.write(await fs.promises.readFile(`${import.meta.dirname}/validated-email.html`))
return res.end()
}
// PUT /blob/:multihash /////////////////////////////////////////////////////
if (req.method === 'PUT' && req.url?.startsWith('/blob/')) {
// TODO: validate signed URL
const chunks = []
for await (const chunk of req) {
chunks.push(chunk)
}
const bytes = Buffer.concat(chunks)
const result = await context.carStoreBucket.put(bytes)
if (result.ok) console.log(`Stored: ${result.ok.link} (${bytes.length} bytes)`)
return res.end()
}
// GET /blob/:multihash /////////////////////////////////////////////////////
if (req.method === 'GET' && req.url?.startsWith('/blob/')) {
let digest
try {
const url = new URL(req.url, config.publicApiURL)
digest = Digest.decode(base58btc.decode(url.pathname.split('/')[2]))
} catch (err) {
res.statusCode = 400
res.write(`invalid multihash: ${err.message}`)
return res.end()
}
let range
if (req.headers.range) {
try {
range = parseRange(req.headers.range)
console.log(`Range: ${req.headers.range}`)
} catch (err) {
res.statusCode = 400
res.write(`invalid range: ${err.message}`)
return res.end()
}
}
const result = await blobStorage.stream(digest, { range })
if (result.error) {
console.error('failed to read blob', result.error, range)
res.statusCode = result.error.name === 'RecordNotFound' ? 404 : 500
res.write('failed to read blob')
return res.end()
}
return await result.ok.pipeTo(Writable.toWeb(res))
}
// GET /claims/:cid /////////////////////////////////////////////////////////
if (req.method === 'GET' && req.url?.startsWith('/claims/')) {
const url = new URL(req.url, config.publicApiURL)
let link
try {
link = Link.parse(url.pathname.split('/')[2])
} catch (err) {
res.statusCode = 400
res.write(`invalid CID: ${err.message}`)
return res.end()
}
const walkcsv = url.searchParams.get('walk')
const walk = new Set(walkcsv ? walkcsv.split(',') : [])
const readable = walkClaims({ claimFetcher: context.claimStore }, link, walk)
res.setHeader('Content-Type', 'application/vnd.ipld.car; version=1;')
return await readable.pipeThrough(new CARWriterStream()).pipeTo(Writable.toWeb(res))
}
// POST / ///////////////////////////////////////////////////////////////////
if (req.method === 'POST' && req.url === '/') {
const chunks = []
for await (const chunk of req) {
chunks.push(chunk)
}
const response = await server.request({
method: req.method ?? 'POST',
// @ts-expect-error
headers: req.headers,
body: Buffer.concat(chunks)
})
res.statusCode = response.status ?? 200
for (const [k, v] of Object.entries(response.headers)) {
res.setHeader(k, v)
}
res.write(response.body)
return res.end()
}
res.statusCode = 404
res.end()
})
httpServer.listen(config.apiPort, () => {
console.log(`Server listening on :${config.apiPort}`)
})