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 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
x4
x4
x4
x4
x4
x4
 
 
 
x4
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
 
 
 
 
 
 
 
x4
x11
x18
x27
x11
x11
x11
x22
x22
x22
x22
x11
x11
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
 
 
 
 
 
 
 
 
x4
x7
x10
x13
x7
x7
x7
x7
x7
x20
x20
x26
x26
x26
x20
x20
x20
x20
x20
x20
x20
 
x20
x7
x7
 
 
 
 
x10
x7
x7
x4 |
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
/**
* TransformStream classes to encode and decode to and from hexadecimal data in a streaming manner.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeHex } from "@std/encoding/unstable-hex";
* import { HexEncoderStream } from "@std/encoding/unstable-hex-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new HexEncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeHex(await Deno.readFile("./deno.lock")),
* );
* ```
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };
import {
alphabet,
calcSizeHex,
decode,
encode,
rAlphabet,
} from "./_common16.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 hexadecimal stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the hexadecimal stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeHex } from "@std/encoding/unstable-hex";
* import { HexEncoderStream } from "@std/encoding/unstable-hex-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new HexEncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeHex(await Deno.readFile("./deno.lock")),
* );
* ```
*/
export class HexEncoderStream<T extends "string" | "bytes">
extends TransformStream<
Uint8Array_,
T extends "bytes" ? Uint8Array_ : string
> {
/**
* Constructs a new instance.
*
* @param options The options for the hexadecimal stream.
*/
constructor(options: { output?: T } = {}) {
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>;
}();
super({
transform(chunk, controller) {
const [output, i] = detach(chunk, calcSizeHex(chunk.length));
encode(output, i, 0, alphabet);
controller.enqueue(decode(output));
},
});
}
}
/**
* Transforms a hexadecimal stream into a {@link Uint8Array<ArrayBuffer>} stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the hexadecimal stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import {
* HexDecoderStream,
* HexEncoderStream,
* } from "@std/encoding/unstable-hex-stream";
* import { toBytes } from "@std/streams/unstable-to-bytes";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new HexEncoderStream({ output: "bytes" }))
* .pipeThrough(new HexDecoderStream({ input: "bytes" }));
*
* assertEquals(
* await toBytes(readable),
* await Deno.readFile("./deno.lock"),
* );
* ```
*/
export class HexDecoderStream<T extends "string" | "bytes">
extends TransformStream<
T extends "bytes" ? Uint8Array_ : string,
Uint8Array_
> {
/**
* Constructs a new instance.
*
* @param options The options of the hexadecimal stream.
*/
constructor(options: { input?: T } = {}) {
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(1);
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 % 2;
if (remainder) push.set(output.subarray(-remainder));
const o = decode(
output.subarray(0, -remainder || undefined),
0,
0,
rAlphabet,
);
controller.enqueue(output.subarray(0, o));
},
flush(controller) {
if (remainder) {
const o = decode(push.subarray(0, remainder), 0, 0, rAlphabet);
controller.enqueue(push.subarray(0, o));
}
},
});
}
}
|