Skip to content

fix: fixed issues with req.url and added more tests #413

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

Open
wants to merge 15 commits into
base: master
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
4 changes: 3 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"rules": {
"recommended": true,
"correctness": {
"noVoidTypeReturn": "off"
"noVoidTypeReturn": "off",
"noUnusedImports": "error",
"noUnusedVariables": "error"
},
"style": {
"noParameterAssign": "off"
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ app.engine('html', async (path, locals, _opts, cb) => {
cb(null, renderedTemplate)
})

app.get('/', (req, res) => {
app.get('/', (_req, res) => {
res.render('hello.html', { name: 'v1rtl' })
})

Expand Down
2 changes: 1 addition & 1 deletion examples/postgresql-typeorm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ createConnection()

// register routes

app.get('/users', async (req: Request, res: Response, next) => {
app.get('/users', async (_req: Request, res: Response, next) => {
try {
const users = await userRepository.find()
res.json(users)
Expand Down
2 changes: 1 addition & 1 deletion examples/preact-ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const app = new App()
const PreactApp = ({ page }) => (page ? html`<h1>You visited ${page}</h1>` : html`<h1>Hello World</h1>`)

app
.get('/htm.js', async (req, res) => {
.get('/htm.js', async (_req, res) => {
const preactFile = await readFile(`${process.cwd()}/node_modules/htm/preact/standalone.module.js`)

res.set('Content-Type', 'text/javascript').send(preactFile.toString())
Expand Down
6 changes: 3 additions & 3 deletions examples/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const schema = {
const app = new App()

app
.get('/docs/:docId', addToDocs({ params: { docId: 'number' } }), (req, res) => {
.get('/docs/:docId', addToDocs({ params: { docId: 'number' } }), (_req, res) => {
res.status(200).send('done')
})
.post(
Expand All @@ -27,10 +27,10 @@ app
),
(_req, res) => void res.status(200).send('done')
)
.get('/users', addToDocs({ query: { userId: { type: 'number', optional: true } } }, ['users']), (req, res) => {
.get('/users', addToDocs({ query: { userId: { type: 'number', optional: true } } }, ['users']), (_req, res) => {
res.status(200).send('done')
})
.get('/:userId/:docId', addToDocs({ params: { userId: 'number', docId: 'number' } }), (req, res) => {
.get('/:userId/:docId', addToDocs({ params: { userId: 'number', docId: 'number' } }), (_req, res) => {
res.status(200).send('done')
})

Expand Down
2 changes: 1 addition & 1 deletion examples/turbo-http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { status } from '@tinyhttp/send'
import turbo from 'turbo-http'

const app = new App({
applyExtensions: (_req, res, next) => {
applyExtensions: (_req, _res, next) => {
next()
},
onError: (err, req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/vue-ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const app = new App()

app
.use(sirv())
.get('*', async (req, res) => {
.get('*', async (_req, res) => {
const app = createApp()

const html = await renderToString(app)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"test:coverage": "vitest run --dir tests --coverage",
"test": "pnpm test:coverage",
"lint": "biome lint .",
"lint:fix": "biome lint --fix .",
"format": "biome format .",
"format:fix": "biome format --fix .",
"check": "biome check .",
"echo:ancestor": "echo ${ANCESTOR_REF:-$(git merge-base origin/master HEAD)}",
"build": "pnpm -r build",
Expand Down
4 changes: 2 additions & 2 deletions packages/accepts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Accepts {
*
* The `type` value may be a single mime type string such as "application/json", the extension name such as "json" or an array `["json", "html", "text/plain"]`. When a list or array is given the _best_ match, if any is returned. When no types are given as arguments, returns all types accepted by the client in the preference order.
*/
types(types: string | string[], ...args: string[]): string[] | string | false {
types(types?: string | string[], ...args: string[]): string[] | string | false {
let mimeTypes: string[] = []

// support flattened arguments
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Accepts {
* ['es', 'pt', 'en']
*
*/
languages(languages: string | string[], ...args: string[]): string | string[] | boolean {
languages(languages?: string | string[], ...args: string[]): string | string[] | boolean {
let _languages: string[] = languages as string[]

// support flattened arguments
Expand Down
11 changes: 5 additions & 6 deletions packages/app/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ export class App<Req extends Request = Request, Res extends Response = Response>

const exts = this.applyExtensions || extendMiddleware<RenderOptions>(this)

req.originalUrl = req.url || req.originalUrl
req.originalUrl = req.originalUrl || req.url

const pathname = getPathname(req.originalUrl)
const pathname = getPathname(req.url)

const matched = this.#find(pathname)

Expand Down Expand Up @@ -359,9 +359,8 @@ export class App<Req extends Request = Request, Res extends Response = Response>
try {
params = regex ? getURLParams(regex, pathname) : {}
} catch (e) {
console.error(e)
if (e instanceof URIError) return res.sendStatus(400)
throw e
if (e instanceof URIError) return res.sendStatus(400) // Handle malformed URI
return this.onError(e, req, res)
}

// Warning: users should not use :wild as a pattern
Expand All @@ -379,7 +378,7 @@ export class App<Req extends Request = Request, Res extends Response = Response>
req.params = { ...req.params, ...params }

if (mw.type === 'mw') {
req.url = lead(req.originalUrl.substring(prefix.length))
req.url = lead(req.url.substring(prefix.length))
}

if (!req.path) req.path = getPathname(req.url)
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getProtocol = (req: Request, trust: Trust): Protocol => {

if (!trustRemoteAddress(req, trust)) return proto

const header = (req.headers['X-Forwarded-Proto'] as string) || proto
const header = (req.get('X-Forwarded-Proto') as string) || proto

const index = header.indexOf(',')

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { TemplateEngine, TemplateEngineOptions } from './types.js'
function tryStat(path: string) {
try {
return statSync(path)
} catch (e) {
} catch (_e) {
return undefined
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cookie/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
function tryDecode(str: string, decode: (str: string) => string) {
try {
return decode(str)
} catch (e) {
} catch (_e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be replaced with catch {

return str
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/proxy-addr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function parseNetmask(netmask: string) {
* @param trust
* @public
*/
export function proxyaddr(req: Req, trust: Trust): string {
function proxyaddr(req: Req, trust: Trust): string {
const addrs = alladdrs(req, trust)

return addrs[addrs.length - 1]
Expand Down Expand Up @@ -216,5 +216,4 @@ function trustSingle(subnet: Subnet) {
}
}

export { alladdrs as all }
export { compile }
export { compile, alladdrs as all, proxyaddr }
1 change: 0 additions & 1 deletion packages/req/src/fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function isStale(etag: string, noneMatch: string) {
export function fresh(reqHeaders: IncomingHttpHeaders, resHeaders: OutgoingHttpHeaders) {
const modifiedSince = reqHeaders['if-modified-since']
const noneMatch = reqHeaders['if-none-match']

if (!modifiedSince && !noneMatch) return false

const cacheControl = reqHeaders['cache-control']
Expand Down
2 changes: 1 addition & 1 deletion packages/type-is/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function tryNormalizeType(value: string) {

try {
return normalizeType(value)
} catch (err) {
} catch (_err) {
return null
}
}
Expand Down
Loading
Loading