hyperdrive-http
Advanced tools
Comparing version 3.0.0 to 3.1.1
31
index.js
@@ -6,3 +6,4 @@ var pump = require('pump') | ||
var rangeParser = require('range-parser') | ||
var JSONStream = require('JSONStream') | ||
var ndjson = require('ndjson') | ||
var encoding = require('dat-encoding') | ||
@@ -32,10 +33,23 @@ module.exports = HyperdriveHttp | ||
function parse (url) { | ||
var key = url.slice(1, 65) | ||
var filename = url.slice(66) | ||
if (!/^[0-9a-f]{64}$/.test(key) && !singleArchive) return null | ||
else if (singleArchive) filename = url.slice(1) | ||
var segs = url.split('/').filter(Boolean) | ||
var key = segs[0] | ||
var filename = segs[1] | ||
var op = 'get' | ||
if (/\.changes$/.test(key)) { | ||
key = key.slice(0, -8) | ||
op = 'changes' | ||
} | ||
try { | ||
encoding.decode(key) | ||
} catch (_) { | ||
if (!singleArchive) return null | ||
} | ||
if (singleArchive) filename = url.slice(1) | ||
return { | ||
key: key, | ||
filename: filename | ||
filename: filename, | ||
op: op | ||
} | ||
@@ -49,3 +63,3 @@ } | ||
if (!datUrl.filename) { | ||
var src = archive.list({live: false}) | ||
var src = archive.list({live: datUrl.op === 'changes'}) | ||
var timeout = TimeoutStream({ | ||
@@ -58,4 +72,3 @@ objectMode: true, | ||
}) | ||
var stringify = JSONStream.stringify('[', ',', ']\n', 2) | ||
pump(src, timeout, stringify, res) | ||
pump(src, timeout, ndjson.serialize(), res) | ||
} | ||
@@ -62,0 +75,0 @@ |
{ | ||
"name": "hyperdrive-http", | ||
"version": "3.0.0", | ||
"version": "3.1.1", | ||
"description": "Handle Hyperdrive HTTP Requests", | ||
@@ -9,8 +9,13 @@ "main": "index.js", | ||
}, | ||
"author": "Joe Hand <joe@joeahand.com> (http://joeahand.com/)", | ||
"license": "ISC", | ||
"keywords": [ | ||
"hyperdrive", | ||
"dat" | ||
], | ||
"author": "Joe Hand <joe@hand.email>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"JSONStream": "^1.1.2", | ||
"callback-timeout": "^2.1.0", | ||
"dat-encoding": "^2.0.2", | ||
"mime": "^1.3.4", | ||
"ndjson": "^1.4.3", | ||
"pump": "^1.0.1", | ||
@@ -17,0 +22,0 @@ "range-parser": "^1.2.0", |
# Hyperdrive Http | ||
Handle Hyperdrive HTTP Requests | ||
Serve a [hyperdrive](https://github.com/mafintosh/hyperdrive) archive files & metadata over HTTP. For an example of use, see [dat.haus](https://github.com/juliangruber/dat.haus). | ||
Expects you to: | ||
* Bring your own http server | ||
* Manage your own hyperdrive archives | ||
* Connect to the swarm before callback | ||
## Usage | ||
Hyperdrive-http returns a function to call when you receive a http request: | ||
```js | ||
var server = http.createServer() | ||
server.on('request', hyperdriveHttp(archive)) | ||
``` | ||
### URL Format | ||
Hyperdrive-http responds to any URL with a specific format. If the URL does cannot be parsed, it will return a 404. | ||
#### Multiple archives on one site | ||
* Get metadata for archive: `http://dat.haus/dat.haus/c5dbfe5521d8dddba683544ee4b1c7f6ce1c7b23bd387bd850397e4aaf9afbd9/` | ||
* Get file from archive: `http://dat.haus/dat.haus/c5dbfe5521d8dddba683544ee4b1c7f6ce1c7b23bd387bd850397e4aaf9afbd9/filename.pdf` | ||
#### Single Archive Mode | ||
* Get metadata for archive: `http://archive-example.com/` | ||
* Get file from archive: `http://archive-example.com/filename.pdf` | ||
### Setup | ||
To use hyperdrive-http you will need to: | ||
* Create your own http server | ||
* Setup your hyperdrive archive(s) | ||
* Connect to the swarm before serving archive | ||
### API | ||
Hyperdrive works with either a archive lookup function or a single archive: | ||
Initiate with an archive lookup function: | ||
@@ -18,2 +45,25 @@ `var onrequest = hyperdriveHttp(getArchive)` | ||
The archive lookup function would look like this: | ||
```js | ||
var getArchive = function (datInfo, cb) { | ||
// datInfo = { | ||
// key: archive.key, | ||
// filename: filename.txt // If file is requested in URL | ||
// op: 'get' or 'changes' | ||
// } | ||
// Find the archive to return: | ||
var archive = cache.get(datInfo.key) | ||
if (!archive) { | ||
archive = drive.createArchive(datInfo.key) | ||
// Make sure you join the swarm before callback | ||
sw.join(archive.discoveryKey) | ||
} | ||
cb(null, archive) // callback with your found archive | ||
} | ||
``` | ||
## Example | ||
```javascript | ||
@@ -45,4 +95,5 @@ var hyperdriveHttp = require('hyperdrive-http') | ||
key: archive.key, | ||
filename: someFile.txt | ||
filename: someFile.txt, | ||
op: 'get' // or 'changes' | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7094
84
97
7
+ Addeddat-encoding@^2.0.2
+ Addedndjson@^1.4.3
+ Addedbase-x@1.1.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddat-encoding@2.1.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedndjson@1.5.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsplit2@2.2.0(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
- RemovedJSONStream@^1.1.2
- RemovedJSONStream@1.3.5(transitive)
- Removedjsonparse@1.3.1(transitive)
- Removedthrough@2.3.8(transitive)