@casoon/auditmysite
Version:
Professional website analysis suite with robust accessibility testing, Core Web Vitals performance monitoring, SEO analysis, and content optimization insights. Features isolated browser contexts, retry mechanisms, and comprehensive API endpoints for profe
178 lines • 7.13 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileQueueStateAdapter = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const queue_state_1 = require("../../types/queue-state");
class FileQueueStateAdapter {
constructor(stateDir = './.queue-states') {
this.stateDir = path.resolve(stateDir);
}
async save(state) {
try {
// Ensure state directory exists
await fs.mkdir(this.stateDir, { recursive: true });
const statePath = this.getStatePath(state.id);
const stateData = JSON.stringify(state, null, 2);
// Write state atomically using a temporary file
const tempPath = `${statePath}.tmp`;
await fs.writeFile(tempPath, stateData, 'utf8');
await fs.rename(tempPath, statePath);
}
catch (error) {
throw new queue_state_1.QueueStateError(`Failed to save queue state ${state.id}: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : new Error(String(error)));
}
}
async load(id) {
try {
const statePath = this.getStatePath(id);
// Check if state file exists
try {
await fs.access(statePath);
}
catch {
return null; // State doesn't exist
}
const stateData = await fs.readFile(statePath, 'utf8');
const state = JSON.parse(stateData);
// Validate basic structure
if (!state.id || !Array.isArray(state.urls)) {
throw new Error('Invalid state structure');
}
return state;
}
catch (error) {
throw new queue_state_1.QueueStateError(`Failed to load queue state ${id}: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : new Error(String(error)));
}
}
async exists(id) {
try {
const statePath = this.getStatePath(id);
await fs.access(statePath);
return true;
}
catch {
return false;
}
}
async delete(id) {
try {
const statePath = this.getStatePath(id);
await fs.unlink(statePath);
}
catch (error) {
if (error instanceof Error && 'code' in error && error.code !== 'ENOENT') {
throw new queue_state_1.QueueStateError(`Failed to delete queue state ${id}: ${error.message}`, error);
}
else if (!(error instanceof Error) || !('code' in error) || error.code !== 'ENOENT') {
throw new queue_state_1.QueueStateError(`Failed to delete queue state ${id}: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : new Error(String(error)));
}
}
}
async list() {
try {
// Check if state directory exists
try {
await fs.access(this.stateDir);
}
catch {
return []; // Directory doesn't exist, no states
}
const files = await fs.readdir(this.stateDir);
const stateFiles = files
.filter(file => file.endsWith('.json'))
.map(file => path.basename(file, '.json'));
return stateFiles;
}
catch (error) {
throw new queue_state_1.QueueStateError(`Failed to list queue states: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : new Error(String(error)));
}
}
async cleanup(maxAge = 7 * 24 * 60 * 60 * 1000) {
try {
// Check if state directory exists
try {
await fs.access(this.stateDir);
}
catch {
return; // Directory doesn't exist, nothing to clean
}
const files = await fs.readdir(this.stateDir);
const now = Date.now();
for (const file of files) {
if (!file.endsWith('.json'))
continue;
const filePath = path.join(this.stateDir, file);
const stats = await fs.stat(filePath);
if (now - stats.mtime.getTime() > maxAge) {
await fs.unlink(filePath);
}
}
}
catch (error) {
throw new queue_state_1.QueueStateError(`Failed to cleanup old queue states: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : new Error(String(error)));
}
}
getStatePath(id) {
// Sanitize the ID to create a safe filename
const safeId = id.replace(/[^a-zA-Z0-9-_]/g, '_');
return path.join(this.stateDir, `${safeId}.json`);
}
/**
* Get information about a specific state without loading the full data
*/
async getStateInfo(id) {
try {
const statePath = this.getStatePath(id);
const stats = await fs.stat(statePath);
const stateData = await fs.readFile(statePath, 'utf8');
const state = JSON.parse(stateData);
return {
id: state.id,
status: state.status,
totalUrls: state.totalUrls,
processedUrls: state.processedUrls.length,
lastUpdateTime: state.lastUpdateTime,
size: stats.size
};
}
catch {
return null;
}
}
}
exports.FileQueueStateAdapter = FileQueueStateAdapter;
//# sourceMappingURL=file-queue-state-adapter.js.map