diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/classes/headers.js b/classes/headers.js new file mode 100644 index 0000000..a2c61ed --- /dev/null +++ b/classes/headers.js @@ -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; diff --git a/classes/response.js b/classes/response.js new file mode 100644 index 0000000..a746fe1 --- /dev/null +++ b/classes/response.js @@ -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; diff --git a/index.js b/index.js index 903e2c0..bc4a1cc 100644 --- a/index.js +++ b/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();