"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArtifactStore = void 0;
// packages/core/src/isolation/artifact-store.ts
const fs_1 = require("fs");
const path_1 = require("path");
class ArtifactStore {
    basePath;
    constructor(basePath) {
        this.basePath = basePath;
    }
    async write(name, content) {
        const filePath = (0, path_1.join)(this.basePath, name);
        const resolved = (0, path_1.resolve)(filePath);
        if (!resolved.startsWith((0, path_1.resolve)(this.basePath) + path_1.sep) && resolved !== (0, path_1.resolve)(this.basePath)) {
            throw new Error(`Path traversal blocked: ${name}`);
        }
        (0, fs_1.mkdirSync)((0, path_1.dirname)(resolved), { recursive: true });
        (0, fs_1.writeFileSync)(resolved, content, 'utf-8');
    }
    async read(name) {
        const resolved = (0, path_1.resolve)((0, path_1.join)(this.basePath, name));
        if (!resolved.startsWith((0, path_1.resolve)(this.basePath) + path_1.sep) && resolved !== (0, path_1.resolve)(this.basePath)) {
            throw new Error(`Path traversal blocked: ${name}`);
        }
        return (0, fs_1.readFileSync)(resolved, 'utf-8');
    }
    async exists(name) {
        const resolved = (0, path_1.resolve)((0, path_1.join)(this.basePath, name));
        if (!resolved.startsWith((0, path_1.resolve)(this.basePath) + path_1.sep) && resolved !== (0, path_1.resolve)(this.basePath)) {
            throw new Error(`Path traversal blocked: ${name}`);
        }
        return (0, fs_1.existsSync)(resolved);
    }
    async list() {
        if (!(0, fs_1.existsSync)(this.basePath))
            return [];
        const results = [];
        const walk = (dir) => {
            for (const entry of (0, fs_1.readdirSync)(dir)) {
                const fullPath = (0, path_1.join)(dir, entry);
                if ((0, fs_1.statSync)(fullPath).isDirectory()) {
                    walk(fullPath);
                }
                else {
                    results.push((0, path_1.relative)(this.basePath, fullPath));
                }
            }
        };
        walk(this.basePath);
        return results;
    }
}
exports.ArtifactStore = ArtifactStore;
//# sourceMappingURL=artifact-store.js.map