A toolkit for writing Dat-based services in nodejs.
const {createNode} = require('@beaker/dat-node')
// instantiate a new dat node
const dat = createNode({
path: './dat'
})
// get, create, or fork archives
var archive = await dat.getArchive('dat://beakerbrowser.com')
var archive = await dat.createArchive({title: 'My Archive'})
var archive = await dat.forkArchive('dat://beakerbrowser.com', {title: 'My Fork of the Beaker site'})
// you can read & write files within the archive
var filedata = await archive.readFile('/index.html', 'utf8')
await archive.writeFile('/new/thing.json', JSON.stringify({hi: 'world'}))
// listen to global network events
dat.on('network-changed', ...) // change to network conditions (aka new peer)
dat.on('download', ...) // data was downloaded
dat.on('upload', ...) // data was uploaded
dat.on('sync', ...) // a feed was synced
// get internal information about a dat
await dat.getMtime('dat://beakerbrowser.com') // unix timestamp of last change to the dat
await dat.getDiskUsage('dat://beakerbrowser.com') // in bytes
await dat.getSyncProgress('dat://beakerbrowser.com') // 0-1, where 1 is 100% synced
Provides the same Dat APIs that Beaker browser uses, so that code written which depends on the DatArchive will work here and in the browser.
This repo is a work in progress. It still needs:
node examples/simplest-possible-example.js
node examples/create-archive.js
node examples/download-archive.js dat://beakerbrowser.com
Create a new dat dat. See the DatNode
API.
const {createNode} = require('@beaker/dat-node')
var dat = createNode({
path: './data'
})
Async. Start listening for connections in the dat network. Only need to call if createNode
is called with {autoListen: false}
, or if dat.close()
has been called.
Async. Unswarm all dats and stop listening.
Create a new DatArchive
with the given opts.
var archive = dat.getArchive('dat://beakerbrowser.com')
Async. Create a new DatArchive
with the given opts. Will automatically persist and swarm the archive.
var archive = await dat.createArchive({title: 'My new archive'})
Async. Create a new DatArchive
which duplicates the content of the given archive. Can optionally modify the manifest with opts
. Will automatically persist and swarm the archive.
var archive = await dat.forkArchive('dat://beakerbrowser.com', {title: 'My fork of the Beaker site'})
Async. Get the modification time of the dat's data. (Tells you the last time new data was written.)
var mtime = await dat.getMtime('dat://beakerbrowser.com')
Async. Get the amount of bytes being used by the dat in the local cache.
var bytes = await dat.getDiskUsage('dat://beakerbrowser.com')
Async. Get the percentage of the total data downloaded (between 0 and 1).
var pct = await dat.getSyncProgress('dat://beakerbrowser.com')
Async. Is all of the dat's data cached?
var pct = await dat.isFullySynced('dat://beakerbrowser.com')
Get a readable string-stream containing the content of the debug log. Useful for providing debugging interfaces.
TODO decide what features this should include
A DatNodeStorage
instance.
A DatNodeSwarm
instance.
A DatDNS
instance.
The swarm port is now bound and listening.
A critical error has occurred.
A change in the network-connectivity of a given dat.
dat.on('network-changed', (url, {connections}) => {
// ...
})
Some data has been downloaded.
dat.on('download', (url, {feed, block, bytes}) => {
// ...
})
Some data has been uploaded.
dat.on('upload', (url, {feed, block, bytes}) => {
// ...
})
A feed has been fully downloaded.
dat.on('sync', (url, {feed}) => {
// ...
})
The local data store. Includes all data which is persisted onto disk. Can be found on the DatNode
API as dat.storage
.
Async. List the metadata of locally-stored dats.
var datInfos = await dat.storage.list()
Async. Fetch the stored metadata about the archive. Returns null
if not present locally.
var datInfo = await dat.storage.get('dat://beakerbrowser.com')
Async. Delete all cached data for the dat.
await dat.storage.delete('dat://beakerbrowser.com')
Async. Get the modification time of the dat's data. (Tells you the last time new data was written.)
var mtime = await dat.storage.getMtime('dat://beakerbrowser.com')
Async. Get the amount of bytes being used by the dat in the local cache.
var bytes = await dat.storage.getDiskUsage('dat://beakerbrowser.com')
Async. Get the disk-cached DNS lookup result for the given hostname.
Async. Set the disk-cached DNS lookup result for the given hostname.
Async. Remove the disk-cached DNS lookup result for the given hostname.
Async. Load a dat into memory, add it to the local storage (if not yet present) and begin swarming. Does not need to be called if autoSwarm
is true.
await dat.swarm.join('dat://beakerbrowser.com')
Async. Stop swarming the given dat.
await dat.swarm.leave('dat://beakerbrowser.com')
Will not remove the dat's data from the local storage (see dat.storage.remove()
).
Number. The port being listened to.
A 32-byte buffer containing a randomly-generated ID, used to deduplicate connections.
The swarm port is now bound and listening.
A critical error has occurred.
Emitted when a peer has been discovered.
Emitted when a peer has been banned as a connection candidate.
Emitted when a peer has been rejected as a connection candidate.
Emitted when a peer has been dropped from tracking, typically because it failed too many times to connect.
Emitted when a connection is being attempted.
Emitted when a connection attempt fails.
Emitted when you've connected to a peer and are now initializing the connection's session.
Emitted when the handshake fails to complete in a timely fashion.
Emitted when you have fully connected to another peer.
Emitted when you've disconnected from a peer.
Emitted when multiple connections are detected with a peer, and so one is going to be dropped (the connection given).
The DNS manager. Can be found on the DatNode
API as dat.dns
.
Async. Get the key of the given URL.
var key = await dat.dns.resolve('dat://beakerbrowser.com')
A dat "archive" instance. See the DatArchive
API docs.
const {createNode} = require('@beaker/dat-node')
const dat = createNode()
var archive = dat.getArchive('dat://beakerbrowser.com')
await archive.readdir('/') => [...]