Skip to content
A pure JavaScript browser-friendly api for using dat
Branch: master
Clone or download
Latest commit 419cefc Mar 20, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests New api + hyperdrive 9 + websocket replication (#13) Feb 18, 2019
.gitignore
.travis.yml add node Dec 21, 2016
.zuul.yml Make browser friendly by using webrtc-swarm Feb 17, 2017
README.md Fix typo (#19) Feb 25, 2019
index.js Fix opts not getting passed to dat.get() Mar 20, 2019
package.json Version 7.1.1 Mar 20, 2019
repo.js Change how storage works, update docs (#18) Feb 22, 2019

README.md

dat-js Travis NPM version

A pure JavaScript browser-friendly api for using dat.

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

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 dat = new Dat()
var repo = dat.get('dat://SOME_ARCHIVE_URL')

var readStream = repo.archive.readFile('hello.txt', function (err, data) {
  console.log(data)
})

Replicating a dat in memory

var Dat = require('dat-js')

var dat = new Dat()
var repo = dat.create()

console.log('dat url is:', repo.url)

// You can start reading/writing right away
var writer = repo.archive.createWriteStream('hello.txt')

writer.write('world')
writer.end(function () { replicate(repo.url) })

function replicate (url) {
  var clone = new Dat()
  var repo = clone.get(url)

  var readStream = repo.archive.createReadStream('hello.txt')
  readStream.on('data', function (data) {
    console.log(data.toString()) // prints 'world'
  })
}

Replication with a websocket gateway

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

var dat = new Dat({
  gateway: 'ws://gateway.mauve.moe:3000'
})
var repo = dat.get('dat://SOME_ARCHIVE_URL')

var readStream = repo.archive.createReadStream('hello.txt')

pump(readStream, concat(function (data) {
  console.log(data)
}))

Persisting a created dat and loading it from storage

var Dat = require('dat-js')
var db = require('random-access-idb')('dats')

var dat = new Dat()

var repo = dat.create({
  db: db
})

repo.archive.writeFile('/example.txt', 'Hello World!', () => {
  // Save it for later
  localStorage.setItem('My_Repo', repo.url)
})

// Next time your app loads

var repo = dat.get(localStorage.getItem('My_Repo'), {
  db: db
})

repo.archive.readFile('/example.txt', 'utf-8', (err, data) => {
  console.log(`It's still there: ${data}`)
})

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.

  • options: any options you can pass to mafintosh/hyperdrive. These options will become default for all dats. In addition it has the following:
  • signalhub: An optional string or array of strings for signalhubws servers to use for WebRTC. Note that this isn't compatible with older signalhub servers. These servers are used to discover and connect to WebRTC peers. Note that each signalhub constitutes a different WebRTC swarm. The default is https://signalhubws.mauve.moe/
  • gateway: An optional string or array of strings for dat-gateway instances for websocket replication. This is used to proxy data from the rest of the dat network when there are no WebRTC peers available with your data. There is no gateway set by default, but you can use https://gateway.mauve.moe/ if you want it.

dat.get(url, [options])

Adds a new dat with the given url. Joins the appropriate swarm for that url and begins to upload and download data. If the dat was already added, it will return the existing instance. One gotcha is that dat-js doesn't support DNS resolution yet. As such you'll need to use the actual archive key for loading websites.

  • url: Either a dat:// url or just the public key in string form.
  • options: These options will override any options given in the Dat constructor.

dat.create([options])

Creates a new dat, wait for it to be ready before trying to access the url.

  • options: These options will override any options given in the Dat constructor.

dat.has(url)

Returns whether a given url has been loaded already.

Properties

dat.repos

Array of repo instances

Repo

The repo object managed by dat.

repo.url

The dat:// url of the repo. Note that for newly created repos, you must wait for it to be ready.

rep.ready(cb)

Invokes the cb once the repo is fully initialized. You can do reads and writes from the archive before then, but this is important if you're creating a new archive. If the repo is already ready, it will invoke cb on the next tick.

repo.close()

Closes the swarm and underlying database.

repo.archive

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

repo.swarm

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

Events

repo

Fired every time a new repo is ready.

close

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

You can’t perform that action at this time.