durability 4.1.0 → 4.1.1
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 +1 -1
- package/dist/serialization.js +23 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -86,7 +86,7 @@ switch (result.status) {
|
|
|
86
86
|
}
|
|
87
87
|
```
|
|
88
88
|
|
|
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.
|
|
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; as with `JSON.stringify`, `undefined` properties are dropped and `undefined` array elements become `null`.
|
|
90
90
|
|
|
91
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
92
|
|
package/dist/serialization.js
CHANGED
|
@@ -3,10 +3,32 @@ const storedValueSchema = z.discriminatedUnion('kind', [
|
|
|
3
3
|
z.object({ kind: z.literal('value'), value: z.json() }),
|
|
4
4
|
z.object({ kind: z.literal('undefined') }),
|
|
5
5
|
]);
|
|
6
|
+
/**
|
|
7
|
+
* What `JSON.stringify` would produce: `undefined` properties are dropped and
|
|
8
|
+
* `undefined` array elements become `null`. `z.json()` rejects both as given,
|
|
9
|
+
* although they are routine in values such as lifecycle events whose handler
|
|
10
|
+
* returned nothing.
|
|
11
|
+
*/
|
|
12
|
+
const asJson = (value) => {
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
return value.map((item) => (item === undefined ? null : asJson(item)));
|
|
15
|
+
}
|
|
16
|
+
if (value === null || typeof value !== 'object') {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
// Only plain objects are rewritten; anything else must fail validation as is.
|
|
20
|
+
const prototype = Object.getPrototypeOf(value);
|
|
21
|
+
if (prototype === Object.prototype || prototype === null) {
|
|
22
|
+
return Object.fromEntries(Object.entries(value)
|
|
23
|
+
.filter(([, item]) => item !== undefined)
|
|
24
|
+
.map(([key, item]) => [key, asJson(item)]));
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
6
28
|
// Wrapping preserves a successful `undefined` result, which bare JSON cannot represent.
|
|
7
29
|
export const serialize = (value) => JSON.stringify(value === undefined
|
|
8
30
|
? { kind: 'undefined' }
|
|
9
|
-
: { kind: 'value', value: z.json().parse(value) });
|
|
31
|
+
: { kind: 'value', value: z.json().parse(asJson(value)) });
|
|
10
32
|
export const deserialize = (value) => {
|
|
11
33
|
const stored = storedValueSchema.parse(JSON.parse(value));
|
|
12
34
|
return stored.kind === 'undefined' ? undefined : stored.value;
|