├── .gitignore
├── .travis.yml
├── package.json
├── LICENSE
├── README.md
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - '0.10'
4 | - '0.12'
5 | - '4'
6 | - '6'
7 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "dat-ls",
3 | "version": "2.1.0",
4 | "description": "Small program that lists all the changes in a dat",
5 | "main": "index.js",
6 | "bin": {
7 | "dat-ls": "./index.js"
8 | },
9 | "dependencies": {
10 | "dat-encoding": "^4.0.2",
11 | "hyperdiscovery": "^6.0.1",
12 | "hyperdrive": "^9.2.3",
13 | "pretty-bytes": "^4.0.2",
14 | "random-access-memory": "^2.4.0"
15 | },
16 | "devDependencies": {
17 | "standard": "^8.5.0"
18 | },
19 | "scripts": {
20 | "test": "standard"
21 | },
22 | "repository": {
23 | "type": "git",
24 | "url": "https://github.com/mafintosh/dat-ls.git"
25 | },
26 | "author": "Mathias Buus (@mafintosh)",
27 | "license": "MIT",
28 | "bugs": {
29 | "url": "https://github.com/mafintosh/dat-ls/issues"
30 | },
31 | "homepage": "https://github.com/mafintosh/dat-ls"
32 | }
33 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Mathias Buus
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://dat-ecosystem.org/)
2 |
3 | More info on active projects and modules at [dat-ecosystem.org](https://dat-ecosystem.org/)
4 |
5 | ---
6 |
7 | # dat-ls
8 |
9 | Small program that lists all the changes in a dat
10 |
11 | ```
12 | npm install -g dat-ls
13 | ```
14 |
15 | [](http://travis-ci.org/mafintosh/dat-ls)
16 |
17 | ## Usage
18 |
19 | ```
20 | dat-ls
21 | ```
22 |
23 | Here is an example
24 |
25 | ```
26 | > dat-ls 8a1b6864f290a1204a3fe048ced34865a24f5fdef4ce3753bab613f66636587d
27 |
28 | Dat contains 6 changes
29 |
30 | [put] /
31 | [put] LICENSE (1.08 kB, 1 blocks)
32 | [put] README.md (145 B, 1 blocks)
33 | [put] index.js (1.18 kB, 1 blocks)
34 | [put] package.json (648 B, 1 blocks)
35 |
36 | Total content size: 3.05 kB
37 | ```
38 |
39 | Per default it'll exit after reading the first snapshot of the change feed.
40 | To keep listening for changes use the `--live` flag
41 |
42 | ```
43 | > dat-ls 8a1b6864f290a1204a3fe048ced34865a24f5fdef4ce3753bab613f66636587d --live
44 | ```
45 |
46 | ## License
47 |
48 | MIT
49 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | var swarm = require('hyperdiscovery')
4 | var hyperdrive = require('hyperdrive')
5 | var prettyBytes = require('pretty-bytes')
6 | var ram = require('random-access-memory')
7 | var encoding = require('dat-encoding')
8 |
9 | var key = process.argv[2]
10 | var live = process.argv.indexOf('--live') > -1 || process.argv.indexOf('-l') > -1
11 |
12 | if (!key) {
13 | console.error('Usage: dat-ls [key]')
14 | process.exit(1)
15 | }
16 |
17 | try {
18 | key = encoding.toStr(key)
19 | } catch (e) {
20 | console.error('Invalid Dat key')
21 | process.exit(1)
22 | }
23 |
24 | var archive = hyperdrive(ram, key, {sparse: true})
25 | var size = 0
26 |
27 | archive.ready(function () {
28 | swarm(archive)
29 | archive.metadata.update(run)
30 | })
31 |
32 | function run () {
33 | var zeros = '000000000000'
34 | var files = {}
35 |
36 | var rs = archive.history({
37 | live: live
38 | })
39 |
40 | var seq = 0
41 |
42 | console.log('Dat contains %d changes\n', archive.metadata.length)
43 | if (!live) {
44 | var digits = Math.log(archive.metadata.length) / Math.log(10)
45 | if (digits !== Math.ceil(digits)) digits = Math.ceil(digits)
46 | else digits++
47 | zeros = zeros.slice(0, digits)
48 | }
49 |
50 | rs.on('data', function (data) {
51 | var s = (seq++).toString()
52 | s = zeros.slice(s.length) + s
53 |
54 | if (data.type === 'put') {
55 | // TODO: delete size for later deletes
56 | if (files[data.name]) size -= files[data.name].value.size
57 |
58 | size += data.value.size
59 | files[data.name] = data
60 | }
61 | if (data.type === 'del') {
62 | size -= files[data.name].value.size
63 | delete files[data.name]
64 | }
65 |
66 | switch (data.type) {
67 | case 'put': return console.log(s + ' [put] %s (%s, %s %s)', data.name, prettyBytes(data.value.size), data.value.blocks, data.value.blocks === 1 ? 'block' : 'blocks')
68 | case 'del': return console.log(s + ' [del] %s', data.name || '(empty)')
69 | }
70 |
71 | console.log(s + ' [' + data.type + ']', data.name)
72 | })
73 |
74 | rs.on('end', function () {
75 | console.log()
76 | console.log('Total content size: %s (%s metadata)', prettyBytes(size), prettyBytes(archive.metadata.byteLength))
77 | process.exit(0)
78 | })
79 | }
80 |
--------------------------------------------------------------------------------