Code Monkey home page Code Monkey logo

Comments (8)

attilaorosz avatar attilaorosz commented on June 6, 2024 3

If you send the response yourself, make sure to return the response object from your controller. That way the framework knows you handled the response yourself and won't try to send it again.

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 6, 2024

Never seen this issue before, tho I’ve never tried with esbuild. I’m not sure if it is resolved but esbuild did not support reflect metadata in the past. Could you include the whole source you want to compile or setup a repro repo?

from routing-controllers.

yannickboy15 avatar yannickboy15 commented on June 6, 2024

Esbuild doesn't really support decorators, so I wrote a manual check for it:

// naively check if @ is present
function hasDecorator(fileContent, offset = 0) {
const atPosition = fileContent.indexOf("@", offset);

if (atPosition === -1) {
	return false;
}

if (atPosition === 1) {
	return true;
}

// ignore "@ and '@ as they are used in require('@org/repo')
if (["'", '"'].includes(fileContent.substr(atPosition - 1, 1))) {
	return hasDecorator(fileContent, atPosition + 1);
}

return true;

}`

Which then checks the following:

const serverConfig = Object.assign({}, config, {
	entry: "./src/server/main.ts",
	module: {
		rules: [
			{
				test: /\.tsx?$/,
				oneOf: [
					{
						test: (filePath) => {
							if (!filePath) {
								return false;
							}
							try {
								const fileContent = fs.readFileSync(filePath).toString();

								return !hasDecorator(fileContent);
							} catch (e) {
								return false;
							}
						},
						use: [
							{
								loader: "esbuild-loader",
							},
						],
					},
					{
						use: [
							{
								loader: "ts-loader",
								options: { transpileOnly: true },
							},
						],
					},
				],
				exclude: /node_modules/,
			},
		],
	},
	resolve: {
		extensions: [".tsx", ".ts", ".js", ".json"],
	},
	output: {
		filename: "index.js",
		path: path.resolve(__dirname, "../server/packages/gamemode"),
	},
	target: "node",
	devtool: "source-map",
	plugins: [new webpack.BannerPlugin(fs.readFileSync("./LICENSE.md", "utf8"))],
	stats: {
		warnings: false,
	},
});

I'm not sure if that's the cause?

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 6, 2024

My best guess is that no tree shaking happens in your build. So even tho your app does not use koa, it includes it in the build because it's part of the package. Does this happen if you use tsc only?

from routing-controllers.

yannickboy15 avatar yannickboy15 commented on June 6, 2024

Looking at some blog posts, they do mention it's possible:

https://www.hotovo.com/blog/speeding-up-webpack-builds-with-esbuild

Just wondering right, would there be a chance to also get like lightweight packages that don't rely on like a Koa for Express and no Express for Koa variants? Meaning the package from the build has no Koa/Express included depending on your npm install. So if we do the npm install for an Express variant only, the drivers of Koa and all don't come along etc.

from routing-controllers.

attilaorosz avatar attilaorosz commented on June 6, 2024

With tsc they don’t rely on the other framework because if it’s not used it’s omitted. The parent frameworks are included dynamically based on what you use. I have routing-controllers running in prod for multiple projects with express and we don’t have koa installed.

from routing-controllers.

yannickboy15 avatar yannickboy15 commented on June 6, 2024

It looks like Tsc did indeed the trick @attilaorosz ! Converted it all and it seems to work now. Only issue I am now receiving is the following:

UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:543:11)

Code is as follows:

@JsonController()
@injectable()
export class InformationController {
	constructor(@inject("IInformationService") private informationService: IInformationService) {}

	@Get("/test")
	gatherInformation(@Req() request: any, @Res() response: any) {
		try {
			response.send("Success");
		} catch (error: any) {
			response.status(500).send("Something broke!");
		}
	}
}

Got a clue why? The response returns Success correctly, yet it throws the warning.

from routing-controllers.

github-actions avatar github-actions commented on June 6, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from routing-controllers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.