Skip to content

Commit

Permalink
add channelsblocked feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 20, 2023
1 parent fe9c288 commit 08bb764
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = class KeepAlive {
}

ping () {
for (let reply of this.replies) {
for (const reply of this.replies) {
reply?.ssePing()
}
this.timer.refresh()
Expand Down
14 changes: 13 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const files = [
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
"x-no-compression": 1
'x-no-compression': 1
})

function close () {
Expand Down Expand Up @@ -140,7 +140,19 @@ const files = [
.send(files[2])
})

const channelsBlocked = new Set(process.env.BLOCKED_CHANNELS ? process.env.BLOCKED_CHANNELS.split(',') : [])

const channelsBlockedHandler = channelsBlocked.size !== 0
? (req, reply, done) => {
if (channelsBlocked.has(req.params.channel)) {
return reply.status(403).send('Channel has been disabled due to too many connections.')
}
done()
}
: undefined

fastify.post('/:channel', {
onRequest: channelsBlockedHandler,
schema: {
body: {
type: 'object'
Expand Down

0 comments on commit 08bb764

Please sign in to comment.