wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
'use strict';
exports.__esModule = true;
var fs = require("fs");
var path_1 = require("path");
var crypto_1 = require("crypto");
function jsonParse(data, cb) {
    var result = null;
    try {
        result = JSON.parse(data);
    }
    catch (ex) {
        return cb(ex);
    }
    cb(null, result);
}
function getCacheKey(url) {
    var hash = crypto_1.createHash('sha512');
    hash.update(url);
    return hash.digest('hex');
}
var FileCache = /** @class */ (function () {
    function FileCache(location) {
        this._location = location;
    }
    FileCache.prototype.getResponse = function (url, callback) {
        var key = path_1.resolve(this._location, getCacheKey(url));
        fs.readFile(key + '.json', 'utf8', function (err, data) {
            if (err && err.code === 'ENOENT')
                return callback(null, null);
            else if (err)
                return callback(err, null);
            jsonParse(data, function (err, response) {
                if (err) {
                    return callback(err, null);
                }
                var body = fs.createReadStream(key + '.body');
                response.body = body;
                callback(null, response);
            });
        });
    };
    FileCache.prototype.setResponse = function (url, response) {
        var key = path_1.resolve(this._location, getCacheKey(url));
        var errored = false;
        fs.mkdir(this._location, function (err) {
            if (err && err.code !== 'EEXIST') {
                console.warn('Error creating cache: ' + err.message);
                return;
            }
            response.body.pipe(fs.createWriteStream(key + '.body')).on('error', function (err) {
                errored = true;
                console.warn('Error writing to cache: ' + err.message);
            }).on('close', function () {
                if (!errored) {
                    fs.writeFile(key + '.json', JSON.stringify({
                        statusCode: response.statusCode,
                        headers: response.headers,
                        requestHeaders: response.requestHeaders,
                        requestTimestamp: response.requestTimestamp
                    }, null, '  '), function (err) {
                        if (err) {
                            console.warn('Error writing to cache: ' + err.message);
                        }
                    });
                }
            });
        });
    };
    FileCache.prototype.updateResponseHeaders = function (url, response) {
        var key = path_1.resolve(this._location, getCacheKey(url));
        fs.readFile(key + '.json', 'utf8', function (err, data) {
            if (err) {
                console.warn('Error writing to cache: ' + err.message);
                return;
            }
            var parsed = null;
            try {
                parsed = JSON.parse(data);
            }
            catch (ex) {
                console.warn('Error writing to cache: ' + ex.message);
                return;
            }
            fs.writeFile(key + '.json', JSON.stringify({
                statusCode: parsed.statusCode,
                headers: response.headers,
                requestHeaders: parsed.requestHeaders,
                requestTimestamp: response.requestTimestamp
            }, null, '  '), function (err) {
                if (err) {
                    console.warn('Error writing to cache: ' + err.message);
                }
            });
        });
    };
    FileCache.prototype.invalidateResponse = function (url, callback) {
        var key = path_1.resolve(this._location, getCacheKey(url));
        fs.unlink(key + '.json', function (err) {
            if (err && err.code === 'ENOENT')
                return callback(null);
            else
                callback(err || null);
        });
    };
    return FileCache;
}());
exports["default"] = FileCache;