forked from bigearth/rest.bitbox.earth
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from Bitcoin-com/stage
v2.2.6
- Loading branch information
Showing
28 changed files
with
168 additions
and
546 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var RateLimit = require("express-rate-limit"); | ||
// Set max requests per minute | ||
var maxRequests = process.env.RATE_LIMIT_MAX_REQUESTS ? parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) : 60; | ||
// Unique route mapped to its rate limit | ||
var uniqueRateLimits = {}; | ||
var routeRateLimit = function (req, res, next) { | ||
// Disable rate limiting if 0 passed from RATE_LIMIT_MAX_REQUESTS | ||
if (maxRequests === 0) | ||
return next(); | ||
// TODO: Auth: Set or disable rate limit if authenticated user | ||
// Current route | ||
var path = req.baseUrl + req.path; | ||
var route = req.method + path.split("/").slice(0, 4).join("/"); | ||
// Create new RateLimit if none exists for this route | ||
if (!uniqueRateLimits[route]) { | ||
uniqueRateLimits[route] = new RateLimit({ | ||
windowMs: 60 * 1000, | ||
delayMs: 0, | ||
max: maxRequests, | ||
handler: function (req, res /*next*/) { | ||
res.format({ | ||
json: function () { | ||
res.status(500).json({ | ||
error: "Too many requests. Limits are 60 requests per minute." | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
// Call rate limit for this route | ||
uniqueRateLimits[route](req, res, next); | ||
}; | ||
exports.routeRateLimit = routeRateLimit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "rest.bitcoin.com", | ||
"version": "2.2.5", | ||
"version": "2.2.6", | ||
"description": "REST API for Bitcoin.com's Cloud", | ||
"author": "Gabriel Cardona <[email protected]>", | ||
"contributors": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as express from "express" | ||
const RateLimit = require("express-rate-limit") | ||
|
||
// Set max requests per minute | ||
const maxRequests = process.env.RATE_LIMIT_MAX_REQUESTS ? parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) : 60 | ||
|
||
// Unique route mapped to its rate limit | ||
const uniqueRateLimits: any = {} | ||
|
||
const routeRateLimit = function( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction | ||
) { | ||
// Disable rate limiting if 0 passed from RATE_LIMIT_MAX_REQUESTS | ||
if (maxRequests === 0) return next() | ||
|
||
// TODO: Auth: Set or disable rate limit if authenticated user | ||
|
||
// Current route | ||
const path = req.baseUrl + req.path | ||
const route = req.method + path.split("/").slice(0,4).join("/") | ||
|
||
// Create new RateLimit if none exists for this route | ||
if (!uniqueRateLimits[route]) { | ||
uniqueRateLimits[route] = new RateLimit({ | ||
windowMs: 60 * 1000, // 1 minute window | ||
delayMs: 0, // disable delaying - full speed until the max limit is reached | ||
max: maxRequests, // start blocking after maxRequests | ||
handler: function(req: express.Request, res: express.Response /*next*/) { | ||
res.format({ | ||
json: function() { | ||
res.status(500).json({ | ||
error: "Too many requests. Limits are 60 requests per minute." | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
// Call rate limit for this route | ||
uniqueRateLimits[route](req, res, next) | ||
} | ||
|
||
export { | ||
routeRateLimit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.