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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ console.log(result)
// }
```

Page `1` returns the most recent messages.
Message history is stored in memory per process, so pagination works for single-process deployments only.

### Notifications

```javascript
Expand Down
19 changes: 17 additions & 2 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const MESSAGE_TYPES = {
}

const roomMessages = new Map()
const MAX_MESSAGES_PER_ROOM = 1000
const MAX_PAGE_SIZE = 100

function sendMessage(roomId, message) {
if (!roomId) throw new Error(
Expand Down Expand Up @@ -36,14 +38,27 @@ function sendMessage(roomId, message) {
if (!roomMessages.has(roomId)) {
roomMessages.set(roomId, [])
}
roomMessages.get(roomId).push(payload)
const messages = roomMessages.get(roomId)
messages.push(payload)
if (messages.length > MAX_MESSAGES_PER_ROOM) {
messages.splice(0, messages.length - MAX_MESSAGES_PER_ROOM)
}
getIO().to(roomId).emit('message:new', payload)
return payload
}

function editMessage(roomId, messageId, newContent) {
const messages = roomMessages.get(roomId) || []
const message = messages.find(msg => msg.id === messageId)
if (message) {
message.content = newContent
message.editedAt = new Date()
} else {
console.warn(
`[quick-socket] editMessage warning: message "${messageId}" was not found in memory for room "${roomId}". ` +
'The socket event was emitted, but the in-memory message history was not updated.'
)
}
if (!message) throw new Error(
`[quick-socket] editMessage() could not find message "${messageId}" in room "${roomId}".`
)
Expand Down Expand Up @@ -72,7 +87,7 @@ function sendTyping(roomId, userId, isTyping) {
function getRoomMessages(roomId, page = 1, limit = 20) {
const messages = roomMessages.get(roomId) || []
const currentPage = Math.max(Number(page) || 1, 1)
const pageSize = Math.max(Number(limit) || 20, 1)
const pageSize = Math.min(Math.max(Number(limit) || 20, 1), MAX_PAGE_SIZE)
const total = messages.length
const totalPages = total === 0 ? 0 : Math.ceil(total / pageSize)
const end = total - (currentPage - 1) * pageSize
Expand Down
32 changes: 29 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ async function runTests() {
const messagesAfterClose = quickSocket.getRoomMessages('room-1', 1, 20)
log('closeRoom clears stored room messages', messagesAfterClose.total === 0)

// ── Validation Tests ──
let validationPassed = true

try {
quickSocket.sendMessage(undefined, {})
validationPassed = false
} catch (err) {
if (!err.message.includes('requires a roomId')) validationPassed = false
}

try {
quickSocket.sendMessage('room-1', { senderId: 'u1', content: '' })
validationPassed = false
} catch (err) {
if (!err.message.includes('cannot be empty')) validationPassed = false
}

try {
quickSocket.createRoom('')
validationPassed = false
} catch (err) {
if (!err.message.includes('non-empty string roomId')) validationPassed = false
}

log('validation throws correct errors for invalid inputs', validationPassed)

// ── Test 14: MESSAGE_TYPES constants ──
log('MESSAGE_TYPES.TEXT is "text"', quickSocket.MESSAGE_TYPES.TEXT === 'text')
log('MESSAGE_TYPES.IMAGE is "image"', quickSocket.MESSAGE_TYPES.IMAGE === 'image')
Expand Down Expand Up @@ -212,7 +238,7 @@ async function runTests() {
const next = (err) => {
log(
'authMiddleware missing token returns error',
err instanceof Error && err.message === 'No token provided'
err instanceof Error && err.message.includes('Authentication failed: no token found')
)
resolve()
}
Expand All @@ -230,7 +256,7 @@ async function runTests() {
const next = (err) => {
log(
'authMiddleware invalid token returns error',
err instanceof Error && err.message === 'Authentication failed'
err instanceof Error && err.message.includes('Authentication failed: the authFn you provided threw an error')
)
resolve()
}
Expand All @@ -246,7 +272,7 @@ async function runTests() {
const next = (err) => {
log(
'authMiddleware missing auth object returns error',
err instanceof Error && err.message === 'No token provided'
err instanceof Error && err.message.includes('Authentication failed: no token found')
)
resolve()
}
Expand Down