Standalone DHub RPC client
npm install @dhub/client
const DHubClient = require('@dhub/client')
const client = new DHubClient() // connect to the DHub server
const basestore = client.basestore() // make a basestore
const feed = basestore.get(someDDatabaseKey) // make a ddatabase
await feed.get(42) // get some data from the ddatabase
Make a new DHub RPC client. Options include:
{
host: 'dhub', // the ipc name of the running server
// defaults to dhub
port // a TCP port to connect to
}
If port
is specified, or host
and port
are both specified, then the client will attempt to connect over TCP.
If you only provide a host
option, then it will be considered a Unix socket name.
Static method to wait for the local IPC server to be up and running.
Get status of the local daemon. Includes stuff like API version etc.
Fully close the client. Cancels all inflight requests.
Wait for the client to have fully connected and loaded initial data.
Make a new remote basestore. Optionally you can pass a specific namespace to load a specific basestore. If you do not pass a namespace a random one is generated for you.
The remote basestore network instance.
A one-line replication function for RemoteDDatabases
(see below for details).
The remote basestore instances has an API that mimicks the normal basestore API.
Make a new remote ddatabase instance. If you pass a key that specific feed is loaded, if not a new one is made.
Get the "default" feed for this basestore, which is derived from the namespace.
The name (namespace) of this basestore.
Close the basestore. Closes all feeds made in this basestore.
The remote networker instance has an API that mimicks the normal basestore networker API.
Make sure all the peer state is loaded locally. client.ready
calls this for you.
Note you do not have to call this before using any of the apis, this just makes sure network.peers is populated.
A list of peers we are connected to.
Emitted when a peer is added.
Emitted when a peer is removed.
Configure the network for this specific discovery key or RemoteDDatabase. Options include:
{
lookup: true, // should we find peers?
announce: true, // should we announce ourself as a peer?
flush: true // wait for the full swarm flush before returning?
remember: false // persist this configuration so it stays around after we close our session?
}
Register a network protocol extension.
The remote feed instances has an API that mimicks the normal DDatabase API.
The feed public key
The feed discovery key.
Boolean indicating if this feed is writable.
Wait for the key, discoveryKey, writability, initial peers to be loaded.
Get a block of data from the feed.
Options include:
{
ifAvailable: true,
wait: false,
onwait () { ... }
}
See the DDatabase docs for more info on these options.
Note if you don't await the promise straight away you can use it to to cancel the operation, later using feed.cancel
const p = feed.get(42)
// ... cancel the get
feed.cancel(p)
await p // Was cancelled
Cancel a get
Check if the feed has a specific block
Select a range to be downloaded.
Similarly to feed.get
you can use the promise itself
to cancel a download using feed.undownload(p)
Stop downloading a range.
Fetch an update for the feed.
Options include:
{
minLength: ..., // some min length to update to
ifAvailable: true,
hash: true
}
See the DDatabase docs for more info on these options.
Append a block or array of blocks to the ddatabase
A list of peers this feed is connected to.
Emitted when a peer is added.
Emitted when a peer is removed.
Emitted when the feed is appended to, either locally or remotely.
Emitted when a block is downloaded. data
is a pseudo-buffer with {length, byteLength}
but no buffer content.
Emitted when a block is uploaded. data
is a pseudo-buffer with {length, byteLength}
but no buffer content.
DHub also includes a simple replication function for RemoteDDatabases
that does two things:
- It first configures the network (
client.network.configure(base, { announce: true, lookup: true })
) - Then it does a
base.update({ ifAvailable: true })
to try to fetch the latest length from the network.
This saves a bit of time when swarming a RemoteDDatabase
.
Quickly connect a RemoteDDatabase
to the DSwarm network.
MIT