@mastra/upstash 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1728,10 +1728,11 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1728
1728
  run_id: runId
1729
1729
  });
1730
1730
  const now = (/* @__PURE__ */ new Date()).toISOString();
1731
- const resultJson = await this.client.eval(`
1731
+ const luaScript = `
1732
1732
  local key = KEYS[1]
1733
1733
  local optsJson = ARGV[1]
1734
1734
  local now = ARGV[2]
1735
+ local expectedStatusJson = ARGV[3]
1735
1736
 
1736
1737
  -- Get existing data
1737
1738
  local existing = redis.call('GET', key)
@@ -1751,6 +1752,21 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1751
1752
  return nil
1752
1753
  end
1753
1754
 
1755
+ -- Compare-and-set guard: bail out unless the persisted status is one the caller expects.
1756
+ -- This runs inside the same script as the write, so the check and the write are atomic.
1757
+ if expectedStatusJson ~= '' then
1758
+ local expected = cjson.decode(expectedStatusJson)
1759
+ local matched = false
1760
+ for _, status in ipairs(expected) do
1761
+ if snapshot.status == status then
1762
+ matched = true
1763
+ end
1764
+ end
1765
+ if not matched then
1766
+ return nil
1767
+ end
1768
+ end
1769
+
1754
1770
  -- Merge the new options with the existing snapshot
1755
1771
  local opts = cjson.decode(optsJson)
1756
1772
  for k, v in pairs(opts) do
@@ -1766,7 +1782,14 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1766
1782
 
1767
1783
  -- Return the full updated data
1768
1784
  return cjson.encode(data)
1769
- `, [key], [JSON.stringify(opts), now]);
1785
+ `;
1786
+ const { expectedStatus, ...state } = opts;
1787
+ const expectedStatusJson = expectedStatus === void 0 ? "" : JSON.stringify(Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus]);
1788
+ const resultJson = await this.client.eval(luaScript, [key], [
1789
+ JSON.stringify(state),
1790
+ now,
1791
+ expectedStatusJson
1792
+ ]);
1770
1793
  if (!resultJson) return;
1771
1794
  let data;
1772
1795
  if (typeof resultJson === "string") data = JSON.parse(resultJson);