timing.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var timing_exports = {};
  19. __export(timing_exports, {
  20. endTime: () => endTime,
  21. setMetric: () => setMetric,
  22. startTime: () => startTime,
  23. timing: () => timing,
  24. wrapTime: () => wrapTime
  25. });
  26. module.exports = __toCommonJS(timing_exports);
  27. var import_context = require("../../context");
  28. const getTime = () => {
  29. try {
  30. return performance.now();
  31. } catch {
  32. }
  33. return Date.now();
  34. };
  35. const timing = (config) => {
  36. const options = {
  37. total: true,
  38. enabled: true,
  39. totalDescription: "Total Response Time",
  40. autoEnd: true,
  41. crossOrigin: false,
  42. ...config
  43. };
  44. return async function timing2(c, next) {
  45. const headers = [];
  46. const timers = /* @__PURE__ */ new Map();
  47. if (c.get("metric")) {
  48. return await next();
  49. }
  50. c.set("metric", { headers, timers });
  51. if (options.total) {
  52. startTime(c, "total", options.totalDescription);
  53. }
  54. await next();
  55. if (options.total) {
  56. endTime(c, "total");
  57. }
  58. if (options.autoEnd) {
  59. timers.forEach((_, key) => endTime(c, key));
  60. }
  61. const enabled = typeof options.enabled === "function" ? options.enabled(c) : options.enabled;
  62. if (enabled) {
  63. c.res.headers.append("Server-Timing", headers.join(","));
  64. const crossOrigin = typeof options.crossOrigin === "function" ? options.crossOrigin(c) : options.crossOrigin;
  65. if (crossOrigin) {
  66. c.res.headers.append(
  67. "Timing-Allow-Origin",
  68. typeof crossOrigin === "string" ? crossOrigin : "*"
  69. );
  70. }
  71. }
  72. };
  73. };
  74. const setMetric = (c, name, valueDescription, description, precision) => {
  75. const metrics = c.get("metric");
  76. if (!metrics) {
  77. console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
  78. return;
  79. }
  80. if (typeof valueDescription === "number") {
  81. const dur = valueDescription.toFixed(precision || 1);
  82. const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
  83. metrics.headers.push(metric);
  84. } else {
  85. const metric = valueDescription ? `${name};desc="${valueDescription}"` : `${name}`;
  86. metrics.headers.push(metric);
  87. }
  88. };
  89. const startTime = (c, name, description) => {
  90. const metrics = c.get("metric");
  91. if (!metrics) {
  92. console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
  93. return;
  94. }
  95. metrics.timers.set(name, { description, start: getTime() });
  96. };
  97. const endTime = (c, name, precision) => {
  98. const metrics = c.get("metric");
  99. if (!metrics) {
  100. console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
  101. return;
  102. }
  103. const timer = metrics.timers.get(name);
  104. if (!timer) {
  105. console.warn(`Timer "${name}" does not exist!`);
  106. return;
  107. }
  108. const { description, start } = timer;
  109. const duration = getTime() - start;
  110. setMetric(c, name, duration, description, precision);
  111. metrics.timers.delete(name);
  112. };
  113. async function wrapTime(c, name, callable, description, precision) {
  114. startTime(c, name, description);
  115. try {
  116. return await callable;
  117. } finally {
  118. endTime(c, name, precision);
  119. }
  120. }
  121. // Annotate the CommonJS export names for ESM import in node:
  122. 0 && (module.exports = {
  123. endTime,
  124. setMetric,
  125. startTime,
  126. timing,
  127. wrapTime
  128. });