forked from Frando/corestore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.js
55 lines (48 loc) · 1.26 KB
/
node.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
// Default handlers.
const raf = require('random-access-file')
const mkdirp = require('mkdirp')
const discoverySwarm = require('discovery-swarm')
const swarmDefaults = require('dat-swarm-defaults')
const p = require('path')
const level = require('level')
const Corestore = require('./index.js')
const storage = {
create (path) {
return nestStorage(raf, path)
},
prepare (path) {
return new Promise((resolve, reject) => {
mkdirp(path, err => err ? reject(err) : resolve())
})
},
delete (path) {
// left out for now.
// return fs.remove(path)
}
}
function swarm (opts) {
const swarm = discoverySwarm(swarmDefaults({
id: opts.id,
hash: false,
utp: defaultTrue(opts.utp),
tcp: defaultTrue(opts.tcp),
dht: defaultTrue(opts.dht),
stream: opts.stream
}))
swarm.listen(opts.port || undefined)
return swarm
}
const defaultOpts = { storage, swarm, level }
const NodeCorestore = Corestore.withDefaults(defaultOpts)
NodeCorestore.defaultOpts = defaultOpts
module.exports = NodeCorestore
function defaultTrue (x) {
return x === undefined ? true : x
}
function nestStorage (storage, ...prefixes) {
return function (name, opts) {
let path = p.join(...prefixes, name)
let ret = storage(path, opts)
return ret
}
}