lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.32k stars 481 forks source link

Delay between @security execution and main API logic only when deployed #1647

Open vinayak-scrrum opened 1 week ago

vinayak-scrrum commented 1 week ago

Sorting

Versions:

Issue Description: I am experiencing a 5-6 second delay between the execution of the @security decorator and the main API logic, but only when the application is deployed (on azure app service). This issue does not occur locally.

Steps to Reproduce:

  1. I consoled the time at each step from the authentication middleware to the API response.
  2. I have 3 securities in the @security decorator, so the authentication middleware is called 3 times.
  3. The authentication middleware combined execution takes only 1 second.
  4. There is a gap of 4-5 seconds between the last console log in the authentication middleware and the first console log in the API logic.

API Logic:

@Response<ErrorResponse>("Invalid Token")
@Security({
    "security1": ["scope1", "scope2", "scope3"],
    "security2": [],
    "security3": ["scope1", "scope2"]
})
@Post("/")
public async addDemo(@Request() req: IRequest, @Body() reqBody: IDemoRequest): Promise<SuccessResponseWrapped<DemoDocumentJSON>> {
    try {
        console.log(`STEP 1 ${new Date()}`);
        let demo = await this.addDemoService.create(req, reqBody);
        console.log(`STEP 17 ${new Date()}`);
        return this._successResponse(req, demo, "Demo added successfully", 200);
    } catch (error) {
        throw new MyError(error.code, error.message);
    }
}

Authentication Middleware:

export async function expressAuthentication(request: IRequest, securityName: string, scopes?: string[]): Promise<any> {
    try {
        console.log(`Authentication middleware Start - ${new Date()}`);

        if (securityName === "security1") {
            // security1 logic
        } else if (securityName === "security2") {
            // security2 logic
        } else if (securityName === "security3") {
            // security3 logic
        } else {
            // other logic
        }

        console.log(`Authentication middleware End - ${new Date()}`);
    } catch (error) {
        throw new MyError(error);
    }
}

Request for Help: Can anyone help me understand why there is such a gap and how I can fix it? Any insights or suggestions would be greatly appreciated.

Thank you!

github-actions[bot] commented 1 week ago

Hello there vinayak-scrrum 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀