Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rollback changes introduced by v6.116.0 on the async hook context #1973

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 20 additions & 18 deletions packages/platform/common/src/services/PlatformHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,30 +133,32 @@ export class PlatformHandler {
return;
}

const resolver = new AnyToPromiseWithCtx({$ctx, err});
return $ctx.runInContext(async () => {
const resolver = new AnyToPromiseWithCtx({$ctx, err});

try {
const {state, data, status, headers} = await resolver.call(handler);
try {
const {state, data, status, headers} = await resolver.call(handler);

if (state === AnyToPromiseStatus.RESOLVED) {
if (status) {
$ctx.response.status(status);
}
if (state === AnyToPromiseStatus.RESOLVED) {
if (status) {
$ctx.response.status(status);
}

if (headers) {
$ctx.response.setHeaders(headers);
}
if (headers) {
$ctx.response.setHeaders(headers);
}

if (data !== undefined) {
$ctx.data = data;
}
if (data !== undefined) {
$ctx.data = data;
}

// Can be canceled by the handler itself
return await this.onSuccess($ctx.data, requestOptions);
// Can be canceled by the handler itself
return await this.onSuccess($ctx.data, requestOptions);
}
} catch (er) {
return this.onError(er, requestOptions);
}
} catch (er) {
return this.onError(er, requestOptions);
}
});
}

protected async onError(er: Error, requestOptions: OnRequestOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type multer from "multer";
import Express, {RouterOptions} from "express";
import type {PlatformViews} from "@tsed/platform-views";
import {
createContext,
InjectorService,
Expand All @@ -14,17 +11,19 @@ import {
PlatformMulterSettings,
PlatformRequest,
PlatformResponse,
PlatformStaticsOptions,
runInContext
PlatformStaticsOptions
} from "@tsed/common";
import {promisify} from "util";
import {Env, isFunction, nameOf, Type} from "@tsed/core";
import type {PlatformViews} from "@tsed/platform-views";
import {OptionsJson, OptionsText, OptionsUrlencoded} from "body-parser";
import Express, {RouterOptions} from "express";
import type multer from "multer";
import {promisify} from "util";
import {PlatformExpressStaticsOptions} from "../interfaces/PlatformExpressStaticsOptions";
import {staticsMiddleware} from "../middlewares/staticsMiddleware";
import {PlatformExpressHandler} from "../services/PlatformExpressHandler";
import {PlatformExpressResponse} from "../services/PlatformExpressResponse";
import {PlatformExpressRequest} from "../services/PlatformExpressRequest";
import {staticsMiddleware} from "../middlewares/staticsMiddleware";
import {PlatformExpressStaticsOptions} from "../interfaces/PlatformExpressStaticsOptions";
import {OptionsJson, OptionsText, OptionsUrlencoded} from "body-parser";
import {PlatformExpressResponse} from "../services/PlatformExpressResponse";

declare module "express" {
export interface Request {
Expand Down Expand Up @@ -158,9 +157,9 @@ export class PlatformExpress implements PlatformAdapter<Express.Application, Exp
const app = this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;

app.getApp().use(async (request: any, response: any, next: any) => {
const $ctx = await invoke({request, response});
await invoke({request, response});

return $ctx.runInContext(next);
return next();
});

return this;
Expand Down
21 changes: 10 additions & 11 deletions packages/platform/platform-koa/src/components/PlatformKoa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ import {
PlatformMulterSettings,
PlatformRequest,
PlatformResponse,
PlatformStaticsOptions,
runInContext
PlatformStaticsOptions
} from "@tsed/common";
import {isFunction, Type} from "@tsed/core";
import Koa, {Context, Next} from "koa";
import koaBodyParser, {Options} from "koa-bodyparser";
// @ts-ignore
import koaQs from "koa-qs";
import send from "koa-send";
import {resourceNotFoundMiddleware} from "../middlewares/resourceNotFoundMiddleware";
import {PlatformKoaResponse} from "../services/PlatformKoaResponse";
import {PlatformKoaRequest} from "../services/PlatformKoaRequest";
import {staticsMiddleware} from "../middlewares/staticsMiddleware";
import {PlatformKoaHandler} from "../services/PlatformKoaHandler";
import {PlatformKoaRequest} from "../services/PlatformKoaRequest";
import {PlatformKoaResponse} from "../services/PlatformKoaResponse";
import {getMulter} from "../utils/multer";
import {staticsMiddleware} from "../middlewares/staticsMiddleware";
import send from "koa-send";
// @ts-ignore
import koaQs from "koa-qs";
import koaBodyParser, {Options} from "koa-bodyparser";

declare global {
namespace TsED {
Expand Down Expand Up @@ -129,13 +128,13 @@ export class PlatformKoa implements PlatformAdapter<Koa, KoaRouter> {
const invoke = createContext(this.injector);

app.getApp().use(async (ctx: Context, next: Next) => {
const $ctx = await invoke({
await invoke({
request: ctx.request as any,
response: ctx.response as any,
ctx
});

return $ctx.runInContext(next);
return next();
});

return this;
Expand Down