-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileStore.js
59 lines (48 loc) · 1.63 KB
/
fileStore.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
var fs = require('fs-promise')
var PassThrough = require('stream').PassThrough
function writeStreamToFile (filename, stream) {
return new Promise(function (resolve, reject) {
var file = fs.createWriteStream(filename)
stream.on('error', reject)
stream.on('end', resolve)
stream.pipe(file)
})
}
module.exports = function (options) {
var path = typeof options === 'object' && Object.prototype.hasOwnProperty.call(options, 'url') ? options.url : undefined
return {
filename: function (url) {
return path + '/' + encodeURIComponent(url)
},
responseExists: function (url) {
return fs.exists(this.filename(url) + '.json')
},
writeResponse: function (url, response) {
var filename = this.filename(url)
var body = response.body
delete response.body
var fileStream = body.pipe(new PassThrough())
var responseStream = body.pipe(new PassThrough())
var responseJson = JSON.stringify(response, null, 2)
return fs
.mkdirs(path)
.then(function () {
writeStreamToFile(filename, fileStream).catch(function (e) {
console.error((e && e.stack) || e)
}).then(function () {
return fs.writeFile(filename + '.json', responseJson)
})
response.body = responseStream
return response
})
},
readResponse: function (url) {
var filename = this.filename(url)
return fs.readFile(filename + '.json', 'utf-8').then(function (contents) {
var response = JSON.parse(contents)
response.body = fs.createReadStream(filename)
return response
})
}
}
}