@netlify/content-engine
Version:
98 lines • 3.79 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cache_manager_1 = __importDefault(require("cache-manager"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const fsStore = __importStar(require("../cache/cache-fs"));
const path_1 = __importDefault(require("path"));
const MAX_CACHE_SIZE = 250;
const TTL = Number.MAX_SAFE_INTEGER;
class GatsbyCache {
name;
store;
directory;
// TODO: remove `.cache` in v4. This is compat mode - cache-manager cache implementation
// expose internal cache that gives access to `.del` function that wasn't available in public
// cache interface (gatsby-plugin-sharp use it to clear no longer needed data)
cache;
// @ts-ignore - set & get types are missing from fsStore?
constructor({ name = `db`, store = fsStore } = {}) {
this.name = name;
this.store = store;
this.directory = path_1.default.join(global.__GATSBY?.root ?? process.cwd(), `.cache`, `caches`, name);
}
init() {
fs_extra_1.default.ensureDirSync(this.directory);
const configs = [
{
store: `memory`,
max: MAX_CACHE_SIZE,
ttl: TTL,
},
{
store: this.store,
ttl: TTL,
options: {
path: this.directory,
ttl: TTL,
},
},
];
const caches = configs.map((cache) => cache_manager_1.default.caching(cache));
this.cache = cache_manager_1.default.multiCaching(caches);
return this;
}
async get(key) {
return new Promise((resolve) => {
if (!this.cache) {
throw new Error(`GatsbyCache wasn't initialised yet, please run the init method first`);
}
this.cache.get(key, (err, res) => {
resolve(err ? undefined : res);
});
});
}
async set(key, value, args = { ttl: TTL }) {
return new Promise((resolve) => {
if (!this.cache) {
throw new Error(`GatsbyCache wasn't initialised yet, please run the init method first`);
}
this.cache.set(key, value, args, (err) => {
resolve(err ? undefined : value);
});
});
}
async del(key) {
if (!this.cache) {
throw new Error(`GatsbyCache wasn't initialised yet, please run the init method first`);
}
return this.cache.del(key);
}
}
exports.default = GatsbyCache;
//# sourceMappingURL=cache.js.map
;