@lostgradient/weft 0.24.1 → 0.24.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.
Files changed (97) hide show
  1. package/README.md +1 -1
  2. package/dist/core/engine/bulk-operations-purge.d.ts +1 -1
  3. package/dist/core/engine/bulk-operations-purge.js +3 -2
  4. package/dist/core/engine/bulk-operations-retry.d.ts +16 -1
  5. package/dist/core/engine/bulk-operations-retry.js +19 -6
  6. package/dist/core/engine/bulk-operations.d.ts +3 -2
  7. package/dist/core/engine/bulk-operations.js +2 -2
  8. package/dist/core/engine/callback-creators-bundles.js +1 -1
  9. package/dist/core/engine/callback-creators-schedule.js +2 -2
  10. package/dist/core/engine/child-workflow.d.ts +13 -1
  11. package/dist/core/engine/child-workflow.js +22 -4
  12. package/dist/core/engine/generation-codec.d.ts +22 -0
  13. package/dist/core/engine/generation-codec.js +2 -0
  14. package/dist/core/engine/index.js +1 -1
  15. package/dist/core/engine/lifecycle/recovery-isolation.d.ts +20 -0
  16. package/dist/core/engine/lifecycle/recovery-isolation.js +26 -0
  17. package/dist/core/engine/lifecycle/resume-body.d.ts +44 -0
  18. package/dist/core/engine/lifecycle/resume-body.js +211 -0
  19. package/dist/core/engine/lifecycle/resume.d.ts +46 -3
  20. package/dist/core/engine/lifecycle/resume.js +21 -210
  21. package/dist/core/engine/lifecycle/standalone-claim-acquire.d.ts +68 -2
  22. package/dist/core/engine/lifecycle/standalone-claim-acquire.js +14 -4
  23. package/dist/core/engine/lifecycle/start-commit.d.ts +2 -0
  24. package/dist/core/engine/lifecycle/start-commit.js +16 -8
  25. package/dist/core/engine/lifecycle/start-or-signal-resolution.d.ts +8 -2
  26. package/dist/core/engine/lifecycle/start-or-signal-resolution.js +11 -3
  27. package/dist/core/engine/lifecycle/start-precondition-attribution.d.ts +10 -4
  28. package/dist/core/engine/lifecycle/start-schedule-timing.d.ts +11 -0
  29. package/dist/core/engine/lifecycle/start-schedule-timing.js +21 -0
  30. package/dist/core/engine/lifecycle/start-terminal-conflict-purge.d.ts +90 -11
  31. package/dist/core/engine/lifecycle/start-terminal-conflict-purge.js +24 -5
  32. package/dist/core/engine/lifecycle/start.d.ts +40 -3
  33. package/dist/core/engine/lifecycle/start.js +19 -26
  34. package/dist/core/engine/lifecycle/transition.d.ts +9 -0
  35. package/dist/core/engine/lifecycle/transition.js +20 -25
  36. package/dist/core/engine/lifecycle.d.ts +2 -1
  37. package/dist/core/engine/lifecycle.js +2 -1
  38. package/dist/core/engine/schedule-overlap.js +2 -1
  39. package/dist/core/engine/schedule-run.d.ts +10 -0
  40. package/dist/core/engine/schedule-run.js +1 -1
  41. package/dist/core/engine/schedules.d.ts +11 -2
  42. package/dist/core/engine/termination/complete.js +1 -1
  43. package/dist/core/engine/termination/suspend.js +3 -0
  44. package/dist/core/engine/validation/schedule.d.ts +25 -1
  45. package/dist/core/engine/validation/schedule.js +4 -10
  46. package/dist/core/engine/validation.js +10 -17
  47. package/dist/core/engine/workflow-claim-reclaim-target.js +6 -6
  48. package/dist/core/engine/workflow-claim-registry.d.ts +79 -75
  49. package/dist/core/engine/workflow-claim-registry.js +15 -2
  50. package/dist/core/engine/workflow-generation-fence.d.ts +74 -0
  51. package/dist/core/engine/workflow-generation-fence.js +19 -0
  52. package/dist/core/start-workflow-validation.d.ts +15 -0
  53. package/dist/core/start-workflow-validation.js +11 -1
  54. package/dist/core/workflow-identifiers.d.ts +37 -0
  55. package/dist/core/workflow-identifiers.js +19 -1
  56. package/dist/http.js +2 -2
  57. package/dist/indexeddb.js +1 -1
  58. package/dist/server/fleet-event-feed.js +6 -6
  59. package/dist/server/rest-binding.d.ts +11 -0
  60. package/dist/server/runtime/task-dispatch-envelope.d.ts +32 -5
  61. package/dist/server/runtime/task-dispatch-envelope.js +23 -1
  62. package/dist/server/runtime/task-dispatch.d.ts +3 -1
  63. package/dist/server/runtime/task-dispatch.js +9 -8
  64. package/dist/server/task-ledger-codec.d.ts +19 -0
  65. package/dist/server/task-ledger-codec.js +3 -0
  66. package/dist/server/task-ledger.d.ts +1 -1
  67. package/dist/server/task-ledger.js +1 -0
  68. package/dist/storage/bun-sql.js +132 -99
  69. package/dist/storage/compressed-storage.js +1 -1
  70. package/dist/storage/generation-keys.d.ts +52 -0
  71. package/dist/storage/generation-keys.js +4 -0
  72. package/dist/storage/index.d.ts +76 -65
  73. package/dist/storage/interface.d.ts +24 -232
  74. package/dist/storage/interface.js +1 -1
  75. package/dist/storage/key-prefixes.d.ts +1 -1
  76. package/dist/storage/key-prefixes.js +1 -0
  77. package/dist/storage/lease-keys.d.ts +48 -0
  78. package/dist/storage/lease-keys.js +8 -0
  79. package/dist/storage/lmdb.js +1 -1
  80. package/dist/storage/memory.js +1 -1
  81. package/dist/storage/neon.js +3 -3
  82. package/dist/storage/node-sqlite.js +132 -99
  83. package/dist/storage/postgres.js +3 -3
  84. package/dist/storage/resolve.js +1 -1
  85. package/dist/storage/scoped-storage.js +1 -1
  86. package/dist/storage/signal-keys.d.ts +23 -0
  87. package/dist/storage/signal-keys.js +9 -0
  88. package/dist/storage/testing.js +1 -1
  89. package/dist/storage/turso.js +2 -2
  90. package/dist/storage/workflow-lifecycle-keys.d.ts +128 -0
  91. package/dist/storage/workflow-lifecycle-keys.js +46 -0
  92. package/dist/storage/workflow-record-keys.d.ts +98 -0
  93. package/dist/storage/workflow-record-keys.js +47 -0
  94. package/dist/version.d.ts +1 -1
  95. package/dist/version.js +1 -1
  96. package/dist/web-extension.js +1 -1
  97. package/package.json +1 -1
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Storage key builders for the durable per-run execution record: workflow
3
+ * state, checkpoints, timelines, schedules, queued operations, events, async
4
+ * activity resolution, and workflow updates.
5
+ *
6
+ * These are spread into `KEYS` in `interface.ts` rather than declared there, so
7
+ * the workflow-record keyspace can carry its full rationale without pushing
8
+ * that file's documented line ceiling. Callers still reach them through `KEYS`,
9
+ * which keeps one import contract for storage keys.
10
+ *
11
+ * @module storage/workflow-record-keys
12
+ */
13
+ /**
14
+ * Workflow state, checkpoint, timeline, schedule, queued-operation, and event
15
+ * keys.
16
+ *
17
+ * Spread into `KEYS` ahead of `SIGNAL_KEYS` and `WORKFLOW_LIFECYCLE_KEYS_CORE`
18
+ * to preserve the pre-split `Object.keys(KEYS)` insertion order; not intended
19
+ * to be imported directly by engine code.
20
+ */
21
+ export declare const WORKFLOW_RECORD_KEYS_CORE: {
22
+ readonly workflow: (id: string) => string;
23
+ readonly checkpoint: (id: string) => string;
24
+ readonly checkpointHistory: (id: string, step: number) => string;
25
+ readonly timelinePrefix: (id: string) => string;
26
+ readonly timeline: (id: string, step: number) => string;
27
+ readonly schedule: (id: string) => string;
28
+ readonly scheduleTick: (fireAt: number, id: string) => string;
29
+ readonly scheduleRun: (workflowId: string) => string;
30
+ /**
31
+ * Durable per-workflow manifest for the schedule that launched a run. Unlike
32
+ * `scheduleRun`, this link survives terminal cleanup so schedule history can
33
+ * be queried until the workflow itself is purged.
34
+ */
35
+ readonly scheduleRunLink: (workflowId: string) => string;
36
+ readonly scheduleRunBySchedulePrefix: (scheduleId: string) => string;
37
+ readonly scheduleRunBySchedule: (scheduleId: string, workflowId: string) => string;
38
+ readonly operation: (queue: string, scheduledAt: number, id: string) => string;
39
+ readonly operationInflight: (id: string) => string;
40
+ readonly operationQueued: (id: string) => string;
41
+ readonly operationResolved: (id: string) => string;
42
+ readonly bulkOperationAuditPrefix: () => string;
43
+ readonly bulkOperationAudit: (timestamp: number, requestId: string, confirmationToken: string) => string;
44
+ readonly operationResolvedByTimePrefix: () => string;
45
+ readonly operationResolvedByTime: (resolvedAt: number, id: string) => string;
46
+ readonly asyncActivity: (workflowId: string, token: string) => string;
47
+ readonly asyncActivityResolution: (workflowId: string, token: string) => string;
48
+ readonly activityReconciliationPrefix: (workflowId: string) => string;
49
+ readonly activityReconciliation: (workflowId: string, activityName: string, idempotencyKeyDigest: string) => string;
50
+ readonly eventPrefix: (workflowId: string) => string;
51
+ readonly event: (workflowId: string, sequence: number) => string;
52
+ readonly eventHead: (workflowId: string) => string;
53
+ readonly eventWatermark: (workflowId: string) => string;
54
+ readonly fleetEventPrefix: () => string;
55
+ readonly fleetEvent: (sequence: number) => string;
56
+ readonly fleetEventTail: () => string;
57
+ readonly fleetEventWatermark: () => string;
58
+ readonly fleetEventByWorkflowPrefix: (workflowId: string) => string;
59
+ readonly fleetEventByWorkflow: (workflowId: string, sequence: number) => string;
60
+ };
61
+ /**
62
+ * Workflow attribute/tag index and update keys.
63
+ *
64
+ * Spread into `KEYS` after `WORKFLOW_LIFECYCLE_KEYS_CORE` to preserve the
65
+ * pre-split `Object.keys(KEYS)` insertion order; not intended to be imported
66
+ * directly by engine code.
67
+ */
68
+ export declare const WORKFLOW_RECORD_KEYS_EXTENDED: {
69
+ readonly attribute: (workflowId: string) => string;
70
+ readonly attributeIndex: (attributeName: string, encodedValue: string, workflowId: string) => string;
71
+ readonly tagIndex: (tag: string, workflowId: string) => string;
72
+ readonly updatePrefix: (workflowId: string) => string;
73
+ readonly update: (workflowId: string, updateId: string) => string;
74
+ readonly updateResponse: (updateId: string) => string;
75
+ readonly updateIdempotency: (workflowId: string, key: string) => string;
76
+ /**
77
+ * Maps a start `idempotencyKey` to the workflow id created for it. Written
78
+ * atomically with the workflow record under a `conditionalBatch` gated on this
79
+ * key being absent, so concurrent same-key starts converge on one workflow.
80
+ * Unlike `updateIdempotency`, it is keyed by the idempotency key alone (no
81
+ * workflow id) because the workflow id is the value it resolves to. It is
82
+ * intentionally NOT swept on terminal cleanup: it must outlive the run so a
83
+ * post-completion `startOrSignal` sees a terminal workflow (and conflicts)
84
+ * rather than missing the mapping and creating a fresh run.
85
+ */
86
+ readonly startIdempotency: (key: string) => string;
87
+ /**
88
+ * The convergence signal id that `startOrSignal` derives from an idempotency
89
+ * key so independent same-key callers deliver ONE signal. Deliberately uses the
90
+ * RAW key (not `encodeStorageKeyComponent`): unlike `startIdempotency` this is a
91
+ * signal id, not a storage key, and `validateSignalId` is character-agnostic, so
92
+ * the raw key is always a valid id as long as it fits the byte cap (enforced as
93
+ * ≤117 bytes so `"start-idem:"` + key stays within the 128-byte signal-id
94
+ * ceiling). The two derivations are independent namespaces; both are individually
95
+ * correct, so the raw-vs-encoded difference is harmless.
96
+ */
97
+ readonly startIdempotencySignalId: (key: string) => string;
98
+ };
@@ -0,0 +1,47 @@
1
+ import { encodeStorageKeyComponent, formatSortableStorageTimestamp } from "./key-encoding.js";
2
+ const formatSortableTimestamp = formatSortableStorageTimestamp;
3
+ export const WORKFLOW_RECORD_KEYS_CORE = {
4
+ workflow: (id) => `wf:${encodeStorageKeyComponent(id)}`,
5
+ checkpoint: (id) => `wf:${encodeStorageKeyComponent(id)}:ckpt`,
6
+ checkpointHistory: (id, step) => `wf:${encodeStorageKeyComponent(id)}:ckpt:${String(step).padStart(10, "0")}`,
7
+ timelinePrefix: (id) => `wf:${encodeStorageKeyComponent(id)}:timeline:`,
8
+ timeline: (id, step) => `wf:${encodeStorageKeyComponent(id)}:timeline:${String(step).padStart(10, "0")}`,
9
+ schedule: (id) => `schedule:${encodeStorageKeyComponent(id)}`,
10
+ scheduleTick: (fireAt, id) => `schedule-due:${String(fireAt).padStart(16, "0")}:${encodeStorageKeyComponent(id)}`,
11
+ scheduleRun: (workflowId) => `schedule-run:${encodeStorageKeyComponent(workflowId)}`,
12
+ scheduleRunLink: (workflowId) => `schedule-run-link:${encodeStorageKeyComponent(workflowId)}`,
13
+ scheduleRunBySchedulePrefix: (scheduleId) => `schedule-run-by-schedule:${encodeStorageKeyComponent(scheduleId)}:`,
14
+ scheduleRunBySchedule: (scheduleId, workflowId) => `schedule-run-by-schedule:${encodeStorageKeyComponent(scheduleId)}:${encodeStorageKeyComponent(workflowId)}`,
15
+ operation: (queue, scheduledAt, id) => `op:${queue}:${formatSortableTimestamp(scheduledAt)}:${id}`,
16
+ operationInflight: (id) => `op:inflight:${id}`,
17
+ operationQueued: (id) => `op:queued:${id}`,
18
+ operationResolved: (id) => `op:resolved:${id}`,
19
+ bulkOperationAuditPrefix: () => "audit:bulk:",
20
+ bulkOperationAudit: (timestamp, requestId, confirmationToken) => `audit:bulk:${formatSortableTimestamp(timestamp)}:${encodeStorageKeyComponent(requestId)}:${encodeStorageKeyComponent(confirmationToken)}`,
21
+ operationResolvedByTimePrefix: () => "op:resolved-by-time:",
22
+ operationResolvedByTime: (resolvedAt, id) => `op:resolved-by-time:${formatSortableTimestamp(resolvedAt)}:${encodeStorageKeyComponent(id)}`,
23
+ asyncActivity: (workflowId, token) => `async-act:v1:${encodeStorageKeyComponent(workflowId)}:${encodeStorageKeyComponent(token)}`,
24
+ asyncActivityResolution: (workflowId, token) => `async-act:v1:${encodeStorageKeyComponent(workflowId)}:${encodeStorageKeyComponent(token)}:resolution`,
25
+ activityReconciliationPrefix: (workflowId) => `actrec:v1:${encodeStorageKeyComponent(workflowId)}:`,
26
+ activityReconciliation: (workflowId, activityName, idempotencyKeyDigest) => `actrec:v1:${encodeStorageKeyComponent(workflowId)}:${encodeStorageKeyComponent(activityName)}:${idempotencyKeyDigest}`,
27
+ eventPrefix: (workflowId) => `ev:${encodeStorageKeyComponent(workflowId)}:`,
28
+ event: (workflowId, sequence) => `ev:${encodeStorageKeyComponent(workflowId)}:${String(sequence).padStart(10, "0")}`,
29
+ eventHead: (workflowId) => `ev:${encodeStorageKeyComponent(workflowId)}:head`,
30
+ eventWatermark: (workflowId) => `ev:${encodeStorageKeyComponent(workflowId)}:watermark`,
31
+ fleetEventPrefix: () => "fleet-event:",
32
+ fleetEvent: (sequence) => `fleet-event:${String(sequence).padStart(10, "0")}`,
33
+ fleetEventTail: () => "fleet-event-tail",
34
+ fleetEventWatermark: () => "fleet-event-watermark",
35
+ fleetEventByWorkflowPrefix: (workflowId) => `fleet-event-by-workflow:${encodeStorageKeyComponent(workflowId)}:`,
36
+ fleetEventByWorkflow: (workflowId, sequence) => `fleet-event-by-workflow:${encodeStorageKeyComponent(workflowId)}:${String(sequence).padStart(10, "0")}`
37
+ }, WORKFLOW_RECORD_KEYS_EXTENDED = {
38
+ attribute: (workflowId) => `attr:${encodeStorageKeyComponent(workflowId)}`,
39
+ attributeIndex: (attributeName, encodedValue, workflowId) => `idx:${attributeName}:${encodedValue}:${encodeStorageKeyComponent(workflowId)}`,
40
+ tagIndex: (tag, workflowId) => `tag:${encodeStorageKeyComponent(tag)}:${encodeStorageKeyComponent(workflowId)}`,
41
+ updatePrefix: (workflowId) => `upd:${encodeStorageKeyComponent(workflowId)}:`,
42
+ update: (workflowId, updateId) => `upd:${encodeStorageKeyComponent(workflowId)}:${updateId}`,
43
+ updateResponse: (updateId) => `upr:${updateId}`,
44
+ updateIdempotency: (workflowId, key) => `upk:${encodeStorageKeyComponent(workflowId)}:${key}`,
45
+ startIdempotency: (key) => `start-idem:${encodeStorageKeyComponent(key)}`,
46
+ startIdempotencySignalId: (key) => `start-idem:${key}`
47
+ };
package/dist/version.d.ts CHANGED
@@ -9,4 +9,4 @@
9
9
  * console.log(`Running Weft ${VERSION}`);
10
10
  * ```
11
11
  */
12
- export declare const VERSION = "0.24.1";
12
+ export declare const VERSION = "0.24.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.24.1";
1
+ export const VERSION = "0.24.2";
@@ -1 +1 @@
1
- function T(e){let t=[];for(let r=0;r<e.length;r+=512)t.push(String.fromCharCode(...e.subarray(r,r+512)));return btoa(t.join(""))}function D(e){let t=atob(e),r=new Uint8Array(t.length);for(let i=0;i<t.length;i+=1)r[i]=t.charCodeAt(i);return r}function u(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}async function b(e,t){return await e.get(t)!==null}async function*c(e,t,r){for await(let[i]of e.scan(t,r))yield i}async function S(e,t){let r=0;for await(let i of c(e,t))r++;return r}async function w(e,t){let r=[];for await(let i of c(e,t))r.push({type:"delete",key:i});if(r.length===0)return 0;return await e.batch(r),r.length}async function v(e,t,r){let i=[];for await(let s of c(e,t,r))i.push({type:"delete",key:s});if(i.length===0)return 0;return await e.batch(i),i.length}function _(e,t,r){if(!e.capabilities()[t])throw Error(`Feature "${r}" requires storage capability "${t}", but this storage backend does not provide it.`)}function n(e){return encodeURIComponent(e)}function l(e){return String(e).padStart(16,"0")}var W={catalogEntry:(e,t)=>`catalog-entry:${n(e)}:${n(t)}`,catalogEntryPrefix:(e)=>`catalog-entry:${n(e)}:`,catalogActive:(e)=>`catalog-active:${n(e)}`,catalogTombstone:(e,t)=>`catalog-tombstone:${n(e)}:${n(t)}`,catalogTombstonePrefix:()=>"catalog-tombstone:"};var I="default";var K={applicationMailbox:(e,t)=>`appmbx:v1:${n(e)}:${n(t)}`,applicationMailboxSinkProbe:(e,t,r)=>`appprobe:v1:${n(e)}:${n(t)}:${n(r)}`,applicationCommandPrefix:(e,t)=>`appcmd:v1:${n(e)}:${n(t)}:`,applicationCommand:(e,t,r)=>`appcmd:v1:${n(e)}:${n(t)}:${n(r)}`,applicationCommandReadyPrefix:(e,t)=>`appready:v1:${n(e)}:${n(t)}:`,applicationCommandReady:(e,t,r)=>`appready:v1:${n(e)}:${n(t)}:${l(r)}`,applicationCommandIdempotency:(e,t,r)=>`appidem:v1:${n(e)}:${n(t)}:${n(r)}`,applicationCommandBySequencePrefix:(e,t)=>`appseq:v1:${n(e)}:${n(t)}:`,applicationCommandBySequence:(e,t,r)=>`appseq:v1:${n(e)}:${n(t)}:${l(r)}`,applicationCommandTerminalPrefix:(e,t)=>`appterm:v1:${n(e)}:${n(t)}:`,applicationCommandTerminal:(e,t,r,i)=>`appterm:v1:${n(e)}:${n(t)}:${l(r)}:${n(i)}`};var L={applicationOutbox:(e,t)=>`appobx:v1:${n(e)}:${n(t)}`,applicationOutboxSinkProbe:(e,t,r)=>`appprobe:v1:outbox:${n(e)}:${n(t)}:${n(r)}`,applicationDeliveryPrefix:(e,t)=>`appdlv:v1:${n(e)}:${n(t)}:`,applicationDelivery:(e,t,r)=>`appdlv:v1:${n(e)}:${n(t)}:${n(r)}`,applicationDeliveryDuePrefix:(e,t)=>`appdue:v1:${n(e)}:${n(t)}:`,applicationDeliveryDue:(e,t,r,i)=>`appdue:v1:${n(e)}:${n(t)}:${l(r)}:${n(i)}`,applicationDeliveryIdempotency:(e,t,r)=>`appdidem:v1:${n(e)}:${n(t)}:${n(r)}`,applicationDeliveryBySequencePrefix:(e,t)=>`appdseq:v1:${n(e)}:${n(t)}:`,applicationDeliveryBySequence:(e,t,r)=>`appdseq:v1:${n(e)}:${n(t)}:${l(r)}`,applicationDeliveryTerminalPrefix:(e,t)=>`appdterm:v1:${n(e)}:${n(t)}:`,applicationDeliveryTerminal:(e,t,r,i)=>`appdterm:v1:${n(e)}:${n(t)}:${l(r)}:${n(i)}`};var U={workflowOwnerEpoch:(e)=>`wf-owner-epoch:${n(e)}`,workflowOwnerHolder:(e)=>`wf-owner-holder:${n(e)}`,ownershipModeMarker:()=>"ownership-mode-marker"};var E=1e4;class z extends Error{code="StorageBatchOperationLimitExceededError";cap=E;count;target;constructor(e,t){super(`${e} count ${t} exceeds MAX_BATCH_OPERATIONS (${E}).`);this.name="StorageBatchOperationLimitExceededError",this.target=e,this.count=t}}function d(e,t){if(t>E)throw new z(e,t)}function M(e){return e.length>0?e.slice(0,-1)+String.fromCharCode(e.charCodeAt(e.length-1)+1):"ÿ"}function q(e,t={}){if(t.gt!==void 0&&e<=t.gt)return!1;if(t.gte!==void 0&&e<t.gte)return!1;if(t.lt!==void 0&&e>=t.lt)return!1;if(t.lte!==void 0&&e>t.lte)return!1;return!0}function Ue(e,t){if(e===null||t===null)return e===t;if(e.byteLength!==t.byteLength)return!1;for(let r=0;r<e.byteLength;r++)if(e[r]!==t[r])return!1;return!0}async function Y(e,t){if(e.has)return e.has(t);return b(e,t)}function H(e,t,r){if(e.keys)return e.keys(t,r);return c(e,t,r)}async function V(e,t){if(e.count)return e.count(t);return S(e,t)}async function j(e,t){if(e.deletePrefix)return e.deletePrefix(t);return w(e,t)}async function G(e,t,r){if(d("conditionalBatch conditions",t.length),d("conditionalBatch operations",r.length),_(e,"conditionalBatch","storageConditionalBatch"),!e.conditionalBatch)throw Error("This storage backend reports conditionalBatch capability but does not implement the conditionalBatch() method.");return e.conditionalBatch(t,r)}var a=l,te="0",ne="1",N=(e,t,r,i)=>`sig:${n(e)}:${n(t)}:${i}:${n(r)}`,Ne={workflow:(e)=>`wf:${n(e)}`,checkpoint:(e)=>`wf:${n(e)}:ckpt`,checkpointHistory:(e,t)=>`wf:${n(e)}:ckpt:${String(t).padStart(10,"0")}`,timelinePrefix:(e)=>`wf:${n(e)}:timeline:`,timeline:(e,t)=>`wf:${n(e)}:timeline:${String(t).padStart(10,"0")}`,schedule:(e)=>`schedule:${n(e)}`,scheduleTick:(e,t)=>`schedule-due:${String(e).padStart(16,"0")}:${n(t)}`,scheduleRun:(e)=>`schedule-run:${n(e)}`,scheduleRunLink:(e)=>`schedule-run-link:${n(e)}`,scheduleRunBySchedulePrefix:(e)=>`schedule-run-by-schedule:${n(e)}:`,scheduleRunBySchedule:(e,t)=>`schedule-run-by-schedule:${n(e)}:${n(t)}`,operation:(e,t,r)=>`op:${e}:${a(t)}:${r}`,operationInflight:(e)=>`op:inflight:${e}`,operationQueued:(e)=>`op:queued:${e}`,operationResolved:(e)=>`op:resolved:${e}`,bulkOperationAuditPrefix:()=>"audit:bulk:",bulkOperationAudit:(e,t,r)=>`audit:bulk:${a(e)}:${n(t)}:${n(r)}`,operationResolvedByTimePrefix:()=>"op:resolved-by-time:",operationResolvedByTime:(e,t)=>`op:resolved-by-time:${a(e)}:${n(t)}`,asyncActivity:(e,t)=>`async-act:v1:${n(e)}:${n(t)}`,asyncActivityResolution:(e,t)=>`async-act:v1:${n(e)}:${n(t)}:resolution`,activityReconciliationPrefix:(e)=>`actrec:v1:${n(e)}:`,activityReconciliation:(e,t,r)=>`actrec:v1:${n(e)}:${n(t)}:${r}`,eventPrefix:(e)=>`ev:${n(e)}:`,event:(e,t)=>`ev:${n(e)}:${String(t).padStart(10,"0")}`,eventHead:(e)=>`ev:${n(e)}:head`,eventWatermark:(e)=>`ev:${n(e)}:watermark`,fleetEventPrefix:()=>"fleet-event:",fleetEvent:(e)=>`fleet-event:${String(e).padStart(10,"0")}`,fleetEventTail:()=>"fleet-event-tail",fleetEventWatermark:()=>"fleet-event-watermark",fleetEventByWorkflowPrefix:(e)=>`fleet-event-by-workflow:${n(e)}:`,fleetEventByWorkflow:(e,t)=>`fleet-event-by-workflow:${n(e)}:${String(t).padStart(10,"0")}`,signal:(e,t,r)=>N(e,t,r,ne),startSignal:(e,t,r)=>N(e,t,r,te),signalSequence:(e)=>`sigseq:v1:${n(e)}`,signalAcceptedResponsePrefix:(e)=>`sigres:v1:${n(e)}:`,signalAcceptedResponse:(e,t,r)=>`sigres:v1:${n(e)}:${n(t)}:${n(r)}`,deadline:(e,t)=>`wf-deadline:${a(e)}:${n(t)}`,terminalCleanup:(e,t)=>`wf-cleanup:${a(e)}:${n(t)}`,teardownTimer:(e,t)=>`wf-teardown:${a(e)}:${n(t)}`,delayedStart:(e,t)=>`wf-delayed:${a(e)}:${n(t)}`,terminalWorkflowPrefix:()=>"wf-terminal:",terminalWorkflow:(e,t)=>`wf-terminal:${a(e)}:${n(t)}`,attribute:(e)=>`attr:${n(e)}`,attributeIndex:(e,t,r)=>`idx:${e}:${t}:${n(r)}`,tagIndex:(e,t)=>`tag:${n(e)}:${n(t)}`,updatePrefix:(e)=>`upd:${n(e)}:`,update:(e,t)=>`upd:${n(e)}:${t}`,updateResponse:(e)=>`upr:${e}`,updateIdempotency:(e,t)=>`upk:${n(e)}:${t}`,startIdempotency:(e)=>`start-idem:${n(e)}`,startIdempotencySignalId:(e)=>`start-idem:${e}`,livenessPrefix:()=>"liveness:",liveness:(e)=>`liveness:${n(e)}`,leasePrefix:()=>"lease:",leaseEpoch:()=>"lease:epoch",leaseHolder:()=>"lease:holder",...K,...L,...U,...W,budget:(e,t,r)=>`budget:${e}:${t}:${r}`,review:(e,t)=>`review:${n(e)}:${t}`,workflowHeaders:(e)=>`wf-headers:${n(e)}`,childCancellationPrefix:(e)=>`child-cancel:${n(e)}:`,childCancellation:(e,t)=>`child-cancel:${n(e)}:${n(t)}`,childWorkflowByParentPrefix:(e,t)=>`child-by-parent:${n(e)}:${t===void 0?"":`${n(t)}:`}`,childWorkflowByParent:(e,t,r)=>`child-by-parent:${n(e)}:${t===void 0?"":`${n(t)}:`}${n(r)}`,terminalCleanupNeeded:(e)=>`wf-cleanup-needed:${n(e)}`,workflowConcurrency:(e,t)=>`wf-concurrency:${n(e)}:${n(t)}`,workflowConcurrencyHolder:(e)=>`wf-concurrency-holder:${n(e)}`,workflowHasServices:(e)=>`wf-has-services:${n(e)}`,finalizerState:(e)=>`wf-finalizer-state:${n(e)}`,teardownOwed:(e)=>`wf-teardown-needed:${n(e)}`,teardownSucceeded:(e)=>`wf-teardown-succeeded:${n(e)}`,teardownDeadLetter:(e)=>`wf-teardown-deadletter:${n(e)}`,offload:(e,t)=>`offload:${n(e)}:${t}`,archive:(e,t)=>`archive:${n(e)}:${t}`,stateExecution:(e,t)=>`state:execution:${n(e)}:${n(t)}`,stateWorkflow:(e,t)=>`state:workflow-scope:${I}:${n(e)}:${n(t)}`,streamChunkPrefix:(e,t)=>`blob:${n(e)}:${t}:chunk:`,streamChunk:(e,t,r)=>`blob:${n(e)}:${t}:chunk:${String(r).padStart(10,"0")}`,streamTail:(e,t)=>`blob:${n(e)}:${t}:tail`,streamMetadata:(e,t)=>`blob:${n(e)}:${t}:meta`,budgetCharged:(e)=>`budget-charged:${e}`,toolEffect:(e,t,r)=>`tool-effect:${n(e)}:${t}:${r}`,workflowVisibilityStatus:(e,t)=>`wf-idx-status:${n(e)}:${n(t)}`,workflowVisibilityType:(e,t)=>`wf-idx-type:${n(e)}:${n(t)}`,workflowVisibilityCreated:(e,t)=>`wf-idx-created:${a(e)}:${n(t)}`,workflowVisibilityUpdated:(e,t)=>`wf-idx-updated:${a(e)}:${n(t)}`,workflowVisibilityDeadline:(e,t)=>`wf-idx-deadline:${a(e)}:${n(t)}`,workflowVisibilityManifest:(e)=>`wf-idx-manifest:${n(e)}`,workflowVisibilityMetaVersion:()=>"wf-idx-meta:version",workflowVisibilityMetaBuiltAt:()=>"wf-idx-meta:built-at",workflowVisibilityMetaCursor:()=>"wf-idx-meta:cursor"};function re(e){if(e===void 0)return;if(typeof e!=="number"||!Number.isInteger(e)||e<0)throw Error("deleteRange limit must be a finite non-negative integer");return e===0?0:e}function f(e){let t={},r=!1;for(let s of["gt","gte","lt","lte"]){let o=e[s];if(o===void 0)continue;if(typeof o!=="string")throw Error("deleteRange bounds must be strings");t[s]=o,r=!0}if(!r)throw Error("deleteRange requires at least one of gt/gte/lt/lte; use deletePrefix to delete a whole prefix");let i=re(e.limit);if(i!==void 0)t.limit=i;return t}async function F(e,t,r){let i=f(r);if(e.deleteRange)return e.deleteRange(t,i);return v(e,t,i)}function ie(e,t){let r={key:e,open:!1};if(t.gte!==void 0&&t.gte>r.key)r.key=t.gte,r.open=!1;if(t.gt!==void 0&&t.gt>=r.key)r.key=t.gt,r.open=!0;return r}function oe(e,t){let r={key:M(e),open:!0};if(t.lt!==void 0&&t.lt<=r.key)r.key=t.lt,r.open=!0;if(t.lte!==void 0&&t.lte<r.key)r.key=t.lte,r.open=!1;return r}function Ye(e,t){let r=ie(e,t),i=oe(e,t);if(r.key>i.key||r.key===i.key&&(r.open||i.open))return null;return{lower:r,upper:i}}function k(e){return e.replaceAll(/:+$/g,"")}function se(e,t){let r=k(e),i=k(t);if(r.length===0)return i;if(i.length===0)return r;return`${r}:${i}`}class P{#t;#n;constructor(e,t){this.#t=e,this.#n=k(t)}#e(e){if(this.#n.length===0)return e;return e.length===0?`${this.#n}:`:`${this.#n}:${e}`}#r(e){return e.map((t)=>{if(t.type==="put")return{type:"put",key:this.#e(t.key),value:t.value};return{type:"delete",key:this.#e(t.key)}})}#o(e){if(this.#n.length===0)return e;return e.slice(this.#n.length+1)}#i(e){let t={};if(e.limit!==void 0)t.limit=e.limit;if(e.gt!==void 0)t.gt=this.#e(e.gt);if(e.gte!==void 0)t.gte=this.#e(e.gte);if(e.lt!==void 0)t.lt=this.#e(e.lt);if(e.lte!==void 0)t.lte=this.#e(e.lte);return t}#s(e={}){let t=this.#i(e);if(e.reverse!==void 0)t.reverse=e.reverse;return t}#g(e){return this.#i(e)}capabilities(){return this.#t.capabilities()}scoped(e){return new P(this.#t,se(this.#n,e))}async get(e){return this.#t.get(this.#e(e))}async put(e,t){await this.#t.put(this.#e(e),t)}async delete(e){await this.#t.delete(this.#e(e))}async*scan(e,t){for await(let[r,i]of this.#t.scan(this.#e(e),this.#s(t)))yield[this.#o(r),i]}async batch(e){d("batch operations",e.length),await this.#t.batch(this.#r(e))}async conditionalBatch(e,t){return G(this.#t,e.map((r)=>({key:this.#e(r.key),expectedValue:r.expectedValue})),this.#r(t))}async has(e){return Y(this.#t,this.#e(e))}async deletePrefix(e){return j(this.#t,this.#e(e))}async deleteRange(e,t){let r=this.#g(f(t));return F(this.#t,this.#e(e),r)}async*keys(e,t){for await(let r of H(this.#t,this.#e(e),this.#s(t)))yield this.#o(r)}async count(e){return V(this.#t,this.#e(e))}[Symbol.dispose](){this.#t[Symbol.dispose]()}}function Q(e,t){return new P(e,t)}var J=1,y="__weftStorageKeyspace",ae=102400,le=8192;function ge(e){return typeof e==="object"&&e!==null&&"then"in e&&typeof e.then==="function"}function C(e){if(!u(e))return!1;if(e.__weftStorage!==J)return!1;return typeof e.value==="string"}function ce(e){if(!u(e))return!1;return Object.values(e).every(C)}function x(e){if(e!==y)return;throw Error(`WebExtensionStorage key "${y}" is reserved for adapter metadata.`)}function ue(e){for(let t of e)x(t.key)}function X(e){return{__weftStorage:J,value:T(e)}}function Z(e){if(!C(e))return null;return D(e.value)}function de(e){return C(e)}function O(e){return new TextEncoder().encode(JSON.stringify(e)).byteLength}function pe(e){if(!u(e))return!1;if(e.runtime!==void 0&&!u(e.runtime))return!1;return e.storage===void 0||u(e.storage)}function fe(e){if(e!==void 0){if(!pe(e))throw Error("WebExtensionStorage injected namespace must be an object.");return e}let t=globalThis,r=t.browser??t.chrome;if(r?.storage===void 0)throw Error("WebExtensionStorage requires globalThis.browser.storage or globalThis.chrome.storage.");return r}function me(e,t){let r=e.storage?.[t];if(r===void 0)throw Error(`WebExtensionStorage area "${t}" is not available.`);return r}function ye(e){if(e==="session")return"ephemeral";if(e==="sync"||e==="managed")return"remote";return"local"}function he(e){let t=e.runtime?.lastError?.message;return t===void 0?null:Error(t)}async function R(e,t,r){return new Promise((i,s)=>{let o=!1,p=(g)=>{if(o)return;let A=he(e);if(o=!0,A!==null)s(A);else i(g)},B=(g)=>{if(o)return;o=!0,s(g)},ee=(g)=>p(g),h;try{h=t(ee)}catch{try{h=r()}catch(g){B(g);return}}if(ge(h))h.then(p,B)})}class m{static#t=new WeakMap;#n;#e;#r;#o;#i;constructor(e={},t){this.#r=e.area??"local",this.#o=ye(this.#r),this.#n=fe(t),this.#e=me(this.#n,this.#r),this.#i=()=>{},this.#n.storage?.onChanged?.addListener?.(this.#i)}capabilities(){return{persistence:this.#o,readAfterWrite:"session",scanConsistency:"best-effort",atomicBatch:!1,conditionalBatch:!1,boundedRangeDelete:!1}}async#s(e){return R(this.#n,(t)=>this.#e.get(e,t),()=>this.#e.get(e))}async#g(e){await R(this.#n,(t)=>this.#e.set(e,t),()=>this.#e.set(e))}async#d(e){if(this.#e.getBytesInUse===void 0)return null;return R(this.#n,(t)=>this.#e.getBytesInUse?.(e,t),()=>this.#e.getBytesInUse?.(e))}async#a(){let t=(await this.#s(y))[y];if(t===void 0)return{};if(!ce(t))throw Error("WebExtensionStorage keyspace is not a valid storage envelope map.");return{...t}}async#c(e){let t={[y]:e};await this.#m(t),await this.#g(t)}async#u(e){let t=m.#t.get(this.#e)??Promise.resolve(),r={},i=new Promise((o)=>{r.release=o}),s=t.catch(()=>{return}).then(()=>i);m.#t.set(this.#e,s),await t.catch(()=>{return});try{return await e()}finally{if(r.release?.(),m.#t.get(this.#e)===s)m.#t.delete(this.#e)}}#l(){if(this.#r==="managed")throw Error('WebExtensionStorage area "managed" is read-only.')}#p(e){let t=this.#e.QUOTA_BYTES_PER_ITEM??le;for(let[r,i]of Object.entries(e)){let s=O({[r]:i});if(s>t)throw Error(`WebExtensionStorage sync item quota exceeded for "${r}": ${String(s)} > ${String(t)}.`)}}async#f(e){let t=Object.keys(e),r=await this.#d(null),i=await this.#d(t);if(r!==null&&i!==null)return r-i+O(e);let s=await this.#s(null);return O({...s,...e})}async#m(e){if(this.#r!=="sync")return;this.#p(e);let t=this.#e.QUOTA_BYTES??ae,r=await this.#f(e);if(r>t)throw Error(`WebExtensionStorage sync total quota exceeded: ${String(r)} > ${String(t)}.`)}async get(e){x(e);let t=await this.#a();return Z(t[e])}async put(e,t){this.#l(),x(e),await this.#u(async()=>{let r=await this.#a();r[e]=X(t),await this.#c(r)})}async delete(e){this.#l(),x(e),await this.#u(async()=>{let t=await this.#a();if(!(e in t))return;delete t[e],await this.#c(t)})}async*scan(e,t={}){let r=await this.#a(),i=Object.keys(r).filter((o)=>o.startsWith(e)).filter((o)=>q(o,t)).filter((o)=>de(r[o])).toSorted();if(t.reverse)i=i.toReversed();let s=0;for(let o of i){if(t.limit!==void 0&&s>=t.limit)break;let p=Z(r[o]);if(p===null)continue;yield[o,p],s+=1}}async batch(e){if(d("batch operations",e.length),this.#l(),e.length===0)return;ue(e),await this.#u(async()=>{let t=await this.#a();for(let r of e)if(r.type==="put")t[r.key]=X(r.value);else delete t[r.key];await this.#c(t)})}has(e){return b(this,e)}async*keys(e,t){yield*c(this,e,t)}count(e){return S(this,e)}async deletePrefix(e){return this.#l(),w(this,e)}async deleteRange(e,t){let r=f(t);return this.#l(),v(this,e,r)}scoped(e){return Q(this,e)}[Symbol.dispose](){this.#n.storage?.onChanged?.removeListener?.(this.#i)}}export{m as WebExtensionStorage};
1
+ function T(e){let t=[];for(let r=0;r<e.length;r+=512)t.push(String.fromCharCode(...e.subarray(r,r+512)));return btoa(t.join(""))}function D(e){let t=atob(e),r=new Uint8Array(t.length);for(let i=0;i<t.length;i+=1)r[i]=t.charCodeAt(i);return r}function u(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}async function b(e,t){return await e.get(t)!==null}async function*c(e,t,r){for await(let[i]of e.scan(t,r))yield i}async function S(e,t){let r=0;for await(let i of c(e,t))r++;return r}async function E(e,t){let r=[];for await(let i of c(e,t))r.push({type:"delete",key:i});if(r.length===0)return 0;return await e.batch(r),r.length}async function w(e,t,r){let i=[];for await(let s of c(e,t,r))i.push({type:"delete",key:s});if(i.length===0)return 0;return await e.batch(i),i.length}function W(e,t,r){if(!e.capabilities()[t])throw Error(`Feature "${r}" requires storage capability "${t}", but this storage backend does not provide it.`)}function n(e){return encodeURIComponent(e)}function a(e){return String(e).padStart(16,"0")}var K={catalogEntry:(e,t)=>`catalog-entry:${n(e)}:${n(t)}`,catalogEntryPrefix:(e)=>`catalog-entry:${n(e)}:`,catalogActive:(e)=>`catalog-active:${n(e)}`,catalogTombstone:(e,t)=>`catalog-tombstone:${n(e)}:${n(t)}`,catalogTombstonePrefix:()=>"catalog-tombstone:"};var L={workflowGeneration:(e)=>`wf-gen:${n(e)}`};var I={livenessPrefix:()=>"liveness:",liveness:(e)=>`liveness:${n(e)}`,leasePrefix:()=>"lease:",leaseEpoch:()=>"lease:epoch",leaseHolder:()=>"lease:holder"};var N={applicationMailbox:(e,t)=>`appmbx:v1:${n(e)}:${n(t)}`,applicationMailboxSinkProbe:(e,t,r)=>`appprobe:v1:${n(e)}:${n(t)}:${n(r)}`,applicationCommandPrefix:(e,t)=>`appcmd:v1:${n(e)}:${n(t)}:`,applicationCommand:(e,t,r)=>`appcmd:v1:${n(e)}:${n(t)}:${n(r)}`,applicationCommandReadyPrefix:(e,t)=>`appready:v1:${n(e)}:${n(t)}:`,applicationCommandReady:(e,t,r)=>`appready:v1:${n(e)}:${n(t)}:${a(r)}`,applicationCommandIdempotency:(e,t,r)=>`appidem:v1:${n(e)}:${n(t)}:${n(r)}`,applicationCommandBySequencePrefix:(e,t)=>`appseq:v1:${n(e)}:${n(t)}:`,applicationCommandBySequence:(e,t,r)=>`appseq:v1:${n(e)}:${n(t)}:${a(r)}`,applicationCommandTerminalPrefix:(e,t)=>`appterm:v1:${n(e)}:${n(t)}:`,applicationCommandTerminal:(e,t,r,i)=>`appterm:v1:${n(e)}:${n(t)}:${a(r)}:${n(i)}`};var Y={applicationOutbox:(e,t)=>`appobx:v1:${n(e)}:${n(t)}`,applicationOutboxSinkProbe:(e,t,r)=>`appprobe:v1:outbox:${n(e)}:${n(t)}:${n(r)}`,applicationDeliveryPrefix:(e,t)=>`appdlv:v1:${n(e)}:${n(t)}:`,applicationDelivery:(e,t,r)=>`appdlv:v1:${n(e)}:${n(t)}:${n(r)}`,applicationDeliveryDuePrefix:(e,t)=>`appdue:v1:${n(e)}:${n(t)}:`,applicationDeliveryDue:(e,t,r,i)=>`appdue:v1:${n(e)}:${n(t)}:${a(r)}:${n(i)}`,applicationDeliveryIdempotency:(e,t,r)=>`appdidem:v1:${n(e)}:${n(t)}:${n(r)}`,applicationDeliveryBySequencePrefix:(e,t)=>`appdseq:v1:${n(e)}:${n(t)}:`,applicationDeliveryBySequence:(e,t,r)=>`appdseq:v1:${n(e)}:${n(t)}:${a(r)}`,applicationDeliveryTerminalPrefix:(e,t)=>`appdterm:v1:${n(e)}:${n(t)}:`,applicationDeliveryTerminal:(e,t,r,i)=>`appdterm:v1:${n(e)}:${n(t)}:${a(r)}:${n(i)}`};var U={workflowOwnerEpoch:(e)=>`wf-owner-epoch:${n(e)}`,workflowOwnerHolder:(e)=>`wf-owner-holder:${n(e)}`,ownershipModeMarker:()=>"ownership-mode-marker"};var ge="0",ce="1",F=(e,t,r,i)=>`sig:${n(e)}:${n(t)}:${i}:${n(r)}`,M={signal:(e,t,r)=>F(e,t,r,ce),startSignal:(e,t,r)=>F(e,t,r,ge),signalSequence:(e)=>`sigseq:v1:${n(e)}`,signalAcceptedResponsePrefix:(e)=>`sigres:v1:${n(e)}:`,signalAcceptedResponse:(e,t,r)=>`sigres:v1:${n(e)}:${n(t)}:${n(r)}`};var z="default";var l=a,q={deadline:(e,t)=>`wf-deadline:${l(e)}:${n(t)}`,terminalCleanup:(e,t)=>`wf-cleanup:${l(e)}:${n(t)}`,teardownTimer:(e,t)=>`wf-teardown:${l(e)}:${n(t)}`,delayedStart:(e,t)=>`wf-delayed:${l(e)}:${n(t)}`,terminalWorkflowPrefix:()=>"wf-terminal:",terminalWorkflow:(e,t)=>`wf-terminal:${l(e)}:${n(t)}`},G={budget:(e,t,r)=>`budget:${e}:${t}:${r}`,review:(e,t)=>`review:${n(e)}:${t}`,workflowHeaders:(e)=>`wf-headers:${n(e)}`,childCancellationPrefix:(e)=>`child-cancel:${n(e)}:`,childCancellation:(e,t)=>`child-cancel:${n(e)}:${n(t)}`,childWorkflowByParentPrefix:(e,t)=>`child-by-parent:${n(e)}:${t===void 0?"":`${n(t)}:`}`,childWorkflowByParent:(e,t,r)=>`child-by-parent:${n(e)}:${t===void 0?"":`${n(t)}:`}${n(r)}`,terminalCleanupNeeded:(e)=>`wf-cleanup-needed:${n(e)}`,workflowConcurrency:(e,t)=>`wf-concurrency:${n(e)}:${n(t)}`,workflowConcurrencyHolder:(e)=>`wf-concurrency-holder:${n(e)}`,workflowHasServices:(e)=>`wf-has-services:${n(e)}`,finalizerState:(e)=>`wf-finalizer-state:${n(e)}`,teardownOwed:(e)=>`wf-teardown-needed:${n(e)}`,teardownSucceeded:(e)=>`wf-teardown-succeeded:${n(e)}`,teardownDeadLetter:(e)=>`wf-teardown-deadletter:${n(e)}`,offload:(e,t)=>`offload:${n(e)}:${t}`,archive:(e,t)=>`archive:${n(e)}:${t}`,stateExecution:(e,t)=>`state:execution:${n(e)}:${n(t)}`,stateWorkflow:(e,t)=>`state:workflow-scope:${z}:${n(e)}:${n(t)}`,streamChunkPrefix:(e,t)=>`blob:${n(e)}:${t}:chunk:`,streamChunk:(e,t,r)=>`blob:${n(e)}:${t}:chunk:${String(r).padStart(10,"0")}`,streamTail:(e,t)=>`blob:${n(e)}:${t}:tail`,streamMetadata:(e,t)=>`blob:${n(e)}:${t}:meta`,budgetCharged:(e)=>`budget-charged:${e}`,toolEffect:(e,t,r)=>`tool-effect:${n(e)}:${t}:${r}`,workflowVisibilityStatus:(e,t)=>`wf-idx-status:${n(e)}:${n(t)}`,workflowVisibilityType:(e,t)=>`wf-idx-type:${n(e)}:${n(t)}`,workflowVisibilityCreated:(e,t)=>`wf-idx-created:${l(e)}:${n(t)}`,workflowVisibilityUpdated:(e,t)=>`wf-idx-updated:${l(e)}:${n(t)}`,workflowVisibilityDeadline:(e,t)=>`wf-idx-deadline:${l(e)}:${n(t)}`,workflowVisibilityManifest:(e)=>`wf-idx-manifest:${n(e)}`,workflowVisibilityMetaVersion:()=>"wf-idx-meta:version",workflowVisibilityMetaBuiltAt:()=>"wf-idx-meta:built-at",workflowVisibilityMetaCursor:()=>"wf-idx-meta:cursor"};var v=a,H={workflow:(e)=>`wf:${n(e)}`,checkpoint:(e)=>`wf:${n(e)}:ckpt`,checkpointHistory:(e,t)=>`wf:${n(e)}:ckpt:${String(t).padStart(10,"0")}`,timelinePrefix:(e)=>`wf:${n(e)}:timeline:`,timeline:(e,t)=>`wf:${n(e)}:timeline:${String(t).padStart(10,"0")}`,schedule:(e)=>`schedule:${n(e)}`,scheduleTick:(e,t)=>`schedule-due:${String(e).padStart(16,"0")}:${n(t)}`,scheduleRun:(e)=>`schedule-run:${n(e)}`,scheduleRunLink:(e)=>`schedule-run-link:${n(e)}`,scheduleRunBySchedulePrefix:(e)=>`schedule-run-by-schedule:${n(e)}:`,scheduleRunBySchedule:(e,t)=>`schedule-run-by-schedule:${n(e)}:${n(t)}`,operation:(e,t,r)=>`op:${e}:${v(t)}:${r}`,operationInflight:(e)=>`op:inflight:${e}`,operationQueued:(e)=>`op:queued:${e}`,operationResolved:(e)=>`op:resolved:${e}`,bulkOperationAuditPrefix:()=>"audit:bulk:",bulkOperationAudit:(e,t,r)=>`audit:bulk:${v(e)}:${n(t)}:${n(r)}`,operationResolvedByTimePrefix:()=>"op:resolved-by-time:",operationResolvedByTime:(e,t)=>`op:resolved-by-time:${v(e)}:${n(t)}`,asyncActivity:(e,t)=>`async-act:v1:${n(e)}:${n(t)}`,asyncActivityResolution:(e,t)=>`async-act:v1:${n(e)}:${n(t)}:resolution`,activityReconciliationPrefix:(e)=>`actrec:v1:${n(e)}:`,activityReconciliation:(e,t,r)=>`actrec:v1:${n(e)}:${n(t)}:${r}`,eventPrefix:(e)=>`ev:${n(e)}:`,event:(e,t)=>`ev:${n(e)}:${String(t).padStart(10,"0")}`,eventHead:(e)=>`ev:${n(e)}:head`,eventWatermark:(e)=>`ev:${n(e)}:watermark`,fleetEventPrefix:()=>"fleet-event:",fleetEvent:(e)=>`fleet-event:${String(e).padStart(10,"0")}`,fleetEventTail:()=>"fleet-event-tail",fleetEventWatermark:()=>"fleet-event-watermark",fleetEventByWorkflowPrefix:(e)=>`fleet-event-by-workflow:${n(e)}:`,fleetEventByWorkflow:(e,t)=>`fleet-event-by-workflow:${n(e)}:${String(t).padStart(10,"0")}`},V={attribute:(e)=>`attr:${n(e)}`,attributeIndex:(e,t,r)=>`idx:${e}:${t}:${n(r)}`,tagIndex:(e,t)=>`tag:${n(e)}:${n(t)}`,updatePrefix:(e)=>`upd:${n(e)}:`,update:(e,t)=>`upd:${n(e)}:${t}`,updateResponse:(e)=>`upr:${e}`,updateIdempotency:(e,t)=>`upk:${n(e)}:${t}`,startIdempotency:(e)=>`start-idem:${n(e)}`,startIdempotencySignalId:(e)=>`start-idem:${e}`};var O=1e4;class X extends Error{code="StorageBatchOperationLimitExceededError";cap=O;count;target;constructor(e,t){super(`${e} count ${t} exceeds MAX_BATCH_OPERATIONS (${O}).`);this.name="StorageBatchOperationLimitExceededError",this.target=e,this.count=t}}function d(e,t){if(t>O)throw new X(e,t)}function j(e){return e.length>0?e.slice(0,-1)+String.fromCharCode(e.charCodeAt(e.length-1)+1):"ÿ"}function Q(e,t={}){if(t.gt!==void 0&&e<=t.gt)return!1;if(t.gte!==void 0&&e<t.gte)return!1;if(t.lt!==void 0&&e>=t.lt)return!1;if(t.lte!==void 0&&e>t.lte)return!1;return!0}function at(e,t){if(e===null||t===null)return e===t;if(e.byteLength!==t.byteLength)return!1;for(let r=0;r<e.byteLength;r++)if(e[r]!==t[r])return!1;return!0}async function Z(e,t){if(e.has)return e.has(t);return b(e,t)}function J(e,t,r){if(e.keys)return e.keys(t,r);return c(e,t,r)}async function ee(e,t){if(e.count)return e.count(t);return S(e,t)}async function te(e,t){if(e.deletePrefix)return e.deletePrefix(t);return E(e,t)}async function ne(e,t,r){if(d("conditionalBatch conditions",t.length),d("conditionalBatch operations",r.length),W(e,"conditionalBatch","storageConditionalBatch"),!e.conditionalBatch)throw Error("This storage backend reports conditionalBatch capability but does not implement the conditionalBatch() method.");return e.conditionalBatch(t,r)}var lt={...H,...M,...q,...V,...I,...N,...Y,...U,...K,...L,...G};function ue(e){if(e===void 0)return;if(typeof e!=="number"||!Number.isInteger(e)||e<0)throw Error("deleteRange limit must be a finite non-negative integer");return e===0?0:e}function f(e){let t={},r=!1;for(let s of["gt","gte","lt","lte"]){let o=e[s];if(o===void 0)continue;if(typeof o!=="string")throw Error("deleteRange bounds must be strings");t[s]=o,r=!0}if(!r)throw Error("deleteRange requires at least one of gt/gte/lt/lte; use deletePrefix to delete a whole prefix");let i=ue(e.limit);if(i!==void 0)t.limit=i;return t}async function re(e,t,r){let i=f(r);if(e.deleteRange)return e.deleteRange(t,i);return w(e,t,i)}function de(e,t){let r={key:e,open:!1};if(t.gte!==void 0&&t.gte>r.key)r.key=t.gte,r.open=!1;if(t.gt!==void 0&&t.gt>=r.key)r.key=t.gt,r.open=!0;return r}function pe(e,t){let r={key:j(e),open:!0};if(t.lt!==void 0&&t.lt<=r.key)r.key=t.lt,r.open=!0;if(t.lte!==void 0&&t.lte<r.key)r.key=t.lte,r.open=!1;return r}function dt(e,t){let r=de(e,t),i=pe(e,t);if(r.key>i.key||r.key===i.key&&(r.open||i.open))return null;return{lower:r,upper:i}}function R(e){return e.replaceAll(/:+$/g,"")}function fe(e,t){let r=R(e),i=R(t);if(r.length===0)return i;if(i.length===0)return r;return`${r}:${i}`}class C{#t;#n;constructor(e,t){this.#t=e,this.#n=R(t)}#e(e){if(this.#n.length===0)return e;return e.length===0?`${this.#n}:`:`${this.#n}:${e}`}#r(e){return e.map((t)=>{if(t.type==="put")return{type:"put",key:this.#e(t.key),value:t.value};return{type:"delete",key:this.#e(t.key)}})}#o(e){if(this.#n.length===0)return e;return e.slice(this.#n.length+1)}#i(e){let t={};if(e.limit!==void 0)t.limit=e.limit;if(e.gt!==void 0)t.gt=this.#e(e.gt);if(e.gte!==void 0)t.gte=this.#e(e.gte);if(e.lt!==void 0)t.lt=this.#e(e.lt);if(e.lte!==void 0)t.lte=this.#e(e.lte);return t}#s(e={}){let t=this.#i(e);if(e.reverse!==void 0)t.reverse=e.reverse;return t}#g(e){return this.#i(e)}capabilities(){return this.#t.capabilities()}scoped(e){return new C(this.#t,fe(this.#n,e))}async get(e){return this.#t.get(this.#e(e))}async put(e,t){await this.#t.put(this.#e(e),t)}async delete(e){await this.#t.delete(this.#e(e))}async*scan(e,t){for await(let[r,i]of this.#t.scan(this.#e(e),this.#s(t)))yield[this.#o(r),i]}async batch(e){d("batch operations",e.length),await this.#t.batch(this.#r(e))}async conditionalBatch(e,t){return ne(this.#t,e.map((r)=>({key:this.#e(r.key),expectedValue:r.expectedValue})),this.#r(t))}async has(e){return Z(this.#t,this.#e(e))}async deletePrefix(e){return te(this.#t,this.#e(e))}async deleteRange(e,t){let r=this.#g(f(t));return re(this.#t,this.#e(e),r)}async*keys(e,t){for await(let r of J(this.#t,this.#e(e),this.#s(t)))yield this.#o(r)}async count(e){return ee(this.#t,this.#e(e))}[Symbol.dispose](){this.#t[Symbol.dispose]()}}function ie(e,t){return new C(e,t)}var ae=1,y="__weftStorageKeyspace",me=102400,ye=8192;function he(e){return typeof e==="object"&&e!==null&&"then"in e&&typeof e.then==="function"}function _(e){if(!u(e))return!1;if(e.__weftStorage!==ae)return!1;return typeof e.value==="string"}function be(e){if(!u(e))return!1;return Object.values(e).every(_)}function x(e){if(e!==y)return;throw Error(`WebExtensionStorage key "${y}" is reserved for adapter metadata.`)}function Se(e){for(let t of e)x(t.key)}function oe(e){return{__weftStorage:ae,value:T(e)}}function se(e){if(!_(e))return null;return D(e.value)}function Ee(e){return _(e)}function k(e){return new TextEncoder().encode(JSON.stringify(e)).byteLength}function $e(e){if(!u(e))return!1;if(e.runtime!==void 0&&!u(e.runtime))return!1;return e.storage===void 0||u(e.storage)}function we(e){if(e!==void 0){if(!$e(e))throw Error("WebExtensionStorage injected namespace must be an object.");return e}let t=globalThis,r=t.browser??t.chrome;if(r?.storage===void 0)throw Error("WebExtensionStorage requires globalThis.browser.storage or globalThis.chrome.storage.");return r}function xe(e,t){let r=e.storage?.[t];if(r===void 0)throw Error(`WebExtensionStorage area "${t}" is not available.`);return r}function ve(e){if(e==="session")return"ephemeral";if(e==="sync"||e==="managed")return"remote";return"local"}function Oe(e){let t=e.runtime?.lastError?.message;return t===void 0?null:Error(t)}async function P(e,t,r){return new Promise((i,s)=>{let o=!1,p=(g)=>{if(o)return;let A=Oe(e);if(o=!0,A!==null)s(A);else i(g)},B=(g)=>{if(o)return;o=!0,s(g)},le=(g)=>p(g),h;try{h=t(le)}catch{try{h=r()}catch(g){B(g);return}}if(he(h))h.then(p,B)})}class m{static#t=new WeakMap;#n;#e;#r;#o;#i;constructor(e={},t){this.#r=e.area??"local",this.#o=ve(this.#r),this.#n=we(t),this.#e=xe(this.#n,this.#r),this.#i=()=>{},this.#n.storage?.onChanged?.addListener?.(this.#i)}capabilities(){return{persistence:this.#o,readAfterWrite:"session",scanConsistency:"best-effort",atomicBatch:!1,conditionalBatch:!1,boundedRangeDelete:!1}}async#s(e){return P(this.#n,(t)=>this.#e.get(e,t),()=>this.#e.get(e))}async#g(e){await P(this.#n,(t)=>this.#e.set(e,t),()=>this.#e.set(e))}async#d(e){if(this.#e.getBytesInUse===void 0)return null;return P(this.#n,(t)=>this.#e.getBytesInUse?.(e,t),()=>this.#e.getBytesInUse?.(e))}async#a(){let t=(await this.#s(y))[y];if(t===void 0)return{};if(!be(t))throw Error("WebExtensionStorage keyspace is not a valid storage envelope map.");return{...t}}async#c(e){let t={[y]:e};await this.#m(t),await this.#g(t)}async#u(e){let t=m.#t.get(this.#e)??Promise.resolve(),r={},i=new Promise((o)=>{r.release=o}),s=t.catch(()=>{return}).then(()=>i);m.#t.set(this.#e,s),await t.catch(()=>{return});try{return await e()}finally{if(r.release?.(),m.#t.get(this.#e)===s)m.#t.delete(this.#e)}}#l(){if(this.#r==="managed")throw Error('WebExtensionStorage area "managed" is read-only.')}#p(e){let t=this.#e.QUOTA_BYTES_PER_ITEM??ye;for(let[r,i]of Object.entries(e)){let s=k({[r]:i});if(s>t)throw Error(`WebExtensionStorage sync item quota exceeded for "${r}": ${String(s)} > ${String(t)}.`)}}async#f(e){let t=Object.keys(e),r=await this.#d(null),i=await this.#d(t);if(r!==null&&i!==null)return r-i+k(e);let s=await this.#s(null);return k({...s,...e})}async#m(e){if(this.#r!=="sync")return;this.#p(e);let t=this.#e.QUOTA_BYTES??me,r=await this.#f(e);if(r>t)throw Error(`WebExtensionStorage sync total quota exceeded: ${String(r)} > ${String(t)}.`)}async get(e){x(e);let t=await this.#a();return se(t[e])}async put(e,t){this.#l(),x(e),await this.#u(async()=>{let r=await this.#a();r[e]=oe(t),await this.#c(r)})}async delete(e){this.#l(),x(e),await this.#u(async()=>{let t=await this.#a();if(!(e in t))return;delete t[e],await this.#c(t)})}async*scan(e,t={}){let r=await this.#a(),i=Object.keys(r).filter((o)=>o.startsWith(e)).filter((o)=>Q(o,t)).filter((o)=>Ee(r[o])).toSorted();if(t.reverse)i=i.toReversed();let s=0;for(let o of i){if(t.limit!==void 0&&s>=t.limit)break;let p=se(r[o]);if(p===null)continue;yield[o,p],s+=1}}async batch(e){if(d("batch operations",e.length),this.#l(),e.length===0)return;Se(e),await this.#u(async()=>{let t=await this.#a();for(let r of e)if(r.type==="put")t[r.key]=oe(r.value);else delete t[r.key];await this.#c(t)})}has(e){return b(this,e)}async*keys(e,t){yield*c(this,e,t)}count(e){return S(this,e)}async deletePrefix(e){return this.#l(),E(this,e)}async deleteRange(e,t){let r=f(t);return this.#l(),w(this,e,r)}scoped(e){return ie(this,e)}[Symbol.dispose](){this.#n.storage?.onChanged?.removeListener?.(this.#i)}}export{m as WebExtensionStorage};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lostgradient/weft",
3
- "version": "0.24.1",
3
+ "version": "0.24.2",
4
4
  "description": "A Bun-native durable execution engine with pluggable key-value storage.",
5
5
  "keywords": [
6
6
  "bun",