acrylblends 0.6.3 → 0.7.0
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 +4 -3
- package/docs/framework/README.md +2 -2
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/guides/jsx.md +51 -1
- package/docs/framework/helpers/ssg.md +3 -6
- package/docs/framework/middleware/builtin/jsx-renderer.md +0 -3
- package/docs/framework/upstream.json +2 -2
- 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-wFwpg-bt.d.ts → app-D8riatvv.d.ts} +1 -1
- package/lib/{base-ddiVmiE7.js → base-NSN_SNv-.js} +1 -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 +1 -1
- package/lib/cache.js +4 -4
- package/lib/{client-BHTr318g.js → client-DDLXwhBo.js} +2 -125
- package/lib/client.d.ts +2 -2
- package/lib/client.js +2 -1
- package/lib/combine.d.ts +1 -1
- package/lib/{components-DYZdxJIJ.js → components-JXVm45kK.js} +1 -1
- package/lib/{components-Ce5FRyHU.js → components-cEW3ovK0.js} +3 -3
- package/lib/compress.d.ts +1 -1
- package/lib/conninfo.d.ts +2 -2
- package/lib/{context-DVQBjuns.d.ts → context-DS3fYyG-.d.ts} +1 -1
- package/lib/context-storage.d.ts +1 -1
- package/lib/cookie.d.ts +1 -1
- package/lib/cors.d.ts +1 -1
- package/lib/csrf.d.ts +1 -1
- package/lib/dev.d.ts +2 -2
- package/lib/etag.d.ts +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/{hooks-Bq5q9IMl.js → hooks-w72iLeFR.js} +6 -6
- package/lib/{index-CUTfU69R.d.ts → index-Bb7YJKCq.d.ts} +2 -2
- package/lib/{index-CsvB-F9j.d.ts → index-BvFh1Gfx.d.ts} +1 -1
- package/lib/{index-Cqcdluyn.d.ts → index-CTy4N7ng.d.ts} +2 -2
- package/lib/index.d.ts +4 -4
- package/lib/ip-restriction.d.ts +2 -2
- package/lib/jsx/dom/client.js +1 -1
- package/lib/jsx/dom/jsx-dev-runtime.js +1 -1
- package/lib/jsx/dom/jsx-runtime.js +1 -1
- package/lib/jsx/dom/server.js +2 -2
- package/lib/jsx/dom.js +4 -4
- package/lib/jsx/jsx-dev-runtime.js +1 -1
- package/lib/jsx/jsx-runtime.js +1 -1
- package/lib/jsx/streaming.js +1 -1
- package/lib/{jsx-dev-runtime-kAhR_4C7.js → jsx-dev-runtime-CWLRA2F3.js} +2 -2
- package/lib/jsx-renderer.d.ts +75 -0
- package/lib/jsx-renderer.js +99 -0
- package/lib/jsx.js +4 -4
- package/lib/jwk.d.ts +1 -1
- package/lib/jwt.d.ts +1 -1
- package/lib/language.d.ts +1 -1
- 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 +1 -1
- package/lib/node-server/conninfo.d.ts +1 -1
- package/lib/node-server/register.d.ts +1 -0
- package/lib/node-server/register.js +73 -0
- 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/powered-by.d.ts +1 -1
- package/lib/pretty-json.d.ts +1 -1
- package/lib/quick.d.ts +1 -1
- package/lib/request-id.d.ts +1 -1
- package/lib/request.d.ts +1 -1
- package/lib/route.d.ts +1 -1
- package/lib/schema-validator.d.ts +1 -1
- package/lib/secure-headers.d.ts +1 -1
- package/lib/{serve-static-Cw23shXv.js → serve-static-B14MF1Va.js} +1 -0
- package/lib/serve-static.d.ts +1 -1
- package/lib/ssg-BwjcA-Ge.js +389 -0
- package/lib/ssg.d.ts +156 -0
- package/lib/ssg.js +2 -0
- package/lib/streaming.d.ts +1 -1
- package/lib/testing.d.ts +3 -3
- package/lib/testing.js +1 -1
- package/lib/timeout.d.ts +1 -1
- package/lib/timing.d.ts +1 -1
- package/lib/tiny.d.ts +1 -1
- package/lib/trailing-slash.d.ts +1 -1
- package/lib/{types-nEL8VPDC.d.ts → types-Dv_nV2cv.d.ts} +1 -1
- package/lib/{types-CQiGmsWW.d.ts → types-zNxyEpG2.d.ts} +2 -2
- package/lib/types.d.ts +1 -1
- package/lib/utils/body.d.ts +1 -1
- package/lib/utils/ipaddr.d.ts +1 -1
- package/lib/utils-Cx61GFMC.js +125 -0
- package/lib/validator.d.ts +1 -1
- package/lib/ws.d.ts +1 -1
- package/package.json +13 -1
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/src/register.ts +86 -0
- package/plugins/acryl-node-server/upstream.json +5 -3
- package/plugins/acryl-router/src/core/helper/ssg/index.ts +14 -0
- package/plugins/acryl-router/src/core/helper/ssg/middleware.ts +83 -0
- package/plugins/acryl-router/src/core/helper/ssg/plugins.ts +72 -0
- package/plugins/acryl-router/src/core/helper/ssg/ssg.ts +471 -0
- package/plugins/acryl-router/src/core/helper/ssg/utils.ts +118 -0
- package/plugins/acryl-router/src/core/jsx/dom/render.ts +6 -6
- package/plugins/acryl-router/src/core/middleware/cache/index.ts +4 -4
- package/plugins/acryl-router/src/core/middleware/jsx-renderer/index.ts +162 -0
- package/plugins/acryl-router/src/helpers/index.ts +1 -0
- package/plugins/acryl-router/src/middleware/index.ts +1 -0
- package/plugins/acryl-router/upstream.json +2 -2
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/main.ts +4 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/main.ts +3 -1
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/extensions/resolve.ts +4 -0
- package/shared/AGENTS.core.md +3 -0
- package/templates/agent/package.json +7 -3
- package/templates/agent/tsconfig.json +10 -2
- package/templates/blank/package.json +7 -3
- package/templates/blank/src/about.tsx +14 -0
- package/templates/blank/src/routes.ts +2 -0
- package/templates/blank/test/app.test.ts +15 -0
- package/templates/blank/tsconfig.json +3 -1
- package/templates/todo/package.json +7 -3
- package/templates/todo/tsconfig.json +3 -1
- package/templates/todo/upstream.json +1 -1
- package/plugins/acryl-inspect/tests/report.spec.ts +0 -69
- package/plugins/acryl-inspect/tests/service.spec.ts +0 -121
- package/plugins/acryl-node-server/tests/adapter/app.ts +0 -76
- package/plugins/acryl-node-server/tests/adapter/assets/.static/plain.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/favicon.ico +0 -0
- package/plugins/acryl-node-server/tests/adapter/assets/secret.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/admin/secret.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/data.json +0 -5
- package/plugins/acryl-node-server/tests/adapter/assets/static/extensionless +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/foo..bar.txt +0 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/plain.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-context-path/plain.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin.br +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.br +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.zst +0 -1
- package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +0 -34
- package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +0 -248
- package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +0 -457
- package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-cert.pem +0 -23
- package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-key.pem +0 -27
- package/plugins/acryl-node-server/tests/adapter/headers.test.ts +0 -252
- package/plugins/acryl-node-server/tests/adapter/helpers/request.ts +0 -190
- package/plugins/acryl-node-server/tests/adapter/listener.test.ts +0 -727
- package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +0 -189
- package/plugins/acryl-node-server/tests/adapter/request.test.ts +0 -1230
- package/plugins/acryl-node-server/tests/adapter/response.test.ts +0 -285
- package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +0 -849
- package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +0 -790
- package/plugins/acryl-node-server/tests/adapter/server.test.ts +0 -1312
- package/plugins/acryl-node-server/tests/adapter/setup.ts +0 -16
- package/plugins/acryl-node-server/tests/adapter/url.test.ts +0 -76
- package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +0 -23
- package/plugins/acryl-node-server/tests/adapter/utils/stream.test.ts +0 -75
- package/plugins/acryl-node-server/tests/adapter/utils.test.ts +0 -235
- package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +0 -260
- package/plugins/acryl-node-server/tests/server.spec.ts +0 -141
- package/plugins/acryl-router/src/core/app.test.ts +0 -3833
- package/plugins/acryl-router/src/core/client/client.test.ts +0 -2313
- package/plugins/acryl-router/src/core/client/types.test.ts +0 -149
- package/plugins/acryl-router/src/core/client/utils.test.ts +0 -405
- package/plugins/acryl-router/src/core/compose.test.ts +0 -751
- package/plugins/acryl-router/src/core/context.test.ts +0 -629
- package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +0 -344
- package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +0 -55
- package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +0 -510
- package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +0 -506
- package/plugins/acryl-router/src/core/helper/css/index.test.tsx +0 -678
- package/plugins/acryl-router/src/core/helper/dev/index.test.ts +0 -181
- package/plugins/acryl-router/src/core/helper/factory/index.test.ts +0 -434
- package/plugins/acryl-router/src/core/helper/html/index.test.ts +0 -78
- package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +0 -356
- package/plugins/acryl-router/src/core/helper/route/index.test.ts +0 -259
- package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +0 -557
- package/plugins/acryl-router/src/core/helper/streaming/stream.test.ts +0 -128
- package/plugins/acryl-router/src/core/helper/streaming/text.test.ts +0 -34
- package/plugins/acryl-router/src/core/helper/testing/index.test.ts +0 -42
- package/plugins/acryl-router/src/core/helper/websocket/index.test.ts +0 -124
- package/plugins/acryl-router/src/core/http-exception.test.ts +0 -45
- package/plugins/acryl-router/src/core/jsx/base.test.tsx +0 -97
- package/plugins/acryl-router/src/core/jsx/children.test.ts +0 -52
- package/plugins/acryl-router/src/core/jsx/components.test.tsx +0 -936
- package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +0 -655
- package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +0 -128
- package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +0 -330
- package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +0 -184
- package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +0 -149
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +0 -172
- package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +0 -3681
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +0 -1344
- package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +0 -199
- package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +0 -859
- package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +0 -40
- package/plugins/acryl-router/src/core/jsx/index.test.tsx +0 -1744
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +0 -666
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +0 -85
- package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +0 -1281
- package/plugins/acryl-router/src/core/jsx/utils.test.ts +0 -366
- package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +0 -434
- package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +0 -1018
- package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +0 -273
- package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +0 -1211
- package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +0 -355
- package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +0 -535
- package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +0 -43
- package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +0 -532
- package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +0 -534
- package/plugins/acryl-router/src/core/middleware/etag/digest.test.ts +0 -21
- package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +0 -503
- package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +0 -217
- package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +0 -1135
- package/plugins/acryl-router/src/core/middleware/jwk/keys.test.json +0 -48
- package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +0 -802
- package/plugins/acryl-router/src/core/middleware/language/index.test.ts +0 -552
- package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +0 -204
- package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +0 -292
- package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +0 -221
- package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +0 -334
- package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +0 -37
- package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +0 -218
- package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +0 -168
- package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +0 -671
- package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +0 -450
- package/plugins/acryl-router/src/core/middleware/serve-static/path.test.ts +0 -50
- package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +0 -70
- package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +0 -310
- package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +0 -381
- package/plugins/acryl-router/src/core/preset/quick.test.ts +0 -42
- package/plugins/acryl-router/src/core/preset/tiny.test.ts +0 -42
- package/plugins/acryl-router/src/core/request.test.ts +0 -751
- package/plugins/acryl-router/src/core/router/common.case.test.ts +0 -1132
- package/plugins/acryl-router/src/core/router/linear-router/router.test.ts +0 -80
- package/plugins/acryl-router/src/core/router/pattern-router/router.test.ts +0 -46
- package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.test.ts +0 -154
- package/plugins/acryl-router/src/core/router/reg-exp-router/router.test.ts +0 -265
- package/plugins/acryl-router/src/core/router/smart-router/router.test.ts +0 -13
- package/plugins/acryl-router/src/core/router/trie-router/node.test.ts +0 -896
- package/plugins/acryl-router/src/core/router/trie-router/router.test.ts +0 -8
- package/plugins/acryl-router/src/core/types.test.ts +0 -4134
- package/plugins/acryl-router/src/core/utils/accept.test.ts +0 -299
- package/plugins/acryl-router/src/core/utils/basic-auth.test.ts +0 -56
- package/plugins/acryl-router/src/core/utils/body.test.ts +0 -496
- package/plugins/acryl-router/src/core/utils/buffer.test.ts +0 -147
- package/plugins/acryl-router/src/core/utils/color.test.ts +0 -23
- package/plugins/acryl-router/src/core/utils/concurrent.test.ts +0 -69
- package/plugins/acryl-router/src/core/utils/cookie.test.ts +0 -460
- package/plugins/acryl-router/src/core/utils/crypto.test.ts +0 -43
- package/plugins/acryl-router/src/core/utils/encode.test.ts +0 -61
- package/plugins/acryl-router/src/core/utils/filepath.test.ts +0 -78
- package/plugins/acryl-router/src/core/utils/html.test.ts +0 -16
- package/plugins/acryl-router/src/core/utils/ipaddr.test.ts +0 -149
- package/plugins/acryl-router/src/core/utils/jwt/jwa.test.ts +0 -86
- package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +0 -1788
- package/plugins/acryl-router/src/core/utils/mime.test.ts +0 -64
- package/plugins/acryl-router/src/core/utils/stream.test.ts +0 -212
- package/plugins/acryl-router/src/core/utils/types.test.ts +0 -295
- package/plugins/acryl-router/src/core/utils/url.test.ts +0 -384
- package/plugins/acryl-router/src/core/validator/utils.test.ts +0 -82
- package/plugins/acryl-router/src/core/validator/validator.test.ts +0 -1488
- package/plugins/acryl-router/tests/docs-examples.spec.ts +0 -152
- package/plugins/acryl-router/tests/jsx.spec.tsx +0 -63
- package/plugins/acryl-router/tests/live-install.spec.ts +0 -64
- package/plugins/acryl-router/tests/loader.spec.ts +0 -72
- package/plugins/acryl-router/tests/loopback-only.spec.ts +0 -80
- package/plugins/acryl-router/tests/no-dom/consumer.ts +0 -11
- package/plugins/acryl-router/tests/no-dom/tsconfig.json +0 -13
- package/plugins/acryl-router/tests/service.spec.ts +0 -426
- package/plugins/acryl-router/tests/shadow-state.spec.ts +0 -79
- package/plugins/acryl-router-http/tests/adapter.spec.ts +0 -174
- package/plugins/acryl-router-http/tests/stream.spec.ts +0 -133
- package/plugins/acryl-router-http/tests/surfaces.spec.ts +0 -101
- package/plugins/acryl-state/src/react.ts +0 -11
- package/plugins/acryl-state/tests/cell.spec.ts +0 -71
- package/plugins/acryl-state/tests/react.spec.ts +0 -39
- package/plugins/acryl-state/tests/service.spec.ts +0 -132
|
@@ -1,557 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { stream } from './index.ts'
|
|
2
|
-
import { Context } from '../../context.ts'
|
|
3
|
-
|
|
4
|
-
describe('Basic Streaming Helper', () => {
|
|
5
|
-
const req = new Request('http://localhost/')
|
|
6
|
-
let c: Context
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
c = new Context(req)
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it('Check stream Response', async () => {
|
|
12
|
-
const res = stream(c, async (stream) => {
|
|
13
|
-
for (let i = 0; i < 3; i++) {
|
|
14
|
-
await stream.write(new Uint8Array([i]))
|
|
15
|
-
await stream.sleep(1)
|
|
16
|
-
}
|
|
17
|
-
})
|
|
18
|
-
if (!res.body) {
|
|
19
|
-
throw new Error('Body is null')
|
|
20
|
-
}
|
|
21
|
-
const reader = res.body.getReader()
|
|
22
|
-
for (let i = 0; i < 3; i++) {
|
|
23
|
-
const { value } = await reader.read()
|
|
24
|
-
expect(value).toEqual(new Uint8Array([i]))
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('Check stream Response if aborted by client', async () => {
|
|
29
|
-
let aborted = false
|
|
30
|
-
const res = stream(c, async (stream) => {
|
|
31
|
-
stream.onAbort(() => {
|
|
32
|
-
aborted = true
|
|
33
|
-
})
|
|
34
|
-
for (let i = 0; i < 3; i++) {
|
|
35
|
-
await stream.write(new Uint8Array([i]))
|
|
36
|
-
await stream.sleep(1)
|
|
37
|
-
}
|
|
38
|
-
})
|
|
39
|
-
if (!res.body) {
|
|
40
|
-
throw new Error('Body is null')
|
|
41
|
-
}
|
|
42
|
-
const reader = res.body.getReader()
|
|
43
|
-
const { value } = await reader.read()
|
|
44
|
-
expect(value).toEqual(new Uint8Array([0]))
|
|
45
|
-
reader.cancel()
|
|
46
|
-
expect(aborted).toBeTruthy()
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('Check stream Response if aborted by abort signal', async () => {
|
|
50
|
-
// Emulate an old version of Bun (version 1.1.0) for this specific test case
|
|
51
|
-
// @ts-expect-error Bun is not typed
|
|
52
|
-
global.Bun = {
|
|
53
|
-
version: '1.1.0',
|
|
54
|
-
}
|
|
55
|
-
const ac = new AbortController()
|
|
56
|
-
const req = new Request('http://localhost/', { signal: ac.signal })
|
|
57
|
-
const c = new Context(req)
|
|
58
|
-
|
|
59
|
-
let aborted = false
|
|
60
|
-
const res = stream(c, async (stream) => {
|
|
61
|
-
stream.onAbort(() => {
|
|
62
|
-
aborted = true
|
|
63
|
-
})
|
|
64
|
-
for (let i = 0; i < 3; i++) {
|
|
65
|
-
await stream.write(new Uint8Array([i]))
|
|
66
|
-
await stream.sleep(1)
|
|
67
|
-
}
|
|
68
|
-
})
|
|
69
|
-
if (!res.body) {
|
|
70
|
-
throw new Error('Body is null')
|
|
71
|
-
}
|
|
72
|
-
const reader = res.body.getReader()
|
|
73
|
-
const { value } = await reader.read()
|
|
74
|
-
expect(value).toEqual(new Uint8Array([0]))
|
|
75
|
-
ac.abort()
|
|
76
|
-
expect(aborted).toBeTruthy()
|
|
77
|
-
// @ts-expect-error Bun is not typed
|
|
78
|
-
delete global.Bun
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it('Check stream Response if pipe is aborted by abort signal', async () => {
|
|
82
|
-
// Emulate an old version of Bun (version 1.1.0) for this specific test case
|
|
83
|
-
// @ts-expect-error Bun is not typed
|
|
84
|
-
global.Bun = {
|
|
85
|
-
version: '1.1.0',
|
|
86
|
-
}
|
|
87
|
-
const ac = new AbortController()
|
|
88
|
-
const req = new Request('http://localhost/', { signal: ac.signal })
|
|
89
|
-
const c = new Context(req)
|
|
90
|
-
|
|
91
|
-
let aborted = false
|
|
92
|
-
const res = stream(c, async (stream) => {
|
|
93
|
-
stream.onAbort(() => {
|
|
94
|
-
aborted = true
|
|
95
|
-
})
|
|
96
|
-
await stream.pipe(new ReadableStream())
|
|
97
|
-
})
|
|
98
|
-
if (!res.body) {
|
|
99
|
-
throw new Error('Body is null')
|
|
100
|
-
}
|
|
101
|
-
const reader = res.body.getReader()
|
|
102
|
-
const pReading = reader.read()
|
|
103
|
-
ac.abort()
|
|
104
|
-
await pReading
|
|
105
|
-
expect(aborted).toBeTruthy()
|
|
106
|
-
// @ts-expect-error Bun is not typed
|
|
107
|
-
delete global.Bun
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
it('Check stream Response if error occurred', async () => {
|
|
111
|
-
const onError = vi.fn()
|
|
112
|
-
const res = stream(
|
|
113
|
-
c,
|
|
114
|
-
async () => {
|
|
115
|
-
throw new Error('error')
|
|
116
|
-
},
|
|
117
|
-
onError
|
|
118
|
-
)
|
|
119
|
-
if (!res.body) {
|
|
120
|
-
throw new Error('Body is null')
|
|
121
|
-
}
|
|
122
|
-
const reader = res.body.getReader()
|
|
123
|
-
const { value } = await reader.read()
|
|
124
|
-
expect(value).toBeUndefined()
|
|
125
|
-
expect(onError).toBeCalledTimes(1)
|
|
126
|
-
expect(onError).toBeCalledWith(new Error('error'), expect.anything()) // 2nd argument is StreamingApi instance
|
|
127
|
-
})
|
|
128
|
-
})
|