-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/release'
- Loading branch information
Showing
26 changed files
with
266 additions
and
108 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
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
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,37 @@ | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
|
||
interface ContextData { | ||
[k: string]: unknown; | ||
} | ||
|
||
class AppContext { | ||
private storage = new AsyncLocalStorage<ContextData>(); | ||
|
||
private getContextObject() { | ||
const data = this.storage.getStore(); | ||
if (!data) throw new Error('Accessing nonexistent async context'); | ||
return data; | ||
} | ||
|
||
async run(cb: () => Promise<void>, data: ContextData = {}): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.storage.run(data, () => { | ||
cb() | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}); | ||
} | ||
|
||
get(key: string) { | ||
return this.getContextObject()[key]; | ||
} | ||
|
||
set(key: string, value: unknown) { | ||
this.getContextObject()[key] = value; | ||
} | ||
} | ||
|
||
const appContext = new AppContext(); | ||
|
||
export { appContext }; |
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 { Request, Response, NextFunction } from 'express'; | ||
import { appContext } from 'api/utils/AppContext'; | ||
|
||
const appContextMiddleware = (_req: Request, _res: Response, next: NextFunction) => { | ||
appContext | ||
.run(async () => { | ||
next(); | ||
}) | ||
.catch(next); | ||
}; | ||
|
||
export { appContextMiddleware }; |
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,12 +1,10 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { tenants } from 'api/tenants/tenantContext'; | ||
import { appContext } from 'api/utils/AppContext'; | ||
import { config } from 'api/config'; | ||
|
||
const multitenantMiddleware = (req: Request, _res: Response, next: NextFunction) => { | ||
tenants | ||
.run(async () => { | ||
next(); | ||
}, req.get('tenant')) | ||
.catch(next); | ||
appContext.set('tenant', req.get('tenant') || config.defaultTenant.name); | ||
next(); | ||
}; | ||
|
||
export { multitenantMiddleware }; |
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,66 @@ | ||
import { appContext } from '../AppContext'; | ||
|
||
describe('appContext', () => { | ||
describe('when running the callback inside a context', () => { | ||
it('it should access the given values', async () => { | ||
await appContext.run( | ||
async () => { | ||
expect(appContext.get('key1')).toBe('value1'); | ||
expect(appContext.get('key2')).toBe('value2'); | ||
}, | ||
{ | ||
key1: 'value1', | ||
key2: 'value2', | ||
} | ||
); | ||
}); | ||
|
||
it('it should return undefined if accessing an unexising key', async () => { | ||
await appContext.run( | ||
async () => { | ||
expect(appContext.get('non-existing')).toBe(undefined); | ||
}, | ||
{ | ||
key: 'value', | ||
} | ||
); | ||
}); | ||
|
||
it('it should set a new key', async () => { | ||
await appContext.run(async () => { | ||
expect(appContext.get('someKey')).toBe(undefined); | ||
appContext.set('someKey', 'someValue'); | ||
expect(appContext.get('someKey')).toBe('someValue'); | ||
}); | ||
}); | ||
|
||
it('it should overwrite existing keys', async () => { | ||
await appContext.run( | ||
async () => { | ||
expect(appContext.get('someKey')).toBe('previous'); | ||
appContext.set('someKey', 'someValue'); | ||
expect(appContext.get('someKey')).toBe('someValue'); | ||
}, | ||
{ | ||
someKey: 'previous', | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('when outside a context', () => { | ||
const error = new Error('Accessing nonexistent async context'); | ||
|
||
it('should throw on get', () => { | ||
expect(() => { | ||
appContext.get('somKey'); | ||
}).toThrow(error); | ||
}); | ||
|
||
it('should throw on set', () => { | ||
expect(() => { | ||
appContext.set('somKey', 'someValue'); | ||
}).toThrow(error); | ||
}); | ||
}); | ||
}); |
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,31 @@ | ||
import request from 'supertest'; | ||
import express, { Application, Request, Response, NextFunction } from 'express'; | ||
import { appContextMiddleware } from '../appContextMiddleware'; | ||
import { appContext } from '../AppContext'; | ||
|
||
const testingRoutes = (app: Application) => { | ||
app.get('/api/testGET', (_req, res, next) => { | ||
res.json(appContext.get('someKey')); | ||
next(); | ||
}); | ||
}; | ||
|
||
const helperMiddleware = (req: Request, _res: Response, next: NextFunction) => { | ||
appContext.set('someKey', req.get('someHeader')); | ||
next(); | ||
}; | ||
|
||
describe('appcontext middleware', () => { | ||
it('should execute next middlewares inside an async context', async () => { | ||
const app: Application = express(); | ||
app.use(appContextMiddleware); | ||
app.use(helperMiddleware); | ||
testingRoutes(app); | ||
|
||
const response = await request(app) | ||
.get('/api/testGET') | ||
.set('someHeader', 'test'); | ||
|
||
expect(response.text).toBe(JSON.stringify('test')); | ||
}); | ||
}); |
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,17 @@ | ||
/*global page*/ | ||
|
||
export default async () => { | ||
await page.addStyleTag({ | ||
content: ` | ||
*, | ||
*::after, | ||
*::before { | ||
transition-delay: 0s !important; | ||
transition-duration: 0s !important; | ||
animation-delay: -0.0001s !important; | ||
animation-duration: 0s !important; | ||
animation-play-state: paused !important; | ||
caret-color: transparent !important; | ||
}`, | ||
}); | ||
}; |
Oops, something went wrong.