Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hyperdrive-http

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hyperdrive-http - npm Package Compare versions

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'
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc