You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
0 commit comments