├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 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 | # dat-next-next 2 | 3 | Experimental quick-n-dirty file sharing tool on top of dat 4 | 5 | ``` 6 | npm install dat-next-next 7 | ``` 8 | 9 | ## Usage 10 | 11 | One one computer 12 | 13 | ``` 14 | cd cool-directory 15 | dat-next-next 16 | ``` 17 | 18 | On another 19 | 20 | ``` 21 | mkdir cool-directory-clone 22 | cd cool-directory-clone 23 | dat-next-next 24 | ``` 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | process.title = 'dat-next-next' 4 | 5 | var discovery = require('hyperdiscovery') 6 | var storage = require('dat-storage') 7 | var hyperdrive = require('hyperdrive') 8 | var mirror = require('mirror-folder') 9 | var minimist = require('minimist') 10 | var pretty = require('prettier-bytes') 11 | var speedometer = require('speedometer') 12 | var diff = require('ansi-diff-stream')() 13 | var path = require('path') 14 | 15 | var downloadSpeed = speedometer() 16 | var uploadSpeed = speedometer() 17 | 18 | var argv = minimist(process.argv.slice(2), { 19 | default: {utp: true, watch: true}, 20 | boolean: ['utp', 'watch', 'live'] 21 | }) 22 | 23 | var key = argv._[0] 24 | var msg = 'Syncing dat ...' 25 | 26 | diff.pipe(process.stdout) 27 | 28 | if (key) download(new Buffer(key, 'hex')) 29 | else upload() 30 | 31 | function download (key) { 32 | 33 | var archive = hyperdrive(storage('.'), key, {latest: true}) 34 | var modified = false 35 | 36 | archive.on('content', function () { 37 | archive.content.on('clear', function () { 38 | modified = true 39 | }) 40 | 41 | archive.content.on('download', function (index, data) { 42 | modified = true 43 | downloadSpeed(data.length) 44 | }) 45 | 46 | archive.content.on('upload', function (index, data) { 47 | uploadSpeed(data.length) 48 | }) 49 | }) 50 | 51 | archive.on('sync', function () { 52 | msg = 'Dat version is fully synced.' 53 | log() 54 | if (modified && !argv.live) process.exit() 55 | msg += ' Waiting for updates ...' 56 | log() 57 | }) 58 | 59 | archive.on('update', function () { 60 | msg = 'Dat was updated, syncing ...' 61 | }) 62 | 63 | archive.on('ready', function () { 64 | discovery(archive, {live: true, utp: !!argv.utp}).on('connection', onconnection) 65 | log() 66 | setInterval(log, 500) 67 | }) 68 | 69 | function log () { 70 | if (argv.debug) return 71 | diff.write( 72 | msg + '\n' + 73 | 'Key is: ' + archive.key.toString('hex') + '\n' + 74 | 'Downloading at ' + pretty(downloadSpeed()) + '/s, Uploading at ' + pretty(uploadSpeed()) + ' (' + archive.metadata.peers.length + ' peers)' 75 | ) 76 | } 77 | } 78 | 79 | 80 | function upload () { 81 | var archive = hyperdrive(storage('.'), {indexing: true, latest: true}) 82 | 83 | archive.on('ready', function () { 84 | if (!archive.writable) { 85 | archive.close(download) 86 | return 87 | } 88 | 89 | archive.content.on('upload', function (index, data) { 90 | uploadSpeed(data.length) 91 | }) 92 | 93 | console.log('Sharing', process.cwd()) 94 | console.log('Key is: ' + archive.key.toString('hex')) 95 | 96 | var carusel = [] 97 | 98 | discovery(archive, {live: true, utp: !!argv.utp}).on('connection', onconnection) 99 | 100 | var progress = mirror(process.cwd(), {name: '/', fs: archive}, {ignore: ignore, watch: argv.watch, dereference: true}) 101 | 102 | progress.on('put', function (src, dst) { 103 | if (carusel.length === 8) carusel.pop() 104 | carusel.unshift('Adding: ' + dst.name) 105 | }) 106 | 107 | progress.on('del', function (dst) { 108 | if (carusel.length === 8) carusel.pop() 109 | carusel.unshift('Removing: ' + dst.name) 110 | }) 111 | 112 | log() 113 | setInterval(log, 500) 114 | 115 | function log () { 116 | if (argv.debug) return 117 | 118 | diff.write( 119 | (carusel.length ? '\n' : '') + carusel.join('\n') + (carusel.length ? '\n\n' : '') + 120 | 'Uploading at ' + pretty(uploadSpeed()) + ' (' + archive.metadata.peers.length + ' peers)' 121 | ) 122 | } 123 | }) 124 | } 125 | 126 | function ignore (name, st) { 127 | if (st && st.isDirectory()) return true // ignore dirs 128 | if (name.indexOf('.DS_Store') > -1) return true 129 | if (name.indexOf('.dat') > -1) return true 130 | return false 131 | } 132 | 133 | function onconnection (c, info) { 134 | if (argv.debug) { 135 | console.log('New connection:', info) 136 | c.on('error', function (err) { 137 | console.log('Connection error:', err) 138 | }) 139 | c.on('close', function () { 140 | console.log('Connection closed') 141 | }) 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dat-next-next", 3 | "version": "0.0.24", 4 | "description": "Experimental quick-n-dirty file sharing tool on top of dat", 5 | "main": "index.js", 6 | "dependencies": { 7 | "ansi-diff-stream": "^1.2.0", 8 | "dat-storage": "^1.0.0", 9 | "hyperdiscovery": "^1.3.0", 10 | "hyperdrive": "^9.1.0", 11 | "minimist": "^1.2.0", 12 | "mirror-folder": "^1.2.2", 13 | "prettier-bytes": "^1.0.3", 14 | "speedometer": "^1.0.0" 15 | }, 16 | "devDependencies": {}, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/mafintosh/dat-next-next.git" 20 | }, 21 | "author": "Mathias Buus (@mafintosh)", 22 | "bin": { 23 | "dat-next-next": "./index.js" 24 | }, 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/mafintosh/dat-next-next/issues" 28 | }, 29 | "homepage": "https://github.com/mafintosh/dat-next-next" 30 | } 31 | --------------------------------------------------------------------------------