convert to ESM
This commit is contained in:
parent
af1e977620
commit
45ca35f057
@ -1,4 +1,4 @@
|
|||||||
module.exports = {
|
{
|
||||||
"env": {
|
"env": {
|
||||||
"commonjs": true,
|
"commonjs": true,
|
||||||
"es6": true,
|
"es6": true,
|
||||||
@ -15,5 +15,7 @@ module.exports = {
|
|||||||
"ecmaVersion": 2018
|
"ecmaVersion": 2018
|
||||||
},
|
},
|
||||||
"rules": {
|
"rules": {
|
||||||
|
"import/extensions": "off",
|
||||||
|
"import/prefer-default-export": "off"
|
||||||
}
|
}
|
||||||
};
|
}
|
@ -1,4 +1,4 @@
|
|||||||
.eslintrc.js
|
.eslintrc.json
|
||||||
test
|
test
|
||||||
.cache
|
.cache
|
||||||
.nyc_output
|
.nyc_output
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
const FPersist = require('fpersist');
|
import FPersist from 'fpersist';
|
||||||
const KeyTimeout = require('./key_timeout.js');
|
import { KeyTimeout } from './key_timeout.js';
|
||||||
|
|
||||||
module.exports = class FileSystemCache {
|
export class FileSystemCache {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
this.ttl = options.ttl;
|
this.ttl = options.ttl;
|
||||||
this.keyTimeout = new KeyTimeout();
|
this.keyTimeout = new KeyTimeout();
|
||||||
@ -26,4 +26,4 @@ module.exports = class FileSystemCache {
|
|||||||
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
module.exports = class KeyTimeout {
|
export class KeyTimeout {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.timeoutHandleForKey = {};
|
this.timeoutHandleForKey = {};
|
||||||
}
|
}
|
||||||
@ -13,4 +13,4 @@ module.exports = class KeyTimeout {
|
|||||||
callback();
|
callback();
|
||||||
}, durationMs);
|
}, 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 = {}) {
|
constructor(options = {}) {
|
||||||
this.ttl = options.ttl;
|
this.ttl = options.ttl;
|
||||||
this.keyTimeout = new KeyTimeout();
|
this.keyTimeout = new KeyTimeout();
|
||||||
@ -23,4 +23,4 @@ module.exports = class MemoryCache {
|
|||||||
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
class Headers {
|
export class Headers {
|
||||||
constructor(rawHeaders) {
|
constructor(rawHeaders) {
|
||||||
this.rawHeaders = rawHeaders;
|
this.rawHeaders = rawHeaders;
|
||||||
}
|
}
|
||||||
@ -29,5 +29,3 @@ class Headers {
|
|||||||
return this.rawHeaders;
|
return this.rawHeaders;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Headers;
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
const stream = require('stream');
|
import stream from 'stream';
|
||||||
const Headers = require('./headers.js');
|
import { Headers } from './headers.js';
|
||||||
|
|
||||||
class Response {
|
export class Response {
|
||||||
constructor(raw, ejectSelfFromCache, fromCache) {
|
constructor(raw, ejectSelfFromCache, fromCache) {
|
||||||
Object.assign(this, raw);
|
Object.assign(this, raw);
|
||||||
this.ejectSelfFromCache = ejectSelfFromCache;
|
this.ejectSelfFromCache = ejectSelfFromCache;
|
||||||
@ -43,5 +43,3 @@ class Response {
|
|||||||
return this.ejectSelfFromCache();
|
return this.ejectSelfFromCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Response;
|
|
||||||
|
21
index.js
21
index.js
@ -1,10 +1,9 @@
|
|||||||
const fetch = require('node-fetch');
|
import fetch from 'node-fetch';
|
||||||
const fs = require('fs');
|
import fs from 'fs';
|
||||||
const { URLSearchParams } = require('url');
|
import { URLSearchParams } from 'url';
|
||||||
const crypto = require('crypto');
|
import crypto from 'crypto';
|
||||||
const Response = require('./classes/response.js');
|
import { Response } from './classes/response.js';
|
||||||
const MemoryCache = require('./classes/caching/memory_cache.js');
|
import { MemoryCache } from './classes/caching/memory_cache.js';
|
||||||
const FileSystemCache = require('./classes/caching/file_system_cache.js');
|
|
||||||
|
|
||||||
const CACHE_VERSION = 2;
|
const CACHE_VERSION = 2;
|
||||||
|
|
||||||
@ -112,7 +111,7 @@ function createFetchWithCache(cache) {
|
|||||||
|
|
||||||
const defaultFetch = createFetchWithCache(new MemoryCache());
|
const defaultFetch = createFetchWithCache(new MemoryCache());
|
||||||
|
|
||||||
module.exports = defaultFetch;
|
export default defaultFetch;
|
||||||
module.exports.fetchBuilder = defaultFetch;
|
export const fetchBuilder = defaultFetch;
|
||||||
module.exports.MemoryCache = MemoryCache;
|
export { MemoryCache } from './classes/caching/memory_cache.js';
|
||||||
module.exports.FileSystemCache = FileSystemCache;
|
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",
|
"name": "node-fetch-cache",
|
||||||
"version": "2.0.1",
|
"version": "2.0.3",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"description": "node-fetch with a persistent cache.",
|
"description": "node-fetch with a persistent cache.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha --timeout 10000 --exit",
|
"test": "mocha --timeout 10000 --exit",
|
||||||
"coverage": "nyc --reporter=lcov --reporter=text npm test",
|
"coverage": "nyc --reporter=lcov --reporter=text npm test",
|
||||||
@ -38,7 +39,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fpersist": "^1.0.5",
|
"fpersist": "^1.0.5",
|
||||||
"node-fetch": "*"
|
"node-fetch": "2.6.1"
|
||||||
},
|
},
|
||||||
"husky": {
|
"husky": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
const fs = require('fs');
|
import { dirname } from 'path';
|
||||||
const FormData = require('form-data');
|
import { fileURLToPath } from 'url';
|
||||||
const assert = require('assert');
|
import fs from 'fs';
|
||||||
const rimraf = require('rimraf');
|
import FormData from 'form-data';
|
||||||
const path = require('path');
|
import assert from 'assert';
|
||||||
const { URLSearchParams } = require('url');
|
import rimraf from 'rimraf';
|
||||||
const standardFetch = require('node-fetch');
|
import path from 'path';
|
||||||
const FetchCache = require('../index.js');
|
import { URLSearchParams } from 'url';
|
||||||
const { Agent } = require('http');
|
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 CACHE_PATH = path.join(__dirname, '..', '.cache');
|
||||||
const expectedPngBuffer = fs.readFileSync(path.join(__dirname, 'expected_png.png'));
|
const expectedPngBuffer = fs.readFileSync(path.join(__dirname, 'expected_png.png'));
|
||||||
@ -60,9 +64,11 @@ async function dualFetch(...args) {
|
|||||||
|
|
||||||
beforeEach(async function() {
|
beforeEach(async function() {
|
||||||
rimraf.sync(CACHE_PATH);
|
rimraf.sync(CACHE_PATH);
|
||||||
cachedFetch = FetchCache.withCache(new FetchCache.MemoryCache());
|
cachedFetch = FetchCache.withCache(new MemoryCache());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let res;
|
||||||
|
|
||||||
describe('Basic property tests', function() {
|
describe('Basic property tests', function() {
|
||||||
it('Has a status property', async function() {
|
it('Has a status property', async function() {
|
||||||
let { cachedFetchResponse, standardFetchResponse } = await dualFetch(TWO_HUNDRED_URL);
|
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() {
|
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'));
|
const s2 = fs.createReadStream(path.join(__dirname, '..', 'index.js'));
|
||||||
|
|
||||||
res = await cachedFetch(TWO_HUNDRED_URL, post(s1));
|
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() {
|
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));
|
res = await cachedFetch(TWO_HUNDRED_URL, post(s1));
|
||||||
assert.strictEqual(res.fromCache, false);
|
assert.strictEqual(res.fromCache, false);
|
||||||
@ -402,7 +408,7 @@ describe('Data tests', function() {
|
|||||||
|
|
||||||
describe('Memory cache tests', function() {
|
describe('Memory cache tests', function() {
|
||||||
it('Supports TTL', async 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);
|
let res = await cachedFetch(TWO_HUNDRED_URL);
|
||||||
assert.strictEqual(res.fromCache, false);
|
assert.strictEqual(res.fromCache, false);
|
||||||
res = await cachedFetch(TWO_HUNDRED_URL);
|
res = await cachedFetch(TWO_HUNDRED_URL);
|
||||||
@ -417,7 +423,7 @@ describe('Memory cache tests', function() {
|
|||||||
|
|
||||||
describe('File system cache tests', function() {
|
describe('File system cache tests', function() {
|
||||||
it('Supports TTL', async 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);
|
let res = await cachedFetch(TWO_HUNDRED_URL);
|
||||||
assert.strictEqual(res.fromCache, false);
|
assert.strictEqual(res.fromCache, false);
|
||||||
res = await cachedFetch(TWO_HUNDRED_URL);
|
res = await cachedFetch(TWO_HUNDRED_URL);
|
||||||
@ -430,7 +436,7 @@ describe('File system cache tests', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Can get PNG buffer body', async 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);
|
res = await cachedFetch(PNG_BODY_URL);
|
||||||
body = await res.buffer();
|
body = await res.buffer();
|
||||||
assert.strictEqual(expectedPngBuffer.equals(body), true);
|
assert.strictEqual(expectedPngBuffer.equals(body), true);
|
||||||
|
Reference in New Issue
Block a user