@worker-tools/html
Version:
HTML templating and streaming response library for Worker Runtimes such as Cloudflare Workers.
33 lines (27 loc) • 1.15 kB
text/typescript
import { StreamResponse, BufferedStreamResponse } from '@worker-tools/stream-response';
import { HTML } from './html.js';
const CONTENT_TYPE = 'Content-Type'
/**
* TBD
*/
export class HTMLResponse extends StreamResponse {
static contentType = 'text/html;charset=UTF-8';
constructor(html: HTML, { headers: _headers, ...init }: ResponseInit = {}) {
const headers = new Headers(_headers);
if (!headers.has(CONTENT_TYPE)) headers.set(CONTENT_TYPE, HTMLResponse.contentType);
super(html, { headers, ...init });
}
}
/**
* If for any reason you don't want to use streaming response bodies,
* you can use this class instead, which will buffer the entire body before releasing it to the network.
* Note that headers will still be sent immediately.
*/
export class BufferedHTMLResponse extends BufferedStreamResponse {
static contentType = 'text/html;charset=UTF-8';
constructor(html: HTML, { headers: _headers, ...init }: ResponseInit = {}) {
const headers = new Headers(_headers);
if (!headers.has(CONTENT_TYPE)) headers.set(CONTENT_TYPE, BufferedHTMLResponse.contentType);
super(html, { headers, ...init });
}
}