Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 17a7d11

Browse files
author
v1rtl
committed
fix req.ip and req.ips
1 parent 3e5bd75 commit 17a7d11

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
egg.json

egg.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
"entry": "./app.ts",
55
"description": "0-legacy, tiny & fast web framework as a replacement of Express",
66
"homepage": "https://github.com/talentlessguy/tinyhttp-deno",
7-
"version": "0.0.10",
8-
"ignore": ["./examples/**/*.ts"],
9-
"files": ["./**/*.ts", "README.md"],
7+
"version": "0.0.11",
8+
"ignore": [
9+
"./examples/**/*.ts"
10+
],
11+
"files": [
12+
"./**/*.ts",
13+
"README.md"
14+
],
1015
"checkFormat": false,
1116
"checkTests": false,
1217
"checkInstallation": false,

extend.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export const extendMiddleware = <
5656

5757
req.query = getQueryParams(req.url)
5858

59+
req.connection = {
60+
remoteAddress: (req.conn.remoteAddr as Deno.NetAddr).hostname
61+
}
62+
5963
req.get = getRequestHeader(req)
6064

6165
if (settings?.freshnessTesting) {
@@ -72,9 +76,6 @@ export const extendMiddleware = <
7276
req.xhr = checkIfXMLHttpRequest(req)
7377
req.is = reqIs(req)
7478

75-
req.ip = getIP(req)
76-
req.ips = getIPs(req)
77-
7879
if (settings?.networkExtensions) {
7980
req.protocol = getProtocol(req)
8081
req.secure = req.protocol === 'https'

extensions/req/security.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const getHostname = (req: Request): string | undefined => {
4545

4646
return index !== -1 ? host.substring(0, index) : host
4747
}
48-
export const getIP = (req: Request): string | undefined => proxyaddr(req, trustRemoteAddress(req)).replace(/^.*:/, '') // striping the redundant prefix addeded by OS to IPv4 address
48+
export const getIP = (req: Request): string | undefined => proxyaddr(req, trustRemoteAddress(req))?.replace(/^.*:/, '') // striping the redundant prefix addeded by OS to IPv4 address
4949

5050
export const getIPs = (req: Request): string[] | undefined => all(req, trustRemoteAddress(req))
5151

onError.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServerRequest } from 'https://deno.land/std@0.87.0/http/server.ts'
1+
import { Request } from './request.ts'
22
import { NextFunction } from 'https://esm.sh/@tinyhttp/router'
33
import { ALL as STATUS_CODES } from 'https://deno.land/x/status@0.1.0/codes.ts'
44
import { status } from 'https://deno.land/x/status@0.1.0/status.ts'
@@ -9,9 +9,9 @@ export type ServerError = Partial<{
99
message: string
1010
}>
1111

12-
export type ErrorHandler = (err: ServerError, req: ServerRequest, next?: NextFunction) => void
12+
export type ErrorHandler = (err: ServerError, req: Request, next?: NextFunction) => void
1313

14-
export const onErrorHandler: ErrorHandler = async (err: ServerError, req: ServerRequest) => {
14+
export const onErrorHandler: ErrorHandler = async (err: ServerError, req: Request) => {
1515
let code = 500
1616

1717
if (err.code && err.code in STATUS_CODES) code = err.code

request.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ export interface Request extends ServerRequest {
4040

4141
cookies?: any
4242
signedCookies?: any
43+
44+
connection: {
45+
remoteAddress: string
46+
}
4347
}

0 commit comments

Comments
 (0)