Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(ls C:/Users/Hanna/projects/node/node_chat/.eslint* 2>/dev/null; ls C:/Users/Hanna/projects/node/node_chat/src/public/.eslint* 2>/dev/null; cat C:/Users/Hanna/projects/node/node_chat/package.json | grep -A 20 eslint)",
"Bash(cd C:/Users/Hanna/projects/node/node_chat && node -e \"\nconst WebSocket = require\\('ws'\\);\n\nprocess.env.PORT = 3002;\nrequire\\('./src/index.js'\\);\n\nsetTimeout\\(\\(\\) => {\n const ws = new WebSocket\\('ws://localhost:3002'\\);\n let step = 0;\n ws.on\\('open', \\(\\) => {\n ws.send\\(JSON.stringify\\({ type: 'set_username', username: 'testuser' }\\)\\);\n }\\);\n ws.on\\('message', \\(data\\) => {\n const msg = JSON.parse\\(data\\);\n console.log\\('MSG:', JSON.stringify\\(msg\\)\\);\n if \\(msg.type === 'username_confirmed'\\) {\n // Test: send create_room with empty name \\(simulate the bug\\)\n ws.send\\(JSON.stringify\\({ type: 'create_room', name: '' }\\)\\);\n }\n if \\(msg.type === 'error' || \\(msg.type === 'room_created' && step === 0\\)\\) {\n step++;\n // Now test with actual name\n ws.send\\(JSON.stringify\\({ type: 'create_room', name: 'test' }\\)\\);\n }\n if \\(msg.type === 'room_created' && step === 1\\) {\n setTimeout\\(\\(\\) => process.exit\\(0\\), 100\\);\n }\n }\\);\n}, 200\\);\n\" 2>&1)"
]
}
}
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
extends: '@mate-academy/eslint-config',
env: {
jest: true
jest: true,
},
rules: {
'no-proto': 0
'no-proto': 0,
},
plugins: ['jest']
plugins: ['jest'],
};
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
33 changes: 29 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
},
"author": "Mate academy",
"license": "GPL-3.0",
"dependencies": {
"ws": "^8.18.0"
},
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
266 changes: 266 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,267 @@
'use strict';

const http = require('http');
const path = require('path');
const fs = require('fs');
const { WebSocketServer } = require('ws');

const PORT = process.env.PORT || 3000;
const PUBLIC_DIR = path.join(__dirname, 'public');

// ─── In-memory state ─────────────────────────────────────────────────────────

const rooms = new Map(); // roomId → { id, name, messages[] }
const clients = new Map(); // ws → { username, roomId }

let nextRoomId = 1;

function createRoom(roomName) {
const id = String(nextRoomId++);

rooms.set(id, { id, name: roomName, messages: [] });

return rooms.get(id);
}

// Seed a default room so the app is usable right away
createRoom('general');

// ─── HTTP server (serves static client files) ────────────────────────────────

const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.ico': 'image/x-icon',
};

const server = http.createServer((req, res) => {
const filePath = path.join(
PUBLIC_DIR,
req.url === '/' ? 'index.html' : req.url,
);

const ext = path.extname(filePath);
const contentType = MIME[ext] || 'tsxt/plain';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a small typo in the default MIME type. It should be text/plain.


fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not found');

return;
}

res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
});

// ─── WebSocket helpers ───────────────────────────────────────────────────────

function send(ws, type, payload = {}) {
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify({ type, ...payload }));
}
}

function broadcast(roomId, type, payload, excludeWs = null) {
for (const [ws, client] of clients) {
if (client.roomId === roomId && ws !== excludeWs) {
send(ws, type, payload);
}
}
}

function broadcastRoomList() {
// eslint-disable-next-line
const roomList = [...rooms.values()].map(({ id, name }) => ({
id,
name,
}));

for (const ws of clients.keys()) {
send(ws, 'room_list', { rooms: roomList });
}
}

// ─── Message handlers ────────────────────────────────────────────────────────

const handlers = {
set_username(ws, { username }) {
if (!username || !username.trim()) {
send(ws, 'error', { message: 'User name cannot be empty' });

return;
}

clients.get(ws).username = username.trim();
send(ws, 'username_confirmed', { username: username.trim() });

send(ws, 'room_list', {
rooms: [...rooms.values()].map(({ id, name }) => ({ id, name })),
});
},

create_room(ws, { name }) {
if (!name || !name.trim()) {
send(ws, 'error', { message: 'Room name cannot be empty' });

return;
}

const room = createRoom(name.trim());

broadcastRoomList();

send(ws, 'room_created', { room: { id: room.id, name: room.name } });
},

rename_room(ws, { roomId, name }) {
const room = rooms.get(roomId);

if (!room) {
send(ws, 'error', { message: 'Room not found' });

return;
}

if (!name || !name.trim()) {
send(ws, 'error', { message: 'Room name cannot be empty' });

return;
}

room.name = name.trim();

broadcastRoomList();

broadcast(roomId, 'room_renamed', { roomId, name: room.name });
},

join_room(ws, { roomId }) {
const room = rooms.get(roomId);
const client = clients.get(ws);

if (!room) {
send(ws, 'error', { message: 'Room not found.' });

return;
}

if (!client.username) {
send(ws, 'error', { message: 'Set a username first' });

return;
}

client.roomId = roomId;

send(ws, 'room_joined', {
room: { id: room.id, name: room.name },
messages: room.messages,
});

broadcast(roomId, 'user_joined', { username: client.username }, ws);
},

delete_room(ws, { roomId }) {
if (!rooms.has(roomId)) {
send(ws, 'error', { message: 'Room not found.' });

return;
}

rooms.delete(roomId);

// Move everyone in that room out
for (const [clientWs, client] of clients) {
if (client.roomId === roomId) {
client.roomId = null;
send(clientWs, 'room_deleted', { roomId });
}
}

broadcastRoomList();
},

send_message(ws, { text }) {
const client = clients.get(ws);

if (!client.roomId) {
send(ws, 'error', { message: 'Join a room first.' });

return;
}

if (!text || !text.trim()) {
send(ws, 'error', { message: 'Message cannot be empty.' });

return;
}

const room = rooms.get(client.roomId);

if (!room) {
send(ws, 'error', { message: 'Room not found.' });

return;
}

const message = {
id: Date.now(),
author: client.username,
text: text.trim(),
time: new Date().toISOString(),
};

room.messages.push(message);
broadcast(client.roomId, 'new_message', { message });
},
};

// ─── WebSocket server ────────────────────────────────────────────────────────

const wss = new WebSocketServer({ server });

wss.on('connection', (ws) => {
clients.set(ws, { username: null, roomId: null });

ws.on('message', (raw) => {
let parsed;

try {
parsed = JSON.parse(raw);
} catch {
send(ws, 'error', { message: 'Invalid JSON.' });

return;
}

const { type, ...payload } = parsed;
const handler = handlers[type];

if (handler) {
handler(ws, payload);
} else {
send(ws, 'error', { message: `Unknown message type: ${type}` });
}
});

ws.on('close', () => {
const client = clients.get(ws);

if (client && client.roomId && client.username) {
broadcast(client.roomId, 'user_left', { username: client.username });
}

clients.delete(ws);
});
});

// ─── Start ───────────────────────────────────────────────────────────────────

server.listen(PORT, () => {
// eslint-disable-next-line
console.log(`Chat server running at http://localhost:${PORT}`);
});
5 changes: 5 additions & 0 deletions src/public/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
env: {
browser: true,
},
};
Loading
Loading