fetch-json-cache 1.3.13 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/get.js CHANGED
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "default", {
9
9
  }
10
10
  });
11
11
  var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
- var _getremote = /*#__PURE__*/ _interop_require_default(require("get-remote"));
12
+ var _getfilecompat = require("get-file-compat");
13
13
  var _path = /*#__PURE__*/ _interop_require_default(require("path"));
14
14
  var _updatets = /*#__PURE__*/ _interop_require_default(require("./update.js"));
15
15
  function _interop_require_default(obj) {
@@ -28,7 +28,7 @@ function getCache(endpoint, callback) {
28
28
  });
29
29
  } else {
30
30
  var record = JSON.parse(contents.toString());
31
- (0, _getremote.default)(endpoint).head(function(err, res) {
31
+ (0, _getfilecompat.head)(endpoint, function(err, res) {
32
32
  // not accessible
33
33
  if (err) {
34
34
  if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/get.ts"],"sourcesContent":["import fs from 'fs';\nimport get from 'get-remote';\nimport path from 'path';\n\nimport update from './update.ts';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nimport type { GetCallback } from './types.ts';\n\nexport default function getCache<T>(endpoint: string, callback: GetCallback<T>): undefined {\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":";;;;+BAgBA;;;eAAwBA;;;yDAhBT;gEACC;2DACC;+DAEE;;;;;;AAYJ,SAASA,SAAYC,QAAgB,EAAEC,QAAwB;;IAC5E,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,iBAAM,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,iBAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;oBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;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 { head } from 'get-file-compat';\nimport path from 'path';\n\nimport update from './update.ts';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nimport type { GetCallback } from './types.ts';\n\nexport default function getCache<T>(endpoint: string, callback: GetCallback<T>): undefined {\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 head(endpoint, (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","head","res","code","message","indexOf","body","headers","etag"],"mappings":";;;;+BAgBA;;;eAAwBA;;;yDAhBT;6BACM;2DACJ;+DAEE;;;;;;AAYJ,SAASA,SAAYC,QAAgB,EAAEC,QAAwB;;IAC5E,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,iBAAM,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,mBAAI,EAACnB,UAAU,SAACU,KAAKU;gBACnB,iBAAiB;gBACjB,IAAIV,KAAK;oBACP,IAAI,AAACA,IAAiBW,IAAI,KAAK,eAAeX,IAAIY,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOtB,SAAS,MAAMc,OAAOS,IAAI;oBAC5I,OAAOvB,SAASS;gBAClB;gBACA,IAAI,AAACU,IAAIK,OAAO,CAAgBC,IAAI,KAAKX,OAAOU,OAAO,CAACC,IAAI,EAAE,OAAOzB,SAAS,MAAMc,OAAOS,IAAI;gBAC/FZ,iBAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;oBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;gBACvC;YACF;QACF;IACF;AACF"}
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "default", {
10
10
  });
11
11
  var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
12
  var _fsremovecompat = require("fs-remove-compat");
13
- var _getremote = /*#__PURE__*/ _interop_require_default(require("get-remote"));
13
+ var _getfilecompat = require("get-file-compat");
14
14
  var _mkdirpclassic = /*#__PURE__*/ _interop_require_default(require("mkdirp-classic"));
15
15
  var _path = /*#__PURE__*/ _interop_require_default(require("path"));
16
16
  var _queuecb = /*#__PURE__*/ _interop_require_default(require("queue-cb"));
@@ -24,24 +24,29 @@ function update(endpoint, callback) {
24
24
  var fullPath = _path.default.join(this.cachePath, "".concat(this.options.hash(endpoint), ".json"));
25
25
  (0, _mkdirpclassic.default)(this.cachePath, function(err) {
26
26
  if (err && err.code !== 'EEXIST') return callback(err);
27
- (0, _getremote.default)(endpoint).json(function(err, res) {
27
+ (0, _getfilecompat.getContent)(endpoint, 'utf8', function(err, res) {
28
28
  if (err) return callback(err);
29
- var record = {
30
- headers: res.headers,
31
- body: res.body
32
- };
33
- var tempFile = "".concat(fullPath, "-").concat((0, _tempsuffix.default)());
34
- var queue = new _queuecb.default(1);
35
- queue.defer(_fs.default.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));
36
- queue.defer(function(cb) {
37
- return (0, _fsremovecompat.rm)(fullPath, cb.bind(null, null));
38
- });
39
- queue.defer(_fs.default.rename.bind(null, tempFile, fullPath));
40
- queue.await(function(err) {
41
- if (!err) return callback(null, record.body);
42
- // cleanup the temp file if the operation failed
43
- (0, _fsremovecompat.rm)(tempFile, callback.bind(err));
44
- });
29
+ try {
30
+ var body = JSON.parse(res.content);
31
+ var record = {
32
+ headers: res.headers,
33
+ body: body
34
+ };
35
+ var tempFile = "".concat(fullPath, "-").concat((0, _tempsuffix.default)());
36
+ var queue = new _queuecb.default(1);
37
+ queue.defer(_fs.default.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));
38
+ queue.defer(function(cb) {
39
+ return (0, _fsremovecompat.rm)(fullPath, cb.bind(null, null));
40
+ });
41
+ queue.defer(_fs.default.rename.bind(null, tempFile, fullPath));
42
+ queue.await(function(err) {
43
+ if (!err) return callback(null, record.body);
44
+ // cleanup the temp file if the operation failed
45
+ (0, _fsremovecompat.rm)(tempFile, callback.bind(err));
46
+ });
47
+ } catch (err) {
48
+ return callback(err);
49
+ }
45
50
  });
46
51
  });
47
52
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/update.ts"],"sourcesContent":["import fs from 'fs';\nimport { rm } from 'fs-remove-compat';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport tempSuffix from 'temp-suffix';\n\nimport type { Record, UpdateCallback } from './types.ts';\n\nexport default function update<T>(endpoint: string, callback: UpdateCallback<T>): undefined {\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 } as Record<T>;\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) => rm(fullPath, 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 rm(tempFile, 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","rm","rename","await"],"mappings":";;;;+BAUA;;;eAAwBA;;;yDAVT;8BACI;gEACH;oEACG;2DACF;8DACC;iEACK;;;;;;AAIR,SAASA,OAAUC,QAAgB,EAAEC,QAA2B;IAC7E,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,kBAAE,EAAC1B,UAAUyB,GAAGH,IAAI,CAAC,MAAM;;YAC/CL,MAAME,KAAK,CAACC,WAAE,CAACO,MAAM,CAACL,IAAI,CAAC,MAAMP,UAAUf;YAC3CiB,MAAMW,KAAK,CAAC,SAACrB;gBACX,IAAI,CAACA,KAAK,OAAOR,SAAS,MAAMa,OAAOE,IAAI;gBAC3C,gDAAgD;gBAChDY,IAAAA,kBAAE,EAACX,UAAUhB,SAASuB,IAAI,CAACf;YAC7B;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 { rm } from 'fs-remove-compat';\nimport { getContent } from 'get-file-compat';\nimport mkdirp from 'mkdirp-classic';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport tempSuffix from 'temp-suffix';\n\nimport type { Record, UpdateCallback } from './types.ts';\n\nexport default function update<T>(endpoint: string, callback: UpdateCallback<T>): undefined {\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 getContent(endpoint, 'utf8', (err, res) => {\n if (err) return callback(err);\n\n try {\n const body = JSON.parse(res.content);\n const record = { headers: res.headers, body } as Record<T>;\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) => rm(fullPath, 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 rm(tempFile, callback.bind(err));\n });\n } catch (err) {\n return callback(err);\n }\n });\n });\n}\n"],"names":["update","endpoint","callback","fullPath","path","join","cachePath","options","hash","mkdirp","err","code","getContent","res","body","JSON","parse","content","record","headers","tempFile","tempSuffix","queue","Queue","defer","fs","writeFile","bind","stringify","cb","rm","rename","await"],"mappings":";;;;+BAUA;;;eAAwBA;;;yDAVT;8BACI;6BACQ;oEACR;2DACF;8DACC;iEACK;;;;;;AAIR,SAASA,OAAUC,QAAgB,EAAEC,QAA2B;IAC7E,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,yBAAU,EAACX,UAAU,QAAQ,SAACS,KAAKG;YACjC,IAAIH,KAAK,OAAOR,SAASQ;YAEzB,IAAI;gBACF,IAAMI,OAAOC,KAAKC,KAAK,CAACH,IAAII,OAAO;gBACnC,IAAMC,SAAS;oBAAEC,SAASN,IAAIM,OAAO;oBAAEL,MAAAA;gBAAK;gBAC5C,IAAMM,WAAW,AAAC,GAAcC,OAAZlB,UAAS,KAAgB,OAAbkB,IAAAA,mBAAU;gBAE1C,IAAMC,QAAQ,IAAIC,gBAAK,CAAC;gBACxBD,MAAME,KAAK,CAACC,WAAE,CAACC,SAAS,CAACC,IAAI,CAAC,MAAMP,UAAUL,KAAKa,SAAS,CAACV,SAAS;gBACtEI,MAAME,KAAK,CAAC,SAACK;2BAAOC,IAAAA,kBAAE,EAAC3B,UAAU0B,GAAGF,IAAI,CAAC,MAAM;;gBAC/CL,MAAME,KAAK,CAACC,WAAE,CAACM,MAAM,CAACJ,IAAI,CAAC,MAAMP,UAAUjB;gBAC3CmB,MAAMU,KAAK,CAAC,SAACtB;oBACX,IAAI,CAACA,KAAK,OAAOR,SAAS,MAAMgB,OAAOJ,IAAI;oBAC3C,gDAAgD;oBAChDgB,IAAAA,kBAAE,EAACV,UAAUlB,SAASyB,IAAI,CAACjB;gBAC7B;YACF,EAAE,OAAOA,KAAK;gBACZ,OAAOR,SAASQ;YAClB;QACF;IACF;AACF"}
package/dist/esm/get.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import fs from 'fs';
2
- import get from 'get-remote';
2
+ import { head } from 'get-file-compat';
3
3
  import path from 'path';
4
4
  import update from './update.js';
5
5
  export default function getCache(endpoint, callback) {
@@ -12,7 +12,7 @@ export default function getCache(endpoint, callback) {
12
12
  });
13
13
  } else {
14
14
  const record = JSON.parse(contents.toString());
15
- get(endpoint).head((err, res)=>{
15
+ head(endpoint, (err, res)=>{
16
16
  // not accessible
17
17
  if (err) {
18
18
  if (err.code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/get.ts"],"sourcesContent":["import fs from 'fs';\nimport get from 'get-remote';\nimport path from 'path';\n\nimport update from './update.ts';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nimport type { GetCallback } from './types.ts';\n\nexport default function getCache<T>(endpoint: string, callback: GetCallback<T>): undefined {\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","get","path","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,SAAS,aAAa;AAC7B,OAAOC,UAAU,OAAO;AAExB,OAAOC,YAAY,cAAc;AAYjC,eAAe,SAASC,SAAYC,QAAgB,EAAEC,QAAwB;IAC5E,MAAMC,WAAWL,KAAKM,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;YAC3CnB,IAAII,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 { head } from 'get-file-compat';\nimport path from 'path';\n\nimport update from './update.ts';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nimport type { GetCallback } from './types.ts';\n\nexport default function getCache<T>(endpoint: string, callback: GetCallback<T>): undefined {\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 head(endpoint, (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","head","path","update","getCache","endpoint","callback","fullPath","join","cachePath","options","hash","readFile","err","contents","call","json","record","JSON","parse","toString","res","code","message","indexOf","body","headers","etag"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,SAASC,IAAI,QAAQ,kBAAkB;AACvC,OAAOC,UAAU,OAAO;AAExB,OAAOC,YAAY,cAAc;AAYjC,eAAe,SAASC,SAAYC,QAAgB,EAAEC,QAAwB;IAC5E,MAAMC,WAAWL,KAAKM,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;YAC3CnB,KAAKI,UAAU,CAACQ,KAAKQ;gBACnB,iBAAiB;gBACjB,IAAIR,KAAK;oBACP,IAAI,AAACA,IAAiBS,IAAI,KAAK,eAAeT,IAAIU,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOlB,SAAS,MAAMW,OAAOQ,IAAI;oBAC5I,OAAOnB,SAASO;gBAClB;gBACA,IAAI,AAACQ,IAAIK,OAAO,CAAgBC,IAAI,KAAKV,OAAOS,OAAO,CAACC,IAAI,EAAE,OAAOrB,SAAS,MAAMW,OAAOQ,IAAI;gBAC/FtB,OAAOY,IAAI,CAAC,IAAI,EAAEV,UAAU,CAACQ,KAAKG;oBAChCH,MAAMP,SAASO,OAAOP,SAAS,MAAMU;gBACvC;YACF;QACF;IACF;AACF"}
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import { rm } from 'fs-remove-compat';
3
- import get from 'get-remote';
3
+ import { getContent } from 'get-file-compat';
4
4
  import mkdirp from 'mkdirp-classic';
5
5
  import path from 'path';
6
6
  import Queue from 'queue-cb';
@@ -9,22 +9,27 @@ export default function update(endpoint, callback) {
9
9
  const fullPath = path.join(this.cachePath, `${this.options.hash(endpoint)}.json`);
10
10
  mkdirp(this.cachePath, (err)=>{
11
11
  if (err && err.code !== 'EEXIST') return callback(err);
12
- get(endpoint).json((err, res)=>{
12
+ getContent(endpoint, 'utf8', (err, res)=>{
13
13
  if (err) return callback(err);
14
- const record = {
15
- headers: res.headers,
16
- body: res.body
17
- };
18
- const tempFile = `${fullPath}-${tempSuffix()}`;
19
- const queue = new Queue(1);
20
- queue.defer(fs.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));
21
- queue.defer((cb)=>rm(fullPath, cb.bind(null, null)));
22
- queue.defer(fs.rename.bind(null, tempFile, fullPath));
23
- queue.await((err)=>{
24
- if (!err) return callback(null, record.body);
25
- // cleanup the temp file if the operation failed
26
- rm(tempFile, callback.bind(err));
27
- });
14
+ try {
15
+ const body = JSON.parse(res.content);
16
+ const record = {
17
+ headers: res.headers,
18
+ body
19
+ };
20
+ const tempFile = `${fullPath}-${tempSuffix()}`;
21
+ const queue = new Queue(1);
22
+ queue.defer(fs.writeFile.bind(null, tempFile, JSON.stringify(record), 'utf8'));
23
+ queue.defer((cb)=>rm(fullPath, cb.bind(null, null)));
24
+ queue.defer(fs.rename.bind(null, tempFile, fullPath));
25
+ queue.await((err)=>{
26
+ if (!err) return callback(null, record.body);
27
+ // cleanup the temp file if the operation failed
28
+ rm(tempFile, callback.bind(err));
29
+ });
30
+ } catch (err) {
31
+ return callback(err);
32
+ }
28
33
  });
29
34
  });
30
35
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/fetch-json-cache/src/update.ts"],"sourcesContent":["import fs from 'fs';\nimport { rm } from 'fs-remove-compat';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport tempSuffix from 'temp-suffix';\n\nimport type { Record, UpdateCallback } from './types.ts';\n\nexport default function update<T>(endpoint: string, callback: UpdateCallback<T>): undefined {\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 } as Record<T>;\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) => rm(fullPath, 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 rm(tempFile, callback.bind(err));\n });\n });\n });\n}\n"],"names":["fs","rm","get","mkdirp","path","Queue","tempSuffix","update","endpoint","callback","fullPath","join","cachePath","options","hash","err","code","json","res","record","headers","body","tempFile","queue","defer","writeFile","bind","JSON","stringify","cb","rename","await"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,SAASC,EAAE,QAAQ,mBAAmB;AACtC,OAAOC,SAAS,aAAa;AAC7B,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,UAAU,OAAO;AACxB,OAAOC,WAAW,WAAW;AAC7B,OAAOC,gBAAgB,cAAc;AAIrC,eAAe,SAASC,OAAUC,QAAgB,EAAEC,QAA2B;IAC7E,MAAMC,WAAWN,KAAKO,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,IAAIlB,MAAM;YACxBkB,MAAMC,KAAK,CAACxB,GAAGyB,SAAS,CAACC,IAAI,CAAC,MAAMJ,UAAUK,KAAKC,SAAS,CAACT,SAAS;YACtEI,MAAMC,KAAK,CAAC,CAACK,KAAO5B,GAAGS,UAAUmB,GAAGH,IAAI,CAAC,MAAM;YAC/CH,MAAMC,KAAK,CAACxB,GAAG8B,MAAM,CAACJ,IAAI,CAAC,MAAMJ,UAAUZ;YAC3Ca,MAAMQ,KAAK,CAAC,CAAChB;gBACX,IAAI,CAACA,KAAK,OAAON,SAAS,MAAMU,OAAOE,IAAI;gBAC3C,gDAAgD;gBAChDpB,GAAGqB,UAAUb,SAASiB,IAAI,CAACX;YAC7B;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 { rm } from 'fs-remove-compat';\nimport { getContent } from 'get-file-compat';\nimport mkdirp from 'mkdirp-classic';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport tempSuffix from 'temp-suffix';\n\nimport type { Record, UpdateCallback } from './types.ts';\n\nexport default function update<T>(endpoint: string, callback: UpdateCallback<T>): undefined {\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 getContent(endpoint, 'utf8', (err, res) => {\n if (err) return callback(err);\n\n try {\n const body = JSON.parse(res.content);\n const record = { headers: res.headers, body } as Record<T>;\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) => rm(fullPath, 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 rm(tempFile, callback.bind(err));\n });\n } catch (err) {\n return callback(err);\n }\n });\n });\n}\n"],"names":["fs","rm","getContent","mkdirp","path","Queue","tempSuffix","update","endpoint","callback","fullPath","join","cachePath","options","hash","err","code","res","body","JSON","parse","content","record","headers","tempFile","queue","defer","writeFile","bind","stringify","cb","rename","await"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,SAASC,EAAE,QAAQ,mBAAmB;AACtC,SAASC,UAAU,QAAQ,kBAAkB;AAC7C,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,UAAU,OAAO;AACxB,OAAOC,WAAW,WAAW;AAC7B,OAAOC,gBAAgB,cAAc;AAIrC,eAAe,SAASC,OAAUC,QAAgB,EAAEC,QAA2B;IAC7E,MAAMC,WAAWN,KAAKO,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,WAAWM,UAAU,QAAQ,CAACO,KAAKE;YACjC,IAAIF,KAAK,OAAON,SAASM;YAEzB,IAAI;gBACF,MAAMG,OAAOC,KAAKC,KAAK,CAACH,IAAII,OAAO;gBACnC,MAAMC,SAAS;oBAAEC,SAASN,IAAIM,OAAO;oBAAEL;gBAAK;gBAC5C,MAAMM,WAAW,GAAGd,SAAS,CAAC,EAAEJ,cAAc;gBAE9C,MAAMmB,QAAQ,IAAIpB,MAAM;gBACxBoB,MAAMC,KAAK,CAAC1B,GAAG2B,SAAS,CAACC,IAAI,CAAC,MAAMJ,UAAUL,KAAKU,SAAS,CAACP,SAAS;gBACtEG,MAAMC,KAAK,CAAC,CAACI,KAAO7B,GAAGS,UAAUoB,GAAGF,IAAI,CAAC,MAAM;gBAC/CH,MAAMC,KAAK,CAAC1B,GAAG+B,MAAM,CAACH,IAAI,CAAC,MAAMJ,UAAUd;gBAC3Ce,MAAMO,KAAK,CAAC,CAACjB;oBACX,IAAI,CAACA,KAAK,OAAON,SAAS,MAAMa,OAAOJ,IAAI;oBAC3C,gDAAgD;oBAChDjB,GAAGuB,UAAUf,SAASmB,IAAI,CAACb;gBAC7B;YACF,EAAE,OAAOA,KAAK;gBACZ,OAAON,SAASM;YAClB;QACF;IACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-json-cache",
3
- "version": "1.3.13",
3
+ "version": "1.4.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",
@@ -38,20 +38,20 @@
38
38
  "version": "tsds version"
39
39
  },
40
40
  "dependencies": {
41
- "fs-remove-compat": "^0.2.3",
42
- "get-remote": "^2.3.2",
43
- "mkdirp-classic": "^0.5.3",
44
- "queue-cb": "^1.6.3",
41
+ "fs-remove-compat": "^1.0.0",
42
+ "get-file-compat": "^2.0.0",
43
+ "mkdirp-classic": "^0.5.2",
44
+ "queue-cb": "^1.0.0",
45
45
  "temp-suffix": "^1.0.10"
46
46
  },
47
47
  "devDependencies": {
48
- "@types/mocha": "^10.0.10",
49
- "@types/node": "^25.0.1",
48
+ "@types/mocha": "*",
49
+ "@types/node": "*",
50
50
  "fs-exists-sync": "^0.1.0",
51
- "node-version-use": "^2.1.5",
52
- "pinkie-promise": "^2.0.1",
53
- "ts-dev-stack": "^1.21.3",
54
- "tsds-config": "^0.2.1"
51
+ "node-version-use": "*",
52
+ "pinkie-promise": "*",
53
+ "ts-dev-stack": "*",
54
+ "tsds-config": "*"
55
55
  },
56
56
  "engines": {
57
57
  "node": ">=0.8"