convert to ESM
This commit is contained in:
parent
af1e977620
commit
45ca35f057
@ -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"
|
||||
}
|
||||
}
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
.eslintrc.js
|
||||
.eslintrc.json
|
||||
test
|
||||
.cache
|
||||
.nyc_output
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
module.exports = class KeyTimeout {
|
||||
export class KeyTimeout {
|
||||
constructor() {
|
||||
this.timeoutHandleForKey = {};
|
||||
}
|
||||
@ -13,4 +13,4 @@ module.exports = class KeyTimeout {
|
||||
callback();
|
||||
}, durationMs);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
21
index.js
21
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';
|
||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "node-fetch-cache",
|
||||
"version": "2.0.1",
|
||||
"version": "2.0.3",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -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": {
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user