refactor(utils/url): Decode parameters more accurately without performance degradation. (#1015)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(utils/url): Decode parameters more accurately without performance degradation. * Add tests about getQueryParam and getQueryParams. * chore(bench): add query param bench * chore: denoify
authored by Taku Amano · committed by GitHub · parent ffbf250 · Browse files · 10 files·+812 added−153 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+812 added−153 removed
- tests:+58 added−41 removed
- other:+754 added−112 removed
Provenance
- Commit
91c04ac- Tree
203d357- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
ffbf250
10 files changed+812 added−153 removed
added:benchmarks/query-param/src/bench.mts+42 added
123456789101112131415161718192021222324252627282930313233343536373839404142import { run, group, bench } from 'mitata'import fastQuerystring from './fast-querystring.mts'import hono from './hono.mts';[ { url: 'http://example.com/?page=1', key: 'page', }, { url: 'http://example.com/?url=http://example.com&page=1', key: 'page', }, { url: 'http://example.com/?page=1', key: undefined, }, { url: 'http://example.com/?url=http://example.com&page=1', key: undefined, }, { url: 'http://example.com/?url=http://example.com/very/very/deep/path/to/something&search=very-long-search-string', key: undefined, }, { url: 'http://example.com/?search=Hono+is+a+small,+simple,+and+ultrafast+web+framework+for+the+Edge.&page=1', key: undefined, }, { url: 'http://example.com/?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10', key: undefined, },].forEach((data) => { const { url, key } = data
group(JSON.stringify(data), () => { bench('hono', () => hono(url, key)) bench('fastQuerystring', () => fastQuerystring(url, key)) })})
run()added:benchmarks/query-param/src/fast-querystring.mts+12 added
123456789101112import { parse } from 'fast-querystring'
const getQueryStringFromURL = (url: string): string => { const queryIndex = url.indexOf('?', 8) const result = queryIndex !== -1 ? url.slice(queryIndex + 1) : '' return result}
export default (url, key?) => { const data = parse(getQueryStringFromURL(url)) return key !== undefined ? data[key] : data }added:benchmarks/query-param/src/hono.mts+5 added
12345import { getQueryParam } from '../../../src/utils/url'
export default (url, key?) => { return getQueryParam(url, key)}added:benchmarks/query-param/package-lock.json+519 added
1+{2+ "name": "query-param",3+ "lockfileVersion": 3,4+ "requires": true,5+ "packages": {6+ "": {7+ "license": "MIT",8+ "dependencies": {9+ "fast-querystring": "^1.1.1",10+ "mitata": "^0.1.6"11+ },12+ "devDependencies": {13+ "tsx": "^3.12.2"14+ }15+ },16+ "node_modules/@esbuild-kit/cjs-loader": {17+ "version": "2.4.2",18+ "resolved": "https://registry.npmjs.org/@esbuild-kit/cjs-loader/-/cjs-loader-2.4.2.tgz",19+ "integrity": "sha512-BDXFbYOJzT/NBEtp71cvsrGPwGAMGRB/349rwKuoxNSiKjPraNNnlK6MIIabViCjqZugu6j+xeMDlEkWdHHJSg==",20+ "dev": true,21+ "dependencies": {22+ "@esbuild-kit/core-utils": "^3.0.0",23+ "get-tsconfig": "^4.4.0"24+ }25+ },26+ "node_modules/@esbuild-kit/core-utils": {27+ "version": "3.1.0",28+ "resolved": "https://registry.npmjs.org/@esbuild-kit/core-utils/-/core-utils-3.1.0.tgz",29+ "integrity": "sha512-Uuk8RpCg/7fdHSceR1M6XbSZFSuMrxcePFuGgyvsBn+u339dk5OeL4jv2EojwTN2st/unJGsVm4qHWjWNmJ/tw==",30+ "dev": true,31+ "dependencies": {32+ "esbuild": "~0.17.6",33+ "source-map-support": "^0.5.21"34+ }35+ },36+ "node_modules/@esbuild-kit/esm-loader": {37+ "version": "2.5.5",38+ "resolved": "https://registry.npmjs.org/@esbuild-kit/esm-loader/-/esm-loader-2.5.5.tgz",39+ "integrity": "sha512-Qwfvj/qoPbClxCRNuac1Du01r9gvNOT+pMYtJDapfB1eoGN1YlJ1BixLyL9WVENRx5RXgNLdfYdx/CuswlGhMw==",40+ "dev": true,41+ "dependencies": {42+ "@esbuild-kit/core-utils": "^3.0.0",43+ "get-tsconfig": "^4.4.0"44+ }45+ },46+ "node_modules/@esbuild/android-arm": {47+ "version": "0.17.14",48+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.17.14.tgz",49+ "integrity": "sha512-0CnlwnjDU8cks0yJLXfkaU/uoLyRf9VZJs4p1PskBr2AlAHeEsFEwJEo0of/Z3g+ilw5mpyDwThlxzNEIxOE4g==",50+ "cpu": [51+ "arm"52+ ],53+ "dev": true,54+ "optional": true,55+ "os": [56+ "android"57+ ],58+ "engines": {59+ "node": ">=12"60+ }61+ },62+ "node_modules/@esbuild/android-arm64": {63+ "version": "0.17.14",64+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.17.14.tgz",65+ "integrity": "sha512-eLOpPO1RvtsP71afiFTvS7tVFShJBCT0txiv/xjFBo5a7R7Gjw7X0IgIaFoLKhqXYAXhahoXm7qAmRXhY4guJg==",66+ "cpu": [67+ "arm64"68+ ],69+ "dev": true,70+ "optional": true,71+ "os": [72+ "android"73+ ],74+ "engines": {75+ "node": ">=12"76+ }77+ },78+ "node_modules/@esbuild/android-x64": {79+ "version": "0.17.14",80+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.17.14.tgz",81+ "integrity": "sha512-nrfQYWBfLGfSGLvRVlt6xi63B5IbfHm3tZCdu/82zuFPQ7zez4XjmRtF/wIRYbJQ/DsZrxJdEvYFE67avYXyng==",82+ "cpu": [83+ "x64"84+ ],85+ "dev": true,86+ "optional": true,87+ "os": [88+ "android"89+ ],90+ "engines": {91+ "node": ">=12"92+ }93+ },94+ "node_modules/@esbuild/darwin-arm64": {95+ "version": "0.17.14",96+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.17.14.tgz",97+ "integrity": "sha512-eoSjEuDsU1ROwgBH/c+fZzuSyJUVXQTOIN9xuLs9dE/9HbV/A5IqdXHU1p2OfIMwBwOYJ9SFVGGldxeRCUJFyw==",98+ "cpu": [99+ "arm64"100+ ],101+ "dev": true,102+ "optional": true,103+ "os": [104+ "darwin"105+ ],106+ "engines": {107+ "node": ">=12"108+ }109+ },110+ "node_modules/@esbuild/darwin-x64": {111+ "version": "0.17.14",112+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.17.14.tgz",113+ "integrity": "sha512-zN0U8RWfrDttdFNkHqFYZtOH8hdi22z0pFm0aIJPsNC4QQZv7je8DWCX5iA4Zx6tRhS0CCc0XC2m7wKsbWEo5g==",114+ "cpu": [115+ "x64"116+ ],117+ "dev": true,118+ "optional": true,119+ "os": [120+ "darwin"121+ ],122+ "engines": {123+ "node": ">=12"124+ }125+ },126+ "node_modules/@esbuild/freebsd-arm64": {127+ "version": "0.17.14",128+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.17.14.tgz",129+ "integrity": "sha512-z0VcD4ibeZWVQCW1O7szaLxGsx54gcCnajEJMdYoYjLiq4g1jrP2lMq6pk71dbS5+7op/L2Aod+erw+EUr28/A==",130+ "cpu": [131+ "arm64"132+ ],133+ "dev": true,134+ "optional": true,135+ "os": [136+ "freebsd"137+ ],138+ "engines": {139+ "node": ">=12"140+ }141+ },142+ "node_modules/@esbuild/freebsd-x64": {143+ "version": "0.17.14",144+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.17.14.tgz",145+ "integrity": "sha512-hd9mPcxfTgJlolrPlcXkQk9BMwNBvNBsVaUe5eNUqXut6weDQH8whcNaKNF2RO8NbpT6GY8rHOK2A9y++s+ehw==",146+ "cpu": [147+ "x64"148+ ],149+ "dev": true,150+ "optional": true,151+ "os": [152+ "freebsd"153+ ],154+ "engines": {155+ "node": ">=12"156+ }157+ },158+ "node_modules/@esbuild/linux-arm": {159+ "version": "0.17.14",160+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.17.14.tgz",161+ "integrity": "sha512-BNTl+wSJ1omsH8s3TkQmIIIQHwvwJrU9u1ggb9XU2KTVM4TmthRIVyxSp2qxROJHhZuW/r8fht46/QE8hU8Qvg==",162+ "cpu": [163+ "arm"164+ ],165+ "dev": true,166+ "optional": true,167+ "os": [168+ "linux"169+ ],170+ "engines": {171+ "node": ">=12"172+ }173+ },174+ "node_modules/@esbuild/linux-arm64": {175+ "version": "0.17.14",176+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.17.14.tgz",177+ "integrity": "sha512-FhAMNYOq3Iblcj9i+K0l1Fp/MHt+zBeRu/Qkf0LtrcFu3T45jcwB6A1iMsemQ42vR3GBhjNZJZTaCe3VFPbn9g==",178+ "cpu": [179+ "arm64"180+ ],181+ "dev": true,182+ "optional": true,183+ "os": [184+ "linux"185+ ],186+ "engines": {187+ "node": ">=12"188+ }189+ },190+ "node_modules/@esbuild/linux-ia32": {191+ "version": "0.17.14",192+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.17.14.tgz",193+ "integrity": "sha512-91OK/lQ5y2v7AsmnFT+0EyxdPTNhov3y2CWMdizyMfxSxRqHazXdzgBKtlmkU2KYIc+9ZK3Vwp2KyXogEATYxQ==",194+ "cpu": [195+ "ia32"196+ ],197+ "dev": true,198+ "optional": true,199+ "os": [200+ "linux"201+ ],202+ "engines": {203+ "node": ">=12"204+ }205+ },206+ "node_modules/@esbuild/linux-loong64": {207+ "version": "0.17.14",208+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.17.14.tgz",209+ "integrity": "sha512-vp15H+5NR6hubNgMluqqKza85HcGJgq7t6rMH7O3Y6ApiOWPkvW2AJfNojUQimfTp6OUrACUXfR4hmpcENXoMQ==",210+ "cpu": [211+ "loong64"212+ ],213+ "dev": true,214+ "optional": true,215+ "os": [216+ "linux"217+ ],218+ "engines": {219+ "node": ">=12"220+ }221+ },222+ "node_modules/@esbuild/linux-mips64el": {223+ "version": "0.17.14",224+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.17.14.tgz",225+ "integrity": "sha512-90TOdFV7N+fgi6c2+GO9ochEkmm9kBAKnuD5e08GQMgMINOdOFHuYLPQ91RYVrnWwQ5683sJKuLi9l4SsbJ7Hg==",226+ "cpu": [227+ "mips64el"228+ ],229+ "dev": true,230+ "optional": true,231+ "os": [232+ "linux"233+ ],234+ "engines": {235+ "node": ">=12"236+ }237+ },238+ "node_modules/@esbuild/linux-ppc64": {239+ "version": "0.17.14",240+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.17.14.tgz",241+ "integrity": "sha512-NnBGeoqKkTugpBOBZZoktQQ1Yqb7aHKmHxsw43NddPB2YWLAlpb7THZIzsRsTr0Xw3nqiPxbA1H31ZMOG+VVPQ==",242+ "cpu": [243+ "ppc64"244+ ],245+ "dev": true,246+ "optional": true,247+ "os": [248+ "linux"249+ ],250+ "engines": {251+ "node": ">=12"252+ }253+ },254+ "node_modules/@esbuild/linux-riscv64": {255+ "version": "0.17.14",256+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.17.14.tgz",257+ "integrity": "sha512-0qdlKScLXA8MGVy21JUKvMzCYWovctuP8KKqhtE5A6IVPq4onxXhSuhwDd2g5sRCzNDlDjitc5sX31BzDoL5Fw==",258+ "cpu": [259+ "riscv64"260+ ],261+ "dev": true,262+ "optional": true,263+ "os": [264+ "linux"265+ ],266+ "engines": {267+ "node": ">=12"268+ }269+ },270+ "node_modules/@esbuild/linux-s390x": {271+ "version": "0.17.14",272+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.17.14.tgz",273+ "integrity": "sha512-Hdm2Jo1yaaOro4v3+6/zJk6ygCqIZuSDJHdHaf8nVH/tfOuoEX5Riv03Ka15LmQBYJObUTNS1UdyoMk0WUn9Ww==",274+ "cpu": [275+ "s390x"276+ ],277+ "dev": true,278+ "optional": true,279+ "os": [280+ "linux"281+ ],282+ "engines": {283+ "node": ">=12"284+ }285+ },286+ "node_modules/@esbuild/linux-x64": {287+ "version": "0.17.14",288+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.17.14.tgz",289+ "integrity": "sha512-8KHF17OstlK4DuzeF/KmSgzrTWQrkWj5boluiiq7kvJCiQVzUrmSkaBvcLB2UgHpKENO2i6BthPkmUhNDaJsVw==",290+ "cpu": [291+ "x64"292+ ],293+ "dev": true,294+ "optional": true,295+ "os": [296+ "linux"297+ ],298+ "engines": {299+ "node": ">=12"300+ }301+ },302+ "node_modules/@esbuild/netbsd-x64": {303+ "version": "0.17.14",304+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.17.14.tgz",305+ "integrity": "sha512-nVwpqvb3yyXztxIT2+VsxJhB5GCgzPdk1n0HHSnchRAcxqKO6ghXwHhJnr0j/B+5FSyEqSxF4q03rbA2fKXtUQ==",306+ "cpu": [307+ "x64"308+ ],309+ "dev": true,310+ "optional": true,311+ "os": [312+ "netbsd"313+ ],314+ "engines": {315+ "node": ">=12"316+ }317+ },318+ "node_modules/@esbuild/openbsd-x64": {319+ "version": "0.17.14",320+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.17.14.tgz",321+ "integrity": "sha512-1RZ7uQQ9zcy/GSAJL1xPdN7NDdOOtNEGiJalg/MOzeakZeTrgH/DoCkbq7TaPDiPhWqnDF+4bnydxRqQD7il6g==",322+ "cpu": [323+ "x64"324+ ],325+ "dev": true,326+ "optional": true,327+ "os": [328+ "openbsd"329+ ],330+ "engines": {331+ "node": ">=12"332+ }333+ },334+ "node_modules/@esbuild/sunos-x64": {335+ "version": "0.17.14",336+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.17.14.tgz",337+ "integrity": "sha512-nqMjDsFwv7vp7msrwWRysnM38Sd44PKmW8EzV01YzDBTcTWUpczQg6mGao9VLicXSgW/iookNK6AxeogNVNDZA==",338+ "cpu": [339+ "x64"340+ ],341+ "dev": true,342+ "optional": true,343+ "os": [344+ "sunos"345+ ],346+ "engines": {347+ "node": ">=12"348+ }349+ },350+ "node_modules/@esbuild/win32-arm64": {351+ "version": "0.17.14",352+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.17.14.tgz",353+ "integrity": "sha512-xrD0mccTKRBBIotrITV7WVQAwNJ5+1va6L0H9zN92v2yEdjfAN7864cUaZwJS7JPEs53bDTzKFbfqVlG2HhyKQ==",354+ "cpu": [355+ "arm64"356+ ],357+ "dev": true,358+ "optional": true,359+ "os": [360+ "win32"361+ ],362+ "engines": {363+ "node": ">=12"364+ }365+ },366+ "node_modules/@esbuild/win32-ia32": {367+ "version": "0.17.14",368+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.17.14.tgz",369+ "integrity": "sha512-nXpkz9bbJrLLyUTYtRotSS3t5b+FOuljg8LgLdINWFs3FfqZMtbnBCZFUmBzQPyxqU87F8Av+3Nco/M3hEcu1w==",370+ "cpu": [371+ "ia32"372+ ],373+ "dev": true,374+ "optional": true,375+ "os": [376+ "win32"377+ ],378+ "engines": {379+ "node": ">=12"380+ }381+ },382+ "node_modules/@esbuild/win32-x64": {383+ "version": "0.17.14",384+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.17.14.tgz",385+ "integrity": "sha512-gPQmsi2DKTaEgG14hc3CHXHp62k8g6qr0Pas+I4lUxRMugGSATh/Bi8Dgusoz9IQ0IfdrvLpco6kujEIBoaogA==",386+ "cpu": [387+ "x64"388+ ],389+ "dev": true,390+ "optional": true,391+ "os": [392+ "win32"393+ ],394+ "engines": {395+ "node": ">=12"396+ }397+ },398+ "node_modules/buffer-from": {399+ "version": "1.1.2",400+ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",401+ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",402+ "dev": true403+ },404+ "node_modules/esbuild": {405+ "version": "0.17.14",406+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.14.tgz",407+ "integrity": "sha512-vOO5XhmVj/1XQR9NQ1UPq6qvMYL7QFJU57J5fKBKBKxp17uDt5PgxFDb4A2nEiXhr1qQs4x0F5+66hVVw4ruNw==",408+ "dev": true,409+ "hasInstallScript": true,410+ "bin": {411+ "esbuild": "bin/esbuild"412+ },413+ "engines": {414+ "node": ">=12"415+ },416+ "optionalDependencies": {417+ "@esbuild/android-arm": "0.17.14",418+ "@esbuild/android-arm64": "0.17.14",419+ "@esbuild/android-x64": "0.17.14",420+ "@esbuild/darwin-arm64": "0.17.14",421+ "@esbuild/darwin-x64": "0.17.14",422+ "@esbuild/freebsd-arm64": "0.17.14",423+ "@esbuild/freebsd-x64": "0.17.14",424+ "@esbuild/linux-arm": "0.17.14",425+ "@esbuild/linux-arm64": "0.17.14",426+ "@esbuild/linux-ia32": "0.17.14",427+ "@esbuild/linux-loong64": "0.17.14",428+ "@esbuild/linux-mips64el": "0.17.14",429+ "@esbuild/linux-ppc64": "0.17.14",430+ "@esbuild/linux-riscv64": "0.17.14",431+ "@esbuild/linux-s390x": "0.17.14",432+ "@esbuild/linux-x64": "0.17.14",433+ "@esbuild/netbsd-x64": "0.17.14",434+ "@esbuild/openbsd-x64": "0.17.14",435+ "@esbuild/sunos-x64": "0.17.14",436+ "@esbuild/win32-arm64": "0.17.14",437+ "@esbuild/win32-ia32": "0.17.14",438+ "@esbuild/win32-x64": "0.17.14"439+ }440+ },441+ "node_modules/fast-decode-uri-component": {442+ "version": "1.0.1",443+ "resolved": "https://registry.npmjs.org/fast-decode-uri-component/-/fast-decode-uri-component-1.0.1.tgz",444+ "integrity": "sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg=="445+ },446+ "node_modules/fast-querystring": {447+ "version": "1.1.1",448+ "resolved": "https://registry.npmjs.org/fast-querystring/-/fast-querystring-1.1.1.tgz",449+ "integrity": "sha512-qR2r+e3HvhEFmpdHMv//U8FnFlnYjaC6QKDuaXALDkw2kvHO8WDjxH+f/rHGR4Me4pnk8p9JAkRNTjYHAKRn2Q==",450+ "dependencies": {451+ "fast-decode-uri-component": "^1.0.1"452+ }453+ },454+ "node_modules/fsevents": {455+ "version": "2.3.2",456+ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",457+ "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",458+ "dev": true,459+ "hasInstallScript": true,460+ "optional": true,461+ "os": [462+ "darwin"463+ ],464+ "engines": {465+ "node": "^8.16.0 || ^10.6.0 || >=11.0.0"466+ }467+ },468+ "node_modules/get-tsconfig": {469+ "version": "4.4.0",470+ "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.4.0.tgz",471+ "integrity": "sha512-0Gdjo/9+FzsYhXCEFueo2aY1z1tpXrxWZzP7k8ul9qt1U5o8rYJwTJYmaeHdrVosYIVYkOy2iwCJ9FdpocJhPQ==",472+ "dev": true,473+ "funding": {474+ "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1"475+ }476+ },477+ "node_modules/mitata": {478+ "version": "0.1.6",479+ "resolved": "https://registry.npmjs.org/mitata/-/mitata-0.1.6.tgz",480+ "integrity": "sha512-VKQ0r3jriTOU9E2Z+mwbZrUmbg4Li4QyFfi7kfHKl6reZhGzL0AYlu3wE0VPXzIwA5xnFzmEQoBwCcNT8stUkA=="481+ },482+ "node_modules/source-map": {483+ "version": "0.6.1",484+ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",485+ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",486+ "dev": true,487+ "engines": {488+ "node": ">=0.10.0"489+ }490+ },491+ "node_modules/source-map-support": {492+ "version": "0.5.21",493+ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz",494+ "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==",495+ "dev": true,496+ "dependencies": {497+ "buffer-from": "^1.0.0",498+ "source-map": "^0.6.0"499+ }500+ },501+ "node_modules/tsx": {502+ "version": "3.12.6",503+ "resolved": "https://registry.npmjs.org/tsx/-/tsx-3.12.6.tgz",504+ "integrity": "sha512-q93WgS3lBdHlPgS0h1i+87Pt6n9K/qULIMNYZo07nSeu2z5QE2CellcAZfofVXBo2tQg9av2ZcRMQ2S2i5oadQ==",505+ "dev": true,506+ "dependencies": {507+ "@esbuild-kit/cjs-loader": "^2.4.2",508+ "@esbuild-kit/core-utils": "^3.0.0",509+ "@esbuild-kit/esm-loader": "^2.5.5"510+ },511+ "bin": {512+ "tsx": "dist/cli.js"513+ },514+ "optionalDependencies": {515+ "fsevents": "~2.3.2"516+ }517+ }518+ }519+}added:benchmarks/query-param/package.json+14 added
1234567891011121314{ "scripts": { "bench:node": "tsx ./src/bench.mts", "bench:bun": "bun run ./src/bench.mts" }, "license": "MIT", "devDependencies": { "tsx": "^3.12.2" }, "dependencies": { "fast-querystring": "^1.1.1", "mitata": "^0.1.6" }}modified:deno_dist/utils/url.ts+78 added-47 removed
79 unmodified lines80 81 return result82 }83 84-export const getQueryStringFromURL = (url: string): string => {85- const queryIndex = url.indexOf('?', 8)86- const result = queryIndex !== -1 ? url.slice(queryIndex + 1) : ''87- return result88-}89-84 export const mergePath = (...paths: string[]): string => {85 let p: string = ''86 let endsWithSlash = false87 39 unmodified lines127 return [base === '' ? '/' : base.replace(/\/$/, ''), optional]128 }129 130 // Optimized137-export const getQueryParam = (138- queryString: string,139- key?: string140-): string | null | Record<string, string> => {141- const results: Record<string, string> = {}131+const _decodeURI = (value: string) => {132+ if (!/[%+]/.test(value)) {133+ return value134+ }135+ if (value.includes('+')) {136+ value = value.replace(/\+/g, ' ')137+ }138+ return value.includes('%') ? decodeURIComponent(value) : value139+}140 143- // eslint-disable-next-line no-constant-condition144- while (true) {145- const andIndex = queryString.indexOf('&')146- let strings = ''147- if (andIndex === -1) {148- strings = queryString149- } else {150- strings = queryString.substring(0, andIndex)151- }141+const _getQueryParam = (142+ url: string,143+ key?: string,144+ multiple?: boolean145+): string | undefined | Record<string, string> | string[] | Record<string, string[]> => {146+ let encoded147 153- const eqIndex = strings.indexOf('=')154- if (eqIndex !== -1) {155- const v = strings.substring(eqIndex + 1)156- const k = strings.substring(0, eqIndex)157- if (key === k) {158- return /\%/.test(v) ? decodeURIComponent(v) : v159- } else {160- results[k] ||= v148+ if (!multiple && key && !/[%+]/.test(key)) {149+ // optimized for unencoded key150+151+ let keyIndex = url.indexOf(`?${key}`, 8)152+ if (keyIndex === -1) {153+ keyIndex = url.indexOf(`&${key}`, 8)154+ }155+ while (keyIndex !== -1) {156+ const trailingKeyCode = url.charCodeAt(keyIndex + key.length + 1)157+ if (trailingKeyCode === 61) {158+ const valueIndex = keyIndex + key.length + 2159+ const endIndex = url.indexOf('&', valueIndex)160+ const value = url.slice(valueIndex, endIndex === -1 ? undefined : endIndex)161+ return _decodeURI(value)162+ } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {163+ return ''164 }162- } else if (strings === key) {163- return ''165+ keyIndex = url.indexOf(`&${key}`, keyIndex)166 }167 166- if (andIndex === -1) break167- queryString = queryString.substring(andIndex + 1, queryString.length)168+ encoded = /[%+]/.test(url)169+ if (!encoded) {170+ return undefined171+ }172+ // fallback to default routine173 }174 170- if (key) return null171- return results172-}175+ const results: Record<string, string> | Record<string, string[]> = {}176+ encoded ??= /[%+]/.test(url)177 174-export const getQueryParams = (175- queryString: string,176- key?: string177-): string[] | null | Record<string, string[]> => {178- const results: Record<string, string[]> = {}178+ let keyIndex = url.indexOf('?', 8)179+ while (keyIndex !== -1) {180+ const valueIndex = url.indexOf('=', keyIndex)181+ let name = url.slice(keyIndex + 1, valueIndex === -1 ? undefined : valueIndex)182+ if (encoded) {183+ name = _decodeURI(name)184+ }185 180- for (const strings of queryString.split('&')) {181- let [k, v] = strings.split('=')182- if (v === undefined) v = ''183- results[k] ||= []184- results[k].push(v.indexOf('%') !== -1 ? decodeURIComponent(v) : v)186+ let value187+ if (valueIndex === -1) {188+ keyIndex = -1189+ value = ''190+ } else {191+ keyIndex = url.indexOf('&', valueIndex)192+ value = url.slice(valueIndex + 1, keyIndex === -1 ? undefined : keyIndex)193+ if (encoded) {194+ value = _decodeURI(value)195+ }196+ }197+198+ if (multiple) {199+ ;((results[name] ??= []) as string[]).push(value)200+ } else {201+ results[name] ??= value202+ }203 }204 187- if (key) return results[key] ? results[key] : null205+ return key ? results[key] : results206+}207 189- return results208+export const getQueryParam: (209+ url: string,210+ key?: string211+) => string | undefined | Record<string, string> = _getQueryParam as (212+ url: string,213+ key?: string214+) => string | undefined | Record<string, string>215+216+export const getQueryParams = (217+ url: string,218+ key?: string219+): string[] | undefined | Record<string, string[]> => {220+ return _getQueryParam(url, key, true) as string[] | undefined | Record<string, string[]>221 }modified:deno_dist/request.ts+3 added-9 removed
10 unmodified lines11 import type { BodyData } from './utils/body.ts'12 import type { Cookie } from './utils/cookie.ts'13 import { parse } from './utils/cookie.ts'14 import type { UnionToIntersection } from './utils/types.ts'15-import { getQueryStringFromURL, getQueryParam, getQueryParams } from './utils/url.ts'15+import { getQueryParam, getQueryParams } from './utils/url.ts'16 17 export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {18 raw: Request19 36 unmodified lines56 57 query(key: string): string | undefined58 query(): Record<string, string>59 query(key?: string) {60- const queryString = getQueryStringFromURL(this.url)61- const result = getQueryParam(queryString, key)62- if (result === null) return undefined63- return result60+ return getQueryParam(this.url, key)61 }62 63 queries(key: string): string[] | undefined64 queries(): Record<string, string[]>65 queries(key?: string) {69- const queryString = getQueryStringFromURL(this.url)70- const result = getQueryParams(queryString, key)71- if (result === null) return undefined72- return result66+ return getQueryParams(this.url, key)67 }68 69 header(name: string): string | undefined70 header(): Record<string, string>111 unmodified linesmodified:src/utils/url.test.ts+58 added-41 removed
2 unmodified lines3 splitRoutingPath,4 getPattern,5 getPathFromURL,6 mergePath,7- getQueryStringFromURL,7 checkOptionalParameter,8 getQueryParam,9 getQueryParams,10 } from './url'83 unmodified lines94 expect(path).toBe('/')95 })96 })97 99- describe('getQueryStringFromURL', () => {100- it('should return strings of query params', () => {101- let queryString = getQueryStringFromURL('https://example.com/?foo=bar')102- expect(queryString).toBe('foo=bar')103- queryString = getQueryStringFromURL('https://example.com/?foo=bar&foo2=bar2')104- expect(queryString).toBe('foo=bar&foo2=bar2')105- queryString = getQueryStringFromURL('https://example.com/')106- expect(queryString).toBe('')107- // This specification allows the fragments as query strings108- queryString = getQueryStringFromURL('https://example.com/?#foo=#bar&#foo2=#bar2')109- expect(queryString).toBe('#foo=#bar&#foo2=#bar2')110- // This specification allows that the string includes two `?` or more111- queryString = getQueryStringFromURL('https://example.com/?foo=bar?foo2=bar2')112- expect(queryString).toBe('foo=bar?foo2=bar2')113- })114- })115-98 describe('mergePath', () => {99 it('mergePath', () => {100 expect(mergePath('/book', '/')).toBe('/book')101 expect(mergePath('/book/', '/')).toBe('/book/')38 unmodified lines140 })141 142 describe('getQueryParam', () => {143 it('Parse URL query strings', () => {162- expect(getQueryParam('name=hey', 'name')).toBe('hey')163- expect(getQueryParam('name=hey#fragment', 'name')).toBe('hey#fragment')164- expect(getQueryParam('name=hey&age=20&tall=170', 'age')).toBe('20')144+ expect(getQueryParam('http://example.com/?name=hey', 'name')).toBe('hey')145+ expect(getQueryParam('http://example.com/?name=hey#fragment', 'name')).toBe('hey#fragment')146+ expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170', 'age')).toBe('20')147+ expect(getQueryParam('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toBe(148+ 'a web framework'149+ )150 let searchParams = new URLSearchParams({ name: '炎' })166- expect(getQueryParam(searchParams.toString(), 'name')).toBe('炎')167- expect(getQueryParam('name=hey&age=20&tall=170', 'weight')).toBe(null)168- expect(getQueryParam('name=hey&age=20&tall=170')).toEqual({151+ expect(getQueryParam(`http://example.com/?${searchParams.toString()}`, 'name')).toBe('炎')152+ searchParams = new URLSearchParams({ '炎 is': 'a web framework' })153+ expect(154+ getQueryParam(155+ `http://example.com/?${searchParams.toString()}`,156+ searchParams.keys().next().value157+ )158+ ).toBe('a web framework')159+ expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170', 'weight')).toBe(160+ undefined161+ )162+ expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170')).toEqual({163 name: 'hey',164 age: '20',165 tall: '170',166 })173- expect(getQueryParam('pretty', 'pretty')).toBe('')174- expect(getQueryParam('pretty', 'prtt')).toBe(null)175- expect(getQueryParam('name=sam&name=tom', 'name')).toBe('sam')176- expect(getQueryParam('name=sam&name=tom')).toEqual({167+ expect(getQueryParam('http://example.com/?pretty', 'pretty')).toBe('')168+ expect(getQueryParam('http://example.com/?pretty', 'prtt')).toBe(undefined)169+ expect(getQueryParam('http://example.com/?name=sam&name=tom', 'name')).toBe('sam')170+ expect(getQueryParam('http://example.com/?name=sam&name=tom')).toEqual({171 name: 'sam',172 })173 searchParams = new URLSearchParams('?name=sam=tom')174 expect(getQueryParam('name', searchParams.get('name')?.toString()))1 unmodified line176 })177 178 describe('getQueryParams', () => {179 it('Parse URL query strings', () => {186- expect(getQueryParams('name=hey', 'name')).toEqual(['hey'])187- expect(getQueryParams('name=hey#fragment', 'name')).toEqual(['hey#fragment'])188- expect(getQueryParams('name=hey&name=foo', 'name')).toEqual(['hey', 'foo'])189- expect(getQueryParams('name=hey&age=20&tall=170', 'age')).toEqual(['20'])190- expect(getQueryParams('name=hey&age=20&tall=170&name=foo&age=30', 'age')).toEqual([191- '20',192- '30',180+ expect(getQueryParams('http://example.com/?name=hey', 'name')).toEqual(['hey'])181+ expect(getQueryParams('http://example.com/?name=hey#fragment', 'name')).toEqual([182+ 'hey#fragment',183 ])184+ expect(getQueryParams('http://example.com/?name=hey&name=foo', 'name')).toEqual([185+ 'hey',186+ 'foo',187+ ])188+ expect(getQueryParams('http://example.com/?name=hey&age=20&tall=170', 'age')).toEqual(['20'])189+ expect(190+ getQueryParams('http://example.com/?name=hey&age=20&tall=170&name=foo&age=30', 'age')191+ ).toEqual(['20', '30'])192+ expect(getQueryParams('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toEqual([193+ 'a web framework',194+ ])194- const searchParams = new URLSearchParams()195+ let searchParams = new URLSearchParams()196 searchParams.append('tag', '炎')197 searchParams.append('tag', 'ほのお')197- expect(getQueryParams(searchParams.toString(), 'tag')).toEqual(['炎', 'ほのお'])198- expect(getQueryParams('name=hey&age=20&tall=170', 'weight')).toEqual(null)199- expect(getQueryParams('name=hey&age=20&tall=170&name=foo&age=30&tall=180')).toEqual({198+ expect(getQueryParams(`http://example.com/?${searchParams.toString()}`, 'tag')).toEqual([199+ '炎',200+ 'ほのお',201+ ])202+ searchParams = new URLSearchParams()203+ searchParams.append('炎 works on', 'Cloudflare Workers')204+ searchParams.append('炎 works on', 'Fastly Compute@Edge')205+ expect(206+ getQueryParams(207+ `http://example.com/?${searchParams.toString()}`,208+ searchParams.keys().next().value209+ )210+ ).toEqual(['Cloudflare Workers', 'Fastly Compute@Edge'])211+ expect(getQueryParams('http://example.com/?name=hey&age=20&tall=170', 'weight')).toEqual(212+ undefined213+ )214+ expect(215+ getQueryParams('http://example.com/?name=hey&age=20&tall=170&name=foo&age=30&tall=180')216+ ).toEqual({217 name: ['hey', 'foo'],218 age: ['20', '30'],219 tall: ['170', '180'],220 })204- expect(getQueryParams('pretty', 'pretty')).toEqual([''])205- expect(getQueryParams('pretty', 'prtt')).toBe(null)221+ expect(getQueryParams('http://example.com/?pretty', 'pretty')).toEqual([''])222+ expect(getQueryParams('http://example.com/?pretty', 'prtt')).toBe(undefined)223 })224 })225 })modified:src/utils/url.ts+78 added-47 removed
79 unmodified lines80 81 return result82 }83 84-export const getQueryStringFromURL = (url: string): string => {85- const queryIndex = url.indexOf('?', 8)86- const result = queryIndex !== -1 ? url.slice(queryIndex + 1) : ''87- return result88-}89-84 export const mergePath = (...paths: string[]): string => {85 let p: string = ''86 let endsWithSlash = false87 39 unmodified lines127 return [base === '' ? '/' : base.replace(/\/$/, ''), optional]128 }129 130 // Optimized137-export const getQueryParam = (138- queryString: string,139- key?: string140-): string | null | Record<string, string> => {141- const results: Record<string, string> = {}131+const _decodeURI = (value: string) => {132+ if (!/[%+]/.test(value)) {133+ return value134+ }135+ if (value.includes('+')) {136+ value = value.replace(/\+/g, ' ')137+ }138+ return value.includes('%') ? decodeURIComponent(value) : value139+}140 143- // eslint-disable-next-line no-constant-condition144- while (true) {145- const andIndex = queryString.indexOf('&')146- let strings = ''147- if (andIndex === -1) {148- strings = queryString149- } else {150- strings = queryString.substring(0, andIndex)151- }141+const _getQueryParam = (142+ url: string,143+ key?: string,144+ multiple?: boolean145+): string | undefined | Record<string, string> | string[] | Record<string, string[]> => {146+ let encoded147 153- const eqIndex = strings.indexOf('=')154- if (eqIndex !== -1) {155- const v = strings.substring(eqIndex + 1)156- const k = strings.substring(0, eqIndex)157- if (key === k) {158- return /\%/.test(v) ? decodeURIComponent(v) : v159- } else {160- results[k] ||= v148+ if (!multiple && key && !/[%+]/.test(key)) {149+ // optimized for unencoded key150+151+ let keyIndex = url.indexOf(`?${key}`, 8)152+ if (keyIndex === -1) {153+ keyIndex = url.indexOf(`&${key}`, 8)154+ }155+ while (keyIndex !== -1) {156+ const trailingKeyCode = url.charCodeAt(keyIndex + key.length + 1)157+ if (trailingKeyCode === 61) {158+ const valueIndex = keyIndex + key.length + 2159+ const endIndex = url.indexOf('&', valueIndex)160+ const value = url.slice(valueIndex, endIndex === -1 ? undefined : endIndex)161+ return _decodeURI(value)162+ } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {163+ return ''164 }162- } else if (strings === key) {163- return ''165+ keyIndex = url.indexOf(`&${key}`, keyIndex)166 }167 166- if (andIndex === -1) break167- queryString = queryString.substring(andIndex + 1, queryString.length)168+ encoded = /[%+]/.test(url)169+ if (!encoded) {170+ return undefined171+ }172+ // fallback to default routine173 }174 170- if (key) return null171- return results172-}175+ const results: Record<string, string> | Record<string, string[]> = {}176+ encoded ??= /[%+]/.test(url)177 174-export const getQueryParams = (175- queryString: string,176- key?: string177-): string[] | null | Record<string, string[]> => {178- const results: Record<string, string[]> = {}178+ let keyIndex = url.indexOf('?', 8)179+ while (keyIndex !== -1) {180+ const valueIndex = url.indexOf('=', keyIndex)181+ let name = url.slice(keyIndex + 1, valueIndex === -1 ? undefined : valueIndex)182+ if (encoded) {183+ name = _decodeURI(name)184+ }185 180- for (const strings of queryString.split('&')) {181- let [k, v] = strings.split('=')182- if (v === undefined) v = ''183- results[k] ||= []184- results[k].push(v.indexOf('%') !== -1 ? decodeURIComponent(v) : v)186+ let value187+ if (valueIndex === -1) {188+ keyIndex = -1189+ value = ''190+ } else {191+ keyIndex = url.indexOf('&', valueIndex)192+ value = url.slice(valueIndex + 1, keyIndex === -1 ? undefined : keyIndex)193+ if (encoded) {194+ value = _decodeURI(value)195+ }196+ }197+198+ if (multiple) {199+ ;((results[name] ??= []) as string[]).push(value)200+ } else {201+ results[name] ??= value202+ }203 }204 187- if (key) return results[key] ? results[key] : null205+ return key ? results[key] : results206+}207 189- return results208+export const getQueryParam: (209+ url: string,210+ key?: string211+) => string | undefined | Record<string, string> = _getQueryParam as (212+ url: string,213+ key?: string214+) => string | undefined | Record<string, string>215+216+export const getQueryParams = (217+ url: string,218+ key?: string219+): string[] | undefined | Record<string, string[]> => {220+ return _getQueryParam(url, key, true) as string[] | undefined | Record<string, string[]>221 }modified:src/request.ts+3 added-9 removed
10 unmodified lines11 import type { BodyData } from './utils/body'12 import type { Cookie } from './utils/cookie'13 import { parse } from './utils/cookie'14 import type { UnionToIntersection } from './utils/types'15-import { getQueryStringFromURL, getQueryParam, getQueryParams } from './utils/url'15+import { getQueryParam, getQueryParams } from './utils/url'16 17 export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {18 raw: Request19 36 unmodified lines56 57 query(key: string): string | undefined58 query(): Record<string, string>59 query(key?: string) {60- const queryString = getQueryStringFromURL(this.url)61- const result = getQueryParam(queryString, key)62- if (result === null) return undefined63- return result60+ return getQueryParam(this.url, key)61 }62 63 queries(key: string): string[] | undefined64 queries(): Record<string, string[]>65 queries(key?: string) {69- const queryString = getQueryStringFromURL(this.url)70- const result = getQueryParams(queryString, key)71- if (result === null) return undefined72- return result66+ return getQueryParams(this.url, key)67 }68 69 header(name: string): string | undefined70 header(): Record<string, string>111 unmodified lines