Version 2.0
This commit is contained in:
29
classes/caching/file_system_cache.js
Normal file
29
classes/caching/file_system_cache.js
Normal file
@ -0,0 +1,29 @@
|
||||
const FPersist = require('fpersist');
|
||||
const KeyTimeout = require('./key_timeout.js');
|
||||
|
||||
module.exports = class FileSystemCache {
|
||||
constructor(options = {}) {
|
||||
this.ttl = options.ttl;
|
||||
this.keyTimeout = new KeyTimeout();
|
||||
|
||||
const cacheDirectory = options.cacheDirectory || '.cache';
|
||||
this.cache = new FPersist(cacheDirectory);
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.cache.getItem(key);
|
||||
}
|
||||
|
||||
remove(key) {
|
||||
this.keyTimeout.clearTimeout(key);
|
||||
return this.cache.deleteItem(key);
|
||||
}
|
||||
|
||||
async set(key, value) {
|
||||
await this.cache.setItem(key, value);
|
||||
|
||||
if (typeof this.ttl === 'number') {
|
||||
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
||||
}
|
||||
}
|
||||
};
|
16
classes/caching/key_timeout.js
Normal file
16
classes/caching/key_timeout.js
Normal file
@ -0,0 +1,16 @@
|
||||
module.exports = class KeyTimeout {
|
||||
constructor() {
|
||||
this.timeoutHandleForKey = {};
|
||||
}
|
||||
|
||||
clearTimeout(key) {
|
||||
clearTimeout(this.timeoutHandleForKey[key]);
|
||||
}
|
||||
|
||||
updateTimeout(key, durationMs, callback) {
|
||||
this.clearTimeout(key);
|
||||
this.timeoutHandleForKey[key] = setTimeout(() => {
|
||||
callback();
|
||||
}, durationMs);
|
||||
}
|
||||
};
|
26
classes/caching/memory_cache.js
Normal file
26
classes/caching/memory_cache.js
Normal file
@ -0,0 +1,26 @@
|
||||
const KeyTimeout = require('./key_timeout.js');
|
||||
|
||||
module.exports = class MemoryCache {
|
||||
constructor(options = {}) {
|
||||
this.ttl = options.ttl;
|
||||
this.keyTimeout = new KeyTimeout();
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.cache[key];
|
||||
}
|
||||
|
||||
remove(key) {
|
||||
this.keyTimeout.clearTimeout(key);
|
||||
delete this.cache[key];
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.cache[key] = value;
|
||||
|
||||
if (typeof this.ttl === 'number') {
|
||||
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
||||
}
|
||||
}
|
||||
};
|
33
classes/headers.js
Normal file
33
classes/headers.js
Normal file
@ -0,0 +1,33 @@
|
||||
class Headers {
|
||||
constructor(rawHeaders) {
|
||||
this.rawHeaders = rawHeaders;
|
||||
}
|
||||
|
||||
entries() {
|
||||
return Object.entries(this.rawHeaders)
|
||||
.sort((e1, e2) => e1[0].localeCompare(e2[0]))
|
||||
.map(([key, val]) => [key, val[0]]);
|
||||
}
|
||||
|
||||
keys() {
|
||||
return this.entries().map((e) => e[0]);
|
||||
}
|
||||
|
||||
values() {
|
||||
return this.entries().map((e) => e[1]);
|
||||
}
|
||||
|
||||
get(name) {
|
||||
return (this.rawHeaders[name.toLowerCase()] || [])[0] || null;
|
||||
}
|
||||
|
||||
has(name) {
|
||||
return !!this.get(name);
|
||||
}
|
||||
|
||||
raw() {
|
||||
return this.rawHeaders;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Headers;
|
47
classes/response.js
Normal file
47
classes/response.js
Normal file
@ -0,0 +1,47 @@
|
||||
const stream = require('stream');
|
||||
const Headers = require('./headers.js');
|
||||
|
||||
class Response {
|
||||
constructor(raw, ejectSelfFromCache, fromCache) {
|
||||
Object.assign(this, raw);
|
||||
this.ejectSelfFromCache = ejectSelfFromCache;
|
||||
this.headers = new Headers(raw.headers);
|
||||
this.fromCache = fromCache;
|
||||
this.bodyUsed = false;
|
||||
|
||||
if (this.bodyBuffer.type === 'Buffer') {
|
||||
this.bodyBuffer = Buffer.from(this.bodyBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
get body() {
|
||||
return stream.Readable.from(this.bodyBuffer);
|
||||
}
|
||||
|
||||
consumeBody() {
|
||||
if (this.bodyUsed) {
|
||||
throw new Error('Error: body used already');
|
||||
}
|
||||
|
||||
this.bodyUsed = true;
|
||||
return this.bodyBuffer;
|
||||
}
|
||||
|
||||
async text() {
|
||||
return this.consumeBody().toString();
|
||||
}
|
||||
|
||||
async json() {
|
||||
return JSON.parse(this.consumeBody().toString());
|
||||
}
|
||||
|
||||
async buffer() {
|
||||
return this.consumeBody();
|
||||
}
|
||||
|
||||
ejectFromCache() {
|
||||
return this.ejectSelfFromCache();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Response;
|
Reference in New Issue
Block a user