-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
417 lines (343 loc) · 10.3 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import HyperSwarm from 'hyperswarm'
import CoreStore from 'corestore'
import Hypercore from 'hypercore'
import Hyperdrive from 'hyperdrive'
import Hyperbee from 'hyperbee'
import crypto from 'hypercore-crypto'
import z32 from 'z32'
import b4a from 'b4a'
import RAA from 'random-access-application'
import RAM from 'random-access-memory'
import { query, wellknown } from 'dns-query'
import { EventEmitter } from 'events'
import path from 'node:path'
// TODO: Base36 encoding/decoding for URLs instead of hex
export const HYPER_PROTOCOL_SCHEME = 'hyper://'
export const DNSLINK_TXT_PREFIX = 'dnslink=/hyper/'
export const DEFAULT_CORE_OPTS = {
sparse: true
}
export const DEFAULT_JOIN_OPTS = {
server: true,
client: true
}
export const DEFAULT_CORESTORE_OPTS = {
}
export const DEFAULT_SWARM_OPTS = {
}
export const DEFAULT_DNS_QUERY_OPTS = {
endpoints: wellknown.endpoints('doh')
}
// Monkey-patching with first class URL support
Object.defineProperty(Hypercore.prototype, 'url', {
get: function () {
return `${HYPER_PROTOCOL_SCHEME}${this.id}/`
}
})
Object.defineProperty(Hyperdrive.prototype, 'url', {
get: function () {
return `${HYPER_PROTOCOL_SCHEME}${this.core.id}/`
}
})
Object.defineProperty(Hyperbee.prototype, 'url', {
get: function () {
return `${HYPER_PROTOCOL_SCHEME}${this.feed.id}/`
}
})
export class SDK extends EventEmitter {
constructor ({
swarm = throwMissing('swarm'),
corestore = throwMissing('corestore'),
dnsLinkPrefix = DNSLINK_TXT_PREFIX,
defaultCoreOpts = DEFAULT_CORE_OPTS,
defaultJoinOpts = DEFAULT_JOIN_OPTS,
defaultDNSOpts = DEFAULT_DNS_QUERY_OPTS,
autoJoin = true,
doReplicate = true
} = {}) {
super()
this.swarm = swarm
this.corestore = corestore
// These probably shouldn't be accessed
this.coreCache = new Map()
this.beeCache = new Map()
this.driveCache = new Map()
this.dnsLinkPrefix = dnsLinkPrefix
this.defaultCoreOpts = defaultCoreOpts
this.defaultJoinOpts = defaultJoinOpts
this.defaultDNSOpts = defaultDNSOpts
this.autoJoin = autoJoin
if (doReplicate) {
swarm.on('connection', (connection, peerInfo) => {
this.emit('peer-add', peerInfo)
connection.once('close', () => this.emit('peer-remove', peerInfo))
this.replicate(connection)
})
}
}
get publicKey () {
return this.swarm.keyPair.publicKey
}
get connections () {
return this.swarm.connections
}
get peers () {
return this.swarm.peers
}
get cores () {
return [...this._cores.values()]
}
async resolveDNSToKey (domain, opts = {}) {
const finalOpts = { ...this.defaultDNSOpts, ...opts }
const name = `_dnslink.${domain}`
const { answers } = await query({
question: { type: 'txt', name }
}, finalOpts)
for (const { data } of answers) {
if (!data || !data.length) continue
const [raw] = data
if (!raw) return
const asString = raw.toString('utf8')
if (asString.startsWith(this.dnsLinkPrefix)) {
if (asString.endsWith('/')) {
return asString.slice(this.dnsLinkPrefix.length, -1)
}
return asString.slice(this.dnsLinkPrefix.length)
}
}
throw new Error(`Unable to resolve DNSLink domain for ${domain}. If you are the site operator, please add a TXT record pointing at _dnslink.${domain} with the value dnslink=/hyper/YOUR_KEY_IN_Z32_HERE`)
}
// Resolves a string to be a key or opts and resolves DNS
// Useful for hypercore opts or Hyperdrive
async resolveNameOrKeyToOpts (nameOrKeyOrURL) {
// If a URL, use the hostname as either a key or a DNS to resolve
// If not a URL, try to decode to a key
// if not a key, use as name to generate a hypercore
// Else it's an errorW
const isKeyString = (typeof nameOrKeyOrURL === 'string')
if (!isKeyString) {
// If a 32 byte buffer, use it as the key
if (nameOrKeyOrURL && nameOrKeyOrURL.length === 32) {
return { key: nameOrKeyOrURL }
} else {
throw new Error('Must specify a name, url, or a 32 byte buffer with a key')
}
}
if (nameOrKeyOrURL.startsWith(HYPER_PROTOCOL_SCHEME)) {
const url = new URL(nameOrKeyOrURL)
// probably a domain
if (url.hostname.includes('.')) {
const resolved = await this.resolveDNSToKey(url.hostname)
const key = stringToKey(resolved)
return { key }
} else {
// Try to parse the hostname to a key
const key = stringToKey(url.hostname)
if (!key) {
// If not a key or a domain, throw an error
throw new Error('URLs must have either an encoded key or a valid DNSlink domain')
}
return { key }
}
} else {
const parsed = stringToKey(nameOrKeyOrURL)
if (parsed) {
return { key: parsed }
} else {
return { name: nameOrKeyOrURL }
}
}
}
async getBee (nameOrKeyOrURL, opts = {}) {
const core = await this.get(nameOrKeyOrURL, opts)
if (this.beeCache.has(core.url)) {
return this.beeCache.get(core.url)
}
const bee = new Hyperbee(core, opts)
core.once('close', () => {
this.beeCache.delete(core.url)
})
this.beeCache.set(core.url, bee)
await bee.ready()
return bee
}
async getDrive (nameOrKeyOrURL, opts = {}) {
const coreOpts = {
...this.defaultCoreOpts,
autoJoin: this.autoJoin,
...opts
}
const resolvedOpts = await this.resolveNameOrKeyToOpts(nameOrKeyOrURL)
const { key, name } = resolvedOpts
let stringKey = key && key.toString('hex')
if (this.driveCache.has(name)) {
return this.driveCache.get(name)
} else if (this.driveCache.has(stringKey)) {
return this.driveCache.get(stringKey)
}
Object.assign(coreOpts, resolvedOpts)
let corestore = this.corestore
if (resolvedOpts.key) {
corestore = this.namespace(stringKey)
} else if (resolvedOpts.name) {
corestore = this.namespace(name)
} else {
throw new Error('Unable to parse')
}
const drive = new Hyperdrive(corestore, key || null)
await drive.ready()
const core = drive.core
stringKey = core.key.toString('hex')
drive.once('close', () => {
this.driveCache.delete(stringKey)
this.driveCache.delete(name)
})
this.driveCache.set(stringKey, drive)
if (name) this.driveCache.set(name, drive)
if (coreOpts.autoJoin && !core.discovery) {
await this.joinCore(core, opts)
}
return drive
}
async get (nameOrKeyOrURL, opts = {}) {
const coreOpts = {
...this.defaultCoreOpts,
autoJoin: this.autoJoin,
...opts
}
const resolvedOpts = await this.resolveNameOrKeyToOpts(nameOrKeyOrURL)
const { key, name } = resolvedOpts
let stringKey = key && key.toString('hex')
if (this.coreCache.has(name)) {
return this.coreCache.get(name)
} else if (this.coreCache.has(stringKey)) {
return this.coreCache.get(stringKey)
}
Object.assign(coreOpts, resolvedOpts)
// There shouldn't be a way to pass null for the key
const core = this.corestore.get(coreOpts)
// Await for core to be ready
await core.ready()
core.once('close', () => {
this.coreCache.delete(stringKey)
this.coreCache.delete(name)
})
stringKey = core.key.toString('hex')
this.coreCache.set(stringKey, core)
if (name) this.coreCache.set(name, core)
if (coreOpts.autoJoin && !core.discovery) {
await this.joinCore(core, opts)
}
return core
}
// Returns a corestore for a namespace
namespace (namespace) {
return this.corestore.namespace(namespace)
}
makeTopicKey (name) {
const [key] = crypto.namespace(name, 1)
return key
}
async joinCore (core, opts = {}) {
if (core.discovery) return
const discovery = this.join(core.discoveryKey, opts)
core.discovery = discovery
// If we're the owner, then we wait until is fully announced
if (core.writable) {
await discovery.flushed()
}
// Await for initial peer for new readable cores
if (!core.writable && !core.length) {
const done = core.findingPeers()
this.swarm.flush().then(done)
await core.update()
}
core.once('close', () => {
discovery.destroy()
})
}
join (topic, opts = {}) {
if (typeof topic === 'string') {
return this.join(this.makeTopicKey(topic), opts)
}
const joinOpts = { ...this.defaultJoinOpts, ...opts }
return this.swarm.join(topic, joinOpts)
}
leave (topic) {
if (typeof topic === 'string') {
return this.leave(this.makeTopicKey(topic))
}
return this.swarm.leave(topic)
}
joinPeer (id) {
return this.swarm.joinPeer(id)
}
leavePeer (id) {
return this.swarm.leavePeer(id)
}
async ready () {
// Wait for the network to be configured?
await this.corestore.ready()
await this.swarm.listen()
}
async close () {
// Close corestore, close hyperswarm
await Promise.all([
this.corestore.close(),
this.swarm.destroy()
])
}
replicate (connection) {
this.corestore.replicate(connection)
}
}
export async function create ({
storage = 'hyper-sdk',
corestoreOpts = DEFAULT_CORESTORE_OPTS,
swarmOpts = DEFAULT_SWARM_OPTS,
...opts
} = {}) {
const isStringStorage = typeof storage === 'string'
const isPathStorage = isStringStorage && (
storage.startsWith('.') || path.isAbsolute(storage)
)
let storageBackend = storage
if (isStringStorage && !isPathStorage) {
storageBackend = RAA(storage)
} else if (storage === false) {
storageBackend = RAM.reusable()
}
const corestore = opts.corestore || new CoreStore(storageBackend, { ...corestoreOpts })
const networkKeypair = await corestore.createKeyPair('noise')
const swarm = opts.swarm || new HyperSwarm({
keyPair: networkKeypair,
...swarmOpts
})
const sdk = new SDK({
swarm,
corestore,
...opts
})
await sdk.ready()
return sdk
}
function stringToKey (string) {
if (string.length === 52) {
try {
return z32.decode(string)
} catch {
// Not formatted properly, probs a name?
}
} else if (string.length === 64) {
// Parse as hex key
try {
return b4a.from(string, 'hex')
} catch {
// Not formatted properly, probs a name?
}
}
return null
}
function throwMissing (name) {
throw new TypeError(`Missing parameter ${name}`)
}