Skip to content

Commit f26e6f5

Browse files
committed
Merge branch 'main' of https://github.com/elysiajs/node
2 parents 4fc78f0 + dc682f0 commit f26e6f5

3 files changed

Lines changed: 96 additions & 12 deletions

File tree

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
// based on https://tsx.is/vscode
5+
"name": "Debug file with tsx",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${file}",
9+
"runtimeExecutable": "tsx",
10+
"console": "integratedTerminal",
11+
"internalConsoleOptions": "neverOpen",
12+
"skipFiles": [
13+
"<node_internals>/**",
14+
"${workspaceFolder}/node_modules/**",
15+
],
16+
}
17+
]
18+
}

src/handler.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ export const mapResponse = (
263263
set.headers['content-type'] = 'text/plain;charset=utf8'
264264

265265
if (res) {
266-
set.headers['content-length'] = (response as string).length
266+
set.headers['content-length'] = Buffer.byteLength(
267+
response as string,
268+
'utf8'
269+
)
267270
res.writeHead(set.status!, set.headers)
268271
res.end(response)
269272
}
@@ -275,7 +278,10 @@ export const mapResponse = (
275278
response = JSON.stringify(response)
276279

277280
set.headers['content-type'] = 'application/json;charset=utf8'
278-
set.headers['content-length'] = (response as string).length
281+
set.headers['content-length'] = Buffer.byteLength(
282+
response as string,
283+
'utf8'
284+
)
279285

280286
if (res) {
281287
res.writeHead(set.status!, set.headers)
@@ -349,7 +355,10 @@ export const mapResponse = (
349355
response = JSON.stringify(response)
350356

351357
set.headers['content-type'] = 'application/json;charset=utf8'
352-
set.headers['content-length'] = (response as string)?.length
358+
set.headers['content-length'] = Buffer.byteLength(
359+
response as string,
360+
'utf8'
361+
)
353362

354363
if (res) {
355364
res.writeHead(set.status!, set.headers)
@@ -392,7 +401,10 @@ export const mapResponse = (
392401
response = (response as number | boolean).toString()
393402

394403
set.headers['content-type'] = 'text/plain;charset=utf8'
395-
set.headers['content-length'] = (response as string).length
404+
set.headers['content-length'] = Buffer.byteLength(
405+
response as string,
406+
'utf8'
407+
)
396408

397409
if (res) {
398410
res.writeHead(set.status!, set.headers)
@@ -477,7 +489,10 @@ export const mapResponse = (
477489
'application/json;charset=utf8'
478490

479491
response = JSON.stringify(response)
480-
set.headers['content-length'] = (response as string).length
492+
set.headers['content-length'] = Buffer.byteLength(
493+
response as string,
494+
'utf8'
495+
)
481496

482497
if (res) {
483498
res.writeHead(set.status!, set.headers)
@@ -489,7 +504,10 @@ export const mapResponse = (
489504
}
490505

491506
set.headers['content-type'] = 'text/plain;charset=utf8'
492-
set.headers['content-length'] = (response as string).length
507+
set.headers['content-length'] = Buffer.byteLength(
508+
response as string,
509+
'utf8'
510+
)
493511

494512
if (res) {
495513
res.writeHead(set.status!, set.headers)
@@ -520,7 +538,10 @@ export const mapEarlyResponse = (
520538
switch (response?.constructor?.name) {
521539
case 'String':
522540
set.headers['content-type'] = 'text/plain;charset=utf8'
523-
set.headers['content-length'] = (response as string).length
541+
set.headers['content-length'] = Buffer.byteLength(
542+
response as string,
543+
'utf8'
544+
)
524545

525546
if (res) {
526547
res.writeHead(set.status!, set.headers)
@@ -534,7 +555,10 @@ export const mapEarlyResponse = (
534555
response = JSON.stringify(response)
535556

536557
set.headers['content-type'] = 'application/json;charset=utf8'
537-
set.headers['content-length'] = (response as string).length
558+
set.headers['content-length'] = Buffer.byteLength(
559+
response as string,
560+
'utf8'
561+
)
538562

539563
if (res) {
540564
res.writeHead(set.status!, set.headers)
@@ -602,7 +626,10 @@ export const mapEarlyResponse = (
602626
response = JSON.stringify(response)
603627

604628
set.headers['content-type'] = 'application/json;charset=utf8'
605-
set.headers['content-length'] = (response as string).length
629+
set.headers['content-length'] = Buffer.byteLength(
630+
response as string,
631+
'utf8'
632+
)
606633

607634
return [response, set as any]
608635

@@ -640,7 +667,10 @@ export const mapEarlyResponse = (
640667
response = (response as number | boolean).toString()
641668

642669
set.headers['content-type'] = 'text/plain;charset=utf8'
643-
set.headers['content-length'] = (response as string).length
670+
set.headers['content-length'] = Buffer.byteLength(
671+
response as string,
672+
'utf8'
673+
)
644674

645675
if (res) {
646676
res.writeHead(set.status!, set.headers)
@@ -722,7 +752,10 @@ export const mapEarlyResponse = (
722752
if (!set.headers['Content-Type'])
723753
set.headers['content-type'] =
724754
'application/json;charset=utf8'
725-
set.headers['content-length'] = (response as string).length
755+
set.headers['content-length'] = Buffer.byteLength(
756+
response as string,
757+
'utf8'
758+
)
726759

727760
if (res) {
728761
res.writeHead(set.status!, set.headers)
@@ -734,7 +767,10 @@ export const mapEarlyResponse = (
734767
}
735768

736769
set.headers['content-type'] = 'text/plain;charset=utf8'
737-
set.headers['content-length'] = (response as string).length
770+
set.headers['content-length'] = Buffer.byteLength(
771+
response as string,
772+
'utf8'
773+
)
738774

739775
if (res) {
740776
res.writeHead(set.status!, set.headers)

test/core/charset.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Elysia from 'elysia'
2+
import { inject } from 'light-my-request'
3+
import { describe, it, expect } from 'vitest'
4+
5+
import node from '../../src'
6+
7+
const app = new Elysia({ adapter: node() })
8+
.get('/', () => ({ utf: 'ú' }))
9+
.compile()
10+
11+
// @ts-expect-error
12+
const handle = app._handle!
13+
14+
describe('Node - Charset', () => {
15+
it('handle UTF-8 2-byte characters', () => {
16+
inject(handle, { path: '/' }, (error, res) => {
17+
expect(error).toBeNull()
18+
19+
const expected = JSON.stringify({ utf: 'ú' })
20+
21+
expect(res?.body).toBe(expected)
22+
expect(res?.headers['content-type']).toBe(
23+
'application/json;charset=utf8'
24+
)
25+
expect(res?.headers['content-length']).toBe(
26+
new TextEncoder().encode(expected).byteLength.toString()
27+
)
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)