corestore 6.0.6 → 6.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +28 -33
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -13,25 +13,26 @@ const CORES_DIR = 'cores'
|
|
|
13
13
|
const PRIMARY_KEY_FILE_NAME = 'primary-key'
|
|
14
14
|
const USERDATA_NAME_KEY = 'corestore/name'
|
|
15
15
|
const USERDATA_NAMESPACE_KEY = 'corestore/namespace'
|
|
16
|
+
const POOL_SIZE = 512 // how many open fds to aim for before cycling them
|
|
16
17
|
|
|
17
18
|
module.exports = class Corestore extends EventEmitter {
|
|
18
19
|
constructor (storage, opts = {}) {
|
|
19
20
|
super()
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.
|
|
22
|
+
const root = opts._root
|
|
23
|
+
|
|
24
|
+
this.storage = Hypercore.defaultStorage(storage, { lock: PRIMARY_KEY_FILE_NAME, poolSize: opts.poolSize || POOL_SIZE })
|
|
25
|
+
this.cores = root ? root.cores : new Map()
|
|
24
26
|
this.cache = !!opts.cache
|
|
27
|
+
this.primaryKey = opts.primaryKey || null
|
|
25
28
|
|
|
26
29
|
this._keyStorage = null
|
|
27
|
-
this._primaryKey = opts.primaryKey
|
|
28
30
|
this._namespace = opts.namespace || DEFAULT_NAMESPACE
|
|
29
31
|
|
|
30
|
-
this._root =
|
|
31
|
-
this._replicationStreams =
|
|
32
|
+
this._root = root || this
|
|
33
|
+
this._replicationStreams = root ? root._replicationStreams : []
|
|
32
34
|
this._overwrite = opts.overwrite === true
|
|
33
35
|
|
|
34
|
-
this._streamSessions = opts._streamSessions || new Map()
|
|
35
36
|
this._sessions = new Set() // sessions for THIS namespace
|
|
36
37
|
|
|
37
38
|
this._findingPeersCount = 0
|
|
@@ -40,9 +41,12 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
40
41
|
if (this._namespace.byteLength !== 32) throw new Error('Namespace must be a 32-byte Buffer or Uint8Array')
|
|
41
42
|
|
|
42
43
|
this._closing = null
|
|
43
|
-
this._opening =
|
|
44
|
+
this._opening = this._open()
|
|
44
45
|
this._opening.catch(safetyCatch)
|
|
45
|
-
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
ready () {
|
|
49
|
+
return this._opening
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
findingPeers () {
|
|
@@ -73,16 +77,19 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
async _open () {
|
|
76
|
-
if (this.
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
if (this._root !== this) {
|
|
81
|
+
await this._root._opening
|
|
82
|
+
this.primaryKey = this._root.primaryKey
|
|
83
|
+
return
|
|
79
84
|
}
|
|
85
|
+
|
|
80
86
|
this._keyStorage = this.storage(PRIMARY_KEY_FILE_NAME)
|
|
87
|
+
|
|
81
88
|
this.primaryKey = await new Promise((resolve, reject) => {
|
|
82
89
|
this._keyStorage.stat((err, st) => {
|
|
83
90
|
if (err && err.code !== 'ENOENT') return reject(err)
|
|
84
91
|
if (err || st.size < 32 || this._overwrite) {
|
|
85
|
-
const key = crypto.randomBytes(32)
|
|
92
|
+
const key = this.primaryKey || crypto.randomBytes(32)
|
|
86
93
|
return this._keyStorage.write(0, key, err => {
|
|
87
94
|
if (err) return reject(err)
|
|
88
95
|
return resolve(key)
|
|
@@ -90,11 +97,11 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
90
97
|
}
|
|
91
98
|
this._keyStorage.read(0, 32, (err, key) => {
|
|
92
99
|
if (err) return reject(err)
|
|
100
|
+
if (this.primaryKey) return resolve(this.primaryKey)
|
|
93
101
|
return resolve(key)
|
|
94
102
|
})
|
|
95
103
|
})
|
|
96
104
|
})
|
|
97
|
-
return this.primaryKey
|
|
98
105
|
}
|
|
99
106
|
|
|
100
107
|
async _generateKeys (opts) {
|
|
@@ -150,7 +157,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
150
157
|
}
|
|
151
158
|
|
|
152
159
|
async _preload (opts) {
|
|
153
|
-
await this.
|
|
160
|
+
if (!this.primaryKey) await this._opening
|
|
154
161
|
|
|
155
162
|
const { discoveryKey, keyPair, auth } = await this._generateKeys(opts)
|
|
156
163
|
const id = b4a.toString(discoveryKey, 'hex')
|
|
@@ -194,10 +201,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
194
201
|
this.cores.set(id, core)
|
|
195
202
|
core.ready().then(() => {
|
|
196
203
|
for (const { stream } of this._replicationStreams) {
|
|
197
|
-
|
|
198
|
-
const session = core.session()
|
|
199
|
-
sessions.push(session)
|
|
200
|
-
core.replicate(stream)
|
|
204
|
+
core.replicate(stream, { session: true })
|
|
201
205
|
}
|
|
202
206
|
}, () => {
|
|
203
207
|
this.cores.delete(id)
|
|
@@ -268,36 +272,26 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
268
272
|
}
|
|
269
273
|
})
|
|
270
274
|
|
|
271
|
-
const sessions = []
|
|
272
275
|
for (const core of this.cores.values()) {
|
|
273
276
|
if (!core.opened) continue // If the core is not opened, it will be replicated in preload.
|
|
274
|
-
|
|
275
|
-
sessions.push(session)
|
|
276
|
-
core.replicate(stream)
|
|
277
|
+
core.replicate(stream, { session: true })
|
|
277
278
|
}
|
|
278
279
|
|
|
279
280
|
const streamRecord = { stream, isExternal }
|
|
280
281
|
this._replicationStreams.push(streamRecord)
|
|
281
|
-
this._streamSessions.set(stream, sessions)
|
|
282
282
|
|
|
283
283
|
stream.once('close', () => {
|
|
284
284
|
this._replicationStreams.splice(this._replicationStreams.indexOf(streamRecord), 1)
|
|
285
|
-
this._streamSessions.delete(stream)
|
|
286
|
-
Promise.all(sessions.map(s => s.close())).catch(safetyCatch)
|
|
287
285
|
})
|
|
286
|
+
|
|
288
287
|
return stream
|
|
289
288
|
}
|
|
290
289
|
|
|
291
290
|
namespace (name) {
|
|
292
291
|
return new Corestore(this.storage, {
|
|
293
|
-
primaryKey: this._opening.then(() => this.primaryKey),
|
|
294
292
|
namespace: generateNamespace(this._namespace, name),
|
|
295
293
|
cache: this.cache,
|
|
296
|
-
_root: this._root
|
|
297
|
-
_opening: this._opening,
|
|
298
|
-
_cores: this.cores,
|
|
299
|
-
_streams: this._replicationStreams,
|
|
300
|
-
_streamSessions: this._streamSessions
|
|
294
|
+
_root: this._root
|
|
301
295
|
})
|
|
302
296
|
}
|
|
303
297
|
|
|
@@ -387,6 +381,7 @@ function isStream (s) {
|
|
|
387
381
|
return typeof s === 'object' && s && typeof s.pipe === 'function'
|
|
388
382
|
}
|
|
389
383
|
|
|
390
|
-
function forceClose (core) {
|
|
384
|
+
async function forceClose (core) {
|
|
385
|
+
await core.ready()
|
|
391
386
|
return Promise.all(core.sessions.map(s => s.close()))
|
|
392
387
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "corestore",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.1",
|
|
4
4
|
"description": "A Hypercore factory that simplifies managing collections of cores.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
],
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"brittle": "^3.0.0",
|
|
28
|
-
"random-access-memory": "^
|
|
28
|
+
"random-access-memory": "^6.0.0",
|
|
29
29
|
"standardx": "^7.0.0"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"b4a": "^1.3.1",
|
|
33
|
-
"hypercore": "^10.
|
|
33
|
+
"hypercore": "^10.3.1",
|
|
34
34
|
"hypercore-crypto": "^3.2.1",
|
|
35
35
|
"safety-catch": "^1.0.1",
|
|
36
36
|
"sodium-universal": "^3.0.4",
|