-
-
Notifications
You must be signed in to change notification settings - Fork 80
Update chat.ts #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update chat.ts #190
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // server/chat.ts | ||
| import type { Server, Socket } from "socket.io"; | ||
|
|
||
| import escapeHtml from "escape-html"; // npm install escape-html | ||
| // --- Simple in-memory per-room history --- | ||
| type HistItem = { | ||
| text: string; | ||
|
|
@@ -72,8 +72,11 @@ export function wireChat(io: Server, socket: Socket) { | |
| // Broadcast a message to everyone in the chat room | ||
| socket.on("chat:message", (payload: ChatMessagePayload) => { | ||
| const { roomId, text, from, clientId, ts } = payload || {}; | ||
| const safeText = (text || "").toString().trim().slice(0, 1000); | ||
| if (!roomId || !safeText) return; | ||
|
|
||
| if (!roomId || !text || !from) return; | ||
| let safeText = text.toString().trim(); | ||
| safeText = safeText.slice(0, 1000); | ||
| safeText = escapeHtml(safeText); | ||
|
Comment on lines
+76
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate text after trimming to prevent empty messages. The current validation checks Apply this diff to fix the validation order: -
- if (!roomId || !text || !from) return;
- let safeText = text.toString().trim();
+ if (!roomId || !from) return;
+ let safeText = text?.toString().trim();
+ if (!safeText) return;
safeText = safeText.slice(0, 1000);
safeText = escapeHtml(safeText);
π€ Prompt for AI Agents |
||
|
|
||
| const final = { | ||
| text: safeText, | ||
|
|
@@ -123,4 +126,4 @@ export function wireChat(io: Server, socket: Socket) { | |
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
Verify that the
escape-htmldependency is installed.Ensure that
escape-htmlhas been added topackage.jsondependencies. Without it, the application will fail at runtime with a module not found error.Run the following script to verify the dependency exists:
π Script executed:
Length of output: 44
π Script executed:
Length of output: 271
π Script executed:
Length of output: 756
Add
escape-htmltobackend/package.jsondependencies.The import statement on line 3 references the
escape-htmlpackage, but it is not listed inbackend/package.jsondependencies. This will cause a module not found error at runtime. Add"escape-html": "^1.4.2"(or appropriate version) to the dependencies section inbackend/package.json.π€ Prompt for AI Agents