-
Notifications
You must be signed in to change notification settings - Fork 84
/
record.js
51 lines (44 loc) · 1.34 KB
/
record.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
// Copyright 2019 SmugMug, Inc.
// Licensed under the terms of the MIT license. Please see LICENSE file in the project root for terms.
var Promise = require('bluebird');
var buffer = require('./buffer');
var path = require('path');
var ejs = require('ejs');
var fs = require('fs');
var debug = require('debug')('yakbak:record');
/**
* Read and pre-compile the tape template.
* @type {Function}
* @private
*/
var render = ejs.compile(fs.readFileSync(path.resolve(__dirname, '../src/tape.ejs'), 'utf8'));
/**
* Record the http interaction between `req` and `res` to disk.
* The format is a vanilla node module that can be used as
* an http.Server handler.
* @param {http.ClientRequest} req
* @param {http.IncomingMessage} res
* @param {String} filename
* @returns {Promise.<String>}
*/
module.exports = function (req, res, filename) {
return buffer(res).then(function (body) {
return render({ req: req, res: res, body: body });
}).then(function (data) {
return write(filename, data);
}).then(function () {
return filename;
});
};
/**
* Write `data` to `filename`. Seems overkill to "promisify" this.
* @param {String} filename
* @param {String} data
* @returns {Promise}
*/
function write(filename, data) {
return Promise.fromCallback(function (done) {
debug('write', filename);
fs.writeFile(filename, data, done);
});
}