├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output/ 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![deprecated](http://badges.github.io/stability-badges/dist/deprecated.svg)](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 | # multidat 8 | [![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7] 9 | [![downloads][8]][9] [![js-standard-style][10]][11] 10 | 11 | Manage multiple [dat][dat] instances in multiple locations. 12 | 13 | ## Usage 14 | ```js 15 | var Multidat = require('multidat') 16 | var toilet = require('toiletdb') 17 | 18 | var db = toilet('/tmp/dat') 19 | Multidat(db, function (err, multidat) { 20 | if (err) throw err 21 | 22 | multidat.create(opts, function (err, dat) { 23 | if (err) throw err 24 | 25 | var dats = multidat.list() 26 | console.log(dats) 27 | 28 | multidat.close(dat.archive.key, function (err) { 29 | if (err) throw err 30 | console.log() 31 | }) 32 | }) 33 | }) 34 | ``` 35 | 36 | ## Error handling 37 | If there is an error initializing a dat, instead of the whole process failing, an error object with attached `.dir` property will be pushed into the list of dats instead. That means when consuming `multidat.list()`, you should check for errors: 38 | 39 | ```js 40 | var dats = multidat.list() 41 | dats.forEach(function (dat) { 42 | if (dat instanceof Error) { 43 | var err = dat 44 | console.log('failed to initialize dat in %s: %s', err.dir, err.message) 45 | } 46 | }) 47 | ``` 48 | 49 | This way you can decide for yourself whether an individual initialization failure should cause the whole process to fail or not. 50 | 51 | ## API 52 | ### Multidat(db, opts, callback(err, multidat)) 53 | Creat a new Multidat instance. Takes a `toiletdb` instance and a callback. 54 | 55 | Options: 56 | 57 | - `Dat`: Use provided dat factory instead of [dat-node](https://github.com/datproject/dat-node) 58 | 59 | ### multidat.create(dir, opts, callback(err, dat)) 60 | Create a new `dat` archive. 61 | 62 | ### dats = multidat.list() 63 | List all available `dat` archives. 64 | 65 | ### multidat.close(key, callback(err)) 66 | Close and remove a `dat` archive. 67 | 68 | ### multidat.readManifest(dat, callback(err, manifest)) 69 | Read the `dat.json` file from the `dat` archive. This method is expected to be 70 | deprecated once `dat` archives provide a built-in method to return archives. 71 | 72 | ## Why? 73 | This package exists to manage multiple `dat` archives in different directories. 74 | The [dat-node][dat-node] package is mostly stateless; all state is persisted 75 | into the archives themselves. This package acts as a layer on top to keep track 76 | of where archives are located and manage them between sessions. 77 | 78 | ## When not to use this 79 | If you're running a server, it's usually enough to run 80 | [mafintosh/hypercore-archiver](https://github.com/mafintosh/hypercore-archiver) 81 | which is more consistent and simpler. If you're building a tool that only needs 82 | to manage a single dat archive at the time it's recommended to use 83 | [datproject/dat-node][dat-node] instead. 84 | 85 | ## See Also 86 | - [datproject/dat-node][dat-node] 87 | - [mafintosh/hyperdrive](https://github.com/mafintosh/hyperdrive) 88 | - [mafintosh/hypercore-archiver](https://github.com/mafintosh/hypercore-archiver) 89 | - [datproject/multidrive](https://github.com/datproject/multidrive) 90 | - [juliangruber/hyperdrive-stats](https://github.com/juliangruber/hyperdrive-stats) 91 | - [juliangruber/dat-encoding](https://github.com/juliangruber/dat-encoding) 92 | 93 | ## License 94 | [MIT](https://tldrlegal.com/license/mit-license) 95 | 96 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 97 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 98 | [2]: https://img.shields.io/npm/v/multidat.svg?style=flat-square 99 | [3]: https://npmjs.org/package/multidat 100 | [4]: https://img.shields.io/travis/datproject/multidat/master.svg?style=flat-square 101 | [5]: https://travis-ci.org/datproject/multidat 102 | [6]: https://img.shields.io/codecov/c/github/datproject/multidat/master.svg?style=flat-square 103 | [7]: https://codecov.io/github/datproject/multidat 104 | [8]: http://img.shields.io/npm/dm/multidat.svg?style=flat-square 105 | [9]: https://npmjs.org/package/multidat 106 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 107 | [11]: https://github.com/feross/standard 108 | [dat]: https://github.com/datproject/dat 109 | [dat-node]: https://github.com/datproject/dat-node 110 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var multidrive = require('multidrive') 2 | var explain = require('explain-error') 3 | var parse = require('fast-json-parse') 4 | var assert = require('assert') 5 | var dat = require('dat-node') 6 | var extend = require('xtend') 7 | 8 | module.exports = Multidat 9 | 10 | function Multidat (db, opts, cb) { 11 | if (!cb) { 12 | cb = opts 13 | opts = {} 14 | } 15 | 16 | assert.equal(typeof db, 'object', 'multidat: db should be type object') 17 | assert.equal(typeof cb, 'function', 'multidat: cb should be type function') 18 | 19 | var datFactory = opts.dat || dat 20 | 21 | multidrive(db, createArchive, closeArchive, function (err, drive) { 22 | if (err) return cb(explain(err, 'multidat: error creating multidrive')) 23 | var multidat = { 24 | readManifest: readManifest, 25 | create: create, 26 | disconnect: drive.disconnect, 27 | close: drive.close, 28 | list: drive.list 29 | } 30 | cb(null, multidat) 31 | 32 | function create (dir, opts, cb) { 33 | if (!cb) { 34 | cb = opts 35 | opts = {} 36 | } 37 | 38 | assert.equal(typeof dir, 'string', 'multidrive.create: dir should be a string') 39 | assert.equal(typeof opts, 'object', 'multidrive.create: opts should be a object') 40 | assert.equal(typeof cb, 'function', 'multidrive.create: cb should be a function') 41 | 42 | var data = { 43 | dir: dir, 44 | opts: opts 45 | } 46 | drive.create(data, cb) 47 | } 48 | }) 49 | 50 | function createArchive (data, done) { 51 | var dir = data.dir 52 | var _opts = extend(opts, data.opts) 53 | datFactory(dir, _opts, done) 54 | } 55 | 56 | function closeArchive (dat, done) { 57 | dat.close(done) 58 | } 59 | } 60 | 61 | function readManifest (dat, cb) { 62 | dat.archive.readFile('dat.json', function (err, buf) { 63 | if (err) return cb(err) 64 | var res = parse(buf.toString()) 65 | if (res.err) return cb(explain(res.err, "multidat.readManifest: couldn't parse dat.json file")) 66 | cb(null, res.value) 67 | }) 68 | } 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "multidat", 3 | "description": "Manage multiple dat instances", 4 | "repository": "datproject/multidat", 5 | "version": "5.1.1", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node .", 9 | "test": "standard && npm run deps && nyc node test.js", 10 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov" 11 | }, 12 | "dependencies": { 13 | "dat-node": "^3.2.0", 14 | "explain-error": "^1.0.3", 15 | "fast-json-parse": "^1.0.2", 16 | "multidrive": "^5.2.0", 17 | "xtend": "^4.0.1" 18 | }, 19 | "devDependencies": { 20 | "copy-dir": "^0.3.0", 21 | "dependency-check": "^2.7.0", 22 | "mkdirp": "^0.5.1", 23 | "nyc": "^10.0.0", 24 | "rimraf": "^2.5.4", 25 | "standard": "^8.6.0", 26 | "tape": "^4.6.3", 27 | "toiletdb": "^1.1.0" 28 | }, 29 | "keywords": [ 30 | "dat", 31 | "hyperdrive", 32 | "manage" 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var toilet = require('toiletdb/inmemory') 2 | var rimraf = require('rimraf') 3 | var mkdirp = require('mkdirp') 4 | var Dat = require('dat-node') 5 | var path = require('path') 6 | var tape = require('tape') 7 | 8 | var Multidat = require('./') 9 | var noop = function () {} 10 | 11 | tape('multidat = Multidat()', function (t) { 12 | t.test('should assert input types', function (t) { 13 | t.plan(2) 14 | t.throws(Multidat.bind(null), /object/) 15 | t.throws(Multidat.bind(null, {}), /function/) 16 | }) 17 | }) 18 | 19 | tape('multidat.create()', function (t) { 20 | t.test('should assert input types', function (t) { 21 | t.plan(4) 22 | var db = toilet({}) 23 | Multidat(db, function (err, multidat) { 24 | t.ifError(err, 'no error') 25 | t.throws(multidat.create.bind(null), 'string') 26 | t.throws(multidat.create.bind(null, ''), 'function') 27 | t.throws(multidat.create.bind(null, 123, noop), 'object') 28 | }) 29 | }) 30 | 31 | t.test('should create a dat', function (t) { 32 | t.plan(4) 33 | var db = toilet({}) 34 | Multidat(db, function (err, multidat) { 35 | t.ifError(err, 'no error') 36 | var location = path.join('/tmp', String(Date.now())) 37 | mkdirp.sync(location) 38 | multidat.create(location, function (err, dat) { 39 | t.ifError(err, 'no error') 40 | t.equal(typeof dat, 'object', 'dat exists') 41 | dat.close(function (err) { 42 | t.ifError(err, 'no error') 43 | rimraf.sync(location) 44 | }) 45 | }) 46 | }) 47 | }) 48 | 49 | t.test('created dat should not be exposed to the network', function (t) { 50 | t.plan(3) 51 | var db = toilet({}) 52 | Multidat(db, function (err, multidat) { 53 | t.ifError(err, 'no error') 54 | var location = path.join('/tmp', String(Date.now())) 55 | mkdirp.sync(location) 56 | multidat.create(location, function (err, dat) { 57 | t.ifError(err, 'no error') 58 | dat.close(function (err) { 59 | t.ifError(err, 'no error') 60 | rimraf.sync(location) 61 | }) 62 | }) 63 | }) 64 | }) 65 | }) 66 | 67 | tape('multidat.list()', function (t) { 68 | t.test('should list all dats', function (t) { 69 | t.plan(4) 70 | 71 | var db = toilet({}) 72 | Multidat(db, function (err, multidat) { 73 | t.ifError(err, 'no error') 74 | 75 | var location = path.join('/tmp', String(Date.now())) 76 | mkdirp.sync(location) 77 | multidat.create(location, function (err, dat) { 78 | t.ifError(err, 'no error') 79 | var dats = multidat.list() 80 | t.equal(dats.length, 1, 'one dat') 81 | dat.close(function (err) { 82 | t.ifError(err, 'no error') 83 | rimraf.sync(location) 84 | }) 85 | }) 86 | }) 87 | 88 | /* t.test('creation error', function (t) { 89 | t.plan(3) 90 | var db = toilet({}) 91 | Multidat(db, {}, function (err, multidat) { 92 | t.ifError(err, 'no error') 93 | multidat.create('/non/existing/path', function (err, dat) { 94 | t.ok(err, 'error') 95 | t.notOk(dat, 'no dat') 96 | }) 97 | }) 98 | }) */ 99 | }) 100 | }) 101 | 102 | tape('multidat.close()', function (t) { 103 | t.test('should be able to close a dat by its key', function (t) { 104 | t.plan(4) 105 | 106 | var db = toilet({}) 107 | Multidat(db, function (err, multidat) { 108 | t.ifError(err, 'no error') 109 | 110 | var location = path.join('/tmp', String(Date.now())) 111 | mkdirp.sync(location) 112 | multidat.create(location, function (err, dat) { 113 | t.ifError(err, 'no error') 114 | multidat.close(dat.key, function (err) { 115 | t.ifError(err, 'no error') 116 | var dats = multidat.list() 117 | t.equal(dats.length, 0, 'no dats') 118 | rimraf.sync(location) 119 | }) 120 | }) 121 | }) 122 | }) 123 | }) 124 | 125 | tape('multidat.readManifest', function (t) { 126 | t.test('should read a manifest if there is one', function (t) { 127 | var sourceLocation = path.join('/tmp', String(Date.now())) 128 | Dat(sourceLocation, { indexing: false }, function (err, sourceDat) { 129 | t.ifError(err, 'no error') 130 | sourceDat.joinNetwork() 131 | var ws = sourceDat.archive.createWriteStream('dat.json') 132 | ws.end(JSON.stringify({ name: 'hello-planet' })) 133 | 134 | var db = toilet({}) 135 | Multidat(db, function (err, multidat) { 136 | t.ifError(err, 'no error') 137 | 138 | var location = path.join('/tmp', String(Date.now())) 139 | mkdirp.sync(location) 140 | 141 | multidat.create(location, { key: sourceDat.key }, function (err, dat) { 142 | t.ifError(err, 'no error') 143 | 144 | dat.joinNetwork() 145 | multidat.readManifest(dat, function (err, manifest) { 146 | t.ifError(err, 'no err') 147 | t.equal(typeof manifest, 'object', 'right type') 148 | t.equal(manifest.name, 'hello-planet', 'right value') 149 | dat.close(function () { 150 | sourceDat.close(function () { 151 | rimraf.sync(location) 152 | rimraf.sync(sourceLocation) 153 | t.end() 154 | }) 155 | }) 156 | }) 157 | }) 158 | }) 159 | }) 160 | }) 161 | }) 162 | --------------------------------------------------------------------------------