-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
132 lines (98 loc) · 2.96 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
const discovery = require('hyperdiscovery')
const datStorage = require('universal-dat-storage')
const DatEncoding = require('dat-encoding')
const crypto = require('hypercore-crypto')
const datDNS = require('dat-dns')
const hyperdrive = require('hyperdrive')
const hypercore = require('hypercore')
const DEFAULT_STORAGE_OPTS = {}
const DEFAULT_SWARM_OPTS = {
extensions: []
}
const DEFAULT_DRIVE_OPTS = {
sparse: true
}
const DEFAULT_CORE_OPTS = {}
const DEFAULT_DNS_OPTS = {}
module.exports = SDK
function SDK ({ storageOpts, swarmOpts, driveOpts, coreOpts, dnsOpts } = {}) {
const storage = datStorage(Object.assign({}, DEFAULT_STORAGE_OPTS, storageOpts))
const swarm = discovery(Object.assign({}, DEFAULT_SWARM_OPTS, swarmOpts))
const dns = datDNS(Object.assign({}, DEFAULT_DNS_OPTS, dnsOpts))
// Track list of hyperdrives
const drives = new Map()
const cores = new Map()
function addExtensions (extensions) {
if (!extensions || !extensions.length) return
// TODO: This has code smell
const currentExtensions = swarm._opts.extensions || []
const finalSet = new Set([...currentExtensions, ...extensions])
swarm._opts.extensions = [...finalSet]
}
function destroy (cb) {
for (let drive of drives.values()) {
drive.close()
}
for (let core of cores.values()) {
core.close()
}
swarm.close(cb)
}
function resolveName (url, cb) {
return dns.resolveName(url, cb)
}
function Hyperdrive (location, opts) {
opts = Object.assign({}, DEFAULT_DRIVE_OPTS, driveOpts, opts)
addExtensions(opts.extensions)
let key = null
if (!location) {
const { publicKey, secretKey } = crypto.keyPair()
key = publicKey
location = DatEncoding.encode(publicKey)
opts.secretKey = secretKey
}
try {
key = DatEncoding.decode(location)
} catch (e) {
// Location must be relative path
}
const stringKey = location.toString('hex')
if (drives.has(stringKey)) return drives.get(stringKey)
const drive = hyperdrive(storage.getDrive(location), key, opts)
drives.set(stringKey, drive)
drive.ready(() => {
swarm.add(drive)
})
return drive
}
function Hypercore (location, opts) {
opts = Object.assign({}, DEFAULT_CORE_OPTS, coreOpts, opts)
addExtensions(opts.extensions)
let key = null
if (!location) {
const { publicKey, secretKey } = crypto.keyPair()
key = publicKey
location = DatEncoding.encode(publicKey)
opts.secretKey = secretKey
}
try {
key = DatEncoding.decode(location)
} catch (e) {
// Location must be relative path
}
const stringKey = location.toString('hex')
if (cores.has(stringKey)) return cores.get(stringKey)
const core = hypercore(storage.getCore(location, key, opts))
cores.set(stringKey, core)
core.ready(() => {
swarm.add(core)
})
return core
}
return {
Hyperdrive,
Hypercore,
resolveName,
destroy
}
}