@nanggo/social-preview
Version:
Generate beautiful social media preview images from any URL
34 lines (33 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateDimensions = validateDimensions;
exports.validateDimension = validateDimension;
const types_1 = require("../../types");
const security_1 = require("../../constants/security");
/**
* Validates image dimensions.
*/
function validateDimensions(width, height) {
if (!Number.isFinite(width) || !Number.isFinite(height)) {
throw new types_1.PreviewGeneratorError(types_1.ErrorType.VALIDATION_ERROR, 'Dimensions must be finite numbers');
}
if (width < security_1.DIMENSION_LIMITS.MIN_WIDTH || height < security_1.DIMENSION_LIMITS.MIN_HEIGHT) {
throw new types_1.PreviewGeneratorError(types_1.ErrorType.VALIDATION_ERROR, `Minimum dimensions: ${security_1.DIMENSION_LIMITS.MIN_WIDTH}x${security_1.DIMENSION_LIMITS.MIN_HEIGHT}`);
}
if (width > security_1.DIMENSION_LIMITS.MAX_WIDTH || height > security_1.DIMENSION_LIMITS.MAX_HEIGHT) {
throw new types_1.PreviewGeneratorError(types_1.ErrorType.VALIDATION_ERROR, `Image dimensions cannot exceed ${security_1.DIMENSION_LIMITS.MAX_WIDTH}x${security_1.DIMENSION_LIMITS.MAX_HEIGHT} pixels`);
}
}
/**
* Validate dimension values.
*/
function validateDimension(value) {
if (typeof value !== 'number' || isNaN(value) || value <= 0) {
throw new types_1.PreviewGeneratorError(types_1.ErrorType.VALIDATION_ERROR, 'Dimension must be a positive number');
}
// Reasonable limits for image dimensions
if (value > security_1.DIMENSION_LIMITS.MAX_WIDTH) {
throw new types_1.PreviewGeneratorError(types_1.ErrorType.VALIDATION_ERROR, `Dimension exceeds maximum allowed size of ${security_1.DIMENSION_LIMITS.MAX_WIDTH} pixels`);
}
return value;
}