durability 4.0.0 → 4.1.0

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/README.md CHANGED
@@ -88,6 +88,8 @@ switch (result.status) {
88
88
 
89
89
  The completed result type is inferred from the handler. Looking up a key that belongs to another operation throws `DuplicateDurableCallError`. Payloads and results must be JSON-serializable.
90
90
 
91
+ An ID stays bound to its operation and payload. Registering it again with the same operation and the same data (object key order does not matter; array order and values do) is a no-op, which is what makes retries safe. Registering it with another operation throws `DuplicateDurableCallError`, and with a different payload `DuplicateDurablePayloadError`, instead of silently dropping the new call.
92
+
91
93
  ## Named alarms
92
94
 
93
95
  ```ts
@@ -37,7 +37,7 @@ export type OperationResults<Handlers extends HandlerMap> = {
37
37
  [Operation in Extract<keyof Handlers, string>]: HandlerResult<Handlers[Operation]>;
38
38
  };
39
39
  type DurableOperationInput<Handler> = {
40
- /** Stable idempotency key used to deduplicate the operation. */
40
+ /** Stable idempotency key; bound to the operation and its payload. */
41
41
  id: string;
42
42
  /** JSON-serializable input passed to the operation handler. */
43
43
  payload: HandlerPayload<Handler>;
@@ -11,12 +11,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
11
  };
12
12
  var _DurabilityCore_instances, _DurabilityCore_engine, _DurabilityCore_handlers, _DurabilityCore_policy, _DurabilityCore_lifecycle, _DurabilityCore_emit, _DurabilityCore_active, _DurabilityCore_executionStarts, _DurabilityCore_trackedSource, _DurabilityCore_runDue, _DurabilityCore_getResult, _DurabilityCore_run, _DurabilityCore_execute, _DurabilityCore_start;
13
13
  import { z } from 'zod';
14
- import { DuplicateDurableCallError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from './errors.js';
14
+ import { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, isErrorInstance, NonRetryableError, reportFailure, } from './errors.js';
15
15
  import { createLifecycleHooks, } from './lifecycle.js';
16
16
  import { durabilityOperationMigrations, migrate, } from './migrations.js';
17
17
  import { createPolicyResolver, methodOptionsSchema, } from './policy.js';
18
18
  import { engineFor, } from './scheduler.js';
19
- import { deserialize, serialize } from './serialization.js';
19
+ import { canonical, deserialize, serialize } from './serialization.js';
20
20
  import { listCreated, updateAttempt } from './storage.js';
21
21
  import { registerTrackedSource, } from './tracked-source.js';
22
22
  const callRowSchema = z.object({
@@ -209,6 +209,11 @@ _DurabilityCore_engine = new WeakMap(), _DurabilityCore_handlers = new WeakMap()
209
209
  throw new Error(`Durable call "${input.id}" was not persisted`);
210
210
  }
211
211
  assertOperation(input.id, current.operation, input.operation);
212
+ // Reuse is idempotent only for the same data; anything else is a caller
213
+ // bug that would otherwise be dropped silently.
214
+ if (!created && canonical(current.payload) !== canonical(payload)) {
215
+ throw new DuplicateDurablePayloadError(input.id, input.operation);
216
+ }
212
217
  return { inserted: created, winner: current };
213
218
  });
214
219
  if (!inserted) {
package/dist/errors.d.ts CHANGED
@@ -49,6 +49,10 @@ export declare class DurableAttemptsExhaustedError extends Error {
49
49
  export declare class DuplicateDurableCallError extends Error {
50
50
  constructor(id: string, existingOperation: string, requestedOperation: string);
51
51
  }
52
+ /** Thrown when an idempotency key is reused for the same operation with a different payload. */
53
+ export declare class DuplicateDurablePayloadError extends Error {
54
+ constructor(id: string, operation: string);
55
+ }
52
56
  /**
53
57
  * Error thrown when a fanout delivery is settled twice.
54
58
  *
package/dist/errors.js CHANGED
@@ -76,6 +76,13 @@ export class DuplicateDurableCallError extends Error {
76
76
  this.name = 'DuplicateDurableCallError';
77
77
  }
78
78
  }
79
+ /** Thrown when an idempotency key is reused for the same operation with a different payload. */
80
+ export class DuplicateDurablePayloadError extends Error {
81
+ constructor(id, operation) {
82
+ super(`Durable call "${id}" for operation "${operation}" was registered with a different payload`);
83
+ this.name = 'DuplicateDurablePayloadError';
84
+ }
85
+ }
79
86
  /**
80
87
  * Error thrown when a fanout delivery is settled twice.
81
88
  *
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { DurabilityLog, maxLogBytes, type DurabilityLogConfig, type LogAppendInp
5
5
  export { RoutingLoad, type EnqueueResult, type LoadSnapshot, type ProcessingLoad, } from './load.js';
6
6
  export type { MessageWrite, StoredMessage } from './stored-message.js';
7
7
  export { DurabilityScheduler, type DurabilityContext, type DurabilitySchedulerConfig, type DurabilitySchedulerOptions, type SchedulerAttachment, } from './scheduler.js';
8
- export { DuplicateDurableCallError, DurableAlarmTimeoutError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, DurableRetryPolicyError, NonRetryableError, FanoutTimeoutError, FanoutSettlementError, FanoutEnqueueError, LogAppendError, LogLeaseError, LogTruncatedError, RoutingError, } from './errors.js';
8
+ export { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAlarmTimeoutError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, DurableRetryPolicyError, NonRetryableError, FanoutTimeoutError, FanoutSettlementError, FanoutEnqueueError, LogAppendError, LogLeaseError, LogTruncatedError, RoutingError, } from './errors.js';
9
9
  export type { DurabilityLifecycleEvent, LifecycleHook } from './lifecycle.js';
10
10
  export { durabilityNamedAlarmMigrations, durabilityOperationMigrations, durabilityFanoutMigrations, durabilityLogLeaseMigrations, durabilityLogMigrations, type DurabilityMigrationResult, type MigrationCapability, } from './migrations.js';
11
11
  export type { DurabilityAlarmMethodOptions, DurabilityMethodOptions, DurabilityRetryOptions, } from './policy.js';
package/dist/index.js CHANGED
@@ -4,5 +4,5 @@ export { DurabilityFanout, } from './fanout.js';
4
4
  export { DurabilityLog, maxLogBytes, } from './log.js';
5
5
  export { RoutingLoad, } from './load.js';
6
6
  export { DurabilityScheduler, } from './scheduler.js';
7
- export { DuplicateDurableCallError, DurableAlarmTimeoutError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, DurableRetryPolicyError, NonRetryableError, FanoutTimeoutError, FanoutSettlementError, FanoutEnqueueError, LogAppendError, LogLeaseError, LogTruncatedError, RoutingError, } from './errors.js';
7
+ export { DuplicateDurableCallError, DuplicateDurablePayloadError, DurableAlarmTimeoutError, DurableAttemptsExhaustedError, DurableAttemptTimeoutError, DurableResultSerializationError, DurableRetryPolicyError, NonRetryableError, FanoutTimeoutError, FanoutSettlementError, FanoutEnqueueError, LogAppendError, LogLeaseError, LogTruncatedError, RoutingError, } from './errors.js';
8
8
  export { durabilityNamedAlarmMigrations, durabilityOperationMigrations, durabilityFanoutMigrations, durabilityLogLeaseMigrations, durabilityLogMigrations, } from './migrations.js';
@@ -1,2 +1,7 @@
1
1
  export declare const serialize: (value: unknown) => string;
2
2
  export declare const deserialize: (value: string) => unknown;
3
+ /**
4
+ * The stored form with object keys sorted at every depth, so two payloads
5
+ * compare equal when they carry the same data regardless of key order.
6
+ */
7
+ export declare const canonical: (stored: string) => string;
@@ -11,3 +11,19 @@ export const deserialize = (value) => {
11
11
  const stored = storedValueSchema.parse(JSON.parse(value));
12
12
  return stored.kind === 'undefined' ? undefined : stored.value;
13
13
  };
14
+ const canonicalize = (value) => {
15
+ if (Array.isArray(value)) {
16
+ return value.map(canonicalize);
17
+ }
18
+ if (value !== null && typeof value === 'object') {
19
+ return Object.fromEntries(Object.keys(value)
20
+ .sort()
21
+ .map((key) => [key, canonicalize(value[key])]));
22
+ }
23
+ return value;
24
+ };
25
+ /**
26
+ * The stored form with object keys sorted at every depth, so two payloads
27
+ * compare equal when they carry the same data regardless of key order.
28
+ */
29
+ export const canonical = (stored) => JSON.stringify(canonicalize(JSON.parse(stored)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "durability",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
5
5
  "type": "module",
6
6
  "license": "ISC",
@@ -60,8 +60,8 @@
60
60
  "dependencies": {
61
61
  "@standard-schema/spec": "^1.1.0",
62
62
  "zod": "^4.4.3",
63
- "@durability/transforms": "^0.4.0",
64
- "@durability/storage": "^0.1.1"
63
+ "@durability/storage": "^0.1.1",
64
+ "@durability/transforms": "^0.4.0"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "@cloudflare/workers-types": "^4.20260702.1"