Skip to content
Myr

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

5 files+59 added−17 removed

modified:deno_dist/utils/url.ts+6 added
modified:deno_dist/request.ts+11 added-8 removed
1import { parseBody } from './utils/body.ts'2import type { Cookie } from './utils/cookie.ts'3import { parse } from './utils/cookie.ts'4import { getQueryStringFromURL } from './utils/url.ts'56declare global {7 interface Request<ParamKeyType extends string = string> {8 param: {
53 unmodified lines
62 }63 } as InstanceType<typeof Request>['header']6465 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']7879 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
modified:src/utils/url.test.ts+25 added-1 removed
1import { splitPath, getPattern, getPathFromURL, isAbsoluteURL, mergePath } from './url'1import {2 splitPath,3 getPattern,4 getPathFromURL,5 isAbsoluteURL,6 mergePath,7 getQueryStringFromURL,8} from './url'910describe('url', () => {11 it('splitPath', () => {12 let ps = splitPath('/')
54 unmodified lines
67 expect(path).toBe('/hello/hey')68 })69 })7071 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 })8788 describe('isAbsoluteURL', () => {89 it('absolute url', () => {90 expect(isAbsoluteURL('https://example.com')).toBeTruthy()91 expect(isAbsoluteURL('https://example.com/xxx')).toBeTruthy()
37 unmodified lines
modified:src/utils/url.ts+6 added
modified:src/request.ts+11 added-8 removed
1import { parseBody } from './utils/body'2import type { Cookie } from './utils/cookie'3import { parse } from './utils/cookie'4import { getQueryStringFromURL } from './utils/url'56declare global {7 interface Request<ParamKeyType extends string = string> {8 param: {
53 unmodified lines
62 }63 } as InstanceType<typeof Request>['header']6465 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']7879 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