All files / src url-params.js

98.23% Statements 111/113
91.66% Branches 77/84
94.44% Functions 17/18
99.03% Lines 103/104

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284                    828x                     111x                                         36x 816x   591x   591x 1306x   569x 569x 569x 569x   568x   1306x   27x 20x   27x 27x   24x 24x 24x   24x   80x 80x 80x   80x       132x 132x     305x 305x       591x                   435x               111x   111x 666x                   1154x                       639x 639x 515x   639x                   72x 638x 638x   638x 1090x   1090x   1090x     7x 6x 9x 9x 9x 9x       7x     452x 123x 123x 123x   452x   5x 4x 4x 4x   5x           626x 503x 503x 503x   626x       638x                 66x 66x   66x 19x 47x 7x 40x 8x 32x 8x 8x     66x         35x 31x       111x   111x                 4x 4x 4x 8x 8x 8x 8x 7x   1x 1x     4x                 84x 81x 78x 75x                       31x   31x   31x    
/**
 * URL parameter handling for jsbeeb
 */
 
/**
 * Check if a value is defined (not null and not undefined)
 * @param {*} value - The value to check
 * @returns {boolean} True if the value is neither null nor undefined
 */
function isDefined(value) {
    return value !== null && value !== undefined;
}
 
/**
 * @typedef {"string"|"array"|"int"|"float"|"bool"|"boolDefaultTrue"} ParamType
 */
 
/**
 * Parameter type enum to avoid string literals
 * @enum {string}
 */
export const ParamTypes = {
    /** String parameter (default) */
    STRING: "string",
    /** Array parameter (for parameters that can appear multiple times) */
    ARRAY: "array",
    /** Integer parameter */
    INT: "int",
    /** Float parameter */
    FLOAT: "float",
    /** Boolean parameter (true if present, regardless of value) */
    BOOL: "bool",
    /** Boolean parameter the page takes as true when absent, so the URL carries it as `key=false` when it is off */
    BOOL_DEFAULT_TRUE: "boolDefaultTrue",
};
 
/**
 * Parse a query string into an object
 * @param {string} queryString - The query string to parse
 * @param {(name: string) => ParamType|undefined} [typeOf] - The type of the parameter `name`, STRING if none
 * @returns {Object} Object containing parsed query parameters
 */
export function parseQueryString(queryString, typeOf = () => undefined) {
    if (!queryString) return {};
 
    const parsedQuery = {};
 
    queryString.split("&").forEach(function (keyval) {
        if (!keyval) return;
 
        const keyAndVal = keyval.split("=");
        const key = decodeURIComponent(keyAndVal[0]);
        let val = null;
        if (keyAndVal.length > 1) val = decodeURIComponent(keyAndVal.slice(1).join("="));
 
        const paramType = typeOf(key) ?? ParamTypes.STRING;
 
        switch (paramType) {
            case ParamTypes.ARRAY:
                if (!parsedQuery[key]) {
                    parsedQuery[key] = [];
                }
                parsedQuery[key].push(val);
                break;
            case ParamTypes.INT:
                Eif (val !== undefined) {
                    const parsed = parseInt(val, 10);
                    parsedQuery[key] = isNaN(parsed) ? 0 : parsed;
                }
                break;
            case ParamTypes.FLOAT:
                Eif (val !== undefined) {
                    const parsed = parseFloat(val);
                    parsedQuery[key] = isNaN(parsed) ? 0 : parsed;
                }
                break;
            case ParamTypes.BOOL:
            case ParamTypes.BOOL_DEFAULT_TRUE:
                // Only the exact 'false' string is treated as false.
                parsedQuery[key] = val !== "false";
                break;
            case ParamTypes.STRING:
            default:
                parsedQuery[key] = val;
                break;
        }
    });
 
    return parsedQuery;
}
 
/**
 * Parse the parameters of a page's location: those in its query, and those after its hash.
 * @param {{search: string, hash: string}} location - A Location or URL
 * @param {(name: string) => ParamType|undefined} [typeOf] - The type of the parameter `name`, STRING if none
 * @returns {Object} Object containing parsed query parameters
 */
export function parseLocationParams(location, typeOf = () => undefined) {
    return parseQueryString(location.search.substring(1) + "&" + location.hash.substring(1), typeOf);
}
 
/**
 * Characters RFC 3986 allows literally in a query that `encodeURIComponent` escapes anyway. `&`,
 * `=`, `+`, `#`, `%` and space are left escaped because they delimit something, and `?` because a
 * URL with a second one in it reads as broken even though the grammar permits it.
 */
const QueryLiterals = "$,/:;@";
 
const EscapedQueryLiterals = new RegExp(
    [...QueryLiterals].map((char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`).join("|"),
    "g",
);
 
/**
 * Percent-encode a key or value for a query string, escaping only what delimits something
 * @param {string} component - The key or value to encode
 * @returns {string} The encoded component
 */
function encodeQueryComponent(component) {
    return encodeURIComponent(component).replace(EscapedQueryLiterals, decodeURIComponent);
}
 
/**
 * Append a parameter to the URL
 * @param {string} url - Current URL
 * @param {string} sep - Current separator (? or &)
 * @param {string} key - Parameter key
 * @param {string} [value] - Parameter value (optional for boolean parameters)
 * @returns {Object} Updated URL and separator
 */
function appendParam(url, sep, key, value = undefined) {
    url += sep + encodeQueryComponent(key);
    if (value !== undefined) {
        url += "=" + encodeQueryComponent(value);
    }
    return { url, sep: "&" };
}
 
/**
 * Build a URL string from base URL and query parameters
 * @param {string} baseUrl - The base URL (without query string)
 * @param {Object} parsedQuery - Object containing query parameters
 * @param {(name: string) => ParamType|undefined} [typeOf] - The type of the parameter `name`, STRING if none
 * @returns {string} The complete URL with query parameters
 */
export function buildUrlFromParams(baseUrl, parsedQuery, typeOf = () => undefined) {
    let url = baseUrl;
    let sep = "?";
 
    Object.entries(parsedQuery).forEach(([key, value]) => {
        Iif (key.length === 0) return;
 
        const paramType = typeOf(key) ?? ParamTypes.STRING;
 
        switch (paramType) {
            case ParamTypes.ARRAY:
                // Handle array parameters - each item becomes a separate parameter
                if (Array.isArray(value) && value.length > 0) {
                    value.forEach((val) => {
                        Eif (isDefined(val)) {
                            const result = appendParam(url, sep, key, val);
                            url = result.url;
                            sep = result.sep;
                        }
                    });
                }
                break;
            case ParamTypes.BOOL:
                // For boolean params, only add the key without value if true
                if (value === true) {
                    const result = appendParam(url, sep, key);
                    url = result.url;
                    sep = result.sep;
                }
                break;
            case ParamTypes.BOOL_DEFAULT_TRUE:
                if (typeof value === "boolean") {
                    const result = appendParam(url, sep, key, value ? undefined : "false");
                    url = result.url;
                    sep = result.sep;
                }
                break;
            case ParamTypes.INT:
            case ParamTypes.FLOAT:
            case ParamTypes.STRING:
            default:
                // Include the parameter if it has a value (including zero)
                if (isDefined(value) && value !== "") {
                    const result = appendParam(url, sep, key, value);
                    url = result.url;
                    sep = result.sep;
                }
                break;
        }
    });
 
    return url;
}
 
/**
 * Process autoboot and other emulation parameters
 * @param {Object} parsedQuery - The parsed query parameters
 * @returns {{needsAutoboot: "none"|"boot"|"chain"|"run"|"type", autoType: string}} how the page boots
 */
export function processAutobootParams(parsedQuery) {
    let needsAutoboot = "none";
    let autoType = "";
 
    if (isDefined(parsedQuery.autoboot)) {
        needsAutoboot = "boot";
    } else if (isDefined(parsedQuery.autochain)) {
        needsAutoboot = "chain";
    } else if (isDefined(parsedQuery.autorun)) {
        needsAutoboot = "run";
    } else if (isDefined(parsedQuery.autotype)) {
        needsAutoboot = "type";
        autoType = parsedQuery.autotype;
    }
 
    return { needsAutoboot, autoType };
}
 
/** How the page was asked to boot: as `processAutobootParams` says, or `basic` for a BASIC program in the URL. */
export function bootModeOf(parsedQuery) {
    if (parsedQuery.loadBasic || parsedQuery.embedBasic) return "basic";
    return processAutobootParams(parsedQuery).needsAutoboot;
}
 
/** Where a drive's 40/80 switch is set, `auto` leaving it to whatever disc is loaded. */
export const DriveTracks = Object.freeze({ auto: "auto", forty: "40", eighty: "80" });
 
const NumDrives = 2;
 
/**
 * Process the per-drive 40/80 track settings
 * @param {Object} parsedQuery - The parsed query parameters
 * @returns {{settings: string[], warnings: string[]}} One DriveTracks per drive, and what was
 *   unusable about anything asked for that is not in there
 */
export function processDriveTrackParams(parsedQuery) {
    const warnings = [];
    const settings = [];
    for (let driveIndex = 0; driveIndex < NumDrives; ++driveIndex) {
        const name = `drive${driveIndex}Tracks`;
        const asked = parsedQuery[name];
        const setting = isDefined(asked) ? `${asked}`.toLowerCase() : DriveTracks.auto;
        if (Object.values(DriveTracks).includes(setting)) {
            settings.push(setting);
        } else {
            warnings.push(`${name}=${asked}: a drive is set to 40, 80 or auto.`);
            settings.push(DriveTracks.auto);
        }
    }
    return { settings, warnings };
}
 
/**
 * Guess the appropriate model based on the hostname
 * @param {string} hostname - The hostname to check
 * @returns {string} Model identifier
 */
export function guessModelFromHostname(hostname) {
    if (hostname.startsWith("bbc")) return "B-DFS1.2";
    if (hostname.startsWith("master")) return "Master";
    if (hostname.startsWith("atom")) return "Atom";
    return "B-DFS1.2";
}
 
/**
 * Parse disc images from the query parameters
 * @param {Object} parsedQuery - The query parameters
 * @returns {Object} Object containing disc information
 *   - discImage: disc image URL (?disc1= or, failing that, ?disc=)
 *   - secondDiscImage: second disc URL (?disc2=)
 *   - mmcImage: MMC/SD card image URL (?mmc=, Atom only)
 */
export function parseMediaParams(parsedQuery) {
    const { disc, disc1, disc2, mmc } = parsedQuery;
    // disc1 is the name the drives use; a bare disc is the older spelling, and gives way to it.
    const discImage = disc1 || disc;
 
    return { discImage, secondDiscImage: disc2, mmcImage: mmc };
}