@catamphetamine/id3js
Version:
A modern ID3 parser written completely in JavaScript, making use of typed arrays and the HTML5 File API
34 lines (33 loc) • 951 B
JavaScript
import { Reader } from './reader/reader.js';
/**
* Reads a remote URL
*/
export class RemoteReader extends Reader {
/**
* @param {string} url URL to retrieve
* @param {function} fetch fetch() function implementation
*/
constructor(url, fetch) {
super();
this._url = url;
this._fetch = fetch;
}
/** @inheritdoc */
async open() {
const resp = await this._fetch(this._url, {
method: 'HEAD'
});
const contentLength = resp.headers.get('Content-Length');
this.size = contentLength ? Number(contentLength) : 0;
}
/** @inheritdoc */
async read(length, position) {
const resp = await this._fetch(this._url, {
method: 'GET',
headers: {
Range: `bytes=${position}-${position + length - 1}`
}
});
return await resp.arrayBuffer();
}
}