You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/plugins/overview.md
+42-42Lines changed: 42 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,11 @@ head:
7
7
8
8
- - meta
9
9
- name: 'description'
10
-
content: Elysia is designed to be modular and lightweight. That's why Elysia is creating pre-built common pattern plugin for convenient usage for developers, and thanks to community plugins for customizing Elysia even further.
10
+
content: Elysia is designed to be modular and lightweight, which is why Elysia includes pre-built plugins involving common patterns for convenient developer usage. Elysia is enhanced by community plugins which customize it even further.
11
11
12
12
- - meta
13
13
- name: 'og:description'
14
-
content: Elysia is designed to be modular and lightweight. That's why Elysia is creating pre-built common pattern plugin for convenient usage for developers, and thanks to community plugins for customizing Elysia even further.
14
+
content: Elysia is designed to be modular and lightweight, which is why Elysia includes pre-built plugins involving common patterns for convenient developer usage. Elysia is enhanced by community plugins which customize it even further.
15
15
---
16
16
17
17
# Overview
@@ -21,51 +21,51 @@ Following the same idea as Arch Linux (btw, I use Arch):
21
21
22
22
> Design decisions are made on a case-by-case basis through developer consensus
23
23
24
-
To ensure that developers endup with performant web server they intent to created.
25
-
26
-
That's why Elysia is creating pre-built common pattern plugin for convinient usage for developers:
24
+
This is to ensure developers end up with a performant web server they intend to create. By extension, Elysia includes pre-built common pattern plugins for convenient developer usage:
-[CORS](/plugins/cors) - set up [Cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
29
+
-[Cron](/plugins/cron) - set up [cron](https://en.wikipedia.org/wiki/Cron) job
30
+
-[Eden](/plugins/eden/overview) - end-to-end type safety client for Elysia
31
+
-[GraphQL Apollo](/plugins/graphql-apollo) - run [Apollo GraphQL](https://www.apollographql.com/) on Elysia
32
+
-[GraphQL Yoga](/plugins/graphql-yoga) - run [GraphQL Yoga](https://github.com/dotansimha/graphql-yoga) on Elysia
33
+
-[HTML](/plugins/html) - handle HTML responses
34
+
-[JWT](/plugins/jwt) - authenticate with [JWTs](https://jwt.io/)
35
+
-[Server Timing](/plugins/server-timing) - audit performance bottlenecks with the [Server-Timing API](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing)
-[tRPC](/plugins/trpc) - support [tRPC](https://trpc.io/)
40
+
-[WebSocket](/patterns/websocket) - support [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
41
41
42
42
## Community plugins:
43
-
-[Lucia Auth](https://github.com/pilcrowOnPaper/lucia) - Authentication, simple and clean
44
-
-[Elysia Clerk](https://github.com/wobsoriano/elysia-clerk) - Unofficial Clerk plugin for ElysiaJS
45
-
-[Elysia Polyfills](https://github.com/bogeychan/elysia-polyfills) - run Elysia ecosystem on Node and Deno
46
-
-[Vite](https://github.com/timnghg/elysia-vite) - Simple Elysia plugin that helps you use Vite. It serve your entry html file with Vite's scripts injected
47
-
-[Elysia Helmet](https://github.com/DevTobias/elysia-helmet) - secure Elysia apps with various HTTP headers.
48
-
-[Vite Plugin SSR](https://github.com/timnghg/elysia-vite-plugin-ssr) - Vite Plugin SSR using Elysia server
43
+
-[Lucia Auth](https://github.com/pilcrowOnPaper/lucia) - authentication, simple and clean
-[i18n](https://github.com/eelkevdbos/elysia-i18next) - [i18n](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/i18n) wrapper based on [i18next](https://www.i18next.com/)
61
+
-[Elysia Request ID](https://github.com/gtramontina/elysia-requestid) - add/forward request IDs (`X-Request-ID` or custom)
62
+
-[Elysia HTMX](https://github.com/gtramontina/elysia-htmx) - context helpers for [HTMX](https://htmx.org/)
63
+
-[Elysia HMR HTML](https://github.com/gtrabanco/elysia-hmr-html) - reload HTML files when changing any file in a directory
64
+
-[Elysia Inject HTML](https://github.com/gtrabanco/elysia-inject-html) - inject HTML code in HTML files
65
+
-[Elysia HTTP Error](https://github.com/yfrans/elysia-http-error) - return HTTP errors from Elysia handlers
66
+
-[Elysia Http Status Code](https://github.com/sylvain12/elysia-http-status-code) - integrate HTTP status codes
-[Elysia Tailwind](https://github.com/gtramontina/elysia-tailwind) - Compile [Tailwindcss](https://tailwindcss.com/) in a plugin.
64
-
-[Elysia HMR HTML](https://github.com/gtrabanco/elysia-hmr-html) - Reload html files when change any file in a directory
65
-
-[Elysia Inject HTML](https://github.com/gtrabanco/elysia-inject-html) - So simple plugin to inject HTML code in HTML files
66
-
-[Elysia HTTP Error](https://github.com/yfrans/elysia-http-error) - Easiest way to return HTTP errors from Elysia handlers
67
-
-[Elysia Http Status Code](https://github.com/sylvain12/elysia-http-status-code) - Simple http status code plugin for Elysia.js
68
-
-[NoCache](https://github.com/gaurishhs/elysia-nocache) - A plugin for Elysia to disable caching
69
69
70
70
---
71
-
If you have plugin written for Elysia, feels free to share you plugin by creating PR to[documentation repo](https://github.com/elysiajs/documentation).
71
+
If you wrote a plugin for Elysia, feel free to share it by creating a PR on the[documentation repo](https://github.com/elysiajs/documentation).
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
+
# Server Timing Plugin
18
+
This plugin add support for auditing performance bottleneck with Server Timing API
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".
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/stream".
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/stream
23
+
```
24
+
25
+
Then use it:
26
+
```typescript
27
+
import { Elysia } from'elysia'
28
+
import { Stream } from'@elysiajs/stream'
29
+
30
+
newElysia()
31
+
.get('/', () =>newStream(async (stream) => {
32
+
stream.send('hello')
33
+
34
+
awaitstream.wait(1000)
35
+
stream.send('world')
36
+
37
+
stream.close()
38
+
}))
39
+
.listen(8080)
40
+
```
41
+
42
+
By default, `Stream` will return `Response` with `content-type` of `text/event-stream; charset=utf8`.
43
+
44
+
## Constructor
45
+
Below is the constructor parameter accept by `Stream`:
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
56
+
57
+
## Method
58
+
Below is the method provided by `Stream`:
59
+
60
+
### send
61
+
Enqueue data to stream to send back to client
62
+
63
+
### close
64
+
Close the stream
65
+
66
+
### wait
67
+
Return a promise that resolved in provided value in ms
68
+
69
+
### value
70
+
Inner value of the `ReadableStream`
71
+
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)
77
+
78
+
## OpenAI
79
+
Automatic mode is triggered when parameter is either `Iterable` or `AsyncIterable` streaming response back to client automatically.
80
+
81
+
Below is the example to integrate ChatGPT to Elysia.
82
+
83
+
```ts
84
+
newElysia()
85
+
.get(
86
+
'/ai',
87
+
({ query: { prompt } }) =>
88
+
newStream(
89
+
openai.chat.completions.create({
90
+
model: 'gpt-3.5-turbo',
91
+
stream: true,
92
+
messages: [{
93
+
role: 'user',
94
+
content: prompt
95
+
}]
96
+
})
97
+
)
98
+
)
99
+
```
100
+
101
+
By default [openai](https://npmjs.com/package/openai) chatGPT completion return `AsyncIterable` so you should be able to wrap the OpenAI in `Stream`.
102
+
103
+
## Fetch Stream
104
+
You can pass a fetch from an endpoint that returns the stream to proxy a stream.
105
+
106
+
This is useful for those endpoints that use AI text-generation since you can proxy it directly, eg. [Cloudflare AI](https://developers.cloudflare.com/workers-ai/models/llm/#examples---chat-style-with-system-prompt-preferred).
0 commit comments