@blyp/core 0.1.2
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.
- package/README.md +533 -0
- package/dist/client.js +1132 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +1125 -0
- package/dist/client.mjs.map +1 -0
- package/dist/connectors/betterstack/index.d.ts +7 -0
- package/dist/connectors/betterstack/sender.d.ts +5 -0
- package/dist/connectors/betterstack.js +1508 -0
- package/dist/connectors/betterstack.js.map +1 -0
- package/dist/connectors/betterstack.mjs +1483 -0
- package/dist/connectors/betterstack.mjs.map +1 -0
- package/dist/connectors/otlp/index.d.ts +5 -0
- package/dist/connectors/otlp/sender.d.ts +7 -0
- package/dist/connectors/otlp.js +1347 -0
- package/dist/connectors/otlp.js.map +1 -0
- package/dist/connectors/otlp.mjs +1344 -0
- package/dist/connectors/otlp.mjs.map +1 -0
- package/dist/connectors/posthog/index.d.ts +7 -0
- package/dist/connectors/posthog/sender.d.ts +11 -0
- package/dist/connectors/posthog.js +1503 -0
- package/dist/connectors/posthog.js.map +1 -0
- package/dist/connectors/posthog.mjs +1498 -0
- package/dist/connectors/posthog.mjs.map +1 -0
- package/dist/connectors/sentry/index.d.ts +5 -0
- package/dist/connectors/sentry/sender.d.ts +5 -0
- package/dist/connectors/sentry.js +1356 -0
- package/dist/connectors/sentry.js.map +1 -0
- package/dist/connectors/sentry.mjs +1333 -0
- package/dist/connectors/sentry.mjs.map +1 -0
- package/dist/connectors/shared.d.ts +8 -0
- package/dist/core/colors.d.ts +30 -0
- package/dist/core/config.d.ts +18 -0
- package/dist/core/errors.d.ts +8 -0
- package/dist/core/file-logger.d.ts +19 -0
- package/dist/core/helpers.d.ts +5 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/log-reader.d.ts +5 -0
- package/dist/core/log-record.d.ts +14 -0
- package/dist/core/logger.d.ts +20 -0
- package/dist/core/primary-sink.d.ts +10 -0
- package/dist/core/runtime.d.ts +4 -0
- package/dist/core/sinks/database-primary-sink.d.ts +25 -0
- package/dist/core/sinks/file-primary-sink.d.ts +14 -0
- package/dist/core/structured-log.d.ts +3 -0
- package/dist/database/adapters/drizzle.d.ts +7 -0
- package/dist/database/adapters/prisma.d.ts +9 -0
- package/dist/database/helpers.d.ts +5 -0
- package/dist/database/index.d.ts +3 -0
- package/dist/database.js +24 -0
- package/dist/database.js.map +1 -0
- package/dist/database.mjs +21 -0
- package/dist/database.mjs.map +1 -0
- package/dist/elysia.js +4180 -0
- package/dist/elysia.js.map +1 -0
- package/dist/elysia.mjs +4152 -0
- package/dist/elysia.mjs.map +1 -0
- package/dist/expo.js +719 -0
- package/dist/expo.js.map +1 -0
- package/dist/expo.mjs +717 -0
- package/dist/expo.mjs.map +1 -0
- package/dist/express.js +4170 -0
- package/dist/express.js.map +1 -0
- package/dist/express.mjs +4141 -0
- package/dist/express.mjs.map +1 -0
- package/dist/fastify.js +4187 -0
- package/dist/fastify.js.map +1 -0
- package/dist/fastify.mjs +4158 -0
- package/dist/fastify.mjs.map +1 -0
- package/dist/frameworks/client/index.d.ts +3 -0
- package/dist/frameworks/client/logger.d.ts +4 -0
- package/dist/frameworks/elysia/index.d.ts +2 -0
- package/dist/frameworks/elysia/logger.d.ts +35 -0
- package/dist/frameworks/expo/index.d.ts +2 -0
- package/dist/frameworks/expo/logger.d.ts +3 -0
- package/dist/frameworks/expo/network.d.ts +7 -0
- package/dist/frameworks/express/index.d.ts +2 -0
- package/dist/frameworks/express/logger.d.ts +5 -0
- package/dist/frameworks/fastify/index.d.ts +2 -0
- package/dist/frameworks/fastify/logger.d.ts +4 -0
- package/dist/frameworks/hono/index.d.ts +2 -0
- package/dist/frameworks/hono/logger.d.ts +4 -0
- package/dist/frameworks/nestjs/constants.d.ts +2 -0
- package/dist/frameworks/nestjs/filter.d.ts +8 -0
- package/dist/frameworks/nestjs/helpers.d.ts +25 -0
- package/dist/frameworks/nestjs/index.d.ts +3 -0
- package/dist/frameworks/nestjs/interceptor.d.ts +8 -0
- package/dist/frameworks/nestjs/logger.d.ts +6 -0
- package/dist/frameworks/nestjs/middleware.d.ts +8 -0
- package/dist/frameworks/nestjs/module.d.ts +5 -0
- package/dist/frameworks/nextjs/index.d.ts +2 -0
- package/dist/frameworks/nextjs/logger.d.ts +3 -0
- package/dist/frameworks/shared/http.d.ts +10 -0
- package/dist/frameworks/shared/index.d.ts +5 -0
- package/dist/frameworks/shared/logger.d.ts +35 -0
- package/dist/frameworks/shared/request-context.d.ts +12 -0
- package/dist/frameworks/shared/request-logger.d.ts +4 -0
- package/dist/frameworks/standalone/index.d.ts +3 -0
- package/dist/frameworks/standalone/logger.d.ts +5 -0
- package/dist/frameworks/sveltekit/index.d.ts +2 -0
- package/dist/frameworks/sveltekit/logger.d.ts +3 -0
- package/dist/frameworks/tanstack-start/index.d.ts +2 -0
- package/dist/frameworks/tanstack-start/logger.d.ts +3 -0
- package/dist/frameworks/workers/index.d.ts +2 -0
- package/dist/frameworks/workers/logger.d.ts +3 -0
- package/dist/hono.js +4156 -0
- package/dist/hono.js.map +1 -0
- package/dist/hono.mjs +4128 -0
- package/dist/hono.mjs.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +6581 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +6494 -0
- package/dist/index.mjs.map +1 -0
- package/dist/nestjs.js +4649 -0
- package/dist/nestjs.js.map +1 -0
- package/dist/nestjs.mjs +4621 -0
- package/dist/nestjs.mjs.map +1 -0
- package/dist/nextjs.js +4183 -0
- package/dist/nextjs.js.map +1 -0
- package/dist/nextjs.mjs +4155 -0
- package/dist/nextjs.mjs.map +1 -0
- package/dist/shared/client-log.d.ts +14 -0
- package/dist/shared/errors.d.ts +296 -0
- package/dist/shared/log-value.d.ts +3 -0
- package/dist/shared/once.d.ts +4 -0
- package/dist/shared/remote-delivery.d.ts +6 -0
- package/dist/shared/validation.d.ts +7 -0
- package/dist/standalone.js +3353 -0
- package/dist/standalone.js.map +1 -0
- package/dist/standalone.mjs +3324 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/sveltekit.js +4182 -0
- package/dist/sveltekit.js.map +1 -0
- package/dist/sveltekit.mjs +4154 -0
- package/dist/sveltekit.mjs.map +1 -0
- package/dist/tanstack-start.js +4182 -0
- package/dist/tanstack-start.js.map +1 -0
- package/dist/tanstack-start.mjs +4154 -0
- package/dist/tanstack-start.mjs.map +1 -0
- package/dist/types/connectors/betterstack.d.ts +54 -0
- package/dist/types/connectors/mode.d.ts +2 -0
- package/dist/types/connectors/otlp.d.ts +50 -0
- package/dist/types/connectors/posthog.d.ts +75 -0
- package/dist/types/connectors/sentry.d.ts +48 -0
- package/dist/types/core/config.d.ts +152 -0
- package/dist/types/core/errors.d.ts +16 -0
- package/dist/types/core/file-logger.d.ts +29 -0
- package/dist/types/core/helpers.d.ts +8 -0
- package/dist/types/core/index.d.ts +8 -0
- package/dist/types/core/log-reader.d.ts +4 -0
- package/dist/types/core/log-record.d.ts +1 -0
- package/dist/types/core/logger.d.ts +40 -0
- package/dist/types/core/structured-log.d.ts +57 -0
- package/dist/types/database.d.ts +73 -0
- package/dist/types/frameworks/client.d.ts +32 -0
- package/dist/types/frameworks/elysia.d.ts +33 -0
- package/dist/types/frameworks/expo.d.ts +44 -0
- package/dist/types/frameworks/express.d.ts +24 -0
- package/dist/types/frameworks/fastify.d.ts +22 -0
- package/dist/types/frameworks/hono.d.ts +13 -0
- package/dist/types/frameworks/http.d.ts +47 -0
- package/dist/types/frameworks/nestjs.d.ts +37 -0
- package/dist/types/frameworks/nextjs.d.ts +27 -0
- package/dist/types/frameworks/request-context.d.ts +8 -0
- package/dist/types/frameworks/request-logger.d.ts +8 -0
- package/dist/types/frameworks/shared.d.ts +154 -0
- package/dist/types/frameworks/standalone.d.ts +64 -0
- package/dist/types/frameworks/sveltekit.d.ts +37 -0
- package/dist/types/frameworks/tanstack-start.d.ts +28 -0
- package/dist/types/frameworks/workers.d.ts +33 -0
- package/dist/types/shared/client-log.d.ts +91 -0
- package/dist/types/shared/errors.d.ts +75 -0
- package/dist/types/shared/index.d.ts +4 -0
- package/dist/types/shared/once.d.ts +2 -0
- package/dist/types/shared/remote-delivery.d.ts +32 -0
- package/dist/workers.js +626 -0
- package/dist/workers.js.map +1 -0
- package/dist/workers.mjs +623 -0
- package/dist/workers.mjs.map +1 -0
- package/exports/client.js +3 -0
- package/exports/client.mjs +3 -0
- package/exports/connectors/betterstack.js +1 -0
- package/exports/connectors/betterstack.mjs +1 -0
- package/exports/connectors/otlp.js +1 -0
- package/exports/connectors/otlp.mjs +1 -0
- package/exports/connectors/posthog.js +1 -0
- package/exports/connectors/posthog.mjs +1 -0
- package/exports/connectors/sentry.js +1 -0
- package/exports/connectors/sentry.mjs +1 -0
- package/exports/database.js +1 -0
- package/exports/database.mjs +1 -0
- package/exports/expo.js +1 -0
- package/exports/expo.mjs +1 -0
- package/exports/frameworks/elysia.js +1 -0
- package/exports/frameworks/elysia.mjs +1 -0
- package/exports/frameworks/express.js +1 -0
- package/exports/frameworks/express.mjs +1 -0
- package/exports/frameworks/fastify.js +1 -0
- package/exports/frameworks/fastify.mjs +1 -0
- package/exports/frameworks/hono.js +1 -0
- package/exports/frameworks/hono.mjs +1 -0
- package/exports/frameworks/nestjs.js +1 -0
- package/exports/frameworks/nestjs.mjs +1 -0
- package/exports/frameworks/nextjs.js +1 -0
- package/exports/frameworks/nextjs.mjs +1 -0
- package/exports/frameworks/standalone.js +1 -0
- package/exports/frameworks/standalone.mjs +1 -0
- package/exports/frameworks/sveltekit.js +1 -0
- package/exports/frameworks/sveltekit.mjs +1 -0
- package/exports/frameworks/tanstack-start.js +1 -0
- package/exports/frameworks/tanstack-start.mjs +1 -0
- package/exports/workers.js +1 -0
- package/exports/workers.mjs +1 -0
- package/package.json +291 -0
- package/types/client.d.ts +34 -0
- package/types/connectors/betterstack.d.ts +1 -0
- package/types/connectors/otlp.d.ts +1 -0
- package/types/connectors/posthog.d.ts +1 -0
- package/types/connectors/sentry.d.ts +1 -0
- package/types/database.d.ts +1 -0
- package/types/expo.d.ts +17 -0
- package/types/frameworks/client.d.ts +160 -0
- package/types/frameworks/elysia.d.ts +1 -0
- package/types/frameworks/expo.d.ts +50 -0
- package/types/frameworks/express.d.ts +1 -0
- package/types/frameworks/fastify.d.ts +1 -0
- package/types/frameworks/hono.d.ts +1 -0
- package/types/frameworks/nestjs.d.ts +1 -0
- package/types/frameworks/nextjs.d.ts +1 -0
- package/types/frameworks/standalone.d.ts +1 -0
- package/types/frameworks/sveltekit.d.ts +1 -0
- package/types/frameworks/tanstack-start.d.ts +1 -0
- package/types/frameworks/workers.d.ts +115 -0
- package/types/index.d.ts +1 -0
- package/types/workers.d.ts +13 -0
package/dist/workers.js
ADDED
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/core/colors.ts
|
|
4
|
+
function getMethodColor(method) {
|
|
5
|
+
const colors = {
|
|
6
|
+
GET: "\x1B[32m",
|
|
7
|
+
POST: "\x1B[36m",
|
|
8
|
+
PUT: "\x1B[33m",
|
|
9
|
+
PATCH: "\x1B[34m",
|
|
10
|
+
DELETE: "\x1B[31m"
|
|
11
|
+
};
|
|
12
|
+
const reset = "\x1B[0m";
|
|
13
|
+
return `${colors[method.toUpperCase()] || ""}${method}${reset}`;
|
|
14
|
+
}
|
|
15
|
+
function getStatusColor(statusCode) {
|
|
16
|
+
const reset = "\x1B[0m";
|
|
17
|
+
if (statusCode >= 500) return `\x1B[31m${statusCode}${reset}`;
|
|
18
|
+
if (statusCode >= 400) return `\x1B[33m${statusCode}${reset}`;
|
|
19
|
+
if (statusCode >= 300) return `\x1B[36m${statusCode}${reset}`;
|
|
20
|
+
if (statusCode >= 200) return `\x1B[32m${statusCode}${reset}`;
|
|
21
|
+
return `\x1B[37m${statusCode}${reset}`;
|
|
22
|
+
}
|
|
23
|
+
function getArrowForMethod(method) {
|
|
24
|
+
const arrows = {
|
|
25
|
+
GET: "\u2192",
|
|
26
|
+
POST: "\u2191",
|
|
27
|
+
PUT: "\u21D1",
|
|
28
|
+
PATCH: "\u2197",
|
|
29
|
+
DELETE: "\u2715"
|
|
30
|
+
};
|
|
31
|
+
return arrows[method.toUpperCase()] || "\u2022";
|
|
32
|
+
}
|
|
33
|
+
function getResponseTimeColor(ms) {
|
|
34
|
+
const reset = "\x1B[0m";
|
|
35
|
+
if (ms < 100) return `\x1B[32m${ms}ms${reset}`;
|
|
36
|
+
if (ms < 300) return `\x1B[33m${ms}ms${reset}`;
|
|
37
|
+
if (ms < 1e3) return `\x1B[31m${ms}ms${reset}`;
|
|
38
|
+
return `\x1B[41m\x1B[37m${ms}ms${reset}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/frameworks/shared/http.ts
|
|
42
|
+
function getHeaderValue(headers, name) {
|
|
43
|
+
if (!headers) {
|
|
44
|
+
return void 0;
|
|
45
|
+
}
|
|
46
|
+
if (headers instanceof Headers) {
|
|
47
|
+
return headers.get(name) ?? headers.get(name.toLowerCase()) ?? void 0;
|
|
48
|
+
}
|
|
49
|
+
if (typeof headers.get === "function") {
|
|
50
|
+
const direct2 = headers.get(name);
|
|
51
|
+
return direct2 ?? headers.get(name.toLowerCase()) ?? void 0;
|
|
52
|
+
}
|
|
53
|
+
const record = headers;
|
|
54
|
+
const direct = record[name] ?? record[name.toLowerCase()];
|
|
55
|
+
if (Array.isArray(direct)) {
|
|
56
|
+
return direct[0];
|
|
57
|
+
}
|
|
58
|
+
return direct;
|
|
59
|
+
}
|
|
60
|
+
function parseForwardedHeader(value) {
|
|
61
|
+
if (!value) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
return value.split(",").map((entry) => entry.trim()).filter(Boolean).map((entry) => {
|
|
65
|
+
const match = entry.match(/for="?(\[[^\]]+\]|[^;,\s"]+)/i);
|
|
66
|
+
return match?.[1]?.replace(/^"|"$/g, "") ?? "";
|
|
67
|
+
}).map((entry) => entry.replace(/^\[|\]$/g, "")).filter(Boolean);
|
|
68
|
+
}
|
|
69
|
+
function parseForwardedFor(value) {
|
|
70
|
+
if (!value) {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
return value.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
74
|
+
}
|
|
75
|
+
function stripPort(host) {
|
|
76
|
+
if (host.startsWith("[")) {
|
|
77
|
+
const endIndex = host.indexOf("]");
|
|
78
|
+
return endIndex >= 0 ? host.slice(1, endIndex) : host;
|
|
79
|
+
}
|
|
80
|
+
return host.replace(/:\d+$/, "");
|
|
81
|
+
}
|
|
82
|
+
function detectBrowser(userAgent) {
|
|
83
|
+
if (/edg\//i.test(userAgent)) return "Edge";
|
|
84
|
+
if (/opr\//i.test(userAgent) || /opera/i.test(userAgent)) return "Opera";
|
|
85
|
+
if (/chrome\//i.test(userAgent) && !/edg\//i.test(userAgent)) return "Chrome";
|
|
86
|
+
if (/firefox\//i.test(userAgent)) return "Firefox";
|
|
87
|
+
if (/safari\//i.test(userAgent) && !/chrome\//i.test(userAgent)) return "Safari";
|
|
88
|
+
if (/curl\//i.test(userAgent)) return "curl";
|
|
89
|
+
if (/postmanruntime/i.test(userAgent)) return "Postman";
|
|
90
|
+
return "Unknown";
|
|
91
|
+
}
|
|
92
|
+
function detectOperatingSystem(userAgent) {
|
|
93
|
+
if (/windows/i.test(userAgent)) return "Windows";
|
|
94
|
+
if (/android/i.test(userAgent)) return "Android";
|
|
95
|
+
if (/iphone|ipad|ipod/i.test(userAgent)) return "iOS";
|
|
96
|
+
if (/mac os x|macintosh/i.test(userAgent)) return "macOS";
|
|
97
|
+
if (/linux/i.test(userAgent)) return "Linux";
|
|
98
|
+
return "Unknown";
|
|
99
|
+
}
|
|
100
|
+
function detectDeviceType(userAgent) {
|
|
101
|
+
if (!userAgent) return "unknown";
|
|
102
|
+
if (/bot|crawler|spider|curl|wget|postmanruntime/i.test(userAgent)) return "bot";
|
|
103
|
+
if (/ipad|tablet/i.test(userAgent)) return "tablet";
|
|
104
|
+
if (/mobi|iphone|android/i.test(userAgent)) return "mobile";
|
|
105
|
+
return "desktop";
|
|
106
|
+
}
|
|
107
|
+
function extractPathname(requestUrl, fallbackPath = "/") {
|
|
108
|
+
if (!requestUrl) {
|
|
109
|
+
return fallbackPath;
|
|
110
|
+
}
|
|
111
|
+
if (requestUrl.startsWith("http://") || requestUrl.startsWith("https://")) {
|
|
112
|
+
try {
|
|
113
|
+
return new URL(requestUrl).pathname || fallbackPath;
|
|
114
|
+
} catch {
|
|
115
|
+
return fallbackPath;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (requestUrl.startsWith("/")) {
|
|
119
|
+
const queryIndex = requestUrl.indexOf("?");
|
|
120
|
+
return queryIndex >= 0 ? requestUrl.slice(0, queryIndex) : requestUrl;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
return new URL(requestUrl, "http://localhost").pathname || fallbackPath;
|
|
124
|
+
} catch {
|
|
125
|
+
return fallbackPath;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function createRequestLike(method, url, headers) {
|
|
129
|
+
return { method, url, headers };
|
|
130
|
+
}
|
|
131
|
+
function buildClientDetails(request, fallbackPath) {
|
|
132
|
+
const pathname = fallbackPath ?? extractPathname(request.url);
|
|
133
|
+
const urlObject = (() => {
|
|
134
|
+
try {
|
|
135
|
+
return new URL(request.url);
|
|
136
|
+
} catch {
|
|
137
|
+
try {
|
|
138
|
+
return new URL(pathname, "http://localhost");
|
|
139
|
+
} catch {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
})();
|
|
144
|
+
const hostHeader = getHeaderValue(request.headers, "x-forwarded-host") ?? getHeaderValue(request.headers, "host") ?? urlObject?.host ?? void 0;
|
|
145
|
+
const hostname = hostHeader ? stripPort(hostHeader) : urlObject?.hostname;
|
|
146
|
+
const port = hostHeader?.match(/:(\d+)$/)?.[1] ?? (urlObject?.port || void 0);
|
|
147
|
+
const xForwardedFor = parseForwardedFor(getHeaderValue(request.headers, "x-forwarded-for"));
|
|
148
|
+
const forwardedFor = parseForwardedHeader(getHeaderValue(request.headers, "forwarded"));
|
|
149
|
+
const ipCandidates = [
|
|
150
|
+
getHeaderValue(request.headers, "cf-connecting-ip"),
|
|
151
|
+
getHeaderValue(request.headers, "true-client-ip"),
|
|
152
|
+
getHeaderValue(request.headers, "fly-client-ip"),
|
|
153
|
+
getHeaderValue(request.headers, "x-real-ip"),
|
|
154
|
+
getHeaderValue(request.headers, "x-client-ip"),
|
|
155
|
+
xForwardedFor[0],
|
|
156
|
+
forwardedFor[0]
|
|
157
|
+
].filter((value) => Boolean(value));
|
|
158
|
+
const userAgent = getHeaderValue(request.headers, "user-agent");
|
|
159
|
+
const platform = getHeaderValue(request.headers, "sec-ch-ua-platform")?.replace(/^"|"$/g, "") ?? void 0;
|
|
160
|
+
const deviceType = detectDeviceType(userAgent ?? "");
|
|
161
|
+
return {
|
|
162
|
+
hostname,
|
|
163
|
+
ip: ipCandidates[0],
|
|
164
|
+
forwardedFor: [...xForwardedFor, ...forwardedFor].filter((value, index, values) => {
|
|
165
|
+
return values.indexOf(value) === index;
|
|
166
|
+
}),
|
|
167
|
+
protocol: getHeaderValue(request.headers, "x-forwarded-proto") ?? (urlObject?.protocol ? urlObject.protocol.replace(/:$/, "") : void 0),
|
|
168
|
+
port,
|
|
169
|
+
userAgent,
|
|
170
|
+
origin: getHeaderValue(request.headers, "origin"),
|
|
171
|
+
referer: getHeaderValue(request.headers, "referer"),
|
|
172
|
+
acceptLanguage: getHeaderValue(request.headers, "accept-language"),
|
|
173
|
+
client: {
|
|
174
|
+
ip: ipCandidates[0],
|
|
175
|
+
hostname,
|
|
176
|
+
browser: userAgent ? detectBrowser(userAgent) : void 0,
|
|
177
|
+
os: userAgent ? detectOperatingSystem(userAgent) : void 0,
|
|
178
|
+
deviceType,
|
|
179
|
+
platform,
|
|
180
|
+
isMobile: deviceType === "mobile"
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function buildRequestLogData(request, type, path, statusCode, responseTime, extra = {}) {
|
|
185
|
+
return {
|
|
186
|
+
type,
|
|
187
|
+
method: request.method,
|
|
188
|
+
url: path,
|
|
189
|
+
statusCode,
|
|
190
|
+
responseTime,
|
|
191
|
+
...buildClientDetails(request, path),
|
|
192
|
+
...extra
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function buildInfoLogMessage(method, statusCode, url, responseTime) {
|
|
196
|
+
const methodColor = getMethodColor(method);
|
|
197
|
+
const statusColor = getStatusColor(statusCode);
|
|
198
|
+
const timeColor = getResponseTimeColor(responseTime);
|
|
199
|
+
const arrow = getArrowForMethod(method);
|
|
200
|
+
return `${methodColor} ${arrow} ${statusColor} ${url} ${timeColor}`;
|
|
201
|
+
}
|
|
202
|
+
function toErrorLike(error, fallbackStatusCode) {
|
|
203
|
+
if (error === void 0 || error === null) {
|
|
204
|
+
return fallbackStatusCode === void 0 ? void 0 : { statusCode: fallbackStatusCode, message: `HTTP ${fallbackStatusCode}` };
|
|
205
|
+
}
|
|
206
|
+
if (error instanceof Error) {
|
|
207
|
+
const errorWithStatus = error;
|
|
208
|
+
return {
|
|
209
|
+
status: errorWithStatus.status,
|
|
210
|
+
statusCode: errorWithStatus.statusCode ?? fallbackStatusCode,
|
|
211
|
+
code: errorWithStatus.code,
|
|
212
|
+
message: error.message,
|
|
213
|
+
stack: error.stack,
|
|
214
|
+
why: errorWithStatus.why,
|
|
215
|
+
fix: errorWithStatus.fix,
|
|
216
|
+
link: errorWithStatus.link,
|
|
217
|
+
details: errorWithStatus.details,
|
|
218
|
+
cause: errorWithStatus.cause
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (typeof error === "object") {
|
|
222
|
+
const record = error;
|
|
223
|
+
return {
|
|
224
|
+
status: typeof record.status === "number" ? record.status : void 0,
|
|
225
|
+
statusCode: typeof record.statusCode === "number" ? record.statusCode : fallbackStatusCode,
|
|
226
|
+
code: typeof record.code === "string" || typeof record.code === "number" ? record.code : void 0,
|
|
227
|
+
message: typeof record.message === "string" ? record.message : `HTTP ${fallbackStatusCode ?? 500}`,
|
|
228
|
+
stack: typeof record.stack === "string" ? record.stack : void 0,
|
|
229
|
+
why: typeof record.why === "string" ? record.why : void 0,
|
|
230
|
+
fix: typeof record.fix === "string" ? record.fix : void 0,
|
|
231
|
+
link: typeof record.link === "string" ? record.link : void 0,
|
|
232
|
+
details: record.details !== null && typeof record.details === "object" && !Array.isArray(record.details) ? record.details : void 0,
|
|
233
|
+
cause: record.cause
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
statusCode: fallbackStatusCode,
|
|
238
|
+
message: typeof error === "string" ? error : `HTTP ${fallbackStatusCode ?? 500}`
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function isErrorStatus(statusCode) {
|
|
242
|
+
return statusCode >= 400;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// src/core/structured-log.ts
|
|
246
|
+
function serializeMessage(message) {
|
|
247
|
+
if (typeof message === "string") {
|
|
248
|
+
return message;
|
|
249
|
+
}
|
|
250
|
+
if (message !== null && typeof message === "object") {
|
|
251
|
+
try {
|
|
252
|
+
return JSON.stringify(
|
|
253
|
+
message,
|
|
254
|
+
(_key, value) => {
|
|
255
|
+
if (typeof value === "function") {
|
|
256
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
257
|
+
}
|
|
258
|
+
if (value === void 0) {
|
|
259
|
+
return "[undefined]";
|
|
260
|
+
}
|
|
261
|
+
if (typeof value === "symbol") {
|
|
262
|
+
return value.toString();
|
|
263
|
+
}
|
|
264
|
+
return value;
|
|
265
|
+
},
|
|
266
|
+
2
|
|
267
|
+
);
|
|
268
|
+
} catch {
|
|
269
|
+
return "[Object]";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return String(message);
|
|
273
|
+
}
|
|
274
|
+
function normalizeEventData(message, args) {
|
|
275
|
+
if (typeof message === "string") {
|
|
276
|
+
if (args.length === 0) {
|
|
277
|
+
return void 0;
|
|
278
|
+
}
|
|
279
|
+
return args.length === 1 ? args[0] : args;
|
|
280
|
+
}
|
|
281
|
+
const values = [message, ...args];
|
|
282
|
+
return values.length === 1 ? values[0] : values;
|
|
283
|
+
}
|
|
284
|
+
function normalizeDetails(value) {
|
|
285
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
286
|
+
return void 0;
|
|
287
|
+
}
|
|
288
|
+
return value;
|
|
289
|
+
}
|
|
290
|
+
function normalizeError(error, fallbackStatus) {
|
|
291
|
+
if (error === void 0 || error === null) {
|
|
292
|
+
return fallbackStatus === void 0 || fallbackStatus < 400 ? void 0 : {
|
|
293
|
+
message: `HTTP ${fallbackStatus}`,
|
|
294
|
+
code: fallbackStatus,
|
|
295
|
+
type: "HttpError"
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (error instanceof Error) {
|
|
299
|
+
const errorLike = error;
|
|
300
|
+
return {
|
|
301
|
+
message: error.message,
|
|
302
|
+
code: errorLike.code,
|
|
303
|
+
type: errorLike.type ?? error.name ?? error.constructor?.name,
|
|
304
|
+
stack: error.stack,
|
|
305
|
+
why: errorLike.why,
|
|
306
|
+
fix: errorLike.fix,
|
|
307
|
+
link: errorLike.link,
|
|
308
|
+
details: normalizeDetails(errorLike.details),
|
|
309
|
+
cause: errorLike.cause
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (typeof error === "object") {
|
|
313
|
+
const record = error;
|
|
314
|
+
const statusCode = typeof record.statusCode === "number" ? record.statusCode : typeof record.status === "number" ? record.status : fallbackStatus;
|
|
315
|
+
return {
|
|
316
|
+
message: typeof record.message === "string" ? record.message : `HTTP ${statusCode ?? 500}`,
|
|
317
|
+
code: typeof record.code === "string" || typeof record.code === "number" ? record.code : statusCode,
|
|
318
|
+
type: typeof record.type === "string" ? record.type : typeof record.name === "string" ? record.name : "Error",
|
|
319
|
+
stack: typeof record.stack === "string" ? record.stack : void 0,
|
|
320
|
+
why: typeof record.why === "string" ? record.why : void 0,
|
|
321
|
+
fix: typeof record.fix === "string" ? record.fix : void 0,
|
|
322
|
+
link: typeof record.link === "string" ? record.link : void 0,
|
|
323
|
+
details: normalizeDetails(record.details),
|
|
324
|
+
cause: record.cause
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
message: String(error),
|
|
329
|
+
code: fallbackStatus,
|
|
330
|
+
type: typeof error
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function resolveEmitStatus(options) {
|
|
334
|
+
if (options.response && typeof options.response.status === "number") {
|
|
335
|
+
return options.response.status;
|
|
336
|
+
}
|
|
337
|
+
if (typeof options.status === "number") {
|
|
338
|
+
return options.status;
|
|
339
|
+
}
|
|
340
|
+
if (options.error && typeof options.error === "object" && options.error !== null && typeof options.error.statusCode === "number") {
|
|
341
|
+
return options.error.statusCode;
|
|
342
|
+
}
|
|
343
|
+
if (options.error && typeof options.error === "object" && options.error !== null && typeof options.error.status === "number") {
|
|
344
|
+
return options.error.status;
|
|
345
|
+
}
|
|
346
|
+
return options.error ? 500 : void 0;
|
|
347
|
+
}
|
|
348
|
+
function createStructuredLog(groupId, options) {
|
|
349
|
+
const startedAt = performance.now();
|
|
350
|
+
const fields = { ...options.initialFields ?? {} };
|
|
351
|
+
const events = [];
|
|
352
|
+
let emittedPayload;
|
|
353
|
+
options.onCreate?.();
|
|
354
|
+
const appendEvent = (level, message, args) => {
|
|
355
|
+
events.push({
|
|
356
|
+
level,
|
|
357
|
+
message: serializeMessage(message),
|
|
358
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
359
|
+
...normalizeEventData(message, args) === void 0 ? {} : { data: normalizeEventData(message, args) }
|
|
360
|
+
});
|
|
361
|
+
return structuredLog;
|
|
362
|
+
};
|
|
363
|
+
const structuredLog = {
|
|
364
|
+
set(extraFields) {
|
|
365
|
+
Object.assign(fields, extraFields);
|
|
366
|
+
return structuredLog;
|
|
367
|
+
},
|
|
368
|
+
debug(message, ...args) {
|
|
369
|
+
return appendEvent("debug", message, args);
|
|
370
|
+
},
|
|
371
|
+
info(message, ...args) {
|
|
372
|
+
return appendEvent("info", message, args);
|
|
373
|
+
},
|
|
374
|
+
warn(message, ...args) {
|
|
375
|
+
return appendEvent("warn", message, args);
|
|
376
|
+
},
|
|
377
|
+
warning(message, ...args) {
|
|
378
|
+
return appendEvent("warning", message, args);
|
|
379
|
+
},
|
|
380
|
+
error(message, ...args) {
|
|
381
|
+
return appendEvent("error", message, args);
|
|
382
|
+
},
|
|
383
|
+
success(message, ...args) {
|
|
384
|
+
return appendEvent("success", message, args);
|
|
385
|
+
},
|
|
386
|
+
critical(message, ...args) {
|
|
387
|
+
return appendEvent("critical", message, args);
|
|
388
|
+
},
|
|
389
|
+
table(message, data) {
|
|
390
|
+
return appendEvent("table", message, data === void 0 ? [] : [data]);
|
|
391
|
+
},
|
|
392
|
+
emit(emitOptions = {}) {
|
|
393
|
+
if (emittedPayload) {
|
|
394
|
+
return emittedPayload;
|
|
395
|
+
}
|
|
396
|
+
const defaultFields = options.resolveDefaultFields?.() ?? {};
|
|
397
|
+
const status = resolveEmitStatus(emitOptions);
|
|
398
|
+
const error = normalizeError(emitOptions.error, status);
|
|
399
|
+
const level = emitOptions.level ?? (error ? "error" : "info");
|
|
400
|
+
const payload = {
|
|
401
|
+
...defaultFields,
|
|
402
|
+
...fields,
|
|
403
|
+
groupId,
|
|
404
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
405
|
+
level,
|
|
406
|
+
duration: Math.round(performance.now() - startedAt),
|
|
407
|
+
...typeof status === "number" ? { status } : {},
|
|
408
|
+
...events.length > 0 ? { events: [...events] } : {},
|
|
409
|
+
...error ? { error } : {}
|
|
410
|
+
};
|
|
411
|
+
options.write(payload, emitOptions.message ?? "structured_log");
|
|
412
|
+
emittedPayload = payload;
|
|
413
|
+
options.onEmit?.(payload);
|
|
414
|
+
return payload;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
return structuredLog;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// src/frameworks/workers/logger.ts
|
|
421
|
+
var workersLoggerState = {};
|
|
422
|
+
function serializeMessage2(message) {
|
|
423
|
+
if (typeof message === "string") {
|
|
424
|
+
return message;
|
|
425
|
+
}
|
|
426
|
+
if (message !== null && typeof message === "object") {
|
|
427
|
+
try {
|
|
428
|
+
return JSON.stringify(message, (_key, value) => {
|
|
429
|
+
if (typeof value === "function") {
|
|
430
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
431
|
+
}
|
|
432
|
+
if (value === void 0) {
|
|
433
|
+
return "[undefined]";
|
|
434
|
+
}
|
|
435
|
+
if (typeof value === "symbol") {
|
|
436
|
+
return value.toString();
|
|
437
|
+
}
|
|
438
|
+
return value;
|
|
439
|
+
}, 2);
|
|
440
|
+
} catch {
|
|
441
|
+
return "[Object]";
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return String(message);
|
|
445
|
+
}
|
|
446
|
+
function normalizeStructuredData(message, args) {
|
|
447
|
+
if (typeof message === "string") {
|
|
448
|
+
if (args.length === 0) {
|
|
449
|
+
return void 0;
|
|
450
|
+
}
|
|
451
|
+
return args.length === 1 ? args[0] : args;
|
|
452
|
+
}
|
|
453
|
+
const values = [message, ...args];
|
|
454
|
+
return values.length === 1 ? values[0] : values;
|
|
455
|
+
}
|
|
456
|
+
function getConsoleMethod(level) {
|
|
457
|
+
switch (level) {
|
|
458
|
+
case "debug":
|
|
459
|
+
return "debug";
|
|
460
|
+
case "warn":
|
|
461
|
+
case "warning":
|
|
462
|
+
return "warn";
|
|
463
|
+
case "error":
|
|
464
|
+
case "critical":
|
|
465
|
+
return "error";
|
|
466
|
+
case "success":
|
|
467
|
+
return "log";
|
|
468
|
+
case "info":
|
|
469
|
+
default:
|
|
470
|
+
return "info";
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function writeConsole(method, message, payload) {
|
|
474
|
+
if (typeof console === "undefined") {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const logger = console[method] ?? console.info;
|
|
478
|
+
if (payload === void 0) {
|
|
479
|
+
logger.call(console, message);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
logger.call(console, message, payload);
|
|
483
|
+
}
|
|
484
|
+
function writeStructuredConsole(level, message, payload) {
|
|
485
|
+
writeConsole(getConsoleMethod(level), message, payload);
|
|
486
|
+
}
|
|
487
|
+
function buildRequestMetadata(request, path) {
|
|
488
|
+
return {
|
|
489
|
+
method: request.method,
|
|
490
|
+
url: path,
|
|
491
|
+
...buildClientDetails(request, path)
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
function createScopedPayload(request, path, fields, data) {
|
|
495
|
+
return {
|
|
496
|
+
request: buildRequestMetadata(request, path),
|
|
497
|
+
...fields,
|
|
498
|
+
...data === void 0 ? {} : { data }
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
function emitScopedLog(level, request, path, fields, message, args) {
|
|
502
|
+
writeConsole(
|
|
503
|
+
getConsoleMethod(level),
|
|
504
|
+
serializeMessage2(message),
|
|
505
|
+
createScopedPayload(request, path, fields, normalizeStructuredData(message, args))
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
function resolveEmitStatus2(options) {
|
|
509
|
+
return options.response?.status ?? options.status ?? (options.error ? 500 : 200);
|
|
510
|
+
}
|
|
511
|
+
function createEmitPayload(request, path, fields, startTime, options) {
|
|
512
|
+
const statusCode = resolveEmitStatus2(options);
|
|
513
|
+
const errorLike = toErrorLike(options.error, statusCode);
|
|
514
|
+
return buildRequestLogData(
|
|
515
|
+
request,
|
|
516
|
+
options.error || isErrorStatus(statusCode) ? "http_error" : "http_request",
|
|
517
|
+
path,
|
|
518
|
+
statusCode,
|
|
519
|
+
Math.round(performance.now() - startTime),
|
|
520
|
+
{
|
|
521
|
+
...fields,
|
|
522
|
+
...errorLike ? {
|
|
523
|
+
error: errorLike.message,
|
|
524
|
+
stack: errorLike.stack,
|
|
525
|
+
code: errorLike.code,
|
|
526
|
+
why: errorLike.why,
|
|
527
|
+
fix: errorLike.fix,
|
|
528
|
+
link: errorLike.link,
|
|
529
|
+
details: errorLike.details
|
|
530
|
+
} : {}
|
|
531
|
+
}
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
function createScopedFields(request) {
|
|
535
|
+
return {
|
|
536
|
+
...workersLoggerState.env ?? {},
|
|
537
|
+
...workersLoggerState.customProps?.(request) ?? {}
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
function initWorkersLogger(config = {}) {
|
|
541
|
+
workersLoggerState = {
|
|
542
|
+
env: config.env ? { ...config.env } : void 0,
|
|
543
|
+
customProps: config.customProps
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
function createWorkersLogger(request) {
|
|
547
|
+
const startTime = performance.now();
|
|
548
|
+
const requestLike = createRequestLike(request.method, request.url, request.headers);
|
|
549
|
+
const path = extractPathname(request.url);
|
|
550
|
+
const fields = createScopedFields(request);
|
|
551
|
+
let emittedRecord;
|
|
552
|
+
const logger = {
|
|
553
|
+
set(extraFields) {
|
|
554
|
+
Object.assign(fields, extraFields);
|
|
555
|
+
return logger;
|
|
556
|
+
},
|
|
557
|
+
emit(options = {}) {
|
|
558
|
+
if (emittedRecord) {
|
|
559
|
+
return emittedRecord;
|
|
560
|
+
}
|
|
561
|
+
emittedRecord = createEmitPayload(requestLike, path, fields, startTime, options);
|
|
562
|
+
writeConsole(
|
|
563
|
+
emittedRecord.type === "http_error" ? "error" : "info",
|
|
564
|
+
buildInfoLogMessage(
|
|
565
|
+
requestLike.method,
|
|
566
|
+
emittedRecord.statusCode,
|
|
567
|
+
path,
|
|
568
|
+
emittedRecord.responseTime
|
|
569
|
+
),
|
|
570
|
+
emittedRecord
|
|
571
|
+
);
|
|
572
|
+
return emittedRecord;
|
|
573
|
+
},
|
|
574
|
+
debug(message, ...args) {
|
|
575
|
+
emitScopedLog("debug", requestLike, path, fields, message, args);
|
|
576
|
+
},
|
|
577
|
+
info(message, ...args) {
|
|
578
|
+
emitScopedLog("info", requestLike, path, fields, message, args);
|
|
579
|
+
},
|
|
580
|
+
warn(message, ...args) {
|
|
581
|
+
emitScopedLog("warn", requestLike, path, fields, message, args);
|
|
582
|
+
},
|
|
583
|
+
warning(message, ...args) {
|
|
584
|
+
emitScopedLog("warning", requestLike, path, fields, message, args);
|
|
585
|
+
},
|
|
586
|
+
error(message, ...args) {
|
|
587
|
+
emitScopedLog("error", requestLike, path, fields, message, args);
|
|
588
|
+
},
|
|
589
|
+
success(message, ...args) {
|
|
590
|
+
emitScopedLog("success", requestLike, path, fields, message, args);
|
|
591
|
+
},
|
|
592
|
+
critical(message, ...args) {
|
|
593
|
+
emitScopedLog("critical", requestLike, path, fields, message, args);
|
|
594
|
+
},
|
|
595
|
+
table(message, data) {
|
|
596
|
+
writeConsole("log", message, createScopedPayload(requestLike, path, fields, data));
|
|
597
|
+
if (typeof console !== "undefined" && typeof console.table === "function" && data !== void 0) {
|
|
598
|
+
console.table(data);
|
|
599
|
+
}
|
|
600
|
+
},
|
|
601
|
+
createStructuredLog(groupId, initial) {
|
|
602
|
+
return createStructuredLog(groupId, {
|
|
603
|
+
initialFields: initial,
|
|
604
|
+
resolveDefaultFields: () => ({
|
|
605
|
+
method: requestLike.method,
|
|
606
|
+
path,
|
|
607
|
+
...fields
|
|
608
|
+
}),
|
|
609
|
+
write: (payload, message) => {
|
|
610
|
+
const writeLevel = payload.level === "warning" ? "warning" : payload.level;
|
|
611
|
+
writeStructuredConsole(
|
|
612
|
+
writeLevel === "table" ? "info" : writeLevel,
|
|
613
|
+
message,
|
|
614
|
+
payload
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
return logger;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
exports.createWorkersLogger = createWorkersLogger;
|
|
624
|
+
exports.initWorkersLogger = initWorkersLogger;
|
|
625
|
+
//# sourceMappingURL=workers.js.map
|
|
626
|
+
//# sourceMappingURL=workers.js.map
|