corestore 5.8.0 → 6.0.0-alpha.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.
@@ -0,0 +1,24 @@
1
+ name: Test on Node.js
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - main
10
+ jobs:
11
+ build:
12
+ strategy:
13
+ matrix:
14
+ node-version: [12.x, 14.x, 16.x]
15
+ os: [ubuntu-latest, macos-latest, windows-latest]
16
+ runs-on: ${{ matrix.os }}
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Use Node.js ${{ matrix.node-version }}
20
+ uses: actions/setup-node@v1
21
+ with:
22
+ node-version: ${{ matrix.node-version }}
23
+ - run: npm install
24
+ - run: npm test
package/README.md CHANGED
@@ -1,15 +1,13 @@
1
- # corestore
2
- [![Build Status](https://travis-ci.com/andrewosh/corestore.svg?token=WgJmQm3Kc6qzq1pzYrkx&branch=master)](https://travis-ci.com/andrewosh/corestore)
1
+ # Corestore v6
2
+ [![Test on Node.js](https://github.com/hypercore-skunkworks/neocorestore/actions/workflows/test-node.yml/badge.svg)](https://github.com/hypercore-skunkworks/neocorestore/actions/workflows/test-node.yml)
3
3
 
4
- This module is the canonical implementation of the "corestore" interface, which exposes a Hypercore factory and a set of associated functions for managing generated Hypercores.
4
+ Corestore is a Hypercore factory that makes it easier to manage large collections of named Hypercores.
5
5
 
6
- A corestore is designed to efficiently store and replicate multiple sets of interlinked Hypercores, such as those used by [Hyperdrive](https://github.com/mafintosh/hyperdrive) and [mountable-hypertrie](https://github.com/andrewosh/hypertrie), removing the responsibility of managing custom storage/replication code from these higher-level modules.
7
-
8
- In order to do this, corestore provides:
9
- 1. __Key derivation__ - all writable Hypercore keys are derived from a single master key.
10
- 2. __Caching__ - Two separate caches are used for passively replicating cores (those requested by peers) and active cores (those requested by the owner of the corestore).
11
- 3. __Storage bootstrapping__ - You can create a `default` Hypercore that will be loaded when a key is not specified, which is useful when you don't want to reload a previously-created Hypercore by key.
12
- 4. __Namespacing__ - If you want to create multiple compound data structures backed by a single corestore, you can create namespaced corestores such that each data structure's `default` feed is separate.
6
+ Corestore provides:
7
+ 1. __Key Derivation__ - All writable Hypercore keys are derived from a single master key and a user-provided name.
8
+ 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.
9
+ 3. __Storage Management__ - Hypercores can be stored in any random-access-storage instance, where they will be keyed by their discovery keys.
10
+ 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' })
13
11
 
14
12
  ### Installation
15
13
  `npm i corestore --save`
@@ -18,102 +16,45 @@ In order to do this, corestore provides:
18
16
  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:
19
17
  ```js
20
18
  const Corestore = require('corestore')
21
- const ram = require('random-access-memory')
22
- const store = new Corestore(ram)
23
- await store.ready()
24
- ```
25
-
26
- Hypercores can be generated with both the `get` and `default` methods. If the first writable core is created with `default`, it will be used for storage bootstrapping. We can always reload this bootstrapping core off disk without your having to store its public key externally. Keys for other hypercores should either be stored externally, or referenced from within the default core:
27
- ```js
28
- const core1 = store1.default()
29
- ```
30
- _Note: You do not have to create a default feed before creating additional ones unless you'd like to bootstrap your corestore from disk the next time it's instantiated._
31
19
 
32
- Additional hypercores can be created by key, using the `get` method. In most scenarios, these additional keys can be extracted from the default (bootstrapping) core. If that's not the case, keys will have to be stored externally:
33
- ```js
34
- const core2 = store1.get({ key: Buffer(...) })
35
- ```
36
- All hypercores are indexed by their discovery keys, so that they can be dynamically injected into replication streams when requested.
37
-
38
- Two corestores can be replicated with the `replicate` function, which accepts hypercore's `replicate` options:
39
- ```js
40
- const store1 = new Corestore(ram)
41
- const store2 = new Corestore(ram)
42
- await Promise.all([store1.ready(), store2.ready()]
43
-
44
- const core1 = store2.get()
45
- const core2 = store2.get({ key: core1.key })
46
- const stream = store1.replicate(true, { live: true })
47
- stream.pipe(store2.replicate(false, { live: true })).pipe(stream) // This will replicate all common cores.
20
+ const store = new Corestore('./my-storage')
21
+ const core1 = store.get({ name: 'core-1' })
22
+ const core2 = store.get({ name: 'core-2' })
48
23
  ```
49
24
 
50
25
  ### API
51
- #### `const store = corestore(storage, [opts])`
52
- Create a new corestore instance. `storage` can be either a random-access-storage module, or a function that takes a path and returns a random-access-storage instance.
26
+ #### `const store = new Corestore(storage)`
27
+ Create a new Corestore instance.
53
28
 
54
- Opts is an optional object which can contain any Hypercore constructor options, plus the following:
55
- ```js
56
- {
57
- cacheSize: 1000 // The size of the LRU cache for passively-replicating cores.
58
- }
59
- ```
29
+ `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.
60
30
 
61
- #### `store.default(opts)`
62
- Create a new default hypercore, which is used for bootstrapping the creation of subsequent hypercores. Options match those in `get`.
31
+ #### `const core = store.get(key | { name: 'a-name', ...hypercoreOpts})`
32
+ 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 if the `key` options is set).
63
33
 
64
- #### `store.get(opts)`
65
- Create a new hypercore. Options can be one of the following:
66
- ```js
67
- {
68
- key: 0x1232..., // A Buffer representing a hypercore key
69
- discoveryKey: 0x1232..., // A Buffer representing a hypercore discovery key (must have been previously created by key)
70
- ...opts // All other options accepted by the hypercore constructor
71
- }
72
- ```
73
-
74
- If `opts` is a Buffer, it will be interpreted as a hypercore key.
34
+ If that Hypercore has previously been loaded, subsequent calls to `get` will return a new Hypercore session on the existing core.
75
35
 
76
- #### `store.on('feed', feed, options)`
36
+ All other options besides `name` and `key` will be forwarded to the Hypercore constructor.
77
37
 
78
- Emitted everytime a feed is loaded internally (ie, the first time get(key) is called).
79
- Options will be the full options map passed to .get.
38
+ #### `const stream = store.replicate(opts)`
39
+ Creates a replication stream that's capable of replicating all Hypercores that are managed by the Corestore, assuming the remote peer has the correct capabilities.
80
40
 
81
- #### `store.replicate(isInitiator, [opts])`
82
- Create a replication stream that will replicate all cores currently in memory in the corestore instance.
41
+ `opts` will be forwarded to Hypercore's `replicate` function.
83
42
 
84
- When piped to another corestore's replication stream, only those cores that are shared between the two corestores will be successfully replicated.
85
-
86
- #### `store.list()`
87
- Returns a Map of all cores currently cached in memory. For each core in memory, the map will contain the following entries:
88
- ```
89
- {
90
- discoveryKey => core,
91
- ...
92
- }
93
- ```
43
+ Corestore replicates in an "all-to-all" fashion, meaning that when replication begins, it will attempt to replicate every Hypercore that's currently loaded and in memory. These attempts will fail if the remote side doesn't have a Hypercore's capability -- Corestore replication does not exchange Hypercore keys.
94
44
 
95
- #### `const namespacedStore = store.namespace('some-name')`
96
- Create a "namespaced" corestore that uses the same underlying storage as its parent, and mirrors the complete corestore API.
45
+ If the remote side dynamically adds a new Hypercore to the replication stream, Corestore will load and replicatethat core if possible.
97
46
 
98
- `namespacedStore.default` returns a different default core, using the namespace as part of key generation, which makes it easier to bootstrap multiple data structures from the same corestore. The general pattern is for all data structures to bootstrap themselves from their corestore's default feed:
99
- ```js
100
- const store = new Corestore(ram)
101
- const drive1 = new Hyperdrive(store.namespace('drive1'))
102
- const drive2 = new Hyperdrive(store.namespace('drive2'))
103
- ```
47
+ #### `const store = store.namespace(name)`
48
+ Create a new namespaced Corestore. Namespacing is useful if you're going to be sharing a single Corestore instance between many applications or components, as it prevents name collisions.
104
49
 
105
- Namespaces currently need to be saved separately outside of corestore (as a mapping from key to namespace), so that data structures remain writable across restarts. Extending the above code, this might look like:
50
+ Namespaces can be chained:
106
51
  ```js
107
- async function getDrive (opts = {}) {
108
- let namespace = opts.key ? await lookupNamespace(opts.key) : await createNamespace()
109
- const namespacedCorestore = store.namespace(namespace)
110
- const drive = new Hyperdrive(namespacedCorestore)
111
- await saveNamespace(drive.key, namespace)
112
- }
52
+ const ns1 = store.namespace('a')
53
+ const ns2 = ns1.namespace('b')
54
+ const core1 = ns1.get({ name: 'main' }) // These will load different Hypercores
55
+ const core2 = ns2.get({ name: 'main' })
113
56
  ```
114
57
 
115
- #### `store.close(cb)`
116
- Close all hypercores previously generated by the corestore.
117
-
118
58
  ### License
119
59
  MIT
60
+