@bernierllc/content-autosave-manager
Version:
Automatic content saving with debouncing, retry logic, and conflict detection
56 lines (54 loc) • 2.16 kB
JavaScript
;
/*
Copyright (c) 2025 Bernier LLC
This file is licensed to the client under a limited-use license.
The client may use and modify this code *only within the scope of the project it was delivered for*.
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfigFromEnv = loadConfigFromEnv;
exports.mergeWithDefaults = mergeWithDefaults;
/**
* Load configuration from environment variables
*/
function loadConfigFromEnv() {
const config = {};
if (process.env.AUTOSAVE_DEBOUNCE_MS) {
const parsed = parseInt(process.env.AUTOSAVE_DEBOUNCE_MS, 10);
if (!isNaN(parsed)) {
config.debounceMs = parsed;
}
}
if (process.env.AUTOSAVE_MAX_RETRIES) {
const parsed = parseInt(process.env.AUTOSAVE_MAX_RETRIES, 10);
if (!isNaN(parsed)) {
config.maxRetries = parsed;
}
}
if (process.env.AUTOSAVE_RETRY_STRATEGY) {
const strategy = process.env.AUTOSAVE_RETRY_STRATEGY;
if (strategy === 'exponential' || strategy === 'linear' || strategy === 'fibonacci') {
config.retryStrategy = strategy;
}
}
if (process.env.AUTOSAVE_ENABLE_VERSIONING) {
config.enableVersioning = process.env.AUTOSAVE_ENABLE_VERSIONING === 'true';
}
if (process.env.AUTOSAVE_ENABLE_CONFLICTS) {
config.enableConflictDetection = process.env.AUTOSAVE_ENABLE_CONFLICTS === 'true';
}
return config;
}
/**
* Merge configuration with defaults
*/
function mergeWithDefaults(config) {
const envConfig = loadConfigFromEnv();
return {
debounceMs: config?.debounceMs ?? envConfig.debounceMs ?? 2000,
maxRetries: config?.maxRetries ?? envConfig.maxRetries ?? 3,
retryStrategy: config?.retryStrategy ?? envConfig.retryStrategy ?? 'exponential',
enableVersioning: config?.enableVersioning ?? envConfig.enableVersioning ?? true,
enableConflictDetection: config?.enableConflictDetection ?? envConfig.enableConflictDetection ?? true,
};
}