@blyp/core 0.1.21 → 0.1.22

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 (242) hide show
  1. package/README.md +217 -11
  2. package/STABILITY.md +56 -0
  3. package/dist/ai/anthropic/index.d.ts +3 -0
  4. package/dist/ai/anthropic/normalize.d.ts +9 -0
  5. package/dist/ai/anthropic/stream.d.ts +3 -0
  6. package/dist/ai/anthropic/wrap.d.ts +13 -0
  7. package/dist/ai/anthropic.js +701 -0
  8. package/dist/ai/anthropic.mjs +701 -0
  9. package/dist/ai/fetch.js +701 -0
  10. package/dist/ai/fetch.mjs +701 -0
  11. package/dist/ai/openai/index.d.ts +3 -0
  12. package/dist/ai/openai/normalize.d.ts +10 -0
  13. package/dist/ai/openai/stream.d.ts +3 -0
  14. package/dist/ai/openai/wrap.d.ts +24 -0
  15. package/dist/ai/openai.js +701 -0
  16. package/dist/ai/openai.mjs +701 -0
  17. package/dist/ai/shared/fetch.d.ts +5 -0
  18. package/dist/ai/shared/index.d.ts +2 -0
  19. package/dist/ai/shared/normalize.d.ts +17 -0
  20. package/dist/ai/shared/redaction.d.ts +6 -0
  21. package/dist/ai/shared/stream.d.ts +7 -0
  22. package/dist/ai/shared/trace.d.ts +116 -0
  23. package/dist/ai/shared/types.d.ts +161 -0
  24. package/dist/ai/shared.js +701 -0
  25. package/dist/ai/shared.mjs +701 -0
  26. package/dist/ai/vercel/index.d.ts +3 -0
  27. package/dist/ai/vercel/middleware.d.ts +3 -0
  28. package/dist/ai/vercel/model.d.ts +3 -0
  29. package/dist/ai/vercel.js +701 -0
  30. package/dist/ai/vercel.mjs +701 -0
  31. package/dist/astro.js +618 -4519
  32. package/dist/astro.mjs +618 -4491
  33. package/dist/client.js +1 -1139
  34. package/dist/client.mjs +1 -1132
  35. package/dist/connectors/betterstack.js +9 -1532
  36. package/dist/connectors/betterstack.mjs +9 -1507
  37. package/dist/connectors/databuddy.js +9 -1456
  38. package/dist/connectors/databuddy.mjs +9 -1451
  39. package/dist/connectors/delivery/backoff.d.ts +2 -0
  40. package/dist/connectors/delivery/manager.d.ts +44 -0
  41. package/dist/connectors/delivery/queue-path.d.ts +1 -0
  42. package/dist/connectors/delivery/sqlite-adapter-bun.d.ts +1 -0
  43. package/dist/connectors/delivery/sqlite-adapter-node.d.ts +1 -0
  44. package/dist/connectors/delivery/sqlite-client.d.ts +37 -0
  45. package/dist/connectors/delivery/sqlite-worker.d.ts +1 -0
  46. package/dist/connectors/delivery/studio-queue.d.ts +12 -0
  47. package/dist/connectors/delivery/types.d.ts +98 -0
  48. package/dist/connectors/otlp/index.d.ts +1 -0
  49. package/dist/connectors/otlp.js +9 -1371
  50. package/dist/connectors/otlp.mjs +9 -1368
  51. package/dist/connectors/posthog/properties.d.ts +4 -0
  52. package/dist/connectors/posthog/sender.d.ts +0 -1
  53. package/dist/connectors/posthog.js +10 -1527
  54. package/dist/connectors/posthog.mjs +10 -1522
  55. package/dist/connectors/sentry.js +9 -1380
  56. package/dist/connectors/sentry.mjs +9 -1357
  57. package/dist/core/config.d.ts +7 -2
  58. package/dist/core/log-record.d.ts +5 -4
  59. package/dist/core/logger.d.ts +2 -0
  60. package/dist/core/optional-module.d.ts +1 -0
  61. package/dist/core/pino-pretty-loader.d.ts +4 -0
  62. package/dist/database.js +1 -24
  63. package/dist/database.mjs +1 -21
  64. package/dist/elysia.js +613 -4517
  65. package/dist/elysia.mjs +615 -4491
  66. package/dist/expo.js +1 -726
  67. package/dist/expo.mjs +1 -724
  68. package/dist/express.js +613 -4507
  69. package/dist/express.mjs +615 -4480
  70. package/dist/fastify.js +615 -4526
  71. package/dist/fastify.mjs +617 -4499
  72. package/dist/frameworks/elysia/index.d.ts +1 -1
  73. package/dist/frameworks/elysia/logger.d.ts +2 -35
  74. package/dist/frameworks/nestjs/helpers.d.ts +2 -0
  75. package/dist/frameworks/shared/http.d.ts +3 -2
  76. package/dist/frameworks/shared/index.d.ts +2 -1
  77. package/dist/frameworks/shared/logger.d.ts +2 -0
  78. package/dist/frameworks/shared/request-context.d.ts +2 -0
  79. package/dist/frameworks/shared/trace.d.ts +6 -0
  80. package/dist/hono.js +616 -4496
  81. package/dist/hono.mjs +618 -4470
  82. package/dist/index.d.ts +49 -19
  83. package/dist/index.js +615 -7557
  84. package/dist/index.mjs +618 -7465
  85. package/dist/nestjs.js +626 -4999
  86. package/dist/nestjs.mjs +621 -4966
  87. package/dist/nextjs.js +618 -4525
  88. package/dist/nextjs.mjs +620 -4499
  89. package/dist/nitro.js +617 -4642
  90. package/dist/nitro.mjs +619 -4616
  91. package/dist/nuxt.js +617 -4649
  92. package/dist/nuxt.mjs +619 -4623
  93. package/dist/react-router.js +616 -4559
  94. package/dist/react-router.mjs +618 -4533
  95. package/dist/shared/redaction.d.ts +17 -0
  96. package/dist/standalone.js +606 -3628
  97. package/dist/standalone.mjs +610 -3603
  98. package/dist/sveltekit.js +617 -4523
  99. package/dist/sveltekit.mjs +619 -4497
  100. package/dist/tanstack-start.js +616 -4522
  101. package/dist/tanstack-start.mjs +618 -4496
  102. package/dist/types/core/config.d.ts +57 -0
  103. package/dist/types/core/logger.d.ts +3 -0
  104. package/dist/types/core/structured-log.d.ts +2 -0
  105. package/dist/types/database.d.ts +1 -0
  106. package/dist/types/frameworks/astro.d.ts +1 -0
  107. package/dist/types/frameworks/client.d.ts +2 -0
  108. package/dist/types/frameworks/elysia.d.ts +2 -0
  109. package/dist/types/frameworks/express.d.ts +1 -0
  110. package/dist/types/frameworks/fastify.d.ts +1 -0
  111. package/dist/types/frameworks/hono.d.ts +1 -0
  112. package/dist/types/frameworks/http.d.ts +1 -0
  113. package/dist/types/frameworks/nestjs.d.ts +2 -0
  114. package/dist/types/frameworks/nextjs.d.ts +1 -0
  115. package/dist/types/frameworks/nitro.d.ts +1 -0
  116. package/dist/types/frameworks/react-router.d.ts +2 -0
  117. package/dist/types/frameworks/request-context.d.ts +1 -0
  118. package/dist/types/frameworks/shared.d.ts +4 -1
  119. package/dist/types/frameworks/standalone.d.ts +3 -2
  120. package/dist/types/frameworks/sveltekit.d.ts +1 -0
  121. package/dist/types/frameworks/tanstack-start.d.ts +9 -3
  122. package/dist/types/frameworks/workers.d.ts +3 -0
  123. package/dist/types/shared/client-log.d.ts +1 -0
  124. package/dist/workers.js +2 -626
  125. package/dist/workers.mjs +2 -623
  126. package/package.json +246 -173
  127. package/dist/astro.js.map +0 -1
  128. package/dist/astro.mjs.map +0 -1
  129. package/dist/client.js.map +0 -1
  130. package/dist/client.mjs.map +0 -1
  131. package/dist/connectors/betterstack.js.map +0 -1
  132. package/dist/connectors/betterstack.mjs.map +0 -1
  133. package/dist/connectors/databuddy.js.map +0 -1
  134. package/dist/connectors/databuddy.mjs.map +0 -1
  135. package/dist/connectors/otlp.js.map +0 -1
  136. package/dist/connectors/otlp.mjs.map +0 -1
  137. package/dist/connectors/posthog.js.map +0 -1
  138. package/dist/connectors/posthog.mjs.map +0 -1
  139. package/dist/connectors/sentry.js.map +0 -1
  140. package/dist/connectors/sentry.mjs.map +0 -1
  141. package/dist/database.js.map +0 -1
  142. package/dist/database.mjs.map +0 -1
  143. package/dist/elysia.js.map +0 -1
  144. package/dist/elysia.mjs.map +0 -1
  145. package/dist/expo.js.map +0 -1
  146. package/dist/expo.mjs.map +0 -1
  147. package/dist/express.js.map +0 -1
  148. package/dist/express.mjs.map +0 -1
  149. package/dist/fastify.js.map +0 -1
  150. package/dist/fastify.mjs.map +0 -1
  151. package/dist/hono.js.map +0 -1
  152. package/dist/hono.mjs.map +0 -1
  153. package/dist/index.js.map +0 -1
  154. package/dist/index.mjs.map +0 -1
  155. package/dist/nestjs.js.map +0 -1
  156. package/dist/nestjs.mjs.map +0 -1
  157. package/dist/nextjs.js.map +0 -1
  158. package/dist/nextjs.mjs.map +0 -1
  159. package/dist/nitro.js.map +0 -1
  160. package/dist/nitro.mjs.map +0 -1
  161. package/dist/nuxt.js.map +0 -1
  162. package/dist/nuxt.mjs.map +0 -1
  163. package/dist/react-router.js.map +0 -1
  164. package/dist/react-router.mjs.map +0 -1
  165. package/dist/standalone.js.map +0 -1
  166. package/dist/standalone.mjs.map +0 -1
  167. package/dist/sveltekit.js.map +0 -1
  168. package/dist/sveltekit.mjs.map +0 -1
  169. package/dist/tanstack-start.js.map +0 -1
  170. package/dist/tanstack-start.mjs.map +0 -1
  171. package/dist/workers.js.map +0 -1
  172. package/dist/workers.mjs.map +0 -1
  173. package/exports/client.js +0 -3
  174. package/exports/client.mjs +0 -3
  175. package/exports/connectors/betterstack.js +0 -1
  176. package/exports/connectors/betterstack.mjs +0 -1
  177. package/exports/connectors/databuddy.js +0 -1
  178. package/exports/connectors/databuddy.mjs +0 -1
  179. package/exports/connectors/otlp.js +0 -1
  180. package/exports/connectors/otlp.mjs +0 -1
  181. package/exports/connectors/posthog.js +0 -1
  182. package/exports/connectors/posthog.mjs +0 -1
  183. package/exports/connectors/sentry.js +0 -1
  184. package/exports/connectors/sentry.mjs +0 -1
  185. package/exports/database.js +0 -1
  186. package/exports/database.mjs +0 -1
  187. package/exports/expo.js +0 -1
  188. package/exports/expo.mjs +0 -1
  189. package/exports/frameworks/astro.js +0 -1
  190. package/exports/frameworks/astro.mjs +0 -1
  191. package/exports/frameworks/elysia.js +0 -1
  192. package/exports/frameworks/elysia.mjs +0 -1
  193. package/exports/frameworks/express.js +0 -1
  194. package/exports/frameworks/express.mjs +0 -1
  195. package/exports/frameworks/fastify.js +0 -1
  196. package/exports/frameworks/fastify.mjs +0 -1
  197. package/exports/frameworks/hono.js +0 -1
  198. package/exports/frameworks/hono.mjs +0 -1
  199. package/exports/frameworks/nestjs.js +0 -1
  200. package/exports/frameworks/nestjs.mjs +0 -1
  201. package/exports/frameworks/nextjs.js +0 -1
  202. package/exports/frameworks/nextjs.mjs +0 -1
  203. package/exports/frameworks/nitro.js +0 -1
  204. package/exports/frameworks/nitro.mjs +0 -1
  205. package/exports/frameworks/nuxt.js +0 -1
  206. package/exports/frameworks/nuxt.mjs +0 -1
  207. package/exports/frameworks/react-router.js +0 -1
  208. package/exports/frameworks/react-router.mjs +0 -1
  209. package/exports/frameworks/standalone.js +0 -1
  210. package/exports/frameworks/standalone.mjs +0 -1
  211. package/exports/frameworks/sveltekit.js +0 -1
  212. package/exports/frameworks/sveltekit.mjs +0 -1
  213. package/exports/frameworks/tanstack-start.js +0 -1
  214. package/exports/frameworks/tanstack-start.mjs +0 -1
  215. package/exports/workers.js +0 -1
  216. package/exports/workers.mjs +0 -1
  217. package/types/client.d.ts +0 -34
  218. package/types/connectors/betterstack.d.ts +0 -1
  219. package/types/connectors/databuddy.d.ts +0 -1
  220. package/types/connectors/otlp.d.ts +0 -1
  221. package/types/connectors/posthog.d.ts +0 -1
  222. package/types/connectors/sentry.d.ts +0 -1
  223. package/types/database.d.ts +0 -1
  224. package/types/expo.d.ts +0 -17
  225. package/types/frameworks/astro.d.ts +0 -1
  226. package/types/frameworks/client.d.ts +0 -160
  227. package/types/frameworks/elysia.d.ts +0 -1
  228. package/types/frameworks/expo.d.ts +0 -50
  229. package/types/frameworks/express.d.ts +0 -1
  230. package/types/frameworks/fastify.d.ts +0 -1
  231. package/types/frameworks/hono.d.ts +0 -1
  232. package/types/frameworks/nestjs.d.ts +0 -1
  233. package/types/frameworks/nextjs.d.ts +0 -1
  234. package/types/frameworks/nitro.d.ts +0 -1
  235. package/types/frameworks/nuxt.d.ts +0 -1
  236. package/types/frameworks/react-router.d.ts +0 -1
  237. package/types/frameworks/standalone.d.ts +0 -1
  238. package/types/frameworks/sveltekit.d.ts +0 -1
  239. package/types/frameworks/tanstack-start.d.ts +0 -1
  240. package/types/frameworks/workers.d.ts +0 -115
  241. package/types/index.d.ts +0 -1
  242. package/types/workers.d.ts +0 -13
package/dist/workers.mjs CHANGED
@@ -1,623 +1,2 @@
1
- // src/core/colors.ts
2
- function getMethodColor(method) {
3
- const colors = {
4
- GET: "\x1B[32m",
5
- POST: "\x1B[36m",
6
- PUT: "\x1B[33m",
7
- PATCH: "\x1B[34m",
8
- DELETE: "\x1B[31m"
9
- };
10
- const reset = "\x1B[0m";
11
- return `${colors[method.toUpperCase()] || ""}${method}${reset}`;
12
- }
13
- function getStatusColor(statusCode) {
14
- const reset = "\x1B[0m";
15
- if (statusCode >= 500) return `\x1B[31m${statusCode}${reset}`;
16
- if (statusCode >= 400) return `\x1B[33m${statusCode}${reset}`;
17
- if (statusCode >= 300) return `\x1B[36m${statusCode}${reset}`;
18
- if (statusCode >= 200) return `\x1B[32m${statusCode}${reset}`;
19
- return `\x1B[37m${statusCode}${reset}`;
20
- }
21
- function getArrowForMethod(method) {
22
- const arrows = {
23
- GET: "\u2192",
24
- POST: "\u2191",
25
- PUT: "\u21D1",
26
- PATCH: "\u2197",
27
- DELETE: "\u2715"
28
- };
29
- return arrows[method.toUpperCase()] || "\u2022";
30
- }
31
- function getResponseTimeColor(ms) {
32
- const reset = "\x1B[0m";
33
- if (ms < 100) return `\x1B[32m${ms}ms${reset}`;
34
- if (ms < 300) return `\x1B[33m${ms}ms${reset}`;
35
- if (ms < 1e3) return `\x1B[31m${ms}ms${reset}`;
36
- return `\x1B[41m\x1B[37m${ms}ms${reset}`;
37
- }
38
-
39
- // src/frameworks/shared/http.ts
40
- function getHeaderValue(headers, name) {
41
- if (!headers) {
42
- return void 0;
43
- }
44
- if (headers instanceof Headers) {
45
- return headers.get(name) ?? headers.get(name.toLowerCase()) ?? void 0;
46
- }
47
- if (typeof headers.get === "function") {
48
- const direct2 = headers.get(name);
49
- return direct2 ?? headers.get(name.toLowerCase()) ?? void 0;
50
- }
51
- const record = headers;
52
- const direct = record[name] ?? record[name.toLowerCase()];
53
- if (Array.isArray(direct)) {
54
- return direct[0];
55
- }
56
- return direct;
57
- }
58
- function parseForwardedHeader(value) {
59
- if (!value) {
60
- return [];
61
- }
62
- return value.split(",").map((entry) => entry.trim()).filter(Boolean).map((entry) => {
63
- const match = entry.match(/for="?(\[[^\]]+\]|[^;,\s"]+)/i);
64
- return match?.[1]?.replace(/^"|"$/g, "") ?? "";
65
- }).map((entry) => entry.replace(/^\[|\]$/g, "")).filter(Boolean);
66
- }
67
- function parseForwardedFor(value) {
68
- if (!value) {
69
- return [];
70
- }
71
- return value.split(",").map((entry) => entry.trim()).filter(Boolean);
72
- }
73
- function stripPort(host) {
74
- if (host.startsWith("[")) {
75
- const endIndex = host.indexOf("]");
76
- return endIndex >= 0 ? host.slice(1, endIndex) : host;
77
- }
78
- return host.replace(/:\d+$/, "");
79
- }
80
- function detectBrowser(userAgent) {
81
- if (/edg\//i.test(userAgent)) return "Edge";
82
- if (/opr\//i.test(userAgent) || /opera/i.test(userAgent)) return "Opera";
83
- if (/chrome\//i.test(userAgent) && !/edg\//i.test(userAgent)) return "Chrome";
84
- if (/firefox\//i.test(userAgent)) return "Firefox";
85
- if (/safari\//i.test(userAgent) && !/chrome\//i.test(userAgent)) return "Safari";
86
- if (/curl\//i.test(userAgent)) return "curl";
87
- if (/postmanruntime/i.test(userAgent)) return "Postman";
88
- return "Unknown";
89
- }
90
- function detectOperatingSystem(userAgent) {
91
- if (/windows/i.test(userAgent)) return "Windows";
92
- if (/android/i.test(userAgent)) return "Android";
93
- if (/iphone|ipad|ipod/i.test(userAgent)) return "iOS";
94
- if (/mac os x|macintosh/i.test(userAgent)) return "macOS";
95
- if (/linux/i.test(userAgent)) return "Linux";
96
- return "Unknown";
97
- }
98
- function detectDeviceType(userAgent) {
99
- if (!userAgent) return "unknown";
100
- if (/bot|crawler|spider|curl|wget|postmanruntime/i.test(userAgent)) return "bot";
101
- if (/ipad|tablet/i.test(userAgent)) return "tablet";
102
- if (/mobi|iphone|android/i.test(userAgent)) return "mobile";
103
- return "desktop";
104
- }
105
- function extractPathname(requestUrl, fallbackPath = "/") {
106
- if (!requestUrl) {
107
- return fallbackPath;
108
- }
109
- if (requestUrl.startsWith("http://") || requestUrl.startsWith("https://")) {
110
- try {
111
- return new URL(requestUrl).pathname || fallbackPath;
112
- } catch {
113
- return fallbackPath;
114
- }
115
- }
116
- if (requestUrl.startsWith("/")) {
117
- const queryIndex = requestUrl.indexOf("?");
118
- return queryIndex >= 0 ? requestUrl.slice(0, queryIndex) : requestUrl;
119
- }
120
- try {
121
- return new URL(requestUrl, "http://localhost").pathname || fallbackPath;
122
- } catch {
123
- return fallbackPath;
124
- }
125
- }
126
- function createRequestLike(method, url, headers) {
127
- return { method, url, headers };
128
- }
129
- function buildClientDetails(request, fallbackPath) {
130
- const pathname = fallbackPath ?? extractPathname(request.url);
131
- const urlObject = (() => {
132
- try {
133
- return new URL(request.url);
134
- } catch {
135
- try {
136
- return new URL(pathname, "http://localhost");
137
- } catch {
138
- return null;
139
- }
140
- }
141
- })();
142
- const hostHeader = getHeaderValue(request.headers, "x-forwarded-host") ?? getHeaderValue(request.headers, "host") ?? urlObject?.host ?? void 0;
143
- const hostname = hostHeader ? stripPort(hostHeader) : urlObject?.hostname;
144
- const port = hostHeader?.match(/:(\d+)$/)?.[1] ?? (urlObject?.port || void 0);
145
- const xForwardedFor = parseForwardedFor(getHeaderValue(request.headers, "x-forwarded-for"));
146
- const forwardedFor = parseForwardedHeader(getHeaderValue(request.headers, "forwarded"));
147
- const ipCandidates = [
148
- getHeaderValue(request.headers, "cf-connecting-ip"),
149
- getHeaderValue(request.headers, "true-client-ip"),
150
- getHeaderValue(request.headers, "fly-client-ip"),
151
- getHeaderValue(request.headers, "x-real-ip"),
152
- getHeaderValue(request.headers, "x-client-ip"),
153
- xForwardedFor[0],
154
- forwardedFor[0]
155
- ].filter((value) => Boolean(value));
156
- const userAgent = getHeaderValue(request.headers, "user-agent");
157
- const platform = getHeaderValue(request.headers, "sec-ch-ua-platform")?.replace(/^"|"$/g, "") ?? void 0;
158
- const deviceType = detectDeviceType(userAgent ?? "");
159
- return {
160
- hostname,
161
- ip: ipCandidates[0],
162
- forwardedFor: [...xForwardedFor, ...forwardedFor].filter((value, index, values) => {
163
- return values.indexOf(value) === index;
164
- }),
165
- protocol: getHeaderValue(request.headers, "x-forwarded-proto") ?? (urlObject?.protocol ? urlObject.protocol.replace(/:$/, "") : void 0),
166
- port,
167
- userAgent,
168
- origin: getHeaderValue(request.headers, "origin"),
169
- referer: getHeaderValue(request.headers, "referer"),
170
- acceptLanguage: getHeaderValue(request.headers, "accept-language"),
171
- client: {
172
- ip: ipCandidates[0],
173
- hostname,
174
- browser: userAgent ? detectBrowser(userAgent) : void 0,
175
- os: userAgent ? detectOperatingSystem(userAgent) : void 0,
176
- deviceType,
177
- platform,
178
- isMobile: deviceType === "mobile"
179
- }
180
- };
181
- }
182
- function buildRequestLogData(request, type, path, statusCode, responseTime, extra = {}) {
183
- return {
184
- type,
185
- method: request.method,
186
- url: path,
187
- statusCode,
188
- responseTime,
189
- ...buildClientDetails(request, path),
190
- ...extra
191
- };
192
- }
193
- function buildInfoLogMessage(method, statusCode, url, responseTime) {
194
- const methodColor = getMethodColor(method);
195
- const statusColor = getStatusColor(statusCode);
196
- const timeColor = getResponseTimeColor(responseTime);
197
- const arrow = getArrowForMethod(method);
198
- return `${methodColor} ${arrow} ${statusColor} ${url} ${timeColor}`;
199
- }
200
- function toErrorLike(error, fallbackStatusCode) {
201
- if (error === void 0 || error === null) {
202
- return fallbackStatusCode === void 0 ? void 0 : { statusCode: fallbackStatusCode, message: `HTTP ${fallbackStatusCode}` };
203
- }
204
- if (error instanceof Error) {
205
- const errorWithStatus = error;
206
- return {
207
- status: errorWithStatus.status,
208
- statusCode: errorWithStatus.statusCode ?? fallbackStatusCode,
209
- code: errorWithStatus.code,
210
- message: error.message,
211
- stack: error.stack,
212
- why: errorWithStatus.why,
213
- fix: errorWithStatus.fix,
214
- link: errorWithStatus.link,
215
- details: errorWithStatus.details,
216
- cause: errorWithStatus.cause
217
- };
218
- }
219
- if (typeof error === "object") {
220
- const record = error;
221
- return {
222
- status: typeof record.status === "number" ? record.status : void 0,
223
- statusCode: typeof record.statusCode === "number" ? record.statusCode : fallbackStatusCode,
224
- code: typeof record.code === "string" || typeof record.code === "number" ? record.code : void 0,
225
- message: typeof record.message === "string" ? record.message : `HTTP ${fallbackStatusCode ?? 500}`,
226
- stack: typeof record.stack === "string" ? record.stack : void 0,
227
- why: typeof record.why === "string" ? record.why : void 0,
228
- fix: typeof record.fix === "string" ? record.fix : void 0,
229
- link: typeof record.link === "string" ? record.link : void 0,
230
- details: record.details !== null && typeof record.details === "object" && !Array.isArray(record.details) ? record.details : void 0,
231
- cause: record.cause
232
- };
233
- }
234
- return {
235
- statusCode: fallbackStatusCode,
236
- message: typeof error === "string" ? error : `HTTP ${fallbackStatusCode ?? 500}`
237
- };
238
- }
239
- function isErrorStatus(statusCode) {
240
- return statusCode >= 400;
241
- }
242
-
243
- // src/core/structured-log.ts
244
- function serializeMessage(message) {
245
- if (typeof message === "string") {
246
- return message;
247
- }
248
- if (message !== null && typeof message === "object") {
249
- try {
250
- return JSON.stringify(
251
- message,
252
- (_key, value) => {
253
- if (typeof value === "function") {
254
- return `[Function: ${value.name || "anonymous"}]`;
255
- }
256
- if (value === void 0) {
257
- return "[undefined]";
258
- }
259
- if (typeof value === "symbol") {
260
- return value.toString();
261
- }
262
- return value;
263
- },
264
- 2
265
- );
266
- } catch {
267
- return "[Object]";
268
- }
269
- }
270
- return String(message);
271
- }
272
- function normalizeEventData(message, args) {
273
- if (typeof message === "string") {
274
- if (args.length === 0) {
275
- return void 0;
276
- }
277
- return args.length === 1 ? args[0] : args;
278
- }
279
- const values = [message, ...args];
280
- return values.length === 1 ? values[0] : values;
281
- }
282
- function normalizeDetails(value) {
283
- if (!value || typeof value !== "object" || Array.isArray(value)) {
284
- return void 0;
285
- }
286
- return value;
287
- }
288
- function normalizeError(error, fallbackStatus) {
289
- if (error === void 0 || error === null) {
290
- return fallbackStatus === void 0 || fallbackStatus < 400 ? void 0 : {
291
- message: `HTTP ${fallbackStatus}`,
292
- code: fallbackStatus,
293
- type: "HttpError"
294
- };
295
- }
296
- if (error instanceof Error) {
297
- const errorLike = error;
298
- return {
299
- message: error.message,
300
- code: errorLike.code,
301
- type: errorLike.type ?? error.name ?? error.constructor?.name,
302
- stack: error.stack,
303
- why: errorLike.why,
304
- fix: errorLike.fix,
305
- link: errorLike.link,
306
- details: normalizeDetails(errorLike.details),
307
- cause: errorLike.cause
308
- };
309
- }
310
- if (typeof error === "object") {
311
- const record = error;
312
- const statusCode = typeof record.statusCode === "number" ? record.statusCode : typeof record.status === "number" ? record.status : fallbackStatus;
313
- return {
314
- message: typeof record.message === "string" ? record.message : `HTTP ${statusCode ?? 500}`,
315
- code: typeof record.code === "string" || typeof record.code === "number" ? record.code : statusCode,
316
- type: typeof record.type === "string" ? record.type : typeof record.name === "string" ? record.name : "Error",
317
- stack: typeof record.stack === "string" ? record.stack : void 0,
318
- why: typeof record.why === "string" ? record.why : void 0,
319
- fix: typeof record.fix === "string" ? record.fix : void 0,
320
- link: typeof record.link === "string" ? record.link : void 0,
321
- details: normalizeDetails(record.details),
322
- cause: record.cause
323
- };
324
- }
325
- return {
326
- message: String(error),
327
- code: fallbackStatus,
328
- type: typeof error
329
- };
330
- }
331
- function resolveEmitStatus(options) {
332
- if (options.response && typeof options.response.status === "number") {
333
- return options.response.status;
334
- }
335
- if (typeof options.status === "number") {
336
- return options.status;
337
- }
338
- if (options.error && typeof options.error === "object" && options.error !== null && typeof options.error.statusCode === "number") {
339
- return options.error.statusCode;
340
- }
341
- if (options.error && typeof options.error === "object" && options.error !== null && typeof options.error.status === "number") {
342
- return options.error.status;
343
- }
344
- return options.error ? 500 : void 0;
345
- }
346
- function createStructuredLog(groupId, options) {
347
- const startedAt = performance.now();
348
- const fields = { ...options.initialFields ?? {} };
349
- const events = [];
350
- let emittedPayload;
351
- options.onCreate?.();
352
- const appendEvent = (level, message, args) => {
353
- events.push({
354
- level,
355
- message: serializeMessage(message),
356
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
357
- ...normalizeEventData(message, args) === void 0 ? {} : { data: normalizeEventData(message, args) }
358
- });
359
- return structuredLog;
360
- };
361
- const structuredLog = {
362
- set(extraFields) {
363
- Object.assign(fields, extraFields);
364
- return structuredLog;
365
- },
366
- debug(message, ...args) {
367
- return appendEvent("debug", message, args);
368
- },
369
- info(message, ...args) {
370
- return appendEvent("info", message, args);
371
- },
372
- warn(message, ...args) {
373
- return appendEvent("warn", message, args);
374
- },
375
- warning(message, ...args) {
376
- return appendEvent("warning", message, args);
377
- },
378
- error(message, ...args) {
379
- return appendEvent("error", message, args);
380
- },
381
- success(message, ...args) {
382
- return appendEvent("success", message, args);
383
- },
384
- critical(message, ...args) {
385
- return appendEvent("critical", message, args);
386
- },
387
- table(message, data) {
388
- return appendEvent("table", message, data === void 0 ? [] : [data]);
389
- },
390
- emit(emitOptions = {}) {
391
- if (emittedPayload) {
392
- return emittedPayload;
393
- }
394
- const defaultFields = options.resolveDefaultFields?.() ?? {};
395
- const status = resolveEmitStatus(emitOptions);
396
- const error = normalizeError(emitOptions.error, status);
397
- const level = emitOptions.level ?? (error ? "error" : "info");
398
- const payload = {
399
- ...defaultFields,
400
- ...fields,
401
- groupId,
402
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
403
- level,
404
- duration: Math.round(performance.now() - startedAt),
405
- ...typeof status === "number" ? { status } : {},
406
- ...events.length > 0 ? { events: [...events] } : {},
407
- ...error ? { error } : {}
408
- };
409
- options.write(payload, emitOptions.message ?? "structured_log");
410
- emittedPayload = payload;
411
- options.onEmit?.(payload);
412
- return payload;
413
- }
414
- };
415
- return structuredLog;
416
- }
417
-
418
- // src/frameworks/workers/logger.ts
419
- var workersLoggerState = {};
420
- function serializeMessage2(message) {
421
- if (typeof message === "string") {
422
- return message;
423
- }
424
- if (message !== null && typeof message === "object") {
425
- try {
426
- return JSON.stringify(message, (_key, value) => {
427
- if (typeof value === "function") {
428
- return `[Function: ${value.name || "anonymous"}]`;
429
- }
430
- if (value === void 0) {
431
- return "[undefined]";
432
- }
433
- if (typeof value === "symbol") {
434
- return value.toString();
435
- }
436
- return value;
437
- }, 2);
438
- } catch {
439
- return "[Object]";
440
- }
441
- }
442
- return String(message);
443
- }
444
- function normalizeStructuredData(message, args) {
445
- if (typeof message === "string") {
446
- if (args.length === 0) {
447
- return void 0;
448
- }
449
- return args.length === 1 ? args[0] : args;
450
- }
451
- const values = [message, ...args];
452
- return values.length === 1 ? values[0] : values;
453
- }
454
- function getConsoleMethod(level) {
455
- switch (level) {
456
- case "debug":
457
- return "debug";
458
- case "warn":
459
- case "warning":
460
- return "warn";
461
- case "error":
462
- case "critical":
463
- return "error";
464
- case "success":
465
- return "log";
466
- case "info":
467
- default:
468
- return "info";
469
- }
470
- }
471
- function writeConsole(method, message, payload) {
472
- if (typeof console === "undefined") {
473
- return;
474
- }
475
- const logger = console[method] ?? console.info;
476
- if (payload === void 0) {
477
- logger.call(console, message);
478
- return;
479
- }
480
- logger.call(console, message, payload);
481
- }
482
- function writeStructuredConsole(level, message, payload) {
483
- writeConsole(getConsoleMethod(level), message, payload);
484
- }
485
- function buildRequestMetadata(request, path) {
486
- return {
487
- method: request.method,
488
- url: path,
489
- ...buildClientDetails(request, path)
490
- };
491
- }
492
- function createScopedPayload(request, path, fields, data) {
493
- return {
494
- request: buildRequestMetadata(request, path),
495
- ...fields,
496
- ...data === void 0 ? {} : { data }
497
- };
498
- }
499
- function emitScopedLog(level, request, path, fields, message, args) {
500
- writeConsole(
501
- getConsoleMethod(level),
502
- serializeMessage2(message),
503
- createScopedPayload(request, path, fields, normalizeStructuredData(message, args))
504
- );
505
- }
506
- function resolveEmitStatus2(options) {
507
- return options.response?.status ?? options.status ?? (options.error ? 500 : 200);
508
- }
509
- function createEmitPayload(request, path, fields, startTime, options) {
510
- const statusCode = resolveEmitStatus2(options);
511
- const errorLike = toErrorLike(options.error, statusCode);
512
- return buildRequestLogData(
513
- request,
514
- options.error || isErrorStatus(statusCode) ? "http_error" : "http_request",
515
- path,
516
- statusCode,
517
- Math.round(performance.now() - startTime),
518
- {
519
- ...fields,
520
- ...errorLike ? {
521
- error: errorLike.message,
522
- stack: errorLike.stack,
523
- code: errorLike.code,
524
- why: errorLike.why,
525
- fix: errorLike.fix,
526
- link: errorLike.link,
527
- details: errorLike.details
528
- } : {}
529
- }
530
- );
531
- }
532
- function createScopedFields(request) {
533
- return {
534
- ...workersLoggerState.env ?? {},
535
- ...workersLoggerState.customProps?.(request) ?? {}
536
- };
537
- }
538
- function initWorkersLogger(config = {}) {
539
- workersLoggerState = {
540
- env: config.env ? { ...config.env } : void 0,
541
- customProps: config.customProps
542
- };
543
- }
544
- function createWorkersLogger(request) {
545
- const startTime = performance.now();
546
- const requestLike = createRequestLike(request.method, request.url, request.headers);
547
- const path = extractPathname(request.url);
548
- const fields = createScopedFields(request);
549
- let emittedRecord;
550
- const logger = {
551
- set(extraFields) {
552
- Object.assign(fields, extraFields);
553
- return logger;
554
- },
555
- emit(options = {}) {
556
- if (emittedRecord) {
557
- return emittedRecord;
558
- }
559
- emittedRecord = createEmitPayload(requestLike, path, fields, startTime, options);
560
- writeConsole(
561
- emittedRecord.type === "http_error" ? "error" : "info",
562
- buildInfoLogMessage(
563
- requestLike.method,
564
- emittedRecord.statusCode,
565
- path,
566
- emittedRecord.responseTime
567
- ),
568
- emittedRecord
569
- );
570
- return emittedRecord;
571
- },
572
- debug(message, ...args) {
573
- emitScopedLog("debug", requestLike, path, fields, message, args);
574
- },
575
- info(message, ...args) {
576
- emitScopedLog("info", requestLike, path, fields, message, args);
577
- },
578
- warn(message, ...args) {
579
- emitScopedLog("warn", requestLike, path, fields, message, args);
580
- },
581
- warning(message, ...args) {
582
- emitScopedLog("warning", requestLike, path, fields, message, args);
583
- },
584
- error(message, ...args) {
585
- emitScopedLog("error", requestLike, path, fields, message, args);
586
- },
587
- success(message, ...args) {
588
- emitScopedLog("success", requestLike, path, fields, message, args);
589
- },
590
- critical(message, ...args) {
591
- emitScopedLog("critical", requestLike, path, fields, message, args);
592
- },
593
- table(message, data) {
594
- writeConsole("log", message, createScopedPayload(requestLike, path, fields, data));
595
- if (typeof console !== "undefined" && typeof console.table === "function" && data !== void 0) {
596
- console.table(data);
597
- }
598
- },
599
- createStructuredLog(groupId, initial) {
600
- return createStructuredLog(groupId, {
601
- initialFields: initial,
602
- resolveDefaultFields: () => ({
603
- method: requestLike.method,
604
- path,
605
- ...fields
606
- }),
607
- write: (payload, message) => {
608
- const writeLevel = payload.level === "warning" ? "warning" : payload.level;
609
- writeStructuredConsole(
610
- writeLevel === "table" ? "info" : writeLevel,
611
- message,
612
- payload
613
- );
614
- }
615
- });
616
- }
617
- };
618
- return logger;
619
- }
620
-
621
- export { createWorkersLogger, initWorkersLogger };
622
- //# sourceMappingURL=workers.mjs.map
623
- //# sourceMappingURL=workers.mjs.map
1
+ function T(e){return `${{GET:"\x1B[32m",POST:"\x1B[36m",PUT:"\x1B[33m",PATCH:"\x1B[34m",DELETE:"\x1B[31m"}[e.toUpperCase()]||""}${e}\x1B[0m`}function $(e){let t="\x1B[0m";return e>=500?`\x1B[31m${e}${t}`:e>=400?`\x1B[33m${e}${t}`:e>=300?`\x1B[36m${e}${t}`:e>=200?`\x1B[32m${e}${t}`:`\x1B[37m${e}${t}`}function P(e){return {GET:"\u2192",POST:"\u2191",PUT:"\u21D1",PATCH:"\u2197",DELETE:"\u2715"}[e.toUpperCase()]||"\u2022"}function H(e){let t="\x1B[0m";return e<100?`\x1B[32m${e}ms${t}`:e<300?`\x1B[33m${e}ms${t}`:e<1e3?`\x1B[31m${e}ms${t}`:`\x1B[41m\x1B[37m${e}ms${t}`}function J(e){let t={name:e.name,message:e.message};e.stack&&(t.stack=e.stack);let r=e;return r.cause!==void 0&&(t.cause=w(r.cause)),t}function w(e,t=new WeakSet){if(e instanceof Error)return J(e);if(typeof e=="function")return `[Function: ${e.name||"anonymous"}]`;if(typeof e=="symbol")return e.toString();if(e==null)return e;if(Array.isArray(e))return e.map(r=>w(r,t));if(typeof e=="object"){if(t.has(e))return "[Circular]";t.add(e);let r={};for(let[n,o]of Object.entries(e))r[n]=w(o,t);return t.delete(e),r}return e}function D(e){if(typeof e=="string")return e;if(e instanceof Error)return e.message||e.name;let t=w(e);if(typeof t=="string")return t;try{return JSON.stringify(t,null,2)??String(t)}catch{return String(t)}}var _="[REDACTED]",K=["password","passwd","pwd","secret","token","api_key","apikey","api_secret","authorization","auth","x-api-key","private_key","privatekey","access_token","refresh_token","client_secret","session","cookie","set-cookie","ssn","credit_card","card_number","cvv","cvc","otp","pin"],Y=["authorization","cookie","set-cookie","x-api-key","x-auth-token"],Q=[{type:"bearer",pattern:/\bBearer\s+(?:sk-[A-Za-z0-9]{20,}|pk_(?:live|test)_[A-Za-z0-9]{16,}|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|[A-Za-z0-9._~+/-]{20,})\b/g},{type:"jwt",pattern:/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b/g},{type:"api_key",pattern:/\b(?:sk-[A-Za-z0-9]{20,}|pk_(?:live|test)_[A-Za-z0-9]{16,})\b/g}],X=/\b(?:\d[ -]*?){16}\b/g;function W(e){let t=new Set,r=[];for(let n of e)typeof n!="string"||n.length===0||t.has(n)||(t.add(n),r.push(n));return r}function ee(e){let t=e.flags.includes("g")?e.flags:`${e.flags}g`;return new RegExp(e.source,t)}function te(e){if(!e||typeof e!="object"||Array.isArray(e))return false;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function re(e){return new Set(e.keys.map(t=>t.toLowerCase()))}function ne(e){return new Set([...Y,...e.keys.map(t=>t.toLowerCase())])}function oe(e){return e.split(".").filter(Boolean)}function E(e,t,r=0,n=0){if(r===e.length)return n===t.length;let o=e[r];if(o==="**"){if(r===e.length-1)return true;for(let i=n;i<=t.length;i+=1)if(E(e,t,r+1,i))return true;return false}return n>=t.length||o!=="*"&&o!==t[n]?false:E(e,t,r+1,n+1)}function ie(e,t){return e.length===0||t.paths.length===0?false:t.paths.some(r=>E(oe(r),e))}function se(e){let t=e.replace(/\D/g,"");if(t.length!==16)return false;let r=0,n=false;for(let o=t.length-1;o>=0;o-=1){let i=Number(t[o]);n&&(i*=2,i>9&&(i-=9)),r+=i,n=!n;}return r%10===0}function x(e,t,r){if(r||t.disablePatternScanning)return e;let n=e;for(let{type:o,pattern:i}of Q)n=n.replace(i,`[REDACTED:${o}]`);n=n.replace(X,o=>se(o)?"[REDACTED:card]":o);for(let o of t.patterns)n=n.replace(ee(o),"[REDACTED:pattern]");return n}function h(e,t,r,n){if(typeof e=="string")return x(e,t,!!n.skipPatternScanning);if(e==null||typeof e=="number"||typeof e=="boolean")return e;if(Array.isArray(e))return e.map((i,u)=>h(i,t,r,{path:[...n.path,String(u)],skipPatternScanning:n.skipPatternScanning}));if(!te(e))return e;let o={};for(let[i,u]of Object.entries(e)){let s=[...n.path,i];if(r.has(i.toLowerCase())||ie(s,t)){if(u==null||typeof u=="string"||typeof u=="number"||typeof u=="boolean"){o[i]=_;continue}o[i]=h(u,t,r,{path:s,skipPatternScanning:n.skipPatternScanning});continue}o[i]=h(u,t,r,{path:s,skipPatternScanning:n.skipPatternScanning});}return o}function f(e,t){return {keys:W([...K,...[],...t?.keys??[]]),paths:W([...[],...t?.paths??[]]),patterns:[...[],...t?.patterns??[]].filter(r=>r instanceof RegExp),disablePatternScanning:t?.disablePatternScanning??e?.disablePatternScanning??false}}function l(e,t,r={path:[]}){let n=w(e);return h(n,t,re(t),r)}function R(e,t){return typeof e=="string"?x(e,t,false):D(l(e,t))}function ue(e){if(!e)return {};if(e instanceof Headers){let t={};return e.forEach((r,n)=>{t[n]=r;}),t}if(typeof e.entries=="function"){let t={};for(let[r,n]of e.entries())t[r]=n;return t}return typeof e.get=="function"?{}:{...e}}function A(e,t){let r=ue(e),n=ne(t),o={};for(let[i,u]of Object.entries(r)){if(n.has(i.toLowerCase())){o[i]=_;continue}if(Array.isArray(u)){o[i]=u.map(s=>x(s,t,false));continue}o[i]=typeof u=="string"?x(u,t,false):u;}return o}function p(e,t){if(!e)return;if(e instanceof Headers||typeof e.get=="function")return e.get(t)??e.get(t.toLowerCase())??void 0;let r=e,n=r[t]??r[t.toLowerCase()];return Array.isArray(n)?n[0]:n}function ce(e){return e?e.split(",").map(t=>t.trim()).filter(Boolean).map(t=>t.match(/for="?(\[[^\]]+\]|[^;,\s"]+)/i)?.[1]?.replace(/^"|"$/g,"")??"").map(t=>t.replace(/^\[|\]$/g,"")).filter(Boolean):[]}function ae(e){return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function de(e){if(e.startsWith("[")){let t=e.indexOf("]");return t>=0?e.slice(1,t):e}return e.replace(/:\d+$/,"")}function fe(e){return /edg\//i.test(e)?"Edge":/opr\//i.test(e)||/opera/i.test(e)?"Opera":/chrome\//i.test(e)&&!/edg\//i.test(e)?"Chrome":/firefox\//i.test(e)?"Firefox":/safari\//i.test(e)&&!/chrome\//i.test(e)?"Safari":/curl\//i.test(e)?"curl":/postmanruntime/i.test(e)?"Postman":"Unknown"}function ge(e){return /windows/i.test(e)?"Windows":/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/mac os x|macintosh/i.test(e)?"macOS":/linux/i.test(e)?"Linux":"Unknown"}function le(e){return e?/bot|crawler|spider|curl|wget|postmanruntime/i.test(e)?"bot":/ipad|tablet/i.test(e)?"tablet":/mobi|iphone|android/i.test(e)?"mobile":"desktop":"unknown"}function S(e,t="/"){if(!e)return t;if(e.startsWith("http://")||e.startsWith("https://"))try{return new URL(e).pathname||t}catch{return t}if(e.startsWith("/")){let r=e.indexOf("?");return r>=0?e.slice(0,r):e}try{return new URL(e,"http://localhost").pathname||t}catch{return t}}function v(e,t,r){return {method:e,url:t,headers:r}}function z(e,t,r=f()){let n=t??S(e.url),o=(()=>{try{return new URL(e.url)}catch{try{return new URL(n,"http://localhost")}catch{return null}}})(),i=p(e.headers,"x-forwarded-host")??p(e.headers,"host")??o?.host??void 0,u=i?de(i):o?.hostname,s=i?.match(/:(\d+)$/)?.[1]??(o?.port||void 0),d=ae(p(e.headers,"x-forwarded-for")),c=ce(p(e.headers,"forwarded")),a=[p(e.headers,"cf-connecting-ip"),p(e.headers,"true-client-ip"),p(e.headers,"fly-client-ip"),p(e.headers,"x-real-ip"),p(e.headers,"x-client-ip"),d[0],c[0]].filter(k=>!!k),g=p(e.headers,"user-agent"),b=p(e.headers,"sec-ch-ua-platform")?.replace(/^"|"$/g,"")??void 0,L=le(g??"");return {hostname:u,ip:a[0],forwardedFor:[...d,...c].filter((k,G,I)=>I.indexOf(k)===G),protocol:p(e.headers,"x-forwarded-proto")??(o?.protocol?o.protocol.replace(/:$/,""):void 0),port:s,userAgent:g,origin:p(e.headers,"origin"),referer:p(e.headers,"referer"),acceptLanguage:p(e.headers,"accept-language"),headers:A(e.headers,r),client:{ip:a[0],hostname:u,browser:g?fe(g):void 0,os:g?ge(g):void 0,deviceType:L,platform:b,isMobile:L==="mobile"}}}function O(e,t,r,n,o,i={},u=f()){return {type:t,method:e.method,url:r,statusCode:n,responseTime:o,...z(e,r,u),...i}}function F(e,t,r,n){let o=T(e),i=$(t),u=H(n),s=P(e);return `${o} ${s} ${i} ${r} ${u}`}function M(e,t){if(e==null)return t===void 0?void 0:{statusCode:t,message:`HTTP ${t}`};if(e instanceof Error){let r=e;return {status:r.status,statusCode:r.statusCode??t,code:r.code,message:e.message,stack:e.stack,why:r.why,fix:r.fix,link:r.link,details:r.details,cause:r.cause}}if(typeof e=="object"){let r=e;return {status:typeof r.status=="number"?r.status:void 0,statusCode:typeof r.statusCode=="number"?r.statusCode:t,code:typeof r.code=="string"||typeof r.code=="number"?r.code:void 0,message:typeof r.message=="string"?r.message:`HTTP ${t??500}`,stack:typeof r.stack=="string"?r.stack:void 0,why:typeof r.why=="string"?r.why:void 0,fix:typeof r.fix=="string"?r.fix:void 0,link:typeof r.link=="string"?r.link:void 0,details:r.details!==null&&typeof r.details=="object"&&!Array.isArray(r.details)?r.details:void 0,cause:r.cause}}return {statusCode:t,message:typeof e=="string"?e:`HTTP ${t??500}`}}function j(e){return e>=400}function N(e,t){if(typeof e=="string")return t.length===0?void 0:t.length===1?t[0]:t;let r=[e,...t];return r.length===1?r[0]:r}function Z(e){if(!(!e||typeof e!="object"||Array.isArray(e)))return e}function pe(e,t){if(e==null)return t===void 0||t<400?void 0:{message:`HTTP ${t}`,code:t,type:"HttpError"};if(e instanceof Error){let r=e;return {message:e.message,code:r.code,type:r.type??e.name??e.constructor?.name,stack:e.stack,why:r.why,fix:r.fix,link:r.link,details:Z(r.details),cause:r.cause}}if(typeof e=="object"){let r=e,n=typeof r.statusCode=="number"?r.statusCode:typeof r.status=="number"?r.status:t;return {message:typeof r.message=="string"?r.message:`HTTP ${n??500}`,code:typeof r.code=="string"||typeof r.code=="number"?r.code:n,type:typeof r.type=="string"?r.type:typeof r.name=="string"?r.name:"Error",stack:typeof r.stack=="string"?r.stack:void 0,why:typeof r.why=="string"?r.why:void 0,fix:typeof r.fix=="string"?r.fix:void 0,link:typeof r.link=="string"?r.link:void 0,details:Z(r.details),cause:r.cause}}return {message:String(e),code:t,type:typeof e}}function me(e){return e.response&&typeof e.response.status=="number"?e.response.status:typeof e.status=="number"?e.status:e.error&&typeof e.error=="object"&&e.error!==null&&typeof e.error.statusCode=="number"?e.error.statusCode:e.error&&typeof e.error=="object"&&e.error!==null&&typeof e.error.status=="number"?e.error.status:e.error?500:void 0}function q(e,t){let r=t.redact??f(),n=performance.now(),o=l(t.initialFields??{},r),i=[],u;t.onCreate?.();let s=(c,a,g)=>(i.push({level:c,message:R(a,r),timestamp:new Date().toISOString(),...N(a,g)===void 0?{}:{data:l(N(a,g),r)}}),d),d={set(c){return Object.assign(o,l(c,r)),d},debug(c,...a){return s("debug",c,a)},info(c,...a){return s("info",c,a)},warn(c,...a){return s("warn",c,a)},warning(c,...a){return s("warning",c,a)},error(c,...a){return s("error",c,a)},success(c,...a){return s("success",c,a)},critical(c,...a){return s("critical",c,a)},table(c,a){return s("table",c,a===void 0?[]:[a])},emit(c={}){if(u)return u;let a=l(t.resolveDefaultFields?.()??{},r),g=me(c),b=l(pe(c.error,g),r),L=c.level??(b?"error":"info"),k=l({...a,...o,groupId:e,timestamp:new Date().toISOString(),level:L,duration:Math.round(performance.now()-n),...typeof g=="number"?{status:g}:{},...i.length>0?{events:[...i]}:{},...b?{error:b}:{}},r);return t.write(k,R(c.message??"structured_log",r)),u=k,t.onEmit?.(k),k}};return d}var m={};function B(e){return R(e,m.redact??f())}function ke(e,t){if(typeof e=="string")return t.length===0?void 0:t.length===1?t[0]:t;let r=[e,...t];return r.length===1?r[0]:r}function V(e){switch(e){case "debug":return "debug";case "warn":case "warning":return "warn";case "error":case "critical":return "error";case "success":return "log";default:return "info"}}function C(e,t,r){if(typeof console>"u")return;let n=console[e]??console.info;if(r===void 0){n.call(console,t);return}n.call(console,t,r);}function ye(e,t,r){C(V(e),R(t,m.redact??f()),l(r,m.redact??f()));}function we(e,t){return {method:e.method,url:t,...z(e,t,m.redact??f())}}function U(e,t,r,n){return l({request:we(e,t),...r,...n===void 0?{}:{data:n}},m.redact??f())}function y(e,t,r,n,o,i){C(V(e),B(o),U(t,r,n,ke(o,i)));}function Re(e){return e.response?.status??e.status??(e.error?500:200)}function be(e,t,r,n,o){let i=Re(o),u=M(o.error,i);return O(e,o.error||j(i)?"http_error":"http_request",t,i,Math.round(performance.now()-n),{...r,...u?{error:u.message,stack:u.stack,code:u.code,why:u.why,fix:u.fix,link:u.link,details:u.details}:{}},m.redact??f())}function Le(e){return {...l(m.env??{},m.redact??f()),...l(m.customProps?.(e)??{},m.redact??f())}}function he(e={}){m={env:e.env?{...e.env}:void 0,customProps:e.customProps,redact:f(void 0,e.redact)};}function xe(e){let t=performance.now(),r=v(e.method,e.url,e.headers),n=S(e.url),o=Le(e),i,u={set(s){return Object.assign(o,l(s,m.redact??f())),u},emit(s={}){return i||(i=be(r,n,o,t,s),C(i.type==="http_error"?"error":"info",F(r.method,i.statusCode,n,i.responseTime),i),i)},debug(s,...d){y("debug",r,n,o,s,d);},info(s,...d){y("info",r,n,o,s,d);},warn(s,...d){y("warn",r,n,o,s,d);},warning(s,...d){y("warning",r,n,o,s,d);},error(s,...d){y("error",r,n,o,s,d);},success(s,...d){y("success",r,n,o,s,d);},critical(s,...d){y("critical",r,n,o,s,d);},table(s,d){C("log",B(s),U(r,n,o,d)),typeof console<"u"&&typeof console.table=="function"&&d!==void 0&&console.table(l(d,m.redact??f()));},createStructuredLog(s,d){return q(s,{initialFields:d,resolveDefaultFields:()=>({method:r.method,path:n,...o}),write:(c,a)=>{let g=c.level==="warning"?"warning":c.level;ye(g==="table"?"info":g,a,c);},redact:m.redact??f()})}};return u}
2
+ export{xe as createWorkersLogger,he as initWorkersLogger};