@lspeasy/middleware-pino 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Pradeep Mouli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # @lspeasy/middleware-pino
2
+
3
+ Pino-compatible structured logging middleware for lspeasy middleware pipelines.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @lspeasy/middleware-pino pino
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import pino from 'pino';
15
+ import { LSPClient } from '@lspeasy/client';
16
+ import { createPinoMiddleware } from '@lspeasy/middleware-pino';
17
+
18
+ const logger = pino({ level: 'info' });
19
+
20
+ const client = new LSPClient({
21
+ name: 'logging-client',
22
+ version: '1.0.0',
23
+ middleware: [
24
+ createPinoMiddleware(logger, {
25
+ level: 'debug',
26
+ includeMessageContent: false,
27
+ staticFields: {
28
+ service: 'lsp-client'
29
+ }
30
+ })
31
+ ]
32
+ });
33
+ ```
34
+
35
+ ## Options
36
+
37
+ - `level`: logging level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`)
38
+ - `includeMessageContent`: include full JSON-RPC message payload in log records
39
+ - `staticFields`: fixed metadata included in every log record
40
+ - `formatMessage`: custom formatter for emitted log messages
41
+
42
+ ## Behavior
43
+
44
+ - Emits a `before` log event prior to the next middleware/handler.
45
+ - Emits an `after` log event on successful completion with `durationMs`.
46
+ - Emits an `error` log event when downstream middleware/handler throws.
@@ -0,0 +1,3 @@
1
+ export { createPinoMiddleware } from './logger.js';
2
+ export type { PinoLikeLevel, PinoLikeLogger, PinoMiddlewareOptions } from './logger.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createPinoMiddleware } from './logger.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,33 @@
1
+ export interface MiddlewareContextLike {
2
+ direction: 'clientToServer' | 'serverToClient';
3
+ messageType: 'request' | 'response' | 'notification' | 'error';
4
+ method: string;
5
+ message: unknown;
6
+ transport: string;
7
+ }
8
+ export interface MiddlewareResultLike {
9
+ shortCircuit?: boolean;
10
+ }
11
+ export type MiddlewareLike = (context: MiddlewareContextLike, next: () => Promise<void | MiddlewareResultLike>) => Promise<void | MiddlewareResultLike>;
12
+ export type PinoLikeLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
13
+ export interface PinoLikeLogger {
14
+ trace?: (payload: Record<string, unknown>, message?: string) => void;
15
+ debug?: (payload: Record<string, unknown>, message?: string) => void;
16
+ info?: (payload: Record<string, unknown>, message?: string) => void;
17
+ warn?: (payload: Record<string, unknown>, message?: string) => void;
18
+ error?: (payload: Record<string, unknown>, message?: string) => void;
19
+ fatal?: (payload: Record<string, unknown>, message?: string) => void;
20
+ }
21
+ export interface PinoMiddlewareOptions {
22
+ level?: PinoLikeLevel;
23
+ includeMessageContent?: boolean;
24
+ staticFields?: Record<string, unknown>;
25
+ formatMessage?: (context: {
26
+ phase: 'before' | 'after' | 'error';
27
+ direction: 'clientToServer' | 'serverToClient';
28
+ method: string;
29
+ messageType: 'request' | 'response' | 'notification' | 'error';
30
+ }) => string;
31
+ }
32
+ export declare function createPinoMiddleware(logger: PinoLikeLogger, options?: PinoMiddlewareOptions): MiddlewareLike;
33
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,gBAAgB,GAAG,gBAAgB,CAAC;IAC/C,WAAW,EAAE,SAAS,GAAG,UAAU,GAAG,cAAc,GAAG,OAAO,CAAC;IAC/D,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,oBAAoB;IACnC,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,MAAM,cAAc,GAAG,CAC3B,OAAO,EAAE,qBAAqB,EAC9B,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,GAAG,oBAAoB,CAAC,KAC7C,OAAO,CAAC,IAAI,GAAG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,MAAM,EAAE,MAAM,CAAC;QACf,WAAW,EAAE,SAAS,GAAG,UAAU,GAAG,cAAc,GAAG,OAAO,CAAC;KAChE,KAAK,MAAM,CAAC;CACd;AAuCD,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,cAAc,EACtB,OAAO,GAAE,qBAA0B,GAClC,cAAc,CA2EhB"}
package/dist/logger.js ADDED
@@ -0,0 +1,83 @@
1
+ const defaultOptions = {
2
+ level: 'info',
3
+ includeMessageContent: false,
4
+ staticFields: {},
5
+ formatMessage: ({ phase, direction, method, messageType }) => `lsp.${phase} ${direction} ${messageType} ${method}`
6
+ };
7
+ function toErrorPayload(error) {
8
+ if (error instanceof Error) {
9
+ return {
10
+ name: error.name,
11
+ message: error.message,
12
+ stack: error.stack
13
+ };
14
+ }
15
+ return {
16
+ message: String(error)
17
+ };
18
+ }
19
+ function emit(logger, level, payload, message) {
20
+ const target = logger[level] ?? logger.info;
21
+ if (!target) {
22
+ return;
23
+ }
24
+ target(payload, message);
25
+ }
26
+ export function createPinoMiddleware(logger, options = {}) {
27
+ const resolved = {
28
+ ...defaultOptions,
29
+ ...options,
30
+ staticFields: {
31
+ ...defaultOptions.staticFields,
32
+ ...options.staticFields
33
+ }
34
+ };
35
+ const middleware = async (context, next) => {
36
+ const startedAt = Date.now();
37
+ const basePayload = {
38
+ ...resolved.staticFields,
39
+ direction: context.direction,
40
+ messageType: context.messageType,
41
+ method: context.method,
42
+ transport: context.transport
43
+ };
44
+ if (resolved.includeMessageContent) {
45
+ basePayload['message'] = context.message;
46
+ }
47
+ emit(logger, resolved.level, basePayload, resolved.formatMessage({
48
+ phase: 'before',
49
+ direction: context.direction,
50
+ method: context.method,
51
+ messageType: context.messageType
52
+ }));
53
+ try {
54
+ const result = await next();
55
+ emit(logger, resolved.level, {
56
+ ...basePayload,
57
+ durationMs: Date.now() - startedAt,
58
+ shortCircuit: result?.shortCircuit ?? false
59
+ }, resolved.formatMessage({
60
+ phase: 'after',
61
+ direction: context.direction,
62
+ method: context.method,
63
+ messageType: context.messageType
64
+ }));
65
+ return result;
66
+ }
67
+ catch (error) {
68
+ emit(logger, 'error', {
69
+ ...basePayload,
70
+ durationMs: Date.now() - startedAt,
71
+ error: toErrorPayload(error)
72
+ }, resolved.formatMessage({
73
+ phase: 'error',
74
+ direction: context.direction,
75
+ method: context.method,
76
+ messageType: context.messageType
77
+ }));
78
+ throw error;
79
+ }
80
+ };
81
+ return middleware;
82
+ }
83
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAwCA,MAAM,cAAc,GAEhB;IACF,KAAK,EAAE,MAAM;IACb,qBAAqB,EAAE,KAAK;IAC5B,YAAY,EAAE,EAAE;IAChB,aAAa,EAAE,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE,EAAE,CAC3D,OAAO,KAAK,IAAI,SAAS,IAAI,WAAW,IAAI,MAAM,EAAE;CACvD,CAAC;AAEF,SAAS,cAAc,CAAC,KAAc,EAA2B;IAC/D,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO;YACL,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC;IACJ,CAAC;IAED,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC;KACvB,CAAC;AAAA,CACH;AAED,SAAS,IAAI,CACX,MAAsB,EACtB,KAAoB,EACpB,OAAgC,EAChC,OAAe,EACT;IACN,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC;IAC5C,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO;IACT,CAAC;IACD,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAAA,CAC1B;AAED,MAAM,U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AS,EAAE,OAAO,CAAC,SAAS;gBAC5B,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,WAAW,EAAE,OAAO,CAAC,WAAW;aACjC,CAAC,CACH,CAAC;YACF,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CACF,MAAM,EACN,OAAO,EACP;gBACE,GAAG,WAAW;gBACd,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS;gBAClC,KAAK,EAAE,cAAc,CAAC,KAAK,CAAC;aAC7B,EACD,QAAQ,CAAC,aAAa,CAAC;gBACrB,KAAK,EAAE,OAAO;gBACd,SAAS,EAAE,OAAO,CAAC,SAAS;gBAC5B,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,WAAW,EAAE,OAAO,CAAC,WAAW;aACjC,CAAC,CACH,CAAC;YACF,MAAM,KAAK,CAAC;QACd,CAAC;IAAA,CACF,CAAC;IAEF,OAAO,UAAU,CAAC;AAAA,CACnB"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@lspeasy/middleware-pino",
3
+ "version": "1.0.1",
4
+ "description": "Pino-based structured logging middleware for lspeasy",
5
+ "keywords": [
6
+ "lsp",
7
+ "language-server-protocol",
8
+ "middleware",
9
+ "pino",
10
+ "logging"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "Pradeep Mouli",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/pradeepmouli/lspeasy.git",
17
+ "directory": "packages/middleware/pino"
18
+ },
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ }
25
+ },
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "files": [
29
+ "dist",
30
+ "README.md"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "peerDependencies": {
36
+ "pino": "^9.0.0",
37
+ "@lspeasy/core": "^1.0.1"
38
+ },
39
+ "devDependencies": {
40
+ "typescript": "^5.9.3",
41
+ "@lspeasy/core": "1.0.1"
42
+ },
43
+ "engines": {
44
+ "node": ">=20.0.0"
45
+ },
46
+ "scripts": {
47
+ "build": "tsgo --build",
48
+ "clean": "rm -rf dist *.tsbuildinfo",
49
+ "dev": "tsgo --build --watch",
50
+ "type-check": "tsgo --noEmit"
51
+ }
52
+ }