taglib-wasm
Version:
TagLib for TypeScript platforms: Deno, Node.js, Bun, Electron, browsers, and Cloudflare Workers
185 lines (184 loc) • 5.63 kB
JavaScript
const SUPPORTED_FORMATS = [
"MP3",
"MP4",
"M4A",
"FLAC",
"OGG",
"WAV"
];
var TagLibErrorCode = /* @__PURE__ */ ((TagLibErrorCode2) => {
TagLibErrorCode2["INITIALIZATION_FAILED"] = "INITIALIZATION_FAILED";
TagLibErrorCode2["INVALID_FORMAT"] = "INVALID_FORMAT";
TagLibErrorCode2["UNSUPPORTED_FORMAT"] = "UNSUPPORTED_FORMAT";
TagLibErrorCode2["FILE_OPERATION_FAILED"] = "FILE_OPERATION_FAILED";
TagLibErrorCode2["METADATA_ERROR"] = "METADATA_ERROR";
TagLibErrorCode2["MEMORY_ERROR"] = "MEMORY_ERROR";
TagLibErrorCode2["ENVIRONMENT_ERROR"] = "ENVIRONMENT_ERROR";
return TagLibErrorCode2;
})(TagLibErrorCode || {});
class TagLibError extends Error {
constructor(message, code, context) {
super(message);
this.code = code;
this.context = context;
this.name = "TagLibError";
Object.setPrototypeOf(this, TagLibError.prototype);
}
}
class TagLibInitializationError extends TagLibError {
constructor(message, context) {
super(
createErrorMessage("Failed to initialize TagLib Wasm module", message),
"INITIALIZATION_FAILED" /* INITIALIZATION_FAILED */,
context
);
this.name = "TagLibInitializationError";
Object.setPrototypeOf(this, TagLibInitializationError.prototype);
}
}
class InvalidFormatError extends TagLibError {
constructor(message, bufferSize, context) {
const details = [`Invalid audio file format: ${message}`];
if (bufferSize !== void 0) {
details.push(`Buffer size: ${formatFileSize(bufferSize)}`);
if (bufferSize < 1024) {
details.push(
"Audio files must be at least 1KB to contain valid headers."
);
}
}
super(
details.join(". "),
"INVALID_FORMAT" /* INVALID_FORMAT */,
{ ...context, bufferSize }
);
this.bufferSize = bufferSize;
this.name = "InvalidFormatError";
Object.setPrototypeOf(this, InvalidFormatError.prototype);
}
}
class UnsupportedFormatError extends TagLibError {
constructor(format, supportedFormats = SUPPORTED_FORMATS, context) {
super(
`Unsupported audio format: ${format}. Supported formats: ${supportedFormats.join(", ")}`,
"UNSUPPORTED_FORMAT" /* UNSUPPORTED_FORMAT */,
{ ...context, format, supportedFormats }
);
this.format = format;
this.supportedFormats = supportedFormats;
this.name = "UnsupportedFormatError";
Object.setPrototypeOf(this, UnsupportedFormatError.prototype);
}
}
class FileOperationError extends TagLibError {
constructor(operation, message, path, context) {
const details = [`Failed to ${operation} file`];
if (path) {
details.push(`Path: ${path}`);
}
details.push(message);
super(
details.join(". "),
"FILE_OPERATION_FAILED" /* FILE_OPERATION_FAILED */,
{ ...context, operation, path }
);
this.operation = operation;
this.path = path;
this.name = "FileOperationError";
Object.setPrototypeOf(this, FileOperationError.prototype);
}
}
class MetadataError extends TagLibError {
constructor(operation, message, field, context) {
const details = [`Failed to ${operation} metadata`];
if (field) {
details.push(`Field: ${field}`);
}
details.push(message);
super(
details.join(". "),
"METADATA_ERROR" /* METADATA_ERROR */,
{ ...context, operation, field }
);
this.operation = operation;
this.field = field;
this.name = "MetadataError";
Object.setPrototypeOf(this, MetadataError.prototype);
}
}
class MemoryError extends TagLibError {
constructor(message, context) {
super(
createErrorMessage("Memory allocation failed", message),
"MEMORY_ERROR" /* MEMORY_ERROR */,
context
);
this.name = "MemoryError";
Object.setPrototypeOf(this, MemoryError.prototype);
}
}
class EnvironmentError extends TagLibError {
constructor(environment, message, requiredFeature, context) {
const details = [`Environment '${environment}' ${message}`];
if (requiredFeature) {
details.push(`Required feature: ${requiredFeature}`);
}
super(
details.join(". "),
"ENVIRONMENT_ERROR" /* ENVIRONMENT_ERROR */,
{ ...context, environment, requiredFeature }
);
this.environment = environment;
this.requiredFeature = requiredFeature;
this.name = "EnvironmentError";
Object.setPrototypeOf(this, EnvironmentError.prototype);
}
}
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`;
}
function isTagLibError(error) {
return error instanceof TagLibError;
}
function isInvalidFormatError(error) {
return error instanceof InvalidFormatError;
}
function isUnsupportedFormatError(error) {
return error instanceof UnsupportedFormatError;
}
function isFileOperationError(error) {
return error instanceof FileOperationError;
}
function isMetadataError(error) {
return error instanceof MetadataError;
}
function isMemoryError(error) {
return error instanceof MemoryError;
}
function isEnvironmentError(error) {
return error instanceof EnvironmentError;
}
export {
EnvironmentError,
FileOperationError,
InvalidFormatError,
MemoryError,
MetadataError,
SUPPORTED_FORMATS,
TagLibError,
TagLibErrorCode,
TagLibInitializationError,
UnsupportedFormatError,
isEnvironmentError,
isFileOperationError,
isInvalidFormatError,
isMemoryError,
isMetadataError,
isTagLibError,
isUnsupportedFormatError
};