corestore 6.15.14 → 6.16.0

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 +35 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -16,6 +16,7 @@ const PRIMARY_KEY_FILE_NAME = 'primary-key'
16
16
  const USERDATA_NAME_KEY = 'corestore/name'
17
17
  const USERDATA_NAMESPACE_KEY = 'corestore/namespace'
18
18
  const POOL_SIZE = 512 // how many open fds to aim for before cycling them
19
+ const DEFAULT_MANIFEST = 0 // bump to 1 when this is more widely deployed
19
20
 
20
21
  module.exports = class Corestore extends ReadyResource {
21
22
  constructor (storage, opts = {}) {
@@ -28,6 +29,7 @@ module.exports = class Corestore extends ReadyResource {
28
29
  this.cache = !!opts.cache
29
30
  this.primaryKey = opts.primaryKey || null
30
31
  this.passive = !!opts.passive
32
+ this.manifestVersion = typeof opts.manifestVersion === 'number' ? opts.manifestVersion : (root ? root.manifestVersion : DEFAULT_MANIFEST)
31
33
 
32
34
  this._keyStorage = null
33
35
  this._bootstrap = opts._bootstrap || null
@@ -167,6 +169,18 @@ module.exports = class Corestore extends ReadyResource {
167
169
  if (this._bootstrap) await this._openNamespaceFromBootstrap()
168
170
  }
169
171
 
172
+ async _exists (discoveryKey) {
173
+ const id = b4a.toString(discoveryKey, 'hex')
174
+ const storageRoot = getStorageRoot(id)
175
+
176
+ const st = this.storage(storageRoot + '/oplog')
177
+
178
+ const exists = await new Promise((resolve) => st.stat((err, st) => resolve(!err && st.size > 0)))
179
+ await new Promise(resolve => st.close(resolve))
180
+
181
+ return exists
182
+ }
183
+
170
184
  async _generateKeys (opts) {
171
185
  if (opts._discoveryKey) {
172
186
  return {
@@ -206,14 +220,27 @@ module.exports = class Corestore extends ReadyResource {
206
220
  const publicKey = opts.publicKey || keyPair.publicKey
207
221
 
208
222
  if (opts.compat === false) {
209
- const manifest = { signer: { publicKey } } // default manifest
210
- const key = Hypercore.key(manifest)
223
+ let manifest = { version: this.manifestVersion, signers: [{ publicKey }] } // default manifest
224
+ let key = Hypercore.key(manifest)
225
+ let discoveryKey = crypto.discoveryKey(key)
226
+
227
+ if (!(await this._exists(discoveryKey)) && manifest.version !== 0) {
228
+ const manifestV0 = { version: 0, signers: [{ publicKey }] }
229
+ const keyV0 = Hypercore.key(manifestV0)
230
+ const discoveryKeyV0 = crypto.discoveryKey(keyV0)
231
+
232
+ if (await this._exists(discoveryKeyV0)) {
233
+ manifest = manifestV0
234
+ key = keyV0
235
+ discoveryKey = discoveryKeyV0
236
+ }
237
+ }
211
238
 
212
239
  return {
213
240
  manifest,
214
241
  keyPair,
215
242
  key,
216
- discoveryKey: crypto.discoveryKey(key)
243
+ discoveryKey
217
244
  }
218
245
  }
219
246
 
@@ -275,7 +302,7 @@ module.exports = class Corestore extends ReadyResource {
275
302
 
276
303
  // No more async ticks allowed after this point -- necessary for caching
277
304
 
278
- const storageRoot = [CORES_DIR, id.slice(0, 2), id.slice(2, 4), id].join('/')
305
+ const storageRoot = getStorageRoot(id)
279
306
  const core = new Hypercore(p => this.storage(storageRoot + '/' + p), {
280
307
  _preready: this._preready.bind(this),
281
308
  notDownloadingLinger: this._notDownloadingLinger,
@@ -544,3 +571,7 @@ async function forceClose (core) {
544
571
  await core.ready()
545
572
  return Promise.all(core.sessions.map(s => s.close()))
546
573
  }
574
+
575
+ function getStorageRoot (id) {
576
+ return CORES_DIR + '/' + id.slice(0, 2) + '/' + id.slice(2, 4) + '/' + id
577
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.15.14",
3
+ "version": "6.16.0",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {