mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat: supporting fine async storage control
This commit is contained in:
committed by
Thiago Santos
parent
138577e50a
commit
f98149853e
@@ -20,6 +20,12 @@ export class GuardsConsumer {
|
||||
|
||||
for (const guard of guards) {
|
||||
const result = guard.canActivate(context);
|
||||
if (typeof result === 'boolean') {
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (await this.pickResult(result)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { expect } from 'chai';
|
||||
import { of } from 'rxjs';
|
||||
import { GuardsConsumer } from '../../guards/guards-consumer';
|
||||
import { AsyncLocalStorage } from 'async_hooks';
|
||||
|
||||
describe('GuardsConsumer', () => {
|
||||
let consumer: GuardsConsumer;
|
||||
@@ -44,6 +45,34 @@ describe('GuardsConsumer', () => {
|
||||
expect(canActivate).to.be.true;
|
||||
});
|
||||
});
|
||||
describe('when sync guards initialize AsyncLocalStorages', () => {
|
||||
it('should keep local storages accessible', async () => {
|
||||
const storage1 = new AsyncLocalStorage<number>();
|
||||
const storage2 = new AsyncLocalStorage<number>();
|
||||
const canActivate = await consumer.tryActivate(
|
||||
[
|
||||
{
|
||||
canActivate: () => {
|
||||
storage1.enterWith(1);
|
||||
return true;
|
||||
},
|
||||
},
|
||||
{
|
||||
canActivate: () => {
|
||||
storage2.enterWith(2);
|
||||
return true;
|
||||
},
|
||||
},
|
||||
],
|
||||
[],
|
||||
{ constructor: null },
|
||||
null!,
|
||||
);
|
||||
expect(canActivate).to.be.true;
|
||||
expect(storage1.getStore()).to.equal(1);
|
||||
expect(storage2.getStore()).to.equal(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('pickResult', () => {
|
||||
|
||||
Reference in New Issue
Block a user