Skip to content

Commit 7207848

Browse files
committed
🔧 fix: websocket in guard doesn't work
1 parent 51e7f2f commit 7207848

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# 1.0.0 - 8 Mar 2024
22
Improvement:
3+
- fine-grained reactive cookie
4+
- using single source of truth for cookie
35
- macro support for websocket
46
- add `mapResolve`
5-
- add `{ as: 'global' | 'local' }` to lifecycle event
7+
- add `{ as: 'global' | 'scoped' | 'local' }` to lifecycle event
68
- add ephemeral type
79
- inline `error` to handler
810
- inline `error` has auto-completion and type checking based on status code
@@ -15,17 +17,16 @@ Improvement:
1517
- add test case for all life-cycle
1618
- resolve, mapResolve, derive, mapDerive use ephemeral type to scope down accurately
1719
- inference query dynamic variable
18-
- Fine-grained reactive cookie
19-
- Using single source of truth for cookie
2020

2121
Breaking Change:
22-
- Lifecycle is now local first
22+
- [#513](https://github.com/elysiajs/elysia/issues/513) lifecycle is now local first
2323

2424
Change:
2525
- group private API property
2626
- move `Elysia.routes` to `Elysia.router.history`
2727
- detect possible json before return
2828
- unknown response now return as-is instead of JSON.stringify()
29+
- change Elysia validation error to JSON instead of string
2930

3031
Bug fix:
3132
- [#466](https://github.com/elysiajs/elysia/issues/466) Async Derive leaks request context to other requests if `aot: true`

example/a.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { Elysia, t } from '../src'
22
import { cors } from '../../cors/src'
33

44
const app = new Elysia({ precompile: true })
5-
.ws('/ws', {
6-
open(ws) {
7-
ws.send('Hi')
8-
}
9-
})
10-
.ws('/ws/:id', {
11-
open(ws) {
12-
ws.send('Hi')
5+
.guard(
6+
{
7+
query: t.Object({
8+
__omit: t.ObjectString({
9+
'x-id': t.Numeric(),
10+
'x-token': t.String({ minLength: 150 })
11+
})
12+
})
1313
},
14-
message(ws, data) {
15-
ws.send(data)
16-
}
17-
})
14+
(app) =>
15+
app.ws('/v2/socket/user', {
16+
open(ws) {
17+
ws.send('test')
18+
}
19+
})
20+
)
1821
.listen(3000)

example/stress/instance.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import { Elysia, t } from '../../src'
22

33
const total = 10_000
44

5-
const setup = new Elysia({ name: 'setup' })
6-
.decorate('decorate', 'decorate')
7-
.state('state', 'state')
8-
.model('model', t.String())
9-
.error('error', Error)
5+
const setup = () => new Elysia()
106

117
const t1 = performance.now()
12-
13-
for (let i = 0; i < total; i++) new Elysia().use(setup)
8+
for (let i = 0; i < total; i++) setup()
149

1510
const took = performance.now() - t1
1611

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.0.0-rc.16",
4+
"version": "1.0.0-rc.17",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,9 @@ export default class Elysia<
23152315
this.singleton = mergeDeep(this.singleton, instance.singleton) as any
23162316
this.definitions = mergeDeep(this.definitions, instance.definitions)
23172317

2318+
// ? Inject getServer for websocket and trace (important, do not remove)
2319+
sandbox.getServer = () => this.server
2320+
23182321
if (sandbox.event.request.length)
23192322
this.event.request = [
23202323
...(this.event.request || []),
@@ -3999,8 +4002,9 @@ export default class Elysia<
39994002
this.route(
40004003
'$INTERNALWS',
40014004
path as any,
4002-
// @ts-ignore
4005+
// @ts-expect-error
40034006
(context) => {
4007+
// ! Enable static code analysis just in case resolveUnknownFunction doesn't work, do not remove
40044008
// eslint-disable-next-line @typescript-eslint/no-unused-vars
40054009
const { set, path, qi, headers, query, params } = context
40064010

0 commit comments

Comments
 (0)