response.ts — astro Source File
Architecture documentation for response.ts, a typescript file in the astro codebase. 8 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 4611d913_9978_903f_46fc_e5961ccf4da7["response.ts"] 34cf60ae_d0fa_22a9_892d_8834050ac3c1["../core/cookies/index.js"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> 34cf60ae_d0fa_22a9_892d_8834050ac3c1 0e18005f_823b_9864_a584_bf040da30d0c["../core/errors/dev/index.js"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> 0e18005f_823b_9864_a584_bf040da30d0c ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9 32f8c7d4_d66e_e0cf_b019_46ec3f2fea31["../core/module-loader/index.js"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> 32f8c7d4_d66e_e0cf_b019_46ec3f2fea31 c76397e6_4b28_33b8_0963_02213119b998["../core/routing/3xx.js"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> c76397e6_4b28_33b8_0963_02213119b998 c2f6615e_96e9_c4eb_5f71_cf120e271705["node:http"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> c2f6615e_96e9_c4eb_5f71_cf120e271705 214989f1_72ea_fe85_4487_8b170a9188c5["node:http2"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> 214989f1_72ea_fe85_4487_8b170a9188c5 8f34f3e8_2f0f_1f0d_91dc_c25ece8c8169["node:stream"] 4611d913_9978_903f_46fc_e5961ccf4da7 --> 8f34f3e8_2f0f_1f0d_91dc_c25ece8c8169 style 4611d913_9978_903f_46fc_e5961ccf4da7 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type http from 'node:http';
import { Http2ServerResponse } from 'node:http2';
import { Readable } from 'node:stream';
import { getSetCookiesFromResponse } from '../core/cookies/index.js';
import { getViteErrorPayload } from '../core/errors/dev/index.js';
import type { ErrorWithMetadata } from '../core/errors/index.js';
import type { ModuleLoader } from '../core/module-loader/index.js';
import { redirectTemplate } from '../core/routing/3xx.js';
export async function handle500Response(
loader: ModuleLoader,
res: http.ServerResponse,
err: ErrorWithMetadata,
) {
res.on('close', async () =>
setTimeout(async () => loader.webSocketSend(await getViteErrorPayload(err)), 200),
);
if (res.headersSent) {
res.write(`<script type="module" src="/@vite/client"></script>`);
res.end();
} else {
writeHtmlResponse(
res,
500,
`<title>${err.name}</title><script type="module" src="/@vite/client"></script>`,
);
}
}
export function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: string) {
res.writeHead(statusCode, {
'Content-Type': 'text/html',
'Content-Length': Buffer.byteLength(html, 'utf-8'),
});
res.write(html);
res.end();
}
export function writeRedirectResponse(
res: http.ServerResponse,
statusCode: number,
location: string,
) {
const html = redirectTemplate({
status: statusCode,
absoluteLocation: location,
relativeLocation: location,
});
res.writeHead(statusCode, {
Location: location,
'Content-Type': 'text/html',
'Content-Length': Buffer.byteLength(html, 'utf-8'),
});
res.write(html);
res.end();
}
async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
const { status, headers, body, statusText } = webResponse;
// Attach any set-cookie headers added via Astro.cookies.set()
const setCookiesFromResponse = Array.from(getSetCookiesFromResponse(webResponse));
const setCookieHeaders = [...setCookiesFromResponse, ...headers.getSetCookie()];
const _headers: http.OutgoingHttpHeaders = Object.fromEntries(headers.entries());
if (setCookieHeaders.length) {
_headers['set-cookie'] = setCookieHeaders;
}
// HTTP/2 doesn't support statusMessage
if (!(res instanceof Http2ServerResponse)) {
res.statusMessage = statusText;
}
res.writeHead(status, _headers);
if (body) {
if (Symbol.for('astro.responseBody') in webResponse) {
let stream = (webResponse as any)[Symbol.for('astro.responseBody')];
for await (const chunk of stream) {
res.write(chunk.toString());
}
} else if (body instanceof Readable) {
body.pipe(res);
return;
} else if (typeof body === 'string') {
res.write(body);
} else {
const reader = body.getReader();
res.on('close', () => {
reader.cancel().catch(() => {
// Don't log here, or errors will get logged twice in most cases
});
});
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
res.write(value);
}
}
}
}
res.end();
}
export async function writeSSRResult(
webRequest: Request,
webResponse: Response,
res: http.ServerResponse,
) {
Reflect.set(webRequest, Symbol.for('astro.responseSent'), true);
return writeWebResponse(res, webResponse);
}
Domain
Subdomains
Functions
Dependencies
- ../core/cookies/index.js
- ../core/errors/dev/index.js
- ../core/errors/index.js
- ../core/module-loader/index.js
- ../core/routing/3xx.js
- node:http
- node:http2
- node:stream
Source
Frequently Asked Questions
What does response.ts do?
response.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in response.ts?
response.ts defines 5 function(s): handle500Response, writeHtmlResponse, writeRedirectResponse, writeSSRResult, writeWebResponse.
What does response.ts depend on?
response.ts imports 8 module(s): ../core/cookies/index.js, ../core/errors/dev/index.js, ../core/errors/index.js, ../core/module-loader/index.js, ../core/routing/3xx.js, node:http, node:http2, node:stream.
Where is response.ts in the architecture?
response.ts is located at packages/astro/src/vite-plugin-astro-server/response.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-astro-server).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free