corestore 7.0.5 → 7.0.7

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.
Files changed (2) hide show
  1. package/index.js +46 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -127,6 +127,31 @@ class CoreTracker extends EventEmitter {
127
127
  }
128
128
  }
129
129
 
130
+ class FindingPeers {
131
+ constructor () {
132
+ this.count = 0
133
+ this.pending = []
134
+ }
135
+
136
+ add (core) {
137
+ if (this.count === 0) return
138
+ this.pending.push(core.findingPeers())
139
+ }
140
+
141
+ inc (sessions) {
142
+ if (++this.count !== 1) return
143
+
144
+ for (const core of sessions) {
145
+ this.pending.push(core.findingPeers())
146
+ }
147
+ }
148
+
149
+ dec (sessions) {
150
+ if (--this.count !== 0) return
151
+ while (this.pending.length > 0) this.pending.pop()()
152
+ }
153
+ }
154
+
130
155
  class Corestore extends ReadyResource {
131
156
  constructor (storage, opts = {}) {
132
157
  super()
@@ -137,6 +162,7 @@ class Corestore extends ReadyResource {
137
162
  this.cores = this.root ? this.root.cores : new CoreTracker()
138
163
  this.sessions = new SessionTracker()
139
164
  this.corestores = this.root ? this.root.corestores : new Set()
165
+ this.readOnly = opts.writable === false
140
166
  this.globalCache = this.root ? this.root.globalCache : (opts.globalCache || null)
141
167
  this.primaryKey = this.root ? this.root.primaryKey : (opts.primaryKey || null)
142
168
  this.ns = opts.namespace || DEFAULT_NAMESPACE
@@ -146,6 +172,7 @@ class Corestore extends ReadyResource {
146
172
 
147
173
  this.manifestVersion = 1 // just compat
148
174
 
175
+ this._findingPeers = null // here for legacy
149
176
  this._ongcBound = this._ongc.bind(this)
150
177
 
151
178
  if (this.root) this.corestores.add(this)
@@ -173,6 +200,17 @@ class Corestore extends ReadyResource {
173
200
  }
174
201
  }
175
202
 
203
+ findingPeers () {
204
+ if (this._findingPeers === null) this._findingPeers = new FindingPeers()
205
+ this._findingPeers.inc(this.sessions)
206
+ let done = false
207
+ return () => {
208
+ if (done) return
209
+ done = true
210
+ this._findingPeers.dec(this.sessions)
211
+ }
212
+ }
213
+
176
214
  session (opts) {
177
215
  this._maybeClosed()
178
216
  const root = this.root || this
@@ -314,14 +352,14 @@ class Corestore extends ReadyResource {
314
352
  wait: opts.wait !== false,
315
353
  timeout: opts.timeout || 0,
316
354
  draft: !!opts.draft,
317
- writable: opts.writable
355
+ writable: opts.writable === undefined && this.readOnly ? false : opts.writable
318
356
  }
319
357
 
320
358
  // name requires us to rt to storage + ready, so needs preload
321
359
  // same goes if user has defined async preload obvs
322
360
  if (opts.name || opts.preload) {
323
361
  conf.preload = this._preload(opts)
324
- return new Hypercore(null, null, conf)
362
+ return this._makeSession(conf)
325
363
  }
326
364
 
327
365
  // if not not we can sync create it, which just is easier for the
@@ -331,8 +369,13 @@ class Corestore extends ReadyResource {
331
369
  conf.core = core
332
370
  conf.sessions = this.sessions.get(core.id)
333
371
  conf.ongc = this._ongcBound
372
+ return this._makeSession(conf)
373
+ }
334
374
 
335
- return new Hypercore(null, null, conf)
375
+ _makeSession (conf) {
376
+ const session = new Hypercore(null, null, conf)
377
+ if (this._findingPeers !== null) this._findingPeers.add(session)
378
+ return session
336
379
  }
337
380
 
338
381
  async createKeyPair (name, ns = this.ns) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.0.5",
3
+ "version": "7.0.7",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [