Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #100 from nervosnetwork/fix-array
Browse files Browse the repository at this point in the history
fix: add error code, add missing req.body, fix when array methods
  • Loading branch information
RetricSu authored Dec 3, 2021
2 parents a01d73a + 02e8f4a commit 1e9810d
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/api-server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { wrapper } = require("./lib/ws/methods");
const expressWs = require("express-ws");
const Sentry = require("@sentry/node");
const { AccessGuard } = require("../api-server/lib/cache/guard");
const { LIMIT_EXCEEDED } = require("../api-server/lib/methods/error-code");

NEW_RELIC_LICENSE_KEY = process.env.NEW_RELIC_LICENSE_KEY;

Expand Down Expand Up @@ -108,17 +109,10 @@ accessGuard.setMaxRpm(
MAX_RPM.poly_executeRawL2Transaction
);

app.use(async function (req, _res, next) {
app.use(async function (req, res, next) {
const ip = getIp(req);
console.debug("#for debug:", {
ip: ip,
body: req.body,
method: req.body.method,
has: hasMethod("poly_executeRawL2Transaction"),
});

// restrict access rate limit
if (hasMethod("poly_executeRawL2Transaction") && ip != null) {
if (hasMethod(req.body, "poly_executeRawL2Transaction") && ip != null) {
const rpcRouter = "poly_executeRawL2Transaction";
const reqId = ip;
const isExist = await accessGuard.isExist(rpcRouter, reqId);
Expand All @@ -127,10 +121,29 @@ app.use(async function (req, _res, next) {
}
const isOverRate = await accessGuard.isOverRate(rpcRouter, reqId);
if (isOverRate) {
return _res.send({
if (Array.isArray(req.body)) {
return res.send(
req.body.map((b) => {
return {
jsonrpc: "2.0",
id: b.id,
error: {
code: LIMIT_EXCEEDED,
message:
"you are temporally restrict to the service, please wait.",
},
};
})
);
}

return res.send({
jsonrpc: "2.0",
id: req.body.id,
error: "you are temporally restrict to the service, please wait.",
error: {
code: LIMIT_EXCEEDED,
message: "you are temporally restrict to the service, please wait.",
},
});
}

Expand Down

0 comments on commit 1e9810d

Please sign in to comment.