-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
239 lines (191 loc) · 5.67 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
const path = require('path')
// This is a dirty hack for browserify to work. 😅
if (!path.posix) path.posix = path
const SwarmNetworker = require('corestore-swarm-networking')
const RAA = require('random-access-application')
const DatEncoding = require('dat-encoding')
const crypto = require('hypercore-crypto')
const RAM = require('random-access-memory')
const HypercoreProtocol = require('hypercore-protocol')
const datDNS = require('dat-dns')
const makeHyperdrive = require('hyperdrive')
const Corestore = require('corestore')
const makeHypercore = require('hypercore')
const DEFAULT_SWARM_OPTS = {
extensions: []
}
const DEFAULT_DRIVE_OPTS = {
sparse: true,
persist: true
}
const DEFAULT_CORE_OPTS = {
sparse: true,
persist: true
}
const DEFAULT_DNS_OPTS = {}
const DEFAULT_CORESTORE_OPTS = {
sparse: true
}
const DEFAULT_APPLICATION_NAME = 'dat-sdk'
module.exports = SDK
// TODO: Set up Promise API based on Beaker https://github.com/beakerbrowser/beaker/blob/blue-hyperdrive10/app/bg/web-apis/fg/hyperdrive.js
async function SDK ({
storage,
corestore,
applicationName = DEFAULT_APPLICATION_NAME,
swarmOpts,
driveOpts,
coreOpts,
dnsOpts,
corestoreOpts
} = {}
) {
// Derive storage if it isn't provided
// Don't derive if corestore was provided
if (!storage && !corestore) storage = RAA(applicationName)
if (!corestore) {
corestore = new Corestore(
storage,
Object.assign({}, DEFAULT_CORESTORE_OPTS, corestoreOpts)
)
}
const swarm = new SwarmNetworker(corestore, 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()
await corestore.ready()
// I think this is used to create a persisted identity?
const noiseSeed = corestore._deriveSecret(applicationName, 'replication-keypair')
swarm.listen({
keyPair: HypercoreProtocol.keyPair(noiseSeed)
})
return {
Hyperdrive,
Hypercore,
resolveName,
deleteStorage,
destroy,
_storage: storage,
_corestore: corestore,
_swarm: swarm,
_dns: dns
}
function destroy (cb) {
for (const drive of drives.values()) {
drive.close()
}
for (const core of cores.values()) {
core.close()
}
swarm.close().then(cb, cb)
}
function resolveName (url, cb) {
return dns.resolveName(url, cb)
}
function deleteStorage (key, cb) {
storage.delete(key, cb)
}
function Hyperdrive (nameOrKey, opts) {
if (!nameOrKey) throw new Error('Must give a name or key in the constructor')
opts = Object.assign({}, DEFAULT_DRIVE_OPTS, driveOpts, opts)
let key = null
try {
key = DatEncoding.decode(nameOrKey)
// Normalize keys to be hex strings of the key instead of dat URLs
nameOrKey = key.toString('hex')
} catch (e) {
// Probably isn't a `dat://` URL, so it must be a name
}
if (drives.has(nameOrKey)) return drives.get(nameOrKey)
opts.namespace = nameOrKey
const { persist } = opts
let driveStorage = corestore
if (!persist) {
driveStorage = RAM
} else if (opts.storage) {
driveStorage = opts.storage(key)
} else {
driveStorage = corestore
}
const drive = makeHyperdrive(driveStorage, key, opts)
drives.set(nameOrKey, drive)
if (!key) {
drive.ready(() => {
const key = drive.key
const stringKey = key.toString('hex')
drives.set(stringKey, drive)
})
}
drive.ready(() => {
swarm.seed(drive.discoveryKey)
})
drive.once('close', () => {
swarm.unseed(drive.discoveryKey)
const key = drive.key
const stringKey = key.toString('hex')
drives.delete(stringKey)
drives.delete(nameOrKey)
})
return drive
}
function Hypercore (nameOrKey, opts) {
if (!nameOrKey) throw new Error('Must give a name or key in the constructor')
opts = Object.assign({}, DEFAULT_CORE_OPTS, driveOpts, opts)
let key = null
try {
key = DatEncoding.decode(nameOrKey)
// Normalize keys to be hex strings of the key instead of dat URLs
nameOrKey = key.toString('hex')
} catch (e) {
// Probably isn't a `dat://` URL, so it must be a name
}
if (cores.has(nameOrKey)) return cores.get(nameOrKey)
const { persist } = opts
let coreStorage = null
if (!persist) {
coreStorage = RAM
} else if (opts.storage) {
coreStorage = opts.storage(key)
}
let core = null
// If sotrage was passed in the opts, use it. Else use the corestore
if (coreStorage) {
// We only want to generate keys if we have a custom storage
// Else the corestore does fancy key storage for us
if (!key) {
const { publicKey, secretKey } = crypto.keyPair()
key = publicKey
opts.secretKey = secretKey
}
core = makeHypercore(coreStorage, key, opts)
} else {
if (key) {
// If a dat key was provided, get it from the corestore
core = corestore.get({ key, ...opts })
} else {
// If no dat key was provided, but a name was given, use it as a namespace
core = corestore.namespace(nameOrKey).default(opts)
}
}
cores.set(nameOrKey, core)
if (!key) {
core.ready(() => {
const key = core.key
const stringKey = key.toString('hex')
cores.set(stringKey, core)
})
}
core.ready(() => {
swarm.seed(core.discoveryKey)
})
core.once('close', () => {
const key = core.key
const stringKey = key.toString('hex')
swarm.unseed(core.discoveryKey)
cores.delete(stringKey)
cores.delete(nameOrKey)
})
return core
}
}