Skip to content

Commit

Permalink
V7 (#97)
Browse files Browse the repository at this point in the history
* v7 wip

* missing getter on CoreTracker

* forward user preload to core

* add back sessions and namespaces

* missing encryption key

* must wait for root in sessions

* or null

* decode keys

* add some compat fields

* wait should default to true

* root core store closes all cores

* switch to rocksdb branch

* update package.json

* primaryKey can be optionally passed in

* global cache

* support setNamespace for mutating the ns if you know what you are doing

* Fix namespace before ready (#98)

* add hanging namespace test

* fix deadlock

---------

Co-authored-by: Mathias Buus <mathiasbuus@gmail.com>

* add week ref test

* kill bootstrap and move to simple from key (#100)

* kill bootstrap and move to simple from key

* kill more

* serial line killer

* test for preready sessions

* missing close

* Session manager (#101)

* wip

* fix mutation during iteration

* move ready down

* limit preload usage (#102)

* Support passing encryption in preload (#103)

* support passing encryption from preload

* init isBlockKey also

* forward writable option

---------

Co-authored-by: Mathias Buus <mathiasbuus@gmail.com>

* pre tag with 7

* cores.get supports buffers

* Fix get (#104)

* we fixed the wrong typo

* add watch/unwatch for monitoring core flows

* pin to specific hash tmp

* v7 bump to hypercore 11 (#105)

* bump to hypercore 11

* updated storage api

* remove stale arg

* correctly set primary key if parallel

* condense

* this.primaryKey may be set in constructor

* tweak core setup to use alias and fix open

* test aliases

* docs

* also decode dkey since public

* basic replication test

---------

Co-authored-by: Christophe Diederichs <chm-diederichs@protonmail.com>
Co-authored-by: Christophe Diederichs <45171645+chm-diederichs@users.noreply.github.com>
Co-authored-by: HDegroote <75906619+HDegroote@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 14, 2025
1 parent 1cca652 commit 756948c
Show file tree
Hide file tree
Showing 12 changed files with 475 additions and 1,769 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

50 changes: 12 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Corestore provides:
### Installation
`npm install corestore`

> [!NOTE]
> This readme reflects Corestore 7, our latest major version that is backed by RocksDB for storage and atomicity.
> Whilst we are fully validating that, the npm dist-tag for latest is set to latest version of Corestore 7, the previous major, to avoid too much disruption.
> It will be updated to 11 in a few weeks.
### Usage
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:
```js
Expand All @@ -24,27 +29,17 @@ const core2 = store.get({ name: 'core-2' })
```

### API
#### `const store = new Corestore(storage, opts = {})`
#### `const store = new Corestore(storage)`
Create a new Corestore instance.

`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.

Opts include:
```js
{
inflightRange: null // Advanced option. Forwarded to the Hypercores created by corestore.get(...)
}
```

#### `const core = store.get(key | { name: 'a-name', exclusive, ...hypercoreOpts})`
#### `const core = store.get(key | { name: 'a-name', ...hypercoreOpts})`
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).

If that Hypercore has previously been loaded, subsequent calls to `get` will return a new Hypercore session on the existing core.

If you set the `exclusive` option and you are opening a writable session it will wait for all other exclusive writable to close before
opening the Hypercore effectively meaning any op on the core will wait until its exclusive.

All other options besides `name` and `key` and `exclusive` will be forwarded to the Hypercore constructor.
All other options besides `name` and `key` will be forwarded to the Hypercore constructor.

#### `const stream = store.replicate(optsOrStream)`
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.
Expand All @@ -67,8 +62,11 @@ swarm.join(...)
swarm.on('connection', (connection) => store.replicate(connection))
```

#### `const storeB = storeA.session()`
Create a new Corestore session. Closing a session will close all cores made from this session.

#### `const store = store.namespace(name)`
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.
Create a new namespaced Corestore session. Namespacing is useful if you're going to be sharing a single Corestore instance between many applications or components, as it prevents name collisions.

Namespaces can be chained:
```js
Expand All @@ -78,32 +76,8 @@ const core1 = ns1.get({ name: 'main' }) // These will load different Hypercores
const core2 = ns2.get({ name: 'main' })
```

#### `const storeB = storeA.session(opts)`
Create a new Corestore that shares resources with the original, like cache, cores, replication streams, and storage, while optionally resetting the namespace, overriding `primaryKey`.
Useful when an application wants to accept an optional Corestore, but needs to maintain a predictable key derivation.

`opts` are the same as the constructor options:

```js
{
primaryKey, // Overrides the primaryKey for this session
namespace, // If set to null it will reset to the DEFAULT_NAMESPACE
detach: true, // By disabling this, closing the session will also close the store that created the session
inflightRange: null // Advanced option. Forwarded to the Hypercores created by `session.get(...)
}
```

#### `await store.close()`
Fully close this Corestore instance.

#### `store.on('core-open', core)`
Emitted when the first session for a core is opened.

*Note: This core may close at any time, so treat it as a weak reference*

#### `store.on('core-close', core)`
Emitted when the last session for a core is closed.

### License
MIT

21 changes: 21 additions & 0 deletions example.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Corestore from './index.js'

const store = new Corestore('./store.db')

const core = store.get({ name: 'yo' })
// await core.close()

const store2 = new Corestore('./store2.db')

await core.ready()
await core.append('yo')

const clone = store2.get({ key: core.key })

const stream = store.replicate(true)
const stream2 = store2.replicate(false)

stream.pipe(stream2).pipe(stream)

await clone.ready()
console.log(core, clone)
Loading

0 comments on commit 756948c

Please sign in to comment.