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
var swarm =webrtc(signalhub(link, DEFAULT_SIGNALHUBS))
@@ -127,104 +122,14 @@ There are a million different ways to store and retrieve data in the browser, an
127
122
128
123
The first argument to `hyperdrive` will be the main database for all metadata and content. The `file` option can be supplied to specify how to read and write content data. If a `file` option is not supplied, the content will also be stored in the main database.
129
124
130
-
```js
131
-
var hyperdrive =require('hyperdrive')
132
-
var drive =hyperdrive(<YOURDATABASEHERE>, {file:<CONTENTDATABASEHERE>})
133
-
```
134
-
135
125
There are many different ways to piece modules together to create the storage infrastructure for a hyperdrive -- here are some tested examples:
136
126
137
-
### In-memory storage
138
-
139
-
When the user refreshes their browser, they will lose all previous keys and data. The user will no longer be able to write more data into the hyperdrive.
140
-
141
-
```js
142
-
var hyperdrive =require('hyperdrive')
143
-
var memdb =require('memdb')
144
-
145
-
var drive =hyperdrive(memdb())
146
-
var archive =drive.createArchive()
147
-
```
148
-
149
-
### Persistence with IndexedDB
150
-
151
-
When the user refreshes their browser, their keys will be stored and retrieved.
152
-
153
-
The best module to use for this is `level-browserify`:
154
-
155
-
```js
156
-
var hyperdrive =require('hyperdrive')
157
-
var level =require('level-browserify')
158
-
159
-
var drive =hyperdrive(level('./mydb'))
160
-
var archive =drive.createArchive()
161
-
```
162
-
163
-
This will store all of the hyperdrive metadata *as well as content* in the client's IndexedDB. This is pretty inefficient. You'll notice that with this method that *IndexedDB will start to become full and the hyperdrive database will stop working as usual*.
164
-
165
-
### Persistent metadata in IndexedDB with in-memory file content
166
-
167
-
If you use level-browserify to store file content, you will quickly notice performance issues with large files. Writes after about 3.4GB will become blocked by the browser. You can avoid this by using in-memory storage for the file content.
168
-
169
-
To do this, use [random-access-file-reader](https://github.com/mafintosh/random-access-file-reader) as the file writer and reader for the hyperdrive.
170
-
171
-
```js
172
-
var hyperdrive =require('hyperdrive')
173
-
var level =require('level-browserify')
174
-
var ram =require('random-access-memory')
175
-
176
-
var drive =hyperdrive(level('./mydb'))
177
-
var archive =drive.createArchive({
178
-
file: ram
179
-
})
180
-
```
181
-
182
-
This works well for most cases until you want to write a file to hyperdrive that doesn't fit in memory.
183
-
184
127
### Writing large files from the filesystem to the browser
185
128
186
129
File writes are limited to the available memory on the machine. Files are buffered (read: copied) *into memory* while being written to the hyperdrive instance. This isn't ideal, but works as long as file sizes stay below system RAM limits.
187
130
188
131
To fix this problem, you can use [random-access-file-reader](https://github.com/mafintosh/random-access-file-reader) to read the files directly from the filesystem instead of buffering them into memory.
189
132
190
-
Here we will create a simple program that creates a file 'drag and drop' element on `document.body.` When the user drags files onto the element, pointers to them will be added to the `files` object.
191
-
192
-
193
-
```js
194
-
var drop =require('drag-drop')
195
-
196
-
var files = {}
197
-
198
-
drop(document.body, function (files) {
199
-
files[files[0].name] = files[0]
200
-
})
201
-
```
202
-
203
-
Okay, that's pretty easy. Now let's add the hyperdrive. Hyperdrive needs to know what the pointers are, so when a peer asks for the file, it can read from the filesystem rather from memory. In other words, we are telling the hyperdrive which files it should index.
204
-
205
-
```js
206
-
var drop =require('drag-drop')
207
-
var reader =require('random-access-file-reader')
208
-
var hyperdrive =require('hyperdrive')
209
-
var memdb =require('memdb')
210
-
211
-
var files = {}
212
-
213
-
var drive =hyperdrive(memdb())
214
-
215
-
var archive =drive.createArchive({
216
-
file:function (name) {
217
-
returnreader(files[name])
218
-
}
219
-
})
220
-
221
-
drop(document.body, function (files) {
222
-
files[files[0].name] = files[0]
223
-
// will index the file using hyperdrive without reading the entire file into ram
224
-
archive.append(files[0].name)
225
-
})
226
-
```
227
-
228
133
Come over to our community channels and ask a question. It's probably a good one and we should cover it in the documentation. Thanks for trying it out, and PRs always welcome!
229
134
230
135
[](http://webchat.freenode.net/?channels=dat)
0 commit comments