Erster Docker-Stand
This commit is contained in:
102
_node_modules/hono/dist/middleware/timing/timing.js
generated
vendored
Normal file
102
_node_modules/hono/dist/middleware/timing/timing.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
// src/middleware/timing/timing.ts
|
||||
import "../../context.js";
|
||||
var getTime = () => {
|
||||
try {
|
||||
return performance.now();
|
||||
} catch {
|
||||
}
|
||||
return Date.now();
|
||||
};
|
||||
var timing = (config) => {
|
||||
const options = {
|
||||
total: true,
|
||||
enabled: true,
|
||||
totalDescription: "Total Response Time",
|
||||
autoEnd: true,
|
||||
crossOrigin: false,
|
||||
...config
|
||||
};
|
||||
return async function timing2(c, next) {
|
||||
const headers = [];
|
||||
const timers = /* @__PURE__ */ new Map();
|
||||
if (c.get("metric")) {
|
||||
return await next();
|
||||
}
|
||||
c.set("metric", { headers, timers });
|
||||
if (options.total) {
|
||||
startTime(c, "total", options.totalDescription);
|
||||
}
|
||||
await next();
|
||||
if (options.total) {
|
||||
endTime(c, "total");
|
||||
}
|
||||
if (options.autoEnd) {
|
||||
timers.forEach((_, key) => endTime(c, key));
|
||||
}
|
||||
const enabled = typeof options.enabled === "function" ? options.enabled(c) : options.enabled;
|
||||
if (enabled) {
|
||||
c.res.headers.append("Server-Timing", headers.join(","));
|
||||
const crossOrigin = typeof options.crossOrigin === "function" ? options.crossOrigin(c) : options.crossOrigin;
|
||||
if (crossOrigin) {
|
||||
c.res.headers.append(
|
||||
"Timing-Allow-Origin",
|
||||
typeof crossOrigin === "string" ? crossOrigin : "*"
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
var setMetric = (c, name, valueDescription, description, precision) => {
|
||||
const metrics = c.get("metric");
|
||||
if (!metrics) {
|
||||
console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
|
||||
return;
|
||||
}
|
||||
if (typeof valueDescription === "number") {
|
||||
const dur = valueDescription.toFixed(precision || 1);
|
||||
const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
|
||||
metrics.headers.push(metric);
|
||||
} else {
|
||||
const metric = valueDescription ? `${name};desc="${valueDescription}"` : `${name}`;
|
||||
metrics.headers.push(metric);
|
||||
}
|
||||
};
|
||||
var startTime = (c, name, description) => {
|
||||
const metrics = c.get("metric");
|
||||
if (!metrics) {
|
||||
console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
|
||||
return;
|
||||
}
|
||||
metrics.timers.set(name, { description, start: getTime() });
|
||||
};
|
||||
var endTime = (c, name, precision) => {
|
||||
const metrics = c.get("metric");
|
||||
if (!metrics) {
|
||||
console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
|
||||
return;
|
||||
}
|
||||
const timer = metrics.timers.get(name);
|
||||
if (!timer) {
|
||||
console.warn(`Timer "${name}" does not exist!`);
|
||||
return;
|
||||
}
|
||||
const { description, start } = timer;
|
||||
const duration = getTime() - start;
|
||||
setMetric(c, name, duration, description, precision);
|
||||
metrics.timers.delete(name);
|
||||
};
|
||||
async function wrapTime(c, name, callable, description, precision) {
|
||||
startTime(c, name, description);
|
||||
try {
|
||||
return await callable;
|
||||
} finally {
|
||||
endTime(c, name, precision);
|
||||
}
|
||||
}
|
||||
export {
|
||||
endTime,
|
||||
setMetric,
|
||||
startTime,
|
||||
timing,
|
||||
wrapTime
|
||||
};
|
||||
Reference in New Issue
Block a user