UNPKG

1.35 kBJavaScriptView Raw
1import { parse } from './id3Tag.js';
2import { BrowserFileReader } from './browserFileReader.js';
3import { RemoteReader } from './remoteReader.js';
4const SUPPORTS_FILE = typeof window !== 'undefined' &&
5 'File' in window &&
6 'FileReader' in window &&
7 typeof ArrayBuffer !== 'undefined';
8/**
9 * Parses ID3 tags from a given reader
10 * @param {Reader} reader Reader to use
11 * @return {Promise<ID3Tag>}
12 */
13export async function fromReader(reader) {
14 await reader.open();
15 const tags = await parse(reader);
16 await reader.close();
17 return tags;
18}
19/**
20 * Parses ID3 tags from a local path
21 * @param {string} path Path to file
22 * @return {Promise<ID3Tag>}
23 */
24export async function fromPath(path) {
25 const mod = await import('./localReader.js');
26 return fromReader(new mod.LocalReader(path));
27}
28/**
29 * Parses ID3 tags from a specified URL
30 * @param {string} url URL to retrieve data from
31 * @return {Promise<ID3Tag>}
32 */
33export function fromUrl(url) {
34 return fromReader(new RemoteReader(url));
35}
36/**
37 * Parses ID3 tags from a File instance
38 * @param {File} file File to parse
39 * @return {Promise<ID3Tag>}
40 */
41export function fromFile(file) {
42 if (!SUPPORTS_FILE) {
43 throw new Error('Browser does not have support for the File API and/or ' + 'ArrayBuffers');
44 }
45 return fromReader(new BrowserFileReader(file));
46}