JSPM

  • Created
  • Published
  • Downloads 259
  • Score
    100M100P100Q11737F
  • License MIT

Share datasets on the Internet

Package Exports

  • dat-node

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dat-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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) // dat is created here with a .dat folder

  var db = dat.db // level db in .dat folder
  var archive = dat.archive // hyperdrive archive

  // Join the network
  var network = dat.network(opts) 
  network.swarm // hyperdrive-archive-swarm

  // Track stats
  var stats = dat.stats() // hyperdrive-stats

  // Import Files
  if (archive.owner) {
    var importer = dat.importFiles(opts, cb) // hyperdrive-count-import
  }
})

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:

// dat-js OLD API
var dat = Dat({dir: dir, key: link})
dat.share(function () {
   // import files
   // join network
  console.log('now sharing:', dat.key.toString())
})

In dat-node this would be:

// dat-node v1 NEW API
Dat(dir, {key: link}, function (err, dat) {
  var network = dat.network(opts) // join network
  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), // level-db compatible database
  key: '<dat-key>', // existing key to create archive with or resume
  resume: Boolean, // fail if existing archive differs from opts.key

  // Hyperdrive archive options
  live: Boolean, // archive.live setting (only set if archive is owned)
  file: raf(), // file option for hyperdrive.createArchive()

  // dat-folder-db options
  dbName: '.dat' // directory name for level database
}

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.network([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.stats()

hyperdrive-stats instance for the Dat Archive. Stats are stored in a sublevel database in the .dat folder.