chore(workflows-sdk, utils): Prevent unnecessary serialization (#13413)

* chore(workflows-sdk, utils): Prevent unnecessary serialization

* Create poor-mugs-cheat.md
This commit is contained in:
Adrien de Peretti
2025-09-04 16:19:35 +02:00
committed by GitHub
parent bd571aca82
commit 55a35e4721
5 changed files with 31 additions and 11 deletions

View File

@@ -0,0 +1,7 @@
---
"@medusajs/event-bus-local": patch
"@medusajs/utils": patch
"@medusajs/workflows-sdk": patch
---
chore(workflows-sdk, utils): Prevent unnecessary serialization

View File

@@ -58,6 +58,7 @@ export * from "./object-to-string-path"
export * from "./omit-deep"
export * from "./optional-numeric-serializer"
export * from "./parse-cors-origins"
export * from "./parse-stringify-if-necessary"
export * from "./partition-array"
export * from "./pick-deep"
export * from "./pick-value-from-object"

View File

@@ -0,0 +1,18 @@
import { isDefined } from "./is-defined"
/**
* Only apply JSON.parse JSON.stringify when we have objects, arrays, dates, etc..
* @param result
* @returns
*/
export function parseStringifyIfNecessary(result: unknown) {
if (typeof result !== "object") {
return result
}
const strResult = JSON.stringify(result)
if (isDefined(strResult)) {
return JSON.parse(strResult)
}
return result
}

View File

@@ -1,7 +1,7 @@
import {
deepCopy,
isDefined,
OrchestrationUtils,
parseStringifyIfNecessary,
promiseAll,
} from "@medusajs/utils"
@@ -83,8 +83,5 @@ export async function resolveValue(input, transactionContext) {
? await resolveProperty(copiedInput, transactionContext)
: await unwrapInput(copiedInput, {})
const strResult = JSON.stringify(result) // Symbols return undefined
if (isDefined(strResult)) {
return JSON.parse(strResult)
}
return parseStringifyIfNecessary(result)
}

View File

@@ -82,16 +82,13 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
// This is useful in the event of a distributed transaction where you'd want to emit
// events only once the transaction ends.
private async groupOrEmitEvent<T = unknown>(eventData: Message<T>) {
const eventData_ = JSON.parse(JSON.stringify(eventData))
const { options, ...eventBody } = eventData_
const { options, ...eventBody } = eventData
const eventGroupId = eventBody.metadata?.eventGroupId
if (eventGroupId) {
await this.groupEvent(eventGroupId, eventData_)
await this.groupEvent(eventGroupId, eventData)
} else {
const { options, ...eventBody } = eventData_
const options_ = options as { delay: number }
const options_ = eventData.options as { delay: number }
const delay = (ms?: number) => (ms ? setTimeout(ms) : Promise.resolve())
delay(options_?.delay).then(() =>