Repository: withastro/astro
File: packages/astro/src/runtime/server/render/astro/render.ts (TypeScript)

Code before:
			}

			// If an error occurs during rendering, throw the error as we cannot proceed.
			if (error) {
				throw error;
			}

			// Get the total length of all arrays.
			let length = 0;
			for (let i = 0, len = buffer.length; i < len; i++) {
				length += buffer[i].length;
			}

			// Create a new array with total length and merge all source arrays.
			let mergedArray = new Uint8Array(length);
			let offset = 0;
			for (let i = 0, len = buffer.length; i < len; i++) {
				const item = buffer[i];
				mergedArray.set(item, offset);
				offset += item.length;
			}

			// Empty the array. We do this so that we can reuse the same array.
			buffer.length = 0;

			const returnValue = {
				// The iterator is done when rendering has finished
				// and there are no more chunks to return.
				done: length === 0 && renderingComplete,
				value: mergedArray,
			};

			return returnValue;
		},
		async return() {
			// If the client disconnects,
			// we signal to the rest of the internals to ignore the results of existing renders and avoid kicking off more of them.
			result.cancelled = true;
			return { done: true, value: undefined };
		},
	};

	const destination: RenderDestination = {
		write(chunk) {
			if (isPage && !renderedFirstPageChunk) {
				renderedFirstPageChunk = true;
				if (!result.partial && !DOCTYPE_EXP.test(String(chunk))) {
					const doctype = result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n';
					buffer.push(encoder.encode(doctype));
				}
			}

Review comment (refactor):
Rather than checking if next is string, why don't we just store a separate array for string values and just check that in general? This would simplify this implementation and we don't have to store string | buffer in this array.

Code after:
			}

			// Only create a new promise if rendering is still ongoing. Otherwise
			// there will be a dangling promises that breaks tests (probably not an actual app)
			if (!renderingComplete) {
				next = promiseWithResolvers();
			}

			// If an error occurs during rendering, throw the error as we cannot proceed.
			if (error) {
				throw error;
			}

			// This calculates the length of the final merged array.
			// While doing so, it also replaces consecutive strings with their
			// concatenated Uint8Array equivalent.
			// This is a performance optimization since `TextEncoder#encode` can be
			// costly, so it is faster to encode one larger string than it is
			// to encode many smaller strings.
			let length = 0;
			let stringToEncode = '';
			for (let i = 0, len = buffer.length; i < len; i++) {
				const bufferEntry = buffer[i];

				if (typeof bufferEntry === 'string') {
					const nextIsString = i + 1 < len && typeof buffer[i + 1] === 'string';
					stringToEncode += bufferEntry;
					if (!nextIsString) {
						const encoded = encoder.encode(stringToEncode);
						length += encoded.length;
						stringToEncode = '';
						buffer[i] = encoded;
					} else {
						buffer[i] = '';
					}
				} else {
					length += bufferEntry.length;
				}
			}

			// Create a new array with total length and merge all source arrays.
			let mergedArray = new Uint8Array(length);
			let offset = 0;
			for (let i = 0, len = buffer.length; i < len; i++) {
				const item = buffer[i];
				// If an item is an empty string, it must've been cleared out earlier
				// when we converted it into a larger Uint8Array. Thus, we can skip it.
				if (item === '') {
					continue;
				}
				// TypeScript will think this is `string | Uint8Array` but, because of