acrylblends 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.js +83 -7
  3. package/dist/index.js.map +1 -1
  4. package/lib/audit-BXnYsLge.d.ts +58 -0
  5. package/lib/cell-ChU41DZD.d.ts +32 -0
  6. package/lib/client-modules-CLvWOjbd.js +267 -0
  7. package/lib/connection-ZLEso7XL.d.ts +25 -0
  8. package/lib/conninfo.d.ts +1 -23
  9. package/lib/cordis/db-sqlite.d.ts +56 -0
  10. package/lib/cordis/db-sqlite.js +58 -0
  11. package/lib/cordis/db.d.ts +8 -0
  12. package/lib/cordis/db.js +1 -0
  13. package/lib/cordis/door.d.ts +159 -0
  14. package/lib/cordis/door.js +757 -0
  15. package/lib/cordis/inspect.d.ts +77 -0
  16. package/lib/cordis/inspect.js +149 -0
  17. package/lib/cordis/kv-db.d.ts +16 -0
  18. package/lib/cordis/kv-db.js +18 -0
  19. package/lib/cordis/kv-json.d.ts +16 -0
  20. package/lib/cordis/kv-json.js +44 -0
  21. package/lib/cordis/kv.d.ts +8 -0
  22. package/lib/cordis/kv.js +1 -0
  23. package/lib/cordis/node-server.d.ts +45 -0
  24. package/lib/cordis/node-server.js +93 -0
  25. package/lib/cordis/router-http.d.ts +56 -0
  26. package/lib/cordis/router-http.js +117 -0
  27. package/lib/cordis/router.d.ts +8 -0
  28. package/lib/cordis/router.js +7 -0
  29. package/lib/cordis/state/react.d.ts +6 -0
  30. package/lib/cordis/state/react.js +11 -0
  31. package/lib/cordis/state.d.ts +33 -0
  32. package/lib/cordis/state.js +163 -0
  33. package/lib/core-CaY8XDZ6.js +19 -0
  34. package/lib/database-DBHdwefi.d.ts +44 -0
  35. package/lib/db/conformance.d.ts +11 -0
  36. package/lib/db/conformance.js +267 -0
  37. package/lib/db/sqlite.d.ts +1 -1
  38. package/lib/db/sqlite.js +1 -196
  39. package/lib/db.d.ts +2 -1
  40. package/lib/{src-0hI7M7cN.js → deprecated-BP4fPt38.js} +0 -17
  41. package/lib/door/host.d.ts +24 -0
  42. package/lib/door/host.js +2 -0
  43. package/lib/door/page.d.ts +105 -0
  44. package/lib/door/page.js +1030 -0
  45. package/lib/door/protocol.d.ts +2 -0
  46. package/lib/door/protocol.js +197 -0
  47. package/lib/error-t4N2js2I.d.ts +8 -0
  48. package/lib/etag-DaKMOjty.js +135 -0
  49. package/lib/etag.js +1 -134
  50. package/lib/helpers-CvTauQT5.js +5 -0
  51. package/lib/http-exception-kpGJkmpi.d.ts +57 -0
  52. package/lib/http-exception.d.ts +1 -56
  53. package/lib/{index-BLTYsyc1.d.ts → index-BnGgRKTv.d.ts} +3 -39
  54. package/lib/index-BqVQkTnu.d.ts +98 -0
  55. package/lib/index-Dmst2QCQ.d.ts +44 -0
  56. package/lib/index.d.ts +1 -1
  57. package/lib/index.js +2 -1
  58. package/lib/jwk-LkzeoaU8.js +117 -0
  59. package/lib/jwk.js +1 -116
  60. package/lib/jwt-CskdhEW9.js +115 -0
  61. package/lib/jwt.js +2 -114
  62. package/lib/kv/conformance.d.ts +13 -0
  63. package/lib/kv/conformance.js +301 -0
  64. package/lib/kv/db.d.ts +2 -2
  65. package/lib/kv/json.d.ts +1 -1
  66. package/lib/kv-9f-AlRKN.d.ts +39 -0
  67. package/lib/kv.d.ts +2 -1
  68. package/lib/language-D9a4zqvn.js +198 -0
  69. package/lib/language.js +1 -197
  70. package/lib/middleware-CPQ8himN.js +12 -0
  71. package/lib/node-server/serve-static.d.ts +0 -1
  72. package/lib/node-server/serve-static.js +1 -1
  73. package/lib/node-server.d.ts +1 -7
  74. package/lib/node-server.js +3 -1586
  75. package/lib/open-CMhnJODn.js +197 -0
  76. package/lib/protocol-BizVt4J6.d.ts +144 -0
  77. package/lib/request-id-CKVvHOO1.js +34 -0
  78. package/lib/request-id.js +1 -33
  79. package/lib/secure-headers-mP3j6Hm6.js +163 -0
  80. package/lib/secure-headers.js +1 -162
  81. package/lib/{serve-static-B849HlX2.js → serve-static-3uo_9rjF.js} +1 -4
  82. package/lib/server-DvgMGtzo.js +1323 -0
  83. package/lib/snapshot-text-D3NspDyT.js +338 -0
  84. package/lib/src-CwABFr6r.js +328 -0
  85. package/lib/timeout.d.ts +1 -1
  86. package/lib/timing-Cky7C1Jf.js +183 -0
  87. package/lib/timing.js +1 -182
  88. package/package.json +89 -1
  89. package/plugins/acryl-db/plugin.json +6 -1
  90. package/plugins/acryl-db-sqlite/plugin.json +4 -0
  91. package/plugins/acryl-db-sqlite/src/facade/node.ts +1 -1
  92. package/plugins/acryl-db-sqlite/src/index.ts +2 -2
  93. package/plugins/acryl-db-sqlite/src/open.ts +2 -2
  94. package/plugins/acryl-door/plugin.json +4 -1
  95. package/plugins/acryl-door/src/host/http.ts +2 -2
  96. package/plugins/acryl-door/src/host/mcp-routes.ts +1 -1
  97. package/plugins/acryl-door/src/host/operator-routes.ts +1 -1
  98. package/plugins/acryl-door/src/host/page-routes.ts +3 -3
  99. package/plugins/acryl-door/src/index.ts +2 -2
  100. package/plugins/acryl-inspect/plugin.json +4 -1
  101. package/plugins/acryl-inspect/src/service.ts +2 -2
  102. package/plugins/acryl-kv/plugin.json +6 -1
  103. package/plugins/acryl-kv-db/plugin.json +4 -0
  104. package/plugins/acryl-kv-db/src/index.ts +1 -1
  105. package/plugins/acryl-kv-db/src/open.ts +3 -3
  106. package/plugins/acryl-kv-json/plugin.json +4 -0
  107. package/plugins/acryl-kv-json/src/index.ts +1 -1
  108. package/plugins/acryl-kv-json/src/open.ts +2 -2
  109. package/plugins/acryl-node-server/plugin.json +5 -1
  110. package/plugins/acryl-node-server/src/adapter/conninfo.ts +1 -1
  111. package/plugins/acryl-node-server/src/adapter/early-hints.ts +1 -1
  112. package/plugins/acryl-node-server/src/adapter/serve-static.ts +2 -2
  113. package/plugins/acryl-node-server/src/adapter/websocket.ts +2 -2
  114. package/plugins/acryl-node-server/src/client-modules.ts +2 -2
  115. package/plugins/acryl-node-server/src/index.ts +1 -1
  116. package/plugins/acryl-router/plugin.json +11 -1
  117. package/plugins/acryl-router/src/connection.d.ts +25 -0
  118. package/plugins/acryl-router/src/core/app-base.d.ts +215 -0
  119. package/plugins/acryl-router/src/core/app.d.ts +19 -0
  120. package/plugins/acryl-router/src/core/client/client.d.ts +4 -0
  121. package/plugins/acryl-router/src/core/client/fetch-result-please.d.ts +35 -0
  122. package/plugins/acryl-router/src/core/client/index.d.ts +7 -0
  123. package/plugins/acryl-router/src/core/client/types.d.ts +229 -0
  124. package/plugins/acryl-router/src/core/client/utils.d.ts +18 -0
  125. package/plugins/acryl-router/src/core/compose.d.ts +14 -0
  126. package/plugins/acryl-router/src/core/context.d.ts +443 -0
  127. package/plugins/acryl-router/src/core/helper/accepts/accepts.d.ts +30 -0
  128. package/plugins/acryl-router/src/core/helper/accepts/index.d.ts +5 -0
  129. package/plugins/acryl-router/src/core/helper/conninfo/index.d.ts +5 -0
  130. package/plugins/acryl-router/src/core/helper/conninfo/types.d.ts +36 -0
  131. package/plugins/acryl-router/src/core/helper/cookie/index.d.ts +25 -0
  132. package/plugins/acryl-router/src/core/helper/css/common.d.ts +59 -0
  133. package/plugins/acryl-router/src/core/helper/css/index.d.ts +80 -0
  134. package/plugins/acryl-router/src/core/helper/dev/index.d.ts +20 -0
  135. package/plugins/acryl-router/src/core/helper/factory/index.d.ts +82 -0
  136. package/plugins/acryl-router/src/core/helper/html/index.d.ts +8 -0
  137. package/plugins/acryl-router/src/core/helper/proxy/index.d.ts +73 -0
  138. package/plugins/acryl-router/src/core/helper/route/index.d.ts +75 -0
  139. package/plugins/acryl-router/src/core/helper/ssg/index.d.ts +7 -0
  140. package/plugins/acryl-router/src/core/helper/ssg/middleware.d.ts +44 -0
  141. package/plugins/acryl-router/src/core/helper/ssg/plugins.d.ts +27 -0
  142. package/plugins/acryl-router/src/core/helper/ssg/ssg.d.ts +91 -0
  143. package/plugins/acryl-router/src/core/helper/ssg/utils.d.ts +16 -0
  144. package/plugins/acryl-router/src/core/helper/streaming/index.d.ts +8 -0
  145. package/plugins/acryl-router/src/core/helper/streaming/sse.d.ts +13 -0
  146. package/plugins/acryl-router/src/core/helper/streaming/stream.d.ts +3 -0
  147. package/plugins/acryl-router/src/core/helper/streaming/text.d.ts +3 -0
  148. package/plugins/acryl-router/src/core/helper/streaming/utils.d.ts +1 -0
  149. package/plugins/acryl-router/src/core/helper/testing/index.d.ts +13 -0
  150. package/plugins/acryl-router/src/core/helper/websocket/index.d.ts +70 -0
  151. package/plugins/acryl-router/src/core/http-exception.d.ts +58 -0
  152. package/plugins/acryl-router/src/core/index.d.ts +37 -0
  153. package/plugins/acryl-router/src/core/jsx/base.d.ts +61 -0
  154. package/plugins/acryl-router/src/core/jsx/children.d.ts +9 -0
  155. package/plugins/acryl-router/src/core/jsx/components.d.ts +15 -0
  156. package/plugins/acryl-router/src/core/jsx/constants.d.ts +6 -0
  157. package/plugins/acryl-router/src/core/jsx/context.d.ts +51 -0
  158. package/plugins/acryl-router/src/core/jsx/dom/components.d.ts +10 -0
  159. package/plugins/acryl-router/src/core/jsx/dom/context.d.ts +3 -0
  160. package/plugins/acryl-router/src/core/jsx/dom/css.d.ts +83 -0
  161. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.d.ts +39 -0
  162. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.d.ts +16 -0
  163. package/plugins/acryl-router/src/core/jsx/dom/jsx-dev-runtime.d.ts +8 -0
  164. package/plugins/acryl-router/src/core/jsx/dom/jsx-runtime.d.ts +7 -0
  165. package/plugins/acryl-router/src/core/jsx/dom/render.d.ts +73 -0
  166. package/plugins/acryl-router/src/core/jsx/dom/utils.d.ts +1 -0
  167. package/plugins/acryl-router/src/core/jsx/hooks/index.d.ts +47 -0
  168. package/plugins/acryl-router/src/core/jsx/index.d.ts +76 -0
  169. package/plugins/acryl-router/src/core/jsx/intrinsic-element/common.d.ts +6 -0
  170. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.d.ts +13 -0
  171. package/plugins/acryl-router/src/core/jsx/intrinsic-elements.d.ts +735 -0
  172. package/plugins/acryl-router/src/core/jsx/streaming.d.ts +36 -0
  173. package/plugins/acryl-router/src/core/jsx/types.d.ts +36 -0
  174. package/plugins/acryl-router/src/core/jsx/utils.d.ts +4 -0
  175. package/plugins/acryl-router/src/core/request/constants.d.ts +1 -0
  176. package/plugins/acryl-router/src/core/request.d.ts +306 -0
  177. package/plugins/acryl-router/src/core/router/reg-exp-router/index.d.ts +6 -0
  178. package/plugins/acryl-router/src/core/router/reg-exp-router/matcher.d.ts +7 -0
  179. package/plugins/acryl-router/src/core/router/reg-exp-router/node.d.ts +13 -0
  180. package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.d.ts +17 -0
  181. package/plugins/acryl-router/src/core/router/reg-exp-router/router.d.ts +11 -0
  182. package/plugins/acryl-router/src/core/router/reg-exp-router/trie.d.ts +8 -0
  183. package/plugins/acryl-router/src/core/router/smart-router/index.d.ts +5 -0
  184. package/plugins/acryl-router/src/core/router/smart-router/router.d.ts +11 -0
  185. package/plugins/acryl-router/src/core/router/trie-router/index.d.ts +5 -0
  186. package/plugins/acryl-router/src/core/router/trie-router/node.d.ts +6 -0
  187. package/plugins/acryl-router/src/core/router/trie-router/router.d.ts +7 -0
  188. package/plugins/acryl-router/src/core/router/utils.d.ts +1 -0
  189. package/plugins/acryl-router/src/core/router.d.ts +97 -0
  190. package/plugins/acryl-router/src/core/types.d.ts +573 -0
  191. package/plugins/acryl-router/src/core/utils/accept.d.ts +6 -0
  192. package/plugins/acryl-router/src/core/utils/body.d.ts +79 -0
  193. package/plugins/acryl-router/src/core/utils/buffer.d.ts +17 -0
  194. package/plugins/acryl-router/src/core/utils/color.d.ts +21 -0
  195. package/plugins/acryl-router/src/core/utils/concurrent.d.ts +11 -0
  196. package/plugins/acryl-router/src/core/utils/constants.d.ts +4 -0
  197. package/plugins/acryl-router/src/core/utils/cookie.d.ts +41 -0
  198. package/plugins/acryl-router/src/core/utils/crypto.d.ts +15 -0
  199. package/plugins/acryl-router/src/core/utils/handler.d.ts +6 -0
  200. package/plugins/acryl-router/src/core/utils/headers.d.ts +8 -0
  201. package/plugins/acryl-router/src/core/utils/html.d.ts +44 -0
  202. package/plugins/acryl-router/src/core/utils/http-status.d.ts +32 -0
  203. package/plugins/acryl-router/src/core/utils/mime.d.ts +70 -0
  204. package/plugins/acryl-router/src/core/utils/stream.d.ts +31 -0
  205. package/plugins/acryl-router/src/core/utils/types.d.ts +128 -0
  206. package/plugins/acryl-router/src/core/utils/url.d.ts +44 -0
  207. package/plugins/acryl-router/src/core/validator/index.d.ts +7 -0
  208. package/plugins/acryl-router/src/core/validator/utils.d.ts +29 -0
  209. package/plugins/acryl-router/src/core/validator/validator.d.ts +16 -0
  210. package/plugins/acryl-router/src/helpers/index.d.ts +17 -0
  211. package/plugins/acryl-router/src/helpers/schema-validator.d.ts +54 -0
  212. package/plugins/acryl-router-http/plugin.json +4 -1
  213. package/plugins/acryl-router-http/src/index.ts +1 -1
  214. package/plugins/acryl-state/plugin.json +5 -1
  215. package/plugins/acryl-state/src/service.ts +3 -3
  216. package/plugins/tsconfig.json +32 -20
  217. package/docs/framework/UPSTREAM.md +0 -3
  218. package/docs/framework/upstream.json +0 -9
  219. package/lib/index-D4FAsBZu.d.ts +0 -85
  220. package/plugins/acryl-db/upstream.json +0 -18
  221. package/plugins/acryl-db-sqlite/upstream.json +0 -21
  222. package/plugins/acryl-door/upstream.json +0 -28
  223. package/plugins/acryl-inspect/upstream.json +0 -18
  224. package/plugins/acryl-kv/upstream.json +0 -18
  225. package/plugins/acryl-kv-db/upstream.json +0 -21
  226. package/plugins/acryl-kv-json/upstream.json +0 -22
  227. package/plugins/acryl-node-server/upstream.json +0 -21
  228. package/plugins/acryl-router/upstream.json +0 -20
  229. package/plugins/acryl-router-http/upstream.json +0 -22
  230. package/plugins/acryl-state/NOTICE +0 -30
  231. package/plugins/acryl-state/upstream.json +0 -22
  232. package/plugins/desktop/upstream.json +0 -12
  233. package/plugins/desktop-web/upstream.json +0 -12
  234. package/templates/todo/upstream.json +0 -12
  235. /package/lib/{index-CTy4N7ng.d.ts → deprecated-CTy4N7ng.d.ts} +0 -0
@@ -0,0 +1,183 @@
1
+ //#region ../../plugins/acryl-router/src/core/middleware/timing/timing.ts
2
+ const getTime = () => {
3
+ try {
4
+ return performance.now();
5
+ } catch {}
6
+ return Date.now();
7
+ };
8
+ /**
9
+ * Server-Timing Middleware for Acryl.
10
+ *
11
+ *
12
+ * @param {TimingOptions} [config] - The options for the timing middleware.
13
+ * @param {boolean} [config.total=true] - Show the total response time.
14
+ * @param {boolean | ((c: Context) => boolean)} [config.enabled=true] - Whether timings should be added to the headers or not.
15
+ * @param {string} [config.totalDescription=Total Response Time] - Description for the total response time.
16
+ * @param {boolean} [config.autoEnd=true] - If `startTime()` should end automatically at the end of the request.
17
+ * @param {boolean | string | ((c: Context) => boolean | string)} [config.crossOrigin=false] - The origin this timings header should be readable.
18
+ * @returns {MiddlewareHandler} The middleware handler function.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const app = new Acryl()
23
+ *
24
+ * // add the middleware to your router
25
+ * app.use(timing());
26
+ *
27
+ * app.get('/', async (c) => {
28
+ * // add custom metrics
29
+ * setMetric(c, 'region', 'europe-west3')
30
+ *
31
+ * // add custom metrics with timing, must be in milliseconds
32
+ * setMetric(c, 'custom', 23.8, 'My custom Metric')
33
+ *
34
+ * // start a new timer
35
+ * startTime(c, 'db');
36
+ *
37
+ * const data = await db.findMany(...);
38
+ *
39
+ * // end the timer
40
+ * endTime(c, 'db');
41
+ *
42
+ * return c.json({ response: data });
43
+ * });
44
+ * ```
45
+ */
46
+ const timing = (config) => {
47
+ const options = {
48
+ total: true,
49
+ enabled: true,
50
+ totalDescription: "Total Response Time",
51
+ autoEnd: true,
52
+ crossOrigin: false,
53
+ ...config
54
+ };
55
+ return async function timing(c, next) {
56
+ const headers = [];
57
+ const timers = /* @__PURE__ */ new Map();
58
+ if (c.get("metric")) return await next();
59
+ c.set("metric", {
60
+ headers,
61
+ timers
62
+ });
63
+ if (options.total) startTime(c, "total", options.totalDescription);
64
+ await next();
65
+ if (options.total) endTime(c, "total");
66
+ if (options.autoEnd) timers.forEach((_, key) => {
67
+ endTime(c, key);
68
+ });
69
+ if (typeof options.enabled === "function" ? options.enabled(c) : options.enabled) {
70
+ c.res.headers.append("Server-Timing", headers.join(","));
71
+ const crossOrigin = typeof options.crossOrigin === "function" ? options.crossOrigin(c) : options.crossOrigin;
72
+ if (crossOrigin) c.res.headers.append("Timing-Allow-Origin", typeof crossOrigin === "string" ? crossOrigin : "*");
73
+ }
74
+ };
75
+ };
76
+ /**
77
+ * Set a metric for the timing middleware.
78
+ *
79
+ * @param {Context} c - The context of the request.
80
+ * @param {string} name - The name of the metric.
81
+ * @param {number | string} [valueDescription] - The value or description of the metric.
82
+ * @param {string} [description] - The description of the metric.
83
+ * @param {number} [precision] - The precision of the metric value.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * setMetric(c, 'region', 'europe-west3')
88
+ * setMetric(c, 'custom', 23.8, 'My custom Metric')
89
+ * ```
90
+ */
91
+ const setMetric = (c, name, valueDescription, description, precision) => {
92
+ const metrics = c.get("metric");
93
+ if (!metrics) {
94
+ console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
95
+ return;
96
+ }
97
+ if (typeof valueDescription === "number") {
98
+ const dur = valueDescription.toFixed(precision || 1);
99
+ const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
100
+ metrics.headers.push(metric);
101
+ } else {
102
+ const metric = valueDescription ? `${name};desc="${valueDescription}"` : `${name}`;
103
+ metrics.headers.push(metric);
104
+ }
105
+ };
106
+ /**
107
+ * Start a timer for the timing middleware.
108
+ *
109
+ * @param {Context} c - The context of the request.
110
+ * @param {string} name - The name of the timer.
111
+ * @param {string} [description] - The description of the timer.
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * startTime(c, 'db')
116
+ * ```
117
+ */
118
+ const startTime = (c, name, description) => {
119
+ const metrics = c.get("metric");
120
+ if (!metrics) {
121
+ console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
122
+ return;
123
+ }
124
+ metrics.timers.set(name, {
125
+ description,
126
+ start: getTime()
127
+ });
128
+ };
129
+ /**
130
+ * End a timer for the timing middleware.
131
+ *
132
+ * @param {Context} c - The context of the request.
133
+ * @param {string} name - The name of the timer.
134
+ * @param {number} [precision] - The precision of the timer value.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * endTime(c, 'db')
139
+ * ```
140
+ */
141
+ const endTime = (c, name, precision) => {
142
+ const metrics = c.get("metric");
143
+ if (!metrics) {
144
+ console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
145
+ return;
146
+ }
147
+ const timer = metrics.timers.get(name);
148
+ if (!timer) {
149
+ console.warn(`Timer "${name}" does not exist!`);
150
+ return;
151
+ }
152
+ const { description, start } = timer;
153
+ const duration = getTime() - start;
154
+ setMetric(c, name, duration, description, precision);
155
+ metrics.timers.delete(name);
156
+ };
157
+ /**
158
+ * Wrap a Promise to capture its duration.
159
+ * @param {Context} c - The context of the request.
160
+ * @param {string} name - The name of the timer.
161
+ * @param {Promise<T>} callable - The Promise to time.
162
+ * @param {string} [description] - The description of the timer.
163
+ * @param {number} [precision] - The precision of the timer value.
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * // Instead of this:
168
+ * const data = await db.findMany(...);
169
+ *
170
+ * // do this:
171
+ * const data = await wrapTime(c, 'query', db.findMany(...));
172
+ * ```
173
+ * */
174
+ async function wrapTime(c, name, callable, description, precision) {
175
+ startTime(c, name, description);
176
+ try {
177
+ return await callable;
178
+ } finally {
179
+ endTime(c, name, precision);
180
+ }
181
+ }
182
+ //#endregion
183
+ export { wrapTime as a, timing as i, setMetric as n, startTime as r, endTime as t };
package/lib/timing.js CHANGED
@@ -1,183 +1,2 @@
1
- //#region ../../plugins/acryl-router/src/core/middleware/timing/timing.ts
2
- const getTime = () => {
3
- try {
4
- return performance.now();
5
- } catch {}
6
- return Date.now();
7
- };
8
- /**
9
- * Server-Timing Middleware for Acryl.
10
- *
11
- *
12
- * @param {TimingOptions} [config] - The options for the timing middleware.
13
- * @param {boolean} [config.total=true] - Show the total response time.
14
- * @param {boolean | ((c: Context) => boolean)} [config.enabled=true] - Whether timings should be added to the headers or not.
15
- * @param {string} [config.totalDescription=Total Response Time] - Description for the total response time.
16
- * @param {boolean} [config.autoEnd=true] - If `startTime()` should end automatically at the end of the request.
17
- * @param {boolean | string | ((c: Context) => boolean | string)} [config.crossOrigin=false] - The origin this timings header should be readable.
18
- * @returns {MiddlewareHandler} The middleware handler function.
19
- *
20
- * @example
21
- * ```ts
22
- * const app = new Acryl()
23
- *
24
- * // add the middleware to your router
25
- * app.use(timing());
26
- *
27
- * app.get('/', async (c) => {
28
- * // add custom metrics
29
- * setMetric(c, 'region', 'europe-west3')
30
- *
31
- * // add custom metrics with timing, must be in milliseconds
32
- * setMetric(c, 'custom', 23.8, 'My custom Metric')
33
- *
34
- * // start a new timer
35
- * startTime(c, 'db');
36
- *
37
- * const data = await db.findMany(...);
38
- *
39
- * // end the timer
40
- * endTime(c, 'db');
41
- *
42
- * return c.json({ response: data });
43
- * });
44
- * ```
45
- */
46
- const timing = (config) => {
47
- const options = {
48
- total: true,
49
- enabled: true,
50
- totalDescription: "Total Response Time",
51
- autoEnd: true,
52
- crossOrigin: false,
53
- ...config
54
- };
55
- return async function timing(c, next) {
56
- const headers = [];
57
- const timers = /* @__PURE__ */ new Map();
58
- if (c.get("metric")) return await next();
59
- c.set("metric", {
60
- headers,
61
- timers
62
- });
63
- if (options.total) startTime(c, "total", options.totalDescription);
64
- await next();
65
- if (options.total) endTime(c, "total");
66
- if (options.autoEnd) timers.forEach((_, key) => {
67
- endTime(c, key);
68
- });
69
- if (typeof options.enabled === "function" ? options.enabled(c) : options.enabled) {
70
- c.res.headers.append("Server-Timing", headers.join(","));
71
- const crossOrigin = typeof options.crossOrigin === "function" ? options.crossOrigin(c) : options.crossOrigin;
72
- if (crossOrigin) c.res.headers.append("Timing-Allow-Origin", typeof crossOrigin === "string" ? crossOrigin : "*");
73
- }
74
- };
75
- };
76
- /**
77
- * Set a metric for the timing middleware.
78
- *
79
- * @param {Context} c - The context of the request.
80
- * @param {string} name - The name of the metric.
81
- * @param {number | string} [valueDescription] - The value or description of the metric.
82
- * @param {string} [description] - The description of the metric.
83
- * @param {number} [precision] - The precision of the metric value.
84
- *
85
- * @example
86
- * ```ts
87
- * setMetric(c, 'region', 'europe-west3')
88
- * setMetric(c, 'custom', 23.8, 'My custom Metric')
89
- * ```
90
- */
91
- const setMetric = (c, name, valueDescription, description, precision) => {
92
- const metrics = c.get("metric");
93
- if (!metrics) {
94
- console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
95
- return;
96
- }
97
- if (typeof valueDescription === "number") {
98
- const dur = valueDescription.toFixed(precision || 1);
99
- const metric = description ? `${name};dur=${dur};desc="${description}"` : `${name};dur=${dur}`;
100
- metrics.headers.push(metric);
101
- } else {
102
- const metric = valueDescription ? `${name};desc="${valueDescription}"` : `${name}`;
103
- metrics.headers.push(metric);
104
- }
105
- };
106
- /**
107
- * Start a timer for the timing middleware.
108
- *
109
- * @param {Context} c - The context of the request.
110
- * @param {string} name - The name of the timer.
111
- * @param {string} [description] - The description of the timer.
112
- *
113
- * @example
114
- * ```ts
115
- * startTime(c, 'db')
116
- * ```
117
- */
118
- const startTime = (c, name, description) => {
119
- const metrics = c.get("metric");
120
- if (!metrics) {
121
- console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
122
- return;
123
- }
124
- metrics.timers.set(name, {
125
- description,
126
- start: getTime()
127
- });
128
- };
129
- /**
130
- * End a timer for the timing middleware.
131
- *
132
- * @param {Context} c - The context of the request.
133
- * @param {string} name - The name of the timer.
134
- * @param {number} [precision] - The precision of the timer value.
135
- *
136
- * @example
137
- * ```ts
138
- * endTime(c, 'db')
139
- * ```
140
- */
141
- const endTime = (c, name, precision) => {
142
- const metrics = c.get("metric");
143
- if (!metrics) {
144
- console.warn("Metrics not initialized! Please add the `timing()` middleware to this route!");
145
- return;
146
- }
147
- const timer = metrics.timers.get(name);
148
- if (!timer) {
149
- console.warn(`Timer "${name}" does not exist!`);
150
- return;
151
- }
152
- const { description, start } = timer;
153
- const duration = getTime() - start;
154
- setMetric(c, name, duration, description, precision);
155
- metrics.timers.delete(name);
156
- };
157
- /**
158
- * Wrap a Promise to capture its duration.
159
- * @param {Context} c - The context of the request.
160
- * @param {string} name - The name of the timer.
161
- * @param {Promise<T>} callable - The Promise to time.
162
- * @param {string} [description] - The description of the timer.
163
- * @param {number} [precision] - The precision of the timer value.
164
- *
165
- * @example
166
- * ```ts
167
- * // Instead of this:
168
- * const data = await db.findMany(...);
169
- *
170
- * // do this:
171
- * const data = await wrapTime(c, 'query', db.findMany(...));
172
- * ```
173
- * */
174
- async function wrapTime(c, name, callable, description, precision) {
175
- startTime(c, name, description);
176
- try {
177
- return await callable;
178
- } finally {
179
- endTime(c, name, precision);
180
- }
181
- }
182
- //#endregion
1
+ import { a as wrapTime, i as timing, n as setMetric, r as startTime, t as endTime } from "./timing-Cky7C1Jf.js";
183
2
  export { endTime, setMetric, startTime, timing, wrapTime };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "acrylblends",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "The Acryl web framework (import { Acryl } from \"acrylblends\") and the acrylblends command that scaffolds Cordis apps.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -442,6 +442,74 @@
442
442
  "types": "./lib/kv/db.d.ts",
443
443
  "default": "./lib/kv/db.js"
444
444
  },
445
+ "./db/conformance": {
446
+ "types": "./lib/db/conformance.d.ts",
447
+ "default": "./lib/db/conformance.js"
448
+ },
449
+ "./kv/conformance": {
450
+ "types": "./lib/kv/conformance.d.ts",
451
+ "default": "./lib/kv/conformance.js"
452
+ },
453
+ "./door/host": {
454
+ "types": "./lib/door/host.d.ts",
455
+ "default": "./lib/door/host.js"
456
+ },
457
+ "./door/protocol": {
458
+ "types": "./lib/door/protocol.d.ts",
459
+ "default": "./lib/door/protocol.js"
460
+ },
461
+ "./door/page": {
462
+ "types": "./lib/door/page.d.ts",
463
+ "default": "./lib/door/page.js"
464
+ },
465
+ "./cordis/router": {
466
+ "types": "./lib/cordis/router.d.ts",
467
+ "default": "./lib/cordis/router.js"
468
+ },
469
+ "./cordis/router-http": {
470
+ "types": "./lib/cordis/router-http.d.ts",
471
+ "default": "./lib/cordis/router-http.js"
472
+ },
473
+ "./cordis/state": {
474
+ "types": "./lib/cordis/state.d.ts",
475
+ "default": "./lib/cordis/state.js"
476
+ },
477
+ "./cordis/state/react": {
478
+ "types": "./lib/cordis/state/react.d.ts",
479
+ "default": "./lib/cordis/state/react.js"
480
+ },
481
+ "./cordis/inspect": {
482
+ "types": "./lib/cordis/inspect.d.ts",
483
+ "default": "./lib/cordis/inspect.js"
484
+ },
485
+ "./cordis/node-server": {
486
+ "types": "./lib/cordis/node-server.d.ts",
487
+ "default": "./lib/cordis/node-server.js"
488
+ },
489
+ "./cordis/db": {
490
+ "types": "./lib/cordis/db.d.ts",
491
+ "default": "./lib/cordis/db.js"
492
+ },
493
+ "./cordis/db-sqlite": {
494
+ "types": "./lib/cordis/db-sqlite.d.ts",
495
+ "default": "./lib/cordis/db-sqlite.js"
496
+ },
497
+ "./cordis/kv": {
498
+ "types": "./lib/cordis/kv.d.ts",
499
+ "default": "./lib/cordis/kv.js"
500
+ },
501
+ "./cordis/kv-json": {
502
+ "types": "./lib/cordis/kv-json.d.ts",
503
+ "default": "./lib/cordis/kv-json.js"
504
+ },
505
+ "./cordis/kv-db": {
506
+ "types": "./lib/cordis/kv-db.d.ts",
507
+ "default": "./lib/cordis/kv-db.js"
508
+ },
509
+ "./cordis/door": {
510
+ "types": "./lib/cordis/door.d.ts",
511
+ "default": "./lib/cordis/door.js"
512
+ },
445
513
  "./package.json": "./package.json",
446
514
  "./cli": {
447
515
  "types": "./dist/index.d.ts",
@@ -480,6 +548,26 @@
480
548
  "access": "public"
481
549
  },
482
550
  "sideEffects": false,
551
+ "peerDependencies": {
552
+ "@deepseek-ai/cordis": "^4.0.2",
553
+ "@deepseek-ai/schemastery": "~3.18.4",
554
+ "react": ">=18",
555
+ "vitest": ">=3"
556
+ },
557
+ "peerDependenciesMeta": {
558
+ "@deepseek-ai/cordis": {
559
+ "optional": true
560
+ },
561
+ "@deepseek-ai/schemastery": {
562
+ "optional": true
563
+ },
564
+ "react": {
565
+ "optional": true
566
+ },
567
+ "vitest": {
568
+ "optional": true
569
+ }
570
+ },
483
571
  "scripts": {
484
572
  "build": "tsdown",
485
573
  "clean": "rm -rf dist",
@@ -13,5 +13,10 @@
13
13
  "#acryl-db": "acryl-db/src/index.ts",
14
14
  "#acryl-db/conformance": "acryl-db/src/conformance.ts"
15
15
  },
16
- "suite": "src/conformance.ts"
16
+ "suite": "src/conformance.ts",
17
+ "entries": {
18
+ "": "src/index.ts",
19
+ "/cordis": "src/cordis.ts",
20
+ "/conformance": "src/conformance.ts"
21
+ }
17
22
  }
@@ -12,5 +12,9 @@
12
12
  "omit": ["**/*.test.*", "**/*.spec.*", "**/*.case.ts"],
13
13
  "imports": {
14
14
  "#acryl-db-sqlite": "acryl-db-sqlite/src/index.ts"
15
+ },
16
+ "entries": {
17
+ "": "src/index.ts",
18
+ "/open": "src/open.ts"
15
19
  }
16
20
  }
@@ -2,7 +2,7 @@ import { mkdir } from "node:fs/promises";
2
2
  import { dirname } from "node:path";
3
3
  import type { StatementSync } from "node:sqlite";
4
4
  import { DatabaseSync } from "node:sqlite";
5
- import type { Db, DbExecutor, SqlValue } from "../../../acryl-db/src/index.ts";
5
+ import type { Db, DbExecutor, SqlValue } from "acryl-db";
6
6
 
7
7
  /** Node SQLite connection settings for a database file. */
8
8
  export type NodeSqliteDatabaseOptions = {
@@ -12,8 +12,8 @@ import { dirname, isAbsolute, join, resolve } from 'node:path'
12
12
  import { fileURLToPath } from 'node:url'
13
13
  import type { Context } from '@deepseek-ai/cordis'
14
14
  import z from '@deepseek-ai/schemastery'
15
- import { DB_SERVICE } from '../../acryl-db/src/index.ts'
16
- import type {} from '../../acryl-db/src/cordis.ts'
15
+ import { DB_SERVICE } from 'acryl-db'
16
+ import type {} from 'acryl-db/cordis'
17
17
  import { openDb } from './open.ts'
18
18
 
19
19
  export { NodeSqliteDatabase, openNodeSqliteDatabase, type NodeSqliteDatabaseOptions } from './facade/node.ts'
@@ -6,10 +6,10 @@
6
6
  * This entry imports nothing from Cordis.
7
7
  */
8
8
 
9
- import { applyMigrations, MIGRATIONS_TABLE, readMigrationFiles, type Db, type SqlValue } from '../../acryl-db/src/index.ts'
9
+ import { applyMigrations, MIGRATIONS_TABLE, readMigrationFiles, type Db, type SqlValue } from 'acryl-db'
10
10
  import { openNodeSqliteDatabase } from './facade/node.ts'
11
11
 
12
- export type { Db, DbExecutor, SqlValue } from '../../acryl-db/src/index.ts'
12
+ export type { Db, DbExecutor, SqlValue } from 'acryl-db'
13
13
 
14
14
  export interface OpenDbOptions {
15
15
  /** The database file, its folder created if missing; `:memory:` for a database that lives until `close`. */
@@ -10,5 +10,8 @@
10
10
  },
11
11
  "mount": "mount it after acryl-node-server and extensions in development builds: root.plugin(door); add <script type=\"module\" src=\"/api/acryl-door/page.js\"></script> to the pages; the command is .acryl/bin/acryl",
12
12
  "gitignore": [".acryl/"],
13
- "omit": ["**/*.test.*", "**/*.spec.*", "**/*.case.ts"]
13
+ "omit": ["**/*.test.*", "**/*.spec.*", "**/*.case.ts"],
14
+ "entries": {
15
+ "": "src/index.ts"
16
+ }
14
17
  }
@@ -5,8 +5,8 @@
5
5
  */
6
6
 
7
7
  import { timingSafeEqual } from 'node:crypto'
8
- import { Acryl, type Context, type MiddlewareHandler } from '../../../acryl-router/src/index.ts'
9
- import { bodyLimit, loopbackAuthority, loopbackOnly, methodNotAllowed } from '../../../acryl-router/src/middleware/index.ts'
8
+ import { Acryl, type Context, type MiddlewareHandler } from 'acryl-router'
9
+ import { bodyLimit, loopbackAuthority, loopbackOnly, methodNotAllowed } from 'acryl-router/middleware'
10
10
 
11
11
  /** Every door route lives below this path. */
12
12
  export const DOOR_PREFIX = '/api/acryl-door'
@@ -4,7 +4,7 @@
4
4
  * operator client sends. The same guard as the operator's routes: loopback, this run's secret.
5
5
  */
6
6
 
7
- import { Acryl } from '../../../acryl-router/src/index.ts'
7
+ import { Acryl } from 'acryl-router'
8
8
  import { UiControlError } from '../page/protocol.ts'
9
9
  import type { Door } from './door.ts'
10
10
  import { DoorError } from './door.ts'
@@ -5,7 +5,7 @@
5
5
  * away aborts its call through the request's signal.
6
6
  */
7
7
 
8
- import { Acryl } from '../../../acryl-router/src/index.ts'
8
+ import { Acryl } from 'acryl-router'
9
9
  import { UiControlError } from '../page/protocol.ts'
10
10
  import { Door, DoorError } from './door.ts'
11
11
  import { error, INVALID_JSON, jsonBody, operatorOnly, readJson, type ReportError } from './http.ts'
@@ -6,9 +6,9 @@
6
6
  * `{ window, message }` (a result, an error or a focus change). Both are for the app's own page only (same origin, loopback).
7
7
  */
8
8
 
9
- import { Acryl } from '../../../acryl-router/src/index.ts'
10
- import { streamSSE } from '../../../acryl-router/src/helpers/index.ts'
11
- import { loopbackOnly } from '../../../acryl-router/src/middleware/index.ts'
9
+ import { Acryl } from 'acryl-router'
10
+ import { streamSSE } from 'acryl-router/helpers'
11
+ import { loopbackOnly } from 'acryl-router/middleware'
12
12
  import { parseChannelToHost, WINDOW_ID } from '../page/protocol.ts'
13
13
  import type { UiChannel } from './channel.ts'
14
14
  import { error, INVALID_JSON, jsonBody, ownPage, readJson } from './http.ts'
@@ -12,8 +12,8 @@
12
12
  import { join } from 'node:path'
13
13
  import type { Context } from '@deepseek-ai/cordis'
14
14
  import z from '@deepseek-ai/schemastery'
15
- import { Acryl } from '../../acryl-router/src/index.ts'
16
- import { clientModules } from '../../acryl-node-server/src/client-modules.ts'
15
+ import { Acryl } from 'acryl-router'
16
+ import { clientModules } from 'acryl-node-server/client-modules'
17
17
  import { auditPath, AuditLog } from './host/audit.ts'
18
18
  import { UiChannel } from './host/channel.ts'
19
19
  import { registerChatTools } from './host/chat-tools.ts'
@@ -8,5 +8,8 @@
8
8
  "@deepseek-ai/cordis": "4.0.4"
9
9
  },
10
10
  "mount": "root.plugin(inspect); ctx.inspect.plugins(), ctx.inspect.explain('name'), ctx.inspect.render()",
11
- "omit": ["**/*.test.*", "**/*.case.ts"]
11
+ "omit": ["**/*.test.*", "**/*.case.ts"],
12
+ "entries": {
13
+ "": "src/index.ts"
14
+ }
12
15
  }
@@ -5,8 +5,8 @@
5
5
  */
6
6
 
7
7
  import { Context, Service } from '@deepseek-ai/cordis'
8
- import { loopbackOnly } from '../../acryl-router/src/middleware/index.ts'
9
- import { Acryl } from '../../acryl-router/src/index.ts'
8
+ import { loopbackOnly } from 'acryl-router/middleware'
9
+ import { Acryl } from 'acryl-router'
10
10
  import { explain, ownerOf, renderReports, reportFiber, type FiberView, type PluginReport } from './report.ts'
11
11
 
12
12
  declare module '@deepseek-ai/cordis' {
@@ -13,5 +13,10 @@
13
13
  "#acryl-kv": "acryl-kv/src/index.ts",
14
14
  "#acryl-kv/conformance": "acryl-kv/src/conformance.ts"
15
15
  },
16
- "suite": "src/conformance.ts"
16
+ "suite": "src/conformance.ts",
17
+ "entries": {
18
+ "": "src/index.ts",
19
+ "/cordis": "src/cordis.ts",
20
+ "/conformance": "src/conformance.ts"
21
+ }
17
22
  }
@@ -12,5 +12,9 @@
12
12
  "omit": ["**/*.test.*", "**/*.spec.*", "**/*.case.ts"],
13
13
  "imports": {
14
14
  "#acryl-kv-db": "acryl-kv-db/src/index.ts"
15
+ },
16
+ "entries": {
17
+ "": "src/index.ts",
18
+ "/open": "src/open.ts"
15
19
  }
16
20
  }
@@ -8,7 +8,7 @@
8
8
 
9
9
  import type { Context } from '@deepseek-ai/cordis'
10
10
  import z from '@deepseek-ai/schemastery'
11
- import { KV_SERVICE } from '../../acryl-kv/src/index.ts'
11
+ import { KV_SERVICE } from 'acryl-kv'
12
12
  import { kvOnDb } from './open.ts'
13
13
 
14
14
  export { kvOnDb, type KvOnDbOptions } from './open.ts'