Skip to content

Commit 371921b

Browse files
committed
🎉 feat: 0.8
1 parent c41363d commit 371921b

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/html",
3-
"version": "0.8.0-rc.0",
3+
"version": "0.8.0-rc.1",
44
"description": "Plugin for Elysia that add support for returning html",
55
"author": {
66
"name": "saltyAom",

src/html.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Elysia } from 'elysia'
2-
import { Readable } from 'stream'
2+
import { Readable } from 'node:stream'
3+
import { renderToStream } from '@kitajs/html/suspense'
4+
35
import { handleHtml } from './handler'
46
import { HtmlOptions } from './options'
57
import { isHtml } from './utils'
6-
import { renderToStream } from '@kitajs/html/suspense'
78

89
export function html(options: HtmlOptions = {}) {
910
// Defaults
@@ -12,7 +13,10 @@ export function html(options: HtmlOptions = {}) {
1213
options.isHtml ??= isHtml
1314
options.autoDoctype ??= true
1415

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 }) => {
1620
return {
1721
html(
1822
value: Readable | JSX.Element
@@ -34,10 +38,8 @@ export function html(options: HtmlOptions = {}) {
3438
}
3539
})
3640

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({
4143
response: value,
4244
set
4345
}) {
@@ -47,12 +49,19 @@ export function html(options: HtmlOptions = {}) {
4749
// @kitajs/html stream
4850
(value instanceof Readable && 'rid' in value)
4951
) {
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)
5161
}
5262

53-
return value
63+
return undefined
5464
})
55-
}
5665

5766
return instance
5867
}

0 commit comments

Comments
 (0)