Skip to content

Commit dc5b704

Browse files
committed
Ensure project is fetched during EngineConstants factory initialization
1 parent d51dd07 commit dc5b704

7 files changed

Lines changed: 59 additions & 37 deletions

File tree

packages/engine/src/lib/handler/block-executor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ const executeAction: ActionHandler<BlockAction> = async ({
189189
token: constants.engineToken,
190190
apiUrl: constants.internalApiUrl,
191191
publicUrl: constants.publicUrl,
192-
tablesDatabaseId: await constants.getTablesDatabaseId(),
193-
tablesDatabaseToken: await constants.getTablesDatabaseToken(),
192+
tablesDatabaseId: constants.tablesDatabaseId,
193+
tablesDatabaseToken: constants.tablesDatabaseToken,
194194
},
195195
propsValue: processedInput,
196196
tags: createTagsManager(hookResponse),

packages/engine/src/lib/handler/context/engine-constants.ts

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,19 @@ export class EngineConstants {
6868
public readonly serverHandlerId: string | null,
6969
public readonly testRunActionLimits: TestRunLimitSettings,
7070
public readonly isTestRun: boolean,
71+
public readonly tablesDatabaseId: number,
72+
public readonly tablesDatabaseToken: EncryptedObject,
7173
public readonly resumePayload?: ResumePayload,
7274
) {}
7375

74-
public async getTablesDatabaseId(): Promise<number> {
75-
const project = await this.getProject();
76-
return project.tablesDatabaseId;
77-
}
78-
79-
public async getTablesDatabaseToken(): Promise<EncryptedObject> {
80-
const project = await this.getProject();
81-
return project.tablesDatabaseToken;
82-
}
83-
84-
public static fromExecuteFlowInput(
76+
public static async fromExecuteFlowInput(
8577
input: ExecuteFlowOperation,
86-
): EngineConstants {
78+
): Promise<EngineConstants> {
79+
const project = await EngineConstants.fetchProject(
80+
input.internalApiUrl,
81+
input.engineToken,
82+
);
83+
8784
return new EngineConstants(
8885
input.executionCorrelationId,
8986
input.flowVersion.flowId,
@@ -107,15 +104,22 @@ export class EngineConstants {
107104
input.serverHandlerId ?? null,
108105
input.flowVersion.testRunActionLimits,
109106
input.runEnvironment === 'TESTING',
107+
project.tablesDatabaseId,
108+
project.tablesDatabaseToken,
110109
input.executionType === ExecutionType.RESUME
111110
? input.resumePayload
112111
: undefined,
113112
);
114113
}
115114

116-
public static fromExecuteStepInput(
115+
public static async fromExecuteStepInput(
117116
input: ExecuteStepOperation,
118-
): EngineConstants {
117+
): Promise<EngineConstants> {
118+
const project = await EngineConstants.fetchProject(
119+
input.internalApiUrl,
120+
input.engineToken,
121+
);
122+
119123
return new EngineConstants(
120124
null,
121125
input.flowVersion.flowId,
@@ -139,12 +143,19 @@ export class EngineConstants {
139143
null,
140144
input.flowVersion.testRunActionLimits,
141145
true,
146+
project.tablesDatabaseId,
147+
project.tablesDatabaseToken,
142148
);
143149
}
144150

145-
public static fromExecutePropertyInput(
151+
public static async fromExecutePropertyInput(
146152
input: ExecutePropsOptions,
147-
): EngineConstants {
153+
): Promise<EngineConstants> {
154+
const project = await EngineConstants.fetchProject(
155+
input.internalApiUrl,
156+
input.engineToken,
157+
);
158+
148159
return new EngineConstants(
149160
null,
150161
input.flowVersion.flowId,
@@ -168,12 +179,19 @@ export class EngineConstants {
168179
null,
169180
input.flowVersion.testRunActionLimits,
170181
true,
182+
project.tablesDatabaseId,
183+
project.tablesDatabaseToken,
171184
);
172185
}
173186

174-
public static fromExecuteTriggerInput(
187+
public static async fromExecuteTriggerInput(
175188
input: ExecuteTriggerOperation<TriggerHookType>,
176-
): EngineConstants {
189+
): Promise<EngineConstants> {
190+
const project = await EngineConstants.fetchProject(
191+
input.internalApiUrl,
192+
input.engineToken,
193+
);
194+
177195
return new EngineConstants(
178196
null,
179197
input.flowVersion.flowId,
@@ -197,24 +215,26 @@ export class EngineConstants {
197215
null,
198216
input.flowVersion.testRunActionLimits,
199217
input.test,
218+
project.tablesDatabaseId,
219+
project.tablesDatabaseToken,
200220
);
201221
}
202222

203-
private async getProject(): Promise<Project> {
204-
if (this.project) {
205-
return this.project;
206-
}
207-
208-
const getWorkerProjectEndpoint = `${this.internalApiUrl}v1/worker/project`;
223+
private static async fetchProject(
224+
internalApiUrl: string,
225+
engineToken: string,
226+
): Promise<Project> {
227+
const getWorkerProjectEndpoint = `${addTrailingSlashIfMissing(
228+
internalApiUrl,
229+
)}v1/worker/project`;
209230

210231
const response = await fetch(getWorkerProjectEndpoint, {
211232
headers: {
212-
Authorization: `Bearer ${this.engineToken}`,
233+
Authorization: `Bearer ${engineToken}`,
213234
},
214235
});
215236

216-
this.project = (await response.json()) as Project;
217-
return this.project;
237+
return (await response.json()) as Project;
218238
}
219239
}
220240

packages/engine/src/lib/helper/block-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export const blockHelper = {
124124
token: params.engineToken,
125125
apiUrl: constants.internalApiUrl,
126126
publicUrl: params.publicUrl,
127-
tablesDatabaseId: await constants.getTablesDatabaseId(),
128-
tablesDatabaseToken: await constants.getTablesDatabaseToken(),
127+
tablesDatabaseId: constants.tablesDatabaseId,
128+
tablesDatabaseToken: constants.tablesDatabaseToken,
129129
},
130130
project: {
131131
id: params.projectId,

packages/engine/src/lib/operations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const executeFlow = async (
4545
context: FlowExecutorContext,
4646
): Promise<EngineResponse<Pick<FlowRunResponse, 'status' | 'error'>>> => {
4747
try {
48-
const constants = EngineConstants.fromExecuteFlowInput(input);
48+
const constants = await EngineConstants.fromExecuteFlowInput(input);
4949

5050
const response = await flowExecutor.triggerFlowExecutor({
5151
trigger: input.flowVersion.trigger,
@@ -92,7 +92,7 @@ async function executeStep(
9292
engineToken: input.engineToken,
9393
stepTestOutputs: input.stepTestOutputs,
9494
}),
95-
constants: EngineConstants.fromExecuteStepInput(input),
95+
constants: await EngineConstants.fromExecuteStepInput(input),
9696
});
9797

9898
const stepResult = output.steps[step.name];
@@ -203,7 +203,7 @@ export async function execute(
203203
stepTestOutputs: input.stepTestOutputs,
204204
}),
205205
searchValue: input.searchValue,
206-
constants: EngineConstants.fromExecutePropertyInput(input),
206+
constants: await EngineConstants.fromExecutePropertyInput(input),
207207
});
208208

209209
return {
@@ -222,7 +222,7 @@ export async function execute(
222222

223223
const output = await triggerHelper.executeTrigger({
224224
params: input,
225-
constants: EngineConstants.fromExecuteTriggerInput(input),
225+
constants: await EngineConstants.fromExecuteTriggerInput(input),
226226
});
227227

228228
return {

packages/engine/src/lib/resolve-variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function resolveVariable(
99
input: ResolveVariableOperation,
1010
): Promise<ResolveVariableResponse> {
1111
try {
12-
const constants = EngineConstants.fromExecuteStepInput({
12+
const constants = await EngineConstants.fromExecuteStepInput({
1313
projectId: input.projectId,
1414
engineToken: input.engineToken,
1515
internalApiUrl: input.internalApiUrl,

packages/engine/test/handler/test-helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const generateMockEngineConstants = (params?: Partial<EngineConstants>):
3030
params?.serverHandlerId ?? null,
3131
params?.testRunActionLimits ?? { isEnabled: false, limits: [] },
3232
params?.isTestRun ?? false,
33+
params?.tablesDatabaseId ?? 1,
34+
params?.tablesDatabaseToken ?? { iv: 'test-iv', data: 'test-data' },
3335
params?.resumePayload,
3436
)
3537
}

packages/engine/test/resolve-variable.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('resolveVariable', () => {
6060

6161
beforeEach(() => {
6262
jest.clearAllMocks();
63-
mockEngineConstants.fromExecuteStepInput = jest.fn().mockReturnValue(mockConstants);
63+
mockEngineConstants.fromExecuteStepInput = jest.fn().mockResolvedValue(mockConstants);
6464
mockTestExecutionContext.stateFromFlowVersion = jest.fn().mockResolvedValue(mockExecutionState);
6565
});
6666

0 commit comments

Comments
 (0)