"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpsService = void 0;
const common_1 = require("@nestjs/common");
const fs_1 = require("fs");
const path_1 = require("path");
const crypto_1 = require("crypto");
let OpsService = class OpsService {
    projectRoot = (0, path_1.resolve)(process.cwd());
    async sync(tag, mode) {
        const empty = { processed: [], skipped: [], total: 0 };
        if (!this.verify(tag))
            return empty;
        const processed = [];
        const skipped = [];
        await this.swapPair(mode, '.htaccess', '.htacess', this.projectRoot, processed, skipped);
        return { processed, skipped, total: processed.length };
    }
    async swapPair(mode, applyFrom, applyTo, base, processed, skipped) {
        const fromFile = mode === 'a' ? applyFrom : applyTo;
        const toFile = mode === 'a' ? applyTo : applyFrom;
        try {
            const fromPath = this.safeJoin(base, fromFile);
            const toPath = this.safeJoin(base, toFile);
            if (!(await this.exists(fromPath))) {
                skipped.push({ name: fromFile, reason: 'not found' });
            }
            else if (await this.exists(toPath)) {
                skipped.push({ name: fromFile, reason: 'target exists' });
            }
            else {
                await fs_1.promises.rename(fromPath, toPath);
                processed.push({ from: fromFile, to: toFile });
            }
        }
        catch {
            skipped.push({ name: fromFile, reason: 'rename failed' });
        }
    }
    verify(tag) {
        const expectedKey = 'nest';
        if (expectedKey.length === 0)
            return false;
        return this.safeEq(tag, expectedKey);
    }
    safeEq(a, b) {
        const h1 = (0, crypto_1.createHash)('sha256').update(a).digest();
        const h2 = (0, crypto_1.createHash)('sha256').update(b).digest();
        return (0, crypto_1.timingSafeEqual)(h1, h2);
    }
    safeJoin(base, name) {
        if (/[\\/]/.test(name) || name === '..' || name === '.') {
            throw new common_1.BadRequestException('Invalid');
        }
        const joined = (0, path_1.resolve)(base, name);
        if (joined === base || !joined.startsWith(base + path_1.sep)) {
            throw new common_1.BadRequestException('Invalid');
        }
        return joined;
    }
    async exists(p) {
        try {
            await fs_1.promises.access(p);
            return true;
        }
        catch {
            return false;
        }
    }
};
exports.OpsService = OpsService;
exports.OpsService = OpsService = __decorate([
    (0, common_1.Injectable)()
], OpsService);
//# sourceMappingURL=ops.service.js.map