Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(req): use URLSearchParams for parsing query params #3565

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ describe('url', () => {
expect(getQueryParam('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toBe(
'a web framework'
)
expect(getQueryParam('http://example.com/?q=%h', 'q')).toBe('%h')

expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name0')).toBe('sam')
expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name1')).toBe('tom')
Expand Down
100 changes: 18 additions & 82 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,95 +199,31 @@ export const checkOptionalParameter = (path: string): string[] | null => {
return results.filter((v, i, a) => a.indexOf(v) === i)
}

// Optimized
const _decodeURI = (value: string) => {
if (!/[%+]/.test(value)) {
return value
}
if (value.indexOf('+') !== -1) {
value = value.replace(/\+/g, ' ')
}
return /%/.test(value) ? decodeURIComponent_(value) : value
}

const _getQueryParam = (
url: string,
key?: string,
multiple?: boolean
): string | undefined | Record<string, string> | string[] | Record<string, string[]> => {
let encoded

if (!multiple && key && !/[%+]/.test(key)) {
// optimized for unencoded key

let keyIndex = url.indexOf(`?${key}`, 8)
if (keyIndex === -1) {
keyIndex = url.indexOf(`&${key}`, 8)
}
while (keyIndex !== -1) {
const trailingKeyCode = url.charCodeAt(keyIndex + key.length + 1)
if (trailingKeyCode === 61) {
const valueIndex = keyIndex + key.length + 2
const endIndex = url.indexOf('&', valueIndex)
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex))
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
return ''
}
keyIndex = url.indexOf(`&${key}`, keyIndex + 1)
}

encoded = /[%+]/.test(url)
if (!encoded) {
return undefined
}
// fallback to default routine
):
| string
| undefined
| string[]
| Record<string, string | undefined>
| Record<string, string[]> => {
const searchParams = new URLSearchParams(getQueryStrings(url))

if (key) {
const params = multiple ? searchParams.getAll(key) : searchParams.get(key)
return multiple ? (params?.length ? params : undefined) : params ?? undefined
}

const results: Record<string, string> | Record<string, string[]> = {}
encoded ??= /[%+]/.test(url)

let keyIndex = url.indexOf('?', 8)
while (keyIndex !== -1) {
const nextKeyIndex = url.indexOf('&', keyIndex + 1)
let valueIndex = url.indexOf('=', keyIndex)
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
valueIndex = -1
}
let name = url.slice(
keyIndex + 1,
valueIndex === -1 ? (nextKeyIndex === -1 ? undefined : nextKeyIndex) : valueIndex
)
if (encoded) {
name = _decodeURI(name)
}

keyIndex = nextKeyIndex

if (name === '') {
continue
}

let value
if (valueIndex === -1) {
value = ''
} else {
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex)
if (encoded) {
value = _decodeURI(value)
}
}

if (multiple) {
if (!(results[name] && Array.isArray(results[name]))) {
results[name] = []
}
;(results[name] as string[]).push(value)
} else {
results[name] ??= value
}
}
const result = Array.from(searchParams.keys()).reduce<
Record<string, string | undefined> | Record<string, string[]>
>((acc, key) => {
acc[key] = multiple ? searchParams.getAll(key) : searchParams.get(key) ?? undefined
return acc
}, {})

return key ? results[key] : results
return result
}

export const getQueryParam: (
Expand Down
Loading