Skip to content

Commit d96e5a7

Browse files
committed
Add readme
1 parent ddeafd8 commit d96e5a7

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,56 @@ npm install random-access-http
88

99
[![Build Status](https://travis-ci.org/bcomnes/random-access-http.svg?branch=master)](https://travis-ci.org/bcomnes/random-access-http)
1010

11+
## Why?
12+
13+
Peers in a distributed system tend to come and go over a short period of time in many common p2p scenarios, especially when you are giving away a file without incentivizing the swarm to seed the file for a long time. There are also an abundance of free cloud hosts that let you host large files over http.
14+
15+
This module provides you random access to a file hosted over http so that it can be used by a client in a distributed system (such as hypercore or hyperdrive) to acquire parts of the file for itself and the other peers in the swarm.
16+
17+
## Usage
18+
19+
```js
20+
var randomAccessHttp = require('random-access-http')
21+
22+
var file = randomAccessHttp('http://example.com/somefile.mp4')
23+
24+
// Read 10 bytes at an offset of 5
25+
file.read(5, 10, function(err, buffer) {
26+
console.log(buffer)
27+
file.close(function() {
28+
console.log('http keepalive agents and sockets destroyed')
29+
})
30+
})
31+
```
32+
33+
file will use a keepalive agent to reduce the number http requests needed for the session. When you are done you should call `file.close()` to destroy the agent.
34+
35+
## API
36+
37+
#### `var file = randomAccessFile(url)`
38+
39+
Create a new 'file' that reads from the provided `url`. The `url` can be either `http` or `https`.
40+
41+
#### `file.write(offset, buffer, [callback])`
42+
43+
Not implemented! Please let me know if you have opinions on how to implement this.
44+
45+
#### `file.read(offset, length, callback)`
46+
47+
Read a buffer at a specific offset. Callback is called with the buffer read. Currently this will fail if the server returns byte ranges different than what is requested. PRs welcome to expand the flexibility of this method to allow for servers that return fat ranges.
48+
49+
#### `file.close([callback])`
50+
51+
Close outstanding http keepalive agent sockets.
52+
53+
#### `file.on('open')`
54+
55+
Emitted when the url has been checked to support range requests and the keep-alive agent has been created.
56+
57+
#### `file.on('close')`
58+
59+
Emitted after the keepalive agent and its associated sockets have been destroyed.
60+
1161
## See Also
1262

1363
- [bittorrent.org/beps/bep_0019 WebSeed - HTTP/FTP Seeding (GetRight style)](http://www.bittorrent.org/beps/bep_0019.html)

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
9999
if (res.statusCode !== 206) return cb(new Error('Bad response: ' + res.statusCode))
100100
var expectedRange = `bytes ${range}/${self.length}`
101101
if (res.headers['content-range'] !== expectedRange) return cb(new Error('Server returned unexpected range: ' + res.headers['content-range']))
102+
if (offset + length > self.length) return cb(new Error('Could not satisfy length'))
102103
var concatStream = concat(onBuf)
103-
var limiter = limitStream(length + 1)
104+
var limiter = limitStream(length + 1) // blow up if we get more data back than needed
104105

105106
pump(res, limiter, concatStream, function (err) {
106107
if (err) return cb(new Error(`problem while reading stream: ${err}`))

0 commit comments

Comments
 (0)