fix lint warnings
This commit is contained in:
parent
49741e9608
commit
022626e7f9
1
.eslintignore
Normal file
1
.eslintignore
Normal file
@ -0,0 +1 @@
|
||||
test
|
27
classes/headers.js
Normal file
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
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;
|
63
index.js
63
index.js
@ -3,71 +3,12 @@ const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const path = require('path');
|
||||
|
||||
const Response = require('./classes/response.js');
|
||||
|
||||
function md5(str) {
|
||||
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) {
|
||||
const buffer = await fetchRes.buffer();
|
||||
|
||||
|
Reference in New Issue
Block a user