* `value` itself, if it is base64 of a packed extension. Checking the decoded bytes, not just the * alphabet, matters: a mistyped relative path such as `extensions/myext` is valid base64 too, and * sending it would surface as an unrelated error from the browser. * @param {string} value * @return
(value)
| 216 | * @throws {error.InvalidArgumentError} if it is neither. |
| 217 | */ |
| 218 | function asBase64(value) { |
| 219 | const bytes = /^[A-Za-z0-9+/]+={0,2}$/.test(value) ? Buffer.from(value, 'base64') : null |
| 220 | if (!bytes || !ARCHIVE_SIGNATURES.some((signature) => bytes.subarray(0, signature.length).equals(signature))) { |
| 221 | throw new error.InvalidArgumentError( |
| 222 | `No such file or directory, and not base64 of a packed extension (.xpi/.zip/.crx): ${truncate(value)}`, |
| 223 | ) |
| 224 | } |
| 225 | return value |
| 226 | } |
| 227 | |
| 228 | function truncate(value, max = 80) { |
| 229 | return value.length > max ? `${value.slice(0, max)}...` : value |
no test coverage detected