Skip to content

Commit

Permalink
feat(namespace): Official support overriding the default namespace
Browse files Browse the repository at this point in the history
use opts.namespace instead opts._namespace

 add documentation to
opts.namespace

BREAKING CHANGE: undocumented opts._namespace won't work anymore

Closes hypercore-protocol#2
  • Loading branch information
Nuhvi committed Apr 14, 2022
1 parent 9e9c299 commit b23dbb4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ 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.

#### `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 if the `key` options is set).
#### `const core = store.get(key | options)`
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` option is set).

`options` include:

``` js
{
name: 'a-name', // name to derive the Hypercore keyPair from
key: key, // or pass the public key of a non-writable Hypercore
...hypercoreOpt
}
```

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

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

#### `const stream = store.replicate(opts)`
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 Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = class Corestore extends EventEmitter {

this._keyStorage = null
this._primaryKey = opts.primaryKey
this._namespace = opts._namespace || DEFAULT_NAMESPACE
this._namespace = opts.namespace || DEFAULT_NAMESPACE
this._replicationStreams = opts._streams || []
this._overwrite = opts.overwrite === true

Expand Down Expand Up @@ -277,7 +277,7 @@ module.exports = class Corestore extends EventEmitter {
namespace (name) {
if (!b4a.isBuffer(name)) name = b4a.from(name)
return new Corestore(this.storage, {
_namespace: generateNamespace(this._namespace, name),
namespace: generateNamespace(this._namespace, name),
_opening: this._opening,
_cores: this.cores,
_streams: this._replicationStreams,
Expand Down

0 comments on commit b23dbb4

Please sign in to comment.