"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateS3 = validateS3;
exports.uploadS3 = uploadS3;
exports.isValidS3Path = isValidS3Path;
exports.normalizeS3Path = normalizeS3Path;
exports.isValidS3BucketName = isValidS3BucketName;
const s3_1 = __importDefault(require("../../../lib/AWS/s3"));
const actions_core_1 = require("@segment/actions-core");
function validateS3(payload) {
    if (!payload.s3_aws_access_key) {
        throw new actions_core_1.InvalidAuthenticationError('Selected S3 upload mode, but missing AWS Access Key');
    }
    if (!payload.s3_aws_secret_key) {
        throw new actions_core_1.InvalidAuthenticationError('Selected S3 upload mode, but missing AWS Secret Key');
    }
    if (!payload.s3_aws_bucket_name) {
        throw new actions_core_1.InvalidAuthenticationError('Selected S3 upload mode, but missing AWS S3 bucket name');
    }
    if (!payload.s3_aws_region) {
        throw new actions_core_1.InvalidAuthenticationError('Selected S3 upload mode, but missing AWS Region');
    }
}
async function uploadS3(payload, filename, fileContent, request) {
    const method = 'PUT';
    const opts = await (0, s3_1.default)(payload.s3_aws_bucket_name, payload.s3_aws_region, filename, method, fileContent, payload.s3_aws_access_key, payload.s3_aws_secret_key);
    if (!opts.headers || !opts.method || !opts.host || !opts.path) {
        throw new actions_core_1.InvalidAuthenticationError('Unable to generate signature header for AWS S3 request.');
    }
    return await request(`https://${opts.host}/${opts.path}`, {
        headers: opts.headers,
        method,
        body: opts.body
    });
}
function isValidS3Path(key) {
    const encoder = new TextEncoder();
    const byteLength = encoder.encode(key).length;
    if (byteLength > 1024 || key.trim() === '') {
        return false;
    }
    const forbiddenChars = new Set(['\\', '{', '}', '^', '[', ']', '%', '`', '"', '<', '>', '#', '|', '~']);
    for (let i = 0; i < key.length; i++) {
        const charCode = key.charCodeAt(i);
        if ((charCode >= 0 && charCode <= 31) || (charCode >= 128 && charCode <= 255)) {
            return false;
        }
        if (forbiddenChars.has(key[i])) {
            return false;
        }
    }
    return true;
}
function normalizeS3Path(path) {
    return path?.replace(/^\/+|\/+$/g, '');
}
function isValidS3BucketName(bucketName) {
    if (!bucketName || bucketName.length < 3 || bucketName.length > 63) {
        return false;
    }
    const validCharsRegex = /^[a-z0-9.-]+$/;
    if (!validCharsRegex.test(bucketName)) {
        return false;
    }
    const beginEndRegex = /^[a-z0-9].*[a-z0-9]$/;
    if (!beginEndRegex.test(bucketName)) {
        return false;
    }
    if (bucketName.includes('..')) {
        return false;
    }
    const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$/;
    if (ipRegex.test(bucketName)) {
        return false;
    }
    const forbiddenPrefixes = ['xn--', 'sthree-', 'amzn-s3-demo-'];
    for (const prefix of forbiddenPrefixes) {
        if (bucketName.startsWith(prefix)) {
            return false;
        }
    }
    const forbiddenSuffixes = ['-s3alias', '--ol-s3', '.mrap', '--x-s3', '--table-s3'];
    for (const suffix of forbiddenSuffixes) {
        if (bucketName.endsWith(suffix)) {
            return false;
        }
    }
    return true;
}
//# sourceMappingURL=s3.js.map