This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
68 lines (57 loc) · 1.76 KB
/
index.js
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
var multidrive = require('multidrive')
var explain = require('explain-error')
var parse = require('fast-json-parse')
var assert = require('assert')
var dat = require('dat-node')
var extend = require('xtend')
module.exports = Multidat
function Multidat (db, opts, cb) {
if (!cb) {
cb = opts
opts = {}
}
assert.equal(typeof db, 'object', 'multidat: db should be type object')
assert.equal(typeof cb, 'function', 'multidat: cb should be type function')
var datFactory = opts.dat || dat
multidrive(db, createArchive, closeArchive, function (err, drive) {
if (err) return cb(explain(err, 'multidat: error creating multidrive'))
var multidat = {
readManifest: readManifest,
create: create,
disconnect: drive.disconnect,
close: drive.close,
list: drive.list
}
cb(null, multidat)
function create (dir, opts, cb) {
if (!cb) {
cb = opts
opts = {}
}
assert.equal(typeof dir, 'string', 'multidrive.create: dir should be a string')
assert.equal(typeof opts, 'object', 'multidrive.create: opts should be a object')
assert.equal(typeof cb, 'function', 'multidrive.create: cb should be a function')
var data = {
dir: dir,
opts: opts
}
drive.create(data, cb)
}
})
function createArchive (data, done) {
var dir = data.dir
var _opts = extend(opts, data.opts)
datFactory(dir, _opts, done)
}
function closeArchive (dat, done) {
dat.close(done)
}
}
function readManifest (dat, cb) {
dat.archive.readFile('dat.json', function (err, buf) {
if (err) return cb(err)
var res = parse(buf.toString())
if (res.err) return cb(explain(res.err, "multidat.readManifest: couldn't parse dat.json file"))
cb(null, res.value)
})
}