diff --git a/.changeset/poor-mugs-cheat.md b/.changeset/poor-mugs-cheat.md new file mode 100644 index 0000000000..7fd6377bd9 --- /dev/null +++ b/.changeset/poor-mugs-cheat.md @@ -0,0 +1,7 @@ +--- +"@medusajs/event-bus-local": patch +"@medusajs/utils": patch +"@medusajs/workflows-sdk": patch +--- + +chore(workflows-sdk, utils): Prevent unnecessary serialization diff --git a/packages/core/utils/src/common/index.ts b/packages/core/utils/src/common/index.ts index 67cfaad8ab..22c44aaf46 100644 --- a/packages/core/utils/src/common/index.ts +++ b/packages/core/utils/src/common/index.ts @@ -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" diff --git a/packages/core/utils/src/common/parse-stringify-if-necessary.ts b/packages/core/utils/src/common/parse-stringify-if-necessary.ts new file mode 100644 index 0000000000..b341f782db --- /dev/null +++ b/packages/core/utils/src/common/parse-stringify-if-necessary.ts @@ -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 +} diff --git a/packages/core/workflows-sdk/src/utils/composer/helpers/resolve-value.ts b/packages/core/workflows-sdk/src/utils/composer/helpers/resolve-value.ts index 381e826868..0b6b8de748 100644 --- a/packages/core/workflows-sdk/src/utils/composer/helpers/resolve-value.ts +++ b/packages/core/workflows-sdk/src/utils/composer/helpers/resolve-value.ts @@ -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) } diff --git a/packages/modules/event-bus-local/src/services/event-bus-local.ts b/packages/modules/event-bus-local/src/services/event-bus-local.ts index a7cf76c454..a4aeb2f56c 100644 --- a/packages/modules/event-bus-local/src/services/event-bus-local.ts +++ b/packages/modules/event-bus-local/src/services/event-bus-local.ts @@ -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(eventData: Message) { - 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(() =>