-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.ts
36 lines (33 loc) · 984 Bytes
/
decoder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
interface Base64DecoderIterator {
[Symbol.iterator](): Base64DecoderIterator;
next(value: string): IteratorResult<Uint8Array, Uint8Array>;
return(): IteratorResult<Uint8Array, Uint8Array>;
}
export function decodeBase64(): Base64DecoderIterator {
let extra = "";
return {
[Symbol.iterator]() {
return this;
},
next(chunk) {
chunk = chunk.replaceAll(/\s/g, "");
chunk = extra + chunk;
const extraLength = chunk.length % 4;
if (extraLength > 0) {
extra = chunk.slice(-extraLength);
chunk = chunk.slice(0, -extraLength);
} else {
extra = "";
}
if (chunk.length > 0) {
return { value: Uint8Array.fromBase64(chunk), done: false };
} else {
return { value: new Uint8Array(), done: false };
}
},
return() {
if (extra == "") return { value: new Uint8Array(), done: true };
return { value: Uint8Array.fromBase64(extra), done: true };
},
};
}