Skip to content

Commit e3b19ee

Browse files
committed
📘 doc(plugin/stream): add event and retry
1 parent d6c70f7 commit e3b19ee

File tree

5 files changed

+113
-9
lines changed

5 files changed

+113
-9
lines changed

docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ export default defineConfig({
290290
text: 'JWT',
291291
link: '/plugins/jwt'
292292
},
293+
{
294+
text: 'Server Timing',
295+
link: '/plugins/server-timing'
296+
},
293297
{
294298
text: 'Static',
295299
link: '/plugins/static'

docs/plugins/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ That's why Elysia is creating pre-built common pattern plugin for convinient usa
3434
- [GraphQL Yoga](/plugins/graphql-yoga) - run GraphQL Yoga on Elysia
3535
- [HTML](/plugins/html) - convenient plugin for handling HTML response
3636
- [JWT](/plugins/jwt) - authentication with JWT
37+
- [Server Timing](/plugins/server-timing) - audit performance bottleneck with Server Timing API
3738
- [Static](/plugins/static) - serve static file/folders
3839
- [Stream](/plugins/stream) - response streaming, and server sent event
3940
- [Swagger](/plugins/swagger) - generate Swagger documentation in 1 line

docs/plugins/server-timing.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Server Timing Plugin - ElysiaJS
3+
head:
4+
- - meta
5+
- property: 'og:title'
6+
content: Server Timing Plugin - ElysiaJS
7+
8+
- - meta
9+
- name: 'description'
10+
content: Plugin for Elysia for performance audit via Server Timing API. Start by installing the plugin with "bun add @elysiajs/server-timing".
11+
12+
- - meta
13+
- name: 'og:description'
14+
content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/server-timing".
15+
---
16+
17+
# Stream Plugin
18+
This plugin add support for streaming response or sending Server Sent Event back to client.
19+
20+
Install with:
21+
```bash
22+
bun add @elysiajs/server-timing
23+
```
24+
25+
Then use it:
26+
```typescript
27+
import { Elysia } from 'elysia'
28+
import { serverTiming } from '@elysiajs/server-timing'
29+
30+
new Elysia()
31+
.use(serverTiming())
32+
.get('/', () => 'hello')
33+
.listen(8080)
34+
```
35+
36+
Server Timing then will append header 'Server-Timing' with log duration, function name and detail for each life-cycle function.
37+
38+
To inspect, open browser developer tools > Network > [Request made through Elysia server] > Timing.
39+
40+
Then inspect the Server Timing of your server
41+
42+
## Config
43+
Below is a config which is accepted by the plugin
44+
45+
### enabled
46+
@default `NODE_ENV !== 'production'`
47+
48+
Determine whether or not Server Timing should be enabled
49+
50+
### allow
51+
@default `undefined`
52+
53+
A condition whether server timing should be log
54+
55+
### trace
56+
@default `undefined`
57+
58+
Allow Server Timing to log specified life-cycle events:
59+
60+
Trace accepts object of the following:
61+
- request: capture duration from request
62+
- parse: capture duration from parse
63+
- transform: capture duration from transform
64+
- beforeHandle: capture duration from beforeHandle
65+
- handle: capture duration from handle
66+
- afterHandle: capture duration from afterHandle
67+
- total: capture total duration from start to finish
68+
69+
## Pattern
70+
Below you can find the common patterns to use the plugin.
71+
72+
- [Allow Condition](#allow-condition)
73+
74+
## Allow Condition
75+
You may disabled Server Timing on specific route via `allow` property
76+
77+
```ts
78+
import { Elysia } from 'elysia'
79+
import { serverTiming } from '@elysiajs/server-timing'
80+
81+
new Elysia()
82+
.use(
83+
serverTiming({
84+
allow: ({ request }) => {
85+
return new URL(request.url).pathname !== '/no-trace'
86+
}
87+
})
88+
)
89+
```

docs/plugins/static.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Set response headers of files
8585
## Pattern
8686
Below you can find the common patterns to use the plugin.
8787

88+
- [Single File](#single-file)
89+
8890
## Single file
8991
Suppose you want to return just a single file, you can use `Bun.file` instead of using static plugin
9092
```typescript

docs/plugins/stream.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ head:
77

88
- - meta
99
- name: 'description'
10-
content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/static".
10+
content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/stream".
1111

1212
- - meta
1313
- name: 'og:description'
14-
content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/static".
14+
content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/stream".
1515
---
1616

1717
# Stream Plugin
@@ -43,12 +43,16 @@ By default, `Stream` will return `Response` with `content-type` of `text/event-s
4343

4444
## Constructor
4545
Below is the constructor parameter accept by `Stream`:
46-
- Automatic:
47-
- Iterable
48-
- AsyncIterable
49-
- ReadableStream
50-
- Response
51-
- Manual: Callback of `(stream: this) => unknown` or `undefined`
46+
1. Stream:
47+
- Automatic: Automatically stream response from provided value
48+
- Iterable
49+
- AsyncIterable
50+
- ReadableStream
51+
- Response
52+
- Manual: Callback of `(stream: this) => unknown` or `undefined`
53+
2. Options: `StreamOptions`
54+
- [event](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event): A string identifying the type of event described
55+
- [retry](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#retry): The reconnection time in milliseconds
5256

5357
## Method
5458
Below is the method provided by `Stream`:
@@ -65,7 +69,11 @@ Return a promise that resolved in provided value in ms
6569
### value
6670
Inner value of the `ReadableStream`
6771

68-
# Example
72+
## Pattern
73+
Below you can find the common patterns to use the plugin.
74+
- [OpenAI](#openai)
75+
- [Fetch Stream](#fetch-stream)
76+
- [Server Sent Event](#server-sent-event)
6977

7078
## OpenAI
7179
Automatic mode is triggered when parameter is either `Iterable` or `AsyncIterable` streaming response back to client automatically.

0 commit comments

Comments
 (0)