Skip to content

Commit 306234a

Browse files
committed
🔧 fix: #14
1 parent ff76f8e commit 306234a

File tree

7 files changed

+759
-32
lines changed

7 files changed

+759
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.2.4 - 9 Jan 2024
2+
Bug fix:
3+
- Support Elysia 1.2.11
4+
- [#14](https://github.com/elysiajs/node/issues/14) ReadableStream has already been used if request is reference multiple time
5+
16
# 1.2.3 - 27 Dec 2024
27
Bug fix:
38
- async module doesn't load (eg. @elysiajs/swagger)

bun.lock

Lines changed: 667 additions & 0 deletions
Large diffs are not rendered by default.

bun.lockb

-120 KB
Binary file not shown.

example/c.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { Elysia } from 'elysia'
22
import { node } from '../src'
33

4-
new Elysia({ adapter: node() })
5-
.ws('/', {
6-
upgrade({ set }) {
7-
set.headers['a'] = 'b'
8-
},
9-
open(ws) {
10-
ws.subscribe('topic')
11-
ws.publish('topic', 'Hello')
12-
},
13-
message(ws, message) {
14-
ws.publish('topic', message)
4+
const plugin = new Elysia({ prefix: '/api/v1' }).post(
5+
'/',
6+
async ({ body, store, error, request }) => {
7+
return {
8+
message: 'Hello World'
159
}
10+
}
11+
)
12+
13+
const app = new Elysia({
14+
adapter: node()
15+
})
16+
.use(plugin)
17+
.listen(8000, ({ port }) => {
18+
console.log(`Server is running on http://localhost:${port}`)
1619
})
17-
.listen(3000)
20+
21+
// console.log(app._handle.toString())
22+
// console.log(app.routes[0].compile().toString())

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/node",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "Plugin for Elysia for retreiving Bearer token",
55
"license": "MIT",
66
"scripts": {
@@ -31,7 +31,7 @@
3131
"@types/formidable": "^3.4.5",
3232
"@types/node": "^22.10.2",
3333
"@types/ws": "^8.5.13",
34-
"elysia": "1.2.7",
34+
"elysia": "1.2.10",
3535
"eslint": "9.17.0",
3636
"light-my-request": "^6.4.0",
3737
"tsup": "^8.3.5",

src/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ export const node = () => {
7676
mapCompactResponse
7777
},
7878
composeHandler: {
79-
declare(inference) {
80-
if (inference.request)
81-
return (
82-
`Object.defineProperty(c,'request',{` +
83-
`get(){` +
84-
`return nodeRequestToWebstand(c[ElysiaNodeContext].req)` +
85-
`}` +
86-
`})\n`
87-
)
88-
},
79+
// declare(inference) {
80+
// if (inference.request)
81+
// return (
82+
// `Object.defineProperty(c,'request',{` +
83+
// `get(){` +
84+
// `return nodeRequestToWebstand(c[ElysiaNodeContext].req)` +
85+
// `}` +
86+
// `})\n`
87+
// )
88+
// },
8989
mapResponseContext: 'c[ElysiaNodeContext].res',
9090
headers: `c.headers=c[ElysiaNodeContext].req.headers\n`,
9191
inject: {
@@ -201,7 +201,7 @@ export const node = () => {
201201
for (const key of Object.keys(app.singleton.decorator))
202202
decoratorsLiteral += `,${key}: decorator['${key}']`
203203

204-
const hasTrace = app.event.trace.length > 0
204+
const hasTrace = !!app.event.trace?.length
205205

206206
if (hasTrace) fnLiteral += `const id=randomId()\n`
207207

@@ -313,7 +313,7 @@ export const node = () => {
313313
hooks: lifecycle,
314314
websocket: options
315315
})
316-
app.router.ws.history.push(['$INTERNALWS', path, options])
316+
app.router.http.history.push(['$INTERNALWS', path, options])
317317
},
318318
listen(app) {
319319
return (options, callback) => {
@@ -445,7 +445,7 @@ export const node = () => {
445445
try {
446446
serverInfo.reload(
447447
typeof options === 'object'
448-
? options as any
448+
? (options as any)
449449
: {
450450
port: options
451451
}
@@ -455,20 +455,25 @@ export const node = () => {
455455
}
456456
)
457457

458+
// @ts-ignore
459+
app.router.http.build?.()
460+
458461
if (
459462
isNotEmpty(app.router.static.ws) ||
460-
app.router.ws.history.length
463+
app.router.http.history.find((x) => x[0] === 'ws')
461464
)
462465
attachWebSocket(app, server)
463466

464-
for (let i = 0; i < app.event.start.length; i++)
465-
app.event.start[i].fn(this)
467+
if (app.event.start)
468+
for (let i = 0; i < app.event.start.length; i++)
469+
app.event.start[i].fn(this)
466470

467471
process.on('beforeExit', () => {
468472
server.close()
469473

470-
for (let i = 0; i < app.event.stop.length; i++)
471-
app.event.stop[i].fn(this)
474+
if (app.event.stop)
475+
for (let i = 0; i < app.event.stop.length; i++)
476+
app.event.stop[i].fn(this)
472477
})
473478
}
474479
}

test/core/basic.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,49 @@ describe('Node - Core', () => {
5353
}
5454
)
5555
})
56+
57+
it('handle plugin', () => {
58+
const plugin = new Elysia({ prefix: '/api/v1' }).post(
59+
'/',
60+
async ({ request }) => {
61+
return {
62+
message: 'Yugiri'
63+
}
64+
},
65+
{
66+
beforeHandle({ request }) {
67+
request
68+
}
69+
}
70+
)
71+
72+
const app = new Elysia({
73+
adapter: node()
74+
})
75+
.use(plugin)
76+
.listen(8000, ({ port }) => {
77+
console.log(`Server is running on http://localhost:${port}`)
78+
})
79+
80+
inject(
81+
// @ts-ignore
82+
app._handle,
83+
{
84+
path: '/api/v1',
85+
method: 'POST',
86+
headers: {
87+
'Content-Type': 'application/json'
88+
},
89+
body: JSON.stringify({ message: 'Hello Yugiri' })
90+
},
91+
(error, res) => {
92+
expect(error).toBeNull()
93+
94+
expect(res?.body).toBe(JSON.stringify({ message: 'Yugiri' }))
95+
expect(res?.headers['content-type']).toBe(
96+
'application/json;charset=utf8'
97+
)
98+
}
99+
)
100+
})
56101
})

0 commit comments

Comments
 (0)