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

40 lines (39 loc) 1.08 kB
import { FileOperationError, InvalidFormatError } from "../errors.js"; import { getTagLib } from "./config.js"; async function withAudioFile(file, fn, options) { const taglib = await getTagLib(); const audioFile = await taglib.open(file, options); try { if (!audioFile.isValid()) { throw new InvalidFormatError( "File may be corrupted or in an unsupported format" ); } return await fn(audioFile); } finally { audioFile.dispose(); } } async function withAudioFileSave(file, fn) { return withAudioFile(file, (audioFile) => { fn(audioFile); if (!audioFile.save()) { throw new FileOperationError( "save", "Failed to save metadata changes. The file may be read-only or corrupted." ); } return audioFile.getFileBuffer(); }, { partial: false }); } async function withAudioFileSaveToFile(file, fn) { return withAudioFile(file, async (audioFile) => { fn(audioFile); await audioFile.saveToFile(file); }); } export { withAudioFile, withAudioFileSave, withAudioFileSaveToFile };