This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcreate-repo.js
47 lines (40 loc) · 1.52 KB
/
create-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
import { nanoid } from 'nanoid'
import { codecs } from './codecs.js'
import { createBackend } from './create-backend.js'
import { Key } from 'interface-datastore/key'
import { createRepo } from 'ipfs-repo'
import { MemoryLock } from 'ipfs-repo/locks/memory'
/**
* @param {object} options
* @param {string} [options.path]
* @param {number} [options.version]
* @param {number} [options.spec]
* @param {boolean} [options.autoMigrate]
* @param {(version: number, percentComplete: string, message: string) => void} [options.onMigrationProgress]
* @param {import('ipfs-core-types/src/config').Config} [options.config]
*/
export async function createTempRepo (options = {}) {
const path = options.path || 'ipfs-test-' + nanoid()
const backend = createBackend()
const encoder = new TextEncoder()
if (options.version) {
await backend.root.open()
await backend.root.put(new Key('/version'), encoder.encode(`${options.version}`))
await backend.root.close()
}
if (options.spec) {
await backend.root.open()
await backend.root.put(new Key('/datastore_spec'), encoder.encode(`${options.spec}`))
await backend.root.close()
}
if (options.config) {
await backend.root.open()
await backend.root.put(new Key('/config'), encoder.encode(JSON.stringify(options.config)))
await backend.root.close()
}
return createRepo(path, (codeOrName) => codecs.getCodec(codeOrName), backend, {
repoLock: MemoryLock,
autoMigrate: options.autoMigrate,
onMigrationProgress: options.onMigrationProgress
})
}