-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhtml-response.ts
32 lines (27 loc) · 1.15 KB
/
html-response.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { StreamResponse, BufferedStreamResponse } from 'https://ghuc.cc/worker-tools/stream-response/index.ts';
import { HTML } from './html.ts';
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 });
}
}