forked from dat-ecosystem/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (135 loc) · 4.25 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var fs = require('fs')
var tty = require('tty')
var path = require('path')
var os = require('os')
var debug = require('debug')('dat.init')
var request = require('request').defaults({json: true})
var transformations = require('./lib/transformations')
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 datVersion = require(path.join(__dirname, 'package.json')).version
module.exports = Dat
// new 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)
this.version = datVersion
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 (typeof opts === 'undefined') 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 || opts.path || process.cwd()
this.opts = opts
var paths = this.paths(dir)
debug(JSON.stringify(opts))
readDatJSON(function(err, data) {
if (err) throw err // TODO: emit when Dat is becomes an eventemitter
self.package = data
if (data.transformations.put) self.beforePut = transformations(data.transformations.put)
if (data.transformations.get) self.afterGet = transformations(data.transformations.get)
if (!opts.storage) {
self.meta = meta(self, function(err) {
onReady()
})
} else {
function read() {
readPort(paths.port, opts, function(err) {
// ignore err
loadMeta()
})
}
// windows server fails when timeout is lower than 2000
// timeout if called from CLI && is not a mac
if (!tty.isatty(0) && !(os.platform().match('darwin'))) 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 onReady()
return self.init(opts, onReady)
} else {
onReady()
}
})
}
function readDatJSON(cb) {
fs.readFile(path.join(dir, 'dat.json'), 'utf-8', function(err, data) {
if (err && err.code !== 'ENOENT') return cb(err)
try {
data = JSON.parse(data || '{}')
} catch (err) {
return cb(new Error('Invalid dat.json file'))
}
// normalize
if (!data.transformations) data.transformations = {}
if (Array.isArray(data.transformations)) data.transformations = {put:data.transformations}
cb(null, data)
})
}
}
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 + '/api/manifest', function(err, resp, json) {
if (err || !json.methods) {
// assume PORT to be invalid
return fs.unlink(portPath, cb)
}
opts.remoteAddress = datAddress
opts.manifest = json
cb()
})
})
}
Dat.prototype = commands