taglib-wasm
Version:
TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps
53 lines (52 loc) • 1.68 kB
JavaScript
import { EnvironmentError, FileOperationError } from "../errors.js";
import { getPlatformIO } from "../runtime/platform-io.js";
async function readFileData(file) {
if (file instanceof Uint8Array) return file;
if (file instanceof ArrayBuffer) return new Uint8Array(file);
if (typeof File !== "undefined" && file instanceof File) {
return new Uint8Array(await file.arrayBuffer());
}
if (typeof file === "string") {
try {
return await getPlatformIO().readFile(file);
} catch (error) {
if (error instanceof EnvironmentError) throw error;
throw new FileOperationError("read", error.message, file);
}
}
const inputType = Object.prototype.toString.call(file);
throw new FileOperationError(
"read",
`Invalid file input type: ${inputType}. Expected string path, Uint8Array, ArrayBuffer, or File object.`
);
}
async function getFileSize(path) {
try {
const s = await getPlatformIO().stat(path);
return s.size;
} catch (error) {
if (error instanceof EnvironmentError) throw error;
throw new FileOperationError("stat", error.message, path);
}
}
async function readPartialFileData(path, headerSize, footerSize) {
const io = getPlatformIO();
if (!io.readPartial) {
throw new EnvironmentError(
"current runtime",
"does not support partial file reading",
"filesystem access with seek support"
);
}
try {
return await io.readPartial(path, headerSize, footerSize);
} catch (error) {
if (error instanceof EnvironmentError) throw error;
throw new FileOperationError("read", error.message, path);
}
}
export {
getFileSize,
readFileData,
readPartialFileData
};