@visulima/pail 4.0.0-alpha.5 → 4.0.0-alpha.7
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/CHANGELOG.md +37 -0
- package/LICENSE.md +6 -6
- package/README.md +323 -0
- package/dist/error.d.ts +104 -0
- package/dist/error.js +76 -0
- package/dist/index.browser.d.ts +2 -0
- package/dist/index.browser.js +2 -1
- package/dist/index.server.d.ts +2 -0
- package/dist/index.server.js +6 -4
- package/dist/interactive/index.js +1 -1
- package/dist/middleware/elysia.d.ts +71 -0
- package/dist/middleware/elysia.js +70 -0
- package/dist/middleware/express.d.ts +86 -0
- package/dist/middleware/express.js +29 -0
- package/dist/middleware/fastify.d.ts +81 -0
- package/dist/middleware/fastify.js +46 -0
- package/dist/middleware/hono.d.ts +85 -0
- package/dist/middleware/hono.js +33 -0
- package/dist/middleware/next/handler.d.ts +36 -0
- package/dist/middleware/next/handler.js +53 -0
- package/dist/middleware/next/middleware.d.ts +59 -0
- package/dist/middleware/next/storage.d.ts +14 -0
- package/dist/middleware/shared/create-middleware-logger.d.ts +82 -0
- package/dist/middleware/shared/headers.d.ts +14 -0
- package/dist/middleware/shared/routes.d.ts +30 -0
- package/dist/middleware/shared/storage.d.ts +29 -0
- package/dist/middleware/sveltekit.d.ts +123 -0
- package/dist/middleware/sveltekit.js +43 -0
- package/dist/packem_shared/{AbstractJsonReporter-DWRpTtGw.js → AbstractJsonReporter-CGKHS8_M.js} +103 -21
- package/dist/packem_shared/{AbstractJsonReporter-BaZ33PlE.js → AbstractJsonReporter-DDjDkciI.js} +103 -21
- package/dist/packem_shared/{InteractiveManager-CbE7d1kY.js → InteractiveManager-Cd6A14ZK.js} +1 -1
- package/dist/packem_shared/{JsonReporter-BV5lMnJX.js → JsonReporter-B3XX8GHN.js} +1 -1
- package/dist/packem_shared/{JsonReporter-BRw4skd5.js → JsonReporter-p_BXg6Sj.js} +1 -1
- package/dist/packem_shared/{PrettyReporter-DLQtmATi.js → PrettyReporter-CvBn-hxP.js} +5 -4
- package/dist/packem_shared/{Spinner-B9JUdsbY.js → Spinner-DIdVcfWq.js} +34 -1
- package/dist/packem_shared/{abstract-pretty-reporter-DckLMlGF.js → abstract-pretty-reporter-jU8WL_6c.js} +121 -15
- package/dist/packem_shared/createPailError-B11aRfrT.js +76 -0
- package/dist/packem_shared/headers-Cp4uLtr4.js +123 -0
- package/dist/packem_shared/{index-EZ_WSQZS.js → index-frijFf5m.js} +120 -14
- package/dist/packem_shared/pailMiddleware-Ci88geIF.js +24 -0
- package/dist/packem_shared/storage-D0vqz8OX.js +36 -0
- package/dist/packem_shared/useLogger-D0rU3lcX.js +33 -0
- package/dist/processor/environment-processor.d.ts +124 -0
- package/dist/processor/environment-processor.js +78 -0
- package/dist/processor/message-formatter-processor.d.ts +1 -2
- package/dist/processor/sampling-processor.d.ts +111 -0
- package/dist/processor/sampling-processor.js +59 -0
- package/dist/reporter/file/json-file-reporter.js +1 -1
- package/dist/reporter/http/abstract-http-reporter.js +1 -1
- package/dist/reporter/http/http-reporter.edge-light.js +103 -21
- package/dist/reporter/json/index.browser.js +2 -2
- package/dist/reporter/json/index.js +2 -2
- package/dist/reporter/pretty/index.js +1 -1
- package/dist/reporter/simple/simple-reporter.server.js +4 -3
- package/dist/spinner.js +34 -1
- package/dist/wide-event.d.ts +300 -0
- package/dist/wide-event.js +281 -0
- package/package.json +68 -4
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { WideEvent } from "../wide-event.d.ts";
|
|
2
|
+
import type { PailMiddlewareOptions } from "./shared/create-middleware-logger.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* An Elysia-like instance that supports the derive/hook registration API.
|
|
5
|
+
* Compatible with Elysia v1+.
|
|
6
|
+
*/
|
|
7
|
+
interface PailElysiaInstance {
|
|
8
|
+
derive: (options: {
|
|
9
|
+
as: string;
|
|
10
|
+
}, handler: (context: {
|
|
11
|
+
request: Request;
|
|
12
|
+
}) => Record<string, unknown>) => PailElysiaInstance;
|
|
13
|
+
onAfterHandle: (options: {
|
|
14
|
+
as: string;
|
|
15
|
+
}, handler: (context: {
|
|
16
|
+
request: Request;
|
|
17
|
+
set: {
|
|
18
|
+
status?: number;
|
|
19
|
+
};
|
|
20
|
+
}) => Promise<void>) => PailElysiaInstance;
|
|
21
|
+
onError: (options: {
|
|
22
|
+
as: string;
|
|
23
|
+
}, handler: (context: {
|
|
24
|
+
error: Error;
|
|
25
|
+
request: Request;
|
|
26
|
+
}) => Promise<void>) => PailElysiaInstance;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
30
|
+
* Must be called within a request handler where pail middleware is active.
|
|
31
|
+
* @returns The request-scoped WideEvent logger
|
|
32
|
+
* @throws Error if called outside of the middleware context
|
|
33
|
+
*/
|
|
34
|
+
declare const useLogger: () => WideEvent;
|
|
35
|
+
/**
|
|
36
|
+
* Configuration options for the Elysia pail plugin.
|
|
37
|
+
* @template T - Custom logger type names from the pail instance
|
|
38
|
+
*/
|
|
39
|
+
type ElysiaPluginOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
40
|
+
/**
|
|
41
|
+
* Register pail wide event logging as an Elysia plugin.
|
|
42
|
+
*
|
|
43
|
+
* Injects a `log` property into handler context via `derive`, accessible via:
|
|
44
|
+
* - `context.log` in route handlers (auto-injected)
|
|
45
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
46
|
+
*
|
|
47
|
+
* The wide event is automatically emitted on response or error.
|
|
48
|
+
* @param app The Elysia instance
|
|
49
|
+
* @param options Plugin configuration
|
|
50
|
+
* @returns The Elysia instance (for chaining)
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { Elysia } from "elysia";
|
|
54
|
+
* import { createPail } from "@visulima/pail";
|
|
55
|
+
* import { pailPlugin, useLogger } from "@visulima/pail/middleware/elysia";
|
|
56
|
+
*
|
|
57
|
+
* const logger = createPail();
|
|
58
|
+
* const app = new Elysia();
|
|
59
|
+
*
|
|
60
|
+
* pailPlugin(app, { pail: logger });
|
|
61
|
+
*
|
|
62
|
+
* app.get("/api/users", ({ log }) => {
|
|
63
|
+
* log.set({ user: { id: 1 } });
|
|
64
|
+
* return { ok: true };
|
|
65
|
+
* });
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare const pailPlugin: <T extends string = string>(app: PailElysiaInstance, options: ElysiaPluginOptions<T>) => PailElysiaInstance;
|
|
69
|
+
export { useLogger };
|
|
70
|
+
export type { ElysiaPluginOptions, PailElysiaInstance };
|
|
71
|
+
export type { WideEvent } from "../wide-event.d.ts";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createRequire as __cjs_createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
const __cjs_require = __cjs_createRequire(import.meta.url);
|
|
4
|
+
|
|
5
|
+
const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
|
|
6
|
+
|
|
7
|
+
const __cjs_getBuiltinModule = (module) => {
|
|
8
|
+
// Check if we're in Node.js and version supports getBuiltinModule
|
|
9
|
+
if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
|
|
10
|
+
const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
|
|
11
|
+
// Node.js 20.16.0+ and 22.3.0+
|
|
12
|
+
if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
|
|
13
|
+
return __cjs_getProcess.getBuiltinModule(module);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// Fallback to createRequire
|
|
17
|
+
return __cjs_require(module);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
AsyncLocalStorage
|
|
22
|
+
} = __cjs_getBuiltinModule("node:async_hooks");
|
|
23
|
+
import { a as extractSafeHeaders, c as createMiddlewareLogger } from '../packem_shared/headers-Cp4uLtr4.js';
|
|
24
|
+
|
|
25
|
+
const storage = new AsyncLocalStorage();
|
|
26
|
+
const activeLoggers = /* @__PURE__ */ new WeakSet();
|
|
27
|
+
const useLogger = () => {
|
|
28
|
+
const logger = storage.getStore();
|
|
29
|
+
if (!logger || !activeLoggers.has(logger)) {
|
|
30
|
+
throw new Error("[pail] useLogger() called outside of Elysia pail plugin context.");
|
|
31
|
+
}
|
|
32
|
+
return logger;
|
|
33
|
+
};
|
|
34
|
+
const pailPlugin = (app, options) => {
|
|
35
|
+
const requestState = /* @__PURE__ */ new WeakMap();
|
|
36
|
+
const emitted = /* @__PURE__ */ new WeakSet();
|
|
37
|
+
return app.derive({ as: "global" }, ({ request }) => {
|
|
38
|
+
const url = new URL(request.url);
|
|
39
|
+
const requestId = request.headers.get("x-request-id") ?? crypto.randomUUID();
|
|
40
|
+
const safeHeaders = extractSafeHeaders(request.headers);
|
|
41
|
+
const result = createMiddlewareLogger(options, {
|
|
42
|
+
headers: safeHeaders,
|
|
43
|
+
method: request.method,
|
|
44
|
+
path: url.pathname,
|
|
45
|
+
requestId
|
|
46
|
+
});
|
|
47
|
+
requestState.set(request, result);
|
|
48
|
+
if (!result.skipped) {
|
|
49
|
+
activeLoggers.add(result.logger);
|
|
50
|
+
storage.enterWith(result.logger);
|
|
51
|
+
}
|
|
52
|
+
return { log: result.logger };
|
|
53
|
+
}).onAfterHandle({ as: "global" }, async ({ request, set }) => {
|
|
54
|
+
const state = requestState.get(request);
|
|
55
|
+
if (!state?.skipped && state && !emitted.has(request)) {
|
|
56
|
+
emitted.add(request);
|
|
57
|
+
state.finish({ status: set.status ?? 200 });
|
|
58
|
+
activeLoggers.delete(state.logger);
|
|
59
|
+
}
|
|
60
|
+
}).onError({ as: "global" }, async ({ error, request }) => {
|
|
61
|
+
const state = requestState.get(request);
|
|
62
|
+
if (!state?.skipped && state && !emitted.has(request)) {
|
|
63
|
+
emitted.add(request);
|
|
64
|
+
state.finish({ error });
|
|
65
|
+
activeLoggers.delete(state.logger);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export { pailPlugin, useLogger };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { WideEvent } from "../wide-event.d.ts";
|
|
2
|
+
import type { PailMiddlewareOptions } from "./shared/create-middleware-logger.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* An Express-like request object with the pail logger attached.
|
|
5
|
+
* Use this to type your route handlers when accessing `req.log`.
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import type { PailRequest } from "@visulima/pail/middleware/express";
|
|
9
|
+
*
|
|
10
|
+
* app.get("/api/users", (req: PailRequest, res) => {
|
|
11
|
+
* req.log?.set({ user: { id: 1 } });
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
interface PailRequest {
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
headers: Record<string, string | string[] | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* The request-scoped WideEvent logger, attached by pail middleware.
|
|
20
|
+
* Only present for non-excluded routes.
|
|
21
|
+
*/
|
|
22
|
+
log?: WideEvent;
|
|
23
|
+
method: string;
|
|
24
|
+
originalUrl?: string;
|
|
25
|
+
path: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* An Express-like response object.
|
|
29
|
+
*/
|
|
30
|
+
interface PailResponse {
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
on: (event: string, listener: () => void) => void;
|
|
33
|
+
statusCode: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Express-compatible next function.
|
|
37
|
+
*/
|
|
38
|
+
type PailNextFunction = (error?: unknown) => void;
|
|
39
|
+
/**
|
|
40
|
+
* The Express middleware function signature returned by `pailMiddleware()`.
|
|
41
|
+
*/
|
|
42
|
+
type PailExpressMiddleware = (request: PailRequest, response: PailResponse, next: PailNextFunction) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
45
|
+
* Must be called within a request handled by the pail Express middleware.
|
|
46
|
+
* @returns The request-scoped WideEvent logger
|
|
47
|
+
* @throws Error if called outside of the middleware context
|
|
48
|
+
*/
|
|
49
|
+
declare const useLogger: () => WideEvent;
|
|
50
|
+
/**
|
|
51
|
+
* Configuration options for the Express pail middleware.
|
|
52
|
+
* @template T - Custom logger type names from the pail instance
|
|
53
|
+
*/
|
|
54
|
+
type ExpressMiddlewareOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Create Express middleware that attaches a WideEvent logger to each request.
|
|
57
|
+
*
|
|
58
|
+
* The logger is available via:
|
|
59
|
+
* - `req.log` on the request object
|
|
60
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
61
|
+
*
|
|
62
|
+
* The wide event is automatically emitted when the response finishes.
|
|
63
|
+
* @param options Middleware configuration
|
|
64
|
+
* @returns Express middleware function
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import express from "express";
|
|
68
|
+
* import { createPail } from "@visulima/pail";
|
|
69
|
+
* import { pailMiddleware, useLogger } from "@visulima/pail/middleware/express";
|
|
70
|
+
*
|
|
71
|
+
* const app = express();
|
|
72
|
+
* const logger = createPail();
|
|
73
|
+
*
|
|
74
|
+
* app.use(pailMiddleware({ pail: logger }));
|
|
75
|
+
*
|
|
76
|
+
* app.get("/api/users", (req, res) => {
|
|
77
|
+
* req.log.set({ user: { id: 1 } });
|
|
78
|
+
* // or: useLogger().set({ user: { id: 1 } });
|
|
79
|
+
* res.json({ ok: true });
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare const pailMiddleware: <T extends string = string>(options: ExpressMiddlewareOptions<T>) => PailExpressMiddleware;
|
|
84
|
+
export { useLogger };
|
|
85
|
+
export type { ExpressMiddlewareOptions, PailExpressMiddleware, PailNextFunction, PailRequest, PailResponse };
|
|
86
|
+
export type { WideEvent } from "../wide-event.d.ts";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { e as extractSafeNodeHeaders, c as createMiddlewareLogger } from '../packem_shared/headers-Cp4uLtr4.js';
|
|
2
|
+
import { c as createLoggerStorage } from '../packem_shared/storage-D0vqz8OX.js';
|
|
3
|
+
|
|
4
|
+
const loggerStorage = createLoggerStorage("Express middleware context. Make sure pail middleware is registered before your route handlers.");
|
|
5
|
+
const useLogger = () => loggerStorage.useLogger();
|
|
6
|
+
const pailMiddleware = (options) => (request, response, next) => {
|
|
7
|
+
const requestId = request.headers["x-request-id"] ?? crypto.randomUUID();
|
|
8
|
+
const path = request.originalUrl ?? request.path;
|
|
9
|
+
const safeHeaders = extractSafeNodeHeaders(request.headers);
|
|
10
|
+
const { finish, logger, skipped } = createMiddlewareLogger(options, {
|
|
11
|
+
headers: safeHeaders,
|
|
12
|
+
method: request.method,
|
|
13
|
+
path,
|
|
14
|
+
requestId
|
|
15
|
+
});
|
|
16
|
+
if (skipped) {
|
|
17
|
+
next();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
request.log = logger;
|
|
21
|
+
response.on("finish", () => {
|
|
22
|
+
finish({ status: response.statusCode });
|
|
23
|
+
});
|
|
24
|
+
loggerStorage.storage.run(logger, () => {
|
|
25
|
+
next();
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { pailMiddleware, useLogger };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { WideEvent } from "../wide-event.d.ts";
|
|
2
|
+
import type { PailMiddlewareOptions } from "./shared/create-middleware-logger.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* A Fastify-like request object with the pail logger attached.
|
|
5
|
+
* Use this to type your route handlers when accessing `request.log`.
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import type { PailFastifyRequest } from "@visulima/pail/middleware/fastify";
|
|
9
|
+
*
|
|
10
|
+
* app.get("/api/users", async (request: PailFastifyRequest, reply) => {
|
|
11
|
+
* request.log?.set({ user: { id: 1 } });
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
interface PailFastifyRequest {
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
headers: Record<string, string | string[] | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* The request-scoped WideEvent logger, attached by pail plugin.
|
|
20
|
+
* Only present for non-excluded routes.
|
|
21
|
+
*/
|
|
22
|
+
log?: WideEvent;
|
|
23
|
+
method: string;
|
|
24
|
+
url: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A Fastify-like reply object.
|
|
28
|
+
*/
|
|
29
|
+
interface PailFastifyReply {
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
statusCode: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A Fastify-like instance that supports the hook registration API.
|
|
35
|
+
*/
|
|
36
|
+
interface PailFastifyInstance {
|
|
37
|
+
addHook: ((name: "onError", hook: (request: PailFastifyRequest, reply: PailFastifyReply, error: Error) => Promise<void>) => void) & ((name: "onRequest", hook: (request: PailFastifyRequest, reply: PailFastifyReply, done: () => void) => void) => void) & ((name: "onResponse", hook: (request: PailFastifyRequest, reply: PailFastifyReply) => Promise<void>) => void);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
41
|
+
* Must be called within a request handled by the pail Fastify plugin.
|
|
42
|
+
* @returns The request-scoped WideEvent logger
|
|
43
|
+
* @throws Error if called outside of the plugin context
|
|
44
|
+
*/
|
|
45
|
+
declare const useLogger: () => WideEvent;
|
|
46
|
+
/**
|
|
47
|
+
* Configuration options for the Fastify pail plugin.
|
|
48
|
+
* @template T - Custom logger type names from the pail instance
|
|
49
|
+
*/
|
|
50
|
+
type FastifyPluginOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
51
|
+
/**
|
|
52
|
+
* Register pail wide event logging as a Fastify plugin.
|
|
53
|
+
*
|
|
54
|
+
* Attaches a WideEvent logger to each request, accessible via:
|
|
55
|
+
* - `request.log` on the Fastify request object
|
|
56
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
57
|
+
*
|
|
58
|
+
* The wide event is automatically emitted on response or error.
|
|
59
|
+
* @param fastify The Fastify instance
|
|
60
|
+
* @param options Plugin configuration
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* import Fastify from "fastify";
|
|
64
|
+
* import { createPail } from "@visulima/pail";
|
|
65
|
+
* import { pailPlugin, useLogger } from "@visulima/pail/middleware/fastify";
|
|
66
|
+
*
|
|
67
|
+
* const app = Fastify();
|
|
68
|
+
* const logger = createPail();
|
|
69
|
+
*
|
|
70
|
+
* pailPlugin(app, { pail: logger });
|
|
71
|
+
*
|
|
72
|
+
* app.get("/api/users", async (request, reply) => {
|
|
73
|
+
* request.log.set({ user: { id: 1 } });
|
|
74
|
+
* return { ok: true };
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare const pailPlugin: <T extends string = string>(fastify: PailFastifyInstance, options: FastifyPluginOptions<T>) => void;
|
|
79
|
+
export { useLogger };
|
|
80
|
+
export type { FastifyPluginOptions, PailFastifyInstance, PailFastifyReply, PailFastifyRequest };
|
|
81
|
+
export type { WideEvent } from "../wide-event.d.ts";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { e as extractSafeNodeHeaders, c as createMiddlewareLogger } from '../packem_shared/headers-Cp4uLtr4.js';
|
|
2
|
+
import { c as createLoggerStorage } from '../packem_shared/storage-D0vqz8OX.js';
|
|
3
|
+
|
|
4
|
+
const loggerStorage = createLoggerStorage("Fastify middleware context. Make sure pail plugin is registered before your route handlers.");
|
|
5
|
+
const useLogger = () => loggerStorage.useLogger();
|
|
6
|
+
const pailPlugin = (fastify, options) => {
|
|
7
|
+
const requestState = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
const emitted = /* @__PURE__ */ new WeakSet();
|
|
9
|
+
fastify.addHook("onRequest", (request, _reply, done) => {
|
|
10
|
+
const requestId = request.headers["x-request-id"] ?? crypto.randomUUID();
|
|
11
|
+
const safeHeaders = extractSafeNodeHeaders(request.headers);
|
|
12
|
+
const { finish, logger, skipped } = createMiddlewareLogger(options, {
|
|
13
|
+
headers: safeHeaders,
|
|
14
|
+
method: request.method,
|
|
15
|
+
path: request.url,
|
|
16
|
+
requestId
|
|
17
|
+
});
|
|
18
|
+
if (skipped) {
|
|
19
|
+
done();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
request.log = logger;
|
|
23
|
+
requestState.set(request, { finish });
|
|
24
|
+
loggerStorage.storage.run(logger, () => {
|
|
25
|
+
done();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
fastify.addHook("onResponse", async (request, reply) => {
|
|
29
|
+
const state = requestState.get(request);
|
|
30
|
+
if (!state || emitted.has(request)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
emitted.add(request);
|
|
34
|
+
state.finish({ status: reply.statusCode });
|
|
35
|
+
});
|
|
36
|
+
fastify.addHook("onError", async (request, _reply, error) => {
|
|
37
|
+
const state = requestState.get(request);
|
|
38
|
+
if (!state || emitted.has(request)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
emitted.add(request);
|
|
42
|
+
state.finish({ error });
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export { pailPlugin, useLogger };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { WideEvent } from "../wide-event.d.ts";
|
|
2
|
+
import type { PailMiddlewareOptions } from "./shared/create-middleware-logger.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* A Hono-like context object with access to the pail logger.
|
|
5
|
+
*
|
|
6
|
+
* Use `c.get("log")` or `useLogger(c)` to access the WideEvent logger.
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import type { PailHonoContext } from "@visulima/pail/middleware/hono";
|
|
10
|
+
*
|
|
11
|
+
* app.get("/api/users", (c: PailHonoContext) => {
|
|
12
|
+
* const log = c.get("log");
|
|
13
|
+
* log.set({ user: { id: 1 } });
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
interface PailHonoContext {
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
get: (key: string) => unknown;
|
|
20
|
+
req: {
|
|
21
|
+
header: (name: string) => string | undefined;
|
|
22
|
+
method: string;
|
|
23
|
+
path: string;
|
|
24
|
+
raw: {
|
|
25
|
+
headers: Headers;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
res: {
|
|
29
|
+
status: number;
|
|
30
|
+
};
|
|
31
|
+
set: (key: string, value: unknown) => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Hono-compatible next function.
|
|
35
|
+
*/
|
|
36
|
+
type PailHonoNext = () => Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* The Hono middleware function signature returned by `pailMiddleware()`.
|
|
39
|
+
*/
|
|
40
|
+
type PailHonoMiddleware = (c: PailHonoContext, next: PailHonoNext) => Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Configuration options for the Hono pail middleware.
|
|
43
|
+
* @template T - Custom logger type names from the pail instance
|
|
44
|
+
*/
|
|
45
|
+
type HonoMiddlewareOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Retrieve the WideEvent logger from Hono context.
|
|
48
|
+
* Must be called within a request handler where pail middleware is active.
|
|
49
|
+
* @param c The Hono context object
|
|
50
|
+
* @returns The request-scoped WideEvent logger
|
|
51
|
+
* @throws Error if the middleware is not registered
|
|
52
|
+
*/
|
|
53
|
+
declare const useLogger: (c: PailHonoContext) => WideEvent;
|
|
54
|
+
/**
|
|
55
|
+
* Create Hono middleware that attaches a WideEvent logger to each request.
|
|
56
|
+
*
|
|
57
|
+
* The logger is available via:
|
|
58
|
+
* - `c.get("log")` on the Hono context
|
|
59
|
+
* - `useLogger(c)` helper function
|
|
60
|
+
*
|
|
61
|
+
* The wide event is automatically emitted when the handler completes or throws.
|
|
62
|
+
* @param options Middleware configuration
|
|
63
|
+
* @returns Hono middleware function
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { Hono } from "hono";
|
|
67
|
+
* import { createPail } from "@visulima/pail";
|
|
68
|
+
* import { pailMiddleware, useLogger } from "@visulima/pail/middleware/hono";
|
|
69
|
+
*
|
|
70
|
+
* const app = new Hono();
|
|
71
|
+
* const logger = createPail();
|
|
72
|
+
*
|
|
73
|
+
* app.use("*", pailMiddleware({ pail: logger }));
|
|
74
|
+
*
|
|
75
|
+
* app.get("/api/users", (c) => {
|
|
76
|
+
* const log = useLogger(c);
|
|
77
|
+
* log.set({ user: { id: 1 } });
|
|
78
|
+
* return c.json({ ok: true });
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare const pailMiddleware: <T extends string = string>(options: HonoMiddlewareOptions<T>) => PailHonoMiddleware;
|
|
83
|
+
export { useLogger };
|
|
84
|
+
export type { HonoMiddlewareOptions, PailHonoContext, PailHonoMiddleware, PailHonoNext };
|
|
85
|
+
export type { WideEvent } from "../wide-event.d.ts";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { a as extractSafeHeaders, c as createMiddlewareLogger } from '../packem_shared/headers-Cp4uLtr4.js';
|
|
2
|
+
|
|
3
|
+
const useLogger = (c) => {
|
|
4
|
+
const logger = c.get("log");
|
|
5
|
+
if (!logger) {
|
|
6
|
+
throw new Error("[pail] useLogger() called but pail middleware is not registered.");
|
|
7
|
+
}
|
|
8
|
+
return logger;
|
|
9
|
+
};
|
|
10
|
+
const pailMiddleware = (options) => async (c, next) => {
|
|
11
|
+
const requestId = c.req.header("x-request-id") ?? crypto.randomUUID();
|
|
12
|
+
const safeHeaders = extractSafeHeaders(c.req.raw.headers);
|
|
13
|
+
const { finish, logger, skipped } = createMiddlewareLogger(options, {
|
|
14
|
+
headers: safeHeaders,
|
|
15
|
+
method: c.req.method,
|
|
16
|
+
path: c.req.path,
|
|
17
|
+
requestId
|
|
18
|
+
});
|
|
19
|
+
if (skipped) {
|
|
20
|
+
await next();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
c.set("log", logger);
|
|
24
|
+
try {
|
|
25
|
+
await next();
|
|
26
|
+
finish({ status: c.res.status });
|
|
27
|
+
} catch (error) {
|
|
28
|
+
finish({ error: error instanceof Error ? error : new Error(String(error)) });
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { pailMiddleware, useLogger };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PailMiddlewareOptions } from "../shared/create-middleware-logger.d.ts";
|
|
2
|
+
type NextPailOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
3
|
+
/**
|
|
4
|
+
* Create a `withPail` wrapper function for Next.js route handlers and server actions.
|
|
5
|
+
*
|
|
6
|
+
* Wraps handler execution in AsyncLocalStorage so that `useLogger()` works
|
|
7
|
+
* anywhere in the async call stack. The wide event is automatically emitted
|
|
8
|
+
* when the handler completes or throws.
|
|
9
|
+
* @param options Configuration options
|
|
10
|
+
* @returns A `withPail` wrapper function
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // lib/pail.ts
|
|
14
|
+
* import { createPail } from "@visulima/pail";
|
|
15
|
+
* import { createWithPail } from "@visulima/pail/middleware/next";
|
|
16
|
+
*
|
|
17
|
+
* const logger = createPail();
|
|
18
|
+
* export const withPail = createWithPail({ pail: logger });
|
|
19
|
+
*
|
|
20
|
+
* // app/api/users/route.ts
|
|
21
|
+
* import { withPail } from "@/lib/pail";
|
|
22
|
+
* import { useLogger } from "@visulima/pail/middleware/next";
|
|
23
|
+
*
|
|
24
|
+
* export const GET = withPail(async (request: Request) => {
|
|
25
|
+
* const log = useLogger();
|
|
26
|
+
* log.set({ user: { id: 1 } });
|
|
27
|
+
* return Response.json({ ok: true });
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare const createWithPail: <T extends string = string>(options: NextPailOptions<T>) => <TArgs extends unknown[], TReturn>(handler: (...args: TArgs) => Promise<TReturn> | TReturn) => (...args: TArgs) => Promise<TReturn>;
|
|
32
|
+
export type { NextPailOptions };
|
|
33
|
+
export type { WideEvent } from "../../wide-event.d.ts";
|
|
34
|
+
export type { PailNextMiddlewareOptions } from "./middleware/next/middleware.d.ts";
|
|
35
|
+
export { pailMiddleware } from "./middleware/next/middleware.d.ts";
|
|
36
|
+
export { useLogger } from "./storage.d.ts";
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { a as extractSafeHeaders, c as createMiddlewareLogger } from '../../packem_shared/headers-Cp4uLtr4.js';
|
|
2
|
+
import { pailStorage } from '../../packem_shared/useLogger-D0rU3lcX.js';
|
|
3
|
+
export { useLogger } from '../../packem_shared/useLogger-D0rU3lcX.js';
|
|
4
|
+
export { pailMiddleware } from '../../packem_shared/pailMiddleware-Ci88geIF.js';
|
|
5
|
+
|
|
6
|
+
const createWithPail = (options) => (
|
|
7
|
+
/**
|
|
8
|
+
* Wrap a Next.js route handler or server action with wide event logging.
|
|
9
|
+
* @returns A wrapped handler that creates and emits a WideEvent
|
|
10
|
+
*/
|
|
11
|
+
(handler) => async (...args) => {
|
|
12
|
+
const [firstArgument] = args;
|
|
13
|
+
const isRequest = firstArgument instanceof Request;
|
|
14
|
+
let method = "UNKNOWN";
|
|
15
|
+
let path = "/";
|
|
16
|
+
let requestId = crypto.randomUUID();
|
|
17
|
+
let headers = {};
|
|
18
|
+
if (isRequest) {
|
|
19
|
+
const request = firstArgument;
|
|
20
|
+
const url = new URL(request.url);
|
|
21
|
+
method = request.method;
|
|
22
|
+
path = url.pathname;
|
|
23
|
+
headers = extractSafeHeaders(request.headers);
|
|
24
|
+
const middlewareRequestId = request.headers.get("x-request-id");
|
|
25
|
+
if (middlewareRequestId) {
|
|
26
|
+
requestId = middlewareRequestId;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const { finish, logger, skipped } = createMiddlewareLogger(options, {
|
|
30
|
+
headers,
|
|
31
|
+
method,
|
|
32
|
+
path,
|
|
33
|
+
requestId
|
|
34
|
+
});
|
|
35
|
+
if (skipped) {
|
|
36
|
+
return handler(...args);
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const result = await pailStorage.run(logger, () => handler(...args));
|
|
40
|
+
const status = result instanceof Response ? result.status : 200;
|
|
41
|
+
finish({ status });
|
|
42
|
+
return result;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
const errorInstance = error instanceof Error ? error : new Error(String(error));
|
|
45
|
+
const errorStatus = errorInstance.status ?? errorInstance.statusCode ?? 500;
|
|
46
|
+
finish({ error: errorInstance });
|
|
47
|
+
logger.setStatus(errorStatus);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
export { createWithPail };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal type definitions to avoid requiring next as a dependency.
|
|
3
|
+
*/
|
|
4
|
+
interface NextRequest {
|
|
5
|
+
headers: Headers;
|
|
6
|
+
nextUrl: {
|
|
7
|
+
pathname: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
interface NextResponse {
|
|
11
|
+
headers: Headers;
|
|
12
|
+
}
|
|
13
|
+
interface NextResponseConstructor {
|
|
14
|
+
next: (options?: {
|
|
15
|
+
request?: {
|
|
16
|
+
headers?: Headers;
|
|
17
|
+
};
|
|
18
|
+
}) => NextResponse;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Options for the Next.js pail middleware.
|
|
22
|
+
*/
|
|
23
|
+
interface PailNextMiddlewareOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Glob patterns for paths to exclude from logging.
|
|
26
|
+
* Excluded paths won't get request IDs or timing headers.
|
|
27
|
+
* @example ["/_next/**", "/favicon.ico"]
|
|
28
|
+
*/
|
|
29
|
+
exclude?: string[];
|
|
30
|
+
/**
|
|
31
|
+
* Glob patterns for paths to include in logging.
|
|
32
|
+
* If not set, all non-excluded paths are included.
|
|
33
|
+
*/
|
|
34
|
+
include?: string[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a Next.js edge middleware that sets `x-request-id` and `x-pail-start`
|
|
38
|
+
* headers on the request for downstream use by `withPail()`.
|
|
39
|
+
*
|
|
40
|
+
* This middleware runs at the edge and prepares request metadata.
|
|
41
|
+
* The actual wide event logging happens in `withPail()`.
|
|
42
|
+
* @param NextResponseClass The NextResponse class from "next/server.d.ts";
|
|
43
|
+
* @param options Middleware configuration
|
|
44
|
+
* @returns A middleware function
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // middleware.ts
|
|
48
|
+
* import { NextResponse } from "next/server.d.ts";
|
|
49
|
+
* import { pailMiddleware } from "@visulima/pail/middleware/next";
|
|
50
|
+
*
|
|
51
|
+
* const middleware = pailMiddleware(NextResponse, {
|
|
52
|
+
* exclude: ["/_next/**", "/favicon.ico"],
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* export default middleware;
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare const pailMiddleware: (NextResponseClass: NextResponseConstructor, options?: PailNextMiddlewareOptions) => (request: NextRequest) => NextResponse;
|
|
59
|
+
export type { PailNextMiddlewareOptions };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { WideEvent } from "../../wide-event.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* AsyncLocalStorage instance used to propagate the WideEvent logger
|
|
5
|
+
* through Next.js server actions, route handlers, and server components.
|
|
6
|
+
*/
|
|
7
|
+
export declare const pailStorage: AsyncLocalStorage<WideEvent>;
|
|
8
|
+
/**
|
|
9
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
10
|
+
* Must be called within a `withPail()` wrapped handler or server action.
|
|
11
|
+
* @returns The request-scoped WideEvent logger
|
|
12
|
+
* @throws Error if called outside of a withPail context
|
|
13
|
+
*/
|
|
14
|
+
export declare const useLogger: () => WideEvent;
|