Skip to content

Commit b366bfe

Browse files
committed
format
1 parent b0a1090 commit b366bfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2294
-1701
lines changed

README.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Built for sharing large datasets and streams of real time data
88

99
## Features
1010

11-
* **Sparse replication.** Only download the data you are interested in.
12-
* **Realtime.** Get the latest updates to the log fast and securely.
13-
* **Performant.** Uses a simple flat file structure to maximize I/O performance.
14-
* **Secure.** Uses signed merkle trees to verify log integrity in real time.
15-
* **Modular.** Hypercore aims to do one thing and one thing well - distributing a stream of data.
11+
- **Sparse replication.** Only download the data you are interested in.
12+
- **Realtime.** Get the latest updates to the log fast and securely.
13+
- **Performant.** Uses a simple flat file structure to maximize I/O performance.
14+
- **Secure.** Uses signed merkle trees to verify log integrity in real time.
15+
- **Modular.** Hypercore aims to do one thing and one thing well - distributing a stream of data.
1616

1717
Note that the latest release is Hypercore 10, which adds support for truncate and many other things.
1818
Version 10 is not compatible with earlier versions (9 and earlier), but is considered LTS, meaning the storage format and wire protocol is forward compatible with future versions.
@@ -31,7 +31,7 @@ Make a new Hypercore instance.
3131

3232
`storage` should be set to a directory where you want to store the data and core metadata.
3333

34-
``` js
34+
```js
3535
const core = new Hypercore('./directory') // store data in ./directory
3636
```
3737

@@ -41,7 +41,7 @@ Alternatively you can pass a [Hypercore Storage](https://github.com/holepunchto/
4141

4242
`options` include:
4343

44-
``` js
44+
```js
4545
{
4646
createIfMissing: true, // create a new Hypercore key pair if none was present in storage
4747
overwrite: false, // overwrite any old Hypercore that might already exist
@@ -114,7 +114,7 @@ Signers are an array of objects with the following structure:
114114
Append a block of data (or an array of blocks) to the core.
115115
Returns the new length and byte length of the core.
116116

117-
``` js
117+
```js
118118
// simple call append with a new block of data
119119
await core.append(Buffer.from('I am a block of data'))
120120

@@ -138,7 +138,7 @@ await core.append([Buffer.from('batch block 1'), Buffer.from('batch block 2')])
138138
Get a block of data.
139139
If the data is not available locally this method will prioritize and wait for the data to be downloaded.
140140

141-
``` js
141+
```js
142142
// get block #42
143143
const block = await core.get(42)
144144

@@ -151,7 +151,7 @@ const blockLocal = await core.get(44, { wait: false })
151151

152152
`options` include:
153153

154-
``` js
154+
```js
155155
{
156156
wait: true, // wait for block to be downloaded
157157
onwait: () => {}, // hook that is called if the get is waiting for download
@@ -171,15 +171,15 @@ Check if the core has all blocks between `start` and `end`.
171171

172172
Waits for initial proof of the new core length until all `findingPeers` calls has finished.
173173

174-
``` js
174+
```js
175175
const updated = await core.update()
176176

177177
console.log('core was updated?', updated, 'length is', core.length)
178178
```
179179

180180
`options` include:
181181

182-
``` js
182+
```js
183183
{
184184
wait: false,
185185
activeRequests: undefined, // Advanced option. Pass requests for replicating blocks
@@ -196,15 +196,15 @@ Seek to a byte offset.
196196
Returns `[index, relativeOffset]`, where `index` is the data block the `byteOffset` is contained in and `relativeOffset` is
197197
the relative byte offset in the data block.
198198

199-
``` js
199+
```js
200200
await core.append([Buffer.from('abc'), Buffer.from('d'), Buffer.from('efg')])
201201

202202
const first = await core.seek(1) // returns [0, 1]
203203
const second = await core.seek(3) // returns [1, 0]
204204
const third = await core.seek(5) // returns [2, 1]
205205
```
206206

207-
``` js
207+
```js
208208
{
209209
wait: true, // wait for data to be downloaded
210210
timeout: 0, // wait at max some milliseconds (0 means no timeout)
@@ -216,7 +216,7 @@ const third = await core.seek(5) // returns [2, 1]
216216

217217
Make a read stream to read a range of data out at once.
218218

219-
``` js
219+
```js
220220
// read the full core
221221
const fullStream = core.createReadStream()
222222

@@ -233,7 +233,7 @@ for await (const data of fullStream) {
233233

234234
`options` include:
235235

236-
``` js
236+
```js
237237
{
238238
start: 0,
239239
end: core.length,
@@ -248,7 +248,7 @@ for await (const data of fullStream) {
248248

249249
Make a byte stream to read a range of bytes.
250250

251-
``` js
251+
```js
252252
// Read the full core
253253
const fullStream = core.createByteStream()
254254

@@ -266,7 +266,7 @@ partialStream.pipe(process.stdout)
266266

267267
`options` include:
268268

269-
``` js
269+
```js
270270
{
271271
byteOffset: 0, // Offset where to start from
272272
byteLength: core.byteLength - options.byteOffset, // How many bytes to read
@@ -278,11 +278,11 @@ partialStream.pipe(process.stdout)
278278

279279
Make a write stream to append chunks as blocks.
280280

281-
``` js
281+
```js
282282
const ws = core.createWriteStream()
283283

284284
// Listen for stream finishing
285-
const done = new Promise(resolve => ws.on('finish', resolve))
285+
const done = new Promise((resolve) => ws.on('finish', resolve))
286286

287287
for (const data of ['hello', 'world']) ws.write(data)
288288
ws.end()
@@ -297,14 +297,15 @@ console.log(await core.get(core.length - 1)) // 'world'
297297

298298
Clear stored blocks between `start` and `end`, reclaiming storage when possible.
299299

300-
``` js
300+
```js
301301
await core.clear(4) // clear block 4 from your local cache
302302
await core.clear(0, 10) // clear block 0-10 from your local cache
303303
```
304304

305305
The core will also gossip to peers it is connected to, that is no longer has these blocks.
306306

307307
`options` include:
308+
308309
```js
309310
{
310311
diff: false // Returned `cleared` bytes object is null unless you enable this
@@ -319,6 +320,7 @@ Per default this will update the fork id of the core to `+ 1`, but you can set t
319320
Note that the fork id should be monotonely incrementing.
320321

321322
`options` include:
323+
322324
```js
323325
{
324326
fork: core.fork + 1, // The new fork id after truncating
@@ -372,7 +374,7 @@ await range.done()
372374

373375
A range can have the following properties:
374376

375-
``` js
377+
```js
376378
{
377379
start: startIndex,
378380
end: nonInclusiveEndIndex,
@@ -384,7 +386,7 @@ A range can have the following properties:
384386

385387
To download the full core continuously (often referred to as non sparse mode) do
386388

387-
``` js
389+
```js
388390
// Note that this will never be considered downloaded as the range
389391
// will keep waiting for new blocks to be appended.
390392
core.download({ start: 0, end: -1 })
@@ -398,7 +400,7 @@ core.download({ blocks: [4, 9, 7] })
398400

399401
To cancel downloading a range simply destroy the range instance.
400402

401-
``` js
403+
```js
402404
// will stop downloading now
403405
range.destroy()
404406
```
@@ -504,10 +506,10 @@ Info {
504506
fork: 0,
505507
padding: 8,
506508
storage: {
507-
oplog: 8192,
508-
tree: 4096,
509-
blocks: 4096,
510-
bitfield: 4096
509+
oplog: 8192,
510+
tree: 4096,
511+
blocks: 4096,
512+
bitfield: 4096
511513
}
512514
}
513515
```
@@ -660,7 +662,7 @@ If you are using a P2P swarm like [Hyperswarm](https://github.com/hyperswarm/hyp
660662
If you want to multiplex the replication over an existing Hypercore replication stream you can pass
661663
another stream instance instead of the `isInitiator` boolean.
662664

663-
``` js
665+
```js
664666
// assuming we have two cores, localCore + remoteCore, sharing the same key
665667
// on a server
666668
const net = require('net')

examples/announce.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const core = new Hypercore('./source')
55

66
start()
77

8-
async function start () {
8+
async function start() {
99
await core.ready()
1010
while (core.length < 1000) {
1111
await core.append('block #' + core.length)
1212
}
1313

1414
const swarm = new Hyperswarm()
15-
swarm.on('connection', socket => core.replicate(socket))
15+
swarm.on('connection', (socket) => core.replicate(socket))
1616
swarm.join(core.discoveryKey, { server: true, client: false })
1717

1818
console.log('Core:', core.key.toString('hex'))

examples/basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const Hypercore = require('../')
22

33
start()
44

5-
async function start () {
5+
async function start() {
66
const core = new Hypercore('/tmp/basic')
77
await core.append(['Hello', 'World'])
88
console.log(core)

examples/http.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ const rangeParser = require('range-parser')
77
// Later replicate so other peers can also watch it: node http.js
88
// Other peers: node http.js <core key>
99

10-
const key = process.argv[2] && process.argv[2] !== 'import' ? Buffer.from(process.argv[2], 'hex') : null
10+
const key =
11+
process.argv[2] && process.argv[2] !== 'import' ? Buffer.from(process.argv[2], 'hex') : null
1112
const core = new Hypercore('/tmp/movie' + (key ? '-peer' : ''), key)
1213

1314
if (process.argv[2] === 'import') importData(process.argv[3])
1415
else start()
1516

16-
async function start () {
17+
async function start() {
1718
await core.ready()
1819
if (core.writable) console.log('Share this core key:', core.key.toString('hex'))
1920

@@ -33,46 +34,51 @@ async function start () {
3334
await core.update()
3435
}
3536

36-
http.createServer(function (req, res) {
37-
res.setHeader('Content-Type', 'video/mp4')
38-
res.setHeader('Accept-Ranges', 'bytes')
37+
http
38+
.createServer(function (req, res) {
39+
res.setHeader('Content-Type', 'video/mp4')
40+
res.setHeader('Accept-Ranges', 'bytes')
3941

40-
let byteOffset = 0
41-
let byteLength = core.byteLength
42+
let byteOffset = 0
43+
let byteLength = core.byteLength
4244

43-
if (req.headers.range) {
44-
const ranges = rangeParser(core.byteLength, req.headers.range)
45+
if (req.headers.range) {
46+
const ranges = rangeParser(core.byteLength, req.headers.range)
4547

46-
if (ranges === -1 || ranges === -2) {
47-
res.statusCode = 206
48-
res.setHeader('Content-Length', 0)
49-
res.end()
50-
return
51-
}
48+
if (ranges === -1 || ranges === -2) {
49+
res.statusCode = 206
50+
res.setHeader('Content-Length', 0)
51+
res.end()
52+
return
53+
}
5254

53-
const range = ranges[0]
54-
byteOffset = range.start
55-
byteLength = range.end - range.start + 1
55+
const range = ranges[0]
56+
byteOffset = range.start
57+
byteLength = range.end - range.start + 1
5658

57-
res.statusCode = 206
58-
res.setHeader('Content-Range', 'bytes ' + range.start + '-' + range.end + '/' + core.byteLength)
59-
}
59+
res.statusCode = 206
60+
res.setHeader(
61+
'Content-Range',
62+
'bytes ' + range.start + '-' + range.end + '/' + core.byteLength
63+
)
64+
}
6065

61-
res.setHeader('Content-Length', byteLength)
66+
res.setHeader('Content-Length', byteLength)
6267

63-
if (req.method === 'HEAD') {
64-
res.end()
65-
return
66-
}
68+
if (req.method === 'HEAD') {
69+
res.end()
70+
return
71+
}
6772

68-
const bs = core.createByteStream({ byteOffset, byteLength })
69-
bs.pipe(res, noop)
70-
}).listen(function () {
71-
console.log('HTTP server on http://localhost:' + this.address().port)
72-
})
73+
const bs = core.createByteStream({ byteOffset, byteLength })
74+
bs.pipe(res, noop)
75+
})
76+
.listen(function () {
77+
console.log('HTTP server on http://localhost:' + this.address().port)
78+
})
7379
}
7480

75-
async function importData (filename) {
81+
async function importData(filename) {
7682
const fs = require('fs')
7783
const rs = fs.createReadStream(filename)
7884

@@ -83,4 +89,4 @@ async function importData (filename) {
8389
console.log('done!', core)
8490
}
8591

86-
function noop () {}
92+
function noop() {}

examples/lookup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const core = new Hypercore('./clone', process.argv[2])
55

66
start()
77

8-
async function start () {
8+
async function start() {
99
await core.ready()
1010

1111
const swarm = new Hyperswarm()
12-
swarm.on('connection', socket => core.replicate(socket))
12+
swarm.on('connection', (socket) => core.replicate(socket))
1313
swarm.join(core.discoveryKey, { server: false, client: true })
1414

1515
console.log((await core.get(42)).toString())

0 commit comments

Comments
 (0)