-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-s3-repo.js
81 lines (75 loc) · 2.08 KB
/
create-s3-repo.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
import { S3Datastore } from 'datastore-s3'
import { createRepo } from 'ipfs-repo'
import { BlockstoreDatastoreAdapter } from 'blockstore-datastore-adapter'
import { ShardingDatastore } from 'datastore-core/sharding'
import { NextToLast } from 'datastore-core/shard'
import * as raw from 'multiformats/codecs/raw'
import * as json from 'multiformats/codecs/json'
import * as dagPb from '@ipld/dag-pb'
import * as dagCbor from '@ipld/dag-cbor'
/**
* @typedef {import('multiformats/codecs/interface').BlockCodec<any, any>} BlockCodec
*/
/**
* A convenience method for creating an S3 backed IPFS repo
*
* @param {string} path
* @param {import('aws-sdk/clients/s3')} s3
* @param {import('ipfs-repo').RepoLock} repoLock
*/
export const createS3Repo = (path, s3, repoLock) => {
const storeConfig = {
s3,
createIfMissing: true
}
/**
* These are the codecs we want to support, you may wish to add others
*
* @type {Record<string | number, BlockCodec>}
*/
const codecs = {
[raw.code]: raw,
[raw.name]: raw,
[json.code]: json,
[json.name]: json,
[dagPb.code]: dagPb,
[dagPb.name]: dagPb,
[dagCbor.code]: dagCbor,
[dagCbor.name]: dagCbor
}
/**
* @type {import('ipfs-repo/src/types').loadCodec}
*/
const loadCodec = async (codeOrName) => {
if (codecs[codeOrName]) {
return codecs[codeOrName]
}
throw new Error(`Unsupported codec ${codeOrName}`)
}
return createRepo(path, loadCodec, {
root: new ShardingDatastore(
new S3Datastore(path, storeConfig),
new NextToLast(2)
),
blocks: new BlockstoreDatastoreAdapter(
new ShardingDatastore(
new S3Datastore(`${path}blocks`, storeConfig),
new NextToLast(2)
)
),
datastore: new ShardingDatastore(
new S3Datastore(`${path}datastore`, storeConfig),
new NextToLast(2)
),
keys: new ShardingDatastore(
new S3Datastore(`${path}keys`, storeConfig),
new NextToLast(2)
),
pins: new ShardingDatastore(
new S3Datastore(`${path}pins`, storeConfig),
new NextToLast(2)
)
}, {
repoLock
})
}