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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
x4
x4
x4
x4
x4
x4
x4
 
 
 
x4
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
 
 
 
 
 
 
 
x4
x15
x15
x26
x33
x15
x15
x15
x15
x15
x34
x34
x34
 
x34
x42
x42
x42
x34
x34
 
 
 
 
 
 
 
x34
x15
x15
x26
x26
x26
x26
 
x26
x26
x30
x30
x30
x26
x26
x26
x15
x15
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
 
 
 
 
 
 
 
x4
x9
x9
x14
x17
x9
x9
x9
x9
x9
x27
x27
x39
x39
x39
x27
x27
x27
x27
x27
x27
x27
x27
 
x27
x9
x9
x14
x16
x16
x16
x16
x16
x16
 
x16
x16
x14
x9
x9
x4 |
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
/**
* TransformStream classes to encode and decode to and from base64 data in a streaming manner.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeBase64 } from "@std/encoding/unstable-base64";
* import { Base64EncoderStream } from "@std/encoding/unstable-base64-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new Base64EncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeBase64(await Deno.readFile("./deno.lock"), { alphabet: "base64" }),
* );
* ```
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };
import {
alphabet,
type Base64Alphabet,
calcSizeBase64,
decode,
encode,
padding,
rAlphabet,
} from "./_common64.ts";
import { detach } from "./_common_detach.ts";
type Expect<T> = T extends "bytes" ? Uint8Array_ : string;
const encoder = new TextEncoder();
const decoder = new TextDecoder();
/**
* Transforms a {@linkcode Uint8Array<ArrayBuffer>} stream into a base64 stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the base64 stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeBase64 } from "@std/encoding/unstable-base64";
* import { Base64EncoderStream } from "@std/encoding/unstable-base64-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new Base64EncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeBase64(await Deno.readFile("./deno.lock")),
* );
* ```
*/
export class Base64EncoderStream<T extends "string" | "bytes">
extends TransformStream<
Uint8Array_,
T extends "bytes" ? Uint8Array_ : string
> {
/**
* Constructs a new instance.
*
* @param options The options for the base64 stream.
*/
constructor(options: { alphabet?: Base64Alphabet; output?: T } = {}) {
const abc = alphabet[options.alphabet ?? "base64"];
const decode = function (): (input: Uint8Array_) => Expect<T> {
if (options.output === "bytes") return (x) => x as Expect<T>;
return (x) => decoder.decode(x) as Expect<T>;
}();
const push = new Uint8Array(2);
let remainder = 0;
super({
transform(chunk, controller) {
let [output, i] = detach(
chunk,
calcSizeBase64(remainder + chunk.length),
);
if (remainder) {
i -= remainder;
output.set(push.subarray(0, remainder), i);
}
remainder = (output.length - i) % 3;
if (remainder) push.set(output.subarray(-remainder));
const o = encode(
output.subarray(0, -remainder || undefined),
i,
0,
abc,
padding,
);
controller.enqueue(decode(output.subarray(0, o)));
},
flush(controller) {
if (remainder) {
const [output, i] = detach(
push.subarray(0, remainder),
calcSizeBase64(remainder),
);
let o = encode(output, i, 0, abc, padding);
if (options.alphabet === "base64url") {
const i = output.indexOf(padding, o - 2);
if (i > 0) o = i;
}
controller.enqueue(decode(output.subarray(0, o)));
}
},
});
}
}
/**
* Transforms a base64 stream into a {@link Uint8Array<ArrayBuffer>} stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the base64 stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import {
* Base64DecoderStream,
* Base64EncoderStream,
* } from "@std/encoding/unstable-base64-stream";
* import { toBytes } from "@std/streams/unstable-to-bytes";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new Base64EncoderStream({ output: "string" }))
* .pipeThrough(new Base64DecoderStream({ input: "string" }));
*
* assertEquals(
* await toBytes(readable),
* await Deno.readFile("./deno.lock"),
* );
* ```
*/
export class Base64DecoderStream<T extends "string" | "bytes">
extends TransformStream<
T extends "bytes" ? Uint8Array_ : string,
Uint8Array_
> {
/**
* Constructs a new instance.
*
* @param options The options for the base64 stream.
*/
constructor(options: { alphabet?: Base64Alphabet; input?: T } = {}) {
const abc = rAlphabet[options.alphabet ?? "base64"];
const encode = function (): (input: Expect<T>) => Uint8Array_ {
if (options.input === "bytes") return (x) => x as Uint8Array_;
return (x) => encoder.encode(x as string) as Uint8Array_;
}();
const push = new Uint8Array(3);
let remainder = 0;
super({
transform(chunk, controller) {
let output = encode(chunk);
if (remainder) {
output = detach(output, remainder + output.length)[0];
output.set(push.subarray(0, remainder));
}
remainder = output.length % 4;
if (remainder) push.set(output.subarray(-remainder));
const o = decode(
output.subarray(0, -remainder || undefined),
0,
0,
abc,
padding,
);
controller.enqueue(output.subarray(0, o));
},
flush(controller) {
if (remainder) {
const o = decode(
push.subarray(0, remainder),
0,
0,
abc,
padding,
);
controller.enqueue(push.subarray(0, o));
}
},
});
}
}
|