-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
25 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,30 @@ | ||
/** | ||
* @adonisjs/http-server | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare module '@ioc:Adonis/Core/AsyncHttpContext' { | ||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
|
||
/** | ||
* Async Http context available during the lifecycle of HTTP requests | ||
*/ | ||
export interface AsyncHttpContextContract { | ||
/** | ||
* Returns the current HTTP context or null if called outside of a request. | ||
*/ | ||
getContext(): HttpContextContract | null | ||
|
||
/** | ||
* Returns the current HTTP context or throws if called outside of a request. | ||
*/ | ||
getContextOrFail(): HttpContextContract | ||
} | ||
|
||
const AsyncHttpContext: AsyncHttpContextContract | ||
export default AsyncHttpContext | ||
} |
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,49 @@ | ||
/** | ||
* @adonisjs/http-server | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../../adonis-typings/index.ts" /> | ||
|
||
import { AsyncLocalStorage } from 'async_hooks' | ||
import { Exception } from '@poppinss/utils' | ||
import { AsyncHttpContextContract } from '@ioc:Adonis/Core/AsyncHttpContext' | ||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
|
||
const adonisLocalStorage = new AsyncLocalStorage<InternalAsyncHttpContext>() | ||
|
||
export class InternalAsyncHttpContext { | ||
constructor(private ctx: HttpContextContract) {} | ||
|
||
public getContext() { | ||
return this.ctx | ||
} | ||
|
||
public run(callback: () => any) { | ||
return adonisLocalStorage.run(this, callback) | ||
} | ||
} | ||
|
||
class AsyncHttpContext implements AsyncHttpContextContract { | ||
public getContext() { | ||
const store = adonisLocalStorage.getStore() | ||
if (store) { | ||
return store.getContext() | ||
} | ||
return null | ||
} | ||
|
||
public getContextOrFail() { | ||
const store = adonisLocalStorage.getStore() | ||
if (store) { | ||
return store.getContext() | ||
} | ||
throw new Exception('AsyncHttpContext accessed outside of a request context') | ||
} | ||
} | ||
|
||
export const asyncHttpContext = new AsyncHttpContext() |
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