-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.js
More file actions
610 lines (520 loc) · 15 KB
/
index.js
File metadata and controls
610 lines (520 loc) · 15 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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 { EventEmitter } from 'events'
import { join } from 'path'
import RocksDB from 'rocksdb-native'
/** @import {JoinOpts,SwarmOpts,PeerDiscovery,Connection} from "hyperswarm" */
/** @import {BeeOpts} from "hyperbee" */
/** @import {CoreOpts} from "hypercore" */
/** @import {DriveOpts} from "hyperdrive" */
/** @import {CoreStoreOpts} from "corestore" */
/** @typedef {Buffer|Uint8Array} Key */
/** @typedef {string|Key} NameOrKeyOrURL */
/** @typedef {{key?:Key|null, name?:string}} ResolvedKeyOrName */
/**
* @typedef {object} DNSResponse
* @property {{name: string, data: string}} DNSResponse.Answer
*/
// TODO: Base36 encoding/decoding for URLs instead of hex
export const HYPER_PROTOCOL_SCHEME = 'hyper://'
export const DEFAULT_CORE_OPTS = {}
export const DEFAULT_JOIN_OPTS = {
server: true,
client: true
}
export const DEFAULT_CORESTORE_OPTS = {}
export const DEFAULT_SWARM_OPTS = {}
// 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}/`
}
})
const DEFAULT_DNS_RESOLVER = 'https://mozilla.cloudflare-dns.com/dns-query'
const DNSLINK_PREFIX = 'dnslink=/hyper/'
export class SDK extends EventEmitter {
#fetch
#dnsCache
#dnsMemoryCache
#defaultCoreOpts
#defaultJoinOpts
#dnsResolver
#swarm
#corestore
#coreCache
#beeCache
#driveCache
/**
* @param {object} [options]
* @param {typeof globalThis["fetch"]} [options.fetch]
* @param {HyperSwarm} [options.swarm]
* @param {CoreStore} [options.corestore]
* @param {CoreOpts} [options.defaultCoreOpts]
* @param {JoinOpts} [options.defaultJoinOpts]
* @param {string} [options.dnsResolver]
* @param {boolean} [options.autoJoin=true]
* @param {boolean} [options.doReplicate=true]
* @param {RocksDB} [options.dnsCache]
*/
constructor ({
swarm,
corestore,
dnsCache,
fetch = globalThis.fetch,
defaultCoreOpts = DEFAULT_CORE_OPTS,
defaultJoinOpts = DEFAULT_JOIN_OPTS,
dnsResolver = DEFAULT_DNS_RESOLVER,
autoJoin = true,
doReplicate = true
} = {}) {
super()
if (!swarm) throw new TypeError('Missing parameter swarm')
if (!corestore) throw new TypeError('Missing parameter corestore')
if (!dnsCache) throw new TypeError('Missing parameter dnsCache')
this.#swarm = swarm
this.#corestore = corestore
this.#dnsCache = dnsCache
this.#fetch = fetch
// These probably shouldn't be accessed
this.#dnsMemoryCache = new Map()
this.#coreCache = new Map()
this.#beeCache = new Map()
this.#driveCache = new Map()
this.#defaultCoreOpts = defaultCoreOpts
this.#defaultJoinOpts = defaultJoinOpts
this.#dnsResolver = dnsResolver
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 swarm () {
return this.#swarm
}
get corestore () {
return this.#corestore
}
get publicKey () {
return this.#swarm.keyPair.publicKey
}
get connections () {
return this.#swarm.connections
}
get peers () {
return this.#swarm.peers
}
/**
* @type {Hypercore[]}
*/
get cores () {
return [...this.#coreCache.values()]
}
/**
* @type {Hyperdrive[]}
*/
get drives () {
return [...this.#driveCache.values()]
}
/** @type {Hyperbee[]} */
get bees () {
return [...this.#beeCache.values()]
}
/**
* Resolve DNS names to a hypercore key using the DNSLink spec
* @param {string} hostname Hostname to resolve, e,g, `agregore.mauve.moe`
* @returns {Promise<string>}
*/
async resolveDNSToKey (hostname) {
// TODO: Check for TTL?
if (this.#dnsMemoryCache.has(hostname)) {
return this.#dnsMemoryCache.get(hostname)
}
const fetch = this.#fetch
const subdomained = `_dnslink.${hostname}`
const url = `${this.#dnsResolver}?name=${subdomained}&type=TXT`
let answers = null
try {
const response = await fetch(url, {
headers: { accept: 'application/dns-json' }
})
if (!response.ok) {
throw new Error(
`Unable to resolve DoH for ${hostname} ${await response.text()}`
)
}
const dnsResults = /** @type {DNSResponse} */ (await response.json())
answers = dnsResults.Answer
await this.#dnsCache.put(hostname, JSON.stringify(dnsResults))
} catch (e) {
const cached = await this.#dnsCache.get(hostname)
if (cached) {
answers = JSON.parse(cached).Answer
}
}
for (let { name, data } of answers) {
if (name !== subdomained || !data) {
continue
}
if (data.startsWith('"')) {
data = data.slice(1, -1)
}
if (!data.startsWith(DNSLINK_PREFIX)) {
continue
}
const key = data.split('/')[2]
this.#dnsMemoryCache.set(hostname, key)
return key
}
throw new Error(`DNS-Link Record not found for TXT ${subdomained}`)
}
/**
* Resolves a string to be a key or opts and resolves DNS
* Useful for hypercore opts or Hyperdrive
* @param {NameOrKeyOrURL} nameOrKeyOrURL Name or key or URL to resolve
* @returns {Promise<ResolvedKeyOrName>}
*/
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 key = await this.resolveDNSToKey(url.hostname)
return { key: stringToKey(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 }
}
}
}
/**
*
* @param {NameOrKeyOrURL} nameOrKeyOrURL Name or key or hyper URL for the bee
* @param {BeeOpts & CoreOpts & JoinOpts} opts Options for configuring Hyperbee
* @returns {Promise<Hyperbee>}
*/
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
}
/**
*
* @param {NameOrKeyOrURL} nameOrKeyOrURL
* @param {DriveOpts&JoinOpts} opts
* @returns {Promise<Hyperdrive>}
*/
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 (stringKey) {
corestore = this.namespace(stringKey)
} else if (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
}
/**
* @template DataType
* Get a HyperCore by its name or key or URL
* @param {NameOrKeyOrURL} nameOrKeyOrURL
* @param {CoreOpts&JoinOpts} [opts]
* @returns {Promise<Hypercore<DataType>>}
*/
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
}
/**
* Get a sub CoreStore for a given namespace. Use this to derive core names for a particular group
* @param {string} namespace Namespace to store cores under
* @returns {CoreStore}
*/
namespace (namespace) {
return this.corestore.namespace(namespace)
}
/**
* Derive a topic key (for hypercores) from a namespace.
* @param {string} name Name of the namespace to derive
* @returns {Key}
*/
makeTopicKey (name) {
const [key] = crypto.namespace(name, 1)
return key
}
/**
* Start peer discovery on a core. Use this if you created a core on a namespaced CoreStore
* @param {Hypercore} core
* @param {JoinOpts} opts
* @returns {Promise<void>}
*/
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()
})
}
/**
*
* @param {string|Key} topic
* @param {JoinOpts} opts
* @returns {PeerDiscovery}
*/
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)
}
/**
*
* @param {string|Key} topic
* @returns {Promise<void>}
*/
leave (topic) {
if (typeof topic === 'string') {
return this.leave(this.makeTopicKey(topic))
}
return this.swarm.leave(topic)
}
/**
* @param {Key} id
*/
joinPeer (id) {
this.swarm.joinPeer(id)
}
/**
* @param {Key} id
*/
leavePeer (id) {
this.swarm.leavePeer(id)
}
async ready () {
// Wait for the network to be configured?
await this.corestore.ready()
await this.swarm.listen()
}
async close () {
await this.#dnsCache.flush()
// Close corestore, close hyperswarm
await Promise.all([
this.corestore.close(),
this.swarm.destroy(),
this.#dnsCache.close()
])
}
/**
* Persist any pending transactions to disk and pause network activity.
* Use this when the app goes in the background or you want to pause seeding.
*/
async suspend() {
await this.swarm.suspend()
await this.corestore.suspend()
}
/**
* Resume network interactions and re-enable storage after suspending.
*/
async resume() {
await this.corestore.resume()
await this.swarm.resume()
}
/**
* Replicate a connection from hyperswarm manually
* @param {Connection} connection
*/
replicate (connection) {
this.corestore.replicate(connection)
}
}
/**
* Initialize the SDK
* @param {object} options
* @param {string} options.storage
* @param {CoreStoreOpts} [options.corestoreOpts]
* @param {SwarmOpts} [options.swarmOpts]
* @param {typeof globalThis["fetch"]} [options.fetch]
* @param {HyperSwarm} [options.swarm]
* @param {CoreStore} [options.corestore]
* @param {RocksDB} [options.dnsCache]
* @param {CoreOpts} [options.defaultCoreOpts]
* @param {JoinOpts} [options.defaultJoinOpts]
* @param {string} [options.dnsResolver]
* @param {boolean} [options.autoJoin=true]
* @param {boolean} [options.doReplicate=true]
* @returns {Promise<SDK>}
*/
export async function create ({
storage,
corestoreOpts = DEFAULT_CORESTORE_OPTS,
swarmOpts = DEFAULT_SWARM_OPTS,
fetch = globalThis.fetch,
...opts
} = {storage: ''}) {
if (!storage) {
throw new Error('Storage parameter is required to be a valid file path')
}
const corestore =
opts.corestore || new CoreStore(storage, { ...corestoreOpts })
const dnsCache = opts.dnsCache || new RocksDB(join(storage, 'dnsCache'))
const networkKeypair = await corestore.createKeyPair('noise')
const swarm =
opts.swarm ||
new HyperSwarm({
keyPair: networkKeypair,
...swarmOpts
})
const sdk = new SDK({
...opts,
fetch: fetch || (await import('bare-fetch')).default,
corestore,
swarm,
dnsCache
})
await sdk.ready()
return sdk
}
/**
* @param {string} string
* @returns {Key|null}
*/
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
}