hypercore 10.0.0-alpha.10 → 10.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/index.js +5 -0
- package/lib/streams.js +39 -0
- package/package.json +1 -1
- package/test/streams.js +55 -0
package/README.md
CHANGED
|
@@ -98,6 +98,19 @@ Truncate the core to a smaller length.
|
|
|
98
98
|
Per default this will update the fork id of the core to `+ 1`, but you can set the fork id you prefer with the option.
|
|
99
99
|
Note that the fork id should be monotonely incrementing.
|
|
100
100
|
|
|
101
|
+
#### `const stream = core.createReadStream([options])`
|
|
102
|
+
|
|
103
|
+
Make a read stream. Options include:
|
|
104
|
+
|
|
105
|
+
``` js
|
|
106
|
+
{
|
|
107
|
+
start: 0,
|
|
108
|
+
end: core.length,
|
|
109
|
+
live: false,
|
|
110
|
+
snapshot: true // auto set end to core.length on open or update it on every read
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
101
114
|
#### `const range = core.download([range])`
|
|
102
115
|
|
|
103
116
|
Download a range of data.
|
package/index.js
CHANGED
|
@@ -14,6 +14,7 @@ const Replicator = require('./lib/replicator')
|
|
|
14
14
|
const Extensions = require('./lib/extensions')
|
|
15
15
|
const Core = require('./lib/core')
|
|
16
16
|
const BlockEncryption = require('./lib/block-encryption')
|
|
17
|
+
const { ReadStream } = require('./lib/streams')
|
|
17
18
|
|
|
18
19
|
const promises = Symbol.for('hypercore.promises')
|
|
19
20
|
const inspect = Symbol.for('nodejs.util.inspect.custom')
|
|
@@ -400,6 +401,10 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
400
401
|
return this._decode(encoding, block)
|
|
401
402
|
}
|
|
402
403
|
|
|
404
|
+
createReadStream (opts) {
|
|
405
|
+
return new ReadStream(this, opts)
|
|
406
|
+
}
|
|
407
|
+
|
|
403
408
|
download (range) {
|
|
404
409
|
const linear = !!(range && range.linear)
|
|
405
410
|
|
package/lib/streams.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const { Readable } = require('streamx')
|
|
2
|
+
|
|
3
|
+
class ReadStream extends Readable {
|
|
4
|
+
constructor (core, opts = {}) {
|
|
5
|
+
super()
|
|
6
|
+
|
|
7
|
+
this.core = core
|
|
8
|
+
this.start = opts.start || 0
|
|
9
|
+
this.end = typeof opts.end === 'number' ? opts.end : -1
|
|
10
|
+
this.snapshot = !opts.live && opts.snapshot !== false
|
|
11
|
+
this.live = !!opts.live
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
_open (cb) {
|
|
15
|
+
this._openP().then(cb, cb)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_read (cb) {
|
|
19
|
+
this._readP().then(cb, cb)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async _openP () {
|
|
23
|
+
if (this.end === -1) await this.core.update()
|
|
24
|
+
else await this.core.ready()
|
|
25
|
+
if (this.snapshot && this.end === -1) this.end = this.core.length
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async _readP () {
|
|
29
|
+
const end = this.live ? -1 : (this.end === -1 ? this.core.length : this.end)
|
|
30
|
+
if (end >= 0 && this.start >= end) {
|
|
31
|
+
this.push(null)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.push(await this.core.get(this.start++))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
exports.ReadStream = ReadStream
|
package/package.json
CHANGED
package/test/streams.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const test = require('brittle')
|
|
2
|
+
|
|
3
|
+
const { create } = require('./helpers')
|
|
4
|
+
|
|
5
|
+
test('basic read stream', async function (t) {
|
|
6
|
+
const core = await create()
|
|
7
|
+
|
|
8
|
+
const expected = [
|
|
9
|
+
'hello',
|
|
10
|
+
'world',
|
|
11
|
+
'verden',
|
|
12
|
+
'welt'
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
await core.append(expected)
|
|
16
|
+
|
|
17
|
+
for await (const data of core.createReadStream()) {
|
|
18
|
+
t.alike(data.toString(), expected.shift())
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
t.is(expected.length, 0)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('read stream with start / end', async function (t) {
|
|
25
|
+
const core = await create()
|
|
26
|
+
|
|
27
|
+
const datas = [
|
|
28
|
+
'hello',
|
|
29
|
+
'world',
|
|
30
|
+
'verden',
|
|
31
|
+
'welt'
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
await core.append(datas)
|
|
35
|
+
|
|
36
|
+
{
|
|
37
|
+
const expected = datas.slice(1)
|
|
38
|
+
|
|
39
|
+
for await (const data of core.createReadStream({ start: 1 })) {
|
|
40
|
+
t.alike(data.toString(), expected.shift())
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
t.is(expected.length, 0)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
const expected = datas.slice(2, 3)
|
|
48
|
+
|
|
49
|
+
for await (const data of core.createReadStream({ start: 2, end: 3 })) {
|
|
50
|
+
t.alike(data.toString(), expected.shift())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
t.is(expected.length, 0)
|
|
54
|
+
}
|
|
55
|
+
})
|