|
| 1 | +const advice = { |
| 2 | + textEncoder: { |
| 3 | + name: "Text encoding", |
| 4 | + message: "This challenge needs the browser TextEncoder API.", |
| 5 | + fix: "Use an up-to-date browser with JavaScript enabled.", |
| 6 | + }, |
| 7 | + webCrypto: { |
| 8 | + name: "Web Crypto", |
| 9 | + message: "This challenge needs the Web Crypto API.", |
| 10 | + fix: "Use an up-to-date browser over HTTPS and disable extensions that block crypto.subtle.", |
| 11 | + }, |
| 12 | + worker: { |
| 13 | + name: "Web Workers", |
| 14 | + message: "This challenge must run in a Web Worker.", |
| 15 | + fix: "Disable extensions that block Web Workers or try a standard browser configuration.", |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Return advice only for missing capabilities required by this challenge. |
| 21 | + * |
| 22 | + * @param {number} challengeType |
| 23 | + * @param {{environment?: object, nativeCrypto?: boolean}} [options] |
| 24 | + * @return {{name: string, message: string, fix: string}[]} |
| 25 | + */ |
| 26 | +export function detectMissingCapabilities(challengeType, { |
| 27 | + environment = globalThis, |
| 28 | + nativeCrypto = import.meta.env.VITE_NATIVE_CRYPTO === "true", |
| 29 | +} = {}){ |
| 30 | + if (challengeType !== 1 && challengeType !== 2){ |
| 31 | + return []; |
| 32 | + } |
| 33 | + |
| 34 | + const missing = []; |
| 35 | + if (typeof environment.TextEncoder !== "function"){ |
| 36 | + missing.push(advice.textEncoder); |
| 37 | + } |
| 38 | + if (nativeCrypto && !environment.crypto?.subtle){ |
| 39 | + missing.push(advice.webCrypto); |
| 40 | + } |
| 41 | + if (challengeType === 2 && typeof environment.Worker !== "function"){ |
| 42 | + missing.push(advice.worker); |
| 43 | + } |
| 44 | + return missing; |
| 45 | +} |
0 commit comments