Skip to content

Commit

Permalink
feat(api-server): Add NanotronUrl class for handling client request URLs
Browse files Browse the repository at this point in the history
This commit adds a new file, `url.ts`, which contains the implementation of the `NanotronUrl` class. This class extends the `URL` class from the `node:url` module and provides additional functionality for handling client request URLs in the Nanotron API server.

The `NanotronUrl` class includes a static property `versionPattern_` which is a regular expression used to match and replace the version prefix in the URL. It also has properties `method` and `debugId` which store the HTTP method and the debug ID of the request respectively.

The constructor of the `NanotronUrl` class takes a `clientRequest` object of type `IncomingMessage` and a `prefix` parameter which represents the URL prefix. It modifies the URL by removing the prefix and replacing the version prefix with a single `/`. The modified URL is then passed to the `URL` constructor along with a default base URL.

This commit enhances the functionality of the Nanotron API server by providing a dedicated class for handling client request URLs. This improves code organization and maintainability.
  • Loading branch information
alimd committed Sep 12, 2024
1 parent 680e1e5 commit 3e41fd6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/api-server/src/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {URL} from 'node:url';

import type {HttpMethod} from './type.js';
import type {IncomingMessage} from 'node:http';

export class NanotronUrl extends URL {
protected static versionPattern_ = new RegExp('^/v[0-9]+/');

readonly method: HttpMethod;
readonly debugId: string;

constructor(clientRequest: IncomingMessage, prefix: `/${string}/` | '/') {
let url = clientRequest.url ?? '';
if (prefix !== '/' && url.indexOf(prefix) === 0) {
url = url.slice(prefix.length - 1); // include `/`
}
url = url.replace(NanotronUrl.versionPattern_, '/');

super(url, 'http://hostname/');

this.method = (clientRequest.method ?? 'GET').toUpperCase() as HttpMethod;

this.debugId = `[${this.method}]${this.pathname}`;
}
}

0 comments on commit 3e41fd6

Please # to comment.