@@ -5,38 +5,47 @@ const EventEmitter = require('events');
55const retryCodes = [ 429 ] . concat ( ( process . env . JSON_CACHE_RETRY_CODES || '' )
66 . split ( ',' ) . map ( code => parseInt ( code . trim ( ) , 10 ) ) ) ;
77
8+ const defaultOpts = {
9+ parser : JSON . parse ,
10+ promiseLib : Promise ,
11+ logger : console ,
12+ delayStart : false ,
13+ opts : { } ,
14+ maxListeners : 10 ,
15+ useEmitter : false ,
16+ maxRetry : 1 ,
17+ integrity : ( ) => true ,
18+ } ;
19+
820class JSONCache extends EventEmitter {
921 /**
1022 * Make a new cache
11- * @param {string } url url to fetch
12- * @param {number } [timeout=60000] optional timeout
13- * @param {Object } opts Options object
14- * @param {function } opts.parser optional parser to parse data. defaults to JSON.parse
15- * @param {Class } opts.promiseLib optional promise library override
16- * @param {Object } opts.logger optional Logger
17- * @param {boolean } opts.delayStart whether or not to delay starting updating the cache
18- * until start is requested
19- * @param {Object } opts.opts options to pass to the parser
20- * @param {number } opts.maxListeners maximum listeners (only applicable if leveraging emitter)
21- * @param {boolean } opts.useEmitter whether or not to use the optional node emitter
22- * @param {number } opts.maxRetry maximum number of attempts to retry getting data
23- * @param {function } opts.integrity optional function to check if the data is worth keeping
23+ * @param {string } url url to fetch
24+ * @param {number } [timeout=60000] optional timeout
25+ * @param {Object } options Options object
26+ * @param {function } options.parser optional parser to parse data. defaults to JSON.parse
27+ * @param {Class } options.promiseLib optional promise library override
28+ * @param {Object } options.logger optional Logger
29+ * @param {boolean } options.delayStart whether or not to delay starting updating the cache
30+ * until start is requested
31+ * @param {Object } options.opts options to pass to the parser
32+ * @param {number } options.maxListeners maximum listeners
33+ * (only applicable if leveraging emitter)
34+ * @param {boolean } options.useEmitter whether or not to use the optional node emitter
35+ * @param {number } options.maxRetry maximum number of attempts to retry getting data
36+ * @param {function } options.integrity optional function to check if the data is worth keeping
2437 */
25- constructor ( url , timeout = 60000 , {
26- parser = JSON . parse , promiseLib = Promise , logger = console , delayStart = true ,
27- opts, maxListeners = 45 , useEmitter = true , maxRetry = 30 , integrity = ( ) => true ,
28- } = {
29- parser : JSON . parse ,
30- promiseLib : Promise ,
31- logger : console ,
32- delayStart : true ,
33- opts : { } ,
34- maxListeners : 45 ,
35- useEmitter : true ,
36- maxRetry : 30 ,
37- integrity : ( ) => true ,
38- } ) {
38+ constructor ( url , timeout = 60000 , options ) {
3939 super ( ) ;
40+
41+ // eslint-disable-next-line no-param-reassign
42+ const {
43+ parser, promiseLib, logger, delayStart, opts, maxListeners, useEmitter, maxRetry, integrity,
44+ } = {
45+ ...defaultOpts ,
46+ ...options ,
47+ } ;
48+
4049 this . url = url ;
4150 // eslint-disable-next-line global-require
4251 this . protocol = this . url . startsWith ( 'https' ) ? require ( 'https' ) : require ( 'http' ) ;
0 commit comments