corestore 7.0.13 → 7.0.15
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 +84 -26
- package/package.json +1 -8
package/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const b4a = require('b4a')
|
|
2
2
|
const Hypercore = require('hypercore')
|
|
3
3
|
const ReadyResource = require('ready-resource')
|
|
4
|
-
const EventEmitter = require('events')
|
|
5
4
|
const sodium = require('sodium-universal')
|
|
6
5
|
const crypto = require('hypercore-crypto')
|
|
7
6
|
const ID = require('hypercore-id-encoding')
|
|
@@ -77,11 +76,14 @@ class SessionTracker {
|
|
|
77
76
|
}
|
|
78
77
|
}
|
|
79
78
|
|
|
80
|
-
class CoreTracker
|
|
79
|
+
class CoreTracker {
|
|
81
80
|
constructor () {
|
|
82
|
-
super()
|
|
83
81
|
this.map = new Map()
|
|
84
82
|
this.watching = []
|
|
83
|
+
|
|
84
|
+
this._gcing = new Set()
|
|
85
|
+
this._gcInterval = null
|
|
86
|
+
this._gcCycleBound = this._gcCycle.bind(this)
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
get size () {
|
|
@@ -100,15 +102,33 @@ class CoreTracker extends EventEmitter {
|
|
|
100
102
|
store.watchIndex = -1
|
|
101
103
|
}
|
|
102
104
|
|
|
105
|
+
resume (id) {
|
|
106
|
+
const core = this.map.get(id)
|
|
107
|
+
|
|
108
|
+
if (!core) return null
|
|
109
|
+
|
|
110
|
+
// signal back that we have a closing one stored
|
|
111
|
+
if (core.closing) return core
|
|
112
|
+
|
|
113
|
+
if (core.gc) {
|
|
114
|
+
this._gcing.delete(core)
|
|
115
|
+
if (this._gcing.size === 0) this._stopGC()
|
|
116
|
+
core.gc = 0
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return core
|
|
120
|
+
}
|
|
121
|
+
|
|
103
122
|
get (id) {
|
|
104
123
|
// we allow you do call this from the outside, so support normal buffers also
|
|
105
124
|
if (b4a.isBuffer(id)) id = b4a.toString(id, 'hex')
|
|
106
|
-
|
|
125
|
+
const core = this.map.get(id)
|
|
126
|
+
if (!core || core.closing) return null
|
|
127
|
+
return core
|
|
107
128
|
}
|
|
108
129
|
|
|
109
130
|
set (id, core) {
|
|
110
131
|
this.map.set(id, core)
|
|
111
|
-
this.emit('add', core) // TODO: will be removed
|
|
112
132
|
if (this.watching.length > 0) this._emit(core)
|
|
113
133
|
}
|
|
114
134
|
|
|
@@ -119,13 +139,53 @@ class CoreTracker extends EventEmitter {
|
|
|
119
139
|
}
|
|
120
140
|
}
|
|
121
141
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
this.
|
|
142
|
+
_gc (core) {
|
|
143
|
+
const id = toHex(core.discoveryKey)
|
|
144
|
+
if (this.map.get(id) === core) this.map.delete(id)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_gcCycle () {
|
|
148
|
+
for (const core of this._gcing) {
|
|
149
|
+
if (++core.gc < 4) continue
|
|
150
|
+
const gc = this._gc.bind(this, core)
|
|
151
|
+
core.close().then(gc, gc)
|
|
152
|
+
this._gcing.delete(core)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (this._gcing.size === 0) this._stopGC()
|
|
125
156
|
}
|
|
126
157
|
|
|
127
|
-
|
|
128
|
-
|
|
158
|
+
gc (core) {
|
|
159
|
+
core.gc = 1 // first strike
|
|
160
|
+
this._gcing.add(core)
|
|
161
|
+
if (this._gcing.size === 1) this._startGC()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_stopGC () {
|
|
165
|
+
clearInterval(this._gcInterval)
|
|
166
|
+
this._gcInterval = null
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_startGC () {
|
|
170
|
+
if (this._gcInterval) return
|
|
171
|
+
this._gcInterval = setInterval(this._gcCycleBound, 2000)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
close () {
|
|
175
|
+
this._stopGC()
|
|
176
|
+
this._gcing.clear()
|
|
177
|
+
|
|
178
|
+
const all = []
|
|
179
|
+
for (const core of this.map.values()) all.push(core.close())
|
|
180
|
+
this.map.clear()
|
|
181
|
+
|
|
182
|
+
return Promise.all(all)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
* [Symbol.iterator] () {
|
|
186
|
+
for (const core of this.map.values()) {
|
|
187
|
+
if (!core.closing) yield core
|
|
188
|
+
}
|
|
129
189
|
}
|
|
130
190
|
}
|
|
131
191
|
|
|
@@ -286,17 +346,16 @@ class Corestore extends ReadyResource {
|
|
|
286
346
|
|
|
287
347
|
await Promise.all(closing)
|
|
288
348
|
|
|
289
|
-
|
|
290
|
-
for (const core of this.cores) cores.push(core.close())
|
|
291
|
-
await Promise.all(cores)
|
|
349
|
+
await this.cores.close()
|
|
292
350
|
await this.storage.close()
|
|
293
351
|
}
|
|
294
352
|
|
|
295
353
|
async _attachMaybe (muxer, discoveryKey) {
|
|
296
354
|
if (this.opened === false) await this.ready()
|
|
297
|
-
if (this.cores.get(toHex(discoveryKey)) === null && !(await this.storage.has(discoveryKey))) return
|
|
355
|
+
if (this.cores.get(toHex(discoveryKey)) === null && !(await this.storage.has(discoveryKey, { ifMigrated: true }))) return
|
|
356
|
+
if (this.closing) return
|
|
298
357
|
|
|
299
|
-
const core = this.
|
|
358
|
+
const core = this._openCore(discoveryKey, { createIfMissing: false })
|
|
300
359
|
|
|
301
360
|
if (!core) return
|
|
302
361
|
if (!core.opened) await core.ready()
|
|
@@ -304,6 +363,8 @@ class Corestore extends ReadyResource {
|
|
|
304
363
|
if (!core.replicator.attached(muxer)) {
|
|
305
364
|
core.replicator.attachTo(muxer)
|
|
306
365
|
}
|
|
366
|
+
|
|
367
|
+
core.checkIfIdle()
|
|
307
368
|
}
|
|
308
369
|
|
|
309
370
|
replicate (isInitiator, opts) {
|
|
@@ -378,11 +439,12 @@ class Corestore extends ReadyResource {
|
|
|
378
439
|
|
|
379
440
|
// if not not we can sync create it, which just is easier for the
|
|
380
441
|
// upstream user in terms of guarantees (key is there etc etc)
|
|
381
|
-
const core = this.
|
|
442
|
+
const core = this._openCore(null, opts)
|
|
382
443
|
|
|
383
444
|
conf.core = core
|
|
384
445
|
conf.sessions = this.sessions.get(core.id)
|
|
385
446
|
conf.ongc = this._ongcBound
|
|
447
|
+
|
|
386
448
|
return this._makeSession(conf)
|
|
387
449
|
}
|
|
388
450
|
|
|
@@ -404,7 +466,7 @@ class Corestore extends ReadyResource {
|
|
|
404
466
|
const discoveryKey = opts.name ? await this.storage.getAlias({ name: opts.name, namespace: this.ns }) : null
|
|
405
467
|
this._maybeClosed()
|
|
406
468
|
|
|
407
|
-
const core = this.
|
|
469
|
+
const core = this._openCore(discoveryKey, opts)
|
|
408
470
|
|
|
409
471
|
return {
|
|
410
472
|
core,
|
|
@@ -448,18 +510,15 @@ class Corestore extends ReadyResource {
|
|
|
448
510
|
return result
|
|
449
511
|
}
|
|
450
512
|
|
|
451
|
-
|
|
452
|
-
return this.cores.get(toHex(discoveryKey)) !== null
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
_getCore (discoveryKey, opts) {
|
|
513
|
+
_openCore (discoveryKey, opts) {
|
|
456
514
|
const auth = this._auth(discoveryKey, opts)
|
|
457
515
|
|
|
458
516
|
const id = toHex(auth.discoveryKey)
|
|
459
|
-
const existing = this.cores.
|
|
460
|
-
if (existing) return existing
|
|
517
|
+
const existing = this.cores.resume(id)
|
|
518
|
+
if (existing && !existing.closing) return existing
|
|
461
519
|
|
|
462
520
|
const core = Hypercore.createCore(this.storage, {
|
|
521
|
+
preopen: existing ? existing.closing : null, // always wait for the prev one to close first in any case...
|
|
463
522
|
eagerUpgrade: true,
|
|
464
523
|
notDownloadingLinger: opts.notDownloadingLinger,
|
|
465
524
|
allowFork: opts.allowFork !== false,
|
|
@@ -478,8 +537,7 @@ class Corestore extends ReadyResource {
|
|
|
478
537
|
})
|
|
479
538
|
|
|
480
539
|
core.onidle = () => {
|
|
481
|
-
|
|
482
|
-
this.cores.delete(id, core)
|
|
540
|
+
this.cores.gc(core)
|
|
483
541
|
}
|
|
484
542
|
|
|
485
543
|
core.replicator.ondownloading = () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "corestore",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.15",
|
|
4
4
|
"description": "A Hypercore factory that simplifies managing collections of cores.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
],
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"b4a": "^1.6.7",
|
|
12
|
-
"bare-events": "^2.5.0",
|
|
13
12
|
"hypercore": "^11.0.0",
|
|
14
13
|
"hypercore-crypto": "^3.4.2",
|
|
15
14
|
"hypercore-id-encoding": "^1.3.0",
|
|
@@ -25,12 +24,6 @@
|
|
|
25
24
|
"scripts": {
|
|
26
25
|
"test": "standard && brittle test/*.js"
|
|
27
26
|
},
|
|
28
|
-
"imports": {
|
|
29
|
-
"events": {
|
|
30
|
-
"bare": "bare-events",
|
|
31
|
-
"default": "events"
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
27
|
"repository": {
|
|
35
28
|
"type": "git",
|
|
36
29
|
"url": "https://github.com/holepunchto/corestore2.git"
|