Skip to content

Instantly share code, notes, and snippets.

@joehand
Last active October 5, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joehand/b2958b00595f4e8331b50fe265cbe6c7 to your computer and use it in GitHub Desktop.
Save joehand/b2958b00595f4e8331b50fe265cbe6c7 to your computer and use it in GitHub Desktop.
var tape = require('tape')
var ram = require('random-access-memory')
var hyperdrive = require('..')
tape('sparse read/write 1, passes', function (t) {
var archive = hyperdrive(ram)
archive.on('ready', function () {
var clone = hyperdrive(ram, archive.key, {sparse: true})
archive.writeFile('/hello.txt', 'world', function (err) {
t.error(err, 'no error')
archive.writeFile('/hello2.txt', 'world', function (err) {
t.error(err, 'no error')
var stream = clone.replicate()
stream.pipe(archive.replicate()).pipe(stream)
clone.metadata.update(checkFiles)
})
})
function checkFiles () {
clone.stat('/hello.txt', function (err, stat) {
t.error(err, 'no error')
t.ok(stat, 'has stat')
clone.readFile('/hello.txt', function (err, data) {
t.error(err, 'no error')
t.same(data.toString(), 'world', 'data ok')
clone.stat('/hello2.txt', function (err, stat) {
t.error(err, 'no error')
t.ok(stat, 'has stat')
clone.readFile('/hello.txt', function (err, data) {
t.error(err, 'no error')
t.same(data.toString(), 'world', 'data ok')
t.end()
})
})
})
})
}
})
})
tape('sparse read/write 1, fails', function (t) {
var archive = hyperdrive(ram)
archive.on('ready', function () {
var clone = hyperdrive(ram, archive.key, {sparse: true})
archive.writeFile('/hello.txt', 'world', function (err) {
t.error(err, 'no error')
archive.writeFile('/hello2.txt', 'world', function (err) {
t.error(err, 'no error')
var stream = clone.replicate()
stream.pipe(archive.replicate()).pipe(stream)
clone.metadata.update(start)
})
})
function start () {
clone.stat('/hello.txt', function (err, stat) {
t.error(err, 'no error')
t.ok(stat, 'stat ok')
clone.readFile('/hello.txt', function (err, data) {
t.error(err, 'no error')
t.same(data.toString(), 'world', 'data ok')
clone.stat('/hello2.txt', function (err, stat) {
t.error(err, 'no error')
t.ok(stat, 'stat ok')
clone.readFile('/hello2.txt', function (err, data) {
t.error(err, 'no error')
t.same(data.toString(), 'world', 'data ok')
t.end()
})
})
})
})
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment