Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/cli/src/ui/hooks/useConsoleMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function consoleMessagesReducer(
state: ConsoleMessageItem[],
action: Action,
): ConsoleMessageItem[] {
const MAX_CONSOLE_MESSAGES = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move const to the top level.

switch (action.type) {
case 'ADD_MESSAGES': {
const newMessages = [...state];
Expand All @@ -51,6 +52,12 @@ function consoleMessagesReducer(
newMessages.push({ ...queuedMessage, count: 1 });
}
}

// Limit the number of messages to prevent memory issues
if (newMessages.length > MAX_CONSOLE_MESSAGES) {
return newMessages.slice(newMessages.length - MAX_CONSOLE_MESSAGES);
}

return newMessages;
}
case 'CLEAR':
Expand Down Expand Up @@ -91,9 +98,17 @@ export function useConsoleMessages(): UseConsoleMessagesReturn {

useEffect(() => {
const handleConsoleLog = (payload: ConsoleLogPayload) => {
let content = payload.content;
const MAX_CONSOLE_MSG_LENGTH = 10000;
if (content.length > MAX_CONSOLE_MSG_LENGTH) {
content =
content.slice(0, MAX_CONSOLE_MSG_LENGTH) +
`... [Truncated ${content.length - MAX_CONSOLE_MSG_LENGTH} characters]`;
}

handleNewMessage({
type: payload.type,
content: payload.content,
content,
count: 1,
});
};
Expand All @@ -102,10 +117,18 @@ export function useConsoleMessages(): UseConsoleMessagesReturn {
isStderr: boolean;
chunk: Uint8Array | string;
}) => {
const content =
let content =
typeof payload.chunk === 'string'
? payload.chunk
: new TextDecoder().decode(payload.chunk);

const MAX_OUTPUT_CHUNK_LENGTH = 10000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a helper for this string truncation logic

if (content.length > MAX_OUTPUT_CHUNK_LENGTH) {
content =
content.slice(0, MAX_OUTPUT_CHUNK_LENGTH) +
`... [Truncated ${content.length - MAX_OUTPUT_CHUNK_LENGTH} characters]`;
}

// It would be nice if we could show stderr as 'warn' but unfortunately
// we log non warning info to stderr before the app starts so that would
// be misleading.
Expand Down
Loading