|
1 | 1 | import { assert, expect } from 'chai'; |
2 | | -import { |
3 | | - fromByteArray, |
4 | | - toByteArray, |
5 | | - trimBase64Padding, |
6 | | -} from 'react-native-quick-base64'; |
| 2 | + |
| 3 | +declare global { |
| 4 | + // eslint-disable-next-line no-var |
| 5 | + var base64ToArrayBuffer: ( |
| 6 | + base64: string, |
| 7 | + removeLinebreaks?: boolean, |
| 8 | + ) => ArrayBuffer; |
| 9 | + // eslint-disable-next-line no-var |
| 10 | + var base64FromArrayBuffer: (buffer: ArrayBuffer, urlSafe?: boolean) => string; |
| 11 | +} |
| 12 | + |
7 | 13 | import type { |
8 | 14 | CryptoKey, |
9 | 15 | CryptoKeyPair, |
@@ -72,14 +78,19 @@ function base64ToArrayBuffer(val: string): ArrayBuffer { |
72 | 78 | while (cleaned.endsWith('.')) { |
73 | 79 | cleaned = cleaned.slice(0, -1); |
74 | 80 | } |
75 | | - const arr = toByteArray(cleaned); |
76 | | - return binaryLikeToArrayBuffer(arr); |
| 81 | + return global.base64ToArrayBuffer(cleaned); |
77 | 82 | } |
78 | 83 |
|
79 | | -// TODO: add in `url` from react-native-quick-base64 when 2.1.1 is released |
80 | 84 | function arrayBufferToBase64(buffer: ArrayBuffer, urlSafe: boolean = false) { |
81 | | - const bytes = new Uint8Array(buffer); |
82 | | - return fromByteArray(bytes, urlSafe); |
| 85 | + return global.base64FromArrayBuffer(buffer, urlSafe); |
| 86 | +} |
| 87 | + |
| 88 | +function trimBase64Padding(str: string): string { |
| 89 | + return str.replace(/[.=]{1,2}$/, ''); |
| 90 | +} |
| 91 | + |
| 92 | +function toByteArray(b64: string): Uint8Array { |
| 93 | + return new Uint8Array(global.base64ToArrayBuffer(b64)); |
83 | 94 | } |
84 | 95 |
|
85 | 96 | const SUITE = 'subtle.importKey/exportKey'; |
|
0 commit comments