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

78 lines (77 loc) 2.18 kB
import { getTagLib } from "../simple/config.js"; import { walkDirectory } from "./directory-walker.js"; import { processBatch, processFileWithTagLib } from "./file-processors.js"; async function scanWithTagLib(taglib, filePaths, opts) { const { includeProperties, continueOnError, onProgress, totalFound, signal } = opts; const items = []; const progress = { count: 0 }; const processor = async (filePath) => { try { const metadata = await processFileWithTagLib( filePath, taglib, includeProperties, onProgress, progress, totalFound ); return { status: "ok", ...metadata }; } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); if (continueOnError) { const current = ++progress.count; onProgress?.(current, totalFound, filePath); return { status: "error", path: filePath, error: err }; } else { throw err; } } }; const concurrency = 4; const batchSize = concurrency * 10; for (let i = 0; i < filePaths.length; i += batchSize) { signal?.throwIfAborted(); const batch = filePaths.slice( i, Math.min(i + batchSize, filePaths.length) ); const batchResults = await processBatch(batch, processor, concurrency); items.push(...batchResults); } return items; } async function scanFolder(folderPath, options = {}) { const startTime = Date.now(); const { maxFiles = Infinity, includeProperties = true, continueOnError = true, onProgress, signal } = options; const filePaths = []; let fileCount = 0; for await (const filePath of walkDirectory(folderPath, options)) { signal?.throwIfAborted(); filePaths.push(filePath); fileCount++; if (fileCount >= maxFiles) break; } const totalFound = filePaths.length; const taglib = await getTagLib(); const processOpts = { includeProperties, continueOnError, onProgress, totalFound, signal }; const items = await scanWithTagLib(taglib, filePaths, processOpts); return { items, duration: Date.now() - startTime }; } export { scanFolder };