-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpayload-limit.ts
36 lines (33 loc) · 1.21 KB
/
payload-limit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as https from "https";
import * as http from "http";
import { MB } from "../_util/size";
/**
* this is to save data transfer cost. And basically we should not use CORS Proxy to load large files.
*
* https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
*/
const MAX_TARGET_RESOURCE_MB = 2; // 6mb is max supported by api gateway. (supports up to 2mb on proxy)
export const payloadlimit = (req, res, next) => {
const requrl = req.originalUrl.substring(1);
const agent = requrl.startsWith("https:") ? https : http;
agent
.request(requrl, { method: "HEAD" }, (_resp) => {
const len = Number(_resp.headers["content-length"]);
if (len && len > MB * MAX_TARGET_RESOURCE_MB) {
// reject if data larger than 30mb.
res.status(413).send({
message: `Requested resource exceeds ${MAX_TARGET_RESOURCE_MB}mb`,
issue: "https://github.com/bridgedxyz/base/issues/23",
});
} else {
// if content-length is undefined or less than 30mb, procceed.
next();
}
})
.on("error", (err) => {
// ignore error for this
// target, which is anonymous, might not support HEAD request.
next();
})
.end();
};