summaryrefslogtreecommitdiff
path: root/node_modules/http-basic/lib/memory-cache.js
diff options
context:
space:
mode:
author8cy <[email protected]>2020-04-03 02:37:42 -0700
committer8cy <[email protected]>2020-04-03 02:37:42 -0700
commit60867fb030bae582082340ead7dbc7efdc2f5398 (patch)
tree4c6a7356351be2e4914e15c4703172597c45656e /node_modules/http-basic/lib/memory-cache.js
parentcommenting (diff)
downloads5nical-60867fb030bae582082340ead7dbc7efdc2f5398.tar.xz
s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.zip
2020/04/03, 02:34, v1.2.0
Diffstat (limited to 'node_modules/http-basic/lib/memory-cache.js')
-rw-r--r--node_modules/http-basic/lib/memory-cache.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/node_modules/http-basic/lib/memory-cache.js b/node_modules/http-basic/lib/memory-cache.js
new file mode 100644
index 0000000..cf1f5a9
--- /dev/null
+++ b/node_modules/http-basic/lib/memory-cache.js
@@ -0,0 +1,39 @@
+'use strict';
+
+var PassThrough = require('stream').PassThrough;
+var concat = require('concat-stream');
+var Response = require('http-response-object');
+
+module.exports = MemoryCache;
+function MemoryCache() {
+ this._cache = {};
+}
+
+MemoryCache.prototype.getResponse = function (url, callback) {
+ var cache = this._cache;
+ if (cache[url]) {
+ var body = new PassThrough();
+ body.end(cache[url].body);
+ callback(null, {
+ statusCode: cache[url].statusCode,
+ headers: cache[url].headers,
+ body: body,
+ requestHeaders: cache[url].requestHeaders,
+ requestTimestamp: cache[url].requestTimestamp
+ });
+ } else {
+ callback(null, null);
+ }
+};
+MemoryCache.prototype.setResponse = function (url, response) {
+ var cache = this._cache;
+ response.body.pipe(concat(function (body) {
+ cache[url] = {
+ statusCode: response.statusCode,
+ headers: response.headers,
+ body: body,
+ requestHeaders: response.requestHeaders,
+ requestTimestamp: response.requestTimestamp
+ };
+ }));
+};