Skip to content

Commit

Permalink
fix(nano-server): prefixPattern, logger
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Apr 5, 2023
1 parent 2d24f0f commit f9d203e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
17 changes: 9 additions & 8 deletions core/nano-server/src/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export class AlwatrNanoServer {
keepAliveTimeout: 120_000,
healthRoute: true,
allowAllOrigin: false,
prefixPattern: 'api',
...config,
};

this._logger = createLogger('alwatr-nano-server:' + this._config.port);
this._logger = createLogger('alwatr/nano-server' + (this._config.port !== 80 ? ':' + this._config.port : ''));
this._logger.logMethodArgs('constructor', {config: this._config});

this._requestListener = this._requestListener.bind(this);
Expand Down Expand Up @@ -310,6 +311,7 @@ export class AlwatrNanoServer {

const connection = new AlwatrConnection(incomingMessage, serverResponse, {
allowAllOrigin: this._config.allowAllOrigin,
prefixPattern: this._config.prefixPattern,
});
const route = connection.url.pathname;

Expand Down Expand Up @@ -373,16 +375,15 @@ export class AlwatrNanoServer {
* Alwatr Connection
*/
export class AlwatrConnection {
static versionPattern = new RegExp('^/v[0-9]+');
protected prefixPattern = new RegExp('^/' + (this.config.prefixPattern ?? 'api'));
static _versionPattern = new RegExp('^/v[0-9]+');

/**
* Request URL.
*/
readonly url = new URL(
(this.incomingMessage.url ?? '')
.replace(this.prefixPattern, '')
.replace(AlwatrConnection.versionPattern, ''),
.replace(new RegExp('^/' + this._config.prefixPattern), '')
.replace(AlwatrConnection._versionPattern, ''),
'http://localhost/',
);

Expand All @@ -391,14 +392,14 @@ export class AlwatrConnection {
*/
readonly method = (this.incomingMessage.method ?? 'GET').toUpperCase() as Methods;

protected _logger = createLogger('alwatr-nano-server-connection');
protected _logger = createLogger('alwatr/nano-server-connection');

constructor(
public incomingMessage: IncomingMessage,
public serverResponse: ServerResponse,
public config: ConnectionConfig,
protected _config: ConnectionConfig,
) {
this._logger.logMethodArgs('new', {method: incomingMessage.method, url: incomingMessage.url});
this._logger.logMethodArgs('constructor', {method: incomingMessage.method, url: incomingMessage.url});
}

/**
Expand Down
11 changes: 9 additions & 2 deletions core/nano-server/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export interface NanoServerConfig {
* @default false
*/
allowAllOrigin: boolean;

/**
* API URL prefix pattern.
*
* @default `api`
*/
prefixPattern: string;
}

export interface ConnectionConfig {
Expand All @@ -68,14 +75,14 @@ export interface ConnectionConfig {
*
* @default false
*/
allowAllOrigin?: boolean;
allowAllOrigin: boolean;

/**
* API URL prefix pattern.
*
* @default `api`
*/
prefixPattern?: string;
prefixPattern: string;
}

export type ParamKeyType = 'string' | 'number' | 'boolean';
Expand Down

0 comments on commit f9d203e

Please sign in to comment.