acrylblends 0.6.1 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -6
- package/docs/framework/README.md +3 -2
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/api/presets.md +0 -3
- package/docs/framework/cordis/acryl-in-a-cordis-app.md +9 -0
- package/docs/framework/getting-started/basic.md +5 -4
- package/docs/framework/getting-started/desktop.md +41 -0
- package/docs/framework/guides/jsx-dom.md +0 -3
- package/docs/framework/guides/jsx.md +1 -4
- package/docs/framework/guides/middleware.md +1 -1
- package/docs/framework/helpers/cookie.md +1 -1
- package/docs/framework/helpers/css.md +0 -3
- package/docs/framework/helpers/dev.md +0 -3
- package/docs/framework/helpers/jwt.md +1 -1
- package/docs/framework/helpers/proxy.md +0 -3
- package/docs/framework/middleware/builtin/basic-auth.md +6 -6
- package/docs/framework/middleware/builtin/cache.md +0 -3
- package/docs/framework/middleware/builtin/combine.md +1 -1
- package/docs/framework/middleware/builtin/ip-restriction.md +1 -1
- package/docs/framework/middleware/builtin/logger.md +1 -1
- package/docs/framework/middleware/builtin/mount.md +0 -3
- package/docs/framework/upstream.json +3 -3
- package/lib/accepts.d.ts +1 -1
- package/lib/acryl-base.d.ts +1 -1
- package/lib/adapter.d.ts +1 -1
- package/lib/{app-CmAYW1Al.d.ts → app-wFwpg-bt.d.ts} +1 -1
- package/lib/base-BpQy2Rk7.d.ts +783 -0
- package/lib/base-ddiVmiE7.js +721 -0
- package/lib/basic-auth.d.ts +1 -1
- package/lib/bearer-auth.d.ts +1 -1
- package/lib/body-limit.d.ts +1 -1
- package/lib/cache.d.ts +43 -0
- package/lib/cache.js +169 -0
- package/lib/client.d.ts +2 -2
- package/lib/combine.d.ts +1 -1
- package/lib/common-D8Z4JRCw.d.ts +46 -0
- package/lib/components-Ce5FRyHU.js +268 -0
- package/lib/components-DYZdxJIJ.js +24 -0
- package/lib/compress.d.ts +1 -1
- package/lib/conninfo.d.ts +2 -2
- package/lib/constants-BR9jM9BC.js +9 -0
- package/lib/{context-Bm7rI-oK.d.ts → context-DVQBjuns.d.ts} +1 -1
- package/lib/context-DxFdY_un.d.ts +250 -0
- package/lib/context-storage.d.ts +1 -1
- package/lib/{cookie-ClMaKDQf.d.ts → cookie-CCelEqky.d.ts} +1 -1
- package/lib/cookie.d.ts +3 -3
- package/lib/cors.d.ts +1 -1
- package/lib/csrf.d.ts +1 -1
- package/lib/css-D2jb7X8b.js +320 -0
- package/lib/css.d.ts +82 -0
- package/lib/css.js +134 -0
- package/lib/dev.d.ts +19 -0
- package/lib/dev.js +50 -0
- package/lib/etag.d.ts +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/hooks-Bq5q9IMl.js +762 -0
- package/lib/html-SllAe0YT.d.ts +46 -0
- package/lib/html.d.ts +1 -1
- package/lib/index-C850MeBA.d.ts +94 -0
- package/lib/{index-oOClm_Xk.d.ts → index-CUTfU69R.d.ts} +3 -3
- package/lib/{index-B-3F4wt2.d.ts → index-Cqcdluyn.d.ts} +2 -2
- package/lib/{index-UTBmM3GX.d.ts → index-CsvB-F9j.d.ts} +2 -2
- package/lib/index.d.ts +4 -4
- package/lib/ip-restriction.d.ts +2 -2
- package/lib/jsx/dom/client.d.ts +30 -0
- package/lib/jsx/dom/client.js +49 -0
- package/lib/jsx/dom/css.d.ts +84 -0
- package/lib/jsx/dom/css.js +2 -0
- package/lib/jsx/dom/jsx-dev-runtime.d.ts +3 -0
- package/lib/jsx/dom/jsx-dev-runtime.js +2 -0
- package/lib/jsx/dom/jsx-runtime.d.ts +3 -0
- package/lib/jsx/dom/jsx-runtime.js +2 -0
- package/lib/jsx/dom/server.d.ts +105 -0
- package/lib/jsx/dom/server.js +37 -0
- package/lib/jsx/dom.d.ts +5 -0
- package/lib/jsx/dom.js +74 -0
- package/lib/jsx/jsx-dev-runtime.d.ts +6 -0
- package/lib/jsx/jsx-dev-runtime.js +14 -0
- package/lib/jsx/jsx-runtime.d.ts +10 -0
- package/lib/jsx/jsx-runtime.js +30 -0
- package/lib/jsx/streaming.d.ts +2 -0
- package/lib/jsx/streaming.js +2 -0
- package/lib/jsx-dev-runtime-B22O-Npk.d.ts +7 -0
- package/lib/jsx-dev-runtime-kAhR_4C7.js +354 -0
- package/lib/jsx-runtime-BVVgDSdq.d.ts +1 -0
- package/lib/jsx.d.ts +3 -0
- package/lib/jsx.js +47 -0
- package/lib/jwk.d.ts +4 -4
- package/lib/{jwt-Dg493Tai.d.ts → jwt-BARhotDz.d.ts} +1 -1
- package/lib/jwt.d.ts +4 -4
- package/lib/language.d.ts +1 -1
- package/lib/linear-router-C3FPib6h.js +96 -0
- package/lib/logger.d.ts +1 -1
- package/lib/loopback-only.d.ts +1 -1
- package/lib/method-not-allowed.d.ts +2 -2
- package/lib/method-override.d.ts +2 -2
- package/lib/mount.d.ts +44 -0
- package/lib/mount.js +67 -0
- package/lib/node-server/conninfo.d.ts +1 -1
- package/lib/node-server/serve-static.d.ts +1 -1
- package/lib/node-server/serve-static.js +1 -1
- package/lib/node-server.d.ts +1 -1
- package/lib/node-server.js +1 -1
- package/lib/pattern-router-HZxjDGEQ.js +41 -0
- package/lib/powered-by.d.ts +1 -1
- package/lib/pretty-json.d.ts +1 -1
- package/lib/proxy.d.ts +72 -0
- package/lib/proxy.js +121 -0
- package/lib/quick.d.ts +8 -0
- package/lib/quick.js +17 -0
- package/lib/request-id.d.ts +1 -1
- package/lib/request.d.ts +1 -1
- package/lib/route.d.ts +1 -1
- package/lib/router/linear-router.js +1 -95
- package/lib/router/pattern-router.js +1 -40
- package/lib/schema-validator.d.ts +1 -1
- package/lib/secure-headers.d.ts +1 -1
- package/lib/{serve-static-Dw_m6KxR.js → serve-static-Cw23shXv.js} +1 -0
- package/lib/serve-static.d.ts +1 -1
- package/lib/streaming.d.ts +1 -1
- package/lib/testing.d.ts +4 -4
- package/lib/timeout.d.ts +1 -1
- package/lib/timing.d.ts +1 -1
- package/lib/tiny.d.ts +8 -0
- package/lib/tiny.js +15 -0
- package/lib/trailing-slash.d.ts +1 -1
- package/lib/{types-CHesD1KG.d.ts → types-CQiGmsWW.d.ts} +3 -3
- package/lib/{types-CwrQjlaM.d.ts → types-Lqx5BxQF.d.ts} +10 -1
- package/lib/{types-CDO6I3B0.d.ts → types-nEL8VPDC.d.ts} +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/utils/body.d.ts +1 -1
- package/lib/utils/cookie.d.ts +1 -1
- package/lib/utils/crypto.d.ts +1 -1
- package/lib/utils/html.d.ts +1 -45
- package/lib/utils/ipaddr.d.ts +1 -1
- package/lib/utils/jwt.d.ts +1 -1
- package/lib/utils/types.d.ts +2 -2
- package/lib/validator.d.ts +2 -2
- package/lib/ws.d.ts +1 -1
- package/package.json +69 -1
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/upstream.json +1 -1
- package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +506 -0
- package/plugins/acryl-router/src/core/helper/css/common.ts +337 -0
- package/plugins/acryl-router/src/core/helper/css/index.test.tsx +678 -0
- package/plugins/acryl-router/src/core/helper/css/index.ts +262 -0
- package/plugins/acryl-router/src/core/helper/dev/index.test.ts +181 -0
- package/plugins/acryl-router/src/core/helper/dev/index.ts +79 -0
- package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +356 -0
- package/plugins/acryl-router/src/core/helper/proxy/index.ts +203 -0
- package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +557 -0
- package/plugins/acryl-router/src/core/jsx/base.test.tsx +97 -0
- package/plugins/acryl-router/src/core/jsx/base.ts +490 -0
- package/plugins/acryl-router/src/core/jsx/children.test.ts +52 -0
- package/plugins/acryl-router/src/core/jsx/children.ts +20 -0
- package/plugins/acryl-router/src/core/jsx/components.test.tsx +936 -0
- package/plugins/acryl-router/src/core/jsx/components.ts +257 -0
- package/plugins/acryl-router/src/core/jsx/constants.ts +6 -0
- package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +655 -0
- package/plugins/acryl-router/src/core/jsx/context.ts +266 -0
- package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +128 -0
- package/plugins/acryl-router/src/core/jsx/dom/client.ts +89 -0
- package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +330 -0
- package/plugins/acryl-router/src/core/jsx/dom/components.ts +39 -0
- package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +184 -0
- package/plugins/acryl-router/src/core/jsx/dom/context.ts +52 -0
- package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +149 -0
- package/plugins/acryl-router/src/core/jsx/dom/css.ts +263 -0
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +172 -0
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.ts +91 -0
- package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +3681 -0
- package/plugins/acryl-router/src/core/jsx/dom/index.ts +170 -0
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +1344 -0
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.ts +438 -0
- package/plugins/acryl-router/src/core/jsx/dom/jsx-dev-runtime.ts +23 -0
- package/plugins/acryl-router/src/core/jsx/dom/jsx-runtime.ts +8 -0
- package/plugins/acryl-router/src/core/jsx/dom/render.ts +879 -0
- package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +199 -0
- package/plugins/acryl-router/src/core/jsx/dom/server.ts +76 -0
- package/plugins/acryl-router/src/core/jsx/dom/utils.ts +7 -0
- package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +859 -0
- package/plugins/acryl-router/src/core/jsx/hooks/index.ts +437 -0
- package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +40 -0
- package/plugins/acryl-router/src/core/jsx/index.test.tsx +1744 -0
- package/plugins/acryl-router/src/core/jsx/index.ts +115 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/common.ts +23 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +666 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.ts +210 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-elements.ts +938 -0
- package/plugins/acryl-router/src/core/jsx/jsx-dev-runtime.ts +26 -0
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +85 -0
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.ts +51 -0
- package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +1281 -0
- package/plugins/acryl-router/src/core/jsx/streaming.ts +231 -0
- package/plugins/acryl-router/src/core/jsx/types.ts +41 -0
- package/plugins/acryl-router/src/core/jsx/utils.test.ts +366 -0
- package/plugins/acryl-router/src/core/jsx/utils.ts +202 -0
- package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +1211 -0
- package/plugins/acryl-router/src/core/middleware/cache/index.ts +323 -0
- package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +334 -0
- package/plugins/acryl-router/src/core/middleware/mount/index.ts +108 -0
- package/plugins/acryl-router/src/core/preset/quick.test.ts +42 -0
- package/plugins/acryl-router/src/core/preset/quick.ts +24 -0
- package/plugins/acryl-router/src/core/preset/tiny.test.ts +42 -0
- package/plugins/acryl-router/src/core/preset/tiny.ts +20 -0
- package/plugins/acryl-router/src/core/utils/types.ts +9 -0
- package/plugins/acryl-router/src/helpers/index.ts +3 -0
- package/plugins/acryl-router/src/middleware/index.ts +2 -0
- package/plugins/acryl-router/src/service.ts +22 -5
- package/plugins/acryl-router/tests/jsx.spec.tsx +63 -0
- package/plugins/acryl-router/upstream.json +3 -3
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/save/plugin.json +1 -1
- package/templates/todo/upstream.json +1 -1
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
import { streamSSE } from './index.ts'
|
|
2
|
+
/** @jsxImportSource ../../jsx */
|
|
3
|
+
import { Context } from '../../context.ts'
|
|
4
|
+
import { ErrorBoundary, Suspense } from '../../jsx/index.ts'
|
|
5
|
+
|
|
6
|
+
describe('SSE Streaming helper', () => {
|
|
7
|
+
const req = new Request('http://localhost/')
|
|
8
|
+
let c: Context
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
c = new Context(req)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('Check streamSSE Response', async () => {
|
|
14
|
+
let spy
|
|
15
|
+
const res = streamSSE(c, async (stream) => {
|
|
16
|
+
spy = vi.spyOn(stream, 'close').mockImplementation(async () => {})
|
|
17
|
+
|
|
18
|
+
let id = 0
|
|
19
|
+
const maxIterations = 5
|
|
20
|
+
|
|
21
|
+
while (id < maxIterations) {
|
|
22
|
+
const message = `Message\nIt is ${id}`
|
|
23
|
+
await stream.writeSSE({ data: message, event: 'time-update', id: String(id++) })
|
|
24
|
+
await stream.sleep(10)
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
expect(res).not.toBeNull()
|
|
29
|
+
expect(res.status).toBe(200)
|
|
30
|
+
expect(res.headers.get('Transfer-Encoding')).toEqual('chunked')
|
|
31
|
+
expect(res.headers.get('Content-Type')).toEqual('text/event-stream')
|
|
32
|
+
expect(res.headers.get('Cache-Control')).toEqual('no-cache')
|
|
33
|
+
expect(res.headers.get('Connection')).toEqual('keep-alive')
|
|
34
|
+
|
|
35
|
+
if (!res.body) {
|
|
36
|
+
throw new Error('Body is null')
|
|
37
|
+
}
|
|
38
|
+
const reader = res.body.getReader()
|
|
39
|
+
const decoder = new TextDecoder()
|
|
40
|
+
for (let i = 0; i < 5; i++) {
|
|
41
|
+
const { value } = await reader.read()
|
|
42
|
+
const decodedValue = decoder.decode(value)
|
|
43
|
+
|
|
44
|
+
// Check the structure and content of the SSE message
|
|
45
|
+
let expectedValue = 'event: time-update\n'
|
|
46
|
+
expectedValue += 'data: Message\n'
|
|
47
|
+
expectedValue += `data: It is ${i}\n`
|
|
48
|
+
expectedValue += `id: ${i}\n\n`
|
|
49
|
+
expect(decodedValue).toBe(expectedValue)
|
|
50
|
+
}
|
|
51
|
+
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
52
|
+
expect(spy).toHaveBeenCalled()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('Check streamSSE Response if aborted by client', async () => {
|
|
56
|
+
let aborted = false
|
|
57
|
+
const res = streamSSE(c, async (stream) => {
|
|
58
|
+
stream.onAbort(() => {
|
|
59
|
+
aborted = true
|
|
60
|
+
})
|
|
61
|
+
for (let i = 0; i < 3; i++) {
|
|
62
|
+
await stream.writeSSE({
|
|
63
|
+
data: `Message ${i}`,
|
|
64
|
+
})
|
|
65
|
+
await stream.sleep(1)
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
if (!res.body) {
|
|
69
|
+
throw new Error('Body is null')
|
|
70
|
+
}
|
|
71
|
+
const reader = res.body.getReader()
|
|
72
|
+
const { value } = await reader.read()
|
|
73
|
+
expect(value).toEqual(new TextEncoder().encode('data: Message 0\n\n'))
|
|
74
|
+
reader.cancel()
|
|
75
|
+
expect(aborted).toBeTruthy()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('Check streamSSE Response if aborted by abort signal', async () => {
|
|
79
|
+
// Emulate an old version of Bun (version 1.1.0) for this specific test case
|
|
80
|
+
// @ts-expect-error Bun is not typed
|
|
81
|
+
global.Bun = {
|
|
82
|
+
version: '1.1.0',
|
|
83
|
+
}
|
|
84
|
+
const ac = new AbortController()
|
|
85
|
+
const req = new Request('http://localhost/', { signal: ac.signal })
|
|
86
|
+
const c = new Context(req)
|
|
87
|
+
|
|
88
|
+
let aborted = false
|
|
89
|
+
const res = streamSSE(c, async (stream) => {
|
|
90
|
+
stream.onAbort(() => {
|
|
91
|
+
aborted = true
|
|
92
|
+
})
|
|
93
|
+
for (let i = 0; i < 3; i++) {
|
|
94
|
+
await stream.writeSSE({
|
|
95
|
+
data: `Message ${i}`,
|
|
96
|
+
})
|
|
97
|
+
await stream.sleep(1)
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
if (!res.body) {
|
|
101
|
+
throw new Error('Body is null')
|
|
102
|
+
}
|
|
103
|
+
const reader = res.body.getReader()
|
|
104
|
+
const { value } = await reader.read()
|
|
105
|
+
expect(value).toEqual(new TextEncoder().encode('data: Message 0\n\n'))
|
|
106
|
+
ac.abort()
|
|
107
|
+
expect(aborted).toBeTruthy()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('Should include retry in the SSE message', async () => {
|
|
111
|
+
const retryTime = 3000 // 3 seconds
|
|
112
|
+
const res = streamSSE(c, async (stream) => {
|
|
113
|
+
await stream.writeSSE({
|
|
114
|
+
data: 'This is a test message',
|
|
115
|
+
retry: retryTime,
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
expect(res).not.toBeNull()
|
|
120
|
+
expect(res.status).toBe(200)
|
|
121
|
+
|
|
122
|
+
if (!res.body) {
|
|
123
|
+
throw new Error('Body is null')
|
|
124
|
+
}
|
|
125
|
+
const reader = res.body.getReader()
|
|
126
|
+
const decoder = new TextDecoder()
|
|
127
|
+
const { value } = await reader.read()
|
|
128
|
+
const decodedValue = decoder.decode(value)
|
|
129
|
+
|
|
130
|
+
// Check if the retry parameter is included in the SSE message
|
|
131
|
+
const expectedRetryValue = `retry: ${retryTime}\n\n`
|
|
132
|
+
expect(decodedValue).toContain(expectedRetryValue)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('Should accept retry: 0 and not skip it', async () => {
|
|
136
|
+
const res = streamSSE(c, async (stream) => {
|
|
137
|
+
await stream.writeSSE({
|
|
138
|
+
data: 'This is a test message',
|
|
139
|
+
retry: 0,
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
expect(res).not.toBeNull()
|
|
144
|
+
expect(res.status).toBe(200)
|
|
145
|
+
|
|
146
|
+
if (!res.body) {
|
|
147
|
+
throw new Error('Body is null')
|
|
148
|
+
}
|
|
149
|
+
const reader = res.body.getReader()
|
|
150
|
+
const decoder = new TextDecoder()
|
|
151
|
+
const { value } = await reader.read()
|
|
152
|
+
const decodedValue = decoder.decode(value)
|
|
153
|
+
|
|
154
|
+
expect(decodedValue).toContain('retry: 0\n\n')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('Should emit an empty id to reset Last-Event-ID', async () => {
|
|
158
|
+
const res = streamSSE(c, async (stream) => {
|
|
159
|
+
await stream.writeSSE({
|
|
160
|
+
data: 'reset',
|
|
161
|
+
id: '',
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
expect(await res.text()).toBe('data: reset\nid: \n\n')
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it('Check stream Response if error occurred', async () => {
|
|
169
|
+
const onError = vi.fn()
|
|
170
|
+
const res = streamSSE(
|
|
171
|
+
c,
|
|
172
|
+
async () => {
|
|
173
|
+
throw new Error('Test error')
|
|
174
|
+
},
|
|
175
|
+
onError
|
|
176
|
+
)
|
|
177
|
+
if (!res.body) {
|
|
178
|
+
throw new Error('Body is null')
|
|
179
|
+
}
|
|
180
|
+
const reader = res.body.getReader()
|
|
181
|
+
const decoder = new TextDecoder()
|
|
182
|
+
const { value } = await reader.read()
|
|
183
|
+
const decodedValue = decoder.decode(value)
|
|
184
|
+
expect(decodedValue).toBe('event: error\ndata: Test error\n\n')
|
|
185
|
+
expect(onError).toBeCalledTimes(1)
|
|
186
|
+
expect(onError).toBeCalledWith(new Error('Test error'), expect.anything()) // 2nd argument is StreamingApi instance
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('Check streamSSE Response via Promise<string>', async () => {
|
|
190
|
+
const res = streamSSE(c, async (stream) => {
|
|
191
|
+
await stream.writeSSE({ data: Promise.resolve('Async Message') })
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
expect(res).not.toBeNull()
|
|
195
|
+
expect(res.status).toBe(200)
|
|
196
|
+
|
|
197
|
+
if (!res.body) {
|
|
198
|
+
throw new Error('Body is null')
|
|
199
|
+
}
|
|
200
|
+
const reader = res.body.getReader()
|
|
201
|
+
const decoder = new TextDecoder()
|
|
202
|
+
const { value } = await reader.read()
|
|
203
|
+
const decodedValue = decoder.decode(value)
|
|
204
|
+
expect(decodedValue).toBe('data: Async Message\n\n')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('Check streamSSE Response via JSX.Element', async () => {
|
|
208
|
+
const res = streamSSE(c, async (stream) => {
|
|
209
|
+
await stream.writeSSE({ data: <div>Hello</div> })
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
expect(res).not.toBeNull()
|
|
213
|
+
expect(res.status).toBe(200)
|
|
214
|
+
|
|
215
|
+
if (!res.body) {
|
|
216
|
+
throw new Error('Body is null')
|
|
217
|
+
}
|
|
218
|
+
const reader = res.body.getReader()
|
|
219
|
+
const decoder = new TextDecoder()
|
|
220
|
+
const { value } = await reader.read()
|
|
221
|
+
const decodedValue = decoder.decode(value)
|
|
222
|
+
expect(decodedValue).toBe('data: <div>Hello</div>\n\n')
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('Check streamSSE Response via ErrorBoundary in success case', async () => {
|
|
226
|
+
const AsyncComponent = async () => Promise.resolve(<div>Async Hello</div>)
|
|
227
|
+
const res = streamSSE(c, async (stream) => {
|
|
228
|
+
await stream.writeSSE({
|
|
229
|
+
data: (
|
|
230
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
231
|
+
<AsyncComponent />
|
|
232
|
+
</ErrorBoundary>
|
|
233
|
+
),
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
expect(res).not.toBeNull()
|
|
238
|
+
expect(res.status).toBe(200)
|
|
239
|
+
|
|
240
|
+
if (!res.body) {
|
|
241
|
+
throw new Error('Body is null')
|
|
242
|
+
}
|
|
243
|
+
const reader = res.body.getReader()
|
|
244
|
+
const decoder = new TextDecoder()
|
|
245
|
+
const { value } = await reader.read()
|
|
246
|
+
const decodedValue = decoder.decode(value)
|
|
247
|
+
expect(decodedValue).toBe('data: <div>Async Hello</div>\n\n')
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it('Check streamSSE Response via ErrorBoundary in error case', async () => {
|
|
251
|
+
const AsyncComponent = async () => Promise.reject()
|
|
252
|
+
const res = streamSSE(c, async (stream) => {
|
|
253
|
+
await stream.writeSSE({
|
|
254
|
+
data: (
|
|
255
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
256
|
+
<AsyncComponent />
|
|
257
|
+
</ErrorBoundary>
|
|
258
|
+
),
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
expect(res).not.toBeNull()
|
|
263
|
+
expect(res.status).toBe(200)
|
|
264
|
+
|
|
265
|
+
if (!res.body) {
|
|
266
|
+
throw new Error('Body is null')
|
|
267
|
+
}
|
|
268
|
+
const reader = res.body.getReader()
|
|
269
|
+
const decoder = new TextDecoder()
|
|
270
|
+
const { value } = await reader.read()
|
|
271
|
+
const decodedValue = decoder.decode(value)
|
|
272
|
+
expect(decodedValue).toBe('data: <div>Error</div>\n\n')
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
it('Check streamSSE Response via Suspense with a multi-line fallback', async () => {
|
|
276
|
+
const AsyncComponent = async () => Promise.resolve(<div>Async Hello</div>)
|
|
277
|
+
const res = streamSSE(c, async (stream) => {
|
|
278
|
+
await stream.writeSSE({
|
|
279
|
+
data: (
|
|
280
|
+
<Suspense fallback={<div>{'Loading...\nPlease wait'}</div>}>
|
|
281
|
+
<AsyncComponent />
|
|
282
|
+
</Suspense>
|
|
283
|
+
),
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
expect(res).not.toBeNull()
|
|
288
|
+
expect(res.status).toBe(200)
|
|
289
|
+
|
|
290
|
+
if (!res.body) {
|
|
291
|
+
throw new Error('Body is null')
|
|
292
|
+
}
|
|
293
|
+
const reader = res.body.getReader()
|
|
294
|
+
const decoder = new TextDecoder()
|
|
295
|
+
const { value } = await reader.read()
|
|
296
|
+
const decodedValue = decoder.decode(value)
|
|
297
|
+
expect(decodedValue).toBe('data: <div>Async Hello</div>\n\n')
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('Check streamSSE Response via ErrorBoundary with multi-line content', async () => {
|
|
301
|
+
const AsyncComponent = async () => Promise.resolve(<div>{'Async\nHello'}</div>)
|
|
302
|
+
const res = streamSSE(c, async (stream) => {
|
|
303
|
+
await stream.writeSSE({
|
|
304
|
+
data: (
|
|
305
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
306
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
307
|
+
<AsyncComponent />
|
|
308
|
+
</Suspense>
|
|
309
|
+
</ErrorBoundary>
|
|
310
|
+
),
|
|
311
|
+
})
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
expect(res).not.toBeNull()
|
|
315
|
+
expect(res.status).toBe(200)
|
|
316
|
+
|
|
317
|
+
if (!res.body) {
|
|
318
|
+
throw new Error('Body is null')
|
|
319
|
+
}
|
|
320
|
+
const reader = res.body.getReader()
|
|
321
|
+
const decoder = new TextDecoder()
|
|
322
|
+
const { value } = await reader.read()
|
|
323
|
+
const decodedValue = decoder.decode(value)
|
|
324
|
+
expect(decodedValue).toBe('data: <div>Async\ndata: Hello</div>\n\n')
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
it('Check streamSSE Response via sibling Suspense boundaries with multi-line fallbacks', async () => {
|
|
328
|
+
const One = async () => Promise.resolve(<div>One</div>)
|
|
329
|
+
const Two = async () => Promise.resolve(<div>Two</div>)
|
|
330
|
+
const res = streamSSE(c, async (stream) => {
|
|
331
|
+
await stream.writeSSE({
|
|
332
|
+
data: (
|
|
333
|
+
<div>
|
|
334
|
+
<Suspense fallback={<div>{'Loading1...\nwait'}</div>}>
|
|
335
|
+
<One />
|
|
336
|
+
</Suspense>
|
|
337
|
+
<Suspense fallback={<div>{'Loading2...\nwait'}</div>}>
|
|
338
|
+
<Two />
|
|
339
|
+
</Suspense>
|
|
340
|
+
</div>
|
|
341
|
+
),
|
|
342
|
+
})
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
expect(res).not.toBeNull()
|
|
346
|
+
expect(res.status).toBe(200)
|
|
347
|
+
|
|
348
|
+
if (!res.body) {
|
|
349
|
+
throw new Error('Body is null')
|
|
350
|
+
}
|
|
351
|
+
const reader = res.body.getReader()
|
|
352
|
+
const decoder = new TextDecoder()
|
|
353
|
+
let decodedValue = ''
|
|
354
|
+
for (;;) {
|
|
355
|
+
const { done, value } = await reader.read()
|
|
356
|
+
if (done) {
|
|
357
|
+
break
|
|
358
|
+
}
|
|
359
|
+
decodedValue += decoder.decode(value)
|
|
360
|
+
}
|
|
361
|
+
expect(decodedValue).toBe('data: <div><div>One</div><div>Two</div></div>\n\n')
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
it('Check streamSSE Response via nested Suspense boundaries with multi-line fallback and content', async () => {
|
|
365
|
+
const Inner = async () => Promise.resolve(<div>{'Inner\nContent'}</div>)
|
|
366
|
+
const Outer = async () =>
|
|
367
|
+
Promise.resolve(
|
|
368
|
+
<Suspense fallback={<div>{'B\nb'}</div>}>
|
|
369
|
+
<Inner />
|
|
370
|
+
</Suspense>
|
|
371
|
+
)
|
|
372
|
+
const res = streamSSE(c, async (stream) => {
|
|
373
|
+
await stream.writeSSE({
|
|
374
|
+
data: (
|
|
375
|
+
<Suspense fallback={<div>{'A\na'}</div>}>
|
|
376
|
+
<Outer />
|
|
377
|
+
</Suspense>
|
|
378
|
+
),
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
expect(res).not.toBeNull()
|
|
383
|
+
expect(res.status).toBe(200)
|
|
384
|
+
|
|
385
|
+
if (!res.body) {
|
|
386
|
+
throw new Error('Body is null')
|
|
387
|
+
}
|
|
388
|
+
const reader = res.body.getReader()
|
|
389
|
+
const decoder = new TextDecoder()
|
|
390
|
+
let decodedValue = ''
|
|
391
|
+
for (;;) {
|
|
392
|
+
const { done, value } = await reader.read()
|
|
393
|
+
if (done) {
|
|
394
|
+
break
|
|
395
|
+
}
|
|
396
|
+
decodedValue += decoder.decode(value)
|
|
397
|
+
}
|
|
398
|
+
expect(decodedValue).toBe('data: <div>Inner\ndata: Content</div>\n\n')
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
it('Check streamSSE handles \\r (CR) line ending correctly', async () => {
|
|
402
|
+
const res = streamSSE(c, async (stream) => {
|
|
403
|
+
await stream.writeSSE({
|
|
404
|
+
data: 'Line1\rLine2',
|
|
405
|
+
event: 'test-cr',
|
|
406
|
+
})
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
if (!res.body) {
|
|
410
|
+
throw new Error('Body is null')
|
|
411
|
+
}
|
|
412
|
+
const reader = res.body.getReader()
|
|
413
|
+
const decoder = new TextDecoder()
|
|
414
|
+
const { value } = await reader.read()
|
|
415
|
+
const decodedValue = decoder.decode(value)
|
|
416
|
+
|
|
417
|
+
expect(decodedValue).toBe('event: test-cr\ndata: Line1\ndata: Line2\n\n')
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
it('Check streamSSE handles \\r\\n (CRLF) line ending correctly', async () => {
|
|
421
|
+
const res = streamSSE(c, async (stream) => {
|
|
422
|
+
await stream.writeSSE({
|
|
423
|
+
data: 'Line1\r\nLine2',
|
|
424
|
+
event: 'test-crlf',
|
|
425
|
+
})
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
if (!res.body) {
|
|
429
|
+
throw new Error('Body is null')
|
|
430
|
+
}
|
|
431
|
+
const reader = res.body.getReader()
|
|
432
|
+
const decoder = new TextDecoder()
|
|
433
|
+
const { value } = await reader.read()
|
|
434
|
+
const decodedValue = decoder.decode(value)
|
|
435
|
+
|
|
436
|
+
expect(decodedValue).toBe('event: test-crlf\ndata: Line1\ndata: Line2\n\n')
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
it('Check streamSSE handles mixed line endings correctly', async () => {
|
|
440
|
+
const res = streamSSE(c, async (stream) => {
|
|
441
|
+
await stream.writeSSE({
|
|
442
|
+
data: 'A\nB\rC\r\nD',
|
|
443
|
+
event: 'test-mixed',
|
|
444
|
+
})
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
if (!res.body) {
|
|
448
|
+
throw new Error('Body is null')
|
|
449
|
+
}
|
|
450
|
+
const reader = res.body.getReader()
|
|
451
|
+
const decoder = new TextDecoder()
|
|
452
|
+
const { value } = await reader.read()
|
|
453
|
+
const decodedValue = decoder.decode(value)
|
|
454
|
+
|
|
455
|
+
expect(decodedValue).toBe('event: test-mixed\ndata: A\ndata: B\ndata: C\ndata: D\n\n')
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
it('Should throw error if event contains \\n', async () => {
|
|
459
|
+
const onError = vi.fn()
|
|
460
|
+
const res = streamSSE(
|
|
461
|
+
c,
|
|
462
|
+
async (stream) => {
|
|
463
|
+
await stream.writeSSE({ data: 'test', event: 'test\nevent' })
|
|
464
|
+
},
|
|
465
|
+
onError
|
|
466
|
+
)
|
|
467
|
+
if (!res.body) {
|
|
468
|
+
throw new Error('Body is null')
|
|
469
|
+
}
|
|
470
|
+
const reader = res.body.getReader()
|
|
471
|
+
const decoder = new TextDecoder()
|
|
472
|
+
const { value } = await reader.read()
|
|
473
|
+
const decodedValue = decoder.decode(value)
|
|
474
|
+
expect(decodedValue).toContain('event: error')
|
|
475
|
+
expect(onError).toBeCalledTimes(1)
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
it('Should throw error if event contains \\r', async () => {
|
|
479
|
+
const onError = vi.fn()
|
|
480
|
+
const res = streamSSE(
|
|
481
|
+
c,
|
|
482
|
+
async (stream) => {
|
|
483
|
+
await stream.writeSSE({ data: 'test', event: 'test\revent' })
|
|
484
|
+
},
|
|
485
|
+
onError
|
|
486
|
+
)
|
|
487
|
+
if (!res.body) {
|
|
488
|
+
throw new Error('Body is null')
|
|
489
|
+
}
|
|
490
|
+
const reader = res.body.getReader()
|
|
491
|
+
const decoder = new TextDecoder()
|
|
492
|
+
const { value } = await reader.read()
|
|
493
|
+
const decodedValue = decoder.decode(value)
|
|
494
|
+
expect(decodedValue).toContain('event: error')
|
|
495
|
+
expect(onError).toBeCalledTimes(1)
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
it('Should throw error if id contains \\n', async () => {
|
|
499
|
+
const onError = vi.fn()
|
|
500
|
+
const res = streamSSE(
|
|
501
|
+
c,
|
|
502
|
+
async (stream) => {
|
|
503
|
+
await stream.writeSSE({ data: 'test', id: 'test\nid' })
|
|
504
|
+
},
|
|
505
|
+
onError
|
|
506
|
+
)
|
|
507
|
+
if (!res.body) {
|
|
508
|
+
throw new Error('Body is null')
|
|
509
|
+
}
|
|
510
|
+
const reader = res.body.getReader()
|
|
511
|
+
const decoder = new TextDecoder()
|
|
512
|
+
const { value } = await reader.read()
|
|
513
|
+
const decodedValue = decoder.decode(value)
|
|
514
|
+
expect(decodedValue).toContain('event: error')
|
|
515
|
+
expect(onError).toBeCalledTimes(1)
|
|
516
|
+
})
|
|
517
|
+
|
|
518
|
+
it('Should throw error if id contains \\r', async () => {
|
|
519
|
+
const onError = vi.fn()
|
|
520
|
+
const res = streamSSE(
|
|
521
|
+
c,
|
|
522
|
+
async (stream) => {
|
|
523
|
+
await stream.writeSSE({ data: 'test', id: 'test\rid' })
|
|
524
|
+
},
|
|
525
|
+
onError
|
|
526
|
+
)
|
|
527
|
+
if (!res.body) {
|
|
528
|
+
throw new Error('Body is null')
|
|
529
|
+
}
|
|
530
|
+
const reader = res.body.getReader()
|
|
531
|
+
const decoder = new TextDecoder()
|
|
532
|
+
const { value } = await reader.read()
|
|
533
|
+
const decodedValue = decoder.decode(value)
|
|
534
|
+
expect(decodedValue).toContain('event: error')
|
|
535
|
+
expect(onError).toBeCalledTimes(1)
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
it('Check streamSSE handles consecutive \\r correctly', async () => {
|
|
539
|
+
const res = streamSSE(c, async (stream) => {
|
|
540
|
+
await stream.writeSSE({
|
|
541
|
+
data: 'Left\r\rRight',
|
|
542
|
+
event: 'test-double-cr',
|
|
543
|
+
})
|
|
544
|
+
})
|
|
545
|
+
|
|
546
|
+
if (!res.body) {
|
|
547
|
+
throw new Error('Body is null')
|
|
548
|
+
}
|
|
549
|
+
const reader = res.body.getReader()
|
|
550
|
+
const decoder = new TextDecoder()
|
|
551
|
+
const { value } = await reader.read()
|
|
552
|
+
const decodedValue = decoder.decode(value)
|
|
553
|
+
|
|
554
|
+
// Two \r should produce an empty line in between
|
|
555
|
+
expect(decodedValue).toBe('event: test-double-cr\ndata: Left\ndata: \ndata: Right\n\n')
|
|
556
|
+
})
|
|
557
|
+
})
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** @jsxImportSource ./ */
|
|
2
|
+
|
|
3
|
+
import type { HtmlEscapedString } from '../utils/html.ts'
|
|
4
|
+
import type { Child, JSXNode } from './base.ts'
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6
|
+
import { cloneElement, jsx, Fragment } from './base.ts'
|
|
7
|
+
|
|
8
|
+
describe('cloneElement', () => {
|
|
9
|
+
it('should clone an element with new props', () => {
|
|
10
|
+
const element = <div className='original'>Hello</div>
|
|
11
|
+
const clonedElement = cloneElement(element, { className: 'cloned' })
|
|
12
|
+
expect((clonedElement as unknown as JSXNode).props.className).toBe('cloned')
|
|
13
|
+
expect((clonedElement as unknown as JSXNode).props.children).toBe('Hello')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should clone a children element', () => {
|
|
17
|
+
const fnElement = ({ message }: { message: string }) => <div>{message}</div>
|
|
18
|
+
const element = fnElement({ message: 'Hello' })
|
|
19
|
+
const clonedElement = cloneElement(element, {})
|
|
20
|
+
expect(element.toString()).toBe('<div>Hello</div>')
|
|
21
|
+
expect(clonedElement.toString()).toBe('<div>Hello</div>')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should clone an element with new children', () => {
|
|
25
|
+
const fnElement = ({ message }: { message: string }) => <div>{message}</div>
|
|
26
|
+
const element = fnElement({ message: 'Hello' })
|
|
27
|
+
const clonedElement = cloneElement(element, {}, 'World')
|
|
28
|
+
expect(element.toString()).toBe('<div>Hello</div>')
|
|
29
|
+
expect(clonedElement.toString()).toBe('<div>World</div>')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should self-close a wrapped empty tag', () => {
|
|
33
|
+
const Hr = ({ ...props }) => <hr {...props} />
|
|
34
|
+
const element = <Hr />
|
|
35
|
+
expect(element.toString()).toBe('<hr/>')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should preserve self-closing empty tag when cloned', () => {
|
|
39
|
+
const element = <img src='foo.png' />
|
|
40
|
+
const clonedElement = cloneElement(element, { alt: 'bar' })
|
|
41
|
+
expect(element.toString()).toBe('<img src="foo.png"/>')
|
|
42
|
+
expect(clonedElement.toString()).toBe('<img src="foo.png" alt="bar"/>')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should clone an element with array children', () => {
|
|
46
|
+
const element = (
|
|
47
|
+
<div>
|
|
48
|
+
<span>1</span>
|
|
49
|
+
<span>2</span>
|
|
50
|
+
</div>
|
|
51
|
+
)
|
|
52
|
+
const clonedElement = cloneElement(element, { className: 'cloned' })
|
|
53
|
+
expect(clonedElement.toString()).toBe('<div class="cloned"><span>1</span><span>2</span></div>')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('createElement', () => {
|
|
58
|
+
it('should accept a component that returns a JSXNode', () => {
|
|
59
|
+
const Partial = ({ name }: { name: string }) => jsx('div', { 'x-partial': name }, name)
|
|
60
|
+
|
|
61
|
+
expect((<Partial name='foo' />).toString()).toBe('<div x-partial="foo">foo</div>')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should preserve the SVG element shape', () => {
|
|
65
|
+
const ref = { current: null }
|
|
66
|
+
const element = jsx('svg', { ref }) as unknown as JSXNode
|
|
67
|
+
expect(element.tag).toBe('svg')
|
|
68
|
+
expect(element.type).toBe('svg')
|
|
69
|
+
expect(element.ref).toBe(ref)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('should accept a Child-typed value as a child', () => {
|
|
73
|
+
const child: Child = <span>inner</span>
|
|
74
|
+
const element = jsx('div', null, child) as unknown as JSXNode
|
|
75
|
+
expect(element.toString()).toBe('<div><span>inner</span></div>')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should escape a string returned by an asynchronous component', async () => {
|
|
79
|
+
const Async = async () => '<img src=x onerror=alert(1)>' as unknown as HtmlEscapedString
|
|
80
|
+
|
|
81
|
+
expect(
|
|
82
|
+
String(
|
|
83
|
+
await (
|
|
84
|
+
<div>
|
|
85
|
+
<Async />
|
|
86
|
+
</div>
|
|
87
|
+
).toString()
|
|
88
|
+
)
|
|
89
|
+
).toBe('<div><img src=x onerror=alert(1)></div>')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('should omit an unsupported object child', () => {
|
|
93
|
+
const object = { toString: () => 'plain-text' }
|
|
94
|
+
|
|
95
|
+
expect((<div>{object as never}</div>).toString()).toBe('<div></div>')
|
|
96
|
+
})
|
|
97
|
+
})
|