forked from dat-ecosystem/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (123 loc) · 3.47 KB
/
index.js
File metadata and controls
139 lines (123 loc) · 3.47 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var path = require('path')
var meta = require(path.join(__dirname, 'lib', 'meta.js'))
var commands = require(path.join(__dirname, 'lib', 'commands'))
var getPort = require(path.join(__dirname, 'lib', 'get-port'))
var backend = require(path.join(__dirname, 'lib', 'backend'))
var request = require('request').defaults({json: true})
var fs = require('fs')
var tty = require('tty')
module.exports = Dat
// new Dat(cb)
// new Dat({foo: bar})
// new Dat({foo: bar}, cb)
// new Dat('./foo)
// new Dat('./foo', cb)
// new Dat('./foo', {foo: bar})
// new Dat('./foo', {foo: bar}, cb)
function Dat(dir, opts, onReady) {
var self = this
// if 'new' was not used
if (!(this instanceof Dat)) return new Dat(dir, opts, onReady)
if (typeof dir === 'function') {
onReady = dir
opts = {}
dir = process.cwd()
}
if (typeof dir === 'object') {
onReady = opts
opts = dir
dir = process.cwd()
}
if (typeof opts === 'function') {
onReady = opts
opts = {}
}
if (!onReady) onReady = function(){}
// TODO figure out more descriptive names/API for these
// read dat dir but don't init empty database
if (typeof opts.init === 'undefined') opts.init = true
// read dat dir but don't read database
if (typeof opts.storage === 'undefined') opts.storage = true
this.lockRetries = 0
this.retryLimit = 3
this.dir = dir
this.opts = opts
var paths = this.paths()
this._backend = backend(this)
if (!opts.storage) {
self.meta = meta(self, function(err) {
onReady()
})
} else {
function read() {
readPort(paths.port, opts, function(err) {
// ignore err
loadMeta()
})
}
if (!tty.isatty(0)) setTimeout(read, 2000)
else read()
}
function loadMeta() {
self.meta = meta(self, function(err) {
if (err) return init()
self._storage(opts, function(err) {
if (err && self.lockRetries < self.retryLimit) {
readPort(paths.port, opts, function(err) {
// ignore err
loadMeta()
})
self.lockRetries++
return
}
init()
})
})
}
function init() {
commands._ensureExists({ path: dir }, function (err) {
if (err) {
if (!opts.init) return serve()
return self.init(opts, function(err) {
if (err) return onReady(err)
if (typeof opts.serve === 'undefined') opts.serve = true
serve()
})
}
if (typeof opts.serve === 'undefined') opts.serve = true
return serve()
})
}
function serve() {
if (opts.serve) {
self._storage(opts, function(err) {
if (err) return onReady(err)
self.serve(opts, onReady)
})
return
}
onReady()
}
}
function readPort(portPath, opts, cb) {
getPort.readPort(portPath, function(err, port) {
if (err) return cb(err)
var adminu = opts.adminUser || process.env["DAT_ADMIN_USER"]
var adminp = opts.adminPass || process.env["DAT_ADMIN_PASS"]
var creds = ''
if (adminu && adminp) creds = adminu + ':' + adminp + '@'
var datAddress = 'http://' + creds + '127.0.0.1:' + port
request(datAddress + '/_manifest', function(err, resp, json) {
if (err || !json.methods) {
// assume PORT to be invalid
return fs.unlink(portPath, cb)
}
// otherwise initialize in networked mode
opts.serve = false
opts.remoteAddress = datAddress
opts.manifest = json
cb()
})
})
}
Dat.prototype = commands