diff --git a/cli.js b/cli.js
index c99678b..f43b1ef 100644
--- a/cli.js
+++ b/cli.js
@@ -22,14 +22,14 @@ getDatKey(link, (err, key) => {
})
function start (key) {
- var archive = hyperdrive(storage, key, { sparse: true })
- var server = http.createServer(serve(archive, { live: true }))
+ var drive = hyperdrive(storage, key, { sparse: true })
+ var server = http.createServer(serve(drive, { live: true }))
server.listen(port)
- console.log(`Visit http://localhost:${port} to see archive`)
+ console.log(`Visit http://localhost:${port} to see drive`)
if (key) {
- archive.ready(function () {
- discovery(archive, { live: true })
+ drive.ready(function () {
+ discovery(drive, { live: true })
})
}
}
diff --git a/example.js b/example.js
index 1bd7189..2f54b86 100644
--- a/example.js
+++ b/example.js
@@ -4,14 +4,14 @@ var hyperdrive = require('hyperdrive')
var ram = require('random-access-memory')
var serve = require('.')
-var archive = hyperdrive(ram)
+var drive = hyperdrive(ram)
-var server = http.createServer(serve(archive, { exposeHeaders: true, live: true }))
+var server = http.createServer(serve(drive, { exposeHeaders: true, live: true }))
-archive.writeFile('readme.md', fs.readFileSync('readme.md'))
-archive.writeFile('package.json', fs.readFileSync('package.json'))
-archive.writeFile('index.js', fs.readFileSync('index.js'))
-archive.writeFile('foo/index.html', '
INDEX PAGE YO
')
+drive.writeFile('readme.md', fs.readFileSync('readme.md'))
+drive.writeFile('package.json', fs.readFileSync('package.json'))
+drive.writeFile('index.js', fs.readFileSync('index.js'))
+drive.writeFile('foo/index.html', 'INDEX PAGE YO
')
server.listen(8000)
diff --git a/index.js b/index.js
index 4d5da6d..af2973a 100644
--- a/index.js
+++ b/index.js
@@ -9,56 +9,65 @@ var debug = require('debug')('hyperdrive-http')
module.exports = serve
-function serve (archive, opts) {
+function serve (drive, opts) {
if (!opts) opts = {}
- archive.ready(() => {
- debug('serving', archive.key.toString('hex'))
+ drive.ready(() => {
+ debug('serving', drive.key.toString('hex'))
})
return corsify(onrequest)
function onrequest (req, res) {
- var name = decodeURI(req.url.split('?')[0])
- var query = qs.parse(req.url.split('?')[1] || '')
- opts.viewSource = false // reset for each request
-
- var wait = (query.wait && Number(query.wait.toString())) || 0
- var have = archive.metadata ? archive.metadata.length : -1
-
- if (wait <= have) return checkWebroot()
- waitFor(archive, wait, checkWebroot)
-
- function checkWebroot () {
- if (opts.web_root) return ready() // used cached version
- getManifest(archive, (err, data) => {
- if (err || !data) return ready()
- if (data.web_root) opts.web_root = data.web_root
- ready()
- })
+ if (req.method === 'GET') {
+ ongetfile(drive, opts, req, res)
+ } else {
+ res.statusCode = 500
+ res.end('illegal method')
}
+ }
+}
- function ready () {
- var arch = /^\d+$/.test(query.version) ? archive.checkout(Number(query.version)) : archive
- if (query.viewSource) {
- debug('view source', query)
- opts.viewSource = true
- }
- debug('view', name, 'view dir', name[name.length - 1] === '/')
- if (name[name.length - 1] === '/') ondirectory(arch, name, req, res, opts)
- else onfile(arch, name, req, res, opts)
+function ongetfile (drive, opts, req, res) {
+ var name = decodeURI(req.url.split('?')[0])
+ var query = qs.parse(req.url.split('?')[1] || '')
+ opts.viewSource = false // reset for each request
+
+ var wait = (query.wait && Number(query.wait.toString())) || 0
+ var have = drive.metadata ? drive.metadata.length : -1
+
+ if (wait <= have) return checkWebroot()
+ waitFor(drive, wait, checkWebroot)
+
+ function checkWebroot () {
+ if (opts.web_root) return ready() // used cached version
+ getManifest(drive, (err, data) => {
+ if (err || !data) return ready()
+ if (data.web_root) opts.web_root = data.web_root
+ ready()
+ })
+ }
+
+ function ready () {
+ var arch = /^\d+$/.test(query.version) ? drive.checkout(Number(query.version)) : drive
+ if (query.viewSource) {
+ debug('view source', query)
+ opts.viewSource = true
}
+ debug('view', name, 'view dir', name[name.length - 1] === '/')
+ if (name[name.length - 1] === '/') ondirectory(arch, name, req, res, opts)
+ else onfile(arch, name, req, res, opts)
}
}
-function onfile (archive, name, req, res, opts) {
- archive.stat(name, function (err, st) {
- if (err) return on404(archive, req, res)
+function onfile (drive, name, req, res, opts) {
+ drive.stat(name, function (err, st) {
+ if (err) return on404(drive, req, res)
if (st.isDirectory()) {
res.statusCode = 302
res.setHeader('Location', name + '/')
- ondirectory(archive, name + '/', req, res, opts)
+ ondirectory(drive, name + '/', req, res, opts)
return
}
@@ -75,42 +84,42 @@ function onfile (archive, name, req, res, opts) {
}
if (req.method === 'HEAD') return res.end()
- pump(archive.createReadStream(name, r), res)
+ pump(drive.createReadStream(name, r), res)
})
}
-function on404 (archive, req, res) {
- getManifest(archive, function (err, parsed) {
+function on404 (drive, req, res) {
+ getManifest(drive, function (err, parsed) {
if (err) return onerror(res, 404, err)
var fallbackPage = parsed.fallback_page
if (!fallbackPage) return onerror(res, 404, new Error('Not Found, No Fallback'))
- archive.stat((parsed.web_root || '/') + fallbackPage, function (err) {
+ drive.stat((parsed.web_root || '/') + fallbackPage, function (err) {
if (err) return onerror(res, 404, err)
- onfile(archive, fallbackPage, req, res)
+ onfile(drive, fallbackPage, req, res)
})
})
}
-function ondirectory (archive, name, req, res, opts) {
+function ondirectory (drive, name, req, res, opts) {
debug('ondirectory:', name, 'options', opts)
- if (opts.viewSource) return ondirectoryindex(archive, name, req, res, opts)
+ if (opts.viewSource) return ondirectoryindex(drive, name, req, res, opts)
if (name === '/' && opts.web_root) name = opts.web_root
if (name[name.length - 1] !== '/') name = name + '/'
- archive.stat(name + 'index.html', function (err) {
- if (err) return ondirectoryindex(archive, name, req, res, opts)
- onfile(archive, name + 'index.html', req, res)
+ drive.stat(name + 'index.html', function (err) {
+ if (err) return ondirectoryindex(drive, name, req, res, opts)
+ onfile(drive, name + 'index.html', req, res)
})
}
-function ondirectoryindex (archive, name, req, res, opts) {
- list(archive, name, function (err, entries) {
+function ondirectoryindex (drive, name, req, res, opts) {
+ list(drive, name, function (err, entries) {
if (err) entries = []
- var wait = archive.metadata ? archive.metadata.length + 1 : 0
+ var wait = drive.metadata ? drive.metadata.length + 1 : 0
var script = `
function liveUpdate () {
var xhr = new XMLHttpRequest()
@@ -132,21 +141,21 @@ function ondirectoryindex (archive, name, req, res, opts) {
liveUpdate()
`
- var footer = opts.footer ? 'Archive version: ' + archive.version : null
- var html = toHTML({ directory: name, script: (!opts.live || archive._checkout) ? null : script, footer: footer }, entries)
+ var footer = opts.footer ? 'Archive version: ' + drive.version : null
+ var html = toHTML({ directory: name, script: (!opts.live || drive._checkout) ? null : script, footer: footer }, entries)
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.setHeader('Content-Length', Buffer.byteLength(html))
if (opts.exposeHeaders) {
- res.setHeader('Hyperdrive-Key', archive.key.toString('hex'))
- res.setHeader('Hyperdrive-Version', archive.version)
+ res.setHeader('Hyperdrive-Key', drive.key.toString('hex'))
+ res.setHeader('Hyperdrive-Version', drive.version)
res.setHeader('Hyperdrive-Http-Version', pkg.version)
}
res.end(html)
})
}
-function getManifest (archive, cb) {
- archive.readFile('/dat.json', 'utf-8', function (err, data) {
+function getManifest (drive, cb) {
+ drive.readFile('/dat.json', 'utf-8', function (err, data) {
if (err) return cb(err)
try {
var parsed = JSON.parse(data)
@@ -162,12 +171,12 @@ function getManifest (archive, cb) {
})
}
-function waitFor (archive, until, cb) { // this feels a bit hacky, TODO: make less complicated?
- archive.setMaxListeners(0)
- if (!archive.metadata) archive.once('ready', waitFor.bind(null, archive, until, cb))
- if (archive.metadata.length >= until) return cb()
- archive.metadata.setMaxListeners(0)
- archive.metadata.update(waitFor.bind(null, archive, until, cb))
+function waitFor (drive, until, cb) { // this feels a bit hacky, TODO: make less complicated?
+ drive.setMaxListeners(0)
+ if (!drive.metadata) drive.once('ready', waitFor.bind(null, drive, until, cb))
+ if (drive.metadata.length >= until) return cb()
+ drive.metadata.setMaxListeners(0)
+ drive.metadata.update(waitFor.bind(null, drive, until, cb))
}
function onerror (res, status, err) {
@@ -178,8 +187,8 @@ function onerror (res, status, err) {
res.end(err.stack)
}
-function list (archive, name, cb) {
- archive.readdir(name, function (err, names) {
+function list (drive, name, cb) {
+ drive.readdir(name, function (err, names) {
if (err) return cb(err)
var error = null
@@ -190,7 +199,7 @@ function list (archive, name, cb) {
for (var i = 0; i < names.length; i++) stat(name + names[i], names[i])
function stat (name, base) {
- archive.stat(name, function (err, st) {
+ drive.stat(name, function (err, st) {
if (err) error = err
if (st) {
diff --git a/package.json b/package.json
index 810cc6a..71ae9ce 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
"description": "Handle Hyper[drive|core] HTTP Requests",
"main": "index.js",
"scripts": {
- "test": "standard"
+ "test": "tape test/*.js"
},
"keywords": [
"hypercore",
@@ -29,10 +29,13 @@
},
"homepage": "https://github.com/joehand/hyperdrive-http",
"devDependencies": {
+ "collect-stream": "^1.2.1",
+ "dat-encoding": "^5.0.1",
"dat-link-resolve": "^2.3.0",
- "hyperdiscovery": "^8.0.0",
- "hyperdrive": "^9.0.0",
+ "hyperdrive": "^10.12.3",
"random-access-memory": "^3.0.0",
- "standard": "^12.0.1"
+ "request": "^2.88.2",
+ "standard": "^14.3.4",
+ "tape": "^5.0.1"
}
}
diff --git a/readme.md b/readme.md
index 8d79869..a1e479d 100644
--- a/readme.md
+++ b/readme.md
@@ -1,6 +1,6 @@
# Hyperdrive Http
-Serve a [hyperdrive](https://github.com/mafintosh/hyperdrive) archive over HTTP. For an example of use, see [dat.haus](https://github.com/juliangruber/dat.haus).
+Serve a [hyperdrive](https://github.com/mafintosh/hyperdrive) drive over HTTP. For an example of use, see [dat.haus](https://github.com/juliangruber/dat.haus).
[](https://travis-ci.org/joehand/hyperdrive-http)
@@ -10,7 +10,7 @@ Hyperdrive-http returns a function to call when you receive a http request:
```js
var server = http.createServer().listen(8000)
-server.on('request', hyperdriveHttp(archive))
+server.on('request', hyperdriveHttp(drive))
```
Supports manifest options in `dat.json`:
@@ -23,12 +23,12 @@ Supports manifest options in `dat.json`:
To use hyperdrive-http you will need to:
* Create your own http server
-* Setup your hyperdrive archive
-* For remote archives, connect to the swarm
+* Setup your hyperdrive drive
+* For remote drives, connect to the swarm
## API
-Hyperdrive works with many archives/feeds or a single archive.
+Hyperdrive works with many drives/feeds or a single drive.
#### Options
@@ -37,19 +37,19 @@ Hyperdrive works with many archives/feeds or a single archive.
Hyperdrive-Key: de2a51bbaf8a5545eff82c999f15e1fd29637b3f16db94633cb6e2e0c324f833
Hyperdrive-Version: 4
```
-- `live` - If set to `true` will reload a directly listing if the archive receives updates.
-- `footer` - Add a footer to your HTML page. Automatically adds archive version number to footer.
+- `live` - If set to `true` will reload a directly listing if the drive receives updates.
+- `footer` - Add a footer to your HTML page. Automatically adds drive version number to footer.
### URL Format
Hyperdrive-http responds to any URL with a specific format. If the URL does cannot be parsed, it will return a 404.
-* Get archive listing: `http://archive-example.com/`
-* Get file from archive: `http://archive-example.com/filename.pdf`
+* Get drive listing: `http://drive-example.com/`
+* Get file from drive: `http://drive-example.com/filename.pdf`
-If a directory in the archive contains an `index.html` page that file is returned instead of the directory listing. If you'd like to view files use a query string:
+If a directory in the drive contains an `index.html` page that file is returned instead of the directory listing. If you'd like to view files use a query string:
-* View files: `http://archive-example.com/?viewSource=true`
+* View files: `http://drive-example.com/?viewSource=true`
## CLI
diff --git a/test/404.html b/test/404.html
deleted file mode 100644
index 5aecfca..0000000
--- a/test/404.html
+++ /dev/null
@@ -1 +0,0 @@
-File Not Found Page
\ No newline at end of file
diff --git a/test/basic.js b/test/basic.js
new file mode 100644
index 0000000..1509f2f
--- /dev/null
+++ b/test/basic.js
@@ -0,0 +1,29 @@
+const http = require('http')
+const request = require('request')
+const ram = require('random-access-memory')
+const hyperdrive = require('hyperdrive')
+const tape = require('tape')
+const hyperdriveHttp = require('..')
+
+const PORT = 8000
+const BASE = `http://localhost:${PORT}`
+tape.only('basic get', t => {
+ const server = http.createServer()
+ const drive = hyperdrive(ram)
+ const onrequest = hyperdriveHttp(drive)
+
+ server.listen(PORT)
+ server.once('request', onrequest)
+
+ drive.writeFile('hello', 'world', (err) => {
+ t.error(err, 'write ok')
+ request(BASE + '/hello', (err, res, body) => {
+ t.error(err, 'no request error')
+ t.equal(res.statusCode, 200)
+ t.ok(body, 'responds with file')
+ t.equal(body, 'world', 'content matches')
+ server.close()
+ t.end()
+ })
+ })
+})
diff --git a/test/dat.json b/test/dat.json
deleted file mode 100644
index 3188dd0..0000000
--- a/test/dat.json
+++ /dev/null
@@ -1,6 +0,0 @@
-{
- "title": "Application Title",
- "description": "A short description of the app",
- "fallback_page": "/public/404.html",
- "web_root": "/public"
-}
\ No newline at end of file
diff --git a/test/drive.js b/test/drive.js
deleted file mode 100644
index 390f22f..0000000
--- a/test/drive.js
+++ /dev/null
@@ -1,239 +0,0 @@
-var http = require('http')
-var fs = require('fs')
-var path = require('path')
-var test = require('tape')
-var memdb = require('memdb')
-var hyperdrive = require('hyperdrive')
-var request = require('request')
-var raf = require('random-access-file')
-var ndjson = require('ndjson')
-var hyperdriveHttp = require('..')
-var collect = require('collect-stream')
-var encoding = require('dat-encoding')
-
-var drive = hyperdrive(memdb())
-var archive1 = drive.createArchive({
- file: function (name) {
- return raf(path.join(__dirname, name))
- }
-})
-var archive2 = drive.createArchive({
- file: function (name) {
- return raf(path.join(__dirname, name))
- }
-})
-var archive3 = drive.createArchive()
-var archive4 = drive.createArchive()
-var server = http.createServer()
-var archives = {}
-archives[archive1.key.toString('hex')] = archive1
-archives[archive2.key.toString('hex')] = archive2
-archives[archive3.key.toString('hex')] = archive3
-archives[archive4.key.toString('hex')] = archive4
-
-test('setup', function (t) {
- server.listen(8000)
- server.once('listening', function () {
- archive1.append('feed.js', function () {
- archive1.append('drive.js', function () {
- archive2.append('drive.js', function () {
- archive4.append('dat.json', function () {
- archive4.append('404.html', function () {
- t.end()
- })
- })
- })
- })
- })
- })
-})
-
-test('Single Archive Metadata', function (t) {
- var onrequest = hyperdriveHttp(archive1)
- server.once('request', onrequest)
- request('http://localhost:8000', function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- var data = body.trim().split('\n')
- t.same(data.length, 2, 'Two files in metadata')
- t.same(JSON.parse(data[0]).name, 'feed.js', 'File name correct')
- t.same(res.headers['content-type'], 'application/json', 'JSON content-type header')
- t.end()
- }
- })
-})
-
-test('Single Archive GET File', function (t) {
- var onrequest = hyperdriveHttp(archive1)
- server.once('request', onrequest)
- request('http://localhost:8000/drive.js', function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.ok(body, 'Responds with file')
- fs.stat(path.join(__dirname, 'drive.js'), function (_, stat) {
- t.same(stat.size, body.length, 'File size correct')
- t.same(res.headers['content-type'], 'application/javascript', 'JS content-type header')
- t.end()
- })
- }
- })
-})
-
-test('Single Archive POST File', function (t) {
- var onrequest = hyperdriveHttp(archive3)
- server.once('request', onrequest)
- fs.createReadStream(path.join(__dirname, 'drive.js'))
- .pipe(request.post('http://localhost:8000', function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.equal(body.toString(), encoding.encode(archive3.key), 'Responds with key')
- collect(archive3.createFileReadStream('file'), function (err, body) {
- t.error(err, 'no hyperdrive error')
- t.same(body, fs.readFileSync(path.join(__dirname, 'drive.js')))
- t.end()
- })
- }
- }))
-})
-
-test('Single Archive Metadata Changes', function (t) {
- t.plan(4)
- var count = 0
- var onrequest = hyperdriveHttp(archive1)
- server.once('request', onrequest)
- request('http://localhost:8000/.changes')
- .on('response', function (res) {
- if (!res.statusCode) t.notOk('request failed')
- var timeoutInt = setInterval(function () {
- if (count === 2) {
- clearInterval(timeoutInt)
- res.socket.end()
- }
- }, 100)
- t.pass('receives response')
- t.same(res.headers['content-type'], 'application/json', 'JSON content-type header')
- })
- .pipe(ndjson.parse())
- .on('data', function (obj) {
- count++
- t.ok(obj, 'received file data')
- })
- .on('end', function () {
- if (count < 2) t.fail('response should not end early')
- })
-})
-
-test('Multiple Archives Metadata', function (t) {
- var onrequest = hyperdriveHttp(getArchive)
- server.once('request', onrequest)
- var reqUrl = 'http://localhost:8000/' + archive2.key.toString('hex')
- request(reqUrl, function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- var data = body.trim().split('\n')
- t.same(data.length, 1, 'One file in metadata')
- t.same(JSON.parse(data[0]).name, 'drive.js', 'File name correct')
- t.same(res.headers['content-type'], 'application/json', 'JSON content-type header')
- t.end()
- }
- })
-})
-
-test('Multiple Archives GET File', function (t) {
- var onrequest = hyperdriveHttp(getArchive)
- server.once('request', onrequest)
- var reqUrl = 'http://localhost:8000/' + archive2.key.toString('hex') + '/drive.js'
- request(reqUrl, function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.ok(body, 'Responds with file')
- fs.stat(path.join(__dirname, 'drive.js'), function (_, stat) {
- t.same(stat.size, body.length, 'File size correct')
- t.same(res.headers['content-type'], 'application/javascript', 'JS content-type header')
- t.end()
- })
- }
- })
-})
-
-test('Multiple Archive POST File', function (t) {
- var onrequest = hyperdriveHttp(getArchive)
- server.once('request', onrequest)
- fs.createReadStream(path.join(__dirname, 'drive.js'))
- .pipe(request.post('http://localhost:8000/', function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.equal(body.toString(), encoding.encode(archive3.key), 'Responds with key')
- collect(archive3.createFileReadStream('file'), function (err, body) {
- t.error(err, 'no hyperdrive error')
- t.same(body, fs.readFileSync(path.join(__dirname, 'drive.js')))
- t.end()
- })
- }
- }))
-})
-
-test('Multiple Archive POST File 2', function (t) {
- var onrequest = hyperdriveHttp(getArchive)
- server.once('request', onrequest)
- var reqUrl = 'http://localhost:8000/' + archive3.key.toString('hex')
- fs.createReadStream(path.join(__dirname, 'drive.js'))
- .pipe(request.post(reqUrl, function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.equal(body.toString(), encoding.encode(archive3.key), 'Responds with key')
- collect(archive3.createFileReadStream('file'), function (err, body) {
- t.error(err, 'no hyperdrive error')
- t.same(body, fs.readFileSync(path.join(__dirname, 'drive.js')))
- t.end()
- })
- }
- }))
-})
-
-test('Multiple Archive Metadata Changes', function (t) {
- t.plan(4)
- var count = 0
- var onrequest = hyperdriveHttp(archive1)
- server.once('request', onrequest)
- request('http://localhost:8000/.changes')
- .on('response', function (res) {
- if (!res.statusCode) t.notOk('request failed')
- var timeoutInt = setInterval(function () {
- if (count === 2) {
- clearInterval(timeoutInt)
- res.socket.end()
- }
- }, 100)
- t.pass('receives response')
- t.same(res.headers['content-type'], 'application/json', 'JSON content-type header')
- })
- .pipe(ndjson.parse())
- .on('data', function (obj) {
- count++
- t.ok(obj, 'received file data')
- })
- .on('end', function () {
- if (count < 2) t.fail('response should not end early')
- })
-})
-
-test('Single archive fallback_page Support', function (t) {
- var onrequest = hyperdriveHttp(archive4)
- server.once('request', onrequest)
- request('http://localhost:8000/fakepage.html', function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.same(body, 'File Not Found Page', '404 page content')
- t.end()
- }
- })
-})
-
-test.onFinish(function () {
- server.close()
-})
-
-function getArchive (info, cb) {
- cb(null, archives[info.key] || archive3)
-}
diff --git a/test/feed.js b/test/feed.js
deleted file mode 100644
index 8ce86b8..0000000
--- a/test/feed.js
+++ /dev/null
@@ -1,71 +0,0 @@
-var http = require('http')
-var test = require('tape')
-var memdb = require('memdb')
-var hypercore = require('hypercore')
-var request = require('request')
-var hyperdriveHttp = require('..')
-
-var core = hypercore(memdb())
-var feed1 = core.createFeed()
-var feed2 = core.createFeed()
-var server = http.createServer()
-var feeds = {}
-feeds[feed1.key.toString('hex')] = feed1
-feeds[feed2.key.toString('hex')] = feed2
-
-test('setup', function (t) {
- server.listen(8080)
- server.once('listening', function () {
- feed1.append('hello', function () {
- feed1.append('world', function () {
- t.end()
- })
- })
- var quote = {
- quote: 'Today you are you! That is truer than true! There is no one alive who is you-er than you!',
- source: 'Dr. Seuss'
- }
- feed2.append(JSON.stringify(quote))
- })
-})
-
-test('Single Feed Data', function (t) {
- var onrequest = hyperdriveHttp(feed1)
- server.once('request', onrequest)
- request('http://localhost:8080', function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.ok(body, 'received data')
- body = body.trim().split('\n')
- t.same(body[0], '"hello"', 'first chunk correct')
- t.same(body[1], '"world"', 'second chunk correct')
- t.end()
- } else {
- t.fail('bad response')
- t.end()
- }
- })
-})
-
-test('Multiple Feed Data', function (t) {
- var onrequest = hyperdriveHttp(getFeed)
- server.once('request', onrequest)
- request('http://localhost:8080/' + feed1.key.toString('hex'), function (err, res, body) {
- t.error(err, 'no request error')
- if (!err && res.statusCode === 200) {
- t.ok(body, 'received data')
- body = body.trim().split('\n')
- t.same(body[0], '"hello"', 'first chunk correct')
- t.same(body[1], '"world"', 'second chunk correct')
- t.end()
- }
- })
-})
-
-test.onFinish(function () {
- server.close()
-})
-
-function getFeed (info, cb) {
- cb(null, feeds[info.key])
-}
diff --git a/test/misc.js b/test/misc.js
deleted file mode 100644
index 5a87d01..0000000
--- a/test/misc.js
+++ /dev/null
@@ -1,83 +0,0 @@
-var http = require('http')
-var url = require('url')
-var test = require('tape')
-var request = require('request')
-var hyperdriveHttp = require('..')
-
-var server = http.createServer()
-
-test('setup', function (t) {
- server.listen(8001)
- server.once('listening', function () {
- t.end()
- })
-})
-
-test('GET Parsing no key', function (t) {
- t.plan(8)
- var count = 0
- var rootUrl = 'http://localhost:8001'
- var fileTests = [ // parsed filename should match these
- undefined,
- 'file.txt',
- 'dir/file.txt',
- 'dir/subdir/file.txt'
- ]
-
- var onrequest = hyperdriveHttp(function (info, cb) {
- t.same(info.key, null, 'key is null')
- t.same(info.filename, fileTests[count], 'file parse ok')
- count++
- if (count === fileTests.length) server.removeListener('request', onrequest)
- cb(null, 404)
- })
- server.on('request', onrequest)
- fileTests.forEach(function (filePath) {
- var getUrl = filePath ? url.resolve(rootUrl, filePath) : rootUrl
- request.get(getUrl).on('error', function () {
-
- })
- })
-})
-
-test('GET Parsing with key', function (t) {
- t.plan(8)
- var count = 0
- var rootUrl = 'http://localhost:8001'
- var fileTests = [ // parsed filename should match these
- '72072fab3d3f593453c1caed2b4b176b03af2f58ef725722c8937403997c03f8',
- '72072fab3d3f593453c1caed2b4b176b03af2f58ef725722c8937403997c03f8/file.txt',
- '72072fab3d3f593453c1caed2b4b176b03af2f58ef725722c8937403997c03f8/dir/file.txt',
- '72072fab3d3f593453c1caed2b4b176b03af2f58ef725722c8937403997c03f8/dir/subdir/file.txt'
- ]
-
- var onrequest = hyperdriveHttp(function (info, cb) {
- var segs = fileTests[count].split('/')
- t.same(info.key, segs.shift())
- t.same(info.filename, segs.join('/'), 'file parse ok')
- if (count === fileTests.length) server.removeListener('request', onrequest)
- cb(null, 404)
- })
- server.on('request', onrequest)
- next()
-
- function next () {
- testFile(fileTests[count], function () {
- if (count === fileTests.length) return
- next()
- })
- }
-
- function testFile (filePath, cb) {
- var getUrl = filePath ? url.resolve(rootUrl, filePath) : rootUrl
- request.get(getUrl)
- .on('response', function () {
- count++
- cb()
- })
- }
-})
-
-test.onFinish(function () {
- server.close()
-})