donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
72 lines • 3.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvPersistenceRegistryImpl = void 0;
const DonobuSqliteDb_1 = require("../DonobuSqliteDb");
const PersistencePlugin_1 = require("../PersistencePlugin");
const EnvPersistenceSqlite_1 = require("./EnvPersistenceSqlite");
const EnvPersistenceVolatile_1 = require("./EnvPersistenceVolatile");
/**
* A factory class for creating EnvPersistence instances. Persistence layers are constructed
* eagerly at creation time and reused across all subsequent calls.
*
* Note that "Env" in this particular context is not talking about this
* process's NodeJS environment variables, but rather, the environment variables for Donobu flows.
*/
class EnvPersistenceRegistryImpl {
/**
* Creates an instance with pre-built persistence layers.
*/
constructor(layers) {
this.layers = layers;
}
/**
* Creates an instance by reading environment variables and eagerly constructing
* all applicable persistence layers.
*
* When {@link envPersistenceVolatile} is non-null it is always included in
* the resulting layer list - even if `PERSISTENCE_PRIORITY` does not contain
* `"RAM"`. If `"RAM"` *is* present in the priority list the volatile layer
* occupies that position; otherwise it is appended as a trailing fallback.
* This guarantees that env vars sourced from the host process (e.g. a
* `process.env` snapshot) remain accessible to flows regardless of how the
* caller configured persistence priority.
*/
static async fromEnvironment(envPersistenceVolatile, environ, persistencePlugins = new PersistencePlugin_1.PersistencePluginRegistry()) {
const layers = [];
let volatileIncluded = false;
for (const key of environ.data.PERSISTENCE_PRIORITY) {
switch (key) {
case 'LOCAL':
layers.push(await EnvPersistenceSqlite_1.EnvPersistenceSqlite.create(await (0, DonobuSqliteDb_1.getDonobuSqliteDatabase)()));
break;
case 'RAM':
layers.push(envPersistenceVolatile ?? new EnvPersistenceVolatile_1.EnvPersistenceVolatile());
volatileIncluded = true;
break;
default: {
const plugin = persistencePlugins.get(key);
if (plugin) {
const impl = await plugin.createEnvPersistence();
if (impl) {
layers.push(impl);
}
}
break;
}
}
}
// When an explicit volatile layer was provided (e.g. a process.env snapshot
// for library/test use) but PERSISTENCE_PRIORITY did not include "RAM",
// append it as a fallback so env vars from the host process remain accessible.
if (envPersistenceVolatile && !volatileIncluded) {
layers.push(envPersistenceVolatile);
}
return new EnvPersistenceRegistryImpl(layers);
}
/** Returns all persistence layers. Note that the returned list may be empty! */
async getAll() {
return this.layers;
}
}
exports.EnvPersistenceRegistryImpl = EnvPersistenceRegistryImpl;
//# sourceMappingURL=EnvPersistenceRegistry.js.map