fetch-json-cache 1.2.4 → 1.2.6

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.
@@ -11,7 +11,7 @@ Object.defineProperty(exports, "default", {
11
11
  var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
12
  var _path = /*#__PURE__*/ _interop_require_default(require("path"));
13
13
  var _getremote = /*#__PURE__*/ _interop_require_default(require("get-remote"));
14
- var _update = /*#__PURE__*/ _interop_require_default(require("./update.js"));
14
+ var _update = /*#__PURE__*/ _interop_require_default(require("./update.cjs"));
15
15
  function _interop_require_default(obj) {
16
16
  return obj && obj.__esModule ? obj : {
17
17
  default: obj
@@ -19,7 +19,7 @@ function _interop_require_default(obj) {
19
19
  }
20
20
  function getCache(endpoint, callback) {
21
21
  var _this = this;
22
- var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
22
+ var fullPath = _path.default.join(this.cachePath, "".concat(this.options.hash(endpoint), ".json"));
23
23
  _fs.default.readFile(fullPath, function(err, contents) {
24
24
  // doesn't exist so create
25
25
  if (err) {
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update';\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.cachePath, `${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","cachePath","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,SAAS,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACP,WAAU;IAC1EQ,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"}
@@ -16,7 +16,7 @@ function _interop_require_default(obj) {
16
16
  };
17
17
  }
18
18
  function getCacheAsync(endpoint) {
19
- var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
19
+ var fullPath = _path.default.join(this.cachePath, "".concat(this.options.hash(endpoint), ".json"));
20
20
  try {
21
21
  var contents = _fs.default.readFileSync(fullPath, 'utf8');
22
22
  var record = JSON.parse(contents.toString());
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/getSync.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\nexport default function getCacheAsync(endpoint) {\n const fullPath = path.join(this.cachePath, `${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","cachePath","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,SAAS,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,WAAU;IAC1E,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"}
@@ -9,10 +9,10 @@ Object.defineProperty(exports, "default", {
9
9
  }
10
10
  });
11
11
  var _rimraf2 = /*#__PURE__*/ _interop_require_default(require("rimraf2"));
12
- var _get = /*#__PURE__*/ _interop_require_default(require("./get.js"));
13
- var _getSync = /*#__PURE__*/ _interop_require_default(require("./getSync.js"));
14
- var _hash = /*#__PURE__*/ _interop_require_default(require("./hash.js"));
15
- var _update = /*#__PURE__*/ _interop_require_default(require("./update.js"));
12
+ var _get = /*#__PURE__*/ _interop_require_default(require("./get.cjs"));
13
+ var _getSync = /*#__PURE__*/ _interop_require_default(require("./getSync.cjs"));
14
+ var _hash = /*#__PURE__*/ _interop_require_default(require("./hash.cjs"));
15
+ var _update = /*#__PURE__*/ _interop_require_default(require("./update.cjs"));
16
16
  function _class_call_check(instance, Constructor) {
17
17
  if (!(instance instanceof Constructor)) {
18
18
  throw new TypeError("Cannot call a class as a function");
@@ -32,12 +32,12 @@ function _interop_require_default(obj) {
32
32
  }
33
33
  var Cache = /*#__PURE__*/ function() {
34
34
  "use strict";
35
- function Cache(cacheDirectory) {
35
+ function Cache(cachePath) {
36
36
  var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
37
37
  _class_call_check(this, Cache);
38
38
  if (!_instanceof(this, Cache)) throw new Error('Cache needs to be called with new');
39
- if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');
40
- this.cacheDirectory = cacheDirectory;
39
+ if (!cachePath) throw new Error('Cache needs cachePath');
40
+ this.cachePath = cachePath;
41
41
  this.options = options;
42
42
  if (!this.options.hash) this.options.hash = _hash.default;
43
43
  }
@@ -49,10 +49,13 @@ var Cache = /*#__PURE__*/ function() {
49
49
  options = null;
50
50
  }
51
51
  options = options || {};
52
- if (typeof callback === 'function') return options.force ? _update.default.call(this, endpoint, callback) : _get.default.call(this, endpoint, callback);
52
+ function worker(endpoint, options, callback) {
53
+ options.force ? _update.default.call(this, endpoint, callback) : _get.default.call(this, endpoint, callback);
54
+ }
55
+ if (typeof callback === 'function') return worker.call(this, endpoint, options, callback);
53
56
  return new Promise(function(resolve, reject) {
54
- _this.get(endpoint, options, function(err, json) {
55
- err ? reject(err) : resolve(json);
57
+ return worker.call(_this, endpoint, options, function(err, json) {
58
+ return err ? reject(err) : resolve(json);
56
59
  });
57
60
  });
58
61
  };
@@ -62,14 +65,15 @@ var Cache = /*#__PURE__*/ function() {
62
65
  };
63
66
  _proto.clear = function clear(callback) {
64
67
  var _this = this;
65
- if (typeof callback === 'function') return (0, _rimraf2.default)(this.cacheDirectory, {
66
- disableGlob: true
67
- }, function(_err) {
68
- _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
69
- callback();
70
- });
68
+ function worker(callback) {
69
+ // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
70
+ (0, _rimraf2.default)(this.cachePath, {
71
+ disableGlob: true
72
+ }, callback.bind(null, null));
73
+ }
74
+ if (typeof callback === 'function') return worker.call(this, callback);
71
75
  return new Promise(function(resolve, reject) {
72
- _this.clear(function(err) {
76
+ return worker.call(_this, function(err) {
73
77
  err ? reject(err) : resolve(undefined);
74
78
  });
75
79
  });
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/index.ts"],"sourcesContent":["import rimraf2 from 'rimraf2';\n\nimport get from './get';\nimport getSync from './getSync';\nimport hash from './hash';\nimport update from './update';\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 cachePath: string;\n options: CacheOptions;\n\n constructor(cachePath: string, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cachePath) throw new Error('Cache needs cachePath');\n this.cachePath = cachePath;\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 function worker(endpoint, options, callback) {\n (options as GetOptions).force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n }\n\n if (typeof callback === 'function') return worker.call(this, endpoint, options, callback) as undefined;\n return new Promise((resolve, reject) => worker.call(this, endpoint, options, (err, json) => (err ? reject(err) : resolve(json))));\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 function worker(callback) {\n // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint\n rimraf2(this.cachePath, { disableGlob: true }, callback.bind(null, null));\n }\n\n if (typeof callback === 'function') return worker.call(this, callback);\n return new Promise((resolve, reject) =>\n worker.call(this, (err) => {\n err ? reject(err) : resolve(undefined);\n })\n );\n }\n}\n"],"names":["Cache","cachePath","options","Error","hash","get","endpoint","callback","worker","force","update","call","Promise","resolve","reject","err","json","getSync","clear","rimraf2","disableGlob","bind","undefined"],"mappings":";;;;;;;eAiBqBA;;;8DAjBD;0DAEJ;8DACI;2DACH;6DACE;;;;;;;;;;;;;;;;;;AAYJ,IAAA,AAAMA,sBAAN;;aAAMA,MAIPC,SAAiB;YAAEC,UAAAA,iEAAwB,CAAC;gCAJrCF;QAKjB,IAAI,CAAE,AAAI,YAAJ,IAAI,EALOA,QAKa,MAAM,IAAIG,MAAM;QAC9C,IAAI,CAACF,WAAW,MAAM,IAAIE,MAAM;QAChC,IAAI,CAACF,SAAS,GAAGA;QACjB,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,SAASM,OAAOF,QAAQ,EAAEJ,OAAO,EAAEK,QAAQ;YACxCL,QAAuBO,KAAK,GAAGC,eAAM,CAACC,IAAI,CAAC,IAAI,EAAEL,UAAUC,YAAYF,YAAG,CAACM,IAAI,CAAC,IAAI,EAAEL,UAAUC;QACnG;QAEA,IAAI,OAAOA,aAAa,YAAY,OAAOC,OAAOG,IAAI,CAAC,IAAI,EAAEL,UAAUJ,SAASK;QAChF,OAAO,IAAIK,QAAQ,SAACC,SAASC;mBAAWN,OAAOG,IAAI,QAAOL,UAAUJ,SAAS,SAACa,KAAKC;uBAAUD,MAAMD,OAAOC,OAAOF,QAAQG;;;IAC3H;IAEAC,OAAAA,OAGC,GAHDA,SAAAA,QAAQX,QAAgB;QACtB,2BAA2B;QAC3B,OAAOW,gBAAO,CAACN,IAAI,CAAC,IAAI,EAAEL;IAC5B;IAEAY,OAAAA,KAYC,GAZDA,SAAAA,MAAMX,QAAwB;;QAC5B,SAASC,OAAOD,QAAQ;YACtB,kGAAkG;YAClGY,IAAAA,gBAAO,EAAC,IAAI,CAAClB,SAAS,EAAE;gBAAEmB,aAAa;YAAK,GAAGb,SAASc,IAAI,CAAC,MAAM;QACrE;QAEA,IAAI,OAAOd,aAAa,YAAY,OAAOC,OAAOG,IAAI,CAAC,IAAI,EAAEJ;QAC7D,OAAO,IAAIK,QAAQ,SAACC,SAASC;mBAC3BN,OAAOG,IAAI,QAAO,SAACI;gBACjBA,MAAMD,OAAOC,OAAOF,QAAQS;YAC9B;;IAEJ;WA5CmBtB"}
@@ -21,8 +21,8 @@ function _interop_require_default(obj) {
21
21
  };
22
22
  }
23
23
  function update(endpoint, callback) {
24
- var fullPath = _path.default.join(this.cacheDirectory, "".concat(this.options.hash(endpoint), ".json"));
25
- (0, _mkdirpclassic.default)(this.cacheDirectory, function(err) {
24
+ var fullPath = _path.default.join(this.cachePath, "".concat(this.options.hash(endpoint), ".json"));
25
+ (0, _mkdirpclassic.default)(this.cachePath, function(err) {
26
26
  if (err && err.code !== 'EEXIST') return callback(err);
27
27
  (0, _getremote.default)(endpoint).json(function(err, res) {
28
28
  if (err) return callback(err);
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/update.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport Queue from 'queue-cb';\nimport rimraf2 from 'rimraf2';\nimport tempSuffix from 'temp-suffix';\n\nexport default function update(endpoint, callback) {\n const fullPath = path.join(this.cachePath, `${this.options.hash(endpoint)}.json`);\n\n mkdirp(this.cachePath, (err) => {\n if (err && err.code !== 'EEXIST') return callback(err);\n\n get(endpoint).json((err, res) => {\n if (err) return callback(err);\n const record = { headers: res.headers, body: res.body };\n const tempFile = `${fullPath}-${tempSuffix()}`;\n\n const queue = new Queue(1);\n queue.defer(fs.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));\n queue.defer((cb) => rimraf2(fullPath, { disableGlob: true }, cb.bind(null, null)));\n queue.defer(fs.rename.bind(null, tempFile, fullPath));\n queue.await((err) => {\n if (!err) return callback(null, record.body);\n // cleanup the temp file if the operation failed\n rimraf2(tempFile, { disableGlob: true }, callback.bind(err));\n });\n });\n });\n}\n"],"names":["update","endpoint","callback","fullPath","path","join","cachePath","options","hash","mkdirp","err","code","get","json","res","record","headers","body","tempFile","tempSuffix","queue","Queue","defer","fs","writeFile","bind","JSON","stringify","cb","rimraf2","disableGlob","rename","await"],"mappings":";;;;+BAQA;;;eAAwBA;;;yDART;2DACE;gEACD;oEACG;8DACD;8DACE;iEACG;;;;;;AAER,SAASA,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,IAAMC,WAAWC,aAAI,CAACC,IAAI,CAAC,IAAI,CAACC,SAAS,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACP,WAAU;IAE1EQ,IAAAA,sBAAM,EAAC,IAAI,CAACH,SAAS,EAAE,SAACI;QACtB,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOT,SAASQ;QAElDE,IAAAA,kBAAG,EAACX,UAAUY,IAAI,CAAC,SAACH,KAAKI;YACvB,IAAIJ,KAAK,OAAOR,SAASQ;YACzB,IAAMK,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtD,IAAMC,WAAW,AAAC,GAAcC,OAAZhB,UAAS,KAAgB,OAAbgB,IAAAA,mBAAU;YAE1C,IAAMC,QAAQ,IAAIC,gBAAK,CAAC;YACxBD,MAAME,KAAK,CAACC,WAAE,CAACC,SAAS,CAACC,IAAI,CAAC,MAAMP,UAAUQ,KAAKC,SAAS,CAACZ,SAAS;YACtEK,MAAME,KAAK,CAAC,SAACM;uBAAOC,IAAAA,gBAAO,EAAC1B,UAAU;oBAAE2B,aAAa;gBAAK,GAAGF,GAAGH,IAAI,CAAC,MAAM;;YAC3EL,MAAME,KAAK,CAACC,WAAE,CAACQ,MAAM,CAACN,IAAI,CAAC,MAAMP,UAAUf;YAC3CiB,MAAMY,KAAK,CAAC,SAACtB;gBACX,IAAI,CAACA,KAAK,OAAOR,SAAS,MAAMa,OAAOE,IAAI;gBAC3C,gDAAgD;gBAChDY,IAAAA,gBAAO,EAACX,UAAU;oBAAEY,aAAa;gBAAK,GAAG5B,SAASuB,IAAI,CAACf;YACzD;QACF;IACF;AACF"}
package/dist/esm/get.mjs CHANGED
@@ -3,7 +3,7 @@ import path from 'path';
3
3
  import get from 'get-remote';
4
4
  import update from './update.mjs';
5
5
  export default function getCache(endpoint, callback) {
6
- const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
6
+ const fullPath = path.join(this.cachePath, `${this.options.hash(endpoint)}.json`);
7
7
  fs.readFile(fullPath, (err, contents)=>{
8
8
  // doesn't exist so create
9
9
  if (err) {
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/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"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update';\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.cachePath, `${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","cachePath","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,WAAW;AAU9B,eAAe,SAASC,SAASC,QAAQ,EAAEC,QAAQ;IACjD,MAAMC,WAAWN,KAAKO,IAAI,CAAC,IAAI,CAACC,SAAS,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,UAAU,KAAK,CAAC;IAChFL,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"}
@@ -1,7 +1,7 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
  export default function getCacheAsync(endpoint) {
4
- const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
4
+ const fullPath = path.join(this.cachePath, `${this.options.hash(endpoint)}.json`);
5
5
  try {
6
6
  const contents = fs.readFileSync(fullPath, 'utf8');
7
7
  const record = JSON.parse(contents.toString());
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/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"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/getSync.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\nexport default function getCacheAsync(endpoint) {\n const fullPath = path.join(this.cachePath, `${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","cachePath","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,SAAS,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACL,UAAU,KAAK,CAAC;IAChF,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"}
@@ -10,34 +10,32 @@ let Cache = class Cache {
10
10
  options = null;
11
11
  }
12
12
  options = options || {};
13
- if (typeof callback === 'function') return options.force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);
14
- return new Promise((resolve, reject)=>{
15
- this.get(endpoint, options, (err, json)=>{
16
- err ? reject(err) : resolve(json);
17
- });
18
- });
13
+ function worker(endpoint, options, callback) {
14
+ options.force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);
15
+ }
16
+ if (typeof callback === 'function') return worker.call(this, endpoint, options, callback);
17
+ return new Promise((resolve, reject)=>worker.call(this, endpoint, options, (err, json)=>err ? reject(err) : resolve(json)));
19
18
  }
20
19
  getSync(endpoint) {
21
20
  // TODO: add async fetching
22
21
  return getSync.call(this, endpoint);
23
22
  }
24
23
  clear(callback) {
25
- if (typeof callback === 'function') return rimraf2(this.cacheDirectory, {
26
- disableGlob: true
27
- }, (_err)=>{
28
- _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
29
- callback();
30
- });
31
- return new Promise((resolve, reject)=>{
32
- this.clear((err)=>{
24
+ function worker(callback) {
25
+ // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint
26
+ rimraf2(this.cachePath, {
27
+ disableGlob: true
28
+ }, callback.bind(null, null));
29
+ }
30
+ if (typeof callback === 'function') return worker.call(this, callback);
31
+ return new Promise((resolve, reject)=>worker.call(this, (err)=>{
33
32
  err ? reject(err) : resolve(undefined);
34
- });
35
- });
33
+ }));
36
34
  }
37
- constructor(cacheDirectory, options = {}){
35
+ constructor(cachePath, options = {}){
38
36
  if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');
39
- if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');
40
- this.cacheDirectory = cacheDirectory;
37
+ if (!cachePath) throw new Error('Cache needs cachePath');
38
+ this.cachePath = cachePath;
41
39
  this.options = options;
42
40
  if (!this.options.hash) this.options.hash = hash;
43
41
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/index.ts"],"sourcesContent":["import 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,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"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/index.ts"],"sourcesContent":["import rimraf2 from 'rimraf2';\n\nimport get from './get';\nimport getSync from './getSync';\nimport hash from './hash';\nimport update from './update';\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 cachePath: string;\n options: CacheOptions;\n\n constructor(cachePath: string, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cachePath) throw new Error('Cache needs cachePath');\n this.cachePath = cachePath;\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 function worker(endpoint, options, callback) {\n (options as GetOptions).force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n }\n\n if (typeof callback === 'function') return worker.call(this, endpoint, options, callback) as undefined;\n return new Promise((resolve, reject) => worker.call(this, endpoint, options, (err, json) => (err ? reject(err) : resolve(json))));\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 function worker(callback) {\n // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint\n rimraf2(this.cachePath, { disableGlob: true }, callback.bind(null, null));\n }\n\n if (typeof callback === 'function') return worker.call(this, callback);\n return new Promise((resolve, reject) =>\n worker.call(this, (err) => {\n err ? reject(err) : resolve(undefined);\n })\n );\n }\n}\n"],"names":["rimraf2","get","getSync","hash","update","Cache","endpoint","options","callback","worker","force","call","Promise","resolve","reject","err","json","clear","cachePath","disableGlob","bind","undefined","constructor","Error"],"mappings":"AAAA,OAAOA,aAAa,UAAU;AAE9B,OAAOC,SAAS,QAAQ;AACxB,OAAOC,aAAa,YAAY;AAChC,OAAOC,UAAU,SAAS;AAC1B,OAAOC,YAAY,WAAW;AAYf,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,SAASE,OAAOH,QAAQ,EAAEC,OAAO,EAAEC,QAAQ;YACxCD,QAAuBG,KAAK,GAAGN,OAAOO,IAAI,CAAC,IAAI,EAAEL,UAAUE,YAAYP,IAAIU,IAAI,CAAC,IAAI,EAAEL,UAAUE;QACnG;QAEA,IAAI,OAAOA,aAAa,YAAY,OAAOC,OAAOE,IAAI,CAAC,IAAI,EAAEL,UAAUC,SAASC;QAChF,OAAO,IAAII,QAAQ,CAACC,SAASC,SAAWL,OAAOE,IAAI,CAAC,IAAI,EAAEL,UAAUC,SAAS,CAACQ,KAAKC,OAAUD,MAAMD,OAAOC,OAAOF,QAAQG;IAC3H;IAEAd,QAAQI,QAAgB,EAAiB;QACvC,2BAA2B;QAC3B,OAAOJ,QAAQS,IAAI,CAAC,IAAI,EAAEL;IAC5B;IAEAW,MAAMT,QAAwB,EAAkC;QAC9D,SAASC,OAAOD,QAAQ;YACtB,kGAAkG;YAClGR,QAAQ,IAAI,CAACkB,SAAS,EAAE;gBAAEC,aAAa;YAAK,GAAGX,SAASY,IAAI,CAAC,MAAM;QACrE;QAEA,IAAI,OAAOZ,aAAa,YAAY,OAAOC,OAAOE,IAAI,CAAC,IAAI,EAAEH;QAC7D,OAAO,IAAII,QAAQ,CAACC,SAASC,SAC3BL,OAAOE,IAAI,CAAC,IAAI,EAAE,CAACI;gBACjBA,MAAMD,OAAOC,OAAOF,QAAQQ;YAC9B;IAEJ;IAxCAC,YAAYJ,SAAiB,EAAEX,UAAwB,CAAC,CAAC,CAAE;QACzD,IAAI,CAAE,CAAA,IAAI,YAAYF,KAAI,GAAI,MAAM,IAAIkB,MAAM;QAC9C,IAAI,CAACL,WAAW,MAAM,IAAIK,MAAM;QAChC,IAAI,CAACL,SAAS,GAAGA;QACjB,IAAI,CAACX,OAAO,GAAGA;QACf,IAAI,CAAC,IAAI,CAACA,OAAO,CAACJ,IAAI,EAAE,IAAI,CAACI,OAAO,CAACJ,IAAI,GAAGA;IAC9C;AAmCF;AA7CA,SAAqBE,mBA6CpB"}
@@ -6,8 +6,8 @@ import Queue from 'queue-cb';
6
6
  import rimraf2 from 'rimraf2';
7
7
  import tempSuffix from 'temp-suffix';
8
8
  export default function update(endpoint, callback) {
9
- const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);
10
- mkdirp(this.cacheDirectory, (err)=>{
9
+ const fullPath = path.join(this.cachePath, `${this.options.hash(endpoint)}.json`);
10
+ mkdirp(this.cachePath, (err)=>{
11
11
  if (err && err.code !== 'EEXIST') return callback(err);
12
12
  get(endpoint).json((err, res)=>{
13
13
  if (err) return callback(err);
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/update.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport Queue from 'queue-cb';\nimport rimraf2 from 'rimraf2';\nimport tempSuffix from 'temp-suffix';\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 const record = { headers: res.headers, body: res.body };\n const tempFile = `${fullPath}-${tempSuffix()}`;\n\n const queue = new Queue(1);\n queue.defer(fs.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));\n queue.defer((cb) => rimraf2(fullPath, { disableGlob: true }, cb.bind(null, null)));\n queue.defer(fs.rename.bind(null, tempFile, fullPath));\n queue.await((err) => {\n if (!err) return callback(null, record.body);\n // cleanup the temp file if the operation failed\n rimraf2(tempFile, { disableGlob: true }, callback.bind(err));\n });\n });\n });\n}\n"],"names":["fs","path","get","mkdirp","Queue","rimraf2","tempSuffix","update","endpoint","callback","fullPath","join","cacheDirectory","options","hash","err","code","json","res","record","headers","body","tempFile","queue","defer","writeFile","bind","JSON","stringify","cb","disableGlob","rename","await"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,OAAOC,UAAU,OAAO;AACxB,OAAOC,SAAS,aAAa;AAC7B,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,WAAW,WAAW;AAC7B,OAAOC,aAAa,UAAU;AAC9B,OAAOC,gBAAgB,cAAc;AAErC,eAAe,SAASC,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,MAAMC,WAAWT,KAAKU,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,UAAU,KAAK,CAAC;IAErFL,OAAO,IAAI,CAACS,cAAc,EAAE,CAACG;QAC3B,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOP,SAASM;QAElDb,IAAIM,UAAUS,IAAI,CAAC,CAACF,KAAKG;YACvB,IAAIH,KAAK,OAAON,SAASM;YACzB,MAAMI,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtD,MAAMC,WAAW,GAAGZ,SAAS,CAAC,EAAEJ,cAAc;YAE9C,MAAMiB,QAAQ,IAAInB,MAAM;YACxBmB,MAAMC,KAAK,CAACxB,GAAGyB,SAAS,CAACC,IAAI,CAAC,MAAMJ,UAAUK,KAAKC,SAAS,CAACT,SAAS;YACtEI,MAAMC,KAAK,CAAC,CAACK,KAAOxB,QAAQK,UAAU;oBAAEoB,aAAa;gBAAK,GAAGD,GAAGH,IAAI,CAAC,MAAM;YAC3EH,MAAMC,KAAK,CAACxB,GAAG+B,MAAM,CAACL,IAAI,CAAC,MAAMJ,UAAUZ;YAC3Ca,MAAMS,KAAK,CAAC,CAACjB;gBACX,IAAI,CAACA,KAAK,OAAON,SAAS,MAAMU,OAAOE,IAAI;gBAC3C,gDAAgD;gBAChDhB,QAAQiB,UAAU;oBAAEQ,aAAa;gBAAK,GAAGrB,SAASiB,IAAI,CAACX;YACzD;QACF;IACF;AACF"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/update.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport Queue from 'queue-cb';\nimport rimraf2 from 'rimraf2';\nimport tempSuffix from 'temp-suffix';\n\nexport default function update(endpoint, callback) {\n const fullPath = path.join(this.cachePath, `${this.options.hash(endpoint)}.json`);\n\n mkdirp(this.cachePath, (err) => {\n if (err && err.code !== 'EEXIST') return callback(err);\n\n get(endpoint).json((err, res) => {\n if (err) return callback(err);\n const record = { headers: res.headers, body: res.body };\n const tempFile = `${fullPath}-${tempSuffix()}`;\n\n const queue = new Queue(1);\n queue.defer(fs.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));\n queue.defer((cb) => rimraf2(fullPath, { disableGlob: true }, cb.bind(null, null)));\n queue.defer(fs.rename.bind(null, tempFile, fullPath));\n queue.await((err) => {\n if (!err) return callback(null, record.body);\n // cleanup the temp file if the operation failed\n rimraf2(tempFile, { disableGlob: true }, callback.bind(err));\n });\n });\n });\n}\n"],"names":["fs","path","get","mkdirp","Queue","rimraf2","tempSuffix","update","endpoint","callback","fullPath","join","cachePath","options","hash","err","code","json","res","record","headers","body","tempFile","queue","defer","writeFile","bind","JSON","stringify","cb","disableGlob","rename","await"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,OAAOC,UAAU,OAAO;AACxB,OAAOC,SAAS,aAAa;AAC7B,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,WAAW,WAAW;AAC7B,OAAOC,aAAa,UAAU;AAC9B,OAAOC,gBAAgB,cAAc;AAErC,eAAe,SAASC,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,MAAMC,WAAWT,KAAKU,IAAI,CAAC,IAAI,CAACC,SAAS,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,UAAU,KAAK,CAAC;IAEhFL,OAAO,IAAI,CAACS,SAAS,EAAE,CAACG;QACtB,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOP,SAASM;QAElDb,IAAIM,UAAUS,IAAI,CAAC,CAACF,KAAKG;YACvB,IAAIH,KAAK,OAAON,SAASM;YACzB,MAAMI,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtD,MAAMC,WAAW,GAAGZ,SAAS,CAAC,EAAEJ,cAAc;YAE9C,MAAMiB,QAAQ,IAAInB,MAAM;YACxBmB,MAAMC,KAAK,CAACxB,GAAGyB,SAAS,CAACC,IAAI,CAAC,MAAMJ,UAAUK,KAAKC,SAAS,CAACT,SAAS;YACtEI,MAAMC,KAAK,CAAC,CAACK,KAAOxB,QAAQK,UAAU;oBAAEoB,aAAa;gBAAK,GAAGD,GAAGH,IAAI,CAAC,MAAM;YAC3EH,MAAMC,KAAK,CAACxB,GAAG+B,MAAM,CAACL,IAAI,CAAC,MAAMJ,UAAUZ;YAC3Ca,MAAMS,KAAK,CAAC,CAACjB;gBACX,IAAI,CAACA,KAAK,OAAON,SAAS,MAAMU,OAAOE,IAAI;gBAC3C,gDAAgD;gBAChDhB,QAAQiB,UAAU;oBAAEQ,aAAa;gBAAK,GAAGrB,SAASiB,IAAI,CAACX;YACzD;QACF;IACF;AACF"}
@@ -1 +1 @@
1
- export default function hash(data: any): string;
1
+ export default function hash(data: any): any;
@@ -7,9 +7,9 @@ export interface GetOptions {
7
7
  export type GetCallback = (error?: Error, json?: object) => void;
8
8
  export type ClearCallback = (error?: Error) => void;
9
9
  export default class Cache {
10
- cacheDirectory: string;
10
+ cachePath: string;
11
11
  options: CacheOptions;
12
- constructor(cacheDirectory: string, options?: CacheOptions);
12
+ constructor(cachePath: string, options?: CacheOptions);
13
13
  get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null>;
14
14
  getSync(endpoint: string): object | null;
15
15
  clear(callback?: ClearCallback): undefined | Promise<undefined>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-json-cache",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
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",
@@ -19,12 +19,12 @@
19
19
  "exports": {
20
20
  ".": {
21
21
  "import": "./dist/esm/index.mjs",
22
- "require": "./dist/cjs/index.js",
22
+ "require": "./dist/cjs/index.cjs",
23
23
  "types": "./dist/types/index.d.ts"
24
24
  },
25
25
  "./*": "./*"
26
26
  },
27
- "main": "dist/cjs/index.js",
27
+ "main": "dist/cjs/index.cjs",
28
28
  "module": "dist/esm/index.mjs",
29
29
  "types": "dist/types/index.d.ts",
30
30
  "files": [
@@ -33,23 +33,23 @@
33
33
  "scripts": {
34
34
  "build": "tsds build",
35
35
  "deploy": "tsds deploy",
36
+ "docs": "tsds docs",
36
37
  "format": "biome check --write --unsafe src/ test/",
37
38
  "test": "tsds test:node --no-timeouts",
38
39
  "test:engines": "nvu engines npm test",
39
40
  "version": "tsds version"
40
41
  },
41
42
  "dependencies": {
42
- "get-remote": "^1.3.1",
43
+ "get-remote": "^1.3.2",
43
44
  "mkdirp-classic": "^0.5.3",
44
- "queue-cb": "^1.4.4",
45
+ "queue-cb": "^1.4.6",
45
46
  "rimraf2": "^2.8.2",
46
- "temp-suffix": "^0.1.1"
47
+ "temp-suffix": "^0.1.4"
47
48
  },
48
49
  "devDependencies": {
49
50
  "@biomejs/biome": "^1.9.4",
50
51
  "@types/mocha": "^10.0.10",
51
52
  "@types/node": "^22.10.2",
52
- "depcheck": "^1.4.7",
53
53
  "fs-exists-sync": "^0.1.0",
54
54
  "pinkie-promise": "^2.0.1"
55
55
  },
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/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"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/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"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/index.ts"],"sourcesContent":["import 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":";;;;;;;eAiBqBA;;;8DAjBD;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"}
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/update.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport Queue from 'queue-cb';\nimport rimraf2 from 'rimraf2';\nimport tempSuffix from 'temp-suffix';\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 const record = { headers: res.headers, body: res.body };\n const tempFile = `${fullPath}-${tempSuffix()}`;\n\n const queue = new Queue(1);\n queue.defer(fs.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));\n queue.defer((cb) => rimraf2(fullPath, { disableGlob: true }, cb.bind(null, null)));\n queue.defer(fs.rename.bind(null, tempFile, fullPath));\n queue.await((err) => {\n if (!err) return callback(null, record.body);\n // cleanup the temp file if the operation failed\n rimraf2(tempFile, { disableGlob: true }, callback.bind(err));\n });\n });\n });\n}\n"],"names":["update","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","mkdirp","err","code","get","json","res","record","headers","body","tempFile","tempSuffix","queue","Queue","defer","fs","writeFile","bind","JSON","stringify","cb","rimraf2","disableGlob","rename","await"],"mappings":";;;;+BAQA;;;eAAwBA;;;yDART;2DACE;gEACD;oEACG;8DACD;8DACE;iEACG;;;;;;AAER,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;YACzB,IAAMK,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtD,IAAMC,WAAW,AAAC,GAAcC,OAAZhB,UAAS,KAAgB,OAAbgB,IAAAA,mBAAU;YAE1C,IAAMC,QAAQ,IAAIC,gBAAK,CAAC;YACxBD,MAAME,KAAK,CAACC,WAAE,CAACC,SAAS,CAACC,IAAI,CAAC,MAAMP,UAAUQ,KAAKC,SAAS,CAACZ,SAAS;YACtEK,MAAME,KAAK,CAAC,SAACM;uBAAOC,IAAAA,gBAAO,EAAC1B,UAAU;oBAAE2B,aAAa;gBAAK,GAAGF,GAAGH,IAAI,CAAC,MAAM;;YAC3EL,MAAME,KAAK,CAACC,WAAE,CAACQ,MAAM,CAACN,IAAI,CAAC,MAAMP,UAAUf;YAC3CiB,MAAMY,KAAK,CAAC,SAACtB;gBACX,IAAI,CAACA,KAAK,OAAOR,SAAS,MAAMa,OAAOE,IAAI;gBAC3C,gDAAgD;gBAChDY,IAAAA,gBAAO,EAACX,UAAU;oBAAEY,aAAa;gBAAK,GAAG5B,SAASuB,IAAI,CAACf;YACzD;QACF;IACF;AACF"}
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
File without changes
File without changes