Skip to content

Commit 757459b

Browse files
author
Tobiah
committed
feat: optional integrity check to ensure data is good before keeping
1 parent dadca79 commit 757459b

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ $ npm i -S json-fetch-cache
1616

1717
## Usage
1818
```js
19-
var Cache = require("json-fetch-cache");
19+
const Cache = require('json-fetch-cache');
2020

2121
const pcCache = new Cache('http://content.warframe.com/dynamic/worldState.php', 10000);
2222

23-
pcCache.getData().then((data) => {
23+
pcCache.getData()
24+
.then((data) => {
2425
console.log(data);
2526
process.exit(0);
26-
})
27-
.catch((error)=>{
27+
})
28+
.catch((error)=>{
2829
console.error(error);
29-
})
30+
});
3031
```

jsoncache.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22

33
const EventEmitter = require('events');
44

5-
const http = require('http');
6-
const https = require('https');
7-
85
const retryCodes = [429].concat((process.env.JSON_CACHE_RETRY_CODES || '')
96
.split(',').map(code => parseInt(code.trim(), 10)));
107

118
class JSONCache extends EventEmitter {
9+
/**
10+
* Make a new cache
11+
* @param {string} url url to fetch
12+
* @param {number} [timeout=60000] optional timeout
13+
* @param {[type]} parser optional parser to parse data. defaults to JSON.parse
14+
* @param {[type]} promiseLib optional promise library override
15+
* @param {[type]} logger optional Logger
16+
* @param {[type]} delayStart whether or not to delay starting updating the cache
17+
* until start is requested
18+
* @param {[type]} opts options to pass to the parser
19+
* @param {[type]} maxListeners maximum listeners (only applicable if leveraging emitter)
20+
* @param {[type]} useEmitter whether or not to use the optional node emitter
21+
* @param {[type]} maxRetry maximum number of attempts to retry getting data
22+
* @param {[type]} integrity optional function to check if the data is worth keeping
23+
*/
1224
constructor(url, timeout = 60000, {
1325
parser = JSON.parse, promiseLib = Promise, logger = console, delayStart = true,
14-
opts, maxListeners = 45, useEmitter = true, maxRetry = 30,
26+
opts, maxListeners = 45, useEmitter = true, maxRetry = 30, integrity = () => true,
1527
} = {
1628
parser: JSON.parse,
1729
promiseLib: Promise,
@@ -21,10 +33,12 @@ class JSONCache extends EventEmitter {
2133
maxListeners: 45,
2234
useEmitter: true,
2335
maxRetry: 30,
36+
integrity: () => true,
2437
}) {
2538
super();
2639
this.url = url;
27-
this.protocol = this.url.startsWith('https') ? https : http;
40+
// eslint-disable-next-line global-require
41+
this.protocol = this.url.startsWith('https') ? require('https') : require('http');
2842

2943
this.maxRetry = maxRetry;
3044
this.timeout = timeout;
@@ -37,6 +51,7 @@ class JSONCache extends EventEmitter {
3751
this.delayStart = delayStart;
3852
this.opts = opts;
3953
this.useEmitter = useEmitter;
54+
this.integrity = integrity;
4055
if (useEmitter) {
4156
this.setMaxListeners(maxListeners);
4257
}
@@ -61,7 +76,11 @@ class JSONCache extends EventEmitter {
6176

6277
update() {
6378
this.updating = this.httpGet().then(async (data) => {
64-
this.currentData = this.parser(data, this.opts);
79+
const parsed = this.parser(data, this.opts);
80+
if (!this.integrity(parsed)) return this.currentData;
81+
82+
// data passed integrity check
83+
this.currentData = parsed;
6584
if (this.useEmitter) {
6685
setTimeout(async () => this.emit('update', await this.currentData), 2000);
6786
}

0 commit comments

Comments
 (0)