(raw, isBlob, isUserUrl)
| 62 | * @return {string|Uint8Array|Blob|string} |
| 63 | */ |
| 64 | export const decodeResource = (raw, isBlob, isUserUrl) => { |
| 65 | let res; |
| 66 | const pos = raw::stringIndexOf(','); |
| 67 | const mimeType = pos < 0 ? '' : raw::slice(isUserUrl ? 5/*data:*/ : 0, pos); |
| 68 | const mimeData = pos < 0 ? raw : raw::slice(pos + 1); |
| 69 | if (isBlob === false) { |
| 70 | return `data:${mimeType};base64,${mimeData}`; |
| 71 | } |
| 72 | res = !isUserUrl || mimeType::stringIndexOf('base64') >= 0 |
| 73 | ? safeAtob(mimeData) |
| 74 | : mimeData; |
| 75 | // TODO: do the check in BG and cache/store the result because safe-guarding all the stuff |
| 76 | // regexp picks from an instance internally is inordinately complicated |
| 77 | if (/[\x80-\xFF]/::regexpTest(res)) { |
| 78 | if (U8_fromBase64) { |
| 79 | res = U8_fromBase64(mimeData); |
| 80 | } else { |
| 81 | const len = res.length; |
| 82 | const bytes = new SafeUint8Array(len); |
| 83 | for (let i = 0; i < len; i += 1) { |
| 84 | bytes[i] = safeCharCodeAt(res, i); |
| 85 | } |
| 86 | res = bytes; |
| 87 | } |
| 88 | if (!isBlob) res = new SafeTextDecoder()::tdDecode(res); |
| 89 | } |
| 90 | if (isBlob && isBlob !== SafeUint8Array) { |
| 91 | res = makeSafeBlob(res, mimeType); |
| 92 | if (isBlob !== SafeBlob) res = createObjectURL(res); |
| 93 | } |
| 94 | return res; |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Used by `injected` |
no test coverage detected