@phroun/paged-buffer
Version:
High-performance buffer system for editing massive files with intelligent memory management and undo/redo capabilities
167 lines • 5.28 kB
JavaScript
;
/**
* File-based page storage implementation
*/
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.FilePageStorage = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const page_storage_1 = require("./page-storage");
/**
* File-based page storage implementation
*/
class FilePageStorage extends page_storage_1.PageStorage {
constructor(tempDir) {
super();
this.tempDir = tempDir ?? path.join(os.tmpdir(), 'buffer-pages');
// Fix: Use setImmediate instead of setTimeout, and make the callback async
(function (my) {
void my._ensureTempDir();
})(this);
}
async _ensureTempDir() {
try {
await fs_1.promises.mkdir(this.tempDir, { recursive: true });
}
catch (error) {
if (error.code !== 'EEXIST')
throw error;
}
}
async savePage(pageKey, data) {
const pagePath = path.join(this.tempDir, `${pageKey}.page`);
await fs_1.promises.writeFile(pagePath, data);
}
async loadPage(pageKey) {
const pagePath = path.join(this.tempDir, `${pageKey}.page`);
return await fs_1.promises.readFile(pagePath);
}
async deletePage(pageKey) {
const pagePath = path.join(this.tempDir, `${pageKey}.page`);
try {
await fs_1.promises.unlink(pagePath);
}
catch (error) {
if (error.code !== 'ENOENT')
throw error;
}
}
async pageExists(pageKey) {
const pagePath = path.join(this.tempDir, `${pageKey}.page`);
try {
await fs_1.promises.access(pagePath);
return true;
}
catch {
return false;
}
}
/**
* Get the directory where pages are stored
*/
getTempDir() {
return this.tempDir;
}
/**
* Get all stored page keys (useful for debugging/cleanup)
*/
async getAllPageKeys() {
try {
const files = await fs_1.promises.readdir(this.tempDir);
return files
.filter(file => file.endsWith('.page'))
.map(file => file.slice(0, -5)); // Remove .page extension
}
catch (error) {
if (error.code === 'ENOENT') {
return [];
}
throw error;
}
}
/**
* Get storage statistics
*/
async getStorageStats() {
const pageKeys = await this.getAllPageKeys();
let totalBytes = 0;
for (const pageKey of pageKeys) {
try {
const pagePath = path.join(this.tempDir, `${pageKey}.page`);
const stats = await fs_1.promises.stat(pagePath);
totalBytes += stats.size;
}
catch (error) {
// Skip files that might have been deleted concurrently
continue;
}
}
return {
pageCount: pageKeys.length,
totalBytes,
directory: this.tempDir
};
}
/**
* Clean up all stored pages
*/
async cleanup() {
try {
const files = await fs_1.promises.readdir(this.tempDir);
const pageFiles = files.filter(file => file.endsWith('.page'));
await Promise.all(pageFiles.map(async (file) => {
try {
await fs_1.promises.unlink(path.join(this.tempDir, file));
}
catch (error) {
// Ignore files that might have been deleted concurrently
}
}));
}
catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}
/**
* Remove the temporary directory entirely
*/
async destroy() {
try {
await this.cleanup();
await fs_1.promises.rmdir(this.tempDir);
}
catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}
}
exports.FilePageStorage = FilePageStorage;
//# sourceMappingURL=file-page-storage.js.map