@worker-tools/html
Version:
HTML templating and streaming response library for Worker Runtimes such as Cloudflare Workers.
39 lines • 1.4 kB
JavaScript
import { StreamResponse, BufferedStreamResponse } from '@worker-tools/stream-response';
const CONTENT_TYPE = 'Content-Type';
/**
* TBD
*/
export class HTMLResponse extends StreamResponse {
constructor(html, { headers: _headers, ...init } = {}) {
const headers = new Headers(_headers);
if (!headers.has(CONTENT_TYPE))
headers.set(CONTENT_TYPE, HTMLResponse.contentType);
super(html, { headers, ...init });
}
}
Object.defineProperty(HTMLResponse, "contentType", {
enumerable: true,
configurable: true,
writable: true,
value: 'text/html;charset=UTF-8'
});
/**
* 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 {
constructor(html, { headers: _headers, ...init } = {}) {
const headers = new Headers(_headers);
if (!headers.has(CONTENT_TYPE))
headers.set(CONTENT_TYPE, BufferedHTMLResponse.contentType);
super(html, { headers, ...init });
}
}
Object.defineProperty(BufferedHTMLResponse, "contentType", {
enumerable: true,
configurable: true,
writable: true,
value: 'text/html;charset=UTF-8'
});
//# sourceMappingURL=html-response.js.map