session-context
Advanced tools
Comparing version
@@ -5,1 +5,2 @@ import { AsyncLocalStorage } from 'node:async_hooks'; | ||
export declare const runSession: <R, TArgs extends unknown[]>(callback: (...args: TArgs) => R, ...args: TArgs) => R; | ||
export declare const setProcessEnv: (env: unknown) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runSession = exports.getSessionContext = exports.createSessionContext = void 0; | ||
exports.setProcessEnv = exports.runSession = exports.getSessionContext = exports.createSessionContext = void 0; | ||
const node_async_hooks_1 = require("node:async_hooks"); | ||
@@ -16,5 +16,8 @@ const global = globalThis; | ||
if (!store) { | ||
throw new Error('Global store is not initialized'); | ||
throw new Error('Session context is not initialized'); | ||
} | ||
return store; | ||
if (!store.appContext) { | ||
store.appContext = {}; | ||
} | ||
return store.appContext; | ||
}; | ||
@@ -27,2 +30,17 @@ exports.getSessionContext = getSessionContext; | ||
exports.runSession = runSession; | ||
const setProcessEnv = (env) => { | ||
const store = global.__context.getStore(); | ||
if (!store) { | ||
throw new Error('Session context is not initialized'); | ||
} | ||
store.env = env; | ||
if (!Object.getOwnPropertyDescriptor(process, 'env')?.get) { | ||
Object.defineProperty(process, 'env', { | ||
get() { | ||
return store.env; | ||
}, | ||
}); | ||
} | ||
}; | ||
exports.setProcessEnv = setProcessEnv; | ||
//# sourceMappingURL=index.js.map |
@@ -5,1 +5,2 @@ import { AsyncLocalStorage } from 'node:async_hooks'; | ||
export declare const runSession: <R, TArgs extends unknown[]>(callback: (...args: TArgs) => R, ...args: TArgs) => R; | ||
export declare const setProcessEnv: (env: unknown) => void; |
@@ -12,5 +12,8 @@ import { AsyncLocalStorage } from 'node:async_hooks'; | ||
if (!store) { | ||
throw new Error('Global store is not initialized'); | ||
throw new Error('Session context is not initialized'); | ||
} | ||
return store; | ||
if (!store.appContext) { | ||
store.appContext = {}; | ||
} | ||
return store.appContext; | ||
}; | ||
@@ -21,2 +24,16 @@ export const runSession = (callback, ...args) => { | ||
}; | ||
export const setProcessEnv = (env) => { | ||
const store = global.__context.getStore(); | ||
if (!store) { | ||
throw new Error('Session context is not initialized'); | ||
} | ||
store.env = env; | ||
if (!Object.getOwnPropertyDescriptor(process, 'env')?.get) { | ||
Object.defineProperty(process, 'env', { | ||
get() { | ||
return store.env; | ||
}, | ||
}); | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "session-context", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"main": "./dist/cjs/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/cjs/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17664
21.54%165
28.91%