Skip to content

Commit

Permalink
fix: clone history events to prevent mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goodwin committed Nov 9, 2023
1 parent 1e66f33 commit 1582ad7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/@eventual/core-runtime/src/deep-clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function deepClone<T>(item: T): T {
// TODO: more efficient deep clone
return JSON.parse(JSON.stringify(item));
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class RemoteExecutorProvider<Context = undefined>
executor: WorkflowExecutor<any, any, any>
): Promise<{ storedBytes: number }> {
// provides a shallow copy of the history events.
const historyEvents = executor.history.slice(0);
const historyEvents = executor.historyCloned.slice(0);
historyEvents.push(...newHistoryEvents);
const { bytes } = await this.props.executionHistoryStateStore.updateHistory(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
type UnresolvedEventualDefinition,
} from "./eventual-definition.js";
import { formatExecutionId } from "./execution.js";
import { deepClone } from "../deep-clone.js";

/**
* Put the resolve method on the promise, but don't expose it.
Expand Down Expand Up @@ -206,13 +207,17 @@ export class WorkflowExecutor<Input, Output, Context = undefined> {
) => void;
} = {};

// clone the history so it cannot be modified by the user
public historyCloned: HistoryStateEvent[];

constructor(
private workflow: Workflow<any, Input, Output>,
public history: HistoryStateEvent[],
// TODO: properties used in the workflow should be encoded in the history to keep them constant between runs
private propertyRetriever: PropertyRetriever,
private eventualFactory: EventualFactory = createDefaultEventualFactory()
) {
this.historyCloned = deepClone(history);
this.nextSeq = 0;
this.expected = iterator(history, isCallEvent);
this.events = iterator(history, isCompletionEvent);
Expand Down

0 comments on commit 1582ad7

Please sign in to comment.