Skip to content

Commit 86c660d

Browse files
committed
🔧 fix: check if body is not locked
1 parent 2151d60 commit 86c660d

File tree

5 files changed

+80
-45
lines changed

5 files changed

+80
-45
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.5 - 1 Feb 2025
2+
Bug fix:
3+
- [#23](https://github.com/elysiajs/node/issues/23) Response body object should not be disturbed or locked
4+
15
# 1.2.4 - 1 Feb 2025
26
Change:
37
- Support Elysia 1.2.11

bun.lock

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@types/formidable": "^3.4.5",
1414
"@types/node": "^22.10.2",
1515
"@types/ws": "^8.5.13",
16-
"elysia": "1.2.10",
16+
"elysia": "^1.2.11",
1717
"eslint": "9.17.0",
1818
"light-my-request": "^6.4.0",
1919
"tsup": "^8.3.5",
@@ -24,12 +24,11 @@
2424
"peerDependencies": {
2525
"bufferutil": ">= 4.0.1",
2626
"elysia": ">= 1.2.7",
27-
"bufferutil": ">= 4.0.1",
28-
"elysia": ">= 1.2.7",
27+
"formidable": ">= 3.5.2",
28+
"ws": ">= 8.18.0",
2929
},
3030
"optionalPeers": [
3131
"bufferutil",
32-
"bufferutil",
3332
],
3433
},
3534
},

example/c.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
import { Elysia, t } from 'elysia'
2-
import { node } from '../src'
3-
import cors from '@elysiajs/cors'
2+
import { ElysiaNodeContext, node } from '../src'
43

5-
const app = new Elysia({ adapter: node() })
6-
.use((app) =>
7-
app
8-
.derive({ as: 'global' }, async ({ cookie }) => {})
9-
.onAfterHandle({ as: 'global' }, async ({ response }) => {
10-
console.log('onAfterHandle', response)
11-
// await commitSession();
12-
return response
4+
const app = new Elysia({
5+
adapter: node()
6+
})
7+
.onError(({ request }) => {
8+
return
9+
})
10+
.post(
11+
'/',
12+
({ body }) => {
13+
console.log({ body })
14+
15+
return 'OK'
16+
},
17+
{
18+
body: t.Object({
19+
test: t.String()
1320
})
21+
}
1422
)
15-
.get('/', () => 'Hello World')
1623
.listen(3000)
1724

18-
console.log(app._handle)
25+
// console.log(app._handle.toString())
26+
// console.log(app.routes[0].compile().toString())
27+
// console.log(app.handleError.toString())
28+
29+
const main = async () => {
30+
const res = await fetch('http://localhost:3000/', {
31+
method: 'POST',
32+
headers: {
33+
'Content-Type': 'application/json'
34+
},
35+
body: JSON.stringify({ bad: '' })
36+
})
37+
38+
console.log('status', res.status)
39+
40+
// app.stop(true)
41+
}
42+
43+
main()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.10",
34+
"elysia": "^1.2.11",
3535
"eslint": "9.17.0",
3636
"light-my-request": "^6.4.0",
3737
"tsup": "^8.3.5",

src/index.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ export const nodeRequestToWebstand = (
4141
abortController?: AbortController
4242
) => {
4343
let _signal: AbortSignal
44+
let _body: unknown
4445

4546
return new Request(getUrl(req), {
4647
method: req.method,
4748
headers: req.headers as Record<string, string>,
4849
get body() {
4950
if (req.method === 'GET' || req.method === 'HEAD') return null
51+
if (_body !== undefined) return _body
52+
if (req.readable) return (_body = Readable.toWeb(req) as any)
5053

51-
return Readable.toWeb(req) as any
54+
return null
5255
},
5356
get signal() {
5457
if (_signal) return _signal
@@ -76,16 +79,20 @@ export const node = () => {
7679
mapCompactResponse
7780
},
7881
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-
// },
82+
declare(inference) {
83+
if (inference.request || inference.request)
84+
return (
85+
`if(!('request' in c)){` +
86+
`let _request\n` +
87+
`Object.defineProperty(c,'request',{` +
88+
`get(){` +
89+
`if(_request)return _request\n` +
90+
`return _request=nodeRequestToWebstand(c[ElysiaNodeContext].req)` +
91+
`}` +
92+
`})` +
93+
'}\n'
94+
)
95+
},
8996
mapResponseContext: 'c[ElysiaNodeContext].res',
9097
headers: `c.headers=c[ElysiaNodeContext].req.headers\n`,
9198
inject: {
@@ -208,12 +215,12 @@ export const node = () => {
208215
fnLiteral += `let _request\n` + `const c={`
209216

210217
// @ts-ignore protected
211-
if (app.inference.request || app.inference.cookie)
212-
fnLiteral +=
213-
`get request(){` +
214-
`if(_request)return _request\n` +
215-
`return _request = nodeRequestToWebstand(r)` +
216-
`},`
218+
// if (app.inference.request || app.inference.cookie)
219+
// fnLiteral +=
220+
// `get request(){` +
221+
// `if(_request)return _request\n` +
222+
// `return _request = nodeRequestToWebstand(r)` +
223+
// `},`
217224

218225
fnLiteral +=
219226
`store,` +
@@ -348,10 +355,10 @@ export const node = () => {
348355
typeof options === 'number'
349356
? options
350357
: {
351-
...options,
352-
// @ts-ignore
353-
host: options?.hostname
354-
},
358+
...options,
359+
// @ts-ignore
360+
host: options?.hostname
361+
},
355362
() => {
356363
const address = server.address()
357364
const hostname =
@@ -408,10 +415,10 @@ export const node = () => {
408415
typeof options === 'number'
409416
? options
410417
: {
411-
...options,
412-
// @ts-ignore
413-
host: options?.hostname
414-
}
418+
...options,
419+
// @ts-ignore
420+
host: options?.hostname
421+
}
415422
)
416423
},
417424
requestIP() {
@@ -447,10 +454,10 @@ export const node = () => {
447454
typeof options === 'object'
448455
? (options as any)
449456
: {
450-
port: options
451-
}
457+
port: options
458+
}
452459
)
453-
} catch { }
460+
} catch {}
454461
})
455462
}
456463
)

0 commit comments

Comments
 (0)