-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9697 from nestjs/feat/durable-providers
feat(core): add durable providers feature
- Loading branch information
Showing
18 changed files
with
482 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { ContextIdFactory } from '@nestjs/core'; | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { DurableContextIdStrategy } from '../src/durable/durable-context-id.strategy'; | ||
import { DurableModule } from '../src/durable/durable.module'; | ||
|
||
describe('Durable providers', () => { | ||
let server: any; | ||
let app: INestApplication; | ||
|
||
before(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [DurableModule], | ||
}).compile(); | ||
|
||
app = moduleRef.createNestApplication(); | ||
server = app.getHttpServer(); | ||
await app.init(); | ||
|
||
ContextIdFactory.apply(new DurableContextIdStrategy()); | ||
}); | ||
|
||
describe('when service is durable', () => { | ||
const performHttpCall = (tenantId: number, end: (err?: any) => void) => | ||
request(server) | ||
.get('/durable') | ||
.set({ ['x-tenant-id']: tenantId }) | ||
.end((err, res) => { | ||
if (err) return end(err); | ||
end(res); | ||
}); | ||
|
||
it(`should share durable providers per tenant`, async () => { | ||
let result: request.Response; | ||
result = await new Promise<request.Response>(resolve => | ||
performHttpCall(1, resolve), | ||
); | ||
expect(result.text).equal('Hello world! Counter: 1'); | ||
|
||
result = await new Promise<request.Response>(resolve => | ||
performHttpCall(1, resolve), | ||
); | ||
expect(result.text).equal('Hello world! Counter: 2'); | ||
|
||
result = await new Promise<request.Response>(resolve => | ||
performHttpCall(1, resolve), | ||
); | ||
expect(result.text).equal('Hello world! Counter: 3'); | ||
}); | ||
|
||
it(`should create per-tenant DI sub-tree`, async () => { | ||
let result: request.Response; | ||
result = await new Promise<request.Response>(resolve => | ||
performHttpCall(4, resolve), | ||
); | ||
expect(result.text).equal('Hello world! Counter: 1'); | ||
|
||
result = await new Promise<request.Response>(resolve => | ||
performHttpCall(5, resolve), | ||
); | ||
expect(result.text).equal('Hello world! Counter: 1'); | ||
|
||
result = await new Promise<request.Response>(resolve => | ||
performHttpCall(6, resolve), | ||
); | ||
expect(result.text).equal('Hello world! Counter: 1'); | ||
}); | ||
}); | ||
|
||
after(async () => { | ||
ContextIdFactory['strategy'] = undefined; | ||
await app.close(); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
integration/scopes/src/durable/durable-context-id.strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ChildContextIdInfo, ContextId, ContextIdStrategy } from '@nestjs/core'; | ||
import { Request } from 'express'; | ||
|
||
const tenants = new Map<string, ContextId>(); | ||
|
||
export class DurableContextIdStrategy implements ContextIdStrategy { | ||
attach(contextId: ContextId, request: Request) { | ||
const tenantId = request.headers['x-tenant-id'] as string; | ||
let tenantSubTreeId: ContextId; | ||
|
||
if (tenants.has(tenantId)) { | ||
tenantSubTreeId = tenants.get(tenantId); | ||
} else { | ||
tenantSubTreeId = { id: +tenantId } as ContextId; | ||
tenants.set(tenantId, tenantSubTreeId); | ||
} | ||
|
||
return (info: ChildContextIdInfo) => | ||
info.isTreeDurable ? tenantSubTreeId : contextId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { DurableService } from './durable.service'; | ||
|
||
@Controller('durable') | ||
export class DurableController { | ||
constructor(private readonly durableService: DurableService) {} | ||
|
||
@Get() | ||
greeting(): string { | ||
return this.durableService.greeting(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DurableController } from './durable.controller'; | ||
import { DurableService } from './durable.service'; | ||
|
||
@Module({ | ||
controllers: [DurableController], | ||
providers: [DurableService], | ||
}) | ||
export class DurableModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable, Scope } from '@nestjs/common'; | ||
|
||
@Injectable({ scope: Scope.REQUEST, durable: true }) | ||
export class DurableService { | ||
public instanceCounter = 0; | ||
|
||
greeting() { | ||
++this.instanceCounter; | ||
return `Hello world! Counter: ${this.instanceCounter}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SCOPE_OPTIONS_METADATA } from '@nestjs/common/constants'; | ||
import { Type } from '@nestjs/common/interfaces/type.interface'; | ||
|
||
export function isDurable(provider: Type<unknown>): boolean | undefined { | ||
const metadata = Reflect.getMetadata(SCOPE_OPTIONS_METADATA, provider); | ||
return metadata && metadata.durable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export * from './container'; | ||
export * from './inquirer'; | ||
export { ContextId } from './instance-wrapper'; | ||
export { ChildContextIdInfo, ContextId } from './instance-wrapper'; | ||
export * from './lazy-module-loader'; | ||
export * from './module-ref'; | ||
export * from './modules-container'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.