dat-librarian

1.1.8-beta • Public • Published

DatLibrarian

Stability NPM Version JS Standard Style Build Status Coverage Status Greenkeeper badge

A dedicated Dat peer. Use DatLibrarian to store and peer many Dat archives. Similar to hypercore-archiver.

For example:

const DatLibrarian = require('dat-librarian')
const librarian = new DatLibrarian({ dir: '.dats' })
console.log(librarian.keys)
> [] // no known archives yet
librarian
  // load pre-existing archives
  .load()
  // promises!
  .then(() => {
    // use keys or links per dat-link-resolve
    return librarian.add('garbados.hashbase.io')
  })
  .then(() => {
    console.log(librarian.keys)
    > ['c33bc8d7c32a6e905905efdbf21efea9ff23b00d1c3ee9aea80092eaba6c4957']
  })

Install

Install DatLibrarian and its dependencies with npm:

npm i -S dat-librarian

Usage

Table of Contents

DatLibrarian is a dedicated Dat peer similar to hypercore-archiver that persists archives to disk and seeds them to the network.

Examples

const DatLibrarian = require('dat-librarian')
const librarian = new DatLibrarian({ dir: '.dats' })
librarian
  // load pre-existing archives
  .load()
  // promises!
  .then(() => {
    // use keys or links per dat-link-resolve
    return librarian.add('garbados.hashbase.io')
  })

DatLibrarian

Extends EventEmitter

Instantiate a new DatLibriarian.

Parameters

  • options Object Options object.
    • options.dir String The librarian's working directory, where it will cache archives. Required.
    • options.dat Object Options object passed to Dat()
    • options.net Object Options object passed to dat.joinNetwork()

load

Load Dat archives into cache by checking the working directory for existing archives.

Examples

librarian.load().then(() => {
  ...
})

Returns Promise<Array> A promise that resolves once any existing archives have been loaded into the cache.

get

Get an archive from the cache by link.

Parameters

Examples

librarian.get('garbados.hashbase.io').then((dat) => {
  ...
})

Returns Promise<Dat> Promise that resolves to a Dat archive.

add

Add an archive by link. Automatically joins the network and begins downloading, but does not wait for the archive to complete.

Parameters

Examples

librarian.add('garbados.hashbase.io').then((dat) => {
  ...
})

Returns Promise A promise that resolves once the archive has been added to the cache.

remove

Remove an archive from the cache and the working directory.

Parameters

Examples

librarian.remove('garbados.hashbase.io').then(() => {
  ...
})

Returns Promise A promise that resolves once the archive has been removed.

list

Lists the keys in the cache.

Examples

let keys = librarian.list()
console.log(keys)
> ['c33bc8d7c32a6e905905efdbf21efea9ff23b00d1c3ee9aea80092eaba6c4957']

Returns Array<String> An array of all the keys in the cache.

keys

Getter for the keys in the cache. Alias to #list()

Examples

console.log(librarian.keys)
> ['c33bc8d7c32a6e905905efdbf21efea9ff23b00d1c3ee9aea80092eaba6c4957']

Returns Array<String> An array of all the keys in the cache.

close

Close the librarian and any archives it is peering.

Examples

librarian.close().then(() => {
  ...
})

Returns Promise Promise that resolves once all archives have closed.

resolve

Promification of dat-link-resolve for convenience's sake.

Parameters

Examples

DatLibrarian.resolve('garbados.hashbase.io').then((key) => {
  console.log(key)
  > 'c33bc8d7c32a6e905905efdbf21efea9ff23b00d1c3ee9aea80092eaba6c4957'
})

Returns Promise<Buffer> Key of that Dat archive

join

Event emitted once an archive has completed its first round of peer discovery.

Examples

librarian.on('join', (dat) => {
  ...
})

add

Event emitted when an archive is added.

Examples

librarian.on('add', (dat) => {
  ...
})

remove

Event emitted once an archive has been removed.

Examples

librarian.on('remove', (link) => {
  ...
})

Development

Download the source and run the test suite:

git clone garbados/dat-librarian
cd dat-librarian
npm install
npm test

Contributions

All contributions are welcome: bug reports, feature requests, "why doesn't this work" questions, patches for fixes and features, etc. For all of the above, file an issue or submit a pull request.

License

Apache-2.0

Dependencies (4)

Dev Dependencies (3)

Package Sidebar

Install

npm i dat-librarian

Weekly Downloads

10

Version

1.1.8-beta

License

Apache-2.0

Unpacked Size

17 kB

Total Files

3

Last publish

Collaborators

  • garbados