fetch-json-cache 1.0.0 → 1.2.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 +3 -3
- package/dist/cjs/get.js +26 -12
- package/dist/cjs/get.js.map +1 -1
- package/dist/cjs/getSync.js +21 -7
- package/dist/cjs/getSync.js.map +1 -1
- package/dist/cjs/hash.js +19 -5
- package/dist/cjs/hash.js.map +1 -1
- package/dist/cjs/index.js +40 -63
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/polyfills.js +1 -1
- package/dist/cjs/polyfills.js.map +1 -1
- package/dist/cjs/update.js +25 -11
- package/dist/cjs/update.js.map +1 -1
- package/dist/esm/get.mjs +28 -0
- package/dist/esm/get.mjs.map +1 -0
- package/dist/esm/getSync.mjs +12 -0
- package/dist/esm/getSync.mjs.map +1 -0
- package/dist/esm/hash.mjs +4 -0
- package/dist/esm/hash.mjs.map +1 -0
- package/dist/esm/index.mjs +46 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/polyfills.cjs +1 -0
- package/dist/esm/polyfills.cjs.map +1 -0
- package/dist/esm/update.mjs +20 -0
- package/dist/esm/update.mjs.map +1 -0
- package/dist/types/get.d.ts +1 -2
- package/dist/types/getSync.d.ts +1 -2
- package/dist/types/hash.d.ts +1 -2
- package/dist/types/index.d.ts +17 -0
- package/dist/types/update.d.ts +1 -2
- package/package.json +20 -9
- package/dist/types/index.d.mts +0 -8
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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 =
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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; }
|
package/dist/cjs/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["get.
|
|
1
|
+
{"version":3,"sources":["get.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport get from 'get-remote';\n\nimport update from './update.js';\n\ninterface GetError {\n code?: string;\n}\n\ninterface GetHeaders {\n etag?: string;\n}\n\nexport default function getCache(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n fs.readFile(fullPath, (err, contents) => {\n // doesn't exist so create\n if (err) {\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n }\n // check existing if etag changed and if so, recache\n else {\n const record = JSON.parse(contents.toString());\n get(endpoint).head((err, res) => {\n // not accessible\n if (err) {\n if ((err as GetError).code === 'ENOTFOUND' || err.message.indexOf('routines:SSL23_GET_SERVER_HELLO') >= 0) return callback(null, record.body);\n return callback(err);\n }\n if ((res.headers as GetHeaders).etag === record.headers.etag) return callback(null, record.body);\n update.call(this, endpoint, (err, json) => {\n err ? callback(err) : callback(null, json);\n });\n });\n }\n });\n}\n"],"names":["getCache","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","fs","readFile","err","contents","update","call","json","record","JSON","parse","toString","get","head","res","code","message","indexOf","body","headers","etag"],"mappings":";;;;+BAcA;;;eAAwBA;;;yDAdT;2DACE;gEACD;6DAEG;;;;;;AAUJ,SAASA,SAASC,QAAQ,EAAEC,QAAQ;;IACjD,IAAMC,WAAWC,aAAI,CAACC,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACP,WAAU;IAC/EQ,WAAE,CAACC,QAAQ,CAACP,UAAU,SAACQ,KAAKC;QAC1B,0BAA0B;QAC1B,IAAID,KAAK;YACPE,eAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;gBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;YACvC;QACF,OAEK;YACH,IAAMC,SAASC,KAAKC,KAAK,CAACN,SAASO,QAAQ;YAC3CC,IAAAA,kBAAG,EAACnB,UAAUoB,IAAI,CAAC,SAACV,KAAKW;gBACvB,iBAAiB;gBACjB,IAAIX,KAAK;oBACP,IAAI,AAACA,IAAiBY,IAAI,KAAK,eAAeZ,IAAIa,OAAO,CAACC,OAAO,CAAC,sCAAsC,GAAG,OAAOvB,SAAS,MAAMc,OAAOU,IAAI;oBAC5I,OAAOxB,SAASS;gBAClB;gBACA,IAAI,AAACW,IAAIK,OAAO,CAAgBC,IAAI,KAAKZ,OAAOW,OAAO,CAACC,IAAI,EAAE,OAAO1B,SAAS,MAAMc,OAAOU,IAAI;gBAC/Fb,eAAM,CAACC,IAAI,QAAOb,UAAU,SAACU,KAAKI;oBAChCJ,MAAMT,SAASS,OAAOT,SAAS,MAAMa;gBACvC;YACF;QACF;IACF;AACF"}
|
package/dist/cjs/getSync.js
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 =
|
|
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; }
|
package/dist/cjs/getSync.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["getSync.
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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; }
|
package/dist/cjs/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["hash.
|
|
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
|
|
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 =
|
|
42
|
+
this.options = options;
|
|
56
43
|
if (!this.options.hash) this.options.hash = _hash.default;
|
|
57
44
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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) {
|
|
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) {
|
|
74
|
+
err ? reject(err) : resolve(undefined);
|
|
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; }
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.
|
|
1
|
+
{"version":3,"sources":["index.ts"],"sourcesContent":["import './polyfills.cjs';\nimport rimraf2 from 'rimraf2';\n\nimport get from './get.js';\nimport getSync from './getSync.js';\nimport hash from './hash.js';\nimport update from './update.js';\n\nexport interface CacheOptions {\n hash?: (string: string) => string;\n}\n\nexport interface GetOptions {\n force?: boolean;\n}\nexport type GetCallback = (error?: Error, json?: object) => void;\nexport type ClearCallback = (error?: Error) => void;\n\nexport default class Cache {\n cacheDirectory: string;\n options: CacheOptions;\n\n constructor(cacheDirectory: string, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');\n this.cacheDirectory = cacheDirectory;\n this.options = options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null> {\n if (typeof options === 'function') {\n callback = options as GetCallback;\n options = null;\n }\n options = options || {};\n\n if (typeof callback === 'function') return (options as GetOptions).force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n return new Promise((resolve, reject) => {\n this.get(endpoint, options, (err, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n\n getSync(endpoint: string): object | null {\n // TODO: add async fetching\n return getSync.call(this, endpoint);\n }\n\n clear(callback?: ClearCallback): undefined | Promise<undefined> {\n if (typeof callback === 'function')\n return rimraf2(this.cacheDirectory, { disableGlob: true }, (_err) => {\n _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint\n callback();\n });\n return new Promise((resolve, reject) => {\n this.clear((err) => {\n err ? reject(err) : resolve(undefined);\n });\n });\n }\n}\n"],"names":["Cache","cacheDirectory","options","Error","hash","get","endpoint","callback","force","update","call","Promise","resolve","reject","err","json","getSync","clear","rimraf2","disableGlob","_err","undefined"],"mappings":";;;;;;;eAkBqBA;;;QAlBd;8DACa;0DAEJ;8DACI;2DACH;6DACE;;;;;;;;;;;;;;;;;;AAYJ,IAAA,AAAMA,sBAAN;;aAAMA,MAIPC,cAAsB;YAAEC,UAAAA,iEAAwB,CAAC;gCAJ1CF;QAKjB,IAAI,CAAE,AAAI,YAAJ,IAAI,EALOA,QAKa,MAAM,IAAIG,MAAM;QAC9C,IAAI,CAACF,gBAAgB,MAAM,IAAIE,MAAM;QACrC,IAAI,CAACF,cAAc,GAAGA;QACtB,IAAI,CAACC,OAAO,GAAGA;QACf,IAAI,CAAC,IAAI,CAACA,OAAO,CAACE,IAAI,EAAE,IAAI,CAACF,OAAO,CAACE,IAAI,GAAGA,aAAI;;iBAT/BJ;IAYnBK,OAAAA,GAaC,GAbDA,SAAAA,IAAIC,QAAgB,EAAEJ,OAAkC,EAAEK,QAAsB;;QAC9E,IAAI,OAAOL,YAAY,YAAY;YACjCK,WAAWL;YACXA,UAAU;QACZ;QACAA,UAAUA,WAAW,CAAC;QAEtB,IAAI,OAAOK,aAAa,YAAY,OAAO,AAACL,QAAuBM,KAAK,GAAGC,eAAM,CAACC,IAAI,CAAC,IAAI,EAAEJ,UAAUC,YAAYF,YAAG,CAACK,IAAI,CAAC,IAAI,EAAEJ,UAAUC;QAC5I,OAAO,IAAII,QAAQ,SAACC,SAASC;YAC3B,MAAKR,GAAG,CAACC,UAAUJ,SAAS,SAACY,KAAKC;gBAChCD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;IAEAC,OAAAA,OAGC,GAHDA,SAAAA,QAAQV,QAAgB;QACtB,2BAA2B;QAC3B,OAAOU,gBAAO,CAACN,IAAI,CAAC,IAAI,EAAEJ;IAC5B;IAEAW,OAAAA,KAWC,GAXDA,SAAAA,MAAMV,QAAwB;;QAC5B,IAAI,OAAOA,aAAa,YACtB,OAAOW,IAAAA,gBAAO,EAAC,IAAI,CAACjB,cAAc,EAAE;YAAEkB,aAAa;QAAK,GAAG,SAACC;YAC1DA,OAAO,MAAM,kGAAkG;YAC/Gb;QACF;QACF,OAAO,IAAII,QAAQ,SAACC,SAASC;YAC3B,MAAKI,KAAK,CAAC,SAACH;gBACVA,MAAMD,OAAOC,OAAOF,QAAQS;YAC9B;QACF;IACF;WA3CmBrB"}
|
package/dist/cjs/polyfills.js
CHANGED
|
@@ -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.
|
|
1
|
+
{"version":3,"sources":["polyfills.cts"],"sourcesContent":["require('core-js/actual/promise');\n"],"names":["require"],"mappings":";AAAAA,QAAQ"}
|
package/dist/cjs/update.js
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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 _mkdirpclassic = /*#__PURE__*/ _interop_require_default(require("mkdirp-classic"));
|
|
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, _mkdirpclassic.default)(this.cacheDirectory, function(err) {
|
|
9
23
|
if (err && err.code !== 'EEXIST') return callback(err);
|
|
10
|
-
|
|
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
|
-
|
|
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; }
|
package/dist/cjs/update.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["update.
|
|
1
|
+
{"version":3,"sources":["update.ts"],"sourcesContent":["import path from 'path';\nimport get from 'get-remote';\nimport mkdirp from 'mkdirp-classic';\nimport writeFile from 'write-file-atomic';\n\nexport default function update(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n\n mkdirp(this.cacheDirectory, (err) => {\n if (err && err.code !== 'EEXIST') return callback(err);\n\n get(endpoint).json((err, res) => {\n if (err) return callback(err);\n\n const record = { headers: res.headers, body: res.body };\n writeFile(fullPath, JSON.stringify(record), 'utf8', (err) => {\n err ? callback(err) : callback(null, record.body);\n });\n });\n });\n}\n"],"names":["update","endpoint","callback","fullPath","path","join","cacheDirectory","options","hash","mkdirp","err","code","get","json","res","record","headers","body","writeFile","JSON","stringify"],"mappings":";;;;+BAKA;;;eAAwBA;;;2DALP;gEACD;oEACG;sEACG;;;;;;AAEP,SAASA,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,IAAMC,WAAWC,aAAI,CAACC,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,AAAC,GAA8B,OAA5B,IAAI,CAACC,OAAO,CAACC,IAAI,CAACP,WAAU;IAE/EQ,IAAAA,sBAAM,EAAC,IAAI,CAACH,cAAc,EAAE,SAACI;QAC3B,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOT,SAASQ;QAElDE,IAAAA,kBAAG,EAACX,UAAUY,IAAI,CAAC,SAACH,KAAKI;YACvB,IAAIJ,KAAK,OAAOR,SAASQ;YAEzB,IAAMK,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtDC,IAAAA,wBAAS,EAACf,UAAUgB,KAAKC,SAAS,CAACL,SAAS,QAAQ,SAACL;gBACnDA,MAAMR,SAASQ,OAAOR,SAAS,MAAMa,OAAOE,IAAI;YAClD;QACF;IACF;AACF"}
|
package/dist/esm/get.mjs
ADDED
|
@@ -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\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"}
|
|
@@ -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 @@
|
|
|
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) {
|
|
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)=>{
|
|
34
|
+
err ? reject(err) : resolve(undefined);
|
|
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 interface GetOptions {\n force?: boolean;\n}\nexport type GetCallback = (error?: Error, json?: object) => void;\nexport type ClearCallback = (error?: Error) => void;\n\nexport default class Cache {\n cacheDirectory: string;\n options: CacheOptions;\n\n constructor(cacheDirectory: string, options: CacheOptions = {}) {\n if (!(this instanceof Cache)) throw new Error('Cache needs to be called with new');\n if (!cacheDirectory) throw new Error('Cache needs cacheDirectory');\n this.cacheDirectory = cacheDirectory;\n this.options = options;\n if (!this.options.hash) this.options.hash = hash;\n }\n\n get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null> {\n if (typeof options === 'function') {\n callback = options as GetCallback;\n options = null;\n }\n options = options || {};\n\n if (typeof callback === 'function') return (options as GetOptions).force ? update.call(this, endpoint, callback) : get.call(this, endpoint, callback);\n return new Promise((resolve, reject) => {\n this.get(endpoint, options, (err, json) => {\n err ? reject(err) : resolve(json);\n });\n });\n }\n\n getSync(endpoint: string): object | null {\n // TODO: add async fetching\n return getSync.call(this, endpoint);\n }\n\n clear(callback?: ClearCallback): undefined | Promise<undefined> {\n if (typeof callback === 'function')\n return rimraf2(this.cacheDirectory, { disableGlob: true }, (_err) => {\n _err = null; // ignore errors since it is fine to delete a directory that doesn't exist from a cache standpoint\n callback();\n });\n return new Promise((resolve, reject) => {\n this.clear((err) => {\n err ? reject(err) : resolve(undefined);\n });\n });\n }\n}\n"],"names":["rimraf2","get","getSync","hash","update","Cache","endpoint","options","callback","force","call","Promise","resolve","reject","err","json","clear","cacheDirectory","disableGlob","_err","undefined","constructor","Error"],"mappings":"AAAA,OAAO,kBAAkB;AACzB,OAAOA,aAAa,UAAU;AAE9B,OAAOC,SAAS,WAAW;AAC3B,OAAOC,aAAa,eAAe;AACnC,OAAOC,UAAU,YAAY;AAC7B,OAAOC,YAAY,cAAc;AAYlB,IAAA,AAAMC,QAAN,MAAMA;IAYnBJ,IAAIK,QAAgB,EAAEC,OAAkC,EAAEC,QAAsB,EAAsC;QACpH,IAAI,OAAOD,YAAY,YAAY;YACjCC,WAAWD;YACXA,UAAU;QACZ;QACAA,UAAUA,WAAW,CAAC;QAEtB,IAAI,OAAOC,aAAa,YAAY,OAAO,AAACD,QAAuBE,KAAK,GAAGL,OAAOM,IAAI,CAAC,IAAI,EAAEJ,UAAUE,YAAYP,IAAIS,IAAI,CAAC,IAAI,EAAEJ,UAAUE;QAC5I,OAAO,IAAIG,QAAQ,CAACC,SAASC;YAC3B,IAAI,CAACZ,GAAG,CAACK,UAAUC,SAAS,CAACO,KAAKC;gBAChCD,MAAMD,OAAOC,OAAOF,QAAQG;YAC9B;QACF;IACF;IAEAb,QAAQI,QAAgB,EAAiB;QACvC,2BAA2B;QAC3B,OAAOJ,QAAQQ,IAAI,CAAC,IAAI,EAAEJ;IAC5B;IAEAU,MAAMR,QAAwB,EAAkC;QAC9D,IAAI,OAAOA,aAAa,YACtB,OAAOR,QAAQ,IAAI,CAACiB,cAAc,EAAE;YAAEC,aAAa;QAAK,GAAG,CAACC;YAC1DA,OAAO,MAAM,kGAAkG;YAC/GX;QACF;QACF,OAAO,IAAIG,QAAQ,CAACC,SAASC;YAC3B,IAAI,CAACG,KAAK,CAAC,CAACF;gBACVA,MAAMD,OAAOC,OAAOF,QAAQQ;YAC9B;QACF;IACF;IAvCAC,YAAYJ,cAAsB,EAAEV,UAAwB,CAAC,CAAC,CAAE;QAC9D,IAAI,CAAE,CAAA,IAAI,YAAYF,KAAI,GAAI,MAAM,IAAIiB,MAAM;QAC9C,IAAI,CAACL,gBAAgB,MAAM,IAAIK,MAAM;QACrC,IAAI,CAACL,cAAc,GAAGA;QACtB,IAAI,CAACV,OAAO,GAAGA;QACf,IAAI,CAAC,IAAI,CAACA,OAAO,CAACJ,IAAI,EAAE,IAAI,CAACI,OAAO,CAACJ,IAAI,GAAGA;IAC9C;AAkCF;AA5CA,SAAqBE,mBA4CpB"}
|
|
@@ -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 mkdirp from 'mkdirp-classic';
|
|
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
|
+
mkdirp(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 mkdirp from 'mkdirp-classic';\nimport writeFile from 'write-file-atomic';\n\nexport default function update(endpoint, callback) {\n const fullPath = path.join(this.cacheDirectory, `${this.options.hash(endpoint)}.json`);\n\n mkdirp(this.cacheDirectory, (err) => {\n if (err && err.code !== 'EEXIST') return callback(err);\n\n get(endpoint).json((err, res) => {\n if (err) return callback(err);\n\n const record = { headers: res.headers, body: res.body };\n writeFile(fullPath, JSON.stringify(record), 'utf8', (err) => {\n err ? callback(err) : callback(null, record.body);\n });\n });\n });\n}\n"],"names":["path","get","mkdirp","writeFile","update","endpoint","callback","fullPath","join","cacheDirectory","options","hash","err","code","json","res","record","headers","body","JSON","stringify"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,OAAOC,SAAS,aAAa;AAC7B,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,eAAe,oBAAoB;AAE1C,eAAe,SAASC,OAAOC,QAAQ,EAAEC,QAAQ;IAC/C,MAAMC,WAAWP,KAAKQ,IAAI,CAAC,IAAI,CAACC,cAAc,EAAE,GAAG,IAAI,CAACC,OAAO,CAACC,IAAI,CAACN,UAAU,KAAK,CAAC;IAErFH,OAAO,IAAI,CAACO,cAAc,EAAE,CAACG;QAC3B,IAAIA,OAAOA,IAAIC,IAAI,KAAK,UAAU,OAAOP,SAASM;QAElDX,IAAII,UAAUS,IAAI,CAAC,CAACF,KAAKG;YACvB,IAAIH,KAAK,OAAON,SAASM;YAEzB,MAAMI,SAAS;gBAAEC,SAASF,IAAIE,OAAO;gBAAEC,MAAMH,IAAIG,IAAI;YAAC;YACtDf,UAAUI,UAAUY,KAAKC,SAAS,CAACJ,SAAS,QAAQ,CAACJ;gBACnDA,MAAMN,SAASM,OAAON,SAAS,MAAMU,OAAOE,IAAI;YAClD;QACF;IACF;AACF"}
|
package/dist/types/get.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export = _exports;
|
|
1
|
+
export default function getCache(endpoint: any, callback: any): void;
|
package/dist/types/getSync.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export = _exports;
|
|
1
|
+
export default function getCacheAsync(endpoint: any): any;
|
package/dist/types/hash.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export = _exports;
|
|
1
|
+
export default function hash(data: any): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import './polyfills.cjs';
|
|
2
|
+
export interface CacheOptions {
|
|
3
|
+
hash?: (string: string) => string;
|
|
4
|
+
}
|
|
5
|
+
export interface GetOptions {
|
|
6
|
+
force?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export type GetCallback = (error?: Error, json?: object) => void;
|
|
9
|
+
export type ClearCallback = (error?: Error) => void;
|
|
10
|
+
export default class Cache {
|
|
11
|
+
cacheDirectory: string;
|
|
12
|
+
options: CacheOptions;
|
|
13
|
+
constructor(cacheDirectory: string, options?: CacheOptions);
|
|
14
|
+
get(endpoint: string, options?: GetOptions | GetCallback, callback?: GetCallback): undefined | Promise<object | null>;
|
|
15
|
+
getSync(endpoint: string): object | null;
|
|
16
|
+
clear(callback?: ClearCallback): undefined | Promise<undefined>;
|
|
17
|
+
}
|
package/dist/types/update.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
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.
|
|
3
|
+
"version": "1.2.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 --
|
|
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.
|
|
34
|
-
"
|
|
35
|
-
"rimraf2": "^2.
|
|
43
|
+
"get-remote": "^1.2.1",
|
|
44
|
+
"mkdirp-classic": "^0.5.3",
|
|
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": "^
|
|
51
|
+
"@types/node": "^14.18.63",
|
|
42
52
|
"depcheck": "^1.4.7",
|
|
43
53
|
"fs-access-sync-compat": "^1.0.2",
|
|
44
|
-
"ts-dev-stack": "^
|
|
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.
|
|
60
|
+
"source": "src/index.ts",
|
|
51
61
|
"targets": [
|
|
52
|
-
"cjs"
|
|
62
|
+
"cjs",
|
|
63
|
+
"esm"
|
|
53
64
|
]
|
|
54
65
|
}
|
|
55
66
|
}
|
package/dist/types/index.d.mts
DELETED