This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
208 lines (196 loc) · 5.9 KB
/
storage.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Store Data
const fs = require('fs')
const lock = require('lock').Lock()
const path = require('path')
const pump = require('pump')
const split2 = require('split2')
const through2 = require('through2')
module.exports = {
account: simpleFiles('accounts'),
order: simpleFiles('orders'),
license: {
mkdirp: done => fs.mkdir(
path.join(process.env.DIRECTORY, 'licenses'),
{ recursive: true },
done
),
path: id => path.join(process.env.DIRECTORY, 'licenses', id)
},
project: simpleFiles('projects'),
email: simpleFiles('emails'),
token: simpleFiles('tokens'),
session: simpleFiles('sessions'),
showcase: simpleFiles('showcases'),
stripeID: simpleFiles('stripeIDs'),
signature: {
record: ({ date, signature, orderID }, callback) => {
lock('signatures', unlock => {
callback = unlock(callback)
fs.appendFile(
path.join(process.env.DIRECTORY, 'sigantures.csv'),
[date, signature, orderID].join(','),
callback
)
})
},
createReadStream: () => {
return pump(
fs.createReadStream(
path.join(process.env.DIRECTORY, 'sigantures.csv'),
'utf8'
),
split2(),
through2.obj((chunk, _, done) => {
const [date, signature, orderID] = chunk.split(',')
done(null, { date, signature, orderID })
})
)
}
},
lock
}
const account = module.exports.account
account.confirm = (handle, callback) => {
const properties = { confirmed: new Date().toISOString() }
account.update(handle, properties, callback)
}
const token = module.exports.token
token.use = (id, callback) => {
const file = token.filePath(id)
lock(file, unlock => {
callback = unlock(callback)
token.readWithoutLocking(id, (error, record) => {
/* istanbul ignore if */
if (error) return callback(error)
if (!record) return callback(null, null)
token.deleteWithoutLocking(id, error => {
/* istanbul ignore if */
if (error) return callback(error)
callback(null, record)
})
})
})
}
function simpleFiles (subdirectory, options) {
options = options || {}
const serialization = options.serialization
const filePath = id => path.join(process.env.DIRECTORY, subdirectory, id + '.json')
return {
write: (id, value, callback) => {
lock(filePath(id), unlock => writeWithoutLocking(id, value, unlock(callback)))
},
writeWithoutLocking,
read: (id, callback) => {
lock(filePath(id), unlock => readWithoutLocking(id, unlock(callback)))
},
readWithoutLocking,
createRawReadStream: id => {
return fs.createReadStream(filePath(id), 'utf8')
},
exists: (id, callback) => {
fs.access(filePath(id), error => {
if (error) {
if (error.code === 'ENOENT') {
return callback(null, false)
}
/* istanbul ignore next */
return callback(error)
}
callback(null, true)
})
},
update: (id, updater, callback) => {
const file = filePath(id)
lock(file, unlock => {
callback = unlock(callback)
readFile({ file, serialization }, (error, record) => {
/* istanbul ignore if */
if (error) return callback(error)
if (!record) return callback(null, null)
if (typeof updater === 'function') {
updater(record, error => {
if (error) return callback(error)
write()
})
} else {
Object.assign(record, updater)
write()
}
function write () {
writeFile({ file, data: record, serialization }, error => {
/* istanbul ignore if */
if (error) return callback(error)
callback(null, record)
})
}
})
})
},
list: callback => {
const directory = path.dirname(filePath('x'))
fs.readdir(directory, (error, entries) => {
/* istanbul ignore if */
if (error) return callback(error)
const ids = entries.map(entry => path.basename(entry, '.json'))
callback(null, ids)
})
},
delete: (id, callback) => {
lock(filePath(id), unlock => deleteWithoutLocking(id, unlock(callback)))
},
deleteWithoutLocking,
filePath
}
function writeWithoutLocking (id, value, callback) {
const file = filePath(id)
const directory = path.dirname(file)
fs.mkdir(directory, { recursive: true }, error => {
/* istanbul ignore if */
if (error) return callback(error)
writeFile({ file, data: value, serialization }, callback)
})
}
function readWithoutLocking (id, callback) {
readFile({ file: filePath(id), serialization }, callback)
}
function deleteWithoutLocking (id, callback) {
fs.unlink(filePath(id), error => {
if (error && error.code === 'ENOENT') return callback()
/* istanbul ignore next */
return callback(error)
})
}
}
function readFile (options, callback) {
const file = options.file
const serialization = options.serialization || JSON
fs.readFile(file, (error, data) => {
if (error) {
if (error.code === 'ENOENT') return callback(null, null)
/* istanbul ignore next */
return callback(error)
}
try {
var parsed = serialization.parse(data)
} catch (error) {
/* istanbul ignore next */
return callback(error)
}
return callback(null, parsed)
})
}
function writeFile (options, callback) {
const file = options.file
const data = options.data
const serialization = options.serialization || JSON
const flag = options.flag || 'w'
const stringified = serialization.stringify(data)
fs.writeFile(file, stringified, { flag }, error => {
if (error) {
if (error.code === 'EEXIST') return callback(null, false)
/* istanbul ignore next */
return callback(error, false)
}
callback(null, true)
})
}