chore(workflow-engine-*): cleanup and improvements (#13789)

**What**
Cleanup recent work on workflows
This commit is contained in:
Adrien de Peretti
2025-10-23 12:50:24 +02:00
committed by GitHub
parent 356dcc94ce
commit d51ae2768b
10 changed files with 344 additions and 310 deletions

View File

@@ -1,18 +1,49 @@
import { isDefined } from "./is-defined"
/**
* Only apply JSON.parse JSON.stringify when we have objects, arrays, dates, etc..
* Creates a deep copy of the input, ensuring it's JSON-serializable.
* - Breaks all reference sharing (creates new objects/arrays)
* - Removes non-serializable values (functions, symbols, undefined properties)
* - Normalizes special types (Date -> string)
* - Only stringifies special objects, not entire tree (optimization)
* @param result
* @returns
* @returns A deep copy with no shared references, guaranteed to be JSON-serializable
*/
export function parseStringifyIfNecessary(result: unknown) {
if (typeof result == null || typeof result !== "object") {
export function parseStringifyIfNecessary(result: unknown): any {
if (result == null || typeof result !== "object") {
return result
}
const strResult = JSON.stringify(result)
if (isDefined(strResult)) {
return JSON.parse(strResult)
if (Array.isArray(result)) {
return result.map((item) => parseStringifyIfNecessary(item))
}
return result
const isPlainObject =
result.constructor === Object || result.constructor === undefined
if (!isPlainObject) {
const strResult = JSON.stringify(result)
if (isDefined(strResult)) {
return JSON.parse(strResult)
}
return undefined
}
const copy: any = {}
for (const key in result) {
if (result.hasOwnProperty(key)) {
const value = (result as any)[key]
if (typeof value === "function" || typeof value === "symbol") {
continue
}
const copiedValue = parseStringifyIfNecessary(value)
if (copiedValue !== undefined) {
copy[key] = copiedValue
}
}
}
return copy
}