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 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
x4
x4
x4
x4
x4
x4
x4
 
 
 
x4
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
 
 
 
 
 
 
 
x4
x19
x19
x34
x43
x19
x19
x19
x19
x19
x46
x46
x46
 
x46
x58
x58
x58
x46
x46
 
 
 
 
 
 
 
x46
x19
x19
x34
x34
x34
x34
 
x34
x34
x34
x34
x19
x19
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
 
 
 
 
 
 
 
x4
x11
x11
x18
x22
x11
x11
x11
x11
x11
x43
x43
x67
x67
x67
x43
x43
x43
x43
x43
x43
x43
x43
 
x43
x11
x11
 
 
 
 
 
 
 
 
 
 
x18
x11
x11
x4 |
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
/**
* TransformStream classes to encode and decode to and from base32 data in a streaming manner.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeBase32 } from "@std/encoding/unstable-base32";
* import { Base32EncoderStream } from "@std/encoding/unstable-base32-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new Base32EncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeBase32(await Deno.readFile("./deno.lock"), { alphabet: "base32" }),
* );
* ```
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };
import {
alphabet,
type Base32Alphabet,
calcSizeBase32,
decode,
encode,
padding,
rAlphabet,
} from "./_common32.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 base32 stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the base32 stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeBase32 } from "@std/encoding/unstable-base32";
* import { Base32EncoderStream } from "@std/encoding/unstable-base32-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new Base32EncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeBase32(await Deno.readFile("./deno.lock"), { alphabet: "base32" }),
* );
* ```
*/
export class Base32EncoderStream<T extends "string" | "bytes">
extends TransformStream<
Uint8Array_,
T extends "bytes" ? Uint8Array_ : string
> {
/**
* Constructs a new instance.
*
* @param options The options of the base32 stream.
*/
constructor(options: { alphabet?: Base32Alphabet; output?: T } = {}) {
const abc = alphabet[options.alphabet ?? "base32"];
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(4);
let remainder = 0;
super({
transform(chunk, controller) {
let [output, i] = detach(
chunk,
calcSizeBase32(remainder + chunk.length),
);
if (remainder) {
i -= remainder;
output.set(push.subarray(0, remainder), i);
}
remainder = (output.length - i) % 5;
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),
calcSizeBase32(remainder),
);
encode(output, i, 0, abc, padding);
controller.enqueue(decode(output));
}
},
});
}
}
/**
* Transforms a base32 stream into a {@link Uint8Array<ArrayBuffer>} stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the base32 stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import {
* Base32DecoderStream,
* Base32EncoderStream,
* } from "@std/encoding/unstable-base32-stream";
* import { toBytes } from "@std/streams/unstable-to-bytes";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new Base32EncoderStream({ output: "string" }))
* .pipeThrough(new Base32DecoderStream({ input: "string" }));
*
* assertEquals(
* await toBytes(readable),
* await Deno.readFile("./deno.lock"),
* );
* ```
*
* @module
*/
export class Base32DecoderStream<T extends "string" | "bytes">
extends TransformStream<
T extends "bytes" ? Uint8Array_ : string,
Uint8Array_
> {
/**
* Constructs a new instance.
*
* @param options The options of the base32 stream.
*/
constructor(options: { alphabet?: Base32Alphabet; input?: T } = {}) {
const abc = rAlphabet[options.alphabet ?? "base32"];
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(7);
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 % 8;
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));
}
},
});
}
}
|