fix lint warnings

This commit is contained in:
Randall Schmidt 2020-11-27 23:28:00 -05:00
parent 49741e9608
commit 022626e7f9
4 changed files with 63 additions and 61 deletions

1
.eslintignore Normal file

@ -0,0 +1 @@
test

27
classes/headers.js Normal file

@ -0,0 +1,27 @@
class Headers {
constructor(rawHeaders) {
this.rawHeaders = rawHeaders;
}
entries() {
return Object.entries(this.rawHeaders);
}
keys() {
return Object.keys(this.rawHeaders);
}
values() {
return Object.values(this.rawHeaders);
}
get(name) {
return this.rawHeaders[name.toLowerCase()] || null;
}
has(name) {
return !!this.get(name);
}
}
module.exports = Headers;

33
classes/response.js Normal file

@ -0,0 +1,33 @@
const fs = require('fs');
const Headers = require('./headers.js');
class Response {
constructor(raw, cacheFilePath, fromCache) {
Object.assign(this, raw);
this.cacheFilePath = cacheFilePath;
this.headers = new Headers(raw.headers);
this.fromCache = fromCache;
if (this.bodyBuffer.type === 'Buffer') {
this.bodyBuffer = Buffer.from(this.bodyBuffer);
}
}
text() {
return this.bodyBuffer.toString();
}
json() {
return JSON.parse(this.bodyBuffer.toString());
}
buffer() {
return this.bodyBuffer;
}
ejectFromCache() {
return fs.promises.unlink(this.cacheFilePath);
}
}
module.exports = Response;

@ -3,71 +3,12 @@ const fs = require('fs');
const crypto = require('crypto'); const crypto = require('crypto');
const path = require('path'); const path = require('path');
const Response = require('./classes/response.js');
function md5(str) { function md5(str) {
return crypto.createHash('md5').update(str).digest('hex'); return crypto.createHash('md5').update(str).digest('hex');
} }
class Headers {
constructor(rawHeaders) {
this.rawHeaders = rawHeaders;
}
* entries() {
for (let entry of Object.entries(this.rawHeaders)) {
yield entry;
}
}
* keys() {
for (let key of Object.keys(this.rawHeaders)) {
yield key;
}
}
* values() {
for (let value of Object.values(this.rawHeaders)) {
yield value;
}
}
get(name) {
return this.rawHeaders[name.toLowerCase()] || null;
}
has(name) {
return !!this.get(name);
}
}
class Response {
constructor(raw, cacheFilePath, fromCache) {
Object.assign(this, raw);
this.cacheFilePath = cacheFilePath;
this.headers = new Headers(raw.headers);
this.fromCache = fromCache;
if (this.bodyBuffer.type === 'Buffer') {
this.bodyBuffer = Buffer.from(this.bodyBuffer);
}
}
text() {
return this.bodyBuffer.toString();
}
json() {
return JSON.parse(this.bodyBuffer.toString());
}
buffer() {
return this.bodyBuffer;
}
ejectFromCache() {
return fs.promises.unlink(this.cacheFilePath);
}
}
async function createRawResponse(fetchRes) { async function createRawResponse(fetchRes) {
const buffer = await fetchRes.buffer(); const buffer = await fetchRes.buffer();