Skip to content

Commit

Permalink
🔧 fix: #907, #872, #926, #929, #912
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 4, 2024
1 parent 5ff7410 commit e7dfe29
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.1.26 - 4 Dev 2024
Bug fix:
- [#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
- [#929](https://github.com/elysiajs/elysia/issues/929) Non-ASCII characters cause querystring index to be incorrectly slice
- [#912](https://github.com/elysiajs/elysia/issues/912) handle JavaScript date numeric offset

# 1.1.25 - 14 Nov 2024
Bug fix:
- [#908](https://github.com/elysiajs/elysia/pull/908) boolean-string converted to string
Expand Down
34 changes: 17 additions & 17 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Elysia, t } from '../src'
import { req } from '../test/utils'

const app = new Elysia()
.derive(({ cookie: { test } }) => {
if (!test.value) {
test.value = 'Hello, world!'
}

return {}
const api = new Elysia().get('/', ({ query }) => query, {
query: t.Object({
date: t.Date()
})
.get('/', () => 'Hello, world!')
})

app.handle(
new Request('http://localhost:3000/', {
headers: {
cookie: 'test=Hello, world!'
}
})
)
.then((x) => x.headers)
.then(console.log)
api.handle(req(`/?date=${Date.now()}`)).then(x => x.json()).then(console.log)

// const app = new Elysia()
// .get('/', () => 'ok', {
// query: t.Object({
// key1: t.Union([t.Array(t.String()), t.String()])
// })
// })

// app.handle(req('/?key1=ab&key1=cd&z=が'))
// .then((x) => x.status)
// .then(console.log)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.1.25",
"version": "1.1.26",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
22 changes: 11 additions & 11 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export const composeHandler = ({
}`
} else {
fnLiteral += `if(c.qi !== -1) {
let url = '&' + c.url.slice(c.qi + 1)
let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))
${destructured
.map(
Expand Down Expand Up @@ -728,8 +728,8 @@ export const composeHandler = ({
let temp
if(memory === -1) temp = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else temp = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
if(memory === -1) temp = url.slice(start).replace(/\\+|%20/g, ' ')
else temp = url.slice(start, memory).replace(/\\+|%20/g, ' ')
const charCode = temp.charCodeAt(0)
if(charCode !== 91 && charCode !== 123)
Expand Down Expand Up @@ -757,10 +757,10 @@ export const composeHandler = ({
a${index} = []
if(memory === -1) {
a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+/g, ' '))
a${index}.push(url.slice(start).replace(/\\+|%20/g, ' '))
break
}
else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+/g, ' '))
else a${index}.push(url.slice(start, memory).replace(/\\+|%20/g, ' '))
memory = url.indexOf('&${key}=', memory)
if(memory === -1) break
Expand All @@ -774,8 +774,8 @@ export const composeHandler = ({
const start = memory + ${key.length + 2}
memory = url.indexOf('&', start)
if(memory === -1) a${index} = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else a${index} = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
if(memory === -1) a${index} = url.slice(start).replace(/\\+|%20/g, ' ')
else a${index} = url.slice(start, memory).replace(/\\+|%20/g, ' ')
if (a${index} !== undefined) {
try {
Expand All @@ -792,9 +792,9 @@ export const composeHandler = ({
const start = memory + ${key.length + 2}
memory = url.indexOf('&', start)
if(memory === -1) a${index} = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
if(memory === -1) a${index} = url.slice(start).replace(/\\+|%20/g, ' ')
else {
a${index} = decodeURIComponent(url.slice(start, memory).replace(/\\+/g, ' '))
a${index} = url.slice(start, memory).replace(/\\+|%20/g, ' ')
${
anyOf
Expand All @@ -813,8 +813,8 @@ export const composeHandler = ({
deepMemory = url.indexOf('&', start)
let value
if(deepMemory === -1) value = decodeURIComponent(url.slice(start).replace(/\\+/g, ' '))
else value = decodeURIComponent(url.slice(start, deepMemory).replace(/\\+/g, ' '))
if(deepMemory === -1) value = url.slice(start).replace(/\\+|%20/g, ' ')
else value = url.slice(start, deepMemory).replace(/\\+|%20/g, ' ')
const vStart = value.charCodeAt(0)
const vEnd = value.charCodeAt(value.length - 1)
Expand Down
12 changes: 11 additions & 1 deletion src/type-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,22 @@ export const ElysiaType = {
t.String({
format: 'date-time',
default: new Date().toISOString()
})
}),
t.Number()
],
property
)
)
.Decode((value) => {
if(typeof value === "number") {
const date = new Date(value)

if (!Value.Check(schema, date))
throw new ValidationError('property', schema, date)

return date
}

if (value instanceof Date) return value

const date = new Date(value)
Expand Down
51 changes: 42 additions & 9 deletions test/validator/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('Query Validator', () => {
it('parse optional boolean string with second parameter', async () => {
const schema = t.Object({
registered: t.Optional(t.Boolean()),
other: t.String(),
other: t.String()
})
const app = new Elysia().get('/', ({ query }) => query, {
query: schema
Expand All @@ -216,8 +216,8 @@ describe('Query Validator', () => {

it('parse optional boolean string with default value', async () => {
const schema = t.Object({
registered: t.Optional(t.Boolean({default: true})),
other: t.String(),
registered: t.Optional(t.Boolean({ default: true })),
other: t.String()
})
const app = new Elysia().get('/', ({ query }) => query, {
query: schema
Expand Down Expand Up @@ -682,12 +682,11 @@ describe('Query Validator', () => {
})

it('parse + in query', async () => {
const api = new Elysia()
.get('', ({ query }) => query, {
query: t.Object({
keyword: t.String()
})
const api = new Elysia().get('', ({ query }) => query, {
query: t.Object({
keyword: t.String()
})
})

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

expect(result).toEqual({
'keyword': 'hello world'
keyword: 'hello world'
})
})

// https://github.com/elysiajs/elysia/issues/929
it('slice non-ASCII querystring offset correctly', async () => {
const app = new Elysia().get('/', () => 'ok', {
query: t.Object({
key1: t.Union([t.Array(t.String()), t.String()])
})
})

const response = await Promise.all(
[
'/?key1=ab&key1=cd&z=が',
'/?key1=ab&z=が',
'/?key1=ab&key1=cd&z=x',
'/?z=が&key1=ab&key1=cd',
'/?key1=で&key1=が&z=x'
].map((path) => app.handle(req(path)).then((x) => x.status))
).then((responses) => responses.every((status) => status === 200))

expect(response).toBeTrue()
})

// https://github.com/elysiajs/elysia/issues/912
it('handle JavaScript date numeric offset', () => {
const api = new Elysia().get('/', ({ query }) => query, {
query: t.Object({
date: t.Date()
})
})

api.handle(req(`/?date=${Date.now()}`))
.then((x) => x.json())
.then(console.log)
})
})

0 comments on commit e7dfe29

Please sign in to comment.