UNPKG

1.15 kBPlain TextView Raw
1import { StreamResponse, BufferedStreamResponse } from '@worker-tools/stream-response';
2import { HTML } from './html.js';
3
4const CONTENT_TYPE = 'Content-Type'
5
6/**
7 * TBD
8 */
9export class HTMLResponse extends StreamResponse {
10 static contentType = 'text/html;charset=UTF-8';
11
12 constructor(html: HTML, { headers: _headers, ...init }: ResponseInit = {}) {
13 const headers = new Headers(_headers);
14 if (!headers.has(CONTENT_TYPE)) headers.set(CONTENT_TYPE, HTMLResponse.contentType);
15 super(html, { headers, ...init });
16 }
17}
18
19/**
20 * If for any reason you don't want to use streaming response bodies,
21 * you can use this class instead, which will buffer the entire body before releasing it to the network.
22 * Note that headers will still be sent immediately.
23 */
24export class BufferedHTMLResponse extends BufferedStreamResponse {
25 static contentType = 'text/html;charset=UTF-8';
26
27 constructor(html: HTML, { headers: _headers, ...init }: ResponseInit = {}) {
28 const headers = new Headers(_headers);
29 if (!headers.has(CONTENT_TYPE)) headers.set(CONTENT_TYPE, BufferedHTMLResponse.contentType);
30 super(html, { headers, ...init });
31 }
32}