1
1
import { Elysia } from 'elysia'
2
- import { Readable } from 'stream'
2
+ import { Readable } from 'node:stream'
3
+ import { renderToStream } from '@kitajs/html/suspense'
4
+
3
5
import { handleHtml } from './handler'
4
6
import { HtmlOptions } from './options'
5
7
import { isHtml } from './utils'
6
- import { renderToStream } from '@kitajs/html/suspense'
7
8
8
9
export function html ( options : HtmlOptions = { } ) {
9
10
// Defaults
@@ -12,7 +13,10 @@ export function html(options: HtmlOptions = {}) {
12
13
options . isHtml ??= isHtml
13
14
options . autoDoctype ??= true
14
15
15
- let instance = new Elysia ( { name : '@elysiajs/html' } ) . derive ( ( { set } ) => {
16
+ const instance = new Elysia ( {
17
+ name : '@elysiajs/html' ,
18
+ seed : options
19
+ } ) . derive ( ( { set } ) => {
16
20
return {
17
21
html (
18
22
value : Readable | JSX . Element
@@ -34,10 +38,8 @@ export function html(options: HtmlOptions = {}) {
34
38
}
35
39
} )
36
40
37
- if ( options . autoDetect ) {
38
- // handlerPossibleHtml should be present on a lot of stack traces, so we should not
39
- // use anonymous functions here.
40
- instance = instance . onAfterHandle ( function handlerPossibleHtml ( {
41
+ if ( options . autoDetect )
42
+ return instance . mapResponse ( async function handlerPossibleHtml ( {
41
43
response : value ,
42
44
set
43
45
} ) {
@@ -47,12 +49,19 @@ export function html(options: HtmlOptions = {}) {
47
49
// @kitajs /html stream
48
50
( value instanceof Readable && 'rid' in value )
49
51
) {
50
- return handleHtml ( value , options , 'content-type' in set . headers )
52
+ const response = await handleHtml (
53
+ value ,
54
+ options ,
55
+ 'content-type' in set . headers
56
+ )
57
+
58
+ if ( response instanceof Response ) return response
59
+
60
+ return new Response ( response )
51
61
}
52
62
53
- return value
63
+ return undefined
54
64
} )
55
- }
56
65
57
66
return instance
58
67
}
0 commit comments