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.
- package/.github/workflows/test-node.yml +24 -0
- package/README.md +31 -90
- package/index.js +160 -369
- package/lib/keys.js +120 -0
- package/package.json +15 -19
- package/test/all.js +108 -437
- package/test/helpers/index.js +3 -35
- package/test/keys.js +94 -0
- package/.travis.yml +0 -5
- package/CHANGELOG.md +0 -5
- package/LICENSE +0 -21
|
@@ -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
|
-
#
|
|
2
|
-
[](https://github.com/hypercore-skunkworks/neocorestore/actions/workflows/test-node.yml)
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
Corestore is a Hypercore factory that makes it easier to manage large collections of named Hypercores.
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
const core2 =
|
|
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 =
|
|
52
|
-
Create a new
|
|
26
|
+
#### `const store = new Corestore(storage)`
|
|
27
|
+
Create a new Corestore instance.
|
|
53
28
|
|
|
54
|
-
|
|
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.
|
|
62
|
-
|
|
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
|
-
|
|
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
|
-
|
|
36
|
+
All other options besides `name` and `key` will be forwarded to the Hypercore constructor.
|
|
77
37
|
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
`
|
|
99
|
-
|
|
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
|
|
50
|
+
Namespaces can be chained:
|
|
106
51
|
```js
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
+
|