Skip to content

Add global middleware and guard support#675

Merged
notaphplover merged 6 commits intomainfrom
feat/add-global-middleware-guard
May 1, 2025
Merged

Add global middleware and guard support#675
notaphplover merged 6 commits intomainfrom
feat/add-global-middleware-guard

Conversation

@adrianmjim
Copy link
Member

Added

  • Add applyGlobalMiddleware function to InversifyHttpAdapter
  • Add applyGlobalFunction function to InversifyHttpAdapter

Refactored

  • Refactor InversifyExpressHttpAdapter, InversifyFastifyHttpAdapter and InversifyHonoHttpAdapter to use global middlewares and guards

Motivation

This PR resolves #518 and #519

@changeset-bot
Copy link

changeset-bot bot commented Apr 27, 2025

⚠️ No Changeset found

Latest commit: 1879ae3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

Benchmarks results:

> root@ benchmarks:http:cjs /home/runner/work/monorepo/monorepo
> pnpm run --filter "@inversifyjs/http-benchmarks" benchmarks:run:cjs


> @inversifyjs/http-benchmarks@ benchmarks:run:cjs /home/runner/work/monorepo/monorepo/packages/http/tools/http-benchmarks
> BUILD_TARGET=cjs ./bin/run-cjs.cjs

[CJS] Running benchmarks...

Express Basic Get Request
┌─────────┬───────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                 │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼───────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyExpress''112.878''44.360''3943.387'             │ 43622   │
│ 1       │ 'express''102.856''43.936''4356.461'             │ 48148   │
│ 2       │ 'NestJSExpress''113.005''45.848''3953.930'             │ 43649   │
└─────────┴───────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyExpress vs express Speedup: 0.905x
currentInversifyExpress vs NestJSExpress Speedup: 0.997x

Express v4 Basic Get Request
┌─────────┬────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                  │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyExpress4''100.731''43.099''4469.690'             │ 49283   │
│ 1       │ 'express4''97.542''42.493''4618.384'             │ 50989   │
└─────────┴────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyExpress4 vs express4 Speedup: 0.968x

Fastify Basic Get Request
┌─────────┬───────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                 │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼───────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyFastify''43.111''38.149''11641.916'            │ 117652  │
│ 1       │ 'fastify''34.203''29.422''14562.471'            │ 146487  │
│ 2       │ 'NestJSFastify''38.211''33.385''13242.147'            │ 134241  │
└─────────┴───────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyFastify vs fastify Speedup: 0.799x
currentInversifyFastify vs NestJSFastify Speedup: 0.879x

Hono Basic Get Request
┌─────────┬────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name              │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyHono''40.815''36.361''12276.693'            │ 123321  │
│ 1       │ 'hono''37.041''31.568''13459.347'            │ 135594  │
└─────────┴────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyHono vs hono Speedup: 0.912x

Copy link
Member

@notaphplover notaphplover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good. Let's update fastify to allow non Error rejections and we're ready to merge

@github-actions
Copy link
Contributor

github-actions bot commented May 1, 2025

Benchmarks results:

> root@ benchmarks:http:cjs /home/runner/work/monorepo/monorepo
> pnpm run --filter "@inversifyjs/http-benchmarks" benchmarks:run:cjs


> @inversifyjs/http-benchmarks@ benchmarks:run:cjs /home/runner/work/monorepo/monorepo/packages/http/tools/http-benchmarks
> BUILD_TARGET=cjs ./bin/run-cjs.cjs

[CJS] Running benchmarks...

Express Basic Get Request
┌─────────┬───────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                 │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼───────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyExpress''107.968''45.017''4140.502'             │ 45798   │
│ 1       │ 'express''97.807''42.737''4596.857'             │ 50819   │
│ 2       │ 'NestJSExpress''106.671''45.485''4197.882'             │ 46376   │
└─────────┴───────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyExpress vs express Speedup: 0.901x
currentInversifyExpress vs NestJSExpress Speedup: 0.986x

Express v4 Basic Get Request
┌─────────┬────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                  │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyExpress4''93.954''42.014''4800.714'             │ 53052   │
│ 1       │ 'express4''92.860''41.697''4859.402'             │ 53696   │
└─────────┴────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyExpress4 vs express4 Speedup: 0.988x

Fastify Basic Get Request
┌─────────┬───────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name                 │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼───────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyFastify''40.915''33.324''12283.481'            │ 124041  │
│ 1       │ 'fastify''31.117''26.140''15868.942'            │ 159268  │
│ 2       │ 'NestJSFastify''35.583''30.869''14045.988'            │ 141736  │
└─────────┴───────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyFastify vs fastify Speedup: 0.774x
currentInversifyFastify vs NestJSFastify Speedup: 0.875x

Hono Basic Get Request
┌─────────┬────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name              │ Latency avg (ms) │ Latency med (ms) │ Throughput avg (req/s) │ Samples │
├─────────┼────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼─────────┤
│ 0       │ 'currentInversifyHono''37.468''30.855''13335.761'            │ 134277  │
│ 1       │ 'hono''33.548''28.220''14835.517'            │ 149370  │
└─────────┴────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴─────────┘
currentInversifyHono vs hono Speedup: 0.899x

@notaphplover notaphplover self-requested a review May 1, 2025 09:35
@notaphplover notaphplover merged commit 23b968f into main May 1, 2025
9 checks passed
@notaphplover notaphplover deleted the feat/add-global-middleware-guard branch May 1, 2025 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Implement global middlewares

2 participants