Home / Function/ writeResponse() — astro Function Reference

writeResponse() — astro Function Reference

Architecture documentation for the writeResponse() function in node.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  2dab0a5b_d1f0_3415_f598_d7cd92b794e9["writeResponse()"]
  1354a766_a33e_3a71_462c_58c895a2cb0b["NodeApp"]
  2dab0a5b_d1f0_3415_f598_d7cd92b794e9 -->|defined in| 1354a766_a33e_3a71_462c_58c895a2cb0b
  b7e80edf_e9eb_baf1_e4c5_9b378712743f["getAbortControllerCleanup()"]
  2dab0a5b_d1f0_3415_f598_d7cd92b794e9 -->|calls| b7e80edf_e9eb_baf1_e4c5_9b378712743f
  style 2dab0a5b_d1f0_3415_f598_d7cd92b794e9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/app/node.ts lines 188–239

	static async writeResponse(source: Response, destination: ServerResponse) {
		const { status, headers, body, statusText } = source;
		// HTTP/2 doesn't support statusMessage
		if (!(destination instanceof Http2ServerResponse)) {
			destination.statusMessage = statusText;
		}
		destination.writeHead(status, createOutgoingHttpHeaders(headers));

		const cleanupAbortFromDestination = getAbortControllerCleanup(
			(destination.req as NodeRequest | undefined) ?? undefined,
		);
		if (cleanupAbortFromDestination) {
			const runCleanup = () => {
				cleanupAbortFromDestination();
				if (typeof destination.off === 'function') {
					destination.off('finish', runCleanup);
					destination.off('close', runCleanup);
				} else {
					destination.removeListener?.('finish', runCleanup);
					destination.removeListener?.('close', runCleanup);
				}
			};
			destination.on('finish', runCleanup);
			destination.on('close', runCleanup);
		}
		if (!body) return destination.end();
		try {
			const reader = body.getReader();
			destination.on('close', () => {
				// Cancelling the reader may reject not just because of
				// an error in the ReadableStream's cancel callback, but
				// also because of an error anywhere in the stream.
				reader.cancel().catch((err) => {
					console.error(
						`There was an uncaught error in the middle of the stream while rendering ${destination.req.url}.`,
						err,
					);
				});
			});
			let result = await reader.read();
			while (!result.done) {
				destination.write(result.value);
				result = await reader.read();
			}
			destination.end();
			// the error will be logged by the "on end" callback above
		} catch (err) {
			destination.write('Internal server error', () => {
				err instanceof Error ? destination.destroy(err) : destination.destroy();
			});
		}
	}

Domain

Subdomains

Frequently Asked Questions

What does writeResponse() do?
writeResponse() is a function in the astro codebase, defined in packages/astro/src/core/app/node.ts.
Where is writeResponse() defined?
writeResponse() is defined in packages/astro/src/core/app/node.ts at line 188.
What does writeResponse() call?
writeResponse() calls 1 function(s): getAbortControllerCleanup.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free