fetch-json-cache 1.0.0 → 1.1.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/README.md CHANGED
@@ -14,19 +14,19 @@ var cache = new Cache('/path/to/cache');
14
14
 
15
15
  // get
16
16
  cache.get('https://jsonplaceholder.typicode.com/users', function(err, json) {
17
- assert.ok(!err);
17
+ assert.ok(!err, err ? err.message : '');
18
18
  assert.ok(json.length > 0);
19
19
  })
20
20
 
21
21
  // get with forced update
22
22
  cache.get('https://jsonplaceholder.typicode.com/users', { force: true }, function(err, json) {
23
- assert.ok(!err);
23
+ assert.ok(!err, err ? err.message : '');
24
24
  assert.ok(json.length > 0);
25
25
  })
26
26
 
27
27
  // clear the cache
28
28
  cache.clear(function(err) {
29
- assert.ok(!err);
29
+ assert.ok(!err, err ? err.message : '');
30
30
  })
31
31
 
32
32
  //////////////
package/dist/cjs/get.js CHANGED
@@ -1,31 +1,45 @@
1
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) {
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getCache;
9
+ }
10
+ });
11
+ var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
+ var _path = /*#__PURE__*/ _interop_require_default(require("path"));
13
+ var _getremote = /*#__PURE__*/ _interop_require_default(require("get-remote"));
14
+ var _update = /*#__PURE__*/ _interop_require_default(require("./update.js"));
15
+ function _interop_require_default(obj) {
16
+ return obj && obj.__esModule ? obj : {
17
+ default: obj
18
+ };
19
+ }
20
+ function getCache(endpoint, callback) {
7
21
  var _this = this;
8
- var fullPath = path.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
9
- fs.readFile(fullPath, function(err, contents) {
22
+ var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
23
+ _fs.default.readFile(fullPath, function(err, contents) {
10
24
  // doesn't exist so create
11
25
  if (err) {
12
- update.call(_this, endpoint, function(err, json) {
26
+ _update.default.call(_this, endpoint, function(err, json) {
13
27
  err ? callback(err) : callback(null, json);
14
28
  });
15
29
  } else {
16
30
  var record = JSON.parse(contents.toString());
17
- get(endpoint).head(function(err, res) {
31
+ (0, _getremote.default)(endpoint).head(function(err, res) {
18
32
  // not accessible
19
33
  if (err) {
20
34
  if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);
21
35
  return callback(err);
22
36
  }
23
37
  if (res.headers.etag === record.headers.etag) return callback(null, record.body);
24
- update.call(_this, endpoint, function(err, json) {
38
+ _update.default.call(_this, endpoint, function(err, json) {
25
39
  err ? callback(err) : callback(null, json);
26
40
  });
27
41
  });
28
42
  }
29
43
  });
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; }
44
+ }
45
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -1 +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"}
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":";;;;+BAMA;;;eAAwBA;;;yDANT;2DACE;gEACD;6DAEG;;;;;;AAEJ,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,IAAIA,IAAIY,IAAI,KAAK,eAAeZ,IAAIa,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOvB,SAAS,MAAMc,OAAOU,IAAI;oBAC9H,OAAOxB,SAASS;gBAClB;gBACA,IAAIW,IAAIK,OAAO,CAACC,IAAI,KAAKZ,OAAOW,OAAO,CAACC,IAAI,EAAE,OAAO1B,SAAS,MAAMc,OAAOU,IAAI;gBAC/Eb,eAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;oBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;gBACvC;YACF;QACF;IACF;AACF"}
@@ -1,14 +1,28 @@
1
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"));
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getCacheAsync;
9
+ }
10
+ });
11
+ var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
+ var _path = /*#__PURE__*/ _interop_require_default(require("path"));
13
+ function _interop_require_default(obj) {
14
+ return obj && obj.__esModule ? obj : {
15
+ default: obj
16
+ };
17
+ }
18
+ function getCacheAsync(endpoint) {
19
+ var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
6
20
  try {
7
- var contents = fs.readFileSync(fullPath, 'utf8');
21
+ var contents = _fs.default.readFileSync(fullPath, 'utf8');
8
22
  var record = JSON.parse(contents.toString());
9
23
  return record.body;
10
24
  } catch (_err) {
11
25
  return null;
12
26
  }
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; }
27
+ }
28
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -1 +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"}
1
+ {"version":3,"sources":["getSync.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\nexport default 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":["getCacheAsync","endpoint","fullPath","path","join","cacheDirectory","options","hash","contents","fs","readFileSync","record","JSON","parse","toString","body","_err"],"mappings":";;;;+BAGA;;;eAAwBA;;;yDAHT;2DACE;;;;;;AAEF,SAASA,cAAcC,QAAQ;IAC5C,IAAMC,WAAWC,aAAI,CAACC,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,WAAU;IAC/E,IAAI;QACF,IAAMO,WAAWC,WAAE,CAACC,YAAY,CAACR,UAAU;QAC3C,IAAMS,SAASC,KAAKC,KAAK,CAACL,SAASM,QAAQ;QAC3C,OAAOH,OAAOI,IAAI;IACpB,EAAE,OAAOC,MAAM;QACb,OAAO;IACT;AACF"}
package/dist/cjs/hash.js CHANGED
@@ -1,6 +1,20 @@
1
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; }
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return hash;
9
+ }
10
+ });
11
+ var _crypto = /*#__PURE__*/ _interop_require_default(require("crypto"));
12
+ function _interop_require_default(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+ function hash(data) {
18
+ return _crypto.default.createHash('md5').update(data).digest('hex');
19
+ }
20
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -1 +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"}
1
+ {"version":3,"sources":["hash.ts"],"sourcesContent":["import crypto from 'crypto';\n\nexport default function hash(data) {\n return crypto.createHash('md5').update(data).digest('hex');\n}\n"],"names":["hash","data","crypto","createHash","update","digest"],"mappings":";;;;+BAEA;;;eAAwBA;;;6DAFL;;;;;;AAEJ,SAASA,KAAKC,IAAI;IAC/B,OAAOC,eAAM,CAACC,UAAU,CAAC,OAAOC,MAAM,CAACH,MAAMI,MAAM,CAAC;AACtD"}
package/dist/cjs/index.js CHANGED
@@ -10,29 +10,15 @@ Object.defineProperty(exports, "default", {
10
10
  });
11
11
  require("./polyfills.js");
12
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"));
13
+ var _get = /*#__PURE__*/ _interop_require_default(require("./get.js"));
14
+ var _getSync = /*#__PURE__*/ _interop_require_default(require("./getSync.js"));
15
+ var _hash = /*#__PURE__*/ _interop_require_default(require("./hash.js"));
16
+ var _update = /*#__PURE__*/ _interop_require_default(require("./update.js"));
17
17
  function _class_call_check(instance, Constructor) {
18
18
  if (!(instance instanceof Constructor)) {
19
19
  throw new TypeError("Cannot call a class as a function");
20
20
  }
21
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
22
  function _instanceof(left, right) {
37
23
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
38
24
  return !!right[Symbol.hasInstance](left);
@@ -47,57 +33,48 @@ function _interop_require_default(obj) {
47
33
  }
48
34
  var Cache = /*#__PURE__*/ function() {
49
35
  "use strict";
50
- function Cache(cacheDirectory, options) {
36
+ function Cache(cacheDirectory) {
37
+ var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
51
38
  _class_call_check(this, Cache);
52
39
  if (!_instanceof(this, Cache)) throw new Error('Cache needs to be called with new');
53
40
  if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');
54
41
  this.cacheDirectory = cacheDirectory;
55
- this.options = typeof options === 'undefined' ? {} : options;
42
+ this.options = options;
56
43
  if (!this.options.hash) this.options.hash = _hash.default;
57
44
  }
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
- }
45
+ var _proto = Cache.prototype;
46
+ _proto.get = function get(endpoint, options, callback) {
47
+ var _this = this;
48
+ if (typeof options === 'function') {
49
+ callback = options;
50
+ options = null;
99
51
  }
100
- ]);
52
+ options = options || {};
53
+ if (typeof callback === 'function') return options.force ? _update.default.call(this, endpoint, callback) : _get.default.call(this, endpoint, callback);
54
+ return new Promise(function(resolve, reject) {
55
+ _this.get(endpoint, options, function(err, json) {
56
+ err ? reject(err) : resolve(json);
57
+ });
58
+ });
59
+ };
60
+ _proto.getSync = function getSync(endpoint, _options) {
61
+ // TODO: add async fetching
62
+ return _getSync.default.call(this, endpoint);
63
+ };
64
+ _proto.clear = function clear(callback) {
65
+ var _this = this;
66
+ if (typeof callback === 'function') return (0, _rimraf2.default)(this.cacheDirectory, {
67
+ disableGlob: true
68
+ }, function(_err) {
69
+ _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
70
+ callback();
71
+ });
72
+ return new Promise(function(resolve, reject) {
73
+ _this.clear(function(err, json) {
74
+ err ? reject(err) : resolve(json);
75
+ });
76
+ });
77
+ };
101
78
  return Cache;
102
79
  }();
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; }
80
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -1 +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"}
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, _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":";;;;;;;eAYqBA;;;QAZd;8DACa;0DAEJ;8DACI;2DACH;6DACE;;;;;;;;;;;;;;;;;;AAMJ,IAAA,AAAMA,sBAAN;;aAAMA,MAIPC,cAAc;YAAEC,UAAAA,iEAAwB,CAAC;gCAJlCF;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,OAAO,EAAEK,QAAQ;;QACrC,IAAI,OAAOL,YAAY,YAAY;YACjCK,WAAWL;YACXA,UAAU;QACZ;QACAA,UAAUA,WAAW,CAAC;QAEtB,IAAI,OAAOK,aAAa,YAAY,OAAOL,QAAQM,KAAK,GAAGC,eAAM,CAACC,IAAI,CAAC,IAAI,EAAEJ,UAAUC,YAAYF,YAAG,CAACK,IAAI,CAAC,IAAI,EAAEJ,UAAUC;QAC5H,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,QAAQ,EAAEW,QAAQ;QACxB,2BAA2B;QAC3B,OAAOD,gBAAO,CAACN,IAAI,CAAC,IAAI,EAAEJ;IAC5B;IAEAY,OAAAA,KAWC,GAXDA,SAAAA,MAAMX,QAAQ;;QACZ,IAAI,OAAOA,aAAa,YACtB,OAAOY,IAAAA,gBAAO,EAAC,IAAI,CAAClB,cAAc,EAAE;YAAEmB,aAAa;QAAK,GAAG,SAACC;YAC1DA,OAAO,MAAM,kGAAkG;YAC/Gd;QACF;QACF,OAAO,IAAII,QAAQ,SAACC,SAASC;YAC3B,MAAKK,KAAK,CAAC,SAACJ,KAAKC;gBACfD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;WA3CmBf"}
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
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; }
3
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -1 +1 @@
1
- {"version":3,"sources":["polyfills.cjs"],"sourcesContent":["require('core-js/actual/promise');\n"],"names":["require"],"mappings":";AAAAA,QAAQ"}
1
+ {"version":3,"sources":["polyfills.cts"],"sourcesContent":["require('core-js/actual/promise');\n"],"names":["require"],"mappings":";AAAAA,QAAQ"}
@@ -1,22 +1,36 @@
1
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) {
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return update;
9
+ }
10
+ });
11
+ var _path = /*#__PURE__*/ _interop_require_default(require("path"));
12
+ var _getremote = /*#__PURE__*/ _interop_require_default(require("get-remote"));
13
+ var _mkpath = /*#__PURE__*/ _interop_require_default(require("mkpath"));
14
+ var _writefileatomic = /*#__PURE__*/ _interop_require_default(require("write-file-atomic"));
15
+ function _interop_require_default(obj) {
16
+ return obj && obj.__esModule ? obj : {
17
+ default: obj
18
+ };
19
+ }
20
+ function update(endpoint, callback) {
21
+ var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
22
+ (0, _mkpath.default)(this.cacheDirectory, function(err) {
9
23
  if (err && err.code !== 'EEXIST') return callback(err);
10
- get(endpoint).json(function(err, res) {
24
+ (0, _getremote.default)(endpoint).json(function(err, res) {
11
25
  if (err) return callback(err);
12
26
  var record = {
13
27
  headers: res.headers,
14
28
  body: res.body
15
29
  };
16
- writeFile(fullPath, JSON.stringify(record), 'utf8', function(err) {
30
+ (0, _writefileatomic.default)(fullPath, JSON.stringify(record), 'utf8', function(err) {
17
31
  err ? callback(err) : callback(null, record.body);
18
32
  });
19
33
  });
20
34
  });
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; }
35
+ }
36
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -1 +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"}
1
+ {"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport mkpath from 'mkpath';\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 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":["update","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","mkpath","err","code","get","json","res","record","headers","body","writeFile","JSON","stringify"],"mappings":";;;;+BAKA;;;eAAwBA;;;2DALP;gEACD;6DACG;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,eAAM,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"}
@@ -0,0 +1,28 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import get from 'get-remote';
4
+ import update from './update.mjs';
5
+ export default function getCache(endpoint, callback) {
6
+ const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
7
+ fs.readFile(fullPath, (err, contents)=>{
8
+ // doesn't exist so create
9
+ if (err) {
10
+ update.call(this, endpoint, (err, json)=>{
11
+ err ? callback(err) : callback(null, json);
12
+ });
13
+ } else {
14
+ const record = JSON.parse(contents.toString());
15
+ get(endpoint).head((err, res)=>{
16
+ // not accessible
17
+ if (err) {
18
+ if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);
19
+ return callback(err);
20
+ }
21
+ if (res.headers.etag === record.headers.etag) return callback(null, record.body);
22
+ update.call(this, endpoint, (err, json)=>{
23
+ err ? callback(err) : callback(null, json);
24
+ });
25
+ });
26
+ }
27
+ });
28
+ }
@@ -0,0 +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;AAEjC,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,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/EvB,OAAOY,IAAI,CAAC,IAAI,EAAEV,UAAU,CAACQ,KAAKG;oBAChCH,MAAMP,SAASO,OAAOP,SAAS,MAAMU;gBACvC;YACF;QACF;IACF;AACF"}
@@ -0,0 +1,12 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ export default function getCacheAsync(endpoint) {
4
+ const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
5
+ try {
6
+ const contents = fs.readFileSync(fullPath, 'utf8');
7
+ const record = JSON.parse(contents.toString());
8
+ return record.body;
9
+ } catch (_err) {
10
+ return null;
11
+ }
12
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["getSync.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\nexport default 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","path","getCacheAsync","endpoint","fullPath","join","cacheDirectory","options","hash","contents","readFileSync","record","JSON","parse","toString","body","_err"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,OAAOC,UAAU,OAAO;AAExB,eAAe,SAASC,cAAcC,QAAQ;IAC5C,MAAMC,WAAWH,KAAKI,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACL,UAAU,KAAK,CAAC;IACrF,IAAI;QACF,MAAMM,WAAWT,GAAGU,YAAY,CAACN,UAAU;QAC3C,MAAMO,SAASC,KAAKC,KAAK,CAACJ,SAASK,QAAQ;QAC3C,OAAOH,OAAOI,IAAI;IACpB,EAAE,OAAOC,MAAM;QACb,OAAO;IACT;AACF"}
@@ -0,0 +1,4 @@
1
+ import crypto from 'crypto';
2
+ export default function hash(data) {
3
+ return crypto.createHash('md5').update(data).digest('hex');
4
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["hash.ts"],"sourcesContent":["import crypto from 'crypto';\n\nexport default function hash(data) {\n return crypto.createHash('md5').update(data).digest('hex');\n}\n"],"names":["crypto","hash","data","createHash","update","digest"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAE5B,eAAe,SAASC,KAAKC,IAAI;IAC/B,OAAOF,OAAOG,UAAU,CAAC,OAAOC,MAAM,CAACF,MAAMG,MAAM,CAAC;AACtD"}
@@ -0,0 +1,46 @@
1
+ import './polyfills.cjs';
2
+ import rimraf2 from 'rimraf2';
3
+ import get from './get.mjs';
4
+ import getSync from './getSync.mjs';
5
+ import hash from './hash.mjs';
6
+ import update from './update.mjs';
7
+ let Cache = class Cache {
8
+ get(endpoint, options, callback) {
9
+ if (typeof options === 'function') {
10
+ callback = options;
11
+ options = null;
12
+ }
13
+ options = options || {};
14
+ if (typeof callback === 'function') return options.force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);
15
+ return new Promise((resolve, reject)=>{
16
+ this.get(endpoint, options, (err, json)=>{
17
+ err ? reject(err) : resolve(json);
18
+ });
19
+ });
20
+ }
21
+ getSync(endpoint, _options) {
22
+ // TODO: add async fetching
23
+ return getSync.call(this, endpoint);
24
+ }
25
+ clear(callback) {
26
+ if (typeof callback === 'function') return rimraf2(this.cacheDirectory, {
27
+ disableGlob: true
28
+ }, (_err)=>{
29
+ _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
30
+ callback();
31
+ });
32
+ return new Promise((resolve, reject)=>{
33
+ this.clear((err, json)=>{
34
+ err ? reject(err) : resolve(json);
35
+ });
36
+ });
37
+ }
38
+ constructor(cacheDirectory, options = {}){
39
+ if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');
40
+ if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');
41
+ this.cacheDirectory = cacheDirectory;
42
+ this.options = options;
43
+ if (!this.options.hash) this.options.hash = hash;
44
+ }
45
+ };
46
+ export { Cache as default };
@@ -0,0 +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, _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":["rimraf2","get","getSync","hash","update","Cache","endpoint","options","callback","force","call","Promise","resolve","reject","err","json","_options","clear","cacheDirectory","disableGlob","_err","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;AAMlB,IAAA,AAAMC,QAAN,MAAMA;IAYnBJ,IAAIK,QAAgB,EAAEC,OAAO,EAAEC,QAAQ,EAAE;QACvC,IAAI,OAAOD,YAAY,YAAY;YACjCC,WAAWD;YACXA,UAAU;QACZ;QACAA,UAAUA,WAAW,CAAC;QAEtB,IAAI,OAAOC,aAAa,YAAY,OAAOD,QAAQE,KAAK,GAAGL,OAAOM,IAAI,CAAC,IAAI,EAAEJ,UAAUE,YAAYP,IAAIS,IAAI,CAAC,IAAI,EAAEJ,UAAUE;QAC5H,OAAO,IAAIG,QAAQ,CAACC,SAASC;YAC3B,IAAI,CAACZ,GAAG,CAACK,UAAUC,SAAS,CAACO,KAAKC;gBAChCD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;IAEAb,QAAQI,QAAQ,EAAEU,QAAQ,EAAE;QAC1B,2BAA2B;QAC3B,OAAOd,QAAQQ,IAAI,CAAC,IAAI,EAAEJ;IAC5B;IAEAW,MAAMT,QAAQ,EAAE;QACd,IAAI,OAAOA,aAAa,YACtB,OAAOR,QAAQ,IAAI,CAACkB,cAAc,EAAE;YAAEC,aAAa;QAAK,GAAG,CAACC;YAC1DA,OAAO,MAAM,kGAAkG;YAC/GZ;QACF;QACF,OAAO,IAAIG,QAAQ,CAACC,SAASC;YAC3B,IAAI,CAACI,KAAK,CAAC,CAACH,KAAKC;gBACfD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;IAvCAM,YAAYH,cAAc,EAAEX,UAAwB,CAAC,CAAC,CAAE;QACtD,IAAI,CAAE,CAAA,IAAI,YAAYF,KAAI,GAAI,MAAM,IAAIiB,MAAM;QAC9C,IAAI,CAACJ,gBAAgB,MAAM,IAAII,MAAM;QACrC,IAAI,CAACJ,cAAc,GAAGA;QACtB,IAAI,CAACX,OAAO,GAAGA;QACf,IAAI,CAAC,IAAI,CAACA,OAAO,CAACJ,IAAI,EAAE,IAAI,CAACI,OAAO,CAACJ,IAAI,GAAGA;IAC9C;AAkCF;AA5CA,SAAqBE,mBA4CpB"}
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1 @@
1
+ require('core-js/actual/promise');
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["polyfills.cts"],"sourcesContent":["require('core-js/actual/promise');\n"],"names":["require"],"mappings":"AAAAA,QAAQ"}
@@ -0,0 +1,20 @@
1
+ import path from 'path';
2
+ import get from 'get-remote';
3
+ import mkpath from 'mkpath';
4
+ import writeFile from 'write-file-atomic';
5
+ export default function update(endpoint, callback) {
6
+ const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
7
+ mkpath(this.cacheDirectory, (err)=>{
8
+ if (err && err.code !== 'EEXIST') return callback(err);
9
+ get(endpoint).json((err, res)=>{
10
+ if (err) return callback(err);
11
+ const record = {
12
+ headers: res.headers,
13
+ body: res.body
14
+ };
15
+ writeFile(fullPath, JSON.stringify(record), 'utf8', (err)=>{
16
+ err ? callback(err) : callback(null, record.body);
17
+ });
18
+ });
19
+ });
20
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport mkpath from 'mkpath';\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 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","get","mkpath","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,SAAS;AAC5B,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"}
@@ -1,2 +1 @@
1
- declare function _exports(endpoint: any, callback: any): void;
2
- export = _exports;
1
+ export default function getCache(endpoint: any, callback: any): void;
@@ -1,2 +1 @@
1
- declare function _exports(endpoint: any): any;
2
- export = _exports;
1
+ export default function getCacheAsync(endpoint: any): any;
@@ -1,2 +1 @@
1
- declare function _exports(data: any): string;
2
- export = _exports;
1
+ export default function hash(data: any): string;
@@ -0,0 +1,12 @@
1
+ import './polyfills.cjs';
2
+ export interface CacheOptions {
3
+ hash?: (string: string) => string;
4
+ }
5
+ export default class Cache {
6
+ cacheDirectory: string;
7
+ options: CacheOptions;
8
+ constructor(cacheDirectory: any, options?: CacheOptions);
9
+ get(endpoint: string, options: any, callback: any): any;
10
+ getSync(endpoint: any, _options: any): any;
11
+ clear(callback: any): any;
12
+ }
@@ -1,2 +1 @@
1
- declare function _exports(endpoint: any, callback: any): void;
2
- export = _exports;
1
+ export default function update(endpoint: any, callback: any): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-json-cache",
3
- "version": "1.0.0",
3
+ "version": "1.1.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,7 +15,17 @@
15
15
  },
16
16
  "license": "MIT",
17
17
  "author": "Kevin Malakoff <kmalakoff@gmail.com> (https://github.com/kmalakoff)",
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/esm/index.mjs",
22
+ "require": "./dist/cjs/index.js",
23
+ "types": "./dist/types/index.d.ts"
24
+ },
25
+ "./*": "./*"
26
+ },
18
27
  "main": "dist/cjs/index.js",
28
+ "module": "dist/esm/index.mjs",
19
29
  "types": "dist/types/index.d.ts",
20
30
  "files": [
21
31
  "dist"
@@ -24,32 +34,33 @@
24
34
  "build": "tsds build",
25
35
  "deploy": "tsds deploy",
26
36
  "format": "biome check --write --unsafe src/ test/",
27
- "test": "tsds test:node --timeout=10000",
37
+ "test": "tsds test:node --no-timeouts",
28
38
  "test:engines": "nvu engines npm test",
29
39
  "version": "tsds version"
30
40
  },
31
41
  "dependencies": {
32
42
  "core-js": "^3.39.0",
33
- "get-remote": "^1.0.0",
43
+ "get-remote": "^1.0.2",
34
44
  "mkpath": "^1.0.0",
35
- "rimraf2": "^2.7.1",
45
+ "rimraf2": "^2.8.2",
36
46
  "write-file-atomic": "^1.3.4"
37
47
  },
38
48
  "devDependencies": {
39
49
  "@biomejs/biome": "^1.9.4",
40
50
  "@types/mocha": "^10.0.10",
41
- "@types/node": "^22.10.1",
51
+ "@types/node": "^22.10.2",
42
52
  "depcheck": "^1.4.7",
43
53
  "fs-access-sync-compat": "^1.0.2",
44
- "ts-dev-stack": "^0.16.2"
54
+ "ts-dev-stack": "^1.4.0"
45
55
  },
46
56
  "engines": {
47
57
  "node": ">=0.8"
48
58
  },
49
59
  "tsds": {
50
- "source": "src/index.mjs",
60
+ "source": "src/index.ts",
51
61
  "targets": [
52
- "cjs"
62
+ "cjs",
63
+ "esm"
53
64
  ]
54
65
  }
55
66
  }
@@ -1,8 +0,0 @@
1
- export default class Cache {
2
- constructor(cacheDirectory: any, options: any);
3
- cacheDirectory: any;
4
- options: any;
5
- get(endpoint: any, options: any, callback: any): any;
6
- getSync(endpoint: any, _options: any): any;
7
- clear(callback: any): any;
8
- }