项目作者: yatki

项目描述 :
Flood protection for realtime applications
高级语言: JavaScript
项目地址: git://github.com/yatki/flood-protection.git
创建时间: 2017-10-25T20:36:31Z
项目社区:https://github.com/yatki/flood-protection

开源协议:MIT License

下载


flood-protection

Flood protection for realtime applications

NPM version
Build Status
Coverage Status

Why?

Purpose of this library is to have control on receiving data packages.
It allows you to drop data packages (e.g. messages) if they arrive too quickly.
As an example you may want to use it to prevent spam messages in chat rooms or to limit number of requests to your http/express server.

Notes:

Install

  1. npm install --save flood-protection

Usage

  1. import FloodProtection from 'flood-protection';
  2. const floodProtection = new FloodProtection({
  3. rate: 5,
  4. // default: 5, unit: messages
  5. // IMPORTANT: rate must be >= 1 (greater than or equal to 1)
  6. per: 8,
  7. // default: 8, unit: seconds
  8. });

Basic Example

  1. import FloodProtection from 'flood-protection';
  2. // ...
  3. // io is a Socket.io instance...
  4. io.on('connection', (client) => {
  5. client.emit('connected');
  6. const floodProtection = new FloodProtection();
  7. client.on('message', (text) => {
  8. if (floodProtection.check()) {
  9. // forward message
  10. io.emit('message', text);
  11. } else {
  12. // forbid message
  13. client.emit('flood', {
  14. text: 'Take it easy!',
  15. });
  16. }
  17. });
  18. });

Contribution

As always, I’m open to any contribution and would like to hear your feedback.

Just an important reminder:

If you are planning to contribute to any open source project,
before starting development, please always open an issue and make a proposal first.
This will save you from working on features that are eventually going to be rejected for some reason.

LICENCE

MIT (c) 2017 Mehmet Yatkı