The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Investigate - Slow transformRichTextV2 function (#16535)
Etienned36d0d49d1c6
diff --git a/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text-v2.util.ts b/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text-v2.util.tsindex 44825e0bcc5..7d8bc4665df 100644--- a/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text-v2.util.ts+++ b/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text-v2.util.ts@@ -1,3 +1,5 @@+import { Logger } from '@nestjs/common';+import { isNonEmptyString } from '@sniptt/guards';import {type RichTextV2Metadata,@@ -5,14 +7,32 @@ import {} from 'twenty-shared/types';import { isDefined } from 'twenty-shared/utils';+const logger = new Logger('TransformRichTextV2');++const calculateInputSize = (input: unknown): number => {+ if (typeof input === 'string') {+ return Buffer.byteLength(input, 'utf8');+ }+ if (typeof input === 'object' && input !== null) {+ return Buffer.byteLength(JSON.stringify(input), 'utf8');+ }++ return 0;+};+export const transformRichTextV2Value = async (// eslint-disable-next-line @typescript-eslint/no-explicit-anyrichTextValue: any,): Promise<RichTextV2Metadata> => {+ const startTime = performance.now();+ const inputSize = calculateInputSize(richTextValue);+const parsedValue = isNonEmptyString(richTextValue)? richTextV2ValueSchema.parse(richTextValue): richTextValue;+ const afterParsingTime = performance.now();+const { ServerBlockNoteEditor } = await import('@blocknote/server-util');const serverBlockNoteEditor = ServerBlockNoteEditor.create();@@ -31,6 +51,8 @@ export const transformRichTextV2Value = async (convertedMarkdown = parsedValue.blocknote || null;}+ const afterMarkdownConversionTime = performance.now();+const convertedBlocknote = parsedValue.markdown? JSON.stringify(await serverBlockNoteEditor.tryParseMarkdownToBlocks(@@ -39,6 +61,12 @@ export const transformRichTextV2Value = async (): null;+ const endTime = performance.now();++ logger.debug(+ `transformRichTextV2Value completed - total: ${endTime - startTime}ms, parsing: ${afterParsingTime - startTime}ms, markdown_conversion: ${afterMarkdownConversionTime - afterParsingTime}ms, blocknote_conversion: ${endTime - afterMarkdownConversionTime}ms, size: ${inputSize} bytes`,+ );+return {markdown: parsedValue.markdown || convertedMarkdown,blocknote: parsedValue.blocknote || convertedBlocknote,