-
Notifications
You must be signed in to change notification settings - Fork 45
/
middleware.js
74 lines (60 loc) · 2.18 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
import { NextResponse } from "next/server";
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 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 });
const startTime = new Date().getTime();
const hasFormData = req.headers.get("Content-Type")?.includes("multipart/form-data");
const response = await fetch(req.url, {
method: req.method,
headers: req.headers,
body: hasFormData ? await req.formData() : req.body
});
const time = (new Date().getTime()) - startTime;
// 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 new NextResponse(hasFormData ? await response.formData() : await response.text(), { headers: response.headers, status: response.status });
}
export const config = {
matcher: [
'/(api\/(?!auth).*)'
]
}