@visulima/pail 4.0.0-alpha.2 → 4.0.0-alpha.21
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 +371 -0
- package/LICENSE.md +148 -879
- package/README.md +323 -0
- package/dist/error.d.ts +103 -0
- package/dist/error.js +5 -0
- package/dist/index.browser.d.ts +557 -45
- package/dist/index.browser.js +19 -12
- package/dist/index.server.d.ts +724 -63
- package/dist/index.server.js +27 -529
- package/dist/middleware/elysia.d.ts +73 -0
- package/dist/middleware/elysia.js +1 -0
- package/dist/middleware/express.d.ts +88 -0
- package/dist/middleware/express.js +1 -0
- package/dist/middleware/fastify.d.ts +83 -0
- package/dist/middleware/fastify.js +1 -0
- package/dist/middleware/hono.d.ts +87 -0
- package/dist/middleware/hono.js +1 -0
- package/dist/middleware/next/handler.d.ts +103 -0
- package/dist/middleware/next/handler.js +1 -0
- package/dist/middleware/sveltekit.d.ts +125 -0
- package/dist/middleware/sveltekit.js +1 -0
- package/dist/object-tree.d.ts +67 -70
- package/dist/object-tree.js +2 -89
- package/dist/packem_shared/AbstractJsonReporter-CF0KKdQS.js +1 -0
- package/dist/packem_shared/AbstractJsonReporter-CVwjgboT.js +1 -0
- package/dist/packem_shared/JsonReporter-BJFnahby.js +1 -0
- package/dist/packem_shared/JsonReporter-CnLM-d0U.js +2 -0
- package/dist/packem_shared/PrettyReporter-D75CJT0C.js +5 -0
- package/dist/packem_shared/PrettyReporter-D9aBMmxI.js +45 -0
- package/dist/packem_shared/abstract-json-reporter.d-Boj5w068.d.ts +61 -0
- package/dist/packem_shared/abstract-pretty-reporter-zijeSUgy.js +34 -0
- package/dist/packem_shared/constants-DKfCaSUR.js +1 -0
- package/dist/packem_shared/constants-ep2nsfdy.js +1 -0
- package/dist/packem_shared/create-middleware-logger.d-MK7moe4r.d.ts +47 -0
- package/dist/packem_shared/createPailError-CbU1Ogfa.js +5 -0
- package/dist/packem_shared/headers-D-K_EnkH.js +1 -0
- package/dist/packem_shared/index-COQEZODZ.js +1 -0
- package/dist/packem_shared/pailMiddleware-B5DJoBYl.js +1 -0
- package/dist/packem_shared/storage-DaXAlpiO.js +1 -0
- package/dist/packem_shared/types.d-Ck_RSjLg.d.ts +246 -0
- package/dist/packem_shared/types.d-Cz9S7jZh.d.ts +201 -0
- package/dist/packem_shared/types.d-xxE3FOD3.d.ts +229 -0
- package/dist/packem_shared/useLogger-DB34CuU5.js +1 -0
- package/dist/packem_shared/wide-event.d-DkYD7YVK.d.ts +705 -0
- package/dist/packem_shared/write-console-log-based-on-level-DDSKGBKj.js +1 -0
- package/dist/packem_shared/write-stream-B6BXco86.js +1 -0
- package/dist/processor/caller/caller-processor.d.ts +49 -47
- package/dist/processor/caller/caller-processor.js +1 -59
- package/dist/processor/environment-processor.d.ts +124 -0
- package/dist/processor/environment-processor.js +1 -0
- package/dist/processor/message-formatter-processor.d.ts +51 -50
- package/dist/processor/message-formatter-processor.js +1 -66
- package/dist/processor/opentelemetry-processor.d.ts +69 -66
- package/dist/processor/opentelemetry-processor.js +1 -52
- package/dist/processor/redact-processor.d.ts +60 -41
- package/dist/processor/redact-processor.js +1 -30
- package/dist/processor/sampling-processor.d.ts +112 -0
- package/dist/processor/sampling-processor.js +1 -0
- package/dist/reporter/file/json-file-reporter.d.ts +91 -42
- package/dist/reporter/file/json-file-reporter.js +2 -136
- package/dist/reporter/http/abstract-http-reporter.d.ts +213 -211
- package/dist/reporter/http/abstract-http-reporter.js +2 -435
- package/dist/reporter/http/http-reporter.d.ts +40 -36
- package/dist/reporter/http/http-reporter.edge-light.d.ts +430 -38
- package/dist/reporter/http/http-reporter.edge-light.js +2 -651
- package/dist/reporter/http/http-reporter.js +1 -13
- package/dist/reporter/json/index.browser.d.ts +98 -3
- package/dist/reporter/json/index.browser.js +1 -2
- package/dist/reporter/json/index.d.ts +51 -3
- package/dist/reporter/json/index.js +1 -2
- package/dist/reporter/pretty/index.browser.d.ts +105 -2
- package/dist/reporter/pretty/index.browser.js +1 -1
- package/dist/reporter/pretty/index.d.ts +183 -2
- package/dist/reporter/pretty/index.js +1 -1
- package/dist/reporter/simple/simple-reporter.server.d.ts +133 -18
- package/dist/reporter/simple/simple-reporter.server.js +8 -186
- package/dist/wide-event.d.ts +5 -0
- package/dist/wide-event.js +1 -0
- package/package.json +87 -37
- package/dist/constants.d.ts +0 -37
- package/dist/interactive/index.d.ts +0 -2
- package/dist/interactive/index.js +0 -2
- package/dist/interactive/interactive-manager.d.ts +0 -108
- package/dist/interactive/interactive-stream-hook.d.ts +0 -68
- package/dist/packem_shared/AbstractJsonReporter-BaZ33PlE.js +0 -204
- package/dist/packem_shared/InteractiveManager-CbE7d1kY.js +0 -172
- package/dist/packem_shared/InteractiveStreamHook-DiSubbJ1.js +0 -21
- package/dist/packem_shared/JsonReporter-BRw4skd5.js +0 -28
- package/dist/packem_shared/JsonReporter-VzgyLEYz.js +0 -58
- package/dist/packem_shared/PrettyReporter-C2wVB7yu.js +0 -222
- package/dist/packem_shared/PrettyReporter-DySIXBjQ.js +0 -169
- package/dist/packem_shared/abstract-pretty-reporter-Di_sdm2r.js +0 -50
- package/dist/packem_shared/constants-omsTHUWB.js +0 -119
- package/dist/packem_shared/format-label-De49vNPd.js +0 -1193
- package/dist/packem_shared/get-longest-label-C9PWeyKq.js +0 -9
- package/dist/packem_shared/index-BomQ3E6J.js +0 -650
- package/dist/packem_shared/index-EZ_WSQZS.js +0 -1146
- package/dist/packem_shared/interactive-stream-hook-DG4BtN12.js +0 -141
- package/dist/packem_shared/pail.browser-u2CSR_af.js +0 -1427
- package/dist/packem_shared/write-console-log-based-on-level-DBmRYXpj.js +0 -14
- package/dist/packem_shared/write-stream-BG8fhcs3.js +0 -6
- package/dist/pail.browser.d.ts +0 -412
- package/dist/pail.server.d.ts +0 -233
- package/dist/processor/caller/get-caller-filename.d.ts +0 -23
- package/dist/progress-bar.d.ts +0 -136
- package/dist/progress-bar.js +0 -404
- package/dist/reporter/file/utils/rotating-file-stream.d.ts +0 -48
- package/dist/reporter/http/utils/compression.d.ts +0 -7
- package/dist/reporter/http/utils/log-size-error.d.ts +0 -30
- package/dist/reporter/http/utils/retry.d.ts +0 -27
- package/dist/reporter/json/abstract-json-reporter.d.ts +0 -61
- package/dist/reporter/json/json-reporter.browser.d.ts +0 -40
- package/dist/reporter/json/json-reporter.server.d.ts +0 -50
- package/dist/reporter/pretty/abstract-pretty-reporter.d.ts +0 -83
- package/dist/reporter/pretty/pretty-reporter.browser.d.ts +0 -36
- package/dist/reporter/pretty/pretty-reporter.server.d.ts +0 -70
- package/dist/reporter/raw/raw-reporter.browser.d.ts +0 -5
- package/dist/reporter/raw/raw-reporter.server.d.ts +0 -13
- package/dist/reporter/utils/default-inspector-config.d.ts +0 -3
- package/dist/reporter/utils/format-label.d.ts +0 -3
- package/dist/spinner.d.ts +0 -220
- package/dist/spinner.js +0 -2150
- package/dist/types.d.ts +0 -241
- package/dist/utils/ansi-escapes.d.ts +0 -4
- package/dist/utils/arrayify.d.ts +0 -2
- package/dist/utils/get-longest-badge.d.ts +0 -4
- package/dist/utils/get-longest-label.d.ts +0 -4
- package/dist/utils/merge-types.d.ts +0 -4
- package/dist/utils/stream/safe-stream-handler.d.ts +0 -21
- package/dist/utils/write-console-log-based-on-level.d.ts +0 -4
- package/dist/utils/write-stream.d.ts +0 -2
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { W as WideEvent } from "../packem_shared/wide-event.d-DkYD7YVK.js";
|
|
2
|
+
import { P as PailMiddlewareOptions } from "../packem_shared/create-middleware-logger.d-MK7moe4r.js";
|
|
3
|
+
import 'safe-stable-stringify';
|
|
4
|
+
import "../packem_shared/types.d-Ck_RSjLg.js";
|
|
5
|
+
import '@visulima/colorize';
|
|
6
|
+
import '@visulima/interactive-manager';
|
|
7
|
+
/**
|
|
8
|
+
* An Elysia-like instance that supports the derive/hook registration API.
|
|
9
|
+
* Compatible with Elysia v1+.
|
|
10
|
+
*/
|
|
11
|
+
interface PailElysiaInstance {
|
|
12
|
+
derive: (options: {
|
|
13
|
+
as: string;
|
|
14
|
+
}, handler: (context: {
|
|
15
|
+
request: Request;
|
|
16
|
+
}) => Record<string, unknown>) => PailElysiaInstance;
|
|
17
|
+
onAfterHandle: (options: {
|
|
18
|
+
as: string;
|
|
19
|
+
}, handler: (context: {
|
|
20
|
+
request: Request;
|
|
21
|
+
set: {
|
|
22
|
+
status?: number;
|
|
23
|
+
};
|
|
24
|
+
}) => Promise<void>) => PailElysiaInstance;
|
|
25
|
+
onError: (options: {
|
|
26
|
+
as: string;
|
|
27
|
+
}, handler: (context: {
|
|
28
|
+
error: Error;
|
|
29
|
+
request: Request;
|
|
30
|
+
}) => Promise<void>) => PailElysiaInstance;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
34
|
+
* Must be called within a request handler where pail middleware is active.
|
|
35
|
+
* @returns The request-scoped WideEvent logger
|
|
36
|
+
* @throws Error if called outside of the middleware context
|
|
37
|
+
*/
|
|
38
|
+
declare const useLogger: () => WideEvent;
|
|
39
|
+
/**
|
|
40
|
+
* Configuration options for the Elysia pail plugin.
|
|
41
|
+
* @template T - Custom logger type names from the pail instance
|
|
42
|
+
*/
|
|
43
|
+
type ElysiaPluginOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Register pail wide event logging as an Elysia plugin.
|
|
46
|
+
*
|
|
47
|
+
* Injects a `log` property into handler context via `derive`, accessible via:
|
|
48
|
+
* - `context.log` in route handlers (auto-injected)
|
|
49
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
50
|
+
*
|
|
51
|
+
* The wide event is automatically emitted on response or error.
|
|
52
|
+
* @param app The Elysia instance
|
|
53
|
+
* @param options Plugin configuration
|
|
54
|
+
* @returns The Elysia instance (for chaining)
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* import { Elysia } from "elysia";
|
|
58
|
+
* import { createPail } from "@visulima/pail";
|
|
59
|
+
* import { pailPlugin, useLogger } from "@visulima/pail/middleware/elysia";
|
|
60
|
+
*
|
|
61
|
+
* const logger = createPail();
|
|
62
|
+
* const app = new Elysia();
|
|
63
|
+
*
|
|
64
|
+
* pailPlugin(app, { pail: logger });
|
|
65
|
+
*
|
|
66
|
+
* app.get("/api/users", ({ log }) => {
|
|
67
|
+
* log.set({ user: { id: 1 } });
|
|
68
|
+
* return { ok: true };
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare const pailPlugin: <T extends string = string>(app: PailElysiaInstance, options: ElysiaPluginOptions<T>) => PailElysiaInstance;
|
|
73
|
+
export { type ElysiaPluginOptions, type PailElysiaInstance, type WideEvent, pailPlugin, useLogger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createRequire as u}from"node:module";import{a as m,c as _}from"../packem_shared/headers-D-K_EnkH.js";const p=u(import.meta.url),i=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,h=s=>{if(typeof i<"u"&&i.versions&&i.versions.node){const[a,o]=i.versions.node.split(".").map(Number);if(a>22||a===22&&o>=3||a===20&&o>=16)return i.getBuiltinModule(s)}return p(s)},{AsyncLocalStorage:f}=h("node:async_hooks"),g=new f,d=new WeakSet,w=()=>{const s=g.getStore();if(!s||!d.has(s))throw new Error("[pail] useLogger() called outside of Elysia pail plugin context.");return s},b=(s,a)=>{const o=new WeakMap,l=new WeakSet;return s.derive({as:"global"},({request:e})=>{const t=new URL(e.url),r=e.headers.get("x-request-id")??crypto.randomUUID(),c=m(e.headers),n=_(a,{headers:c,method:e.method,path:t.pathname,requestId:r});return o.set(e,n),n.skipped||(d.add(n.logger),g.enterWith(n.logger)),{log:n.logger}}).onAfterHandle({as:"global"},async({request:e,set:t})=>{const r=o.get(e);!r?.skipped&&r&&!l.has(e)&&(l.add(e),r.finish({status:t.status??200}),d.delete(r.logger))}).onError({as:"global"},async({error:e,request:t})=>{const r=o.get(t);!r?.skipped&&r&&!l.has(t)&&(l.add(t),r.finish({error:e}),d.delete(r.logger))})};export{b as pailPlugin,w as useLogger};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { W as WideEvent } from "../packem_shared/wide-event.d-DkYD7YVK.js";
|
|
2
|
+
import { P as PailMiddlewareOptions } from "../packem_shared/create-middleware-logger.d-MK7moe4r.js";
|
|
3
|
+
import 'safe-stable-stringify';
|
|
4
|
+
import "../packem_shared/types.d-Ck_RSjLg.js";
|
|
5
|
+
import '@visulima/colorize';
|
|
6
|
+
import '@visulima/interactive-manager';
|
|
7
|
+
/**
|
|
8
|
+
* An Express-like request object with the pail logger attached.
|
|
9
|
+
* Use this to type your route handlers when accessing `req.log`.
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import type { PailRequest } from "@visulima/pail/middleware/express";
|
|
13
|
+
*
|
|
14
|
+
* app.get("/api/users", (req: PailRequest, res) => {
|
|
15
|
+
* req.log?.set({ user: { id: 1 } });
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface PailRequest {
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
headers: Record<string, string | string[] | undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* The request-scoped WideEvent logger, attached by pail middleware.
|
|
24
|
+
* Only present for non-excluded routes.
|
|
25
|
+
*/
|
|
26
|
+
log?: WideEvent;
|
|
27
|
+
method: string;
|
|
28
|
+
originalUrl?: string;
|
|
29
|
+
path: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* An Express-like response object.
|
|
33
|
+
*/
|
|
34
|
+
interface PailResponse {
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
on: (event: string, listener: () => void) => void;
|
|
37
|
+
statusCode: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Express-compatible next function.
|
|
41
|
+
*/
|
|
42
|
+
type PailNextFunction = (error?: unknown) => void;
|
|
43
|
+
/**
|
|
44
|
+
* The Express middleware function signature returned by `pailMiddleware()`.
|
|
45
|
+
*/
|
|
46
|
+
type PailExpressMiddleware = (request: PailRequest, response: PailResponse, next: PailNextFunction) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
49
|
+
* Must be called within a request handled by the pail Express middleware.
|
|
50
|
+
* @returns The request-scoped WideEvent logger
|
|
51
|
+
* @throws Error if called outside of the middleware context
|
|
52
|
+
*/
|
|
53
|
+
declare const useLogger: () => WideEvent;
|
|
54
|
+
/**
|
|
55
|
+
* Configuration options for the Express pail middleware.
|
|
56
|
+
* @template T - Custom logger type names from the pail instance
|
|
57
|
+
*/
|
|
58
|
+
type ExpressMiddlewareOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Create Express middleware that attaches a WideEvent logger to each request.
|
|
61
|
+
*
|
|
62
|
+
* The logger is available via:
|
|
63
|
+
* - `req.log` on the request object
|
|
64
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
65
|
+
*
|
|
66
|
+
* The wide event is automatically emitted when the response finishes.
|
|
67
|
+
* @param options Middleware configuration
|
|
68
|
+
* @returns Express middleware function
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import express from "express";
|
|
72
|
+
* import { createPail } from "@visulima/pail";
|
|
73
|
+
* import { pailMiddleware, useLogger } from "@visulima/pail/middleware/express";
|
|
74
|
+
*
|
|
75
|
+
* const app = express();
|
|
76
|
+
* const logger = createPail();
|
|
77
|
+
*
|
|
78
|
+
* app.use(pailMiddleware({ pail: logger }));
|
|
79
|
+
*
|
|
80
|
+
* app.get("/api/users", (req, res) => {
|
|
81
|
+
* req.log.set({ user: { id: 1 } });
|
|
82
|
+
* // or: useLogger().set({ user: { id: 1 } });
|
|
83
|
+
* res.json({ ok: true });
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare const pailMiddleware: <T extends string = string>(options: ExpressMiddlewareOptions<T>) => PailExpressMiddleware;
|
|
88
|
+
export { type ExpressMiddlewareOptions, type PailExpressMiddleware, type PailNextFunction, type PailRequest, type PailResponse, type WideEvent, pailMiddleware, useLogger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{e as u,c as p}from"../packem_shared/headers-D-K_EnkH.js";import{c}from"../packem_shared/storage-DaXAlpiO.js";const a=c("Express middleware context. Make sure pail middleware is registered before your route handlers."),f=()=>a.useLogger(),x=t=>(e,r,s)=>{const d=e.headers["x-request-id"]??crypto.randomUUID(),i=e.originalUrl??e.url??"/",g=u(e.headers),{finish:l,logger:o,skipped:n}=p(t,{headers:g,method:e.method,path:i,requestId:d});if(n){s();return}e.log=o,r.on("finish",()=>{l({status:r.statusCode})}),a.storage.run(o,()=>{s()})};export{x as pailMiddleware,f as useLogger};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { W as WideEvent } from "../packem_shared/wide-event.d-DkYD7YVK.js";
|
|
2
|
+
import { P as PailMiddlewareOptions } from "../packem_shared/create-middleware-logger.d-MK7moe4r.js";
|
|
3
|
+
import 'safe-stable-stringify';
|
|
4
|
+
import "../packem_shared/types.d-Ck_RSjLg.js";
|
|
5
|
+
import '@visulima/colorize';
|
|
6
|
+
import '@visulima/interactive-manager';
|
|
7
|
+
/**
|
|
8
|
+
* A Fastify-like request object with the pail logger attached.
|
|
9
|
+
* Use this to type your route handlers when accessing `request.log`.
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import type { PailFastifyRequest } from "@visulima/pail/middleware/fastify";
|
|
13
|
+
*
|
|
14
|
+
* app.get("/api/users", async (request: PailFastifyRequest, reply) => {
|
|
15
|
+
* request.log?.set({ user: { id: 1 } });
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface PailFastifyRequest {
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
headers: Record<string, string | string[] | undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* The request-scoped WideEvent logger, attached by pail plugin.
|
|
24
|
+
* Only present for non-excluded routes.
|
|
25
|
+
*/
|
|
26
|
+
log?: WideEvent;
|
|
27
|
+
method: string;
|
|
28
|
+
url: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A Fastify-like reply object.
|
|
32
|
+
*/
|
|
33
|
+
interface PailFastifyReply {
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
statusCode: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A Fastify-like instance that supports the hook registration API.
|
|
39
|
+
*/
|
|
40
|
+
interface PailFastifyInstance {
|
|
41
|
+
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);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
45
|
+
* Must be called within a request handled by the pail Fastify plugin.
|
|
46
|
+
* @returns The request-scoped WideEvent logger
|
|
47
|
+
* @throws Error if called outside of the plugin context
|
|
48
|
+
*/
|
|
49
|
+
declare const useLogger: () => WideEvent;
|
|
50
|
+
/**
|
|
51
|
+
* Configuration options for the Fastify pail plugin.
|
|
52
|
+
* @template T - Custom logger type names from the pail instance
|
|
53
|
+
*/
|
|
54
|
+
type FastifyPluginOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Register pail wide event logging as a Fastify plugin.
|
|
57
|
+
*
|
|
58
|
+
* Attaches a WideEvent logger to each request, accessible via:
|
|
59
|
+
* - `request.log` on the Fastify request object
|
|
60
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
61
|
+
*
|
|
62
|
+
* The wide event is automatically emitted on response or error.
|
|
63
|
+
* @param fastify The Fastify instance
|
|
64
|
+
* @param options Plugin configuration
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import Fastify from "fastify";
|
|
68
|
+
* import { createPail } from "@visulima/pail";
|
|
69
|
+
* import { pailPlugin, useLogger } from "@visulima/pail/middleware/fastify";
|
|
70
|
+
*
|
|
71
|
+
* const app = Fastify();
|
|
72
|
+
* const logger = createPail();
|
|
73
|
+
*
|
|
74
|
+
* pailPlugin(app, { pail: logger });
|
|
75
|
+
*
|
|
76
|
+
* app.get("/api/users", async (request, reply) => {
|
|
77
|
+
* request.log.set({ user: { id: 1 } });
|
|
78
|
+
* return { ok: true };
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare const pailPlugin: <T extends string = string>(fastify: PailFastifyInstance, options: FastifyPluginOptions<T>) => void;
|
|
83
|
+
export { type FastifyPluginOptions, type PailFastifyInstance, type PailFastifyReply, type PailFastifyRequest, type WideEvent, pailPlugin, useLogger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{e as p,c as f}from"../packem_shared/headers-D-K_EnkH.js";import{c as l}from"../packem_shared/storage-DaXAlpiO.js";const i=l("Fastify middleware context. Make sure pail plugin is registered before your route handlers."),y=()=>i.useLogger(),w=(a,g)=>{const t=new WeakMap,s=new WeakSet;a.addHook("onRequest",(e,d,r)=>{const o=e.headers["x-request-id"]??crypto.randomUUID(),c=p(e.headers),{finish:u,logger:n,skipped:h}=f(g,{headers:c,method:e.method,path:e.url,requestId:o});if(h){r();return}e.log=n,t.set(e,{finish:u}),i.storage.run(n,()=>{r()})}),a.addHook("onResponse",async(e,d)=>{const r=t.get(e);!r||s.has(e)||(s.add(e),r.finish({status:d.statusCode}))}),a.addHook("onError",async(e,d,r)=>{const o=t.get(e);!o||s.has(e)||(s.add(e),o.finish({error:r}))})};export{w as pailPlugin,y as useLogger};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { W as WideEvent } from "../packem_shared/wide-event.d-DkYD7YVK.js";
|
|
2
|
+
import { P as PailMiddlewareOptions } from "../packem_shared/create-middleware-logger.d-MK7moe4r.js";
|
|
3
|
+
import 'safe-stable-stringify';
|
|
4
|
+
import "../packem_shared/types.d-Ck_RSjLg.js";
|
|
5
|
+
import '@visulima/colorize';
|
|
6
|
+
import '@visulima/interactive-manager';
|
|
7
|
+
/**
|
|
8
|
+
* A Hono-like context object with access to the pail logger.
|
|
9
|
+
*
|
|
10
|
+
* Use `c.get("log")` or `useLogger(c)` to access the WideEvent logger.
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import type { PailHonoContext } from "@visulima/pail/middleware/hono";
|
|
14
|
+
*
|
|
15
|
+
* app.get("/api/users", (c: PailHonoContext) => {
|
|
16
|
+
* const log = c.get("log");
|
|
17
|
+
* log.set({ user: { id: 1 } });
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
interface PailHonoContext {
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
get: (key: string) => unknown;
|
|
24
|
+
req: {
|
|
25
|
+
header: (name: string) => string | undefined;
|
|
26
|
+
method: string;
|
|
27
|
+
path: string;
|
|
28
|
+
raw: {
|
|
29
|
+
headers: Headers;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
res: {
|
|
33
|
+
status: number;
|
|
34
|
+
};
|
|
35
|
+
set: (key: string, value: unknown) => void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Hono-compatible next function.
|
|
39
|
+
*/
|
|
40
|
+
type PailHonoNext = () => Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* The Hono middleware function signature returned by `pailMiddleware()`.
|
|
43
|
+
*/
|
|
44
|
+
type PailHonoMiddleware = (c: PailHonoContext, next: PailHonoNext) => Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Configuration options for the Hono pail middleware.
|
|
47
|
+
* @template T - Custom logger type names from the pail instance
|
|
48
|
+
*/
|
|
49
|
+
type HonoMiddlewareOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieve the WideEvent logger from Hono context.
|
|
52
|
+
* Must be called within a request handler where pail middleware is active.
|
|
53
|
+
* @param c The Hono context object
|
|
54
|
+
* @returns The request-scoped WideEvent logger
|
|
55
|
+
* @throws Error if the middleware is not registered
|
|
56
|
+
*/
|
|
57
|
+
declare const useLogger: (c: PailHonoContext) => WideEvent;
|
|
58
|
+
/**
|
|
59
|
+
* Create Hono middleware that attaches a WideEvent logger to each request.
|
|
60
|
+
*
|
|
61
|
+
* The logger is available via:
|
|
62
|
+
* - `c.get("log")` on the Hono context
|
|
63
|
+
* - `useLogger(c)` helper function
|
|
64
|
+
*
|
|
65
|
+
* The wide event is automatically emitted when the handler completes or throws.
|
|
66
|
+
* @param options Middleware configuration
|
|
67
|
+
* @returns Hono middleware function
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { Hono } from "hono";
|
|
71
|
+
* import { createPail } from "@visulima/pail";
|
|
72
|
+
* import { pailMiddleware, useLogger } from "@visulima/pail/middleware/hono";
|
|
73
|
+
*
|
|
74
|
+
* const app = new Hono();
|
|
75
|
+
* const logger = createPail();
|
|
76
|
+
*
|
|
77
|
+
* app.use("*", pailMiddleware({ pail: logger }));
|
|
78
|
+
*
|
|
79
|
+
* app.get("/api/users", (c) => {
|
|
80
|
+
* const log = useLogger(c);
|
|
81
|
+
* log.set({ user: { id: 1 } });
|
|
82
|
+
* return c.json({ ok: true });
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare const pailMiddleware: <T extends string = string>(options: HonoMiddlewareOptions<T>) => PailHonoMiddleware;
|
|
87
|
+
export { type HonoMiddlewareOptions, type PailHonoContext, type PailHonoMiddleware, type PailHonoNext, type WideEvent, pailMiddleware, useLogger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as c,c as g}from"../packem_shared/headers-D-K_EnkH.js";const l=t=>{const r=t.get("log");if(!r)throw new Error("[pail] useLogger() called but pail middleware is not registered.");return r},p=t=>async(r,a)=>{const o=r.req.header("x-request-id")??crypto.randomUUID(),i=c(r.req.raw.headers),{finish:s,logger:d,skipped:n}=g(t,{headers:i,method:r.req.method,path:r.req.path,requestId:o});if(n){await a();return}r.set("log",d);try{await a(),s({status:r.res.status})}catch(e){throw s({error:e instanceof Error?e:new Error(String(e))}),e}};export{p as pailMiddleware,l as useLogger};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { P as PailMiddlewareOptions } from "../../packem_shared/create-middleware-logger.d-MK7moe4r.js";
|
|
2
|
+
import { W as WideEvent } from "../../packem_shared/wide-event.d-DkYD7YVK.js";
|
|
3
|
+
import 'node:async_hooks';
|
|
4
|
+
import "../../packem_shared/types.d-Ck_RSjLg.js";
|
|
5
|
+
import '@visulima/colorize';
|
|
6
|
+
import '@visulima/interactive-manager';
|
|
7
|
+
import 'safe-stable-stringify';
|
|
8
|
+
/**
|
|
9
|
+
* Minimal type definitions to avoid requiring next as a dependency.
|
|
10
|
+
*/
|
|
11
|
+
interface NextRequest {
|
|
12
|
+
headers: Headers;
|
|
13
|
+
nextUrl: {
|
|
14
|
+
pathname: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
interface NextResponse {
|
|
18
|
+
headers: Headers;
|
|
19
|
+
}
|
|
20
|
+
interface NextResponseConstructor {
|
|
21
|
+
next: (options?: {
|
|
22
|
+
request?: {
|
|
23
|
+
headers?: Headers;
|
|
24
|
+
};
|
|
25
|
+
}) => NextResponse;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Options for the Next.js pail middleware.
|
|
29
|
+
*/
|
|
30
|
+
interface PailNextMiddlewareOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Glob patterns for paths to exclude from logging.
|
|
33
|
+
* Excluded paths won't get request IDs or timing headers.
|
|
34
|
+
* @example ["/_next/**", "/favicon.ico"]
|
|
35
|
+
*/
|
|
36
|
+
exclude?: string[];
|
|
37
|
+
/**
|
|
38
|
+
* Glob patterns for paths to include in logging.
|
|
39
|
+
* If not set, all non-excluded paths are included.
|
|
40
|
+
*/
|
|
41
|
+
include?: string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a Next.js edge middleware that sets `x-request-id` and `x-pail-start`
|
|
45
|
+
* headers on the request for downstream use by `withPail()`.
|
|
46
|
+
*
|
|
47
|
+
* This middleware runs at the edge and prepares request metadata.
|
|
48
|
+
* The actual wide event logging happens in `withPail()`.
|
|
49
|
+
* @param NextResponseClass The NextResponse class from "next/server.js"
|
|
50
|
+
* @param options Middleware configuration
|
|
51
|
+
* @returns A middleware function
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // middleware.ts
|
|
55
|
+
* import { NextResponse } from "next/server.js";
|
|
56
|
+
* import { pailMiddleware } from "@visulima/pail/middleware/next";
|
|
57
|
+
*
|
|
58
|
+
* const middleware = pailMiddleware(NextResponse, {
|
|
59
|
+
* exclude: ["/_next/**", "/favicon.ico"],
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* export default middleware;
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
declare const pailMiddleware: (NextResponseClass: NextResponseConstructor, options?: PailNextMiddlewareOptions) => (request: NextRequest) => NextResponse;
|
|
66
|
+
/**
|
|
67
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
68
|
+
* Must be called within a `withPail()` wrapped handler or server action.
|
|
69
|
+
* @returns The request-scoped WideEvent logger
|
|
70
|
+
* @throws Error if called outside of a withPail context
|
|
71
|
+
*/
|
|
72
|
+
declare const useLogger: () => WideEvent;
|
|
73
|
+
type NextPailOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
74
|
+
/**
|
|
75
|
+
* Create a `withPail` wrapper function for Next.js route handlers and server actions.
|
|
76
|
+
*
|
|
77
|
+
* Wraps handler execution in AsyncLocalStorage so that `useLogger()` works
|
|
78
|
+
* anywhere in the async call stack. The wide event is automatically emitted
|
|
79
|
+
* when the handler completes or throws.
|
|
80
|
+
* @param options Configuration options
|
|
81
|
+
* @returns A `withPail` wrapper function
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // lib/pail.ts
|
|
85
|
+
* import { createPail } from "@visulima/pail";
|
|
86
|
+
* import { createWithPail } from "@visulima/pail/middleware/next";
|
|
87
|
+
*
|
|
88
|
+
* const logger = createPail();
|
|
89
|
+
* export const withPail = createWithPail({ pail: logger });
|
|
90
|
+
*
|
|
91
|
+
* // app/api/users/route.ts
|
|
92
|
+
* import { withPail } from "@/lib/pail";
|
|
93
|
+
* import { useLogger } from "@visulima/pail/middleware/next";
|
|
94
|
+
*
|
|
95
|
+
* export const GET = withPail(async (request: Request) => {
|
|
96
|
+
* const log = useLogger();
|
|
97
|
+
* log.set({ user: { id: 1 } });
|
|
98
|
+
* return Response.json({ ok: true });
|
|
99
|
+
* });
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
declare const createWithPail: <T extends string = string>(options: NextPailOptions<T>) => <TArgs extends unknown[], TReturn>(handler: (...args: TArgs) => Promise<TReturn> | TReturn) => (...args: TArgs) => Promise<TReturn>;
|
|
103
|
+
export { type NextPailOptions, type PailNextMiddlewareOptions, type WideEvent, createWithPail, pailMiddleware, useLogger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as m,c as l}from"../../packem_shared/headers-D-K_EnkH.js";import{pailStorage as w}from"../../packem_shared/useLogger-DB34CuU5.js";import{useLogger as N}from"../../packem_shared/useLogger-DB34CuU5.js";import{pailMiddleware as S}from"../../packem_shared/pailMiddleware-B5DJoBYl.js";const q=u=>s=>async(...a)=>{const[o]=a,f=o instanceof Request;let n="UNKNOWN",i="/",c=crypto.randomUUID(),d={};if(f){const e=o,t=new URL(e.url);n=e.method,i=t.pathname,d=m(e.headers);const r=e.headers.get("x-request-id");r&&(c=r)}const{finish:p,logger:h,skipped:g}=l(u,{headers:d,method:n,path:i,requestId:c});if(g)return s(...a);try{const e=await w.run(h,()=>s(...a)),t=e instanceof Response?e.status:200;return p({status:t}),e}catch(e){const t=e instanceof Error?e:new Error(String(e)),r=t.status??t.statusCode??500;throw p({error:t,status:r}),e}};export{q as createWithPail,S as pailMiddleware,N as useLogger};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { W as WideEvent } from "../packem_shared/wide-event.d-DkYD7YVK.js";
|
|
2
|
+
import { P as PailMiddlewareOptions } from "../packem_shared/create-middleware-logger.d-MK7moe4r.js";
|
|
3
|
+
import 'safe-stable-stringify';
|
|
4
|
+
import "../packem_shared/types.d-Ck_RSjLg.js";
|
|
5
|
+
import '@visulima/colorize';
|
|
6
|
+
import '@visulima/interactive-manager';
|
|
7
|
+
/**
|
|
8
|
+
* A SvelteKit-like event object with access to the pail logger via `locals.log`.
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // In a SvelteKit load function or action:
|
|
12
|
+
* import type { PailSvelteKitEvent } from "@visulima/pail/middleware/sveltekit";
|
|
13
|
+
*
|
|
14
|
+
* export const load = async (event: PailSvelteKitEvent) => {
|
|
15
|
+
* event.locals.log?.set({ user: { id: 1 } });
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface PailSvelteKitEvent {
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
locals: Record<string, unknown> & {
|
|
22
|
+
log?: WideEvent;
|
|
23
|
+
};
|
|
24
|
+
request: Request;
|
|
25
|
+
url: URL;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Options passed to SvelteKit's resolve function.
|
|
29
|
+
*/
|
|
30
|
+
interface PailSvelteKitResolveOptions {
|
|
31
|
+
filterSerializedResponseHeaders?: (name: string) => boolean;
|
|
32
|
+
preload?: (input: {
|
|
33
|
+
type: string;
|
|
34
|
+
}) => boolean;
|
|
35
|
+
transformPageChunk?: (input: {
|
|
36
|
+
html: string;
|
|
37
|
+
}) => string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* SvelteKit resolve function type.
|
|
41
|
+
*/
|
|
42
|
+
type PailSvelteKitResolve = (event: PailSvelteKitEvent, options?: PailSvelteKitResolveOptions) => Promise<Response>;
|
|
43
|
+
/**
|
|
44
|
+
* Input for the SvelteKit handle hook.
|
|
45
|
+
*/
|
|
46
|
+
interface PailSvelteKitHandleInput {
|
|
47
|
+
event: PailSvelteKitEvent;
|
|
48
|
+
resolve: PailSvelteKitResolve;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* SvelteKit handle hook function type returned by `pailHandle()`.
|
|
52
|
+
*/
|
|
53
|
+
type PailSvelteKitHandle = (input: PailSvelteKitHandleInput) => Promise<Response>;
|
|
54
|
+
/**
|
|
55
|
+
* Input for the SvelteKit handleError hook.
|
|
56
|
+
*/
|
|
57
|
+
interface PailSvelteKitHandleErrorInput {
|
|
58
|
+
error: unknown;
|
|
59
|
+
event: PailSvelteKitEvent;
|
|
60
|
+
message: string;
|
|
61
|
+
status: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* SvelteKit handleError hook function type returned by `pailHandleError()`.
|
|
65
|
+
*/
|
|
66
|
+
type PailSvelteKitHandleError = (input: PailSvelteKitHandleErrorInput) => void;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
|
|
69
|
+
* Must be called within a request handled by the pail SvelteKit handle hook.
|
|
70
|
+
* @returns The request-scoped WideEvent logger
|
|
71
|
+
* @throws Error if called outside of the hook context
|
|
72
|
+
*/
|
|
73
|
+
declare const useLogger: () => WideEvent;
|
|
74
|
+
type SvelteKitHandleOptions<T extends string = string> = PailMiddlewareOptions<T>;
|
|
75
|
+
/**
|
|
76
|
+
* Create a SvelteKit `handle` hook that attaches a WideEvent logger to each request.
|
|
77
|
+
*
|
|
78
|
+
* The logger is available via:
|
|
79
|
+
* - `event.locals.log` in server load functions and actions
|
|
80
|
+
* - `useLogger()` from anywhere in the async call stack
|
|
81
|
+
*
|
|
82
|
+
* The wide event is automatically emitted when the response is sent or an error occurs.
|
|
83
|
+
* @param options Hook configuration
|
|
84
|
+
* @returns SvelteKit handle hook function
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // src/hooks.server.ts
|
|
88
|
+
* import { createPail } from "@visulima/pail";
|
|
89
|
+
* import { pailHandle, pailHandleError } from "@visulima/pail/middleware/sveltekit";
|
|
90
|
+
*
|
|
91
|
+
* const logger = createPail();
|
|
92
|
+
*
|
|
93
|
+
* export const handle = pailHandle({ pail: logger });
|
|
94
|
+
* export const handleError = pailHandleError();
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare const pailHandle: <T extends string = string>(options: SvelteKitHandleOptions<T>) => PailSvelteKitHandle;
|
|
98
|
+
/**
|
|
99
|
+
* Create a SvelteKit `handleError` hook that captures errors into the WideEvent logger.
|
|
100
|
+
*
|
|
101
|
+
* Should be used alongside `pailHandle` to ensure errors are recorded in the wide event.
|
|
102
|
+
* @returns SvelteKit handleError hook function
|
|
103
|
+
*/
|
|
104
|
+
declare const pailHandleError: () => PailSvelteKitHandleError;
|
|
105
|
+
/**
|
|
106
|
+
* Convenience function that returns both handle and handleError hooks.
|
|
107
|
+
* @param options Hook configuration
|
|
108
|
+
* @returns Object with `handle` and `handleError` hooks
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // src/hooks.server.ts
|
|
112
|
+
* import { createPail } from "@visulima/pail";
|
|
113
|
+
* import { createPailHooks } from "@visulima/pail/middleware/sveltekit";
|
|
114
|
+
*
|
|
115
|
+
* const logger = createPail();
|
|
116
|
+
* const { handle, handleError } = createPailHooks({ pail: logger });
|
|
117
|
+
*
|
|
118
|
+
* export { handle, handleError };
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare const createPailHooks: <T extends string = string>(options: SvelteKitHandleOptions<T>) => {
|
|
122
|
+
handle: PailSvelteKitHandle;
|
|
123
|
+
handleError: PailSvelteKitHandleError;
|
|
124
|
+
};
|
|
125
|
+
export { type PailSvelteKitEvent, type PailSvelteKitHandle, type PailSvelteKitHandleError, type PailSvelteKitHandleErrorInput, type PailSvelteKitHandleInput, type PailSvelteKitResolve, type PailSvelteKitResolveOptions, type SvelteKitHandleOptions, type WideEvent, createPailHooks, pailHandle, pailHandleError, useLogger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as g,c as i}from"../packem_shared/headers-D-K_EnkH.js";import{c as u}from"../packem_shared/storage-DaXAlpiO.js";const n=u("SvelteKit handle hook context. Make sure pailHandle is added to your hooks.server.ts."),k=()=>n.useLogger(),h=e=>async({event:r,resolve:a})=>{const d=r.request.headers.get("x-request-id")??crypto.randomUUID(),l=g(r.request.headers),{finish:t,logger:s,skipped:c}=i(e,{headers:l,method:r.request.method,path:r.url.pathname,requestId:d});return c?a(r):(r.locals.log=s,n.storage.run(s,async()=>{try{const o=await a(r);return t({status:o.status}),o}catch(o){throw t({error:o instanceof Error?o:new Error(String(o))}),o}}))},p=()=>({error:e,event:r})=>{const a=r.locals.log;a&&e instanceof Error&&a.error(e.message,e)},q=e=>({handle:h(e),handleError:p()});export{q as createPailHooks,h as pailHandle,p as pailHandleError,k as useLogger};
|