Home

Awesome

dat-js Travis NPM version

A pure JavaScript browser-friendly api for using dat.

This repo is not actively maintined. It uses WebRTC for the networking, which none of our other tools use. See this issue for more details and discussion.

Dat is a powerful decentralized data sharing tool. For a Node.js api for working with dats on the filesystem, see dat-node.

Note: Because dat-js uses webrtc, it can only connect to other browser clients. It is not possible for the dat-js library to connect to the UTP and UDP clients used in the Node.js versions.

Want to use Dat in the command line or an app (not build applications)? Check out:

Learn more! docs.datproject.org or chat with us (#dat on IRC)

Example

Getting data from a remote dat

var Dat = require('dat-js')
var concat = require('concat-stream')

var dat = Dat()
dat.add('ARCHIVE_KEY', function (repo) {
  var readStream = repo.archive.createFileReadStream('hello.txt')
  concat(readStream, function (data) {
    console.log(data)
  })
})

Replicating a dat in memory

var Dat = require('dat-js')

var dat = Dat()
dat.add(function (repo) {
  console.log('dat key is:', repo.key)
  var writer = repo.archive.createFileWriteStream('hello.txt')
  writer.write('world')
  writer.end(function () { replicate(repo.key) })
})

function replicate (key) {
  var clone = Dat()
  clone.add(key, function (repo) {
    var readStream = repo.archive.createFileReadStream('hello.txt')
    readStream.on('data', function (data) {
      console.log(data.toString()) // prints 'world'
    })
  })
}

API

var dat = new Dat([options])

Creates a new dat object. The options passed here will be default for any dats created using the add method.

dat.add(key, [options], [onrepo])

Adds a new dat with the given key. Joins the appropriate swarm for that key and begins to upload and download data. The onrepo function will be called when the dat is finished being created.

Properties

dat.repos

Array of repo instances

Repo

The repo object managed by dat.

repo.key

The key of the repo

repo.destroy()

Destroys the swarm and underlying database.

repo.swarm

Get to the original webrtc-swarm instance, where the swarm can be managed.

repo.archive

Get to the original hyperdrive archive instance, where files can be managed using that api.

Events

repo

Fired every time a new repo is ready.

close

Fired when dat is finished closing, including swarm and database.