mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-07 02:47:32 -08:00
e94cda3837
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Waiting to run
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { ExpressionError } from './errors/expression.error';
|
||
|
||
export type EnvProviderState = {
|
||
isProcessAvailable: boolean;
|
||
isEnvAccessBlocked: boolean;
|
||
env: Record<string, string>;
|
||
};
|
||
|
||
/**
|
||
* Captures a snapshot of the environment variables and configuration
|
||
* that can be used to initialize an environment provider.
|
||
*/
|
||
export function createEnvProviderState(): EnvProviderState {
|
||
const isProcessAvailable = typeof process !== 'undefined';
|
||
const isEnvAccessBlocked = isProcessAvailable
|
||
? process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE === 'true'
|
||
: false;
|
||
const env: Record<string, string> =
|
||
!isProcessAvailable || isEnvAccessBlocked ? {} : (process.env as Record<string, string>);
|
||
|
||
return {
|
||
isProcessAvailable,
|
||
isEnvAccessBlocked,
|
||
env,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* Creates a proxy that provides access to the environment variables
|
||
* in the `WorkflowDataProxy`. Use the `createEnvProviderState` to
|
||
* create the default state object that is needed for the proxy,
|
||
* unless you need something specific.
|
||
*
|
||
* @example
|
||
* createEnvProvider(
|
||
* runIndex,
|
||
* itemIndex,
|
||
* createEnvProviderState(),
|
||
* )
|
||
*/
|
||
export function createEnvProvider(
|
||
runIndex: number,
|
||
itemIndex: number,
|
||
providerState: EnvProviderState,
|
||
): Record<string, string> {
|
||
return new Proxy(
|
||
{},
|
||
{
|
||
has() {
|
||
return true;
|
||
},
|
||
|
||
get(_, name) {
|
||
if (name === 'isProxy') return true;
|
||
|
||
if (!providerState.isProcessAvailable) {
|
||
throw new ExpressionError('not accessible via UI, please run node', {
|
||
runIndex,
|
||
itemIndex,
|
||
});
|
||
}
|
||
if (providerState.isEnvAccessBlocked) {
|
||
throw new ExpressionError('access to env vars denied', {
|
||
causeDetailed:
|
||
'If you need access please contact the administrator to remove the environment variable ‘N8N_BLOCK_ENV_ACCESS_IN_NODE‘',
|
||
runIndex,
|
||
itemIndex,
|
||
});
|
||
}
|
||
|
||
return providerState.env[name.toString()];
|
||
},
|
||
},
|
||
);
|
||
}
|