mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-10 04:00:44 +08:00
change log in
This commit is contained in:
parent
9cf026f605
commit
8ca42dd973
211 changed files with 48417 additions and 109 deletions
37
uds-auth/lib/context.js
Normal file
37
uds-auth/lib/context.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { AsyncLocalStorage } from 'node:async_hooks'
|
||||
|
||||
/**
|
||||
* Async local storage for user context
|
||||
* Ensures user context is isolated per request even in concurrent scenarios
|
||||
*/
|
||||
const userContextStorage = new AsyncLocalStorage()
|
||||
|
||||
/**
|
||||
* Get the current user context from async local storage
|
||||
* @returns {object|undefined} User context object or undefined
|
||||
*/
|
||||
export function getUserContext() {
|
||||
return userContextStorage.getStore()
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a function with a specific user context
|
||||
* @param {object} userContext - User context object
|
||||
* @param {Function} fn - Async function to execute
|
||||
* @returns {Promise<any>} Result of the function
|
||||
*/
|
||||
export async function withUserContext(userContext, fn) {
|
||||
return userContextStorage.run(userContext, fn)
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorator/helper for injecting user context into request handlers
|
||||
* @param {Function} handler - Request handler function
|
||||
* @returns {Function} Wrapped handler
|
||||
*/
|
||||
export function withInjectedUserContext(handler) {
|
||||
return async function(ctx, ...args) {
|
||||
const userContext = ctx.userContext || null
|
||||
return userContextStorage.run(userContext, () => handler(ctx, ...args))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue