Skip to content

Commit e7dfe29

Browse files
committed
🔧 fix: #907, #872, #926, #929, #912
1 parent 5ff7410 commit e7dfe29

File tree

6 files changed

+88
-39
lines changed

6 files changed

+88
-39
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.1.26 - 4 Dev 2024
2+
Bug fix:
3+
- [#907](https://github.com/elysiajs/elysia/issues/907), [#872](https://github.com/elysiajs/elysia/issues/872), [#926](https://github.com/elysiajs/elysia/issues/926) BooleanString is not behave correctly if property is not provided
4+
- [#929](https://github.com/elysiajs/elysia/issues/929) Non-ASCII characters cause querystring index to be incorrectly slice
5+
- [#912](https://github.com/elysiajs/elysia/issues/912) handle JavaScript date numeric offset
6+
17
# 1.1.25 - 14 Nov 2024
28
Bug fix:
39
- [#908](https://github.com/elysiajs/elysia/pull/908) boolean-string converted to string

example/a.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { Elysia, t } from '../src'
2+
import { req } from '../test/utils'
23

3-
const app = new Elysia()
4-
.derive(({ cookie: { test } }) => {
5-
if (!test.value) {
6-
test.value = 'Hello, world!'
7-
}
8-
9-
return {}
4+
const api = new Elysia().get('/', ({ query }) => query, {
5+
query: t.Object({
6+
date: t.Date()
107
})
11-
.get('/', () => 'Hello, world!')
8+
})
129

13-
app.handle(
14-
new Request('http://localhost:3000/', {
15-
headers: {
16-
cookie: 'test=Hello, world!'
17-
}
18-
})
19-
)
20-
.then((x) => x.headers)
21-
.then(console.log)
10+
api.handle(req(`/?date=${Date.now()}`)).then(x => x.json()).then(console.log)
11+
12+
// const app = new Elysia()
13+
// .get('/', () => 'ok', {
14+
// query: t.Object({
15+
// key1: t.Union([t.Array(t.String()), t.String()])
16+
// })
17+
// })
18+
19+
// app.handle(req('/?key1=ab&key1=cd&z=が'))
20+
// .then((x) => x.status)
21+
// .then(console.log)

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.1.25",
4+
"version": "1.1.26",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/compose.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ export const composeHandler = ({
694694
}`
695695
} else {
696696
fnLiteral += `if(c.qi !== -1) {
697-
let url = '&' + c.url.slice(c.qi + 1)
697+
let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))
698698
699699
${destructured
700700
.map(
@@ -728,8 +728,8 @@ export const composeHandler = ({
728728
729729
let temp
730730
731-
if(memory === -1) temp = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
732-
else temp = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
731+
if(memory === -1) temp = url.slice(start).replace(/\\+|%20/g, ' ')
732+
else temp = url.slice(start, memory).replace(/\\+|%20/g, ' ')
733733
734734
const charCode = temp.charCodeAt(0)
735735
if(charCode !== 91 && charCode !== 123)
@@ -757,10 +757,10 @@ export const composeHandler = ({
757757
a${index} = []
758758
759759
if(memory === -1) {
760-
a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+/g, ' '))
760+
a${index}.push(url.slice(start).replace(/\\+|%20/g, ' '))
761761
break
762762
}
763-
else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+/g, ' '))
763+
else a${index}.push(url.slice(start, memory).replace(/\\+|%20/g, ' '))
764764
765765
memory = url.indexOf('&${key}=', memory)
766766
if(memory === -1) break
@@ -774,8 +774,8 @@ export const composeHandler = ({
774774
const start = memory + ${key.length + 2}
775775
memory = url.indexOf('&', start)
776776
777-
if(memory === -1) a${index} = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
778-
else a${index} = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
777+
if(memory === -1) a${index} = url.slice(start).replace(/\\+|%20/g, ' ')
778+
else a${index} = url.slice(start, memory).replace(/\\+|%20/g, ' ')
779779
780780
if (a${index} !== undefined) {
781781
try {
@@ -792,9 +792,9 @@ export const composeHandler = ({
792792
const start = memory + ${key.length + 2}
793793
memory = url.indexOf('&', start)
794794
795-
if(memory === -1) a${index} = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
795+
if(memory === -1) a${index} = url.slice(start).replace(/\\+|%20/g, ' ')
796796
else {
797-
a${index} = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
797+
a${index} = url.slice(start, memory).replace(/\\+|%20/g, ' ')
798798
799799
${
800800
anyOf
@@ -813,8 +813,8 @@ export const composeHandler = ({
813813
deepMemory = url.indexOf('&', start)
814814
815815
let value
816-
if(deepMemory === -1) value = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
817-
else value = decodeURIComponent(url.slice(start, deepMemory).replace(/\\+/g, ' '))
816+
if(deepMemory === -1) value = url.slice(start).replace(/\\+|%20/g, ' ')
817+
else value = url.slice(start, deepMemory).replace(/\\+|%20/g, ' ')
818818
819819
const vStart = value.charCodeAt(0)
820820
const vEnd = value.charCodeAt(value.length - 1)

src/type-system.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,22 @@ export const ElysiaType = {
369369
t.String({
370370
format: 'date-time',
371371
default: new Date().toISOString()
372-
})
372+
}),
373+
t.Number()
373374
],
374375
property
375376
)
376377
)
377378
.Decode((value) => {
379+
if(typeof value === "number") {
380+
const date = new Date(value)
381+
382+
if (!Value.Check(schema, date))
383+
throw new ValidationError('property', schema, date)
384+
385+
return date
386+
}
387+
378388
if (value instanceof Date) return value
379389

380390
const date = new Date(value)

test/validator/query.test.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('Query Validator', () => {
203203
it('parse optional boolean string with second parameter', async () => {
204204
const schema = t.Object({
205205
registered: t.Optional(t.Boolean()),
206-
other: t.String(),
206+
other: t.String()
207207
})
208208
const app = new Elysia().get('/', ({ query }) => query, {
209209
query: schema
@@ -216,8 +216,8 @@ describe('Query Validator', () => {
216216

217217
it('parse optional boolean string with default value', async () => {
218218
const schema = t.Object({
219-
registered: t.Optional(t.Boolean({default: true})),
220-
other: t.String(),
219+
registered: t.Optional(t.Boolean({ default: true })),
220+
other: t.String()
221221
})
222222
const app = new Elysia().get('/', ({ query }) => query, {
223223
query: schema
@@ -682,12 +682,11 @@ describe('Query Validator', () => {
682682
})
683683

684684
it('parse + in query', async () => {
685-
const api = new Elysia()
686-
.get('', ({ query }) => query, {
687-
query: t.Object({
688-
keyword: t.String()
689-
})
685+
const api = new Elysia().get('', ({ query }) => query, {
686+
query: t.Object({
687+
keyword: t.String()
690688
})
689+
})
691690

692691
const url = new URL('http://localhost:3000/')
693692
url.searchParams.append('keyword', 'hello world')
@@ -698,7 +697,41 @@ describe('Query Validator', () => {
698697
.then((response) => response.json())
699698

700699
expect(result).toEqual({
701-
'keyword': 'hello world'
700+
keyword: 'hello world'
701+
})
702+
})
703+
704+
// https://github.com/elysiajs/elysia/issues/929
705+
it('slice non-ASCII querystring offset correctly', async () => {
706+
const app = new Elysia().get('/', () => 'ok', {
707+
query: t.Object({
708+
key1: t.Union([t.Array(t.String()), t.String()])
709+
})
702710
})
711+
712+
const response = await Promise.all(
713+
[
714+
'/?key1=ab&key1=cd&z=が',
715+
'/?key1=ab&z=が',
716+
'/?key1=ab&key1=cd&z=x',
717+
'/?z=が&key1=ab&key1=cd',
718+
'/?key1=で&key1=が&z=x'
719+
].map((path) => app.handle(req(path)).then((x) => x.status))
720+
).then((responses) => responses.every((status) => status === 200))
721+
722+
expect(response).toBeTrue()
723+
})
724+
725+
// https://github.com/elysiajs/elysia/issues/912
726+
it('handle JavaScript date numeric offset', () => {
727+
const api = new Elysia().get('/', ({ query }) => query, {
728+
query: t.Object({
729+
date: t.Date()
730+
})
731+
})
732+
733+
api.handle(req(`/?date=${Date.now()}`))
734+
.then((x) => x.json())
735+
.then(console.log)
703736
})
704737
})

0 commit comments

Comments
 (0)