![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@nestjs-steroids/async-context
Advanced tools
npm install @nestjs-steroids/async-context
import { Module } from '@nestjs/common';
import { AsyncHooksModule } from '@nestjs-steroids/async-context';
@Module({
imports: [
AsyncHooksModule
],
})
export class AppModule {}
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { AsyncContext } from '@nestjs-steroids/async-context';
import { v4 as uuid } from 'uuid';
@Injectable()
export class TraceMiddleware implements NestMiddleware {
constructor(private readonly asyncHook: AsyncContext) {}
use(req: Request, res: Response, next: NextFunction) {
// Register AsyncContext
// Without registration accessing to AsyncContext (get or set methods), will throw an error
this.asyncHook.register();
// Putting up UUID string by key 'traceId'
this.asyncHook.set<string, string>('traceId', uuid());
// Retrieving value by key
this.asyncContext.get<string, string>('traceId')
next();
}
}
Let's track traceId
in every log message.
First of all define trace interceptor than register async context
trace.interceptor.ts
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { AsyncContext } from '@nestjs-steroids/async-context';
import { v4 as uuid } from 'uuid';
@Injectable()
export class TraceInterceptor implements NestInterceptor {
constructor(private readonly asyncHook: AsyncContext) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
this.asyncHook.register(); // <-- Register async context
this.asyncHook.set<string, string>('traceId', uuid()); // <-- Define traceId
return next.handle();
}
}
Then we need to register AsyncHooksModule
and TraceInterceptor
app.module.ts
import { APP_INTERCEPTOR } from '@nestjs/core';
import { Logger, Module } from '@nestjs/common';
import { AsyncHooksModule } from '@nestjs-steroids/async-context';
import { TraceInterceptor } from './trace.interceptor';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [AsyncHooksModule], // <-- Register AsyncHooksModule
controllers: [AppController],
providers: [
{
provide: Logger,
useValue: new Logger(),
},
{ // <-- Setup interceptor for entire module
provide: APP_INTERCEPTOR,
useClass: TraceInterceptor,
},
AppService,
],
})
export class AppModule {}
Last step, inject AsyncContex
into controller, service, etc... and use it
app.controller.ts
import { Controller, Get, Logger } from '@nestjs/common';
import { AppService } from './app.service';
import { AsyncContext } from '@nestjs-steroids/async-context';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly asyncContext: AsyncContext, // <-- Inject AsyncContext
private readonly logger: Logger,
) {}
@Get()
getHello(): string {
this.logger.log(
'AppController.getHello',
this.asyncContext.get<string, string>('traceId'), // <-- Usage of AsyncContext
);
process.nextTick(() => {
this.logger.log(
'AppController.getHello -> nextTick',
this.asyncContext.get<string, string>('traceId'),
);
setTimeout(() => {
this.logger.log(
'AppController.getHello -> nextTick -> setTimeout',
this.asyncContext.get<string, string>('traceId'),
);
}, 0);
});
return this.appService.getHello();
}
}
[Nest] 30483 - 07/04/2020, 12:21:06 PM [NestFactory] Starting Nest application...
[Nest] 30483 - 07/04/2020, 12:21:06 PM [InstanceLoader] AsyncHooksModule dependencies initialized +8ms
[Nest] 30483 - 07/04/2020, 12:21:06 PM [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 30483 - 07/04/2020, 12:21:06 PM [RoutesResolver] AppController {}: +3ms
[Nest] 30483 - 07/04/2020, 12:21:06 PM [RouterExplorer] Mapped {, GET} route +1ms
[Nest] 30483 - 07/04/2020, 12:21:06 PM [NestApplication] Nest application successfully started +2ms
[Nest] 30483 - 07/04/2020, 12:21:11 PM [1cf8ad02-7bae-4fa7-963d-66238c94abd3] AppController.getHello
[Nest] 30483 - 07/04/2020, 12:21:11 PM [1cf8ad02-7bae-4fa7-963d-66238c94abd3] AppController.getHello -> nextTick
[Nest] 30483 - 07/04/2020, 12:21:11 PM [1cf8ad02-7bae-4fa7-963d-66238c94abd3] AppController.getHello -> nextTick -> setTimeout
FAQs
NestJS Async Context based on async_hooks
The npm package @nestjs-steroids/async-context receives a total of 4,420 weekly downloads. As such, @nestjs-steroids/async-context popularity was classified as popular.
We found that @nestjs-steroids/async-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.