@peardrive/hyperblobs 2.9.0-pruned.1
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/LICENSE +201 -0
- package/README.md +112 -0
- package/index.js +241 -0
- package/lib/block-map.js +157 -0
- package/lib/monitor.js +92 -0
- package/lib/prefetcher.js +67 -0
- package/lib/streams.js +343 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# hyperblobs (Pruned Edition)
|
|
2
|
+
|
|
3
|
+
> **This is a fork of [holepunchto/hyperblobs](https://github.com/holepunchto/hyperblobs) for the PearDrive pruned stack.**
|
|
4
|
+
|
|
5
|
+
Part of the pruned hypercore stack:
|
|
6
|
+
- `@peardrive/hypercore` - Core with `onBlockMissing` hook
|
|
7
|
+
- `@peardrive/hyperblobs` - This package
|
|
8
|
+
- `@peardrive/hyperdrive` - Drive with `pruned` mode
|
|
9
|
+
|
|
10
|
+
The `onBlockMissing` hook from `@peardrive/hypercore` flows through automatically.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
A simple blob store for Hypercore.
|
|
15
|
+
|
|
16
|
+
Each blob is identified by its unique bounds within the Hypercore, e.g. `{ byteOffset: 0, blockOffset: 0, blockLength: 5, byteLength: 327680 }`, which makes them easy to save and retrieve:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const blobs = new Hyperblobs(core)
|
|
20
|
+
// ID is { byteOffset: 0, blockOffset: 0, blockLength: 1, byteLength: 11 }
|
|
21
|
+
const id = await blobs.put(Buffer.from('hello world', 'utf-8'))
|
|
22
|
+
await blobs.get(id) // Buffer.from('hello world', 'utf-8')
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You can also get from start/end bounds within a single blob:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const blobs = new Hyperblobs(core)
|
|
29
|
+
// ID is { byteOffset: 0, blockOffset: 0, blockLength: 1, byteLength: 11 }
|
|
30
|
+
const id = await blobs.put(Buffer.from('hello world', 'utf-8'))
|
|
31
|
+
await blobs.get(id, { start: 1, length: 2 }) // Buffer.from('el', 'utf-8')
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
If the blob is large, there's a Streams interface (`createReadStream` and `createWriteStream`) too.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
npm i hyperblobs
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
const Hyperblobs = require('hyperblobs')
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### `const blobs = new Hyperblobs(core, opts)`
|
|
49
|
+
|
|
50
|
+
Create a new blob store wrapping a single Hypercore.
|
|
51
|
+
|
|
52
|
+
Options can include:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
{
|
|
56
|
+
blockSize: 64KB // The block size that will be used when storing large blobs.
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### `const id = await blobs.put(blob, opts)`
|
|
61
|
+
|
|
62
|
+
Store a new blob. If the blob is large, it will be chunked according to `opts.blockSize` (default 64KB).
|
|
63
|
+
|
|
64
|
+
Options can include:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
{
|
|
68
|
+
blockSize: 64KB, // The block size that will be used when storing large blobs.
|
|
69
|
+
start: 0, // Relative offset to start within the blob
|
|
70
|
+
end: blob.length - 1, // End offset within the blob (inclusive)
|
|
71
|
+
length: blob.length, // Number of bytes to read.
|
|
72
|
+
core // A custom core to write (overrides the default core)
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### `const content = await blobs.get(id, opts)`
|
|
77
|
+
|
|
78
|
+
Return a complete blob as a `Buffer`.
|
|
79
|
+
|
|
80
|
+
`id` is the value returned by `put`
|
|
81
|
+
|
|
82
|
+
Options can include:
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
{
|
|
86
|
+
core, // A custom core to read from (overrides the default core)
|
|
87
|
+
wait: true, // Wait for block to be downloaded
|
|
88
|
+
timeout: 0 // Wait at max some milliseconds (0 means no timeout)
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### `await blobs.clear(id, opts)`
|
|
93
|
+
|
|
94
|
+
Remove a blob from the core.
|
|
95
|
+
|
|
96
|
+
`opts` are the same as `Hypercore.clear` method.
|
|
97
|
+
|
|
98
|
+
#### `const stream = blobs.createReadStream(id, opts)`
|
|
99
|
+
|
|
100
|
+
Create a Readable stream that will yield the `id` blob.
|
|
101
|
+
|
|
102
|
+
Options match the `get` options.
|
|
103
|
+
|
|
104
|
+
#### `const stream = blobs.createWriteStream(opts)`
|
|
105
|
+
|
|
106
|
+
Create a Writable stream that will save a blob.
|
|
107
|
+
|
|
108
|
+
The corresponding ID will be set on the stream at `stream.id`.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
Apache-2.0
|
package/index.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
const mutexify = require('mutexify')
|
|
2
|
+
const b4a = require('b4a')
|
|
3
|
+
|
|
4
|
+
const { BlockMapReadStream, BlobReadStream, BlobWriteStream } = require('./lib/streams')
|
|
5
|
+
const Monitor = require('./lib/monitor')
|
|
6
|
+
const blockMap = require('./lib/block-map')
|
|
7
|
+
|
|
8
|
+
const DEFAULT_BLOCK_SIZE = 2 ** 16
|
|
9
|
+
|
|
10
|
+
class HyperBlobsBatch {
|
|
11
|
+
constructor(blobs) {
|
|
12
|
+
this.blobs = blobs
|
|
13
|
+
this.blocks = []
|
|
14
|
+
this.bytes = 0
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
ready() {
|
|
18
|
+
return this.blobs.ready()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async put(buffer) {
|
|
22
|
+
if (!this.blobs.core.opened) await this.blobs.core.ready()
|
|
23
|
+
|
|
24
|
+
const blockSize = this.blobs.blockSize
|
|
25
|
+
const result = {
|
|
26
|
+
blockOffset: this.blobs.core.length + this.blocks.length,
|
|
27
|
+
blockLength: 0,
|
|
28
|
+
byteOffset: this.blobs.core.byteLength + this.bytes,
|
|
29
|
+
byteLength: 0
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let offset = 0
|
|
33
|
+
while (offset < buffer.byteLength) {
|
|
34
|
+
const blk = buffer.subarray(offset, offset + blockSize)
|
|
35
|
+
offset += blockSize
|
|
36
|
+
|
|
37
|
+
result.blockLength++
|
|
38
|
+
result.byteLength += blk.byteLength
|
|
39
|
+
this.bytes += blk.byteLength
|
|
40
|
+
this.blocks.push(blk)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async get(id) {
|
|
47
|
+
if (id.blockOffset < this.blobs.core.length) {
|
|
48
|
+
return this.blobs.get(id)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const bufs = []
|
|
52
|
+
|
|
53
|
+
for (
|
|
54
|
+
let i = id.blockOffset - this.blobs.core.length;
|
|
55
|
+
i < id.blockOffset + id.blockLength;
|
|
56
|
+
i++
|
|
57
|
+
) {
|
|
58
|
+
if (i >= this.blocks.length) return null
|
|
59
|
+
bufs.push(this.blocks[i])
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return bufs.length === 1 ? bufs[0] : b4a.concat(bufs)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async flush() {
|
|
66
|
+
await this.blobs.core.append(this.blocks)
|
|
67
|
+
this.blocks = []
|
|
68
|
+
this.bytes = 0
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
close() {
|
|
72
|
+
// noop, atm nothing to unlink
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class Hyperblobs {
|
|
77
|
+
constructor(core, opts = {}) {
|
|
78
|
+
this.core = core
|
|
79
|
+
this.blockSize = opts.blockSize || DEFAULT_BLOCK_SIZE
|
|
80
|
+
|
|
81
|
+
this._lock = mutexify()
|
|
82
|
+
this._monitors = new Set()
|
|
83
|
+
|
|
84
|
+
this._boundUpdatePeers = this._updatePeers.bind(this)
|
|
85
|
+
this._boundOnUpload = this._onUpload.bind(this)
|
|
86
|
+
this._boundOnDownload = this._onDownload.bind(this)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get key() {
|
|
90
|
+
return this.core.key
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get discoveryKey() {
|
|
94
|
+
return this.core.discoveryKey
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get feed() {
|
|
98
|
+
return this.core
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
get locked() {
|
|
102
|
+
return this._lock.locked
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
replicate(isInitiator, opts) {
|
|
106
|
+
return this.core.replicate(isInitiator, opts)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
ready() {
|
|
110
|
+
return this.core.ready()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
close() {
|
|
114
|
+
return this.core.close()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
batch() {
|
|
118
|
+
return new HyperBlobsBatch(this)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
snapshot() {
|
|
122
|
+
return new Hyperblobs(this.core.snapshot())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async put(blob, opts) {
|
|
126
|
+
if (!b4a.isBuffer(blob)) blob = b4a.from(blob)
|
|
127
|
+
const blockSize = (opts && opts.blockSize) || this.blockSize
|
|
128
|
+
|
|
129
|
+
const stream = this.createWriteStream(opts)
|
|
130
|
+
for (let i = 0; i < blob.length; i += blockSize) {
|
|
131
|
+
stream.write(blob.subarray(i, i + blockSize))
|
|
132
|
+
}
|
|
133
|
+
stream.end()
|
|
134
|
+
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
stream.once('error', reject)
|
|
137
|
+
stream.once('close', () => resolve(stream.id))
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async _getAll(id, opts) {
|
|
142
|
+
if (id.blockLength === 1) return this.core.get(id.blockOffset, opts)
|
|
143
|
+
|
|
144
|
+
const promises = new Array(id.blockLength)
|
|
145
|
+
for (let i = 0; i < id.blockLength; i++) {
|
|
146
|
+
promises[i] = this.core.get(id.blockOffset + i, opts)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const blocks = await Promise.all(promises)
|
|
150
|
+
for (let i = 0; i < id.blockLength; i++) {
|
|
151
|
+
if (blocks[i] === null) return null
|
|
152
|
+
}
|
|
153
|
+
return b4a.concat(blocks)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async get(id, opts) {
|
|
157
|
+
if (isAll(id, opts)) return this._getAll(id, opts)
|
|
158
|
+
|
|
159
|
+
const res = []
|
|
160
|
+
try {
|
|
161
|
+
for await (const block of this.createReadStream(id, opts)) {
|
|
162
|
+
res.push(block)
|
|
163
|
+
}
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error.code === 'BLOCK_NOT_AVAILABLE') return null
|
|
166
|
+
throw error
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (res.length === 1) return res[0]
|
|
170
|
+
return b4a.concat(res)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async clear(id, opts) {
|
|
174
|
+
if (id.blockMap) {
|
|
175
|
+
const map = await blockMap.get(this.core, id, { wait: false })
|
|
176
|
+
if (map) {
|
|
177
|
+
for (const b of map.blocks) await this.core.clear(b.index, b.index + 1, opts)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return this.core.clear(id.blockOffset, id.blockOffset + id.blockLength, opts)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
createReadStream(id, opts) {
|
|
184
|
+
const core = opts && opts.core ? opts.core : this.core
|
|
185
|
+
return id.blockMap ? new BlockMapReadStream(core, id, opts) : new BlobReadStream(core, id, opts)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
createWriteStream(opts) {
|
|
189
|
+
const core = opts && opts.core ? opts.core : this.core
|
|
190
|
+
return new BlobWriteStream(core, this._lock, opts)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
monitor(id) {
|
|
194
|
+
const monitor = new Monitor(this, id)
|
|
195
|
+
if (this._monitors.size === 0) this._startListening()
|
|
196
|
+
this._monitors.add(monitor)
|
|
197
|
+
return monitor
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
_removeMonitor(mon) {
|
|
201
|
+
this._monitors.delete(mon)
|
|
202
|
+
if (this._monitors.size === 0) this._stopListening()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
_updatePeers() {
|
|
206
|
+
for (const m of this._monitors) m._updatePeers()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
_onUpload(index, bytes, from) {
|
|
210
|
+
for (const m of this._monitors) m._onUpload(index, bytes, from)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
_onDownload(index, bytes, from) {
|
|
214
|
+
for (const m of this._monitors) m._onDownload(index, bytes, from)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
_startListening() {
|
|
218
|
+
this.core.on('peer-add', this._boundUpdatePeers)
|
|
219
|
+
this.core.on('peer-remove', this._boundUpdatePeers)
|
|
220
|
+
this.core.on('upload', this._boundOnUpload)
|
|
221
|
+
this.core.on('download', this._boundOnDownload)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
_stopListening() {
|
|
225
|
+
this.core.off('peer-add', this._boundUpdatePeers)
|
|
226
|
+
this.core.off('peer-remove', this._boundUpdatePeers)
|
|
227
|
+
this.core.off('upload', this._boundOnUpload)
|
|
228
|
+
this.core.off('download', this._boundOnDownload)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
module.exports = Hyperblobs
|
|
233
|
+
|
|
234
|
+
function isAll(id, opts) {
|
|
235
|
+
if (id.blockMap) return false
|
|
236
|
+
if (!opts) return true
|
|
237
|
+
if (opts.start) return false
|
|
238
|
+
if (opts.length !== undefined || opts.end !== undefined) return false
|
|
239
|
+
if (opts.core) return false
|
|
240
|
+
return true
|
|
241
|
+
}
|
package/lib/block-map.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
const c = require('compact-encoding')
|
|
2
|
+
const b4a = require('b4a')
|
|
3
|
+
const crypto = require('hypercore-crypto')
|
|
4
|
+
|
|
5
|
+
const block = {
|
|
6
|
+
preencode(state, m) {
|
|
7
|
+
c.uint.preencode(state, m.index)
|
|
8
|
+
c.uint.preencode(state, m.byteLength)
|
|
9
|
+
},
|
|
10
|
+
encode(state, m) {
|
|
11
|
+
c.uint.encode(state, m.index)
|
|
12
|
+
c.uint.encode(state, m.byteLength)
|
|
13
|
+
},
|
|
14
|
+
decode(state) {
|
|
15
|
+
return {
|
|
16
|
+
index: c.uint.decode(state),
|
|
17
|
+
byteLength: c.uint.decode(state)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const list = c.array(block)
|
|
23
|
+
|
|
24
|
+
const map = {
|
|
25
|
+
preencode(state, m) {
|
|
26
|
+
c.uint.preencode(state, 0)
|
|
27
|
+
list.preencode(state, m.blocks)
|
|
28
|
+
},
|
|
29
|
+
encode(state, m) {
|
|
30
|
+
c.uint.encode(state, 0)
|
|
31
|
+
list.encode(state, m.blocks)
|
|
32
|
+
},
|
|
33
|
+
decode(state) {
|
|
34
|
+
const version = c.uint.decode(state)
|
|
35
|
+
if (version > 0) throw new Error('Unsupported block map version')
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
version,
|
|
39
|
+
blocks: list.decode(state)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.hash = hashId
|
|
45
|
+
|
|
46
|
+
function hashId(block) {
|
|
47
|
+
return b4a.toString(crypto.hash(block), 'hex')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.get = getBlockMap
|
|
51
|
+
|
|
52
|
+
async function inferBlockMap(core, id, opts = {}) {
|
|
53
|
+
const hashes = !!opts.hashes
|
|
54
|
+
|
|
55
|
+
const map = {
|
|
56
|
+
hashes: hashes ? new Map() : null,
|
|
57
|
+
blocks: []
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
for (let i = id.blockOffset; i < id.blockOffset + id.blockLength; i++) {
|
|
61
|
+
const block = await core.get(i)
|
|
62
|
+
const entry = { index: i, byteLength: block.byteLength }
|
|
63
|
+
map.blocks.push(entry)
|
|
64
|
+
if (hashes) map.hashes.set(hashId(block), entry)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return map
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function getBlockMap(core, id, opts = {}) {
|
|
71
|
+
if (!id.blockMap) return inferBlockMap(core, id, opts)
|
|
72
|
+
|
|
73
|
+
if (id.blockLength > 64) {
|
|
74
|
+
throw new Error('Block map is too large')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const hashes = !!opts.hashes
|
|
78
|
+
|
|
79
|
+
const map = {
|
|
80
|
+
hashes: hashes ? new Map() : null,
|
|
81
|
+
blocks: null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const promises = []
|
|
85
|
+
|
|
86
|
+
for (let i = id.blockOffset; i < id.blockOffset + id.blockLength; i++) {
|
|
87
|
+
promises.push(core.get(i))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const buffers = await Promise.all(promises)
|
|
91
|
+
const m = decodeBlockMap(buffers)
|
|
92
|
+
if (!m) return null
|
|
93
|
+
|
|
94
|
+
map.blocks = m.blocks
|
|
95
|
+
|
|
96
|
+
if (hashes && !core.writable) {
|
|
97
|
+
const blocks = []
|
|
98
|
+
for (let i = 0; i < map.blocks.length; i++) blocks.push(map.blocks[i].index)
|
|
99
|
+
core.download({ blocks })
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (hashes) {
|
|
103
|
+
for (let i = 0; i < map.blocks.length; i++) {
|
|
104
|
+
const b = map.blocks[i]
|
|
105
|
+
const block = await core.get(b.index)
|
|
106
|
+
if (block === null) return null
|
|
107
|
+
map.hashes.set(hashId(block), b)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return map
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
exports.encode = encodeBlockMap
|
|
115
|
+
|
|
116
|
+
function encodeBlockMap(header) {
|
|
117
|
+
const result = []
|
|
118
|
+
|
|
119
|
+
for (let i = 0; i < header.blocks.length; i += 8192) {
|
|
120
|
+
const blocks =
|
|
121
|
+
i === 0 && header.blocks.length < 8192 ? header.blocks : header.blocks.slice(i, i + 8192)
|
|
122
|
+
|
|
123
|
+
const state = { start: 0, end: 0, buffer: null }
|
|
124
|
+
const m = { version: 0, blocks }
|
|
125
|
+
|
|
126
|
+
map.preencode(state, m)
|
|
127
|
+
state.buffer = b4a.allocUnsafe(state.end)
|
|
128
|
+
map.encode(state, m)
|
|
129
|
+
|
|
130
|
+
result.push(state.buffer)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return result
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
exports.decode = decodeBlockMap
|
|
137
|
+
|
|
138
|
+
function decodeBlockMap(buffers) {
|
|
139
|
+
const result = {
|
|
140
|
+
version: 0,
|
|
141
|
+
blocks: null
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (let i = 0; i < buffers.length; i++) {
|
|
145
|
+
if (!buffers[i]) return null
|
|
146
|
+
|
|
147
|
+
const state = { start: 0, end: buffers[i].byteLength, buffer: buffers[i] }
|
|
148
|
+
const r = map.decode(state)
|
|
149
|
+
|
|
150
|
+
result.version = r.version
|
|
151
|
+
|
|
152
|
+
if (result.blocks) result.blocks.push(...r.blocks)
|
|
153
|
+
else result.blocks = r.blocks
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return result
|
|
157
|
+
}
|
package/lib/monitor.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const EventEmitter = require('events')
|
|
2
|
+
const speedometer = require('speedometer')
|
|
3
|
+
|
|
4
|
+
module.exports = class Monitor extends EventEmitter {
|
|
5
|
+
constructor(blobs, id) {
|
|
6
|
+
super()
|
|
7
|
+
|
|
8
|
+
if (!id) throw new Error('id is required')
|
|
9
|
+
|
|
10
|
+
this.blobs = blobs
|
|
11
|
+
this.id = id
|
|
12
|
+
this.peers = 0
|
|
13
|
+
this.uploadSpeedometer = null
|
|
14
|
+
this.downloadSpeedometer = null
|
|
15
|
+
|
|
16
|
+
const stats = {
|
|
17
|
+
startTime: 0,
|
|
18
|
+
percentage: 0,
|
|
19
|
+
peers: 0,
|
|
20
|
+
speed: 0,
|
|
21
|
+
blocks: 0,
|
|
22
|
+
totalBytes: 0, // local + bytes loaded during monitoring
|
|
23
|
+
monitoringBytes: 0, // bytes loaded during monitoring
|
|
24
|
+
targetBytes: 0,
|
|
25
|
+
targetBlocks: 0
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.uploadStats = { ...stats }
|
|
29
|
+
this.downloadStats = { ...stats }
|
|
30
|
+
this.uploadStats.targetBytes = this.downloadStats.targetBytes = this.id.byteLength
|
|
31
|
+
this.uploadStats.targetBlocks = this.downloadStats.targetBlocks = this.id.blockLength
|
|
32
|
+
this.uploadStats.peers = this.downloadStats.peers = this.peers = this.blobs.core.peers.length
|
|
33
|
+
|
|
34
|
+
this.uploadSpeedometer = speedometer()
|
|
35
|
+
this.downloadSpeedometer = speedometer()
|
|
36
|
+
|
|
37
|
+
// Handlers
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// just an alias
|
|
41
|
+
destroy() {
|
|
42
|
+
return this.close()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
close() {
|
|
46
|
+
this.blobs._removeMonitor(this)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_onUpload(index, bytes, from) {
|
|
50
|
+
this._updateStats(this.uploadSpeedometer, this.uploadStats, index, bytes, from)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_onDownload(index, bytes, from) {
|
|
54
|
+
this._updateStats(this.downloadSpeedometer, this.downloadStats, index, bytes, from)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_updatePeers() {
|
|
58
|
+
this.uploadStats.peers = this.downloadStats.peers = this.peers = this.blobs.core.peers.length
|
|
59
|
+
this.emit('update')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_updateStats(speed, stats, index, bytes) {
|
|
63
|
+
if (this.closing) return
|
|
64
|
+
if (!isWithinRange(index, this.id)) return
|
|
65
|
+
|
|
66
|
+
if (!stats.startTime) stats.startTime = Date.now()
|
|
67
|
+
|
|
68
|
+
stats.speed = speed(bytes)
|
|
69
|
+
stats.blocks++
|
|
70
|
+
stats.totalBytes += bytes
|
|
71
|
+
stats.monitoringBytes += bytes
|
|
72
|
+
stats.percentage = toFixed((stats.blocks / stats.targetBlocks) * 100)
|
|
73
|
+
|
|
74
|
+
this.emit('update')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
downloadSpeed() {
|
|
78
|
+
return this.downloadSpeedometer ? this.downloadSpeedometer() : 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
uploadSpeed() {
|
|
82
|
+
return this.uploadSpeedometer ? this.uploadSpeedometer() : 0
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isWithinRange(index, { blockOffset, blockLength }) {
|
|
87
|
+
return index >= blockOffset && index < blockOffset + blockLength
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function toFixed(n) {
|
|
91
|
+
return Math.round(n * 100) / 100
|
|
92
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// should move to hypercore itself
|
|
2
|
+
|
|
3
|
+
const MAX_READAHEAD_TARGET = 0.05 // aim to buffer 5% always
|
|
4
|
+
|
|
5
|
+
module.exports = class Prefetcher {
|
|
6
|
+
constructor(core, { max = 64, start = 0, end = core.length, linear = true } = {}) {
|
|
7
|
+
this.core = core
|
|
8
|
+
this.max = max
|
|
9
|
+
this.range = null
|
|
10
|
+
this.startBound = start
|
|
11
|
+
this.endBound = end
|
|
12
|
+
this.maxReadAhead = Math.max(max * 2, Math.floor((end - start) * MAX_READAHEAD_TARGET))
|
|
13
|
+
|
|
14
|
+
this.start = start
|
|
15
|
+
this.end = start
|
|
16
|
+
this.linear = linear
|
|
17
|
+
this.missing = 0
|
|
18
|
+
|
|
19
|
+
this._ondownloadBound = this._ondownload.bind(this)
|
|
20
|
+
this.core.on('download', this._ondownloadBound)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
_ondownload(index) {
|
|
24
|
+
if (this.range && index < this.end && this.start <= index) {
|
|
25
|
+
this.missing--
|
|
26
|
+
this._update()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
destroy() {
|
|
31
|
+
this.core.off('download', this._ondownloadBound)
|
|
32
|
+
if (this.range) this.range.destroy()
|
|
33
|
+
this.range = null
|
|
34
|
+
this.max = 0
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
update(position) {
|
|
38
|
+
this.start = position
|
|
39
|
+
if (!this.range) this._update()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_update() {
|
|
43
|
+
if (this.missing >= this.max) return
|
|
44
|
+
if (this.range) this.range.destroy()
|
|
45
|
+
|
|
46
|
+
let end = this.end
|
|
47
|
+
|
|
48
|
+
while (end < this.endBound && this.missing < this.max) {
|
|
49
|
+
end = this.core.core.bitfield.firstUnset(end) + 1
|
|
50
|
+
if (end >= this.endBound) break
|
|
51
|
+
this.missing++
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (end > this.start + this.maxReadAhead) end = this.start + this.maxReadAhead
|
|
55
|
+
if (end >= this.endBound) end = this.endBound
|
|
56
|
+
|
|
57
|
+
this.end = end
|
|
58
|
+
|
|
59
|
+
if (this.start >= this.end) return
|
|
60
|
+
|
|
61
|
+
this.range = this.core.download({
|
|
62
|
+
start: this.start,
|
|
63
|
+
end: this.end,
|
|
64
|
+
linear: this.linear
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
}
|
package/lib/streams.js
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
const { Readable, Writable } = require('streamx')
|
|
2
|
+
const { BLOCK_NOT_AVAILABLE } = require('hypercore-errors')
|
|
3
|
+
const Prefetcher = require('./prefetcher')
|
|
4
|
+
const blockMap = require('./block-map')
|
|
5
|
+
|
|
6
|
+
class BlobWriteStream extends Writable {
|
|
7
|
+
constructor(core, lock, opts = {}) {
|
|
8
|
+
super(opts)
|
|
9
|
+
this.id = { blockOffset: 0, byteOffset: 0, blockLength: 0, byteLength: 0 }
|
|
10
|
+
this.core = core
|
|
11
|
+
|
|
12
|
+
this._dedup = !!opts.dedup
|
|
13
|
+
this._blob = opts.blob || null
|
|
14
|
+
this._hashes = null
|
|
15
|
+
this._addBlockMap = !!opts.blockMap || this._dedup
|
|
16
|
+
this._blockMap = this._addBlockMap ? { version: 0, blocks: [] } : null
|
|
17
|
+
this._lock = lock
|
|
18
|
+
this._release = null
|
|
19
|
+
this._batch = []
|
|
20
|
+
|
|
21
|
+
if (this._addBlockMap) this.id.blockMap = true
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async _openp() {
|
|
25
|
+
await this.core.ready()
|
|
26
|
+
const release = await new Promise((resolve) => this._lock(resolve))
|
|
27
|
+
|
|
28
|
+
this._release = release
|
|
29
|
+
this.id.byteOffset = this.core.byteLength
|
|
30
|
+
this.id.blockOffset = this.core.length
|
|
31
|
+
|
|
32
|
+
if (!this._dedup) return
|
|
33
|
+
|
|
34
|
+
if (this._blob) {
|
|
35
|
+
const map = await blockMap.get(this.core, this._blob, { hashes: true })
|
|
36
|
+
this._hashes = map.hashes
|
|
37
|
+
} else {
|
|
38
|
+
this._hashes = new Map()
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_open(cb) {
|
|
43
|
+
this._openp().then(cb, cb)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async _finalp() {
|
|
47
|
+
await this._append()
|
|
48
|
+
|
|
49
|
+
if (this._blockMap) {
|
|
50
|
+
const buffers = await blockMap.encode(this._blockMap)
|
|
51
|
+
this.id.blockOffset = this.core.length
|
|
52
|
+
this.id.byteOffset = this.core.byteLength
|
|
53
|
+
await this.core.append(buffers)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.id.blockLength = this.core.length - this.id.blockOffset
|
|
57
|
+
this.id.byteLength = this.core.byteLength - this.id.byteOffset
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_final(cb) {
|
|
61
|
+
this._finalp().then(cb, cb)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
_destroy(cb) {
|
|
65
|
+
if (this._release) this._release()
|
|
66
|
+
cb(null)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async _append() {
|
|
70
|
+
if (!this._batch.length) return
|
|
71
|
+
|
|
72
|
+
const batch = this._batch
|
|
73
|
+
this._batch = []
|
|
74
|
+
|
|
75
|
+
await this.core.append(batch)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
_write(data, cb) {
|
|
79
|
+
let dup = false
|
|
80
|
+
|
|
81
|
+
if (this._blockMap) {
|
|
82
|
+
let entry = {
|
|
83
|
+
index: this.core.length + this._batch.length,
|
|
84
|
+
byteLength: data.byteLength
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (this._hashes) {
|
|
88
|
+
const id = blockMap.hash(data)
|
|
89
|
+
const existing = this._hashes.get(id)
|
|
90
|
+
|
|
91
|
+
if (existing) {
|
|
92
|
+
entry = existing
|
|
93
|
+
dup = true
|
|
94
|
+
} else {
|
|
95
|
+
this._hashes.set(id, entry)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this._blockMap.blocks.push(entry)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (dup) return cb()
|
|
103
|
+
|
|
104
|
+
this._batch.push(data)
|
|
105
|
+
|
|
106
|
+
if (this._batch.length >= 16) {
|
|
107
|
+
this._append().then(cb, cb)
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return cb()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class BlockMapReadStream extends Readable {
|
|
116
|
+
constructor(core, id, opts = {}) {
|
|
117
|
+
super(opts)
|
|
118
|
+
this.id = id
|
|
119
|
+
this.core = core.session({ wait: opts.wait, timeout: opts.timeout })
|
|
120
|
+
|
|
121
|
+
const noPrefetch = opts.wait === false || opts.prefetch === false || !core.core
|
|
122
|
+
const start = opts.start || 0
|
|
123
|
+
const end =
|
|
124
|
+
opts.end === undefined ? (opts.length === undefined ? -1 : start + opts.length) : opts.end + 1
|
|
125
|
+
|
|
126
|
+
this._blockMap = null
|
|
127
|
+
|
|
128
|
+
this._rangeStart = start
|
|
129
|
+
this._rangeEnd = end
|
|
130
|
+
|
|
131
|
+
this._startIndex = 0
|
|
132
|
+
this._startOffset = 0
|
|
133
|
+
this._endIndex = -1
|
|
134
|
+
this._endOffset = -1
|
|
135
|
+
|
|
136
|
+
this._range = null
|
|
137
|
+
this._noPrefetch = noPrefetch
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async _openp() {
|
|
141
|
+
this._blockMap = await blockMap.get(this.core, this.id)
|
|
142
|
+
|
|
143
|
+
const [startIndex, startOffset, endIndex, endLength] = seekBlockMap(
|
|
144
|
+
this._blockMap,
|
|
145
|
+
this._rangeStart,
|
|
146
|
+
this._rangeEnd
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
this._startIndex = startIndex
|
|
150
|
+
this._startOffset = startOffset
|
|
151
|
+
this._endIndex = endIndex
|
|
152
|
+
this._endOffset = endLength
|
|
153
|
+
|
|
154
|
+
if (this._endIndex === -1) {
|
|
155
|
+
this._endIndex = this._blockMap.blocks.length
|
|
156
|
+
this._endOffset = 0
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
_open(cb) {
|
|
161
|
+
this._openp().then(cb, cb)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_predestroy() {
|
|
165
|
+
if (this._range) this._range.destroy()
|
|
166
|
+
this.core.close().then(noop, noop)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_destroy(cb) {
|
|
170
|
+
if (this._range) this._range.destroy()
|
|
171
|
+
this.core.close().then(cb, cb)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
_prefetch(index) {
|
|
175
|
+
const blocks = []
|
|
176
|
+
for (; index < this._endIndex; index++) blocks.push(this._blockMap.blocks[index])
|
|
177
|
+
this._range = this.core.download({ blocks })
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async _readp() {
|
|
181
|
+
if (this._startIndex >= this._endIndex) {
|
|
182
|
+
this.push(null)
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let block = null
|
|
187
|
+
|
|
188
|
+
const index = this._startIndex++
|
|
189
|
+
const b = this._blockMap.blocks[index]
|
|
190
|
+
|
|
191
|
+
if (!this._range && !this._noPrefetch) {
|
|
192
|
+
block = await this.core.get(b.index, { wait: false })
|
|
193
|
+
if (!block) this._prefetch(index)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (!block) {
|
|
197
|
+
block = await this.core.get(b.index)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!block) throw BLOCK_NOT_AVAILABLE()
|
|
201
|
+
|
|
202
|
+
if (this._startOffset) {
|
|
203
|
+
block = block.subarray(this._startOffset)
|
|
204
|
+
this._startOffset = 0
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (this._startIndex === this._endIndex && this._endOffset) {
|
|
208
|
+
block = block.subarray(0, block.byteLength - this._endOffset)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
this.push(block)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
_read(cb) {
|
|
215
|
+
this._readp().then(cb, cb)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
class BlobReadStream extends Readable {
|
|
220
|
+
constructor(core, id, opts = {}) {
|
|
221
|
+
super(opts)
|
|
222
|
+
this.id = id
|
|
223
|
+
this.core = core.session({ wait: opts.wait, timeout: opts.timeout })
|
|
224
|
+
|
|
225
|
+
const start = id.blockOffset
|
|
226
|
+
const end = id.blockOffset + id.blockLength
|
|
227
|
+
const noPrefetch = opts.wait === false || opts.prefetch === false || !core.core
|
|
228
|
+
|
|
229
|
+
this._prefetch = noPrefetch
|
|
230
|
+
? null
|
|
231
|
+
: new Prefetcher(this.core, { max: opts.prefetch, start, end })
|
|
232
|
+
this._lastPrefetch = null
|
|
233
|
+
|
|
234
|
+
this._pos = opts.start !== undefined ? id.byteOffset + opts.start : id.byteOffset
|
|
235
|
+
|
|
236
|
+
if (opts.length !== undefined) this._end = this._pos + opts.length
|
|
237
|
+
else if (opts.end !== undefined) this._end = id.byteOffset + opts.end + 1
|
|
238
|
+
else this._end = id.byteOffset + id.byteLength
|
|
239
|
+
|
|
240
|
+
this._index = 0
|
|
241
|
+
this._relativeOffset = 0
|
|
242
|
+
this._bytesRead = 0
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async _openp() {
|
|
246
|
+
if (this._pos === this.id.byteOffset) {
|
|
247
|
+
this._index = this.id.blockOffset
|
|
248
|
+
this._relativeOffset = 0
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const result = await this.core.seek(this._pos, {
|
|
253
|
+
start: this.id.blockOffset,
|
|
254
|
+
end: this.id.blockOffset + this.id.blockLength
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
if (!result) throw BLOCK_NOT_AVAILABLE()
|
|
258
|
+
|
|
259
|
+
this._index = result[0]
|
|
260
|
+
this._relativeOffset = result[1]
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
_open(cb) {
|
|
264
|
+
this._openp().then(cb, cb)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
_predestroy() {
|
|
268
|
+
if (this._prefetch) this._prefetch.destroy()
|
|
269
|
+
this.core.close().then(noop, noop)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
_destroy(cb) {
|
|
273
|
+
if (this._prefetch) this._prefetch.destroy()
|
|
274
|
+
this.core.close().then(cb, cb)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async _readp() {
|
|
278
|
+
if (this._pos >= this._end) {
|
|
279
|
+
this.push(null)
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (this._prefetch) this._prefetch.update(this._index)
|
|
284
|
+
|
|
285
|
+
let block = await this.core.get(this._index)
|
|
286
|
+
if (!block) throw BLOCK_NOT_AVAILABLE()
|
|
287
|
+
|
|
288
|
+
const remainder = this._end - this._pos
|
|
289
|
+
if (this._relativeOffset || remainder < block.length) {
|
|
290
|
+
block = block.subarray(this._relativeOffset, this._relativeOffset + remainder)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
this._index++
|
|
294
|
+
this._relativeOffset = 0
|
|
295
|
+
this._pos += block.length
|
|
296
|
+
this._bytesRead += block.length
|
|
297
|
+
|
|
298
|
+
this.push(block)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
_read(cb) {
|
|
302
|
+
this._readp().then(cb, cb)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
module.exports = {
|
|
307
|
+
BlockMapReadStream,
|
|
308
|
+
BlobReadStream,
|
|
309
|
+
BlobWriteStream
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function noop() {}
|
|
313
|
+
|
|
314
|
+
function seekBlockMap(map, start, end) {
|
|
315
|
+
let s = -1
|
|
316
|
+
let so = -1
|
|
317
|
+
let e = -1
|
|
318
|
+
let eo = -1
|
|
319
|
+
|
|
320
|
+
for (let i = 0; i < map.blocks.length; i++) {
|
|
321
|
+
const b = map.blocks[i]
|
|
322
|
+
|
|
323
|
+
if (s === -1) {
|
|
324
|
+
if (start < b.byteLength) {
|
|
325
|
+
s = i
|
|
326
|
+
so = start
|
|
327
|
+
} else {
|
|
328
|
+
start -= b.byteLength
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (e === -1 && end > -1) {
|
|
333
|
+
if (end <= b.byteLength) {
|
|
334
|
+
e = i + 1
|
|
335
|
+
eo = b.byteLength - end
|
|
336
|
+
} else {
|
|
337
|
+
end -= b.byteLength
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return [s, so, e, eo]
|
|
343
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peardrive/hyperblobs",
|
|
3
|
+
"version": "2.9.0-pruned.1",
|
|
4
|
+
"description": "A blob store for Hypercore",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"format": "prettier --write .",
|
|
8
|
+
"lint": "prettier --check . && lunte",
|
|
9
|
+
"test": "npm run test:node && npm run test:bare",
|
|
10
|
+
"test:node": "brittle-node test/*.js",
|
|
11
|
+
"test:bare": "brittle-bare test/*.js"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/holepunchto/hyperblobs.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"hypercore",
|
|
19
|
+
"blob",
|
|
20
|
+
"store"
|
|
21
|
+
],
|
|
22
|
+
"author": "Andrew Osheroff <andrewosh@gmail.com>",
|
|
23
|
+
"license": "Apache-2.0",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/holepunchto/hyperblobs/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/holepunchto/hyperblobs#readme",
|
|
28
|
+
"files": [
|
|
29
|
+
"index.js",
|
|
30
|
+
"lib/**.js"
|
|
31
|
+
],
|
|
32
|
+
"imports": {
|
|
33
|
+
"events": {
|
|
34
|
+
"bare": "bare-events",
|
|
35
|
+
"default": "events"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"b4a": "^1.6.1",
|
|
40
|
+
"bare-events": "^2.5.0",
|
|
41
|
+
"compact-encoding": "^2.18.0",
|
|
42
|
+
"hypercore-crypto": "^3.6.1",
|
|
43
|
+
"hypercore-errors": "^1.1.1",
|
|
44
|
+
"mutexify": "^1.4.0",
|
|
45
|
+
"speedometer": "^1.1.0",
|
|
46
|
+
"streamx": "^2.13.2"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"brittle": "^3.1.0",
|
|
50
|
+
"hypercore": "^11.0.0",
|
|
51
|
+
"lunte": "^1.0.0",
|
|
52
|
+
"prettier": "^3.6.2",
|
|
53
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
54
|
+
}
|
|
55
|
+
}
|