hyperdrive
A file sharing network based on rabin file chunking and append only feeds of data verified by merkle trees.
npm install hyperdrive
If you are interested in learning how hyperdrive works on a technical level a specification is available in the Dat docs repo
Usage
First create a new feed and share it
var hyperdrive =var level =var swarm =var db =var drive =var archive = drivevar ws = archive // add hello.txtwswswsarchive
Then we can access the content from another process with the following code
var swarm =var hyperdrive =var level =var db =var drive =var link = 'your-hyperdrive-link-from-the-above-example' 'hex'var archive = driveswarmswarmswarm
If you want to write/read files to the file system provide a storage driver as the file option
var raf = // a storage driver that writes to the file systemvar archive = drive
API
var drive = hyperdrive(db)
Create a new hyperdrive instance. db should be a levelup instance.
var archive = drive.createArchive([key], [options])
Creates an archive instance. If you want to download/upload an existing archive provide the archive key as the first argument. Options include
live: false // set this to share the archive without finalizing itsparse: false // set this to only download the pieces of the feed you are requesting / prioritizing{// set this to determine how file data is stored.// the storage instance should implement the hypercore storage api// https://github.com/mafintosh/hypercore#storage-apireturn someStorageInstance}
If you do not provide the file option all file data is stored in the leveldb.
archive.key
A buffer that verifies the archive content. In live mode this is a 32 byte public key. Otherwise it is a 32 byte hash.
archive.live
Boolean whether archive is live. true
by default. Note that its only populated after archive.open(cb) has been fired.
archive.append(entry, callback)
Append an entry to the archive. Only possible if this is an live archive you originally created or an unfinalized archive.
If you set the file option in the archive constructor you can use this method to append an already existing file to the archive.
var archive = drivearchive
archive.finalize([callback])
Finalize the archive. You need to do this before sharing it if the archive is not live.
archive.get(index, callback)
Reads an entry from the archive.
archive.download(index, callback)
Fully downloads a file / entry from the archive and calls the callback afterwards.
archive.close([callback])
Closes and releases all resources used by the archive. Call this when you are done using it.
archive.on('download', data)
Emitted every time a piece of data is downloaded
archive.on('upload', data)
Emitted every time a piece of data is uploaded
var rs = archive.list(opts={}, cb)
Returns a readable stream of all entries in the archive.
opts.offset
- start streaming from this offset (default: 0)opts.live
- keep the stream open as new updates arrive (default: false)
You can collect the results of the stream with cb(err, entries)
.
var rs = archive.createFileReadStream(entry, [options])
Returns a readable stream of the file content of an file in the archive.
Options include:
start: startOffset // defaults to 0end: endOffset // defaults to file.length
var ws = archive.createFileWriteStream(entry)
Returns a writable stream that writes a new file to the archive. Only possible if the archive is live and you own it or if the archive is not finalized.
var cursor = archive.createByteCursor(entry, [options])
Creates a cursor that can seek and traverse parts of the file.
var cursor = archive// seek to byte offset 10000 and read the rest.cursor
Options include
start: startOffset // defaults to 0end: endOffset // defaults to file.length
var stream = archive.replicate()
Pipe this stream together with another peer that is interested in the same archive to replicate the content.
License
MIT