Skip to content

Commit d265510

Browse files
committed
Convert current middleware to new GlobalHttpHooks
1 parent ca31743 commit d265510

File tree

4 files changed

+18
-41
lines changed

4 files changed

+18
-41
lines changed

src/components/authentication/authentication.module.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
forwardRef,
3-
Global,
4-
MiddlewareConsumer,
5-
Module,
6-
NestModule,
7-
} from '@nestjs/common';
1+
import { forwardRef, Global, Module } from '@nestjs/common';
82
import { APP_INTERCEPTOR } from '@nestjs/core';
93
import { splitDb } from '~/core';
104
import { AuthorizationModule } from '../authorization/authorization.module';
@@ -54,12 +48,4 @@ import { SessionResolver } from './session.resolver';
5448
AuthenticationRepository,
5549
],
5650
})
57-
export class AuthenticationModule implements NestModule {
58-
constructor(
59-
private readonly currentUserProvider: EdgeDBCurrentUserProvider,
60-
) {}
61-
62-
configure(consumer: MiddlewareConsumer) {
63-
consumer.apply(this.currentUserProvider.use).forRoutes('*');
64-
}
65-
}
51+
export class AuthenticationModule {}

src/components/authentication/current-user.provider.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import { isUUID } from 'class-validator';
99
import { BehaviorSubject, identity } from 'rxjs';
1010
import { Session } from '~/common';
1111
import { EdgeDB, OptionsFn } from '~/core/edgedb';
12-
import { HttpMiddleware } from '~/core/http';
12+
import { GlobalHttpHook } from '~/core/http';
1313

1414
@Injectable()
15-
export class EdgeDBCurrentUserProvider
16-
implements HttpMiddleware, NestInterceptor
17-
{
15+
export class EdgeDBCurrentUserProvider implements NestInterceptor {
1816
// A map to transfer the options' holder
1917
// between the creation in middleware and the use in the interceptor.
2018
private readonly optionsHolderByRequest = new WeakMap<
21-
Parameters<HttpMiddleware['use']>[0],
19+
Parameters<GlobalHttpHook>[0]['raw'],
2220
BehaviorSubject<OptionsFn>
2321
>();
2422

2523
constructor(private readonly edgedb: EdgeDB) {}
2624

27-
use: HttpMiddleware['use'] = (req, res, next) => {
25+
@GlobalHttpHook()
26+
onRequest(...[req, _reply, next]: Parameters<GlobalHttpHook>) {
2827
// Create holder to use later to add current user to globals after it is fetched
2928
const optionsHolder = new BehaviorSubject<OptionsFn>(identity);
30-
this.optionsHolderByRequest.set(req, optionsHolder);
29+
this.optionsHolderByRequest.set(req.raw, optionsHolder);
3130

3231
// These options should apply to the entire HTTP/GQL operation.
3332
// Connect middleware is the only place we get a function which has all of
3433
// this in scope for the use of an ALS context.
3534
this.edgedb.usingOptions(optionsHolder, next);
36-
};
35+
}
3736

3837
/**
3938
* Connect the session to the options' holder

src/core/tracing/tracing.module.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
MiddlewareConsumer,
3-
Module,
4-
NestModule,
5-
OnModuleInit,
6-
} from '@nestjs/common';
1+
import { Module, OnModuleInit } from '@nestjs/common';
72
import { APP_INTERCEPTOR } from '@nestjs/core';
83
import XRay from 'aws-xray-sdk-core';
94
import { ConfigService } from '../config/config.service';
@@ -24,17 +19,13 @@ import { XRayMiddleware } from './xray.middleware';
2419
],
2520
exports: [TracingService],
2621
})
27-
export class TracingModule implements OnModuleInit, NestModule {
22+
export class TracingModule implements OnModuleInit {
2823
constructor(
2924
@Logger('xray') private readonly logger: ILogger,
3025
private readonly config: ConfigService,
3126
private readonly version: VersionService,
3227
) {}
3328

34-
configure(consumer: MiddlewareConsumer) {
35-
consumer.apply(XRayMiddleware).forRoutes('*');
36-
}
37-
3829
async onModuleInit() {
3930
// Don't use cls-hooked lib. It's old and Node has AsyncLocalStorage now.
4031
XRay.enableManualMode();

src/core/tracing/xray.middleware.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import {
66
} from '@nestjs/common';
77
import { GqlExecutionContext } from '@nestjs/graphql';
88
import XRay from 'aws-xray-sdk-core';
9-
import { HttpAdapter, HttpMiddleware as NestMiddleware } from '~/core/http';
9+
import { GlobalHttpHook, HttpAdapter } from '~/core/http';
1010
import { ConfigService } from '../config/config.service';
1111
import { Sampler } from './sampler';
1212
import { TracingService } from './tracing.service';
1313

1414
@Injectable()
15-
export class XRayMiddleware implements NestMiddleware, NestInterceptor {
15+
export class XRayMiddleware implements NestInterceptor {
1616
constructor(
1717
private readonly tracing: TracingService,
1818
private readonly sampler: Sampler,
@@ -23,12 +23,13 @@ export class XRayMiddleware implements NestMiddleware, NestInterceptor {
2323
/**
2424
* Setup root segment for request/response.
2525
*/
26-
use: NestMiddleware['use'] = (req, res, next) => {
26+
@GlobalHttpHook()
27+
onRequest(...[req, res, next]: Parameters<GlobalHttpHook>) {
2728
const traceData = XRay.utils.processTraceData(
2829
req.headers['x-amzn-trace-id'] as string | undefined,
2930
);
3031
const root = new XRay.Segment('cord', traceData.root, traceData.parent);
31-
const reqData = new XRay.middleware.IncomingRequestData(req);
32+
const reqData = new XRay.middleware.IncomingRequestData(req.raw);
3233
root.addIncomingRequestData(reqData);
3334
// Use public DNS as url instead of specific IP
3435
// @ts-expect-error xray library types suck
@@ -40,7 +41,7 @@ export class XRayMiddleware implements NestMiddleware, NestInterceptor {
4041
enumerable: false,
4142
});
4243

43-
res.on('finish', () => {
44+
res.raw.on('finish', () => {
4445
const status = res.statusCode.toString();
4546
if (status.startsWith('4')) {
4647
root.addErrorFlag();
@@ -57,7 +58,7 @@ export class XRayMiddleware implements NestMiddleware, NestInterceptor {
5758
});
5859

5960
this.tracing.segmentStorage.run(root, next);
60-
};
61+
}
6162

6263
/**
6364
* Determine if segment should be traced/sampled.

0 commit comments

Comments
 (0)