UNPKG

taglib-wasm

Version:

TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps

171 lines (170 loc) 5.97 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { SUPPORTED_FORMATS, TagLibError } from "./base.js"; function errorMessage(error) { return error instanceof Error ? error.message : String(error); } function createErrorMessage(prefix, details) { return `${prefix}: ${details}`; } function formatFileSize(bytes) { if (bytes < 1024) return `${bytes} bytes`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } class TagLibInitializationError extends TagLibError { /** * Creates a new TagLibInitializationError * @param message - Description of the initialization failure * @param details - Additional context about the error */ constructor(message, details) { super( "INITIALIZATION", createErrorMessage("Failed to initialize TagLib Wasm module", message), details ); this.name = "TagLibInitializationError"; Object.setPrototypeOf(this, TagLibInitializationError.prototype); } } class InvalidFormatError extends TagLibError { /** * Creates a new InvalidFormatError * @param message - Description of the format error * @param bufferSize - Size of the audio buffer in bytes * @param details - Additional context about the error */ constructor(message, bufferSize, details) { const errorDetails = [`Invalid audio file format: ${message}`]; if (bufferSize !== void 0) { errorDetails.push(`Buffer size: ${formatFileSize(bufferSize)}`); if (bufferSize < 1024) { errorDetails.push( "Audio files must be at least 1KB to contain valid headers." ); } } super( "INVALID_FORMAT", errorDetails.join(". "), { ...details, bufferSize } ); __publicField(this, "bufferSize", bufferSize); this.name = "InvalidFormatError"; Object.setPrototypeOf(this, InvalidFormatError.prototype); } } class UnsupportedFormatError extends TagLibError { /** * Creates a new UnsupportedFormatError * @param format - The unsupported format that was encountered * @param supportedFormats - List of formats that are supported * @param details - Additional context about the error */ constructor(format, supportedFormats = SUPPORTED_FORMATS, details) { super( "UNSUPPORTED_FORMAT", `Unsupported audio format: ${format}. Supported formats: ${supportedFormats.join(", ")}`, { ...details, format, supportedFormats } ); __publicField(this, "format", format); __publicField(this, "supportedFormats", supportedFormats); this.name = "UnsupportedFormatError"; Object.setPrototypeOf(this, UnsupportedFormatError.prototype); } } class FileOperationError extends TagLibError { /** * Creates a new FileOperationError * @param operation - The file operation that failed * @param message - Description of the failure * @param path - File path involved in the operation * @param details - Additional context about the error */ constructor(operation, message, path, details) { const errorDetails = [`Failed to ${operation} file`]; if (path) { errorDetails.push(`Path: ${path}`); } errorDetails.push(message); super( "FILE_OPERATION", errorDetails.join(". "), { ...details, operation, path } ); __publicField(this, "operation", operation); __publicField(this, "path", path); this.name = "FileOperationError"; Object.setPrototypeOf(this, FileOperationError.prototype); } } class MetadataError extends TagLibError { /** * Creates a new MetadataError * @param operation - The metadata operation that failed * @param message - Description of the failure * @param field - The metadata field involved * @param details - Additional context about the error */ constructor(operation, message, field, details) { const errorDetails = [`Failed to ${operation} metadata`]; if (field) { errorDetails.push(`Field: ${field}`); } errorDetails.push(message); super( "METADATA", errorDetails.join(". "), { ...details, operation, field } ); __publicField(this, "operation", operation); __publicField(this, "field", field); this.name = "MetadataError"; Object.setPrototypeOf(this, MetadataError.prototype); } } class MemoryError extends TagLibError { /** * Creates a new MemoryError * @param message - Description of the memory failure * @param details - Additional context about the error */ constructor(message, details) { super( "MEMORY", createErrorMessage("Memory allocation failed", message), details ); this.name = "MemoryError"; Object.setPrototypeOf(this, MemoryError.prototype); } } class EnvironmentError extends TagLibError { /** * Creates a new EnvironmentError * @param environment - The runtime environment name * @param reason - Why the environment is incompatible * @param requiredFeature - The feature that is missing */ constructor(environment, reason, requiredFeature) { const message = requiredFeature ? `Environment '${environment}' ${reason}. Required feature: ${requiredFeature}.` : `Environment '${environment}' ${reason}.`; super("ENVIRONMENT", message); __publicField(this, "environment", environment); __publicField(this, "reason", reason); __publicField(this, "requiredFeature", requiredFeature); this.name = "EnvironmentError"; Object.setPrototypeOf(this, EnvironmentError.prototype); } } export { EnvironmentError, FileOperationError, InvalidFormatError, MemoryError, MetadataError, TagLibInitializationError, UnsupportedFormatError, errorMessage };