dat-node 1.0 alpha
Node module for Dat (replaces dat-js).
Create Dat Archives with a .dat
folder. Join the Dat Network. Track stats and import files.
Features
Usage
Use to create or read Dat Archives on the file system in Node applications.
var Dat = require('dat-node')
Dat(dir, opts, function (err, dat) {
console.log(dat.path)
var db = dat.db
var archive = dat.archive
var network = dat.joinNetwork(opts)
network.swarm
var stats = dat.trackStats()
if (archive.owner) {
var importer = dat.importFiles(opts, cb)
}
})
Moving from dat-js:
Archives are created with a callback function. Once the archive is created, you can join the network directly without choosing to share or download. If the user owns the archive, they will be able to import files.
For example, previously to share files with dat-js we would write:
var dat = Dat({dir: dir, key: link})
dat.share(function () {
console.log('now sharing:', dat.key.toString())
})
In dat-node this would be:
Dat(dir, {key: link}, function (err, dat) {
var network = dat.joinNetwork(opts)
console.log('now sharing:', dat.key.toString())
var importer = dat.importFiles(opts, function () {
console.log('done importing files')
})
})
API
`Dat(dir, [opts], cb)``
Initialize a Dat Archive in dir
. If there is an existing Dat Archive, the archive will be resumed.
Initial opts
can include:
opts = {
db: level(path/.dat),
key: '<dat-key>',
resume: Boolean,
live: Boolean,
file: raf(),
dbName: '.dat'
}
The callback includes a dat
object that has the following properties:
dat.archive
Hyperdrive archive instance.
dat.db
.dat
folder database
dat.path
Path of the Dat Archive
dat-node
provides an easy interface to common Dat modules for the created Dat Archive on the dat
object provided in the callback:
var network = dat.joinNetwork([opts])
Join the Dat Network for your Dat Archive.
opts
are passed to the swarm module. See hyperdrive-archive-swarm for options.
network.swarm
discovery-swarm instance.
network.peers()
Get number of peers connected to you.
var importer = dat.importFiles([opts], [cb])
(must be the archive owner)
Import files to your Dat Archive from the directory using hyperdrive-import-files. Options are passed to the importer module. cb
is called when import is finished.
dat-node
provides a default ignore option, ignoring the .dat
folder and all hidden files or directories.
Returns the importer event emitter.
var stats = dat.trackStats()
hyperdrive-stats instance for the Dat Archive. Stats are stored in a sublevel database in the .dat
folder.
Transfer speeds are tracked using hyperdrive-network-speed and exposed to stats.network
..
stats.network
Get upload and download speeds: stats.network.uploadSpeed
or stats.network.downloadSpeed
.