Skip to content

Commit

Permalink
refactor: namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch committed Sep 28, 2021
1 parent e4214a3 commit f121ccd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A continuation-local storage module compatible with [NestJS](https://nestjs.com/
- [API](#api)
- [Request ID](#request-id)
- [Custom CLS Middleware](#custom-cls-middleware)
- [Namespaces](#namespaces) (currently unsupported)
- [Namespaces](#namespaces) (experimental)

# Install

Expand Down Expand Up @@ -283,9 +283,9 @@ export class HelloClsMiddleware implements NestMiddleware {
> Note: Middleware options passed to `ClsModule.register` do not apply here, so you will need to implement any custom logic (like the generation of request ids) manually.
# (Namespaces)
# Namespaces (experimental)
> Warning: Namespace support is currently not ready and has no tests. This section serves as a documentation of the future API and can change any time.
> Warning: Namespace support is currently experimental no tests. This section serves as a documentation of the future API and can change any time.
The default CLS namespace that the `ClsService` provides should be enough for most application, but should you need it, this package provides (will provide) a way to use multiple CLS namespaces in order to be fully compatible with `cls-hooked`.
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './lib/cls-service-manager';
export * from './lib/cls.constants';
export * from './lib/cls.middleware';
export * from './lib/cls.module';
export * from './lib/cls.service';
export * from './lib/cls.decorators';
28 changes: 19 additions & 9 deletions src/lib/cls-service-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClassProvider, ValueProvider } from '@nestjs/common';
import { createNamespace, Namespace } from 'cls-hooked';
import { ClsService } from '..';
import { CLS_DEFAULT_NAMESPACE } from './cls.constants';
import { ClsService } from './cls.service';

export const getClsServiceToken = (namespace: string) =>
`ClsService-${namespace}`;
Expand All @@ -9,27 +10,36 @@ export class ClsServiceManager {
private static namespaces: Record<string, Namespace> = {};

private static clsServices: Map<string | typeof ClsService, ClsService> =
new Map();
new Map([
[
ClsService,
new ClsService(this.resolveNamespace(CLS_DEFAULT_NAMESPACE)),
],
]);

private static resolveNamespace(name: string) {
if (!this.namespaces[name]) {
this.namespaces[name] = createNamespace(name);
this.namespaces[name] = createNamespace(
name ?? CLS_DEFAULT_NAMESPACE,
);
}
return this.namespaces[name];
}

static setDefaultNamespace(name: string) {
this.clsServices.set(
ClsService,
new ClsService(this.resolveNamespace(name)),
);
}
// static setDefaultNamespace(name: string) {
// this.clsServices.set(
// ClsService,
// new ClsService(this.resolveNamespace(name)),
// );
// }

static addClsService(name: string) {
const service = new ClsService(this.resolveNamespace(name));
this.clsServices.set(
getClsServiceToken(name),
new ClsService(this.resolveNamespace(name)),
);
return service;
}

static getClsService(name?: string) {
Expand Down
1 change: 0 additions & 1 deletion src/lib/cls.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export class ClsModule implements NestModule {

static register(options?: ClsModuleOptions): DynamicModule {
options = { ...new ClsModuleOptions(), ...options };
ClsServiceManager.setDefaultNamespace(options.namespaceName);
ClsServiceManager.addClsService(options.namespaceName);
const clsMiddlewareOptions = {
...new ClsMiddlewareOptions(),
Expand Down
6 changes: 4 additions & 2 deletions src/lib/cls.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { createNamespace } from 'cls-hooked';
import { ClsServiceManager, CLS_DEFAULT_NAMESPACE } from '..';
import { ClsService } from './cls.service';

describe('ClsService', () => {
Expand All @@ -10,7 +10,9 @@ describe('ClsService', () => {
providers: [
{
provide: ClsService,
useValue: new ClsService(createNamespace('test')),
useValue: ClsServiceManager.addClsService(
CLS_DEFAULT_NAMESPACE,
),
},
],
}).compile();
Expand Down
1 change: 0 additions & 1 deletion test/http/http-fastify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { TestHttpController, TestHttpService } from './http.app';
@Module({
imports: [
ClsModule.register({
namespaceName: 'xxx',
middleware: { mount: true },
}),
],
Expand Down

0 comments on commit f121ccd

Please sign in to comment.