Skip to content

Commit

Permalink
refactor(api-server): refactor NanotronClientRequest class
Browse files Browse the repository at this point in the history
Refactor the NanotronClientRequest class in the api-server package.
This commit adds a new property `remoteAddress` to store the remote address of the client.
It also refactors the `getRemoteAddress` method to a private method `getRemoteAddress__` for internal use.
  • Loading branch information
alimd committed Sep 14, 2024
1 parent 416faab commit 17953ad
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/api-server/src/api-client-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export class NanotronClientRequest {

protected readonly logger_;

readonly remoteAddress: string | null;

get headers(): HttpRequestHeaders {
return this.raw_.headers;
}
Expand All @@ -49,6 +51,9 @@ export class NanotronClientRequest {
this.url = url;
this.routeOption = routeOption;

// Get and store remote address.
this.remoteAddress = this.getRemoteAddress__();

// Create logger.
this.logger_ = createLogger('nt-client-request'); // TODO: add client ip
this.logger_.logMethodArgs?.('new', url.debugId);
Expand Down Expand Up @@ -129,15 +134,7 @@ export class NanotronClientRequest {
});
}

getRemoteAddress(): string {
// prettier-ignore
return (
this.raw_.headers['x-forwarded-for']
?.split(',')
.pop()
?.trim() ||
this.raw_.socket.remoteAddress ||
'unknown'
);
private getRemoteAddress__(): string | null {
return this.raw_.headers['x-forwarded-for']?.split(',').pop()?.trim() || this.raw_.socket.remoteAddress || null;
}
}

0 comments on commit 17953ad

Please # to comment.