Skip to content

Commit d1399d3

Browse files
author
Joe Hand
authored
SLEEP + general docs update + more introduction content (#57)
* big docs update * fix sleep link * separate sustainability header * better into and reword * clean up terms and other places * add intro, overview, troubleshooting. update more content * minor change to force new deploy * more words * more updates and some pictures * wordsmith intro to be more about dat uniqueness * syncing & publishing * add link files description * add new gifs * change overview to remove tutorial * add wip tutorial content * update build with dat * add http file
1 parent 079395f commit d1399d3

20 files changed

Lines changed: 675 additions & 188 deletions

assets/dat_folder.png

80.1 KB
Loading

assets/desktop_share.gif

2.69 MB
Loading

assets/install_cli.png

12.8 KB
Loading

assets/install_desktop.png

12.2 KB
Loading

assets/share_link.png

98.6 KB
Loading

docs/contents.json

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
2-
"Dat": {
3-
"Introduction": "modules/dat.md",
4-
"How Dat Works": "how-dat-works.md",
5-
"Terminology": "terms.md",
6-
"FAQ": "faq.md"
2+
"Using Dat": {
3+
"Introduction": "intro.md",
4+
"Dat Concepts": "overview.md",
5+
"Command Line": "modules/dat.md",
6+
"FAQ": "faq.md",
7+
"Troubleshooting": "troubleshooting.md",
8+
"Terminology": "terms.md"
79
},
810
"Cookbook": {
9-
"On a Server": "cookbook/server.md",
11+
"Getting Started": "cookbook/tutorial.md",
12+
"Sharing files over HTTP": "cookbook/http.md",
13+
"Running a Dat Server": "cookbook/server.md",
1014
"In the Browser": "cookbook/browser.md",
11-
"Under the Hood": "cookbook/diy-dat.md"
15+
"Using Dat in JS Apps": "cookbook/diy-dat.md",
16+
"Using Hyperdrive FS": "cookbook/using-fs.md"
1217
},
13-
"Ecosystem": {
18+
"Dat Technology": {
1419
"Overview": "ecosystem.md",
15-
"SLEEP": "sleep.md",
16-
"Hyperdrive": "modules/hyperdrive.md",
17-
"Hypercore": "modules/hypercore.md"
20+
"dat-node": "modules/dat-node.md",
21+
"hyperdrive": "modules/hyperdrive.md",
22+
"hypercore": "modules/hypercore.md",
23+
"hyperdiscovery": "modules/hyperdiscovery.md"
1824
}
1925
}

docs/cookbook/diy-dat.md

Lines changed: 56 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,132 +2,124 @@
22

33
In this guide, we will show how to develop applications with the Dat ecosystem. The Dat ecosystem is very modular making it easy to develop custom applications using Dat.
44

5+
Dat comes with a built in javascript API we use in Dat Desktop and dat command line. For custom applications, or more control, you can also use the core Dat modules separately.
6+
7+
Use Dat in your JS Application:
8+
9+
1. `require('dat')`: use the [high-level Dat JS API](https://github.com/datproject/dat-node).
10+
2. Build your own!
11+
12+
This tutorial will cover the second option and get you familiar with the core Dat modules.
13+
14+
### The Dat Core Modules
15+
516
This tutorial will follow the steps for sharing and downloading files using Dat. In practice, we implement these in [dat-node](https://github.com/datproject/dat-node), a high-level module for using Dat that provides easy access to the core Dat modules.
617

7-
For any Dat application, there are three essential modules you will start with:
18+
For any Dat application, there are two essential modules you will start with:
819

920
1. [hyperdrive](https://npmjs.org/hyperdrive) for file synchronization and versioning
10-
2. [hyperdrive-archive-swarm](https://npmjs.org/hyperdrive-archive-swarm) helps discover and connect to peers over local networks and the internet
11-
3. A [LevelDB](https://npmjs.org/level) compatible database for storing metadata.
21+
2. [hyperdiscovery](https://npmjs.org/hyperdiscovery) helps discover and connect to peers over local networks and the internet
1222

13-
The [Dat CLI](https://npmjs.org/dat) module itself combines these modules and wraps them in a command-line API. These modules can be swapped out for a similarly compatible module, such as switching LevelDb for [MemDB](https://github.com/juliangruber/memdb) (which we do in the first example). More details on how these module work together are available in [How Dat Works](how-dat-works.md).
23+
The [Dat CLI](https://npmjs.org/dat) module itself combines these modules and wraps them in a command-line API. We also use the [dat-storage](https://github.com/datproject/dat-storage) module to handle file and key storage. These modules can be swapped out for a similarly compatible module, such as switching storage for [random-access-memory](https://github.com/mafintosh/random-access-memory).
1424

1525
## Getting Started
1626

1727
You will need node and npm installed to build with Dat. [Read more](https://github.com/datproject/dat/blob/master/CONTRIBUTING.md#development-workflow) about our development work flow to learn how we manage our module dependencies during development.
1828

1929
## Download a File
2030

21-
Our first module will download files from a Dat link entered by the user. View the code for this module on [Github](https://github.com/joehand/diy-dat-examples/tree/master/module-1).
31+
Our first module will download files from a Dat link entered by the user.
2232

2333
```bash
2434
mkdir module-1 && cd module-1
2535
npm init
26-
npm install --save hyperdrive memdb hyperdrive-archive-swarm
36+
npm install --save hyperdrive random-access-memory hyperdiscovery
2737
touch index.js
2838
```
2939

30-
For this example, we will use [memdb](https://github.com/juliangruber/memdb) for our database (keeping the metadata in memory rather than on the file system). In your `index.js` file, require the main modules and set them up:
40+
For this example, we will use random-access-memory for our database (keeping the metadata in memory rather than on the file system). In your `index.js` file, require the main modules and set them up:
3141

3242
```js
33-
var memdb = require('memdb')
34-
var Hyperdrive = require('hyperdrive')
35-
var Swarm = require('hyperdrive-archive-swarm')
43+
var ram = require('random-access-memory')
44+
var hyperdrive = require('hyperdrive')
45+
var discovery = require('hyperdiscovery')
3646

3747
var link = process.argv[2] // user inputs the dat link
3848

39-
var db = memdb()
40-
var drive = Hyperdrive(db)
41-
var archive = drive.createArchive(link)
42-
var swarm = Swarm(archive)
49+
var archive = hyperdrive(ram, link)
50+
archive.ready(function () {
51+
discovery(archive)
52+
})
4353
```
4454

45-
Notice, the user will input the link for the second argument The easiest way to get a file from a hyperdrive archive is to make a read stream. `archive.createFileReadStream` accepts the index number of filename for the first argument. To display the file, we can create a file stream and pipe it to `process.stdout`.
55+
Notice, the user will input the link for the second argument The easiest way to get a file from a hyperdrive archive is to make a read stream. `archive.readFile` accepts the index number of filename for the first argument. To display the file, we can create a file stream and pipe it to `process.stdout`.
4656

4757
```js
48-
var stream = archive.createFileReadStream(0) // get the first file
49-
stream.pipe(process.stdout)
58+
// Make sure your archive has a dat.json file!
59+
var stream = archive.readFile('dat.json', 'utf-8', function (err, data) {
60+
if (err) throw err
61+
console.log(data)
62+
})
5063
```
5164

52-
Now, you can run the module! To download the first file from our docs Dat, run:
65+
Now, you can run the module! To download the `dat.json` file from an archive:
5366

5467
```
55-
node index.js 395e3467bb5b2fa083ee8a4a17a706c5574b740b5e1be6efd65754d4ab7328c2
68+
node index.js dat://<link>
5669
```
5770

58-
You should see the first file in our docs repo.
71+
You should see the `dat.json` file.
5972

6073
#### Bonus: Display any file in the Dat
6174

6275
With a few more lines of code, the user can enter a file to display from the Dat link.
6376

64-
Challenge: create a module that will allow the user to input a Dat link and a filename: `node bonus.js <dat-link> <filename>`. The module will print out that file from the link, as we did above. To get a specific file you can change the file stream to use the filename instead of the index number:
77+
Challenge: create a module that will allow the user to input a Dat link and a filename: `node bonus.js <dat-link> <filename>`. The module will print out that file from the link, as we did above:
6578

6679
```js
67-
var stream = archive.createFileReadStream(fileName)
80+
var stream = archive.readFile(fileName)
6881
```
6982

7083
Once you are finished, see if you can view this file by running:
7184

7285
```bash
73-
node bonus.js 395e3467bb5b2fa083ee8a4a17a706c5574b740b5e1be6efd65754d4ab7328c2 cookbook/diy-dat.md
86+
node bonus.js 395e3467bb5b2fa083ee8a4a17a706c5574b740b5e1be6efd65754d4ab7328c2 readme.md
7487
```
7588

76-
[See how we coded it](https://github.com/joehand/diy-dat-examples/blob/master/module-1/bonus.js).
77-
7889
## Download all files to computer
7990

80-
This module will build on the last module. Instead of displaying a single file, we will download all of the files from a Dat into a local directory. View the code for this module on [Github](https://github.com/joehand/diy-dat-examples/tree/master/module-2).
91+
This module will build on the last module. Instead of displaying a single file, we will download all of the files from a Dat into a local directory.
8192

82-
To download the files to the file system, instead of to a database, we will use the `file` option in `hyperdrive` and the [random-access-file](http://npmjs.org/random-access-file) module. We will also learn two new archive functions that make handling all the files a bit easier than the file stream in module #1.
93+
To download the files to the file system, we are going to use [mirror-folder](https://github.com/mafintosh/mirror-folder). [Read more](/using-fs) about how mirror-folder works with hyperdrive.
8394

84-
Setup will be the same as before (make sure you install random-access-file and stream-each this time):
95+
In practice, you should use [dat-storage](https://github.com/datproject/dat-storage) to do this as it'll be more efficient and keep the metadata on disk.
8596

86-
```bash
87-
mkdir module-2 && cd module-2
88-
npm init
89-
npm install --save hyperdrive memdb hyperdrive-archive-swarm random-access-file stream-each
90-
touch index.js
91-
```
92-
93-
The first part of the module will look the same. We will add random-access-file (and [stream-each](http://npmjs.org/stream-each) to make things easier). The only difference is that we have to specify the `file` option when creating our archive:
97+
Setup will be the same as before (make sure you install `mirror-folder`). The first part of the module will look the same.
9498

9599
```js
96-
var memdb = require('memdb')
97-
var Hyperdrive = require('hyperdrive')
98-
var Swarm = require('hyperdrive-archive-swarm')
99-
var raf = require('random-access-file') // this is new!
100-
var each = require('stream-each')
101-
102-
var link = process.argv[2]
103-
104-
var db = memdb()
105-
var drive = Hyperdrive(db)
106-
var archive = drive.createArchive(link, {
107-
file: function (name) {
108-
return raf(path.join('download', name)) // download into a "download" dir
109-
}
110-
})
111-
var swarm = Swarm(archive)
112-
```
100+
var ram = require('random-access-memory')
101+
var hyperdrive = require('hyperdrive')
102+
var discovery = require('hyperdiscovery')
103+
var mirror = require('mirror-folder')
113104

114-
Now that we are setup, we can work with the archive. The `archive.download` function downloads the file content (to wherever you specified in the file option). To download all the files, we will need a list of files and then we will call download on each of them. `archive.list` will give us the list of the files. We use the stream-each module to make it easy to iterate over each item in the archive, then exit when the stream is finished.
105+
var link = process.argv[2] // user inputs the dat link
106+
var dir = process.cwd() // download to cwd
115107

116-
```js
117-
var stream = archive.list({live: false}) // Use {live: false} for now to make the stream easier to handle.
118-
each(stream, function (entry, next) {
119-
archive.download(entry, function (err) {
120-
if (err) return console.error(err)
121-
console.log('downloaded', entry.name)
122-
next()
108+
var archive = hyperdrive(ram, link)
109+
archive.ready(function () {
110+
discovery(archive)
111+
112+
var progress = mirror({name: '/', fs: archive}, dir, function (err) {
113+
console.log('done downloading!')
114+
})
115+
progress.on('put', function (src) {
116+
console.log(src.name, 'downloaded')
123117
})
124-
}, function () {
125-
process.exit(0)
126118
})
127119
```
128120

129121
You should be able to run the module and see all our docs files in the `download` folder:
130122

131123
```bash
132-
node index.js 395e3467bb5b2fa083ee8a4a17a706c5574b740b5e1be6efd65754d4ab7328c2
124+
node index.js dat://<link>
133125
```

docs/cookbook/http.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Sharing files over HTTP
2+
3+
The Dat command line comes with a built in HTTP server. This is a cool demo because we can also see how version history works! The `--http` option works for files you are sharing *or* downloading.
4+
5+
(If you don't have dat command line installed, run `npm install -g dat`, or [see more info](intro#installation).)
6+
7+
## Serve over HTTP
8+
9+
Serve dat files on http by adding the `--http` option. For example, you can sync an existing dat:
10+
11+
```
12+
❯ dat sync --http
13+
dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639
14+
Sharing dat: 2 files (1.4 MB)
15+
Serving files over http at http://localhost:8080
16+
17+
2 connections | Download 0 B/s Upload 0 B/s
18+
```
19+
20+
Now visit [http://localhost:8080]() to see the files in your browser! The default http port is 8080. You should see a directory listing:
21+
22+
<img src="/assets/cli-http.png" alt="Dat HTTP viewer"/>
23+
24+
If your dat has an `index.html` page, that will be shown instead.
25+
26+
You can combine Dat's http support with our server tools to create a live updating website or place to share files publicly.
27+
28+
## Built-in Versioning
29+
30+
As you may know, Dat automatically versions all files. The HTTP display is an easy way to view version history:
31+
32+
**Use [localhost:8080/?version=2]() to view a specific version.**
33+
34+
## Live reloading
35+
36+
The Dat http viewer also comes with live reloading. If it detects a new version it will automatically reload with the new directory listing or page (as long as you aren't viewing a specific version in the url).
37+
38+
## Sparse Downloading
39+
40+
Dat supports *sparse*, or partial downloads, of datasets. This is really useful if you only want a single file from a large dat. Unfortunately, we haven't quite built a user interface for this into our applications. So you can hack around it!
41+
42+
This will allow you to download a single file from a larger dat, without downloading metadata or any other files.
43+
44+
First, start downloading our demo dat, make sure you include both the flags (`--http`, `--sparse`).
45+
46+
```
47+
❯ dat dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639 ./demo --http --sparse
48+
Cloning: 2 files (1.4 MB)
49+
Serving files over http at http://localhost:8080
50+
51+
3 connections | Download 0 B/s Upload 0 B/s
52+
```
53+
54+
The `--sparse` option tells Dat to only download files you specifically request. See how it works:
55+
56+
1. Check out your `./demo` folder, it should be empty.
57+
2. [Open the Dat](http://localhost:8080) in your browser.
58+
3. Click on a file to download.
59+
4. It should be in your folder now!
60+
61+
Pretty cool! You can use this hack to download only specific files or even older versions of files (if they've been saved somewhere).

docs/cookbook/tutorial.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Getting Started with Dat
2+
3+
In this tutorial we will go through the two main ways to use Dat, sharing data and downloading data. If possible, this is great to go through with a partner to see how Dat works across computers. Get Dat [installed](intro#installation) and get started!
4+
5+
Dat Desktop makes it easy for anyone to get started using Dat with user-friendly interface. If you are comfortable with the command line then you can install dat via npm. You can always switch apps later and keep your dats the same. Dat can share your files to anyone, it does not matter how they are using Dat.
6+
7+
## Command Line Tutorial
8+
9+
### Downloading Data
10+
11+
We made a demo folder we made just for this exercise. Inside the demo folder is a `dat.json` file and a gif. We shared these files via Dat and now you can download them with our dat key!
12+
13+
Similar to git, you do download somebody's dat by running `dat clone <link>`. You can also specify the directory:
14+
15+
```
16+
❯ dat clone dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639 ~/Downloads/dat-demo
17+
dat v13.5.0
18+
Created new dat in /Users/joe/Downloads/dat-demo/.dat
19+
Cloning: 2 files (1.4 MB)
20+
21+
2 connections | Download 614 KB/s Upload 0 B/s
22+
23+
dat sync complete.
24+
Version 4
25+
```
26+
27+
This will download our demo files to the `~/downloads/dat-demo` folder. These files are being shared by a server over Dat (to ensure high availability) but you may connect to any number of users also hosting the content.
28+
29+
You can also also view the files online: [datproject.org/778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639](https://datproject.org/778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639/). datproject.org can download files over Dat and display them on http as long as someone is hosting it. The website temporarily caches data for any visited links (do not view your dat on datproject.org if you do not want us caching your data).
30+
31+
### Sharing Data
32+
33+
We'll be creating a dat from a folder on your computer. If you are with a friend you can sync these files to their computer. Otherwise you can view them online via datproject.org to see how viewing a dat online works.
34+
35+
Find a folder on your computer to share. Any kind of files work with Dat but for now, make sure it's something you want to share with your friends. Dat can handle all sorts of files (Dat works with really big folders too!). We like cat pictures.
36+
37+
First, you can create a new dat inside that folder. Using the `dat create` command also walks us through making a `dat.json` file:
38+
39+
```
40+
❯ dat create
41+
Welcome to dat program!
42+
You can turn any folder on your computer into a Dat.
43+
A Dat is a folder with some magic.
44+
```
45+
46+
This will create a new (empty) dat. Dat will print a link, share this link to give others access to view your files.
47+
48+
Once we have our dat, run `dat share` to scan your files and sync them to the network. Share the link with your friend to instantly start downloading files.
49+
50+
You can also try viewing your files online. Go to [datproject.org](https://datproject.org/explore) and enter your link to preview on the top right. *(Some users, including me when writing this, may have trouble connecting to datproject.org initially. Don't be alarmed! It is something we are working on. Thanks.)*

docs/cookbook/using-fs.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Using the Hyperdrive FS
2+
3+
[Hyperdrive](https://github.com/mafintosh/hyperdrive), the core file system module in dat, exposes an API that mimics the Node fs API. This allows you to create modules that act on hyperdrive or a regular fs with the same API. We have several modules that we make use of this custom fs, such as [mirror-folder](https://github.com/mafintosh/mirror-folder).
4+
5+
Mirror folder can copy a regular directory to another regular directory:
6+
7+
```js
8+
var mirror = require('mirror-folder')
9+
10+
mirror('/source', '/dest', function (err) {
11+
console.log('mirror complete')
12+
})
13+
```
14+
15+
You can also copy a folder to an archive (this is how we do importing in Dat):
16+
17+
```js
18+
var archive = hyperdrive('/dir')
19+
mirror('/source', {name: '/', fs: archive}, function (err) {
20+
console.log('mirror complete')
21+
})
22+
```
23+
24+
### Creating Custom FS Modules
25+
26+
To create a module that uses a custom fs, you can default to the regular `fs` but also accept `fs` as an argument. For example, to print a file you could write this function:
27+
28+
```js
29+
function printFile(file, fs) {
30+
if (!fs) fs = require('fs')
31+
32+
fs.readFile(file, 'utf-8', function (err, data) {
33+
console.log(data)
34+
})
35+
}
36+
```
37+
38+
Then you could use this to print a file from a regular fs:
39+
40+
```js
41+
printFile('/data/hello-world.txt')
42+
```
43+
44+
Or from a hyperdrive archive:
45+
46+
```js
47+
var archive = hyperdrive('/data')
48+
printFile('/hello-world.txt', archive) // pass archive as the fs!
49+
```
50+
51+
## Modules!
52+
53+
See more examples of custom-fs modules:
54+
55+
* [mirror-folder](https://github.com/mafintosh/mirror-folder) - copy files from one fs to another fs (regular or custom)
56+
* [count-files](https://github.com/joehand/count-files) - count files in regular or custom fs.
57+
* [ftpfs](https://github.com/maxogden/ftpfs) - custom fs for FTPs
58+
* [bagit-fs](https://github.com/joehand/bagit-fs) - custom fs module for the BagIt spec.

0 commit comments

Comments
 (0)