-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase64-encoder.ts
More file actions
38 lines (33 loc) · 1.09 KB
/
base64-encoder.ts
File metadata and controls
38 lines (33 loc) · 1.09 KB
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
37
38
import { Buffer } from 'node:buffer'
import { Transform, TransformCallback } from 'node:stream'
export default class Base64Encoder extends Transform {
overflow: Buffer | undefined
_transform(
chunk: Buffer,
encoding: BufferEncoding, // ignored, since it is always buffer
callback: TransformCallback
): void {
if (this.overflow) {
chunk = Buffer.concat([this.overflow, chunk])
this.overflow = undefined
}
// base 64 requires 6 bits (2^6)
// base 256 requires 8 bits (2^8)
// each uint8 is 8 bits, so to make sure we are working on the same number of bits
// every 3 uint8 chars can transform into 4 base64 chars
const overflowSize = chunk.length % 3
if (overflowSize !== 0) {
this.overflow = chunk.subarray(chunk.length - overflowSize)
chunk = chunk.subarray(0, chunk.length - overflowSize)
}
const base64String = chunk.toString('base64')
this.push(base64String)
callback()
}
_flush(callback: TransformCallback): void {
if (this.overflow) {
this.push(this.overflow.toString('base64'))
}
callback()
}
}