-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
178 lines (139 loc) · 4.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var EE = require('events').EventEmitter
, qs = require('querystring')
, path = require('path')
, fs = require('fs')
var jrs = require('json-readdir-stream')
, through = require('through')
, mkdirp = require('mkdirp')
var normal = path.normalize
, join = path.join
var CWD = process.cwd()
module.exports = fsBack
function fsBack(_metaDir, _userDir, _tarballsDir, _storeDir) {
var backend = new EE
, tarballsDir
, storeDir
, userDir
, metaDir
tarballsDir = _tarballsDir ? normal(_tarballsDir) : join(CWD, 'tarballs')
storeDir = _storeDir ? normal(_storeDir) : join(CWD, 'store')
userDir = _userDir ? normal(_userDir) : join(CWD, 'users')
metaDir = _metaDir ? normal(_metaDir) : join(CWD, 'meta')
mkdirp.sync(tarballsDir)
mkdirp.sync(storeDir)
mkdirp.sync(userDir)
mkdirp.sync(metaDir)
backend.getUser = get(userDir)
backend.setUser = set(userDir, 'setUser')
backend.removeUser = remove(userDir, 'removeUser')
backend.createUserStream = streamAll(userDir)
backend.getMeta = get(metaDir)
backend.setMeta = set(metaDir, 'setMeta')
backend.removeMeta = remove(metaDir, 'removeMeta')
backend.createMetaStream = streamAll(metaDir)
backend.get = get(storeDir)
backend.set = set(storeDir, 'set')
backend.remove = remove(storeDir, 'remove')
backend.createStream = streamAll(storeDir)
backend.getTarball = getTarball
backend.setTarball = setTarball
backend.removeTarball = removeTarball
return backend
function get(dir) {
return function getData(_key, _done) {
var key = qs.escape(_key)
, done = _done || noop
readJson(join(dir, key), done)
}
}
function streamAll(dir) {
return function streamData(options) {
return jrs(dir, options).pipe(unescapeStream(options))
}
}
function set(dir, eventName) {
return function setData(_key, data, _done) {
var key = qs.escape(_key)
, done = _done || noop
, oldData
get(dir)(key, gotOld)
function gotOld(err, old) {
if(err) return done(err)
oldData = old
writeJson(join(dir, key), data, savedNew)
}
function savedNew(err, data) {
if(err) return done(err)
done(null, data, oldData)
backend.emit(eventName, key, data, oldData)
}
}
}
function remove(dir, eventName) {
return function removeData(_key, _done) {
var key = qs.escape(_key)
, done = _done || noop
, oldData
get(dir)(key, gotOld)
function gotOld(err, data) {
if(err) return done(err)
oldData = data
fs.unlink(join(dir, key + '.json'), removed)
}
function removed(err) {
if(err) return done(err)
done(null, oldData)
backend.emit(eventName, key, oldData)
}
}
}
function getTarball(name, version) {
return fs.createReadStream(
join(tarballsDir, qs.escape(name) + '@' + version + '.tgz')
)
}
function setTarball(name, version) {
return fs.createWriteStream(
join(tarballsDir, qs.escape(name) + '@' + version + '.tgz')
)
}
function removeTarball(name, version, callback) {
fs.unlink(join(tarballsDir, qs.escape(name) + '@' + version + '.tgz'), callback)
}
}
function writeJson(filename, data, ready) {
try {
var jsonData = JSON.stringify(data, null, 2)
} catch(e) {
return ready(e)
}
fs.writeFile(filename + '.json', jsonData, writeDone)
function writeDone(err) {
if(err) return ready(err)
ready(null, data)
}
}
function readJson(filename, ready) {
fs.readFile(filename + '.json', parseJson)
function parseJson(err, data) {
if(err) return err.code === 'ENOENT' ? ready(null, null) : ready(err)
try {
return ready(null, JSON.parse(data))
} catch(e) {
return ready(e)
}
}
}
function unescapeStream(options) {
if(options && !options.keys && typeof options.keys !== 'undefined') {
return through()
}
return through(function unescape(data) {
if(typeof data === 'object') {
data.key = qs.unescape(data.key)
return this.queue(data)
}
return this.queue(qs.unescape(data))
})
}
function noop() {}