perf(req): improve `c.req.query` performance (#498)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* perf(req): improve `c.req.query` performance * allow fragments as query strings
authored by Yusuke Wada · committed by GitHub · parent e174b70 · Browse files · 5 files·+59 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+59 added−17 removed
- tests:+25 added−1 removed
- other:+34 added−16 removed
Provenance
- Commit
b695481- Tree
6bdda24- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
e174b70
5 files changed+59 added−17 removed
modified:deno_dist/utils/url.ts+6 added
48 unmodified lines495051525354555657585960616235 unmodified lines48 unmodified lines
return result}
export const getQueryStringFromURL = (url: string): string => { const queryIndex = url.indexOf('?') const result = queryIndex !== -1 ? url.substring(queryIndex) : '' return result}
export const isAbsoluteURL = (url: string): boolean => { const match = url.match(URL_REGEXP) if (match) { return true35 unmodified linesmodified:deno_dist/request.ts+11 added-8 removed
1 import { parseBody } from './utils/body.ts'2 import type { Cookie } from './utils/cookie.ts'3 import { parse } from './utils/cookie.ts'4+import { getQueryStringFromURL } from './utils/url.ts'5 6 declare global {7 interface Request<ParamKeyType extends string = string> {8 param: {53 unmodified lines62 }63 } as InstanceType<typeof Request>['header']64 65 Request.prototype.query = function (this: Request, key?: string) {65- const url = new URL(this.url)66+ const queryString = getQueryStringFromURL(this.url)67+ const searchParams = new URLSearchParams(queryString)68 if (key) {67- return url.searchParams.get(key)69+ return searchParams.get(key)70 } else {71 const result: Record<string, string> = {}70- for (const key of url.searchParams.keys()) {71- result[key] = url.searchParams.get(key) || ''72+ for (const key of searchParams.keys()) {73+ result[key] = searchParams.get(key) || ''74 }75 return result76 }77 } as InstanceType<typeof Request>['query']78 79 Request.prototype.queries = function (this: Request, key?: string) {78- const url = new URL(this.url)80+ const queryString = getQueryStringFromURL(this.url)81+ const searchParams = new URLSearchParams(queryString)82 if (key) {80- return url.searchParams.getAll(key)83+ return searchParams.getAll(key)84 } else {85 const result: Record<string, string[]> = {}83- for (const key of url.searchParams.keys()) {84- result[key] = url.searchParams.getAll(key)86+ for (const key of searchParams.keys()) {87+ result[key] = searchParams.getAll(key)88 }89 return result90 }91 } as InstanceType<typeof Request>['queries']23 unmodified linesmodified:src/utils/url.test.ts+25 added-1 removed
1-import { splitPath, getPattern, getPathFromURL, isAbsoluteURL, mergePath } from './url'1+import {2+ splitPath,3+ getPattern,4+ getPathFromURL,5+ isAbsoluteURL,6+ mergePath,7+ getQueryStringFromURL,8+} from './url'9 10 describe('url', () => {11 it('splitPath', () => {12 let ps = splitPath('/')54 unmodified lines67 expect(path).toBe('/hello/hey')68 })69 })70 71+ describe('getQueryStringFromURL', () => {72+ it('should return strings of query params', () => {73+ let queryString = getQueryStringFromURL('https://example.com/?foo=bar')74+ expect(queryString).toBe('?foo=bar')75+ queryString = getQueryStringFromURL('https://example.com/?foo=bar&foo2=bar2')76+ expect(queryString).toBe('?foo=bar&foo2=bar2')77+ queryString = getQueryStringFromURL('https://example.com/')78+ expect(queryString).toBe('')79+ // This specification allows the fragments as query strings80+ queryString = getQueryStringFromURL('https://example.com/?#foo=#bar&#foo2=#bar2')81+ expect(queryString).toBe('?#foo=#bar&#foo2=#bar2')82+ // This specification allows that the string includes two `?` or more83+ queryString = getQueryStringFromURL('https://example.com/?foo=bar?foo2=bar2')84+ expect(queryString).toBe('?foo=bar?foo2=bar2')85+ })86+ })87+88 describe('isAbsoluteURL', () => {89 it('absolute url', () => {90 expect(isAbsoluteURL('https://example.com')).toBeTruthy()91 expect(isAbsoluteURL('https://example.com/xxx')).toBeTruthy()37 unmodified linesmodified:src/utils/url.ts+6 added
48 unmodified lines495051525354555657585960616235 unmodified lines48 unmodified lines
return result}
export const getQueryStringFromURL = (url: string): string => { const queryIndex = url.indexOf('?') const result = queryIndex !== -1 ? url.substring(queryIndex) : '' return result}
export const isAbsoluteURL = (url: string): boolean => { const match = url.match(URL_REGEXP) if (match) { return true35 unmodified linesmodified:src/request.ts+11 added-8 removed
1 import { parseBody } from './utils/body'2 import type { Cookie } from './utils/cookie'3 import { parse } from './utils/cookie'4+import { getQueryStringFromURL } from './utils/url'5 6 declare global {7 interface Request<ParamKeyType extends string = string> {8 param: {53 unmodified lines62 }63 } as InstanceType<typeof Request>['header']64 65 Request.prototype.query = function (this: Request, key?: string) {65- const url = new URL(this.url)66+ const queryString = getQueryStringFromURL(this.url)67+ const searchParams = new URLSearchParams(queryString)68 if (key) {67- return url.searchParams.get(key)69+ return searchParams.get(key)70 } else {71 const result: Record<string, string> = {}70- for (const key of url.searchParams.keys()) {71- result[key] = url.searchParams.get(key) || ''72+ for (const key of searchParams.keys()) {73+ result[key] = searchParams.get(key) || ''74 }75 return result76 }77 } as InstanceType<typeof Request>['query']78 79 Request.prototype.queries = function (this: Request, key?: string) {78- const url = new URL(this.url)80+ const queryString = getQueryStringFromURL(this.url)81+ const searchParams = new URLSearchParams(queryString)82 if (key) {80- return url.searchParams.getAll(key)83+ return searchParams.getAll(key)84 } else {85 const result: Record<string, string[]> = {}83- for (const key of url.searchParams.keys()) {84- result[key] = url.searchParams.getAll(key)86+ for (const key of searchParams.keys()) {87+ result[key] = searchParams.getAll(key)88 }89 return result90 }91 } as InstanceType<typeof Request>['queries']23 unmodified lines