refactor(core): Avoid passing around static state like default timezone (no-changelog) (#7221)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-10-27 14:17:52 +02:00
committed by GitHub
parent 62c096710f
commit 35bb42c1b9
31 changed files with 76 additions and 224 deletions

View File

@@ -23,6 +23,7 @@ import { extend, extendOptional } from './Extensions';
import { extendedFunctions } from './Extensions/ExtendedFunctions';
import { extendSyntax } from './Extensions/ExpressionExtension';
import { evaluateExpression, setErrorHandler } from './ExpressionEvaluatorProxy';
import { getGlobalState } from './GlobalState';
const IS_FRONTEND_IN_DEV_MODE =
typeof process === 'object' &&
@@ -32,13 +33,13 @@ const IS_FRONTEND_IN_DEV_MODE =
const IS_FRONTEND = typeof process === 'undefined' || IS_FRONTEND_IN_DEV_MODE;
export const isSyntaxError = (error: unknown): error is SyntaxError =>
const isSyntaxError = (error: unknown): error is SyntaxError =>
error instanceof SyntaxError || (error instanceof Error && error.name === 'SyntaxError');
export const isExpressionError = (error: unknown): error is ExpressionError =>
const isExpressionError = (error: unknown): error is ExpressionError =>
error instanceof ExpressionError || error instanceof ExpressionExtensionError;
export const isTypeError = (error: unknown): error is TypeError =>
const isTypeError = (error: unknown): error is TypeError =>
error instanceof TypeError || (error instanceof Error && error.name === 'TypeError');
// Make sure that error get forwarded
@@ -58,11 +59,7 @@ const fnConstructors = {
};
export class Expression {
workflow: Workflow;
constructor(workflow: Workflow) {
this.workflow = workflow;
}
constructor(private readonly workflow: Workflow) {}
static resolveWithoutWorkflow(expression: string, data: IDataObject = {}) {
return tmpl.tmpl(expression, data);
@@ -84,7 +81,7 @@ export class Expression {
if (value instanceof Date) {
// We don't want to use JSON.stringify for dates since it disregards workflow timezone
result = DateTime.fromJSDate(value, {
zone: this.workflow.settings?.timezone ?? 'default',
zone: this.workflow.settings?.timezone ?? getGlobalState().defaultTimezone,
}).toISO();
} else {
result = JSON.stringify(value);
@@ -114,7 +111,6 @@ export class Expression {
activeNodeName: string,
connectionInputData: INodeExecutionData[],
mode: WorkflowExecuteMode,
timezone: string,
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData?: IExecuteData,
returnObjectAsString = false,
@@ -143,7 +139,6 @@ export class Expression {
connectionInputData,
siblingParameters,
mode,
timezone,
additionalKeys,
executeData,
-1,
@@ -371,7 +366,6 @@ export class Expression {
node: INode,
parameterValue: string | boolean | undefined,
mode: WorkflowExecuteMode,
timezone: string,
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData?: IExecuteData,
defaultValue?: boolean | number | string | unknown[],
@@ -399,7 +393,6 @@ export class Expression {
node.name,
connectionInputData,
mode,
timezone,
additionalKeys,
executeData,
) as boolean | number | string | undefined;
@@ -415,7 +408,6 @@ export class Expression {
node: INode,
parameterValue: NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[],
mode: WorkflowExecuteMode,
timezone: string,
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData?: IExecuteData,
defaultValue: NodeParameterValueType | undefined = undefined,
@@ -445,7 +437,6 @@ export class Expression {
node.name,
connectionInputData,
mode,
timezone,
additionalKeys,
executeData,
false,
@@ -461,7 +452,6 @@ export class Expression {
node.name,
connectionInputData,
mode,
timezone,
additionalKeys,
executeData,
false,
@@ -487,7 +477,6 @@ export class Expression {
activeNodeName: string,
connectionInputData: INodeExecutionData[],
mode: WorkflowExecuteMode,
timezone: string,
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData?: IExecuteData,
returnObjectAsString = false,
@@ -513,7 +502,6 @@ export class Expression {
activeNodeName,
connectionInputData,
mode,
timezone,
additionalKeys,
executeData,
returnObjectAsString,
@@ -531,7 +519,6 @@ export class Expression {
activeNodeName,
connectionInputData,
mode,
timezone,
additionalKeys,
executeData,
returnObjectAsString,
@@ -551,7 +538,6 @@ export class Expression {
activeNodeName,
connectionInputData,
mode,
timezone,
additionalKeys,
executeData,
returnObjectAsString,

View File

@@ -0,0 +1,15 @@
import { deepCopy } from './utils';
export interface GlobalState {
defaultTimezone: string;
}
let globalState: GlobalState = { defaultTimezone: 'America/New_York' };
export function setGlobalState(state: GlobalState) {
globalState = state;
}
export function getGlobalState() {
return deepCopy(globalState);
}

View File

@@ -196,7 +196,6 @@ export abstract class ICredentialsHelper {
requestOptions: IHttpRequestOptions | IRequestOptionsSimplified,
workflow: Workflow,
node: INode,
defaultTimezone: string,
): Promise<IHttpRequestOptions>;
abstract preAuthentication(
@@ -217,7 +216,6 @@ export abstract class ICredentialsHelper {
nodeCredentials: INodeCredentialsDetails,
type: string,
mode: WorkflowExecuteMode,
defaultTimezone: string,
raw?: boolean,
expressionResolveValues?: ICredentialsExpressionResolveValues,
): Promise<ICredentialDataDecryptedObject>;
@@ -1862,7 +1860,6 @@ export interface IWorkflowExecuteAdditionalData {
instanceBaseUrl: string;
setExecutionStatus?: (status: ExecutionStatus) => void;
sendDataToUI?: (type: string, data: IDataObject | IDataObject[]) => void;
timezone: string;
webhookBaseUrl: string;
webhookWaitingBaseUrl: string;
webhookTestBaseUrl: string;

View File

@@ -885,7 +885,6 @@ export function getNodeWebhooks(
node,
webhookDescription.path,
mode,
additionalData.timezone,
{},
);
if (nodeWebhookPath === undefined) {
@@ -909,7 +908,6 @@ export function getNodeWebhooks(
node,
webhookDescription.isFullPath,
'internal',
additionalData.timezone,
{},
undefined,
false,
@@ -918,7 +916,6 @@ export function getNodeWebhooks(
node,
webhookDescription.restartWebhook,
'internal',
additionalData.timezone,
{},
undefined,
false,
@@ -929,7 +926,6 @@ export function getNodeWebhooks(
node,
webhookDescription.httpMethod,
mode,
additionalData.timezone,
{},
undefined,
'GET',
@@ -1037,7 +1033,6 @@ export function getNodeInputs(
node,
nodeTypeData.inputs,
'internal',
'',
{},
) || []) as ConnectionTypes[];
} catch (e) {
@@ -1060,7 +1055,6 @@ export function getNodeOutputs(
node,
nodeTypeData.outputs,
'internal',
'',
{},
) || []) as ConnectionTypes[];
} catch (e) {

View File

@@ -701,7 +701,6 @@ export class RoutingNode {
this.node.name,
this.connectionInputData,
this.mode,
this.additionalData.timezone,
additionalKeys ?? {},
executeData,
returnObjectAsString,

View File

@@ -28,6 +28,7 @@ import { ExpressionError } from './ExpressionError';
import type { Workflow } from './Workflow';
import { augmentArray, augmentObject } from './AugmentObject';
import { deepCopy } from './utils';
import { getGlobalState } from './GlobalState';
export function isResourceLocatorValue(value: unknown): value is INodeParameterResourceLocator {
return Boolean(
@@ -48,57 +49,28 @@ const isScriptingNode = (nodeName: string, workflow: Workflow) => {
};
export class WorkflowDataProxy {
private workflow: Workflow;
private runExecutionData: IRunExecutionData | null;
private defaultReturnRunIndex: number;
private runIndex: number;
private itemIndex: number;
private activeNodeName: string;
private contextNodeName: string;
private connectionInputData: INodeExecutionData[];
private siblingParameters: INodeParameters;
private mode: WorkflowExecuteMode;
private selfData: IDataObject;
private additionalKeys: IWorkflowDataProxyAdditionalKeys;
private executeData: IExecuteData | undefined;
private defaultTimezone: string;
private timezone: string;
// TODO: Clean that up at some point and move all the options into an options object
constructor(
workflow: Workflow,
private workflow: Workflow,
runExecutionData: IRunExecutionData | null,
runIndex: number,
itemIndex: number,
activeNodeName: string,
private runIndex: number,
private itemIndex: number,
private activeNodeName: string,
connectionInputData: INodeExecutionData[],
siblingParameters: INodeParameters,
mode: WorkflowExecuteMode,
defaultTimezone: string,
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData?: IExecuteData,
defaultReturnRunIndex = -1,
selfData = {},
contextNodeName?: string,
private siblingParameters: INodeParameters,
private mode: WorkflowExecuteMode,
private additionalKeys: IWorkflowDataProxyAdditionalKeys,
private executeData?: IExecuteData,
private defaultReturnRunIndex = -1,
private selfData: IDataObject = {},
private contextNodeName: string = activeNodeName,
) {
this.activeNodeName = activeNodeName;
this.contextNodeName = contextNodeName || activeNodeName;
this.workflow = workflow;
this.runExecutionData = isScriptingNode(this.contextNodeName, workflow)
? runExecutionData !== null
? augmentObject(runExecutionData)
@@ -109,16 +81,7 @@ export class WorkflowDataProxy {
? augmentArray(connectionInputData)
: connectionInputData;
this.defaultReturnRunIndex = defaultReturnRunIndex;
this.runIndex = runIndex;
this.itemIndex = itemIndex;
this.siblingParameters = siblingParameters;
this.mode = mode;
this.defaultTimezone = defaultTimezone;
this.timezone = workflow.settings?.timezone ?? defaultTimezone;
this.selfData = selfData;
this.additionalKeys = additionalKeys;
this.executeData = executeData;
this.timezone = workflow.settings?.timezone ?? getGlobalState().defaultTimezone;
Settings.defaultZone = this.timezone;
}
@@ -266,7 +229,6 @@ export class WorkflowDataProxy {
that.activeNodeName,
that.connectionInputData,
that.mode,
that.timezone,
that.additionalKeys,
that.executeData,
false,
@@ -1185,7 +1147,6 @@ export class WorkflowDataProxy {
that.activeNodeName,
that.connectionInputData,
that.mode,
that.timezone,
that.additionalKeys,
that.executeData,
false,
@@ -1204,10 +1165,10 @@ export class WorkflowDataProxy {
this.connectionInputData,
that.siblingParameters,
that.mode,
that.defaultTimezone,
that.additionalKeys,
that.executeData,
defaultReturnRunIndex,
{},
that.contextNodeName,
);
return dataProxy.getDataProxy();

View File

@@ -9,6 +9,7 @@ export * from './Authentication';
export * from './Constants';
export * from './Cron';
export * from './DeferredPromise';
export * from './GlobalState';
export * from './Interfaces';
export * from './MessageEventBus';
export * from './ExecutionStatus';