Skip to content

Instantly share code, notes, and snippets.

@joehand
Last active October 5, 2017 18:52
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/359489c8b9abfce6aaf0a2cdc9e9ea2f to your computer and use it in GitHub Desktop.
Save joehand/359489c8b9abfce6aaf0a2cdc9e9ea2f to your computer and use it in GitHub Desktop.
hyperdrive-http sparse fail
var http = require('http')
var ram = require('random-access-memory')
var hyperdrive = require('hyperdrive')
var serve = require('./serve')
var archive = hyperdrive(ram)
archive.on('ready', function () {
var clone = hyperdrive(ram, archive.key, {sparse: true})
archive.writeFile('/hello.txt', 'world', function (err) {
if (err) console.error(err)
archive.writeFile('/hello2.txt', 'world', function (err) {
if (err) console.error(err)
var stream = clone.replicate()
stream.pipe(archive.replicate()).pipe(stream)
clone.metadata.update(start)
})
})
function start () {
var server = http.createServer(serve(clone))
server.listen(8080)
console.log(`Visit http://localhost:${8080} to see archive`)
}
})
{
"name": "sparse-http",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"hyperdrive": "^9.9.0",
"random-access-memory": "^2.4.0"
}
}
module.exports = serve
function serve (archive) {
return onrequest
function onrequest (req, res) {
var name = req.url
archive.stat(name, function (err, st) {
if (err) return onerror(res, 404, err)
console.log(st)
archive.readFile(name, function (err, data) {
if (err) return onerror(res, 404, err)
res.write(data)
res.end()
})
})
}
}
function onerror (res, status, err) {
res.statusCode = status
res.end(err.stack)
}
tape('write and read (sparse)', function (t) {
t.plan(13)
tmp(function (err, dir, cleanup) {
t.ifError(err, 'no error')
var archive = hyperdrive(dir)
archive.on('ready', function () {
var clone = create(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')
t.notOk(stat.isDirectory(), 'not directory')
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')
t.notOk(stat.isDirectory(), 'not directory')
clone.readFile('/hello.txt', function (err, data) {
t.error(err, 'no error')
t.same(data.toString(), 'world', 'data ok')
})
})
})
})
}
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment