UNPKG

@clipwhisperer/common

Version:

ClipWhisperer Common - Shared library providing core utilities, database schemas, authentication, bucket management, and common functionality across all ClipWhisperer microservices

76 lines (66 loc) 2.02 kB
/** * Metadata Utilities * Centralized metadata handling and sanitization functions */ /** * Sanitize metadata value for safe storage */ export function sanitizeMetadataValue(value: string | undefined | null): string { if (!value) return ""; // Remove potentially problematic characters return value .replace(/[\r\n\t]/g, " ") // Replace newlines and tabs with spaces .replace(/[^\x20-\x7E]/g, "") // Remove non-printable characters .trim() .substring(0, 1000); // Limit length to prevent storage issues } /** * Create standardized metadata object */ export function createMetadata(data: Record<string, any>): Record<string, string> { const metadata: Record<string, string> = {}; for (const [key, value] of Object.entries(data)) { if (value !== undefined && value !== null) { metadata[key] = sanitizeMetadataValue(String(value)); } } return metadata; } /** * Merge metadata objects safely */ export function mergeMetadata( ...metadataObjects: (Record<string, string> | undefined)[] ): Record<string, string> { const result: Record<string, string> = {}; for (const metadata of metadataObjects) { if (metadata) { Object.assign(result, metadata); } } return result; } /** * Extract common metadata fields */ export function extractCommonMetadata(source: any): Record<string, string> { return createMetadata({ id: source.id, title: source.title, description: source.description, createdAt: source.createdAt || new Date().toISOString(), updatedAt: source.updatedAt || new Date().toISOString(), duration: source.duration, fileSize: source.fileSize, }); } /** * Create request metadata */ export function createRequestMetadata(requestId: string, additionalData?: Record<string, any>): Record<string, string> { return createMetadata({ requestId, timestamp: new Date().toISOString(), ...additionalData, }); }