Skip to content

Commit

Permalink
🧹 chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 4, 2024
2 parents 0797c3f + e7dfe29 commit 5dd1ee3
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 55 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
Binary file modified bun.lockb
Binary file not shown.
36 changes: 3 additions & 33 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
import { Value } from '@sinclair/typebox/value'
import { Elysia, t } from '../src'
import { req } from '../test/utils'

new Request('', {
duplex: 'half',
})

const app = new Elysia().get(
'/',
() => {
return {
duration: 200
}
},
{
response: {
200: t.Object({
duration: t.Number()
}),
400: t.Object({
stuff: t.Number()
})
},
afterResponse({ response }) {
// expectTypeOf<typeof response>().toEqualTypeOf<
// | {
// duration: number
// }
// | {
// stuff: number
// }
// >()
// return undefined as any
}
}
)
console.log(Value.Create(t.Date()) instanceof Date)
22 changes: 11 additions & 11 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export const composeHandler = ({
'}'
} else {
fnLiteral +=
'if(c.qi!==-1){' + `let url = '&' + c.url.slice(c.qi + 1)\n`
'if(c.qi!==-1){' + `let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))\n`

let index = 0
for (const {
Expand Down Expand Up @@ -727,8 +727,8 @@ export const composeHandler = ({
`else\n` +
`a${index}+=','\n` +
`let temp\n` +
`if(memory===-1)temp=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` +
`else temp=decodeURIComponent(url.slice(start, memory).replace(/\\+/g,' '))\n` +
`if(memory===-1)temp=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
`else temp=decodeURIComponent(url.slice(start, memory).replace(/\\+|%20/g,' '))\n` +
`const charCode = temp.charCodeAt(0)\n` +
`if(charCode !== 91 && charCode !== 123)\n` +
`temp='"'+temp+'"'\n` +
Expand All @@ -751,10 +751,10 @@ export const composeHandler = ({
`if(a${index}===undefined)` +
`a${index}=[]\n` +
`if(memory===-1){` +
`a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+/g,' '))\n` +
`a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+|%20/g,' '))\n` +
`break` +
`}` +
`else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+/g,' '))\n` +
`else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+|%20/g,' '))\n` +
`memory=url.indexOf('&${key}=',memory)\n` +
`if(memory===-1) break\n` +
`}`
Expand All @@ -764,8 +764,8 @@ export const composeHandler = ({
`if(memory!==-1){` +
`const start=memory+${key.length + 2}\n` +
`memory=url.indexOf('&',start)\n` +
`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}=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))` +
`else a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+|%20/g,' '))` +
`if(a${index}!==undefined)` +
`try{` +
`a${index}=JSON.parse(a${index})` +
Expand All @@ -778,9 +778,9 @@ export const composeHandler = ({
`if(memory!==-1){` +
`const start=memory+${key.length + 2}\n` +
`memory=url.indexOf('&',start)\n` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
`else{` +
`a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+/g,' '))`
`a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+|%20/g,' '))`

if (anyOf)
fnLiteral +=
Expand All @@ -793,8 +793,8 @@ export const composeHandler = ({
`if(first)first=false\n` +
`else deepMemory = url.indexOf('&', start)\n` +
`let value\n` +
`if(deepMemory===-1)value=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` +
`else value=decodeURIComponent(url.slice(start, deepMemory).replace(/\\+/g,' '))\n` +
`if(deepMemory===-1)value=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
`else value=decodeURIComponent(url.slice(start, deepMemory).replace(/\\+|%20/g,' '))\n` +
`const vStart=value.charCodeAt(0)\n` +
`const vEnd=value.charCodeAt(value.length - 1)\n` +
`if((vStart===91&&vEnd===93)||(vStart===123&&vEnd===125))\n` +
Expand Down
14 changes: 12 additions & 2 deletions 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 Expand Up @@ -409,7 +419,7 @@ export const ElysiaType = {
.Decode((value) => {
if (typeof value === 'string') return value === 'true'

if (property && !Value.Check(schema, value))
if (value !== undefined && !Value.Check(schema, value))
throw new ValidationError('property', schema, value)

return value
Expand Down
6 changes: 3 additions & 3 deletions test/type-system/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('TypeSystem - Date', () => {
expect(Value.Check(schema, '2021/1/1')).toEqual(true)

expect(Value.Check(schema, 'yay')).toEqual(false)
expect(Value.Check(schema, 42)).toEqual(false)
expect(Value.Check(schema, 42)).toEqual(true)
expect(Value.Check(schema, {})).toEqual(false)
expect(Value.Check(schema, undefined)).toEqual(false)
expect(Value.Check(schema, null)).toEqual(false)
Expand All @@ -36,7 +36,7 @@ describe('TypeSystem - Date', () => {
'The encoded value does not match the expected schema'
)
expect(() => Value.Encode(schema, 'yay')).toThrow(error)
expect(() => Value.Encode(schema, 42)).toThrow(error)
expect(() => Value.Encode(schema, 42)).not.toThrow(error)
expect(() => Value.Encode(schema, {})).toThrow(error)
expect(() => Value.Encode(schema, undefined)).toThrow(error)
expect(() => Value.Encode(schema, null)).toThrow(error)
Expand All @@ -56,7 +56,7 @@ describe('TypeSystem - Date', () => {
'Unable to decode value as it does not match the expected schema'
)
expect(() => Value.Decode(schema, 'yay')).toThrow(error)
expect(() => Value.Decode(schema, 42)).toThrow(error)
expect(() => Value.Decode(schema, 42)).not.toThrow(error)
expect(() => Value.Decode(schema, {})).toThrow(error)
expect(() => Value.Decode(schema, undefined)).toThrow(error)
expect(() => Value.Decode(schema, null)).toThrow(error)
Expand Down
73 changes: 67 additions & 6 deletions test/validator/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ describe('Query Validator', () => {
expect(await res.json()).toEqual({ param1: true })
})

it('parse optional boolean string with second parameter', async () => {
const schema = t.Object({
registered: t.Optional(t.Boolean()),
other: t.String()
})
const app = new Elysia().get('/', ({ query }) => query, {
query: schema
})
const res = await app.handle(req('/?other=sucrose'))

expect(res.status).toBe(200)
expect(await res.json()).toEqual({ other: 'sucrose' })
})

it('parse optional boolean string with default value', async () => {
const schema = t.Object({
registered: t.Optional(t.Boolean({ default: true })),
other: t.String()
})
const app = new Elysia().get('/', ({ query }) => query, {
query: schema
})
const res = await app.handle(req('/?other=sucrose'))

expect(res.status).toBe(200)
expect(await res.json()).toEqual({ other: 'sucrose', registered: true })
})

it('validate optional object', async () => {
const app = new Elysia().get(
'/',
Expand Down Expand Up @@ -654,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 @@ -670,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 5dd1ee3

Please sign in to comment.