Skip to content

Commit

Permalink
Merge f2e94f1 into 9fc4352
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 27, 2021
2 parents 9fc4352 + f2e94f1 commit eac4818
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
14 changes: 2 additions & 12 deletions packages/platform/common/src/domain/AnyToPromiseWithCtx.ts
@@ -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
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
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
19 changes: 19 additions & 0 deletions packages/platform/platform-express/cancel-request.js
@@ -0,0 +1,19 @@
const axios = require("axios");

const source = axios.CancelToken.source();

const options = {
method: "GET",
url: "http://localhost:8081/rest/hello/something",
cancelToken: source.token
};

setTimeout(() => {
console.log("==START CANCEL");
source.cancel();
}, 5000);

axios
.request(options)
.then((response) => console.log(response.data))
.catch((error) => console.error(error));
7 changes: 7 additions & 0 deletions packages/platform/platform-express/test/app/index.ts
@@ -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 eac4818

Please sign in to comment.