All files / emulator / emulator / storage.ts

41.18% Branches 7/17
75.00% Lines 63/84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x2
x2
x4
x4
x4
x4
x4
 
x2
x2
 
x2
x4
x4
x4
 
x2
x677
x679
x679
x677
x677
 
x2
 
x23
x23
x23
x329
x329
x350
x350
x329
x23
x23
 
 
 
x2
x3
x3
x3
x3
x3
x3
x3
 
x2
 
x162
x162
x162
x162
x162
x162
x162
x162
x162
 
x2
 
x4
x4
 
x5
x5
 
x848
x848
x848
x848
x982
x982
x1557
x5
 
 
 
 
 
 
 
 
 
x4
x2
 
 
 
 
 
 
 
 
 
 
 





































































































I













I











I















I
I
I
I
I
I






I







// Copyright (c) 2025 Anton A Nesterov <an+vski@vski.sh>, VSKI License
//

export interface MemoryWorkflowRun {
  runId: string;
  status: string;
  workflowName: string;
  input: any[];
  output?: any;
  error?: any;
  executionContext: any;
  executionTimeout?: number;
  startedAt?: Date;
  completedAt?: Date;
  createdAt: Date;
  updatedAt: Date;
  deploymentId: string;
}

export interface MemoryWorkflowStep {
  id: string; // runId-stepId
  runId: string;
  stepId: string;
  stepName: string;
  status: string;
  input: any[];
  output?: any;
  error?: any;
  attempt: number;
  startedAt?: Date;
  completedAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export interface MemoryWorkflowEvent {
  eventId: string;
  runId: string;
  eventType: string;
  correlationId?: string;
  payload: any;
  createdAt: Date;
}

export interface MemoryWorkflowHook {
  hookId: string;
  runId: string;
  token: string;
  metadata: any;
  createdAt: Date;
  updatedAt: Date;
}

export interface MemoryQueueMessage {
  messageId: string;
  queueName: string;
  payload: string;
  runId?: string;
  idempotencyKey?: string;
  status: "pending" | "processing" | "completed" | "failed";
  attempt: number;
  maxAttempts: number;
  notBefore: Date;
  createdAt: Date;
  updatedAt: Date;
  processedAt?: Date;
}

export class MemoryStorage {
  runs = new Map<string, MemoryWorkflowRun>();
  steps = new Map<string, MemoryWorkflowStep>();
  events: MemoryWorkflowEvent[] = [];
  hooks = new Map<string, MemoryWorkflowHook>();
  queue: MemoryQueueMessage[] = [];
  streams = new Map<string, any[]>();

  private dbName: string;
  private static instances = new Map<string, MemoryStorage>();

  constructor(dbName: string) {
    this.dbName = dbName;
    this.load();
  }

  static get(dbName: string): MemoryStorage {
    if (!this.instances.has(dbName)) {
      this.instances.set(dbName, new MemoryStorage(dbName));
    }
    return this.instances.get(dbName)!;
  }

  static getAllDbNames(): string[] {
    // Check localStorage for other DBs that might not be in instances yet
    if (typeof localStorage !== "undefined") {
      const names = new Set(Array.from(this.instances.keys()));
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i);
        if (key?.startsWith("rb_emu_v1:")) {
          names.add(key.replace("rb_emu_v1:", ""));
        }
      }
      return Array.from(names);
    }
    return Array.from(this.instances.keys());
  }

  static clear() {
    if (typeof localStorage !== "undefined") {
      for (const dbName of this.getAllDbNames()) {
        localStorage.removeItem(`rb_emu_v1:${dbName}`);
      }
    }
    this.instances.clear();
  }

  persist() {
    if (typeof localStorage === "undefined") return;
    const data = {
      runs: Array.from(this.runs.entries()),
      steps: Array.from(this.steps.entries()),
      events: this.events,
      hooks: Array.from(this.hooks.entries()),
      queue: this.queue,
    };
    localStorage.setItem(`rb_emu_v1:${this.dbName}`, JSON.stringify(data));
  }

  load() {
    if (typeof localStorage === "undefined") return;
    const saved = localStorage.getItem(`rb_emu_v1:${this.dbName}`);
    if (!saved) return;

    try {
      const data = JSON.parse(saved, (key, value) => {
        // Revive dates
        if (
          typeof value === "string" &&
          /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(value)
        ) {
          return new Date(value);
        }
        return value;
      });

      this.runs = new Map(data.runs || []);
      this.steps = new Map(data.steps || []);
      this.events = data.events || [];
      this.hooks = new Map(data.hooks || []);
      this.queue = data.queue || [];
    } catch (e) {
      console.error(`Failed to load storage for ${this.dbName}:`, e);
    }
  }
}

// Global listener for cross-tab sync
if (typeof window !== "undefined") {
  window.addEventListener("storage", (event) => {
    if (event.key?.startsWith("rb_emu_v1:")) {
      const dbName = event.key.replace("rb_emu_v1:", "");
      const storage = MemoryStorage.get(dbName);
      storage.load();
    }
  });
}