fetch-json-cache 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/get.js.map +1 -1
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/update.js +2 -2
- package/dist/cjs/update.js.map +1 -1
- package/dist/esm/get.mjs.map +1 -1
- package/dist/esm/index.mjs +3 -3
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/update.mjs +2 -2
- package/dist/esm/update.mjs.map +1 -1
- package/dist/types/index.d.ts +9 -4
- package/package.json +4 -4
package/dist/cjs/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update.js';\n\nexport default function getCache(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n fs.readFile(fullPath, (err, contents) => {\n // doesn't exist so create\n if (err) {\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n }\n // check existing if etag changed and if so, recache\n else {\n const record = JSON.parse(contents.toString());\n get(endpoint).head((err, res) => {\n // not accessible\n if (err) {\n if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);\n return callback(err);\n }\n if (res.headers.etag === record.headers.etag) return callback(null, record.body);\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n });\n }\n });\n}\n"],"names":["getCache","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","fs","readFile","err","contents","update","call","json","record","JSON","parse","toString","get","head","res","code","message","indexOf","body","headers","etag"],"mappings":";;;;+
|
|
1
|
+
{"version":3,"sources":["get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update.js';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nexport default function getCache(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n fs.readFile(fullPath, (err, contents) => {\n // doesn't exist so create\n if (err) {\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n }\n // check existing if etag changed and if so, recache\n else {\n const record = JSON.parse(contents.toString());\n get(endpoint).head((err, res) => {\n // not accessible\n if (err) {\n if ((err as GetError).code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);\n return callback(err);\n }\n if ((res.headers as GetHeaders).etag === record.headers.etag) return callback(null, record.body);\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n });\n }\n });\n}\n"],"names":["getCache","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","fs","readFile","err","contents","update","call","json","record","JSON","parse","toString","get","head","res","code","message","indexOf","body","headers","etag"],"mappings":";;;;+BAcA;;;eAAwBA;;;yDAdT;2DACE;gEACD;6DAEG;;;;;;AAUJ,SAASA,SAASC,QAAQ,EAAEC,QAAQ;;IACjD,IAAMC,WAAWC,aAAI,CAACC,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACP,WAAU;IAC/EQ,WAAE,CAACC,QAAQ,CAACP,UAAU,SAACQ,KAAKC;QAC1B,0BAA0B;QAC1B,IAAID,KAAK;YACPE,eAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;gBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;YACvC;QACF,OAEK;YACH,IAAMC,SAASC,KAAKC,KAAK,CAACN,SAASO,QAAQ;YAC3CC,IAAAA,kBAAG,EAACnB,UAAUoB,IAAI,CAAC,SAACV,KAAKW;gBACvB,iBAAiB;gBACjB,IAAIX,KAAK;oBACP,IAAI,AAACA,IAAiBY,IAAI,KAAK,eAAeZ,IAAIa,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOvB,SAAS,MAAMc,OAAOU,IAAI;oBAC5I,OAAOxB,SAASS;gBAClB;gBACA,IAAI,AAACW,IAAIK,OAAO,CAAgBC,IAAI,KAAKZ,OAAOW,OAAO,CAACC,IAAI,EAAE,OAAO1B,SAAS,MAAMc,OAAOU,IAAI;gBAC/Fb,eAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;oBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;gBACvC;YACF;QACF;IACF;AACF"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -57,7 +57,7 @@ var Cache = /*#__PURE__*/ function() {
|
|
|
57
57
|
});
|
|
58
58
|
});
|
|
59
59
|
};
|
|
60
|
-
_proto.getSync = function getSync(endpoint
|
|
60
|
+
_proto.getSync = function getSync(endpoint) {
|
|
61
61
|
// TODO: add async fetching
|
|
62
62
|
return _getSync.default.call(this, endpoint);
|
|
63
63
|
};
|
|
@@ -70,8 +70,8 @@ var Cache = /*#__PURE__*/ function() {
|
|
|
70
70
|
callback();
|
|
71
71
|
});
|
|
72
72
|
return new Promise(function(resolve, reject) {
|
|
73
|
-
_this.clear(function(err
|
|
74
|
-
err ? reject(err) : resolve(
|
|
73
|
+
_this.clear(function(err) {
|
|
74
|
+
err ? reject(err) : resolve(undefined);
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
};
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.ts"],"sourcesContent":["import './polyfills.cjs';\nimport rimraf2 from 'rimraf2';\n\nimport get from './get.js';\nimport getSync from './getSync.js';\nimport hash from './hash.js';\nimport update from './update.js';\n\nexport interface CacheOptions {\n hash?: (string: string) => string;\n}\n\nexport default class Cache {\n cacheDirectory: string;\n options: CacheOptions;\n\n constructor(cacheDirectory, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');\n this.cacheDirectory = cacheDirectory;\n this.options = options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint: string, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = null;\n }\n options = options || {};\n\n if (typeof callback === 'function') return options.force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n return new Promise((resolve, reject) => {\n this.get(endpoint, options, (err, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n\n getSync(endpoint
|
|
1
|
+
{"version":3,"sources":["index.ts"],"sourcesContent":["import './polyfills.cjs';\nimport rimraf2 from 'rimraf2';\n\nimport get from './get.js';\nimport getSync from './getSync.js';\nimport hash from './hash.js';\nimport update from './update.js';\n\nexport interface CacheOptions {\n hash?: (string: string) => string;\n}\n\nexport interface GetOptions {\n force?: boolean;\n}\nexport type GetCallback = (error?: Error, json?: object) => void;\nexport type ClearCallback = (error?: Error) => void;\n\nexport default class Cache {\n cacheDirectory: string;\n options: CacheOptions;\n\n constructor(cacheDirectory: string, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');\n this.cacheDirectory = cacheDirectory;\n this.options = options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null> {\n if (typeof options === 'function') {\n callback = options as GetCallback;\n options = null;\n }\n options = options || {};\n\n if (typeof callback === 'function') return (options as GetOptions).force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n return new Promise((resolve, reject) => {\n this.get(endpoint, options, (err, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n\n getSync(endpoint: string): object | null {\n // TODO: add async fetching\n return getSync.call(this, endpoint);\n }\n\n clear(callback?: ClearCallback): undefined | Promise<undefined> {\n if (typeof callback === 'function')\n return rimraf2(this.cacheDirectory, { disableGlob: true }, (_err) => {\n _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint\n callback();\n });\n return new Promise((resolve, reject) => {\n this.clear((err) => {\n err ? reject(err) : resolve(undefined);\n });\n });\n }\n}\n"],"names":["Cache","cacheDirectory","options","Error","hash","get","endpoint","callback","force","update","call","Promise","resolve","reject","err","json","getSync","clear","rimraf2","disableGlob","_err","undefined"],"mappings":";;;;;;;eAkBqBA;;;QAlBd;8DACa;0DAEJ;8DACI;2DACH;6DACE;;;;;;;;;;;;;;;;;;AAYJ,IAAA,AAAMA,sBAAN;;aAAMA,MAIPC,cAAsB;YAAEC,UAAAA,iEAAwB,CAAC;gCAJ1CF;QAKjB,IAAI,CAAE,AAAI,YAAJ,IAAI,EALOA,QAKa,MAAM,IAAIG,MAAM;QAC9C,IAAI,CAACF,gBAAgB,MAAM,IAAIE,MAAM;QACrC,IAAI,CAACF,cAAc,GAAGA;QACtB,IAAI,CAACC,OAAO,GAAGA;QACf,IAAI,CAAC,IAAI,CAACA,OAAO,CAACE,IAAI,EAAE,IAAI,CAACF,OAAO,CAACE,IAAI,GAAGA,aAAI;;iBAT/BJ;IAYnBK,OAAAA,GAaC,GAbDA,SAAAA,IAAIC,QAAgB,EAAEJ,OAAkC,EAAEK,QAAsB;;QAC9E,IAAI,OAAOL,YAAY,YAAY;YACjCK,WAAWL;YACXA,UAAU;QACZ;QACAA,UAAUA,WAAW,CAAC;QAEtB,IAAI,OAAOK,aAAa,YAAY,OAAO,AAACL,QAAuBM,KAAK,GAAGC,eAAM,CAACC,IAAI,CAAC,IAAI,EAAEJ,UAAUC,YAAYF,YAAG,CAACK,IAAI,CAAC,IAAI,EAAEJ,UAAUC;QAC5I,OAAO,IAAII,QAAQ,SAACC,SAASC;YAC3B,MAAKR,GAAG,CAACC,UAAUJ,SAAS,SAACY,KAAKC;gBAChCD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;IAEAC,OAAAA,OAGC,GAHDA,SAAAA,QAAQV,QAAgB;QACtB,2BAA2B;QAC3B,OAAOU,gBAAO,CAACN,IAAI,CAAC,IAAI,EAAEJ;IAC5B;IAEAW,OAAAA,KAWC,GAXDA,SAAAA,MAAMV,QAAwB;;QAC5B,IAAI,OAAOA,aAAa,YACtB,OAAOW,IAAAA,gBAAO,EAAC,IAAI,CAACjB,cAAc,EAAE;YAAEkB,aAAa;QAAK,GAAG,SAACC;YAC1DA,OAAO,MAAM,kGAAkG;YAC/Gb;QACF;QACF,OAAO,IAAII,QAAQ,SAACC,SAASC;YAC3B,MAAKI,KAAK,CAAC,SAACH;gBACVA,MAAMD,OAAOC,OAAOF,QAAQS;YAC9B;QACF;IACF;WA3CmBrB"}
|
package/dist/cjs/update.js
CHANGED
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "default", {
|
|
|
10
10
|
});
|
|
11
11
|
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
12
12
|
var _getremote = /*#__PURE__*/ _interop_require_default(require("get-remote"));
|
|
13
|
-
var
|
|
13
|
+
var _mkdirpclassic = /*#__PURE__*/ _interop_require_default(require("mkdirp-classic"));
|
|
14
14
|
var _writefileatomic = /*#__PURE__*/ _interop_require_default(require("write-file-atomic"));
|
|
15
15
|
function _interop_require_default(obj) {
|
|
16
16
|
return obj && obj.__esModule ? obj : {
|
|
@@ -19,7 +19,7 @@ function _interop_require_default(obj) {
|
|
|
19
19
|
}
|
|
20
20
|
function update(endpoint, callback) {
|
|
21
21
|
var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
|
|
22
|
-
(0,
|
|
22
|
+
(0, _mkdirpclassic.default)(this.cacheDirectory, function(err) {
|
|
23
23
|
if (err && err.code !== 'EEXIST') return callback(err);
|
|
24
24
|
(0, _getremote.default)(endpoint).json(function(err, res) {
|
|
25
25
|
if (err) return callback(err);
|
package/dist/cjs/update.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport
|
|
1
|
+
{"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport writeFile from 'write-file-atomic';\n\nexport default function update(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n\n mkdirp(this.cacheDirectory, (err) => {\n if (err && err.code !== 'EEXIST') return callback(err);\n\n get(endpoint).json((err, res) => {\n if (err) return callback(err);\n\n const record = { headers: res.headers, body: res.body };\n writeFile(fullPath, JSON.stringify(record), 'utf8', (err) => {\n err ? callback(err) : callback(null, record.body);\n });\n });\n });\n}\n"],"names":["update","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","mkdirp","err","code","get","json","res","record","headers","body","writeFile","JSON","stringify"],"mappings":";;;;+BAKA;;;eAAwBA;;;2DALP;gEACD;oEACG;sEACG;;;;;;AAEP,SAASA,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,IAAMC,WAAWC,aAAI,CAACC,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACP,WAAU;IAE/EQ,IAAAA,sBAAM,EAAC,IAAI,CAACH,cAAc,EAAE,SAACI;QAC3B,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOT,SAASQ;QAElDE,IAAAA,kBAAG,EAACX,UAAUY,IAAI,CAAC,SAACH,KAAKI;YACvB,IAAIJ,KAAK,OAAOR,SAASQ;YAEzB,IAAMK,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtDC,IAAAA,wBAAS,EAACf,UAAUgB,KAAKC,SAAS,CAACL,SAAS,QAAQ,SAACL;gBACnDA,MAAMR,SAASQ,OAAOR,SAAS,MAAMa,OAAOE,IAAI;YAClD;QACF;IACF;AACF"}
|
package/dist/esm/get.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update.js';\n\nexport default function getCache(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n fs.readFile(fullPath, (err, contents) => {\n // doesn't exist so create\n if (err) {\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n }\n // check existing if etag changed and if so, recache\n else {\n const record = JSON.parse(contents.toString());\n get(endpoint).head((err, res) => {\n // not accessible\n if (err) {\n if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);\n return callback(err);\n }\n if (res.headers.etag === record.headers.etag) return callback(null, record.body);\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n });\n }\n });\n}\n"],"names":["fs","path","get","update","getCache","endpoint","callback","fullPath","join","cacheDirectory","options","hash","readFile","err","contents","call","json","record","JSON","parse","toString","head","res","code","message","indexOf","body","headers","etag"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,OAAOC,UAAU,OAAO;AACxB,OAAOC,SAAS,aAAa;AAE7B,OAAOC,YAAY,cAAc;
|
|
1
|
+
{"version":3,"sources":["get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update.js';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nexport default function getCache(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n fs.readFile(fullPath, (err, contents) => {\n // doesn't exist so create\n if (err) {\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n }\n // check existing if etag changed and if so, recache\n else {\n const record = JSON.parse(contents.toString());\n get(endpoint).head((err, res) => {\n // not accessible\n if (err) {\n if ((err as GetError).code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);\n return callback(err);\n }\n if ((res.headers as GetHeaders).etag === record.headers.etag) return callback(null, record.body);\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n });\n }\n });\n}\n"],"names":["fs","path","get","update","getCache","endpoint","callback","fullPath","join","cacheDirectory","options","hash","readFile","err","contents","call","json","record","JSON","parse","toString","head","res","code","message","indexOf","body","headers","etag"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,OAAOC,UAAU,OAAO;AACxB,OAAOC,SAAS,aAAa;AAE7B,OAAOC,YAAY,cAAc;AAUjC,eAAe,SAASC,SAASC,QAAQ,EAAEC,QAAQ;IACjD,MAAMC,WAAWN,KAAKO,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,UAAU,KAAK,CAAC;IACrFL,GAAGY,QAAQ,CAACL,UAAU,CAACM,KAAKC;QAC1B,0BAA0B;QAC1B,IAAID,KAAK;YACPV,OAAOY,IAAI,CAAC,IAAI,EAAEV,UAAU,CAACQ,KAAKG;gBAChCH,MAAMP,SAASO,OAAOP,SAAS,MAAMU;YACvC;QACF,OAEK;YACH,MAAMC,SAASC,KAAKC,KAAK,CAACL,SAASM,QAAQ;YAC3ClB,IAAIG,UAAUgB,IAAI,CAAC,CAACR,KAAKS;gBACvB,iBAAiB;gBACjB,IAAIT,KAAK;oBACP,IAAI,AAACA,IAAiBU,IAAI,KAAK,eAAeV,IAAIW,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOnB,SAAS,MAAMW,OAAOS,IAAI;oBAC5I,OAAOpB,SAASO;gBAClB;gBACA,IAAI,AAACS,IAAIK,OAAO,CAAgBC,IAAI,KAAKX,OAAOU,OAAO,CAACC,IAAI,EAAE,OAAOtB,SAAS,MAAMW,OAAOS,IAAI;gBAC/FvB,OAAOY,IAAI,CAAC,IAAI,EAAEV,UAAU,CAACQ,KAAKG;oBAChCH,MAAMP,SAASO,OAAOP,SAAS,MAAMU;gBACvC;YACF;QACF;IACF;AACF"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -18,7 +18,7 @@ let Cache = class Cache {
|
|
|
18
18
|
});
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
-
getSync(endpoint
|
|
21
|
+
getSync(endpoint) {
|
|
22
22
|
// TODO: add async fetching
|
|
23
23
|
return getSync.call(this, endpoint);
|
|
24
24
|
}
|
|
@@ -30,8 +30,8 @@ let Cache = class Cache {
|
|
|
30
30
|
callback();
|
|
31
31
|
});
|
|
32
32
|
return new Promise((resolve, reject)=>{
|
|
33
|
-
this.clear((err
|
|
34
|
-
err ? reject(err) : resolve(
|
|
33
|
+
this.clear((err)=>{
|
|
34
|
+
err ? reject(err) : resolve(undefined);
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
}
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.ts"],"sourcesContent":["import './polyfills.cjs';\nimport rimraf2 from 'rimraf2';\n\nimport get from './get.js';\nimport getSync from './getSync.js';\nimport hash from './hash.js';\nimport update from './update.js';\n\nexport interface CacheOptions {\n hash?: (string: string) => string;\n}\n\nexport default class Cache {\n cacheDirectory: string;\n options: CacheOptions;\n\n constructor(cacheDirectory, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');\n this.cacheDirectory = cacheDirectory;\n this.options = options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint: string, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = null;\n }\n options = options || {};\n\n if (typeof callback === 'function') return options.force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n return new Promise((resolve, reject) => {\n this.get(endpoint, options, (err, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n\n getSync(endpoint
|
|
1
|
+
{"version":3,"sources":["index.ts"],"sourcesContent":["import './polyfills.cjs';\nimport rimraf2 from 'rimraf2';\n\nimport get from './get.js';\nimport getSync from './getSync.js';\nimport hash from './hash.js';\nimport update from './update.js';\n\nexport interface CacheOptions {\n hash?: (string: string) => string;\n}\n\nexport interface GetOptions {\n force?: boolean;\n}\nexport type GetCallback = (error?: Error, json?: object) => void;\nexport type ClearCallback = (error?: Error) => void;\n\nexport default class Cache {\n cacheDirectory: string;\n options: CacheOptions;\n\n constructor(cacheDirectory: string, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');\n this.cacheDirectory = cacheDirectory;\n this.options = options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null> {\n if (typeof options === 'function') {\n callback = options as GetCallback;\n options = null;\n }\n options = options || {};\n\n if (typeof callback === 'function') return (options as GetOptions).force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n return new Promise((resolve, reject) => {\n this.get(endpoint, options, (err, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n\n getSync(endpoint: string): object | null {\n // TODO: add async fetching\n return getSync.call(this, endpoint);\n }\n\n clear(callback?: ClearCallback): undefined | Promise<undefined> {\n if (typeof callback === 'function')\n return rimraf2(this.cacheDirectory, { disableGlob: true }, (_err) => {\n _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint\n callback();\n });\n return new Promise((resolve, reject) => {\n this.clear((err) => {\n err ? reject(err) : resolve(undefined);\n });\n });\n }\n}\n"],"names":["rimraf2","get","getSync","hash","update","Cache","endpoint","options","callback","force","call","Promise","resolve","reject","err","json","clear","cacheDirectory","disableGlob","_err","undefined","constructor","Error"],"mappings":"AAAA,OAAO,kBAAkB;AACzB,OAAOA,aAAa,UAAU;AAE9B,OAAOC,SAAS,WAAW;AAC3B,OAAOC,aAAa,eAAe;AACnC,OAAOC,UAAU,YAAY;AAC7B,OAAOC,YAAY,cAAc;AAYlB,IAAA,AAAMC,QAAN,MAAMA;IAYnBJ,IAAIK,QAAgB,EAAEC,OAAkC,EAAEC,QAAsB,EAAsC;QACpH,IAAI,OAAOD,YAAY,YAAY;YACjCC,WAAWD;YACXA,UAAU;QACZ;QACAA,UAAUA,WAAW,CAAC;QAEtB,IAAI,OAAOC,aAAa,YAAY,OAAO,AAACD,QAAuBE,KAAK,GAAGL,OAAOM,IAAI,CAAC,IAAI,EAAEJ,UAAUE,YAAYP,IAAIS,IAAI,CAAC,IAAI,EAAEJ,UAAUE;QAC5I,OAAO,IAAIG,QAAQ,CAACC,SAASC;YAC3B,IAAI,CAACZ,GAAG,CAACK,UAAUC,SAAS,CAACO,KAAKC;gBAChCD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;IAEAb,QAAQI,QAAgB,EAAiB;QACvC,2BAA2B;QAC3B,OAAOJ,QAAQQ,IAAI,CAAC,IAAI,EAAEJ;IAC5B;IAEAU,MAAMR,QAAwB,EAAkC;QAC9D,IAAI,OAAOA,aAAa,YACtB,OAAOR,QAAQ,IAAI,CAACiB,cAAc,EAAE;YAAEC,aAAa;QAAK,GAAG,CAACC;YAC1DA,OAAO,MAAM,kGAAkG;YAC/GX;QACF;QACF,OAAO,IAAIG,QAAQ,CAACC,SAASC;YAC3B,IAAI,CAACG,KAAK,CAAC,CAACF;gBACVA,MAAMD,OAAOC,OAAOF,QAAQQ;YAC9B;QACF;IACF;IAvCAC,YAAYJ,cAAsB,EAAEV,UAAwB,CAAC,CAAC,CAAE;QAC9D,IAAI,CAAE,CAAA,IAAI,YAAYF,KAAI,GAAI,MAAM,IAAIiB,MAAM;QAC9C,IAAI,CAACL,gBAAgB,MAAM,IAAIK,MAAM;QACrC,IAAI,CAACL,cAAc,GAAGA;QACtB,IAAI,CAACV,OAAO,GAAGA;QACf,IAAI,CAAC,IAAI,CAACA,OAAO,CAACJ,IAAI,EAAE,IAAI,CAACI,OAAO,CAACJ,IAAI,GAAGA;IAC9C;AAkCF;AA5CA,SAAqBE,mBA4CpB"}
|
package/dist/esm/update.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import get from 'get-remote';
|
|
3
|
-
import
|
|
3
|
+
import mkdirp from 'mkdirp-classic';
|
|
4
4
|
import writeFile from 'write-file-atomic';
|
|
5
5
|
export default function update(endpoint, callback) {
|
|
6
6
|
const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
|
|
7
|
-
|
|
7
|
+
mkdirp(this.cacheDirectory, (err)=>{
|
|
8
8
|
if (err && err.code !== 'EEXIST') return callback(err);
|
|
9
9
|
get(endpoint).json((err, res)=>{
|
|
10
10
|
if (err) return callback(err);
|
package/dist/esm/update.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport
|
|
1
|
+
{"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport writeFile from 'write-file-atomic';\n\nexport default function update(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n\n mkdirp(this.cacheDirectory, (err) => {\n if (err && err.code !== 'EEXIST') return callback(err);\n\n get(endpoint).json((err, res) => {\n if (err) return callback(err);\n\n const record = { headers: res.headers, body: res.body };\n writeFile(fullPath, JSON.stringify(record), 'utf8', (err) => {\n err ? callback(err) : callback(null, record.body);\n });\n });\n });\n}\n"],"names":["path","get","mkdirp","writeFile","update","endpoint","callback","fullPath","join","cacheDirectory","options","hash","err","code","json","res","record","headers","body","JSON","stringify"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,OAAOC,SAAS,aAAa;AAC7B,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,eAAe,oBAAoB;AAE1C,eAAe,SAASC,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,MAAMC,WAAWP,KAAKQ,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,UAAU,KAAK,CAAC;IAErFH,OAAO,IAAI,CAACO,cAAc,EAAE,CAACG;QAC3B,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOP,SAASM;QAElDX,IAAII,UAAUS,IAAI,CAAC,CAACF,KAAKG;YACvB,IAAIH,KAAK,OAAON,SAASM;YAEzB,MAAMI,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtDf,UAAUI,UAAUY,KAAKC,SAAS,CAACJ,SAAS,QAAQ,CAACJ;gBACnDA,MAAMN,SAASM,OAAON,SAAS,MAAMU,OAAOE,IAAI;YAClD;QACF;IACF;AACF"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,11 +2,16 @@ import './polyfills.cjs';
|
|
|
2
2
|
export interface CacheOptions {
|
|
3
3
|
hash?: (string: string) => string;
|
|
4
4
|
}
|
|
5
|
+
export interface GetOptions {
|
|
6
|
+
force?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export type GetCallback = (error?: Error, json?: object) => void;
|
|
9
|
+
export type ClearCallback = (error?: Error) => void;
|
|
5
10
|
export default class Cache {
|
|
6
11
|
cacheDirectory: string;
|
|
7
12
|
options: CacheOptions;
|
|
8
|
-
constructor(cacheDirectory:
|
|
9
|
-
get(endpoint: string, options
|
|
10
|
-
getSync(endpoint:
|
|
11
|
-
clear(callback
|
|
13
|
+
constructor(cacheDirectory: string, options?: CacheOptions);
|
|
14
|
+
get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null>;
|
|
15
|
+
getSync(endpoint: string): object | null;
|
|
16
|
+
clear(callback?: ClearCallback): undefined | Promise<undefined>;
|
|
12
17
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fetch-json-cache",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Caches fetched json. Updates when etag changes and is uses cache regardless if endpoint unreachable. Uses write-file-atomic for safe updates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"every",
|
|
@@ -40,15 +40,15 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"core-js": "^3.39.0",
|
|
43
|
-
"get-remote": "^1.
|
|
44
|
-
"
|
|
43
|
+
"get-remote": "^1.2.2",
|
|
44
|
+
"mkdirp-classic": "^0.5.3",
|
|
45
45
|
"rimraf2": "^2.8.2",
|
|
46
46
|
"write-file-atomic": "^1.3.4"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@biomejs/biome": "^1.9.4",
|
|
50
50
|
"@types/mocha": "^10.0.10",
|
|
51
|
-
"@types/node": "^
|
|
51
|
+
"@types/node": "^14.18.63",
|
|
52
52
|
"depcheck": "^1.4.7",
|
|
53
53
|
"fs-access-sync-compat": "^1.0.2",
|
|
54
54
|
"ts-dev-stack": "^1.4.0"
|