All files / src bitshifters.js

100% Statements 21/21
100% Branches 9/9
100% Functions 5/5
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 482x                   12x 12x               10x 8x 3x 3x   10x       8x 8x 6x 6x 5x               3x 3x 3x 3x 2x      
const SiteBase = "https://bitshifters.github.io/content";
 
/**
 * The discs Bitshifters publish at bitshifters.github.io: their own demos and
 * games, and the archived releases of other BBC Micro groups, each with the
 * page that presents it.
 */
export class BitshiftersArchive {
    /** @param {string} [baseUrl] where the site keeps its content, to point at a test prefix */
    constructor(baseUrl = SiteBase) {
        this._baseUrl = `${baseUrl}/`;
        this._catalogue = null;
    }
 
    /**
     * @returns {Promise<object[]>} every manifest entry, fetched once however many ask at
     *     the same time; a failed fetch is forgotten so the next asker tries again
     */
    catalogue() {
        if (!this._catalogue)
            this._catalogue = this._fetchCatalogue().catch((error) => {
                this._catalogue = null;
                throw error;
            });
        return this._catalogue;
    }
 
    async _fetchCatalogue() {
        const response = await fetch(`${this._baseUrl}manifest.json`);
        if (!response.ok) throw new Error(`Network response was not ok (${response.status})`);
        const data = await response.json();
        if (!Array.isArray(data?.files)) throw new Error("Invalid manifest: missing files array");
        return data.files;
    }
 
    /**
     * @param {string} path a manifest entry's `path`, an SSD or DSD image
     * @returns {Promise<Uint8Array>} the image
     */
    async fetch(path) {
        const url = this._baseUrl + path.split("/").map(encodeURIComponent).join("/");
        console.log("Loading disc from " + url);
        const response = await fetch(url);
        if (!response.ok) throw new Error(`Network response was not ok (${response.status})`);
        return new Uint8Array(await response.arrayBuffer());
    }
}