This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
166 lines (134 loc) · 4.03 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
const EventEmitter = require('events').EventEmitter
const parallel = require('run-parallel')
const encoding = require('dat-encoding')
const RAW = require('random-access-web')
const RAM = require('random-access-memory')
const DiscoverySwarmWeb = require('discovery-swarm-web')
const crypto = require('hypercore-crypto')
const hypercoreProtocol = require('hypercore-protocol')
const hyperdrive = require('hyperdrive')
const DAT_PROTOCOL = 'dat://'
const STORAGE_NAME = 'dats'
const STORAGE_COLLECTION_NAME = `files`
module.exports =
/**
* The Dat object. Manages multiple archives in
* a single discovery-swarm-web instance.
* @param {Object} opts Default options to use for the dat.
*/
class Dat extends EventEmitter {
constructor (opts) {
super()
this.opts = opts || {}
if (!this.opts.id) this.opts.id = crypto.randomBytes(32)
this.archives = []
const discoveryOpts = Object.assign({
stream: (info) => this._replicate(info)
}, this.opts)
this.swarm = DiscoverySwarmWeb(discoveryOpts)
const storageOpts = Object.assign({
name: STORAGE_NAME,
storeName: STORAGE_COLLECTION_NAME
}, this.opts)
this.persistence = RAW(storageOpts)
}
/**
* Returns a archive with the given url.
* @param {url} url The url of the archive.
* @return {hyperdrive} The archive object with the corresponding url.
*/
get (url, opts) {
const key = encoding.decode(url)
const normalizedURL = `dat://${encoding.encode(key)}`
const archive = this.archives.find((archive) => archive.url === normalizedURL)
if (archive) return archive
return this._add(normalizedURL, opts)
}
_add (url, opts) {
if (this.destroyed) throw new Error('client is destroyed')
if (!opts) opts = {}
const finalOpts = Object.assign({}, this.opts, opts)
let key = null
if (url) key = encoding.decode(url)
// If no key was provided, generate one
if (!key) {
const keyPair = crypto.keyPair()
key = keyPair.publicKey
finalOpts.secretKey = keyPair.secretKey
}
const keyString = encoding.encode(key)
const rawStorage = finalOpts.db || (finalOpts.persist ? this.persistence : RAM)
const storage = (file) => {
return rawStorage(keyString + '/' + file)
}
const archive = hyperdrive(storage, key, Object.assign({
sparse: true
}, finalOpts))
archive.url = DAT_PROTOCOL + keyString
this.archives.push(archive)
archive.ready(() => {
this.swarm.join(archive.discoveryKey)
this.emit('archive', archive)
})
return archive
}
create (opts) {
return this._add(null, opts)
}
has (url) {
const key = encoding.decode(url)
const normalizedURL = `dat://${encoding.encode(key)}`
return !!this.archives.find((archive) => archive.url === normalizedURL)
}
_replicate (info) {
var stream = hypercoreProtocol({
id: this.opts.id,
live: true,
encrypt: true,
extensions: this.opts.extensions,
})
stream.on('feed', (discoveryKey) => this._replicateFeed(stream, discoveryKey))
stream.on('error', (e) => {
this.emit('replication-error', e)
})
if (info.channel) {
this._replicateFeed(stream, info.channel)
}
return stream
}
_replicateFeed (stream, discoveryKey) {
if (this.destroyed) {
stream.end()
return
}
const stringKey = encoding.encode(discoveryKey)
const archive = this.archives.find((archive) => {
return encoding.encode(archive.discoveryKey) === stringKey
})
if (!archive) return
archive.replicate({ stream, live: true })
}
/**
* Closes the the swarm, and all underlying hyperdrive instances.
*/
close (cb) {
if (this.destroyed) {
if (cb) process.nextTick(cb)
return
}
this.destroyed = true
this.swarm.close()
if (cb) this.once('close', cb)
parallel(this.archives.map((archive) => {
return (cb) => {
archive.close(cb)
}
}), () => {
this.archives = null
this.emit('close')
})
}
destroy (cb) {
this.close(cb)
}
}