taglib-wasm
Version:
TagLib for TypeScript platforms: Deno, Node.js, Bun, Electron, browsers, and Cloudflare Workers
219 lines • 6.81 kB
TypeScript
/**
* @fileoverview Cloudflare Workers-specific TagLib API
*
* This module provides a specialized API for using TagLib in Cloudflare Workers
* and other edge computing environments where the standard Emscripten module
* loading may not work. It uses C-style function exports for compatibility.
*
* @module taglib-wasm/workers
*/
import type { AudioFormat, AudioProperties, ExtendedTag, Tag, TagLibWorkersConfig } from "./types.ts";
import { type TagLibModule } from "./wasm-workers.ts";
/**
* Represents an audio file with metadata and properties (Workers-compatible).
* This implementation uses C-style function calls for Cloudflare Workers compatibility.
*
* @example
* ```typescript
* const file = taglib.openFile(audioBuffer);
*
* // Get metadata
* const tag = file.tag();
* console.log(tag.title);
*
* // Modify metadata
* file.setTitle("New Title");
* file.save();
*
* // Clean up
* file.dispose();
* ```
*/
export declare class AudioFileWorkers {
private module;
private fileId;
private tagPtr;
private propsPtr;
constructor(module: TagLibModule, fileId: number);
/**
* Check if the file is valid and was loaded successfully.
* @returns true if the file is valid and can be processed
*/
isValid(): boolean;
/**
* Get the file format.
* @returns Audio format (e.g., "MP3", "FLAC", "OGG")
*/
format(): AudioFormat;
/**
* Get basic tag information.
* @returns Object containing title, artist, album, etc.
*/
tag(): Tag;
/**
* Get audio properties (duration, bitrate, etc.).
* @returns Audio properties or null if unavailable
*/
audioProperties(): AudioProperties | null;
/**
* Set the title tag.
* @param title - New title value
*/
setTitle(title: string): void;
/**
* Set the artist tag.
* @param artist - New artist value
*/
setArtist(artist: string): void;
/**
* Set the album tag.
* @param album - New album value
*/
setAlbum(album: string): void;
/**
* Set the comment tag.
* @param comment - New comment value
*/
setComment(comment: string): void;
/**
* Set the genre tag.
* @param genre - New genre value
*/
setGenre(genre: string): void;
/**
* Set the year tag.
* @param year - Release year
*/
setYear(year: number): void;
/**
* Set the track number tag.
* @param track - Track number
*/
setTrack(track: number): void;
/**
* Save changes to the file.
* Note: In Workers context, this saves to the in-memory buffer only.
* @returns true if save was successful
*/
save(): boolean;
/**
* Get the current file buffer after modifications.
* Note: This is not implemented in the Workers API.
* @returns Empty Uint8Array (not implemented)
* @throws {Error} Consider using the Full API for this functionality
*/
getFileBuffer(): Uint8Array;
/**
* Get extended metadata with format-agnostic field names.
* Note: Currently returns only basic fields in Workers API.
* @returns Extended tag object with basic fields populated
*/
extendedTag(): ExtendedTag;
/**
* Set extended metadata using format-agnostic field names.
* Note: Currently only supports basic fields in Workers API.
* @param tag - Partial extended tag object with fields to update
*/
setExtendedTag(tag: Partial<ExtendedTag>): void;
/**
* Clean up resources.
* Always call this when done to prevent memory leaks.
*/
dispose(): void;
}
/**
* Main TagLib class for Cloudflare Workers.
* Provides methods to initialize the library and open audio files
* in edge computing environments.
*
* @example
* ```typescript
* import wasmBinary from "../build/taglib.wasm";
*
* // Initialize TagLib
* const taglib = await TagLibWorkers.initialize(wasmBinary);
*
* // Process audio file
* const file = taglib.openFile(audioBuffer);
* const metadata = file.tag();
* file.dispose();
* ```
*/
export declare class TagLibWorkers {
private module;
private constructor();
/**
* Initialize TagLib for Workers with Wasm binary
*
* @param wasmBinary - The WebAssembly binary as Uint8Array
* @param config - Optional configuration for the Wasm module
*
* @example
* ```typescript
* // In a Cloudflare Worker
* import wasmBinary from "../build/taglib.wasm";
*
* const taglib = await TagLibWorkers.initialize(wasmBinary);
* const file = taglib.open(audioBuffer);
* const metadata = file.tag();
* ```
*/
static initialize(wasmBinary: Uint8Array, config?: TagLibWorkersConfig): Promise<TagLibWorkers>;
/**
* Open an audio file from a buffer.
*
* @param buffer - Audio file data as Uint8Array
* @returns AudioFileWorkers instance
* @throws {Error} If Wasm module is not initialized
* @throws {Error} If file format is invalid or unsupported
* @throws {Error} If Workers API C-style functions are not available
*
* @example
* ```typescript
* const audioData = new Uint8Array(await request.arrayBuffer());
* const file = taglib.open(audioData);
* ```
*/
open(buffer: Uint8Array): AudioFileWorkers;
/**
* Open an audio file from a buffer (backward compatibility).
* Consider using `open()` for consistency with the Full API.
* @param buffer Audio file data as Uint8Array
* @returns Audio file instance
*/
openFile(buffer: Uint8Array): AudioFileWorkers;
/**
* Get the underlying Wasm module for advanced usage.
* @returns The initialized TagLib Wasm module
*/
getModule(): TagLibModule;
}
/**
* Utility function to process audio metadata in a Cloudflare Worker
*
* @example
* ```typescript
* export default {
* async fetch(request: Request): Promise<Response> {
* if (request.method === "POST") {
* const audioData = new Uint8Array(await request.arrayBuffer());
* const metadata = await processAudioMetadata(wasmBinary, audioData);
* return Response.json(metadata);
* }
* return new Response("Method not allowed", { status: 405 });
* }
* };
* ```
*/
export declare function processAudioMetadata(wasmBinary: Uint8Array, audioData: Uint8Array, config?: TagLibWorkersConfig): Promise<{
tag: Tag;
properties: AudioProperties | null;
format: AudioFormat;
}>;
/**
* Re-export commonly used types for convenience.
* These types define the structure of metadata, audio properties,
* and configuration options.
*/
export type { AudioFormat, AudioProperties, ExtendedTag, Tag, TagLibWorkersConfig, };
//# sourceMappingURL=workers.d.ts.map