Spaces:
Paused

File size: 9,193 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { TEXT_ENCODER } from "../../shared/encoding";
import type { ByteChunk, ByteQueue } from "./socket-types";

export function _joinByteChunks(
	chunks: readonly ByteChunk[] | null | undefined,
	totalLength: number,
): ByteChunk {
	if (!chunks?.length) return new Uint8Array(0);
	if (chunks.length === 1) return chunks[0] || new Uint8Array(0);
	const out = new Uint8Array(totalLength);
	let offset = 0;
	for (const chunk of chunks) {
		out.set(chunk, offset);
		offset += chunk.length;
	}
	return out;
}

export function bytesFromBody(body: unknown): ByteChunk | null {
	if (body == null) return null;
	if (typeof body === "string") return TEXT_ENCODER.encode(body);
	if (body instanceof Uint8Array) return body;
	if (body instanceof ArrayBuffer) return new Uint8Array(body);
	if (ArrayBuffer.isView(body))
		return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
	return new Uint8Array(body as ArrayBufferLike);
}

export function createByteQueue(initial?: ByteChunk | null): ByteQueue {
	const chunks: ByteChunk[] = [];
	let headIndex = 0;
	let headOffset = 0;
	let length = 0;
	let scanActive = false;
	let scanChunkIndex = 0;
	let scanOffset = 0;
	let scanBytes = 0;
	let scanPrev = -1;
	let scanMode: "line" | "http-chunk-size" | null = null;
	let scanHttpSize = 0;
	let scanHttpDigits = 0;
	let scanHttpInvalid = false;
	let scanHttpAfterDigitWhitespace = false;
	let scanHttpInExtension = false;
	let scanHttpErrorLine = "";
	if (initial?.length) {
		chunks.push(initial);
		length = initial.length;
	}
	const resetScan = () => {
		scanActive = false;
		scanChunkIndex = headIndex;
		scanOffset = headOffset;
		scanBytes = 0;
		scanPrev = -1;
		scanMode = null;
		scanHttpSize = 0;
		scanHttpDigits = 0;
		scanHttpInvalid = false;
		scanHttpAfterDigitWhitespace = false;
		scanHttpInExtension = false;
		scanHttpErrorLine = "";
	};
	const ensureScan = (mode: "line" | "http-chunk-size") => {
		if (
			scanActive &&
			scanMode === mode &&
			scanChunkIndex >= headIndex &&
			(scanChunkIndex !== headIndex || scanOffset >= headOffset)
		)
			return;
		scanActive = true;
		scanChunkIndex = headIndex;
		scanOffset = headOffset;
		scanBytes = 0;
		scanPrev = -1;
		scanMode = mode;
		scanHttpSize = 0;
		scanHttpDigits = 0;
		scanHttpInvalid = false;
		scanHttpAfterDigitWhitespace = false;
		scanHttpInExtension = false;
		scanHttpErrorLine = "";
	};
	const compact = () => {
		while (headIndex < chunks.length) {
			const first = chunks[headIndex];
			if (!first || headOffset < first.length) break;
			headOffset -= first.length;
			headIndex += 1;
		}
		if (headIndex >= chunks.length) {
			chunks.length = 0;
			headIndex = 0;
			headOffset = 0;
			resetScan();
		} else if (headIndex > 32 && headIndex * 2 >= chunks.length) {
			chunks.splice(0, headIndex);
			if (scanActive) scanChunkIndex = Math.max(0, scanChunkIndex - headIndex);
			headIndex = 0;
		}
	};
	const readByte = (): number => {
		compact();
		if (headIndex >= chunks.length) return -1;
		const first = chunks[headIndex];
		if (!first) return -1;
		const value = first[headOffset];
		if (value === undefined) return -1;
		headOffset += 1;
		length -= 1;
		compact();
		resetScan();
		return value;
	};
	const consumeLine = (
		lineLength: number,
		consumeLength: number,
	): ByteChunk => {
		compact();
		const out = lineLength > 0 ? new Uint8Array(lineLength) : new Uint8Array(0);
		let copied = 0;
		let remaining = consumeLength;
		while (remaining > 0) {
			compact();
			const first = chunks[headIndex];
			if (!first) break;
			const take = Math.min(remaining, first.length - headOffset);
			const copy = Math.min(take, lineLength - copied);
			if (copy > 0) {
				out.set(first.subarray(headOffset, headOffset + copy), copied);
				copied += copy;
			}
			headOffset += take;
			length -= take;
			remaining -= take;
		}
		compact();
		resetScan();
		return out;
	};
	const consumeBytes = (consumeLength: number) => {
		compact();
		let remaining = consumeLength;
		while (remaining > 0) {
			const first = chunks[headIndex];
			if (!first) break;
			const take = Math.min(remaining, first.length - headOffset);
			headOffset += take;
			length -= take;
			remaining -= take;
			if (headOffset >= first.length) {
				headIndex += 1;
				headOffset = 0;
			}
		}
		compact();
		resetScan();
	};
	const scanHttpChunkSizeByte = (b: number) => {
		if (scanHttpInExtension) return;
		if (b === 59) {
			if (scanHttpDigits <= 0 || scanHttpAfterDigitWhitespace)
				scanHttpInvalid = true;
			scanHttpInExtension = true;
			return;
		}
		if (scanHttpErrorLine.length < 80)
			scanHttpErrorLine += b >= 32 && b <= 126 ? String.fromCharCode(b) : "?";
		if (isHttpWhitespaceByte(b)) {
			if (scanHttpDigits > 0) scanHttpAfterDigitWhitespace = true;
			return;
		}
		if (scanHttpAfterDigitWhitespace) {
			scanHttpInvalid = true;
			return;
		}
		const nibble = hexNibbleByte(b);
		if (nibble < 0) {
			scanHttpInvalid = true;
			return;
		}
		scanHttpDigits += 1;
		scanHttpSize = scanHttpSize * 16 + nibble;
		if (!Number.isSafeInteger(scanHttpSize)) scanHttpInvalid = true;
	};
	const api: ByteQueue = {
		get length() {
			return length;
		},
		push(chunk: ByteChunk | null | undefined) {
			if (!chunk?.length) return;
			chunks.push(chunk);
			length += chunk.length;
		},
		read(n: unknown) {
			const count = Math.max(0, Math.min(Number(n) || 0, length));
			if (!count) return new Uint8Array(0);
			compact();
			const first = chunks[headIndex];
			if (first) {
				const available = first.length - headOffset;
				if (count <= available) {
					const out = first.subarray(headOffset, headOffset + count);
					headOffset += count;
					length -= count;
					compact();
					resetScan();
					return out;
				}
			}
			const out = new Uint8Array(count);
			let offset = 0;
			while (offset < count) {
				compact();
				const nextChunk = chunks[headIndex];
				if (!nextChunk) break;
				const take = Math.min(count - offset, nextChunk.length - headOffset);
				out.set(nextChunk.subarray(headOffset, headOffset + take), offset);
				headOffset += take;
				offset += take;
				length -= take;
			}
			compact();
			resetScan();
			return out;
		},
		readLine() {
			const out: number[] = [];
			for (;;) {
				const b = readByte();
				if (b < 0) return null;
				if (b === 13) {
					const next = readByte();
					if (next === 10) return new Uint8Array(out);
					out.push(b);
					if (next >= 0) out.push(next);
					continue;
				}
				out.push(b);
			}
		},
		readLineIfAvailable() {
			compact();
			ensureScan("line");
			for (let c = scanChunkIndex; c < chunks.length; c++) {
				const chunk = chunks[c];
				if (!chunk) continue;
				let start = 0;
				if (c === scanChunkIndex) start = scanOffset;
				else if (c === headIndex) start = headOffset;
				for (let i = start; i < chunk.length; i++) {
					const b = chunk[i];
					if (b === undefined) continue;
					if (scanPrev === 13 && b === 10) {
						return consumeLine(scanBytes - 1, scanBytes + 1);
					}
					scanPrev = b;
					scanBytes += 1;
					scanChunkIndex = c;
					scanOffset = i + 1;
				}
				scanChunkIndex = c + 1;
				scanOffset = 0;
			}
			return null;
		},
		readHttpChunkSizeLineIfAvailable() {
			compact();
			ensureScan("http-chunk-size");
			for (let c = scanChunkIndex; c < chunks.length; c++) {
				const chunk = chunks[c];
				if (!chunk) continue;
				let start = 0;
				if (c === scanChunkIndex) start = scanOffset;
				else if (c === headIndex) start = headOffset;
				for (let i = start; i < chunk.length; i++) {
					const b = chunk[i];
					if (b === undefined) continue;
					if (scanPrev === 13 && b === 10) {
						const result = {
							size: scanHttpInvalid || scanHttpDigits <= 0 ? -1 : scanHttpSize,
							errorLine: trimHttpChunkSizeErrorLine(scanHttpErrorLine),
						};
						consumeBytes(scanBytes + 1);
						return result;
					}
					if (scanPrev >= 0 && !scanHttpInExtension)
						scanHttpChunkSizeByte(scanPrev);
					scanPrev = b;
					scanBytes += 1;
					scanChunkIndex = c;
					scanOffset = i + 1;
				}
				scanChunkIndex = c + 1;
				scanOffset = 0;
			}
			return null;
		},
		skipCRLF() {
			const a = readByte();
			const b = readByte();
			return a === 13 && b === 10;
		},
		drain(controller: ReadableStreamDefaultController<ByteChunk>) {
			compact();
			if (!length) return;
			while (headIndex < chunks.length) {
				const first = chunks[headIndex];
				if (!first) break;
				const out = headOffset ? first.subarray(headOffset) : first;
				if (out.length) controller.enqueue(out);
				headIndex += 1;
				headOffset = 0;
			}
			chunks.length = 0;
			headIndex = 0;
			length = 0;
			resetScan();
		},
	};
	return api;
}

function isHttpWhitespaceByte(value: number | undefined): boolean {
	return value === 32 || value === 9;
}

function hexNibbleByte(value: number | undefined): number {
	if (value === undefined) return -1;
	if (value >= 48 && value <= 57) return value - 48;
	if (value >= 65 && value <= 70) return value - 55;
	if (value >= 97 && value <= 102) return value - 87;
	return -1;
}

function trimHttpChunkSizeErrorLine(value: string): string {
	const semi = value.indexOf(";");
	return (semi >= 0 ? value.slice(0, semi) : value).trim();
}