diff --git a/.eslintrc.js b/.eslintrc.json similarity index 78% rename from .eslintrc.js rename to .eslintrc.json index b155f8c..478ba08 100644 --- a/.eslintrc.js +++ b/.eslintrc.json @@ -1,4 +1,4 @@ -module.exports = { +{ "env": { "commonjs": true, "es6": true, @@ -15,5 +15,7 @@ module.exports = { "ecmaVersion": 2018 }, "rules": { + "import/extensions": "off", + "import/prefer-default-export": "off" } -}; \ No newline at end of file +} diff --git a/.npmignore b/.npmignore index 56a4f91..171fdd9 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,4 @@ -.eslintrc.js +.eslintrc.json test .cache .nyc_output diff --git a/classes/caching/file_system_cache.js b/classes/caching/file_system_cache.js index cdc616a..ccaec8a 100644 --- a/classes/caching/file_system_cache.js +++ b/classes/caching/file_system_cache.js @@ -1,7 +1,7 @@ -const FPersist = require('fpersist'); -const KeyTimeout = require('./key_timeout.js'); +import FPersist from 'fpersist'; +import { KeyTimeout } from './key_timeout.js'; -module.exports = class FileSystemCache { +export class FileSystemCache { constructor(options = {}) { this.ttl = options.ttl; this.keyTimeout = new KeyTimeout(); @@ -26,4 +26,4 @@ module.exports = class FileSystemCache { this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key)); } } -}; +} diff --git a/classes/caching/key_timeout.js b/classes/caching/key_timeout.js index 42e1367..f669b72 100644 --- a/classes/caching/key_timeout.js +++ b/classes/caching/key_timeout.js @@ -1,4 +1,4 @@ -module.exports = class KeyTimeout { +export class KeyTimeout { constructor() { this.timeoutHandleForKey = {}; } @@ -13,4 +13,4 @@ module.exports = class KeyTimeout { callback(); }, durationMs); } -}; +} diff --git a/classes/caching/memory_cache.js b/classes/caching/memory_cache.js index d6bf0bb..1f69a85 100644 --- a/classes/caching/memory_cache.js +++ b/classes/caching/memory_cache.js @@ -1,6 +1,6 @@ -const KeyTimeout = require('./key_timeout.js'); +import { KeyTimeout } from './key_timeout.js'; -module.exports = class MemoryCache { +export class MemoryCache { constructor(options = {}) { this.ttl = options.ttl; this.keyTimeout = new KeyTimeout(); @@ -23,4 +23,4 @@ module.exports = class MemoryCache { this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key)); } } -}; +} diff --git a/classes/headers.js b/classes/headers.js index 149eb29..fbbc6b0 100644 --- a/classes/headers.js +++ b/classes/headers.js @@ -1,4 +1,4 @@ -class Headers { +export class Headers { constructor(rawHeaders) { this.rawHeaders = rawHeaders; } @@ -29,5 +29,3 @@ class Headers { return this.rawHeaders; } } - -module.exports = Headers; diff --git a/classes/response.js b/classes/response.js index f3f6653..47f123e 100644 --- a/classes/response.js +++ b/classes/response.js @@ -1,7 +1,7 @@ -const stream = require('stream'); -const Headers = require('./headers.js'); +import stream from 'stream'; +import { Headers } from './headers.js'; -class Response { +export class Response { constructor(raw, ejectSelfFromCache, fromCache) { Object.assign(this, raw); this.ejectSelfFromCache = ejectSelfFromCache; @@ -43,5 +43,3 @@ class Response { return this.ejectSelfFromCache(); } } - -module.exports = Response; diff --git a/index.js b/index.js index 1713a73..fe6a83f 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,9 @@ -const fetch = require('node-fetch'); -const fs = require('fs'); -const { URLSearchParams } = require('url'); -const crypto = require('crypto'); -const Response = require('./classes/response.js'); -const MemoryCache = require('./classes/caching/memory_cache.js'); -const FileSystemCache = require('./classes/caching/file_system_cache.js'); +import fetch from 'node-fetch'; +import fs from 'fs'; +import { URLSearchParams } from 'url'; +import crypto from 'crypto'; +import { Response } from './classes/response.js'; +import { MemoryCache } from './classes/caching/memory_cache.js'; const CACHE_VERSION = 2; @@ -112,7 +111,7 @@ function createFetchWithCache(cache) { const defaultFetch = createFetchWithCache(new MemoryCache()); -module.exports = defaultFetch; -module.exports.fetchBuilder = defaultFetch; -module.exports.MemoryCache = MemoryCache; -module.exports.FileSystemCache = FileSystemCache; +export default defaultFetch; +export const fetchBuilder = defaultFetch; +export { MemoryCache } from './classes/caching/memory_cache.js'; +export { FileSystemCache } from './classes/caching/file_system_cache.js'; diff --git a/package-lock.json b/package-lock.json index 6d0668e..7756126 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-fetch-cache", - "version": "2.0.1", + "version": "2.0.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a2f96b5..38c65ee 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "2.0.3", "description": "node-fetch with a persistent cache.", "main": "index.js", + "type": "module", "scripts": { "test": "mocha --timeout 10000 --exit", "coverage": "nyc --reporter=lcov --reporter=text npm test", @@ -38,7 +39,7 @@ }, "dependencies": { "fpersist": "^1.0.5", - "node-fetch": "*" + "node-fetch": "2.6.1" }, "husky": { "hooks": { diff --git a/test/tests.js b/test/tests.js index b131f33..5a824b6 100644 --- a/test/tests.js +++ b/test/tests.js @@ -1,12 +1,16 @@ -const fs = require('fs'); -const FormData = require('form-data'); -const assert = require('assert'); -const rimraf = require('rimraf'); -const path = require('path'); -const { URLSearchParams } = require('url'); -const standardFetch = require('node-fetch'); -const FetchCache = require('../index.js'); -const { Agent } = require('http'); +import { dirname } from 'path'; +import { fileURLToPath } from 'url'; +import fs from 'fs'; +import FormData from 'form-data'; +import assert from 'assert'; +import rimraf from 'rimraf'; +import path from 'path'; +import { URLSearchParams } from 'url'; +import standardFetch from 'node-fetch'; +import FetchCache, { MemoryCache, FileSystemCache } from '../index.js'; +import { Agent } from 'http'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); const CACHE_PATH = path.join(__dirname, '..', '.cache'); const expectedPngBuffer = fs.readFileSync(path.join(__dirname, 'expected_png.png')); @@ -60,9 +64,11 @@ async function dualFetch(...args) { beforeEach(async function() { rimraf.sync(CACHE_PATH); - cachedFetch = FetchCache.withCache(new FetchCache.MemoryCache()); + cachedFetch = FetchCache.withCache(new MemoryCache()); }); +let res; + describe('Basic property tests', function() { it('Has a status property', async function() { let { cachedFetchResponse, standardFetchResponse } = await dualFetch(TWO_HUNDRED_URL); @@ -253,7 +259,7 @@ describe('Cache tests', function() { }); it('Gives different read streams different cache keys', async function() { - const s1 = fs.createReadStream(__filename); + const s1 = fs.createReadStream(path.join(__dirname, 'expected_png.png')); const s2 = fs.createReadStream(path.join(__dirname, '..', 'index.js')); res = await cachedFetch(TWO_HUNDRED_URL, post(s1)); @@ -264,7 +270,7 @@ describe('Cache tests', function() { }); it('Gives the same read streams the same cache key', async function() { - const s1 = fs.createReadStream(__filename); + const s1 = fs.createReadStream(path.join(__dirname, 'expected_png.png')); res = await cachedFetch(TWO_HUNDRED_URL, post(s1)); assert.strictEqual(res.fromCache, false); @@ -402,7 +408,7 @@ describe('Data tests', function() { describe('Memory cache tests', function() { it('Supports TTL', async function() { - cachedFetch = FetchCache.withCache(new FetchCache.MemoryCache({ ttl: 100 })); + cachedFetch = FetchCache.withCache(new MemoryCache({ ttl: 100 })); let res = await cachedFetch(TWO_HUNDRED_URL); assert.strictEqual(res.fromCache, false); res = await cachedFetch(TWO_HUNDRED_URL); @@ -417,7 +423,7 @@ describe('Memory cache tests', function() { describe('File system cache tests', function() { it('Supports TTL', async function() { - cachedFetch = FetchCache.withCache(new FetchCache.FileSystemCache({ ttl: 100 })); + cachedFetch = FetchCache.withCache(new FileSystemCache({ ttl: 100 })); let res = await cachedFetch(TWO_HUNDRED_URL); assert.strictEqual(res.fromCache, false); res = await cachedFetch(TWO_HUNDRED_URL); @@ -430,7 +436,7 @@ describe('File system cache tests', function() { }); it('Can get PNG buffer body', async function() { - cachedFetch = FetchCache.withCache(new FetchCache.FileSystemCache()); + cachedFetch = FetchCache.withCache(new FileSystemCache()); res = await cachedFetch(PNG_BODY_URL); body = await res.buffer(); assert.strictEqual(expectedPngBuffer.equals(body), true);