Skip to content

Commit

Permalink
refactor(logger-plugin): extract getPort logic and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed May 7, 2024
1 parent b2096e9 commit df06075
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/plugins/default/logger-plugin.ts
Expand Up @@ -2,6 +2,7 @@ import { URL } from 'url';
import { Plugin } from '../../types';
import { getLogger } from '../../logger';
import type { IncomingMessage } from 'node:http';
import { getPort } from '../../utils/logger-plugin';

type ExpressRequest = {
/** Express req.baseUrl */
Expand Down Expand Up @@ -43,18 +44,19 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;

// construct targetUrl
// const port = proxyRes.req?.socket?.autoSelectFamilyAttemptedAddresses?.[0]?.split(':')?.reverse()[0]; //.at(-1),
const port = Object.keys(proxyRes.req?.agent?.sockets || {})?.[0]?.split(':')[1];
const port = getPort(proxyRes.req?.agent?.sockets);

const obj = {
protocol: proxyRes.req.protocol,
host: proxyRes.req.host,
port: port,
pathname: proxyRes.req.path,
} as URL;

const target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
target.port = obj.port; // optional port

if (port) {
target.port = port;
}
const targetUrl = target.toString();

const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/logger-plugin.ts
@@ -0,0 +1,11 @@
import type { Agent } from 'node:http';

export type Sockets = Pick<Agent, 'sockets'>;

/**
* Get port from target
* Using proxyRes.req.agent.sockets to determine the target port
*/
export function getPort(sockets?: Sockets): string | undefined {
return Object.keys(sockets || {})?.[0]?.split(':')[1];
}
23 changes: 23 additions & 0 deletions test/unit/utils/logger-plugin.spec.ts
@@ -0,0 +1,23 @@
import { getPort, type Sockets } from '../../../src/utils/logger-plugin';

describe('getPort()', () => {
it('should return port from proxyRes.req.agent.sockets', () => {
const sockets = {
'jsonplaceholder.typicode.com:80:': [],

Check failure on line 6 in test/unit/utils/logger-plugin.spec.ts

View workflow job for this annotation

GitHub Actions / Spellcheck

Unknown word (jsonplaceholder)

Check failure on line 6 in test/unit/utils/logger-plugin.spec.ts

View workflow job for this annotation

GitHub Actions / Spellcheck

Unknown word (typicode)
} as unknown as Sockets;

expect(getPort(sockets)).toBe('80');
});

it('should handle missing "sockets" from proxyRes?.req?.agent?.sockets', () => {
const sockets = undefined;

expect(getPort(sockets)).toBe(undefined);
});

it('should handle empty "sockets" from proxyRes?.req?.agent?.sockets', () => {
const sockets = {} as unknown as Sockets;

expect(getPort(sockets)).toBe(undefined);
});
});

0 comments on commit df06075

Please sign in to comment.