@netlify/content-engine
Version:
64 lines • 2.32 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LockStatus = void 0;
exports.getDatabaseDir = getDatabaseDir;
exports.getStorage = getStorage;
exports.closeDatabase = closeDatabase;
const path_1 = __importDefault(require("path"));
const lmdb_1 = __importDefault(require("lmdb"));
var LockStatus;
(function (LockStatus) {
LockStatus[LockStatus["Locked"] = 0] = "Locked";
LockStatus[LockStatus["Unlocked"] = 1] = "Unlocked";
})(LockStatus || (exports.LockStatus = LockStatus = {}));
let databases;
let rootDb;
function getDatabaseDir() {
const rootDir = global.__GATSBY?.root ?? process.cwd();
return path_1.default.join(rootDir, `.cache`, `data`, `core-utils`);
}
function getStorage(fullDbPath) {
if (!databases) {
if (!fullDbPath) {
throw new Error(`LMDB path is not set!`);
}
// __GATSBY_OPEN_LMDBS tracks if we already opened given db in this process
// In `gatsby serve` case we might try to open it twice - once for engines
// and second to get access to `SitePage` nodes (to power trailing slashes
// redirect middleware). This ensure there is single instance within a process.
// Using more instances seems to cause weird random errors.
if (!globalThis.__GATSBY_OPEN_LMDBS) {
globalThis.__GATSBY_OPEN_LMDBS = new Map();
}
databases = globalThis.__GATSBY_OPEN_LMDBS.get(fullDbPath);
if (databases) {
return databases;
}
rootDb = lmdb_1.default.open({
name: `root`,
path: fullDbPath,
compression: true,
sharedStructuresKey: Symbol.for(`structures`),
});
databases = {
remoteFileInfo: rootDb.openDB({
name: `remote-file`,
}),
mutex: rootDb.openDB({
name: `mutex`,
}),
};
globalThis.__GATSBY_OPEN_LMDBS.set(fullDbPath, databases);
}
return databases;
}
async function closeDatabase() {
if (rootDb) {
await rootDb.close();
databases = undefined;
}
}
//# sourceMappingURL=get-storage.js.map
;