Skip to content

Commit

Permalink
fix(common): Fix issue when request is canceled
Browse files Browse the repository at this point in the history
Closes: #1689
  • Loading branch information
Romakita committed Dec 29, 2021
1 parent 36d57fe commit d88fcac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
14 changes: 2 additions & 12 deletions packages/platform/common/src/domain/AnyToPromiseWithCtx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AnyPromiseResult, AnyToPromise, AnyToPromiseStatus} from "@tsed/core";
import {AnyPromiseResult, AnyToPromise} from "@tsed/core";
import {PlatformContext} from "./PlatformContext";

/**
Expand All @@ -22,17 +22,7 @@ export class AnyToPromiseWithCtx extends AnyToPromise {
isDone(): boolean {
const {$ctx} = this;

if (!$ctx?.isDone()) {
if ($ctx?.request.isAborted() || $ctx?.response.isDone()) {
this.destroy();

if (this.status === AnyToPromiseStatus.PENDING) {
this.status = AnyToPromiseStatus.RESOLVED;
}
}
}

return super.isDone();
return $ctx?.isDone() || super.isDone();
}

async call(cb: Function): Promise<AnyPromiseResult<any>> {
Expand Down
10 changes: 9 additions & 1 deletion packages/platform/common/src/domain/PlatformContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ export class PlatformContext extends DIContext implements ContextMethods {
}

isDone() {
return !this.request || !this.response;
if (!this.request || !this.response) {
return true;
}

if (this.request?.isAborted()) {
return true;
}

return this.response?.isDone();
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/platform/common/src/services/PlatformHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class PlatformHandler {
protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {
const {$ctx, metadata, err, handler} = requestOptions;
// istanbul ignore next
if (!$ctx) {
if (!$ctx || $ctx.isDone()) {
$log.error({
name: "HEADERS_SENT",
message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`
Expand Down Expand Up @@ -170,7 +170,7 @@ export class PlatformHandler {
}

// istanbul ignore next
if (!$ctx?.response?.isHeadersSent || $ctx.response.isHeadersSent()) {
if (!$ctx || $ctx?.isDone()) {
$log.warn({
name: "HEADERS_SENT",
message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,
Expand All @@ -194,9 +194,8 @@ export class PlatformHandler {
protected async onSuccess(data: any, requestOptions: OnRequestOptions) {
const {metadata, $ctx, next} = requestOptions;

// TODO see this control is necessary
// istanbul ignore next
if ($ctx.request.isAborted() || $ctx.response.isDone()) {
if ($ctx?.isDone()) {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/platform/platform-express/test/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {$log, BodyParams, Controller, Get, PlatformResponse, Post, QueryParams, Res} from "@tsed/common";
import {Returns} from "@tsed/schema";
import {promisify} from "util";
import {agent, SuperAgentStatic} from "superagent";
import {PlatformExpress} from "../../src";
import {Server} from "./Server";
Expand All @@ -12,6 +13,12 @@ if (process.env.NODE_ENV !== "test") {
return {test: "Hello world"};
}

@Get('/something')
@Returns(204)
public async getSomething(): Promise<void> {
await promisify(setTimeout)(10000);
}

@Get("/image")
@Returns(200).Header("X-Content-Type-Options", "nosniff")
async getGoogle(@Res() res: PlatformResponse) {
Expand Down

0 comments on commit d88fcac

Please sign in to comment.