UNPKG

@netlify/content-engine

Version:
109 lines 3.97 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resetCache = resetCache; const lmdb_1 = require("lmdb"); const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); // Since the regular GatsbyCache saves to "caches" this should be "caches-lmdb" const cacheDbFile = process.env.NODE_ENV === `test` ? `caches-lmdb-${ // FORCE_TEST_DATABASE_ID will be set if this gets executed in worker context // when running jest tests. JEST_WORKER_ID will be set when this gets executed directly // in test context (jest will use jest-worker internally). process.env.FORCE_TEST_DATABASE_ID ?? process.env.JEST_WORKER_ID}` : `caches-lmdb`; let dbPath = path.join(process.cwd(), `.cache/${cacheDbFile}`); function getAlreadyOpenedStore() { if (!globalThis.__GATSBY_OPEN_ROOT_LMDBS) { globalThis.__GATSBY_OPEN_ROOT_LMDBS = new Map(); } return globalThis.__GATSBY_OPEN_ROOT_LMDBS.get(dbPath); } class GatsbyCacheLmdb { db; encoding; name; // Needed for plugins that want to write data to the cache directory 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; constructor({ name = `db`, encoding = `json`, directory = process.cwd(), }) { this.name = name; this.encoding = encoding; this.directory = path.join(directory, `.cache/caches/${name}`); this.cache = this; dbPath = path.join(directory, `.cache/${cacheDbFile}`); } init() { fs.ensureDirSync(this.directory); return this; } static getStore() { let rootDb = getAlreadyOpenedStore(); if (rootDb) { return rootDb; } rootDb = (0, lmdb_1.open)({ name: `root`, path: dbPath, compression: true, maxDbs: 200, }); globalThis.__GATSBY_OPEN_ROOT_LMDBS.set(dbPath, rootDb); return rootDb; } getDb() { if (!this.db) { this.db = GatsbyCacheLmdb.getStore().openDB({ name: this.name, encoding: this.encoding, }); } return this.db; } async get(key) { return this.getDb().get(key); } async set(key, value) { await this.getDb().put(key, value); return value; } async del(key) { return this.getDb().remove(key); } } exports.default = GatsbyCacheLmdb; async function resetCache() { const store = getAlreadyOpenedStore(); if (store) { await store.close(); globalThis.__GATSBY_OPEN_ROOT_LMDBS.delete(dbPath); } await fs.emptyDir(dbPath); } //# sourceMappingURL=cache-lmdb.js.map