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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 317x 40x 316x 315x 315x 643x 202x 202x 202x 202x 202x 643x 13x 7x 13x 13x 4x 4x 4x 4x 3x 3x 3x 3x 35x 35x 147x 147x 315x 40x 40x 240x 478x 249x 249x 229x 249x 178x 178x 178x 351x 351x 351x 6x 5x 11x 11x 11x 11x 6x 67x 20x 20x 20x 67x 278x 218x 218x 218x 278x 178x 10x 10x 17x 17x 13x 13x 13x 2x 11x 2x 9x 9x 4x 2x 2x 2x 2x 1x 1x 1x 10x 8x 8x 8x 2x 6x 2x 4x 2x 2x 1x 1x 8x 40x 40x 4x 4x 4x 8x 8x 8x 8x 7x 1x 1x 4x 48x 45x 42x 39x 4x 4x 4x | /**
* 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"} 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",
};
/**
* Parse a query string into an object
* @param {string} queryString - The query string to parse
* @param {Object.<string, ParamType>} [paramTypes={}] - A map of parameter names to their types
* @returns {Object} Object containing parsed query parameters
*/
export function parseQueryString(queryString, paramTypes = {}) {
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 = paramTypes[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:
// Only the exact 'false' string is treated as false.
parsedQuery[key] = val !== "false";
break;
case ParamTypes.STRING:
default:
parsedQuery[key] = val;
break;
}
});
return parsedQuery;
}
/**
* 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 {Object.<string, ParamType>} [paramTypes={}] - Object mapping parameter names to their types
* @returns {string} The complete URL with query parameters
*/
export function buildUrlFromParams(baseUrl, parsedQuery, paramTypes = {}) {
let url = baseUrl;
let sep = "?";
Object.entries(parsedQuery).forEach(([key, value]) => {
Iif (key.length === 0) return;
// Default to STRING unless explicitly specified
const paramType = paramTypes[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.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 keyboard and gamepad mapping parameters from query string
* @param {Object} parsedQuery - The parsed query parameters
* @param {Object} machineKeys - Emulated machine's key constants (`BBC`, or `ATOM` for the Atom)
* @param {(name: string) => string[]} hostKeyCodes - jsbeeb's host key name to `KeyboardEvent.code` names
* @param {Array} userKeymap - Array to store user key mappings
* @param {Object} gamepad - Gamepad object for handling mapping
* @returns {string[]} descriptions of any mappings that were skipped, for showing to the user
*/
export function processInputParams(parsedQuery, machineKeys, hostKeyCodes, userKeymap, gamepad) {
const warnings = [];
Object.entries(parsedQuery).forEach(([key, val]) => {
Iif (!val) return;
// `KEY.<host key>=<machine key>`, eg `KEY.CAPSLOCK=CTRL`. Host names come from
// `keyCodes`, so the BBC's RETURN is ENTER here; both lists are in the README.
if (key.toUpperCase().indexOf("KEY.") === 0) {
const machineKey = val.toUpperCase();
const nativeKey = key.substring(4).toUpperCase(); // remove KEY.
if (!machineKeys[machineKey]) {
warnings.push(`${key}=${val}: "${machineKey}" is not a key on the emulated machine.`);
} else if (hostKeyCodes(nativeKey).length === 0) {
warnings.push(`${key}=${val}: "${nativeKey}" is not a key on your keyboard.`);
} else {
console.log("mapping " + nativeKey + " to " + machineKey);
userKeymap.push({ native: nativeKey, key: machineKey });
}
} else if (key.indexOf("GP.") === 0) {
// gamepad mapping
// eg ?GP.FIRE2=RETURN
const gamepadKey = key.substring(3).toUpperCase(); // remove GP. prefix
const problem = gamepad.remap(gamepadKey, val.toUpperCase());
if (problem) warnings.push(`${key}=${val}: ${problem}`);
} else {
switch (key) {
case "LEFT":
case "RIGHT":
case "UP":
case "DOWN":
case "FIRE": {
const problem = gamepad.remap(key, val.toUpperCase());
Iif (problem) warnings.push(`${key}=${val}: ${problem}`);
break;
}
}
}
});
return warnings;
}
/**
* Process autoboot and other emulation parameters
* @param {Object} parsedQuery - The parsed query parameters
* @returns {Object} Information about autoboot settings
*/
export function processAutobootParams(parsedQuery) {
let needsAutoboot = false;
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 };
}
/** 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 };
}
|