mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 21:45:31 +09:00
refactor: remove dead code (#3337)
This commit is contained in:
1 parent
a19002652b
commit
93d1f8af46
1527 files changed
+21170
-73846
No files matched your search
@@ -16,10 +16,6 @@ interface ExtractedClientIp {
|
||||
ipVersion: IpAddressFamily;
|
||||
}
|
||||
|
||||
interface HeadersLike {
|
||||
[key: string]: string | Array<string> | undefined;
|
||||
}
|
||||
|
||||
export class MissingClientIpError extends Error {
|
||||
readonly code = 'FORBIDDEN';
|
||||
readonly status = 403;
|
||||
@@ -44,14 +40,6 @@ export function resolveClientIpHeaderName(clientIpHeaderName?: string): string {
|
||||
return normalizeHeaderName(clientIpHeaderName ?? DEFAULT_CLIENT_IP_HEADER_NAME);
|
||||
}
|
||||
|
||||
function toStringHeaderValue(value: string | Array<string> | null | undefined): string | null {
|
||||
if (Array.isArray(value)) {
|
||||
const first = value[0];
|
||||
return typeof first === 'string' ? first : null;
|
||||
}
|
||||
return typeof value === 'string' ? value : null;
|
||||
}
|
||||
|
||||
function parseClientIpHeaderValue(value: string | null): ParsedIpAddress | null {
|
||||
if (value === null) {
|
||||
return null;
|
||||
@@ -59,28 +47,6 @@ function parseClientIpHeaderValue(value: string | null): ParsedIpAddress | null
|
||||
return parseIpAddress(value.split(',', 1)[0]);
|
||||
}
|
||||
|
||||
function getHeaderValue(headers: HeadersLike, name: string): string | null {
|
||||
const lowerName = name.toLowerCase();
|
||||
const directMatch = toStringHeaderValue(headers[lowerName]);
|
||||
if (directMatch !== null) {
|
||||
return directMatch;
|
||||
}
|
||||
for (const [key, value] of Object.entries(headers)) {
|
||||
if (key.toLowerCase() === lowerName) {
|
||||
return toStringHeaderValue(value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function createNodeHeaderReader(headers: HeadersLike): HeaderReader {
|
||||
return {
|
||||
get: (name: string): string | null => {
|
||||
return getHeaderValue(headers, name);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function extractClientIpDetailsFromReader(
|
||||
headerReader: HeaderReader,
|
||||
options?: ClientIpExtractionOptions,
|
||||
@@ -116,15 +82,3 @@ export function requireClientIp(req: Request, options?: ClientIpExtractionOption
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
export function extractClientIpDetailsFromHeaders(
|
||||
headers: HeadersLike,
|
||||
options?: ClientIpExtractionOptions,
|
||||
): ExtractedClientIp | null {
|
||||
return extractClientIpDetailsFromReader(createNodeHeaderReader(headers), options);
|
||||
}
|
||||
|
||||
export function extractClientIpFromHeaders(headers: HeadersLike, options?: ClientIpExtractionOptions): string | null {
|
||||
const extracted = extractClientIpDetailsFromHeaders(headers, options);
|
||||
return extracted?.ip ?? null;
|
||||
}
|
||||
@@ -3,8 +3,6 @@
|
||||
import {
|
||||
extractClientIp,
|
||||
extractClientIpDetails,
|
||||
extractClientIpDetailsFromHeaders,
|
||||
extractClientIpFromHeaders,
|
||||
MissingClientIpError,
|
||||
requireClientIp,
|
||||
resolveClientIpHeaderName,
|
||||
@@ -130,35 +128,6 @@ describe('extractClientIpDetails', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractClientIpFromHeaders', () => {
|
||||
it('extracts from node-style headers with configured header name', () => {
|
||||
const headers = {
|
||||
'x-real-ip': '192.168.1.1',
|
||||
};
|
||||
expect(extractClientIpFromHeaders(headers, {trustClientIpHeader: true, clientIpHeaderName: 'x-real-ip'})).toBe(
|
||||
'192.168.1.1',
|
||||
);
|
||||
});
|
||||
it('supports case-insensitive keys and array values', () => {
|
||||
const headers = {
|
||||
'X-FORWARDED-FOR': ['203.0.113.50'],
|
||||
};
|
||||
expect(extractClientIpFromHeaders(headers, {trustClientIpHeader: true})).toBe('203.0.113.50');
|
||||
expect(extractClientIpDetailsFromHeaders(headers, {trustClientIpHeader: true})).toEqual({
|
||||
ip: '203.0.113.50',
|
||||
source: 'client-ip-header',
|
||||
ipVersion: 'ipv4',
|
||||
});
|
||||
});
|
||||
it('returns null when trust is disabled', () => {
|
||||
expect(extractClientIpFromHeaders({'x-forwarded-for': '192.168.1.1'})).toBeNull();
|
||||
});
|
||||
it('returns null for invalid inputs', () => {
|
||||
expect(extractClientIpFromHeaders({}, {trustClientIpHeader: true})).toBeNull();
|
||||
expect(extractClientIpFromHeaders({'x-forwarded-for': 'not-an-ip'}, {trustClientIpHeader: true})).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('requireClientIp', () => {
|
||||
it('returns ip when present and trusted', () => {
|
||||
const request = new Request('http://example.com', {
|
||||
|
||||
Reference in new issue
Block a user