-
Notifications
You must be signed in to change notification settings - Fork 44
/
middleware.js
105 lines (89 loc) · 2.95 KB
/
middleware.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { NextResponse } from "next/server";
import { Buffer } from "node:buffer";
const createTimeoutPromise = (timeout) => new Promise((resolve) => {
setTimeout(resolve, timeout);
});
async function sendMetric(hostName, metricKey) {
fetch(`${hostName}/api/metric`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ metricKey }),
});
}
async function sendTimerMetric(hostName, metricKey, time) {
fetch(`${hostName}/api/metric`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ time, metricKey }),
});
}
export async function middleware(req) {
const url = new URL(decodeURIComponent(req.url));
const cookie = req.headers.get("Cookie");
const HOST_NAME = "http://" + url.host;
let _metricName = url.pathname.slice(1).split("/").join("_");
if (_metricName.includes("users") || _metricName.includes("profiles")) {
// strip away specific user/profile names in the metric key
_metricName = _metricName.split("_").slice(0, -1).join("_");
}
// skip the /api/metric api endpoint
if (req.url.includes("metric")) return NextResponse.json({ success: true });
try {
let response;
let time;
if (req.body) {
const rawBody = await req.body.getReader().read();
const body = JSON.parse(Buffer.from(rawBody?.value).toString("utf8"));
const startTime = new Date().getTime();
response = await fetch(url.href, {
method: req.method,
headers: {
"Content-Type": "application/json",
"Cookie": cookie
},
body: JSON.stringify(body)
});
time = (new Date().getTime()) - startTime;
} else {
const startTime = new Date().getTime();
response = await fetch(url.href, {
headers: {
"Cookie": cookie
}
});
time = (new Date().getTime()) - startTime;
}
const contentType = response.headers.get("content-type")
let data;
if (contentType === "application/json") {
data = await response.json();
} else {
data = await response.arrayBuffer();
}
// attempt to send metric counter
// and timer metric
// ...will timeout after 150ms
//
/* sending metrics is dispatched to /api/metrics because next.js middleware
* is based off edge-runtime which has limited support for node APIs (see: https://nextjs.org/docs/app/api-reference/edge),
* including UDP which node-statsd requires
*/
Promise.any([
createTimeoutPromise(150),
// sendMetric(HOST_NAME, `${response.status}.${_metricName}`),
sendTimerMetric(HOST_NAME, _metricName, time), // send timing metric
])
return contentType === "application/json" ? NextResponse.json(data) : new NextResponse(data, { headers: { ...response.headers } });
} catch (err) {
return NextResponse.json({ msg: "Failed", reason: err });
}
}
export const config = {
matcher: [
'/(api\/(?!auth).*)'
]
}