aboutsummaryrefslogtreecommitdiffstats
path: root/api.md
blob: 092f3897daf68b8df36768933034736e8001f627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
## 1.0 Architecture Design

![dat-arch.001.jpg](arch.png)


  * dat: command-line
  * dat-desk: desktop application
  * dat-server: http and ui frontend
  * dat-js: JS api
  * hyperdrive: storage layer
  * discovery-swarm: swarm


## dat-js

A common JavaScript API for Dat.

```
var Dat = require('dat-js')
```

#### `var dat = Dat(opts, cb)`

Options

  * `db`: leveldb instance for underlying hyperdrive
  * `DISCOVERY_SERVER`: the discovery server to use. (default 'discovery1.publicbits.org')
  * `DAT_DOMAIN`: the domain to use (default 'dat.local')

#### `dat.link(dir, cb)`

Create a dat link for the given directory. Does not start sharing the dat. Must use `dat.join` to begin serving the link to the swarm.

```js
var linker = dat.link(dir)
var done = function (err, link) {
  if (err) throw err
  console.log('Done linking!', link)
}
var dir = '/Users/karissa/data/mydata'
var linker = dat.link(dir, done)
linker.on('progress', function (progress) {
  console.log('progress item', progress)
})
```

#### `dat.download(link, dir, cb)`

Download the given link to a given location. Get progress events from the stream. Progress events are the same as emitted by the `dat` object.

```js
var done = function (err) {
  if (err) throw err
  console.log('Done downloading!')
}
var link = 'dat://8a82dae10ee8384'
var dir = '/Users/karissa/data/mydata'
var downloader = dat.download(link, dir, done)
downloader.on('progress', function (progress) {
  console.log('progress item', progress)
})
```

#### `dat.join(link, cb)`

Join a swarm for the given link. Should be called after `link` or `download`. Throws error if data has not been downloaded or linked.

#### `dat.leave(link, cb)`

Leave the swarm for the given link.

#### `dat.on('progress', cb)`

Get all progress events from uploading and downloading data.

```js
dat.on('progress', function (progress) {
  console.log(progress)
})
```

## dat

Command-line interface for dat

#### `dat link DIR`

Create a new link for the contents of a directory and begin automatically serving the data to a swarm in the background.

  * `--foreground`: run the server in the foreground instead.

#### `dat download LINK DIR`

Download a link to a directory and begin automatically serving the data to a swarm in the background.

  * `--foreground`: run the server in the foreground instead.

#### `dat join LINK`

Starts serving the given link in a background progress. Must have already performed `link` or `download`.

  * `--foreground`: run the server in the foreground instead.

#### `dat leave LINK`

Stops serving the given link to the discovery swarm. Must have already performed `link` or `download`.

#### `dat ls`

View a static list of the current dat links that are served.

#### `dat mon`

Opens up real-time monitoring panel for viewing progress of running dats. Can provide optional parameter `dat mon LINK` to filter the monitor and logs for a given dat link.