-
Notifications
You must be signed in to change notification settings - Fork 52
/
html-stream.ts
155 lines (134 loc) · 4.15 KB
/
html-stream.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
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
import { htmlEncode } from '@cfworker/web';
export function html(
strings: TemplateStringsArray,
...values: any[]
): ReadableStream {
let cancelled = false;
const { readable, writable } = new TransformStream();
(async () => {
const writer = writable.getWriter();
const encoder = new TextEncoder();
const cancellationToken = {
get cancelled() {
return cancelled;
}
};
let i = 0;
for (; i < values.length && !cancelled; i++) {
await writer.write(encoder.encode(strings[i]));
await pushValue(values[i], writer, encoder, cancellationToken);
}
if (!cancelled) {
await writer.write(encoder.encode(strings[i]));
}
await writer.close();
})();
return readable;
}
interface CancellationToken {
readonly cancelled: boolean;
}
type Writer = WritableStreamDefaultWriter;
async function pushValue(
value: any,
writer: Writer,
encoder: TextEncoder,
cancellationToken: CancellationToken
): Promise<void> {
if (value === null || value === undefined || cancellationToken.cancelled) {
return;
}
if (value instanceof Promise) {
return pushValue(await value, writer, encoder, cancellationToken);
}
if (value instanceof ReadableStream) {
return pushStream(value, writer, cancellationToken);
}
if (Array.isArray(value)) {
return pushArray(value, writer, encoder, cancellationToken);
}
await writer.write(encoder.encode(htmlEncode(value.toString())));
}
async function pushStream(
stream: ReadableStream,
writer: Writer,
cancellationToken: CancellationToken
) {
const reader = stream.getReader();
do {
const { value, done } = await reader.read();
if (done || cancellationToken.cancelled) {
reader.releaseLock();
return;
}
writer.write(value!);
} while (true);
}
async function pushArray(
value: any[],
writer: Writer,
encoder: TextEncoder,
cancellationToken: CancellationToken
) {
for (let i = 0; i < value.length && !cancellationToken.cancelled; i++) {
await pushValue(value[i], writer, encoder, cancellationToken);
}
}
/*
export function html(strings: TemplateStringsArray, ...values: any[]): ReadableStream<Uint8Array> {
let cancelled = false;
return new ReadableStream<Uint8Array>({
async start(controller) {
const encoder = new TextEncoder();
const cancellationToken = { get cancelled() { return cancelled; } };
let i = 0;
for (; i < values.length && !cancelled; i++) {
controller.enqueue(encoder.encode(strings[i]));
await pushValue(values[i], controller, encoder, cancellationToken);
}
if (!cancelled) {
controller.enqueue(encoder.encode(strings[i]));
controller.close();
}
},
cancel() {
cancelled = true;
}
});
}
interface CancellationToken {
readonly cancelled: boolean;
}
type StreamController = Pick<ReadableStreamDefaultController<Uint8Array>, 'enqueue'>;
async function pushValue(value: any, controller: StreamController, encoder: TextEncoder, cancellationToken: CancellationToken): Promise<void> {
if (value === null || value === undefined || cancellationToken.cancelled) {
return;
}
if (value instanceof Promise) {
return pushValue(await value, controller, encoder, cancellationToken);
}
if (value instanceof ReadableStream) {
return pushStream(value, controller, cancellationToken);
}
if (Array.isArray(value)) {
return pushArray(value, controller, encoder, cancellationToken);
}
controller.enqueue(encoder.encode(htmlEncode(value.toString())));
}
async function pushStream(stream: ReadableStream<Uint8Array>, controller: StreamController, cancellationToken: CancellationToken) {
const reader = stream.getReader();
do {
const { value, done } = await reader.read();
if (done || cancellationToken.cancelled) {
reader.releaseLock();
return;
}
controller.enqueue(value);
} while (true);
}
async function pushArray(value: any[], controller: StreamController, encoder: TextEncoder, cancellationToken: CancellationToken) {
for (let i = 0; i < value.length && !cancellationToken.cancelled; i++) {
await pushValue(value[i], controller, encoder, cancellationToken);
}
}
*/