RockteMQ-AI commented on code in PR #3064: URL: https://github.com/apache/rocketmq-dashboard/pull/3064#discussion_r3930169636
########## web/src/utils/messagePayloadPreview.ts: ########## @@ -0,0 +1,410 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { parseMessageProperties } from './messageProperties'; + +export type MessagePropertyMode = 'form' | 'text'; +export type MessagePayloadPreviewStatus = 'ready' | 'warning' | 'error'; +export type MessagePayloadIssueSeverity = 'info' | 'warning' | 'error'; +export type MessageBodyFormat = + 'empty' | 'json-object' | 'json-array' | 'json-scalar' | 'plain-text'; + +export type MessagePayloadIssueCode = + | 'EMPTY_BODY' + | 'BODY_SIZE_LIMIT' + | 'INVALID_PROPERTY_FORMAT' + | 'EMPTY_PROPERTY_KEY' + | 'DUPLICATE_PROPERTY_KEY' + | 'RESERVED_PROPERTY_KEY' + | 'EMPTY_PROPERTY_VALUE' + | 'PROPERTY_COUNT_LIMIT' + | 'PROPERTY_SIZE_LIMIT' + | 'TRIMMED_TAG' + | 'TRIMMED_KEY' + | 'PLAIN_TEXT_BODY' + | 'SCALAR_JSON_BODY'; + +export interface MessagePropertyInput { + key?: string; + value?: string; +} + +export interface MessagePayloadIssue { + code: MessagePayloadIssueCode; + severity: MessagePayloadIssueSeverity; + title: string; + description: string; + field?: 'body' | 'tag' | 'key' | 'properties'; + names?: string[]; +} + +export interface MessagePayloadPreviewInput { + topic?: string; + tag?: string; + key?: string; + body?: string; + propsMode: MessagePropertyMode; + propsText?: string; + properties?: MessagePropertyInput[]; +} + +export interface MessagePayloadPreviewOptions { + maxBodyBytes?: number; + maxProperties?: number; + maxPropertyBytes?: number; +} + +export interface MessagePropertyPreviewEntry { + key: string; + value: string; + keyBytes: number; + valueBytes: number; + reserved: boolean; +} + +export interface MessagePayloadPreview { + status: MessagePayloadPreviewStatus; + issues: MessagePayloadIssue[]; + blockingIssues: MessagePayloadIssue[]; + normalized: { + topic: string; + tag?: string; + key?: string; + body: string; + }; + properties: Record<string, string>; + propertyEntries: MessagePropertyPreviewEntry[]; + summary: { + bodyBytes: number; + tagBytes: number; + keyBytes: number; + propertyCount: number; + propertyBytes: number; + bodyFormat: MessageBodyFormat; + maxBodyBytes: number; + maxProperties: number; + maxPropertyBytes: number; + }; +} + +export const DEFAULT_MAX_MESSAGE_BODY_BYTES = 4 * 1024 * 1024; +export const DEFAULT_MAX_MESSAGE_PROPERTIES = 32; +export const DEFAULT_MAX_MESSAGE_PROPERTY_BYTES = 16 * 1024; + +const RESERVED_PROPERTY_NAMES = new Set([ + 'TAGS', + 'KEYS', + 'UNIQ_KEY', + 'WAIT', + 'DELAY', + 'RETRY_TOPIC', + 'REAL_TOPIC', + 'REAL_QID', + 'TRAN_MSG', + 'PGROUP', +]); + +const textBytes = (value?: string): number => new TextEncoder().encode(value ?? '').length; + +const normalizeText = (value?: string): string => value?.trim() ?? ''; + +const issue = ( + code: MessagePayloadIssueCode, + severity: MessagePayloadIssueSeverity, + title: string, + description: string, + field?: MessagePayloadIssue['field'], + names?: string[], +): MessagePayloadIssue => ({ + code, + severity, + title, + description, + field, + names, +}); + +const bodyFormat = (body: string): MessageBodyFormat => { + const trimmed = body.trim(); + if (!trimmed) return 'empty'; + + try { + const parsed = JSON.parse(trimmed) as unknown; + if (Array.isArray(parsed)) return 'json-array'; + if (parsed !== null && typeof parsed === 'object') return 'json-object'; + return 'json-scalar'; + } catch { + return 'plain-text'; + } +}; + +const fromEntries = (entries: MessagePropertyPreviewEntry[]): Record<string, string> => Review Comment: **[Robustness]** The `fromEntries` function correctly handles prototype pollution (tested in `buildMessagePropertiesFromRows`). The implementation is safe because `Object.fromEntries` creates own properties. Consider adding a brief comment explaining why this is safe, to help future maintainers. ########## web/src/utils/messagePayloadPreview.ts: ########## @@ -0,0 +1,410 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { parseMessageProperties } from './messageProperties'; + +export type MessagePropertyMode = 'form' | 'text'; +export type MessagePayloadPreviewStatus = 'ready' | 'warning' | 'error'; +export type MessagePayloadIssueSeverity = 'info' | 'warning' | 'error'; +export type MessageBodyFormat = + 'empty' | 'json-object' | 'json-array' | 'json-scalar' | 'plain-text'; + +export type MessagePayloadIssueCode = + | 'EMPTY_BODY' + | 'BODY_SIZE_LIMIT' + | 'INVALID_PROPERTY_FORMAT' + | 'EMPTY_PROPERTY_KEY' + | 'DUPLICATE_PROPERTY_KEY' + | 'RESERVED_PROPERTY_KEY' + | 'EMPTY_PROPERTY_VALUE' + | 'PROPERTY_COUNT_LIMIT' + | 'PROPERTY_SIZE_LIMIT' + | 'TRIMMED_TAG' + | 'TRIMMED_KEY' + | 'PLAIN_TEXT_BODY' + | 'SCALAR_JSON_BODY'; + +export interface MessagePropertyInput { + key?: string; + value?: string; +} + +export interface MessagePayloadIssue { + code: MessagePayloadIssueCode; + severity: MessagePayloadIssueSeverity; + title: string; + description: string; + field?: 'body' | 'tag' | 'key' | 'properties'; + names?: string[]; +} + +export interface MessagePayloadPreviewInput { + topic?: string; + tag?: string; + key?: string; + body?: string; + propsMode: MessagePropertyMode; + propsText?: string; + properties?: MessagePropertyInput[]; Review Comment: **[i18n]** The `issue()` helper uses hardcoded Chinese strings for `title` and `description` fields (e.g., `'保留属性名'`, `'自定义属性名不能与 RocketMQ 内部属性同名'`). These should use i18n translation keys via `t()`, similar to how PR #3063 handles parser errors. Consider adding translation keys like `payloadPreview.reservedPropertyTitle` / `payloadPreview.reservedPropertyDesc` to `translations.ts`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
