fetch-json-cache 0.2.8 → 1.0.0
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 +31 -0
- package/dist/cjs/get.js.map +1 -0
- package/dist/cjs/getSync.js +14 -0
- package/dist/cjs/getSync.js.map +1 -0
- package/dist/cjs/hash.js +6 -0
- package/dist/cjs/hash.js.map +1 -0
- package/dist/cjs/index.js +103 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/polyfills.js +3 -0
- package/dist/cjs/polyfills.js.map +1 -0
- package/dist/cjs/update.js +22 -0
- package/dist/cjs/update.js.map +1 -0
- package/dist/types/get.d.ts +2 -0
- package/dist/types/getSync.d.ts +2 -0
- package/dist/types/hash.d.ts +2 -0
- package/dist/types/index.d.mts +8 -0
- package/dist/types/polyfills.d.cts +1 -0
- package/dist/types/update.d.ts +2 -0
- package/package.json +24 -22
- package/lib/get.js +0 -33
- package/lib/getSync.js +0 -13
- package/lib/hash.js +0 -5
- package/lib/index.js +0 -52
- package/lib/polyfills.js +0 -1
- package/lib/update.js +0 -21
package/dist/cjs/get.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var fs = require('fs');
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var get = require('get-remote');
|
|
5
|
+
var update = require('./update');
|
|
6
|
+
module.exports = function getCache(endpoint, callback) {
|
|
7
|
+
var _this = this;
|
|
8
|
+
var fullPath = path.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
|
|
9
|
+
fs.readFile(fullPath, function(err, contents) {
|
|
10
|
+
// doesn't exist so create
|
|
11
|
+
if (err) {
|
|
12
|
+
update.call(_this, endpoint, function(err, json) {
|
|
13
|
+
err ? callback(err) : callback(null, json);
|
|
14
|
+
});
|
|
15
|
+
} else {
|
|
16
|
+
var record = JSON.parse(contents.toString());
|
|
17
|
+
get(endpoint).head(function(err, res) {
|
|
18
|
+
// not accessible
|
|
19
|
+
if (err) {
|
|
20
|
+
if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);
|
|
21
|
+
return callback(err);
|
|
22
|
+
}
|
|
23
|
+
if (res.headers.etag === record.headers.etag) return callback(null, record.body);
|
|
24
|
+
update.call(_this, endpoint, function(err, json) {
|
|
25
|
+
err ? callback(err) : callback(null, json);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["get.js"],"sourcesContent":["const fs = require('fs');\nconst path = require('path');\nconst get = require('get-remote');\n\nconst update = require('./update');\n\nmodule.exports = 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","require","path","get","update","module","exports","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,IAAMA,KAAKC,QAAQ;AACnB,IAAMC,OAAOD,QAAQ;AACrB,IAAME,MAAMF,QAAQ;AAEpB,IAAMG,SAASH,QAAQ;AAEvBI,OAAOC,OAAO,GAAG,SAASC,SAASC,QAAQ,EAAEC,QAAQ;;IACnD,IAAMC,WAAWR,KAAKS,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,WAAU;IAC/ER,GAAGe,QAAQ,CAACL,UAAU,SAACM,KAAKC;QAC1B,0BAA0B;QAC1B,IAAID,KAAK;YACPZ,OAAOc,IAAI,QAAOV,UAAU,SAACQ,KAAKG;gBAChCH,MAAMP,SAASO,OAAOP,SAAS,MAAMU;YACvC;QACF,OAEK;YACH,IAAMC,SAASC,KAAKC,KAAK,CAACL,SAASM,QAAQ;YAC3CpB,IAAIK,UAAUgB,IAAI,CAAC,SAACR,KAAKS;gBACvB,iBAAiB;gBACjB,IAAIT,KAAK;oBACP,IAAIA,IAAIU,IAAI,KAAK,eAAeV,IAAIW,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOnB,SAAS,MAAMW,OAAOS,IAAI;oBAC9H,OAAOpB,SAASO;gBAClB;gBACA,IAAIS,IAAIK,OAAO,CAACC,IAAI,KAAKX,OAAOU,OAAO,CAACC,IAAI,EAAE,OAAOtB,SAAS,MAAMW,OAAOS,IAAI;gBAC/EzB,OAAOc,IAAI,QAAOV,UAAU,SAACQ,KAAKG;oBAChCH,MAAMP,SAASO,OAAOP,SAAS,MAAMU;gBACvC;YACF;QACF;IACF;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var fs = require('fs');
|
|
3
|
+
var path = require('path');
|
|
4
|
+
module.exports = function getCacheAsync(endpoint) {
|
|
5
|
+
var fullPath = path.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
|
|
6
|
+
try {
|
|
7
|
+
var contents = fs.readFileSync(fullPath, 'utf8');
|
|
8
|
+
var record = JSON.parse(contents.toString());
|
|
9
|
+
return record.body;
|
|
10
|
+
} catch (_err) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["getSync.js"],"sourcesContent":["const fs = require('fs');\nconst path = require('path');\n\nmodule.exports = function getCacheAsync(endpoint) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n try {\n const contents = fs.readFileSync(fullPath, 'utf8');\n const record = JSON.parse(contents.toString());\n return record.body;\n } catch (_err) {\n return null;\n }\n};\n"],"names":["fs","require","path","module","exports","getCacheAsync","endpoint","fullPath","join","cacheDirectory","options","hash","contents","readFileSync","record","JSON","parse","toString","body","_err"],"mappings":";AAAA,IAAMA,KAAKC,QAAQ;AACnB,IAAMC,OAAOD,QAAQ;AAErBE,OAAOC,OAAO,GAAG,SAASC,cAAcC,QAAQ;IAC9C,IAAMC,WAAWL,KAAKM,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACL,WAAU;IAC/E,IAAI;QACF,IAAMM,WAAWZ,GAAGa,YAAY,CAACN,UAAU;QAC3C,IAAMO,SAASC,KAAKC,KAAK,CAACJ,SAASK,QAAQ;QAC3C,OAAOH,OAAOI,IAAI;IACpB,EAAE,OAAOC,MAAM;QACb,OAAO;IACT;AACF"}
|
package/dist/cjs/hash.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var crypto = require('crypto');
|
|
3
|
+
module.exports = function hash(data) {
|
|
4
|
+
return crypto.createHash('md5').update(data).digest('hex');
|
|
5
|
+
};
|
|
6
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["hash.js"],"sourcesContent":["const crypto = require('crypto');\n\nmodule.exports = function hash(data) {\n return crypto.createHash('md5').update(data).digest('hex');\n};\n"],"names":["crypto","require","module","exports","hash","data","createHash","update","digest"],"mappings":";AAAA,IAAMA,SAASC,QAAQ;AAEvBC,OAAOC,OAAO,GAAG,SAASC,KAAKC,IAAI;IACjC,OAAOL,OAAOM,UAAU,CAAC,OAAOC,MAAM,CAACF,MAAMG,MAAM,CAAC;AACtD"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return Cache;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
require("./polyfills.js");
|
|
12
|
+
var _rimraf2 = /*#__PURE__*/ _interop_require_default(require("rimraf2"));
|
|
13
|
+
var _get = /*#__PURE__*/ _interop_require_default(require("./get"));
|
|
14
|
+
var _getSync = /*#__PURE__*/ _interop_require_default(require("./getSync"));
|
|
15
|
+
var _hash = /*#__PURE__*/ _interop_require_default(require("./hash"));
|
|
16
|
+
var _update = /*#__PURE__*/ _interop_require_default(require("./update"));
|
|
17
|
+
function _class_call_check(instance, Constructor) {
|
|
18
|
+
if (!(instance instanceof Constructor)) {
|
|
19
|
+
throw new TypeError("Cannot call a class as a function");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function _defineProperties(target, props) {
|
|
23
|
+
for(var i = 0; i < props.length; i++){
|
|
24
|
+
var descriptor = props[i];
|
|
25
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
26
|
+
descriptor.configurable = true;
|
|
27
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
28
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
32
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
33
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
34
|
+
return Constructor;
|
|
35
|
+
}
|
|
36
|
+
function _instanceof(left, right) {
|
|
37
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
38
|
+
return !!right[Symbol.hasInstance](left);
|
|
39
|
+
} else {
|
|
40
|
+
return left instanceof right;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function _interop_require_default(obj) {
|
|
44
|
+
return obj && obj.__esModule ? obj : {
|
|
45
|
+
default: obj
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
var Cache = /*#__PURE__*/ function() {
|
|
49
|
+
"use strict";
|
|
50
|
+
function Cache(cacheDirectory, options) {
|
|
51
|
+
_class_call_check(this, Cache);
|
|
52
|
+
if (!_instanceof(this, Cache)) throw new Error('Cache needs to be called with new');
|
|
53
|
+
if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');
|
|
54
|
+
this.cacheDirectory = cacheDirectory;
|
|
55
|
+
this.options = typeof options === 'undefined' ? {} : options;
|
|
56
|
+
if (!this.options.hash) this.options.hash = _hash.default;
|
|
57
|
+
}
|
|
58
|
+
_create_class(Cache, [
|
|
59
|
+
{
|
|
60
|
+
key: "get",
|
|
61
|
+
value: function get(endpoint, options, callback) {
|
|
62
|
+
var _this = this;
|
|
63
|
+
if (typeof options === 'function') {
|
|
64
|
+
callback = options;
|
|
65
|
+
options = null;
|
|
66
|
+
}
|
|
67
|
+
options = options || {};
|
|
68
|
+
if (typeof callback === 'function') return options.force ? _update.default.call(this, endpoint, callback) : _get.default.call(this, endpoint, callback);
|
|
69
|
+
return new Promise(function(resolve, reject) {
|
|
70
|
+
_this.get(endpoint, options, function(err, json) {
|
|
71
|
+
err ? reject(err) : resolve(json);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
key: "getSync",
|
|
78
|
+
value: function getSync(endpoint, _options) {
|
|
79
|
+
// TODO: add async fetching
|
|
80
|
+
return _getSync.default.call(this, endpoint);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
key: "clear",
|
|
85
|
+
value: function clear(callback) {
|
|
86
|
+
var _this = this;
|
|
87
|
+
if (typeof callback === 'function') return (0, _rimraf2.default)(this.cacheDirectory, {
|
|
88
|
+
disableGlob: true
|
|
89
|
+
}, function(_err) {
|
|
90
|
+
_err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
|
|
91
|
+
callback();
|
|
92
|
+
});
|
|
93
|
+
return new Promise(function(resolve, reject) {
|
|
94
|
+
_this.clear(function(err, json) {
|
|
95
|
+
err ? reject(err) : resolve(json);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
]);
|
|
101
|
+
return Cache;
|
|
102
|
+
}();
|
|
103
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.mjs"],"sourcesContent":["import './polyfills.cjs';\nimport rimraf2 from 'rimraf2';\n\nimport get from './get';\nimport getSync from './getSync';\nimport hash from './hash';\nimport update from './update';\n\nexport default class Cache {\n constructor(cacheDirectory, options) {\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 = typeof options === 'undefined' ? {} : options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint, 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, _options) {\n // TODO: add async fetching\n return getSync.call(this, endpoint);\n }\n\n clear(callback) {\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, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n}\n"],"names":["Cache","cacheDirectory","options","Error","hash","get","endpoint","callback","force","update","call","Promise","resolve","reject","err","json","getSync","_options","clear","rimraf2","disableGlob","_err"],"mappings":";;;;;;;eAQqBA;;;QARd;8DACa;0DAEJ;8DACI;2DACH;6DACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEJ,IAAA,AAAMA,sBAAN;;aAAMA,MACPC,cAAc,EAAEC,OAAO;gCADhBF;QAEjB,IAAI,CAAE,AAAI,YAAJ,IAAI,EAFOA,QAEa,MAAM,IAAIG,MAAM;QAC9C,IAAI,CAACF,gBAAgB,MAAM,IAAIE,MAAM;QACrC,IAAI,CAACF,cAAc,GAAGA;QACtB,IAAI,CAACC,OAAO,GAAG,OAAOA,YAAY,cAAc,CAAC,IAAIA;QACrD,IAAI,CAAC,IAAI,CAACA,OAAO,CAACE,IAAI,EAAE,IAAI,CAACF,OAAO,CAACE,IAAI,GAAGA,aAAI;;kBAN/BJ;;YASnBK,KAAAA;mBAAAA,SAAAA,IAAIC,QAAQ,EAAEJ,OAAO,EAAEK,QAAQ;;gBAC7B,IAAI,OAAOL,YAAY,YAAY;oBACjCK,WAAWL;oBACXA,UAAU;gBACZ;gBACAA,UAAUA,WAAW,CAAC;gBAEtB,IAAI,OAAOK,aAAa,YAAY,OAAOL,QAAQM,KAAK,GAAGC,eAAM,CAACC,IAAI,CAAC,IAAI,EAAEJ,UAAUC,YAAYF,YAAG,CAACK,IAAI,CAAC,IAAI,EAAEJ,UAAUC;gBAC5H,OAAO,IAAII,QAAQ,SAACC,SAASC;oBAC3B,MAAKR,GAAG,CAACC,UAAUJ,SAAS,SAACY,KAAKC;wBAChCD,MAAMD,OAAOC,OAAOF,QAAQG;oBAC9B;gBACF;YACF;;;YAEAC,KAAAA;mBAAAA,SAAAA,QAAQV,QAAQ,EAAEW,QAAQ;gBACxB,2BAA2B;gBAC3B,OAAOD,gBAAO,CAACN,IAAI,CAAC,IAAI,EAAEJ;YAC5B;;;YAEAY,KAAAA;mBAAAA,SAAAA,MAAMX,QAAQ;;gBACZ,IAAI,OAAOA,aAAa,YACtB,OAAOY,IAAAA,gBAAO,EAAC,IAAI,CAAClB,cAAc,EAAE;oBAAEmB,aAAa;gBAAK,GAAG,SAACC;oBAC1DA,OAAO,MAAM,kGAAkG;oBAC/Gd;gBACF;gBACF,OAAO,IAAII,QAAQ,SAACC,SAASC;oBAC3B,MAAKK,KAAK,CAAC,SAACJ,KAAKC;wBACfD,MAAMD,OAAOC,OAAOF,QAAQG;oBAC9B;gBACF;YACF;;;WAxCmBf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
require('core-js/actual/promise');
|
|
3
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["polyfills.cjs"],"sourcesContent":["require('core-js/actual/promise');\n"],"names":["require"],"mappings":";AAAAA,QAAQ"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var path = require('path');
|
|
3
|
+
var get = require('get-remote');
|
|
4
|
+
var mkpath = require('mkpath');
|
|
5
|
+
var writeFile = require('write-file-atomic');
|
|
6
|
+
module.exports = function update(endpoint, callback) {
|
|
7
|
+
var fullPath = path.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
|
|
8
|
+
mkpath(this.cacheDirectory, function(err) {
|
|
9
|
+
if (err && err.code !== 'EEXIST') return callback(err);
|
|
10
|
+
get(endpoint).json(function(err, res) {
|
|
11
|
+
if (err) return callback(err);
|
|
12
|
+
var record = {
|
|
13
|
+
headers: res.headers,
|
|
14
|
+
body: res.body
|
|
15
|
+
};
|
|
16
|
+
writeFile(fullPath, JSON.stringify(record), 'utf8', function(err) {
|
|
17
|
+
err ? callback(err) : callback(null, record.body);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["update.js"],"sourcesContent":["const path = require('path');\nconst get = require('get-remote');\nconst mkpath = require('mkpath');\nconst writeFile = require('write-file-atomic');\n\nmodule.exports = function update(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n\n mkpath(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","require","get","mkpath","writeFile","module","exports","update","endpoint","callback","fullPath","join","cacheDirectory","options","hash","err","code","json","res","record","headers","body","JSON","stringify"],"mappings":";AAAA,IAAMA,OAAOC,QAAQ;AACrB,IAAMC,MAAMD,QAAQ;AACpB,IAAME,SAASF,QAAQ;AACvB,IAAMG,YAAYH,QAAQ;AAE1BI,OAAOC,OAAO,GAAG,SAASC,OAAOC,QAAQ,EAAEC,QAAQ;IACjD,IAAMC,WAAWV,KAAKW,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,WAAU;IAE/EL,OAAO,IAAI,CAACS,cAAc,EAAE,SAACG;QAC3B,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOP,SAASM;QAElDb,IAAIM,UAAUS,IAAI,CAAC,SAACF,KAAKG;YACvB,IAAIH,KAAK,OAAON,SAASM;YAEzB,IAAMI,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtDjB,UAAUM,UAAUY,KAAKC,SAAS,CAACJ,SAAS,QAAQ,SAACJ;gBACnDA,MAAMN,SAASM,OAAON,SAAS,MAAMU,OAAOE,IAAI;YAClD;QACF;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fetch-json-cache",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
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",
|
|
@@ -15,39 +15,41 @@
|
|
|
15
15
|
},
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"author": "Kevin Malakoff <kmalakoff@gmail.com> (https://github.com/kmalakoff)",
|
|
18
|
-
"main": "
|
|
18
|
+
"main": "dist/cjs/index.js",
|
|
19
|
+
"types": "dist/types/index.d.ts",
|
|
19
20
|
"files": [
|
|
20
|
-
"
|
|
21
|
+
"dist"
|
|
21
22
|
],
|
|
22
23
|
"scripts": {
|
|
23
|
-
"build": "",
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"test": "
|
|
28
|
-
"
|
|
24
|
+
"build": "tsds build",
|
|
25
|
+
"deploy": "tsds deploy",
|
|
26
|
+
"format": "biome check --write --unsafe src/ test/",
|
|
27
|
+
"test": "tsds test:node --timeout=10000",
|
|
28
|
+
"test:engines": "nvu engines npm test",
|
|
29
|
+
"version": "tsds version"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"core-js": "^3.
|
|
32
|
-
"get-remote": "^0.
|
|
32
|
+
"core-js": "^3.39.0",
|
|
33
|
+
"get-remote": "^1.0.0",
|
|
33
34
|
"mkpath": "^1.0.0",
|
|
34
|
-
"
|
|
35
|
+
"rimraf2": "^2.7.1",
|
|
35
36
|
"write-file-atomic": "^1.3.4"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"eslint-config-standard": "^17.0.0",
|
|
43
|
-
"eslint-plugin-import": "^2.26.0",
|
|
44
|
-
"eslint-plugin-n": "^15.2.4",
|
|
45
|
-
"eslint-plugin-promise": "^6.0.0",
|
|
39
|
+
"@biomejs/biome": "^1.9.4",
|
|
40
|
+
"@types/mocha": "^10.0.10",
|
|
41
|
+
"@types/node": "^22.10.1",
|
|
42
|
+
"depcheck": "^1.4.7",
|
|
46
43
|
"fs-access-sync-compat": "^1.0.2",
|
|
47
|
-
"
|
|
48
|
-
"prettier": "^2.7.1"
|
|
44
|
+
"ts-dev-stack": "^0.16.2"
|
|
49
45
|
},
|
|
50
46
|
"engines": {
|
|
51
47
|
"node": ">=0.8"
|
|
48
|
+
},
|
|
49
|
+
"tsds": {
|
|
50
|
+
"source": "src/index.mjs",
|
|
51
|
+
"targets": [
|
|
52
|
+
"cjs"
|
|
53
|
+
]
|
|
52
54
|
}
|
|
53
55
|
}
|
package/lib/get.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
var fs = require('fs');
|
|
2
|
-
var path = require('path');
|
|
3
|
-
var get = require('get-remote');
|
|
4
|
-
|
|
5
|
-
var update = require('./update');
|
|
6
|
-
|
|
7
|
-
module.exports = function getCache(endpoint, callback) {
|
|
8
|
-
var self = this;
|
|
9
|
-
var fullPath = path.join(this.cacheDirectory, this.options.hash(endpoint) + '.json');
|
|
10
|
-
fs.readFile(fullPath, function (err, contents) {
|
|
11
|
-
// doesn't exist so create
|
|
12
|
-
if (err) {
|
|
13
|
-
update.call(self, endpoint, function (err, json) {
|
|
14
|
-
err ? callback(err) : callback(null, json);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
// check existing if etag changed and if so, recache
|
|
18
|
-
else {
|
|
19
|
-
var record = JSON.parse(contents.toString());
|
|
20
|
-
get(endpoint).head(function (err, res) {
|
|
21
|
-
// not accessible
|
|
22
|
-
if (err) {
|
|
23
|
-
if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);
|
|
24
|
-
return callback(err);
|
|
25
|
-
}
|
|
26
|
-
if (res.headers.etag === record.headers.etag) return callback(null, record.body);
|
|
27
|
-
update.call(self, endpoint, function (err, json) {
|
|
28
|
-
err ? callback(err) : callback(null, json);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
};
|
package/lib/getSync.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
var fs = require('fs');
|
|
2
|
-
var path = require('path');
|
|
3
|
-
|
|
4
|
-
module.exports = function getCacheAsync(endpoint) {
|
|
5
|
-
var fullPath = path.join(this.cacheDirectory, this.options.hash(endpoint) + '.json');
|
|
6
|
-
try {
|
|
7
|
-
var contents = fs.readFileSync(fullPath, 'utf8');
|
|
8
|
-
var record = JSON.parse(contents.toString());
|
|
9
|
-
return record.body;
|
|
10
|
-
} catch (err) {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
};
|
package/lib/hash.js
DELETED
package/lib/index.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
require('./polyfills');
|
|
2
|
-
var rimraf = require('rimraf');
|
|
3
|
-
|
|
4
|
-
var get = require('./get');
|
|
5
|
-
var getSync = require('./getSync');
|
|
6
|
-
var hash = require('./hash');
|
|
7
|
-
var update = require('./update');
|
|
8
|
-
|
|
9
|
-
function Cache(cacheDirectory, options) {
|
|
10
|
-
if (!(this instanceof Cache)) return new Cache(cacheDirectory, options);
|
|
11
|
-
if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');
|
|
12
|
-
this.cacheDirectory = cacheDirectory;
|
|
13
|
-
this.options = typeof options === 'undefined' ? {} : options;
|
|
14
|
-
if (!this.options.hash) this.options.hash = hash;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
Cache.prototype.get = function (endpoint, options, callback) {
|
|
18
|
-
var self = this;
|
|
19
|
-
if (typeof options === 'function') {
|
|
20
|
-
callback = options;
|
|
21
|
-
options = null;
|
|
22
|
-
}
|
|
23
|
-
options = options || {};
|
|
24
|
-
|
|
25
|
-
if (typeof callback === 'function') return options.force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);
|
|
26
|
-
return new Promise(function (resolve, reject) {
|
|
27
|
-
self.get(endpoint, options, function (err, json) {
|
|
28
|
-
err ? reject(err) : resolve(json);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
Cache.prototype.getSync = function (endpoint, options) {
|
|
34
|
-
// TODO: add async fetching
|
|
35
|
-
return getSync.call(this, endpoint);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
Cache.prototype.clear = function (callback) {
|
|
39
|
-
var self = this;
|
|
40
|
-
if (typeof callback === 'function')
|
|
41
|
-
return rimraf(this.cacheDirectory, function (err) {
|
|
42
|
-
err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
|
|
43
|
-
callback();
|
|
44
|
-
});
|
|
45
|
-
return new Promise(function (resolve, reject) {
|
|
46
|
-
self.clear(function (err, json) {
|
|
47
|
-
err ? reject(err) : resolve(json);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
module.exports = Cache;
|
package/lib/polyfills.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
require('core-js/actual/promise');
|
package/lib/update.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
var path = require('path');
|
|
2
|
-
var get = require('get-remote');
|
|
3
|
-
var mkpath = require('mkpath');
|
|
4
|
-
var writeFile = require('write-file-atomic');
|
|
5
|
-
|
|
6
|
-
module.exports = function update(endpoint, callback) {
|
|
7
|
-
var fullPath = path.join(this.cacheDirectory, this.options.hash(endpoint) + '.json');
|
|
8
|
-
|
|
9
|
-
mkpath(this.cacheDirectory, function (err) {
|
|
10
|
-
if (err && err.code !== 'EEXIST') return callback(err);
|
|
11
|
-
|
|
12
|
-
get(endpoint).json(function (err, res) {
|
|
13
|
-
if (err) return callback(err);
|
|
14
|
-
|
|
15
|
-
var record = { headers: res.headers, body: res.body };
|
|
16
|
-
writeFile(fullPath, JSON.stringify(record), 'utf8', function (err) {
|
|
17
|
-
err ? callback(err) : callback(null, record.body);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
};
|