(str: string)
| 9 | |
| 10 | /** @internal */ |
| 11 | export const decode = (str: string): Either.Either<Uint8Array, Encoding.DecodeException> => { |
| 12 | const stripped = Base64.stripCrlf(str) |
| 13 | const length = stripped.length |
| 14 | if (length % 4 === 1) { |
| 15 | return Either.left( |
| 16 | DecodeException(stripped, `Length should be a multiple of 4, but is ${length}`) |
| 17 | ) |
| 18 | } |
| 19 | |
| 20 | if (!/^[-_A-Z0-9]*?={0,2}$/i.test(stripped)) { |
| 21 | return Either.left(DecodeException(stripped, "Invalid input")) |
| 22 | } |
| 23 | |
| 24 | // Some variants allow or require omitting the padding '=' signs |
| 25 | let sanitized = length % 4 === 2 ? `${stripped}==` : length % 4 === 3 ? `${stripped}=` : stripped |
| 26 | sanitized = sanitized.replace(/-/g, "+").replace(/_/g, "/") |
| 27 | |
| 28 | return Base64.decode(sanitized) |
| 29 | } |
nothing calls this directly
no test coverage detected