aboutsummaryrefslogtreecommitdiffstats
path: root/docs/cookbook/browser.md
diff options
context:
space:
mode:
authorKarissa McKelvey <karissa@users.noreply.github.com>2017-05-30 12:26:43 -0700
committerJoe Hand <joe@joeahand.com>2017-05-30 12:26:43 -0700
commit282ff400143c85cc7920d392a3e8d58aa0bce127 (patch)
tree1c6357f0bd2a9df73c84f91ef48b271979a5dbcb /docs/cookbook/browser.md
parentb7b24c93d845a9133e0714cf0a34aceb01e28db2 (diff)
downloaddat-docs-282ff400143c85cc7920d392a3e8d58aa0bce127.tar.gz
dat-docs-282ff400143c85cc7920d392a3e8d58aa0bce127.zip
update browser page for sleep (#49)
Diffstat (limited to 'docs/cookbook/browser.md')
-rw-r--r--docs/cookbook/browser.md99
1 files changed, 2 insertions, 97 deletions
diff --git a/docs/cookbook/browser.md b/docs/cookbook/browser.md
index b2dedb2..ee20f9b 100644
--- a/docs/cookbook/browser.md
+++ b/docs/cookbook/browser.md
@@ -54,15 +54,12 @@ dat.add(function (repo) {
```js
var Dat = require('dat-js')
-var concat = require('concat-stream')
var clone = Dat()
clone.add(key, function (repo) {
- var readStream = repo.archive.createFileReadStream('hello.txt')
- concat(readStream, function (data) {
+ repo.archive.readFile('hello.txt', function (err, data) {
console.log(data.toString()) // prints 'world'
})
- // and do other things with the stream
})
```
@@ -100,9 +97,7 @@ var pump = require('pump')
var DEFAULT_SIGNALHUBS = 'https://signalhub.mafintosh.com'
-var drive = hyperdrive(memdb())
-
-var archive = drive.createArchive()
+var archive = hyperdrive()
var link = archive.discoveryKey.toString('hex')
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
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.
-```js
-var hyperdrive = require('hyperdrive')
-var drive = hyperdrive(<YOUR DATABASE HERE>, {file: <CONTENT DATABASE HERE>})
-```
-
There are many different ways to piece modules together to create the storage infrastructure for a hyperdrive -- here are some tested examples:
-### In-memory storage
-
-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.
-
-```js
-var hyperdrive = require('hyperdrive')
-var memdb = require('memdb')
-
-var drive = hyperdrive(memdb())
-var archive = drive.createArchive()
-```
-
-### Persistence with IndexedDB
-
-When the user refreshes their browser, their keys will be stored and retrieved.
-
-The best module to use for this is `level-browserify`:
-
-```js
-var hyperdrive = require('hyperdrive')
-var level = require('level-browserify')
-
-var drive = hyperdrive(level('./mydb'))
-var archive = drive.createArchive()
-```
-
-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*.
-
-### Persistent metadata in IndexedDB with in-memory file content
-
-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.
-
-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.
-
-```js
-var hyperdrive = require('hyperdrive')
-var level = require('level-browserify')
-var ram = require('random-access-memory')
-
-var drive = hyperdrive(level('./mydb'))
-var archive = drive.createArchive({
- file: ram
-})
-```
-
-This works well for most cases until you want to write a file to hyperdrive that doesn't fit in memory.
-
### Writing large files from the filesystem to the browser
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.
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.
-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.
-
-
-```js
-var drop = require('drag-drop')
-
-var files = {}
-
-drop(document.body, function (files) {
- files[files[0].name] = files[0]
-})
-```
-
-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.
-
-```js
-var drop = require('drag-drop')
-var reader = require('random-access-file-reader')
-var hyperdrive = require('hyperdrive')
-var memdb = require('memdb')
-
-var files = {}
-
-var drive = hyperdrive(memdb())
-
-var archive = drive.createArchive({
- file: function (name) {
- return reader(files[name])
- }
-})
-
-drop(document.body, function (files) {
- files[files[0].name] = files[0]
- // will index the file using hyperdrive without reading the entire file into ram
- archive.append(files[0].name)
-})
-```
-
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!
[![#dat IRC channel on freenode](https://img.shields.io/badge/irc%20channel-%23dat%20on%20freenode-blue.svg)](http://webchat.freenode.net/?channels=dat)