UNPKG

@temporalio/worker

Version:
292 lines 19.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowCodecRunner = void 0; const internal_non_workflow_1 = require("@temporalio/common/lib/internal-non-workflow"); const proto_1 = require("@temporalio/proto"); /** * Helper class for decoding Workflow activations and encoding Workflow completions. */ class WorkflowCodecRunner { codecs; constructor(codecs) { this.codecs = codecs; } /** * Run codec.decode on the Payloads in the Activation message. */ async decodeActivation(activation) { return proto_1.coresdk.workflow_activation.WorkflowActivation.fromObject({ ...activation, jobs: activation.jobs ? await Promise.all(activation.jobs.map(async (job) => ({ ...job, initializeWorkflow: job.initializeWorkflow ? { ...job.initializeWorkflow, arguments: await (0, internal_non_workflow_1.decodeOptional)(this.codecs, job.initializeWorkflow.arguments), headers: (0, internal_non_workflow_1.noopDecodeMap)(job.initializeWorkflow.headers), continuedFailure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.initializeWorkflow.continuedFailure), memo: { ...job.initializeWorkflow.memo, fields: await (0, internal_non_workflow_1.decodeOptionalMap)(this.codecs, job.initializeWorkflow.memo?.fields), }, lastCompletionResult: { ...job.initializeWorkflow.lastCompletionResult, payloads: await (0, internal_non_workflow_1.decodeOptional)(this.codecs, job.initializeWorkflow.lastCompletionResult?.payloads), }, searchAttributes: job.initializeWorkflow.searchAttributes ? { ...job.initializeWorkflow.searchAttributes, indexedFields: job.initializeWorkflow.searchAttributes.indexedFields ? (0, internal_non_workflow_1.noopDecodeMap)(job.initializeWorkflow.searchAttributes?.indexedFields) : undefined, } : undefined, } : null, queryWorkflow: job.queryWorkflow ? { ...job.queryWorkflow, arguments: await (0, internal_non_workflow_1.decodeOptional)(this.codecs, job.queryWorkflow.arguments), headers: (0, internal_non_workflow_1.noopDecodeMap)(job.queryWorkflow.headers), } : null, doUpdate: job.doUpdate ? { ...job.doUpdate, input: await (0, internal_non_workflow_1.decodeOptional)(this.codecs, job.doUpdate.input), headers: (0, internal_non_workflow_1.noopDecodeMap)(job.doUpdate.headers), } : null, signalWorkflow: job.signalWorkflow ? { ...job.signalWorkflow, input: await (0, internal_non_workflow_1.decodeOptional)(this.codecs, job.signalWorkflow.input), headers: (0, internal_non_workflow_1.noopDecodeMap)(job.signalWorkflow.headers), } : null, resolveActivity: job.resolveActivity ? { ...job.resolveActivity, result: job.resolveActivity.result ? { ...job.resolveActivity.result, completed: job.resolveActivity.result.completed ? { ...job.resolveActivity.result.completed, result: await (0, internal_non_workflow_1.decodeOptionalSingle)(this.codecs, job.resolveActivity.result.completed.result), } : null, failed: job.resolveActivity.result.failed ? { ...job.resolveActivity.result.failed, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveActivity.result.failed.failure), } : null, cancelled: job.resolveActivity.result.cancelled ? { ...job.resolveActivity.result.cancelled, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveActivity.result.cancelled.failure), } : null, } : null, } : null, resolveChildWorkflowExecution: job.resolveChildWorkflowExecution ? { ...job.resolveChildWorkflowExecution, result: job.resolveChildWorkflowExecution.result ? { ...job.resolveChildWorkflowExecution.result, completed: job.resolveChildWorkflowExecution.result.completed ? { ...job.resolveChildWorkflowExecution.result.completed, result: await (0, internal_non_workflow_1.decodeOptionalSingle)(this.codecs, job.resolveChildWorkflowExecution.result.completed.result), } : null, failed: job.resolveChildWorkflowExecution.result.failed ? { ...job.resolveChildWorkflowExecution.result.failed, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveChildWorkflowExecution.result.failed.failure), } : null, cancelled: job.resolveChildWorkflowExecution.result.cancelled ? { ...job.resolveChildWorkflowExecution.result.cancelled, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveChildWorkflowExecution.result.cancelled.failure), } : null, } : null, } : null, resolveChildWorkflowExecutionStart: job.resolveChildWorkflowExecutionStart ? { ...job.resolveChildWorkflowExecutionStart, cancelled: job.resolveChildWorkflowExecutionStart.cancelled ? { ...job.resolveChildWorkflowExecutionStart.cancelled, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveChildWorkflowExecutionStart.cancelled.failure), } : null, } : null, resolveNexusOperation: job.resolveNexusOperation ? { ...job.resolveNexusOperation, result: { completed: job.resolveNexusOperation.result?.completed ? await (0, internal_non_workflow_1.decodeOptionalSingle)(this.codecs, job.resolveNexusOperation.result?.completed) : null, failed: job.resolveNexusOperation.result?.failed ? await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveNexusOperation.result?.failed) : null, cancelled: job.resolveNexusOperation.result?.cancelled ? await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveNexusOperation.result?.cancelled) : null, timedOut: job.resolveNexusOperation.result?.cancelled ? await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveNexusOperation.result?.timedOut) : null, }, } : null, resolveSignalExternalWorkflow: job.resolveSignalExternalWorkflow ? { ...job.resolveSignalExternalWorkflow, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveSignalExternalWorkflow.failure), } : null, resolveRequestCancelExternalWorkflow: job.resolveRequestCancelExternalWorkflow ? { ...job.resolveRequestCancelExternalWorkflow, failure: await (0, internal_non_workflow_1.decodeOptionalFailure)(this.codecs, job.resolveRequestCancelExternalWorkflow.failure), } : null, }))) : null, }); } /** * Run codec.encode on the Payloads inside the Completion message. */ async encodeCompletion(completion) { const encodedCompletion = { ...completion, failed: completion.failed ? { ...completion.failed, failure: await (0, internal_non_workflow_1.encodeOptionalFailure)(this.codecs, completion?.failed?.failure), } : null, successful: completion.successful ? { ...completion.successful, commands: completion.successful.commands ? await Promise.all(completion.successful.commands.map(async (command) => ({ ...command, scheduleActivity: command.scheduleActivity ? { ...command.scheduleActivity, arguments: await (0, internal_non_workflow_1.encodeOptional)(this.codecs, command.scheduleActivity?.arguments), // don't encode headers headers: (0, internal_non_workflow_1.noopEncodeMap)(command.scheduleActivity?.headers), } : undefined, upsertWorkflowSearchAttributes: command.upsertWorkflowSearchAttributes ? { ...command.upsertWorkflowSearchAttributes, searchAttributes: (0, internal_non_workflow_1.noopEncodeMap)(command.upsertWorkflowSearchAttributes.searchAttributes), } : undefined, respondToQuery: command.respondToQuery ? { ...command.respondToQuery, succeeded: { ...command.respondToQuery.succeeded, response: await (0, internal_non_workflow_1.encodeOptionalSingle)(this.codecs, command.respondToQuery.succeeded?.response), }, failed: await (0, internal_non_workflow_1.encodeOptionalFailure)(this.codecs, command.respondToQuery.failed), } : undefined, updateResponse: command.updateResponse ? { ...command.updateResponse, rejected: await (0, internal_non_workflow_1.encodeOptionalFailure)(this.codecs, command.updateResponse.rejected), completed: await (0, internal_non_workflow_1.encodeOptionalSingle)(this.codecs, command.updateResponse.completed), } : undefined, completeWorkflowExecution: command.completeWorkflowExecution ? { ...command.completeWorkflowExecution, result: await (0, internal_non_workflow_1.encodeOptionalSingle)(this.codecs, command.completeWorkflowExecution.result), } : undefined, failWorkflowExecution: command.failWorkflowExecution ? { ...command.failWorkflowExecution, failure: await (0, internal_non_workflow_1.encodeOptionalFailure)(this.codecs, command.failWorkflowExecution.failure), } : undefined, continueAsNewWorkflowExecution: command.continueAsNewWorkflowExecution ? { ...command.continueAsNewWorkflowExecution, arguments: await (0, internal_non_workflow_1.encodeOptional)(this.codecs, command.continueAsNewWorkflowExecution.arguments), memo: await (0, internal_non_workflow_1.encodeMap)(this.codecs, command.continueAsNewWorkflowExecution.memo), // don't encode headers headers: (0, internal_non_workflow_1.noopEncodeMap)(command.continueAsNewWorkflowExecution.headers), // don't encode searchAttributes searchAttributes: (0, internal_non_workflow_1.noopEncodeMap)(command.continueAsNewWorkflowExecution.searchAttributes), } : undefined, startChildWorkflowExecution: command.startChildWorkflowExecution ? { ...command.startChildWorkflowExecution, input: await (0, internal_non_workflow_1.encodeOptional)(this.codecs, command.startChildWorkflowExecution.input), memo: await (0, internal_non_workflow_1.encodeMap)(this.codecs, command.startChildWorkflowExecution.memo), // don't encode headers headers: (0, internal_non_workflow_1.noopEncodeMap)(command.startChildWorkflowExecution.headers), // don't encode searchAttributes searchAttributes: (0, internal_non_workflow_1.noopEncodeMap)(command.startChildWorkflowExecution.searchAttributes), } : undefined, signalExternalWorkflowExecution: command.signalExternalWorkflowExecution ? { ...command.signalExternalWorkflowExecution, args: await (0, internal_non_workflow_1.encodeOptional)(this.codecs, command.signalExternalWorkflowExecution.args), headers: (0, internal_non_workflow_1.noopEncodeMap)(command.signalExternalWorkflowExecution.headers), } : undefined, scheduleLocalActivity: command.scheduleLocalActivity ? { ...command.scheduleLocalActivity, arguments: await (0, internal_non_workflow_1.encodeOptional)(this.codecs, command.scheduleLocalActivity.arguments), // don't encode headers headers: (0, internal_non_workflow_1.noopEncodeMap)(command.scheduleLocalActivity.headers), } : undefined, scheduleNexusOperation: command.scheduleNexusOperation ? { ...command.scheduleNexusOperation, input: await (0, internal_non_workflow_1.encodeOptionalSingle)(this.codecs, command.scheduleNexusOperation.input), } : undefined, modifyWorkflowProperties: command.modifyWorkflowProperties ? { ...command.modifyWorkflowProperties, upsertedMemo: { ...command.modifyWorkflowProperties.upsertedMemo, fields: await (0, internal_non_workflow_1.encodeMap)(this.codecs, command.modifyWorkflowProperties.upsertedMemo?.fields), }, } : undefined, })) ?? []) : null, } : null, }; return proto_1.coresdk.workflow_completion.WorkflowActivationCompletion.encodeDelimited(encodedCompletion).finish(); } } exports.WorkflowCodecRunner = WorkflowCodecRunner; //# sourceMappingURL=workflow-codec-runner.js.map