Skip to content

Commit

Permalink
Optional Rate Limit
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiochimera committed Jul 13, 2024
1 parent f9bdba5 commit a2be115
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion alexa/alexa-adapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
rate_limit: {
value: 60, required: false, validate: function (v) {
value: '', required: false, validate: function (v) {
if (v === undefined || v.trim().length === 0) return true;
const n = parseInt(v);
const f = parseFloat(v);
Expand Down
18 changes: 10 additions & 8 deletions alexa/alexa-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = function (RED) {
node.http_path = node.usehttpnoderoot ? node.Path_join(node.httpNodeRoot, config.http_path || '') : config.http_path || '';
node.http_port = config.port || '';
node.https_server = config.https_server || false;
node.rate_limit = parseInt(config.rate_limit || 60);
node.rate_limit = parseInt(config.rate_limit || '0');
node.http_root = node.Path_join('/', node.http_path.trim());
node.http_server = null;
node.user_dir = RED.settings.userDir || '.';
Expand Down Expand Up @@ -263,13 +263,15 @@ module.exports = function (RED) {
if (node.verbose) node._debug("startServer port " + node.http_port + " rate limit " + node.rate_limit);
const app = express();
app.disable('x-powered-by');
const rateLimitMiddleware = expressRateLimit({
windowMs: 60 * 1000,
max: node.rate_limit,
message: `You have exceeded your ${node.rate_limit} requests per minute limit.`,
headers: true,
});
app.use(rateLimitMiddleware);
if (node.rate_limit > 0) {
const rateLimitMiddleware = expressRateLimit({
windowMs: 60 * 1000,
max: node.rate_limit,
message: `You have exceeded your ${node.rate_limit} requests per minute limit.`,
headers: true,
});
app.use(rateLimitMiddleware);
}
app.use(helmet());
app.use(cors());
app.use(morgan('dev'));
Expand Down

0 comments on commit a2be115

Please sign in to comment.