1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.makeStaticFileCache = makeStaticFileCache;
|
7 |
|
8 | var _caching = require("../caching");
|
9 |
|
10 | var fs = _interopRequireWildcard(require("../../gensync-utils/fs"));
|
11 |
|
12 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
13 |
|
14 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
15 |
|
16 | const nodeFs = require("fs");
|
17 |
|
18 | function makeStaticFileCache(fn) {
|
19 | return (0, _caching.makeStrongCache)(function* (filepath, cache) {
|
20 | const cached = cache.invalidate(() => fileMtime(filepath));
|
21 |
|
22 | if (cached === null) {
|
23 | return null;
|
24 | }
|
25 |
|
26 | return fn(filepath, yield* fs.readFile(filepath, "utf8"));
|
27 | });
|
28 | }
|
29 |
|
30 | function fileMtime(filepath) {
|
31 | try {
|
32 | return +nodeFs.statSync(filepath).mtime;
|
33 | } catch (e) {
|
34 | if (e.code !== "ENOENT" && e.code !== "ENOTDIR") throw e;
|
35 | }
|
36 |
|
37 | return null;
|
38 | } |
\ | No newline at end of file |