corestore 7.4.5 → 7.4.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.
package/README.md CHANGED
@@ -7,7 +7,7 @@ Corestore is a Hypercore factory that makes it easier to manage large collection
7
7
  Corestore provides:
8
8
  1. __Key Derivation__ - All writable Hypercore keys are derived from a single master key and a user-provided name.
9
9
  2. __Session Handling__ - If a single Hypercore is loaded multiple times through the `get` method, the underlying resources will only be opened once (using Hypercore 10's new session feature). Once all sessions are closed, the resources will be released.
10
- 3. __Storage Management__ - Hypercores can be stored in any random-access-storage instance, where they will be keyed by their discovery keys.
10
+ 3. __Storage Management__ - Hypercores can be stored in any `hypercore-storage` instance, where they will be keyed by their discovery keys.
11
11
  4. __Namespacing__ - You can share a single Corestore instance between multiple applications or components without worrying about naming collisions by creating "namespaces" (e.g. `corestore.namespace('my-app').get({ name: 'main' })`)
12
12
 
13
13
  ### Installation
@@ -19,7 +19,7 @@ Corestore provides:
19
19
  > It will be updated to 11 in a few weeks.
20
20
 
21
21
  ### Usage
22
- A corestore instance can be constructed with a random-access-storage module, a function that returns a random-access-storage module given a path, or a string. If a string is specified, it will be assumed to be a path to a local storage directory:
22
+ A corestore instance can be constructed with a `hypercore-storage` instance, or a string. If a string is specified, it will be assumed to be a path to a local storage directory:
23
23
  ```js
24
24
  const Corestore = require('corestore')
25
25
 
@@ -29,10 +29,21 @@ const core2 = store.get({ name: 'core-2' })
29
29
  ```
30
30
 
31
31
  ### API
32
- #### `const store = new Corestore(storage)`
32
+
33
+ #### `const store = new Corestore(storage, options = {})`
34
+
33
35
  Create a new Corestore instance.
34
36
 
35
- `storage` can be either a random-access-storage module, a string, or a function that takes a path and returns an random-access-storage instance.
37
+ `storage` can be either a `hypercore-storage` instance or a string.
38
+
39
+ Options:
40
+
41
+ ```
42
+ {
43
+ primaryKey: null, // The primary key to use as the master key for key derivation.
44
+ writable: true,
45
+ }
46
+ ```
36
47
 
37
48
  #### `const core = store.get(key | { name: 'a-name', ...hypercoreOpts})`
38
49
  Loads a Hypercore, either by name (if the `name` option is provided), or from the provided key (if the first argument is a Buffer or String with hex/z32 key, or if the `key` options is set).
@@ -79,6 +90,29 @@ const core2 = ns2.get({ name: 'main' })
79
90
  #### `const stream = store.list(namespace)`
80
91
  Creates a discovery key stream of all cores within a namespace or all cores in general if no namespace is provided.
81
92
 
93
+ #### `store.watch((core) => {})`
94
+ Register a callback called when new Hypercores are opened. `core` is the internal core for the opened Hypercore. It can be used to create weak references to a Hypercore like so:
95
+
96
+ ```
97
+ store.watch(function (core) {
98
+ const weakCore = new Hypercore({ core, weak: true })
99
+ })
100
+ ```
101
+
102
+ #### `store.unwatch(callback)`
103
+ Unregister a callback used with `store.watch(callback)` so it no longer fires.
104
+
105
+ #### `await store.suspend()`
106
+ Suspend the underlying storage for the Corestore.
107
+
108
+ #### `await store.resume()`
109
+ Resume a suspended Corestore.
110
+
111
+ #### `const keypair = await store.createKeyPair(name, ns = this.ns)`
112
+ Generate a key pair seeded with the Corestore's primary key using a `name` and a `ns` aka namespace. `ns` defaults to the current namespace.
113
+
114
+ This is useful for creating deterministic key pairs that are unique to a peer.
115
+
82
116
  #### `await store.close()`
83
117
  Fully close this Corestore instance.
84
118
 
package/index.js CHANGED
@@ -230,7 +230,7 @@ class Corestore extends ReadyResource {
230
230
  super()
231
231
 
232
232
  this.root = opts.root || null
233
- this.storage = this.root ? this.root.storage : Hypercore.defaultStorage(storage, { id: opts.id, allowBackup: opts.allowBackup })
233
+ this.storage = this.root ? this.root.storage : Hypercore.defaultStorage(storage, { id: opts.id, allowBackup: opts.allowBackup, readOnly: opts.readOnly })
234
234
  this.streamTracker = this.root ? this.root.streamTracker : new StreamTracker()
235
235
  this.cores = this.root ? this.root.cores : new CoreTracker()
236
236
  this.sessions = new SessionTracker()
package/lib/audit.js CHANGED
@@ -1,12 +1,14 @@
1
1
  module.exports = async function * audit (store, { dryRun = false } = {}) {
2
- for await (const { discoveryKey } of store.storage.createCoreStream()) {
3
- const core = store.get({ discoveryKey, active: false })
4
- await core.ready()
2
+ for await (const { discoveryKey, core } of store.storage.createCoreStream()) {
3
+ if (core.version < 1) continue // not migrated, ignore
5
4
 
6
- yield { discoveryKey, key: core.key, audit: await core.core.audit({ dryRun }) }
5
+ const c = store.get({ discoveryKey, active: false })
6
+ await c.ready()
7
+
8
+ yield { discoveryKey, key: c.key, audit: await c.core.audit({ dryRun }) }
7
9
 
8
10
  try {
9
- await core.close()
11
+ await c.close()
10
12
  } catch {
11
13
  // ignore if failed, we are auditing...
12
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.4.5",
3
+ "version": "7.4.7",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [