hypercore 9.12.0 → 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/.github/workflows/test-node.yml +3 -4
- package/README.md +131 -404
- package/__snapshots__/test/storage.js.snapshot.cjs +15 -0
- package/examples/announce.js +19 -0
- package/examples/basic.js +10 -0
- package/examples/http.js +123 -0
- package/examples/lookup.js +20 -0
- package/index.js +365 -1600
- package/lib/bitfield.js +113 -285
- package/lib/block-encryption.js +68 -0
- package/lib/block-store.js +58 -0
- package/lib/core.js +468 -0
- package/lib/extensions.js +76 -0
- package/lib/merkle-tree.js +1110 -0
- package/lib/messages.js +571 -0
- package/lib/mutex.js +39 -0
- package/lib/oplog.js +224 -0
- package/lib/protocol.js +525 -0
- package/lib/random-iterator.js +46 -0
- package/lib/remote-bitfield.js +24 -0
- package/lib/replicator.js +857 -0
- package/lib/streams.js +39 -0
- package/package.json +44 -45
- package/test/basic.js +59 -471
- package/test/bitfield.js +48 -133
- package/test/core.js +290 -0
- package/test/encodings.js +18 -0
- package/test/encryption.js +123 -0
- package/test/extension.js +71 -0
- package/test/helpers/index.js +23 -0
- package/test/merkle-tree.js +518 -0
- package/test/mutex.js +137 -0
- package/test/oplog.js +399 -0
- package/test/preload.js +72 -0
- package/test/replicate.js +227 -824
- package/test/sessions.js +173 -0
- package/test/storage.js +31 -0
- package/test/streams.js +39 -146
- package/test/user-data.js +47 -0
- package/bench/all.sh +0 -65
- package/bench/copy-64kb-blocks.js +0 -51
- package/bench/helpers/read-throttled.js +0 -27
- package/bench/helpers/read.js +0 -47
- package/bench/helpers/write.js +0 -29
- package/bench/read-16kb-blocks-proof-throttled.js +0 -1
- package/bench/read-16kb-blocks-proof.js +0 -1
- package/bench/read-16kb-blocks-throttled.js +0 -1
- package/bench/read-16kb-blocks.js +0 -1
- package/bench/read-512b-blocks.js +0 -1
- package/bench/read-64kb-blocks-linear-batch.js +0 -18
- package/bench/read-64kb-blocks-linear.js +0 -18
- package/bench/read-64kb-blocks-proof.js +0 -1
- package/bench/read-64kb-blocks.js +0 -1
- package/bench/replicate-16kb-blocks.js +0 -19
- package/bench/replicate-64kb-blocks.js +0 -19
- package/bench/write-16kb-blocks.js +0 -1
- package/bench/write-512b-blocks.js +0 -1
- package/bench/write-64kb-blocks-static.js +0 -1
- package/bench/write-64kb-blocks.js +0 -1
- package/example.js +0 -23
- package/lib/cache.js +0 -26
- package/lib/crypto.js +0 -5
- package/lib/replicate.js +0 -829
- package/lib/safe-buffer-equals.js +0 -6
- package/lib/storage.js +0 -421
- package/lib/tree-index.js +0 -183
- package/test/ack.js +0 -306
- package/test/audit.js +0 -36
- package/test/cache.js +0 -93
- package/test/compat.js +0 -209
- package/test/copy.js +0 -377
- package/test/default-storage.js +0 -51
- package/test/extensions.js +0 -137
- package/test/get.js +0 -64
- package/test/head.js +0 -65
- package/test/helpers/create-tracking-ram.js +0 -27
- package/test/helpers/create.js +0 -6
- package/test/helpers/replicate.js +0 -4
- package/test/seek.js +0 -234
- package/test/selections.js +0 -95
- package/test/set-uploading-downloading.js +0 -91
- package/test/stats.js +0 -77
- package/test/timeouts.js +0 -22
- package/test/tree-index.js +0 -841
- package/test/update.js +0 -156
- package/test/value-encoding.js +0 -52
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const Hypercore = require('../')
|
|
2
|
+
const Hyperswarm = require('hyperswarm')
|
|
3
|
+
|
|
4
|
+
const core = new Hypercore('./source')
|
|
5
|
+
|
|
6
|
+
start()
|
|
7
|
+
|
|
8
|
+
async function start () {
|
|
9
|
+
await core.ready()
|
|
10
|
+
while (core.length < 1000) {
|
|
11
|
+
await core.append('block #' + core.length)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const swarm = new Hyperswarm()
|
|
15
|
+
swarm.on('connection', socket => core.replicate(socket))
|
|
16
|
+
swarm.join(core.discoveryKey, { server: true, client: false })
|
|
17
|
+
|
|
18
|
+
console.log('Core:', core.key.toString('hex'))
|
|
19
|
+
}
|
package/examples/http.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
const Hypercore = require('../')
|
|
2
|
+
const streamx = require('streamx')
|
|
3
|
+
const replicator = require('@hyperswarm/replicator')
|
|
4
|
+
|
|
5
|
+
const core = new Hypercore('/tmp/movie')
|
|
6
|
+
|
|
7
|
+
if (process.argv[2] === 'bench') bench()
|
|
8
|
+
else if (process.argv[2]) importData()
|
|
9
|
+
else start()
|
|
10
|
+
|
|
11
|
+
class ByteStream extends streamx.Readable {
|
|
12
|
+
constructor (core, byteOffset, byteLength) {
|
|
13
|
+
super()
|
|
14
|
+
|
|
15
|
+
this.core = core
|
|
16
|
+
this.byteOffset = byteOffset
|
|
17
|
+
this.byteLength = byteLength
|
|
18
|
+
this.index = 0
|
|
19
|
+
this.range = null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async _read (cb) {
|
|
23
|
+
let data = null
|
|
24
|
+
|
|
25
|
+
if (!this.byteLength) {
|
|
26
|
+
this.push(null)
|
|
27
|
+
return cb(null)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (this.byteOffset > 0) {
|
|
31
|
+
const [block, byteOffset] = await core.seek(this.byteOffset)
|
|
32
|
+
this.byteOffset = 0
|
|
33
|
+
this.index = block + 1
|
|
34
|
+
this._select(this.index)
|
|
35
|
+
data = (await core.get(block)).slice(byteOffset)
|
|
36
|
+
} else {
|
|
37
|
+
this._select(this.index + 1)
|
|
38
|
+
data = await core.get(this.index++)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (data.length >= this.byteLength) {
|
|
42
|
+
data = data.slice(0, this.byteLength)
|
|
43
|
+
this.push(data)
|
|
44
|
+
this.push(null)
|
|
45
|
+
} else {
|
|
46
|
+
this.push(data)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.byteLength -= data.length
|
|
50
|
+
|
|
51
|
+
cb(null)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_select (index) {
|
|
55
|
+
if (this.range !== null) this.range.destroy(null)
|
|
56
|
+
this.range = this.core.download({ start: index, end: index + 32, linear: true })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_destroy (cb) {
|
|
60
|
+
if (this.range) this.range.destroy(null)
|
|
61
|
+
cb(null)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function bench () {
|
|
66
|
+
await core.ready()
|
|
67
|
+
|
|
68
|
+
console.time()
|
|
69
|
+
for (let i = 0; i < core.length; i++) {
|
|
70
|
+
await core.get(i)
|
|
71
|
+
}
|
|
72
|
+
console.timeEnd()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function start () {
|
|
76
|
+
const http = require('http')
|
|
77
|
+
const parse = require('range-parser')
|
|
78
|
+
|
|
79
|
+
await core.ready()
|
|
80
|
+
|
|
81
|
+
core.on('download', (index) => console.log('Downloaded block #' + index))
|
|
82
|
+
core.download({ start: 0, end: 1 })
|
|
83
|
+
|
|
84
|
+
// hack until we update the replicator
|
|
85
|
+
core.ready = (cb) => cb(null)
|
|
86
|
+
|
|
87
|
+
replicator(core, {
|
|
88
|
+
discoveryKey: require('crypto').createHash('sha256').update('http').digest(),
|
|
89
|
+
announce: true,
|
|
90
|
+
lookup: true
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
http.createServer(function (req, res) {
|
|
94
|
+
res.setHeader('Content-Type', 'video/x-matroska')
|
|
95
|
+
res.setHeader('Accept-Ranges', 'bytes')
|
|
96
|
+
|
|
97
|
+
let s
|
|
98
|
+
|
|
99
|
+
if (req.headers.range) {
|
|
100
|
+
const range = parse(core.byteLength, req.headers.range)[0]
|
|
101
|
+
const byteLength = range.end - range.start + 1
|
|
102
|
+
res.statusCode = 206
|
|
103
|
+
res.setHeader('Content-Range', 'bytes ' + range.start + '-' + range.end + '/' + core.byteLength)
|
|
104
|
+
s = new ByteStream(core, range.start, byteLength)
|
|
105
|
+
} else {
|
|
106
|
+
s = new ByteStream(core, 0, core.byteLength)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
res.setHeader('Content-Length', s.byteLength)
|
|
110
|
+
s.pipe(res, () => {})
|
|
111
|
+
}).listen(10101)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function importData () {
|
|
115
|
+
const fs = require('fs')
|
|
116
|
+
const rs = fs.createReadStream(process.argv[2])
|
|
117
|
+
|
|
118
|
+
for await (const data of rs) {
|
|
119
|
+
await core.append(data)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log('done!', core)
|
|
123
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const Hypercore = require('../')
|
|
2
|
+
const Hyperswarm = require('hyperswarm')
|
|
3
|
+
|
|
4
|
+
const core = new Hypercore('./clone', process.argv[2])
|
|
5
|
+
|
|
6
|
+
start()
|
|
7
|
+
|
|
8
|
+
async function start () {
|
|
9
|
+
await core.ready()
|
|
10
|
+
|
|
11
|
+
const swarm = new Hyperswarm()
|
|
12
|
+
swarm.on('connection', socket => core.replicate(socket))
|
|
13
|
+
swarm.join(core.discoveryKey, { server: false, client: true })
|
|
14
|
+
|
|
15
|
+
console.log((await core.get(42)).toString())
|
|
16
|
+
console.log((await core.get(142)).toString())
|
|
17
|
+
console.log((await core.get(511)).toString())
|
|
18
|
+
console.log((await core.get(512)).toString())
|
|
19
|
+
console.log((await core.get(513)).toString())
|
|
20
|
+
}
|