@orpc/pino 2.0.0-beta.48 → 2.0.0-beta.49
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 +4 -4
- package/dist/context.d.ts +22 -0
- package/dist/context.js +15 -0
- package/dist/handler-plugin.d.ts +63 -0
- package/dist/handler-plugin.js +167 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +27 -29
- package/dist/context.d.mts +0 -22
- package/dist/context.mjs +0 -6
- package/dist/handler-plugin.d.mts +0 -63
- package/dist/handler-plugin.mjs +0 -139
- package/dist/index.d.mts +0 -2
- package/dist/index.mjs +0 -2
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/pino">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fpino?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
208
209
|
<tr>
|
|
209
210
|
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
211
|
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
-
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
212
212
|
</tr>
|
|
213
213
|
</table>
|
|
214
214
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Logger } from 'pino';
|
|
2
|
+
/**
|
|
3
|
+
* Context symbol under which the Pino logger is stored. Use it to provide a
|
|
4
|
+
* custom logger instance per request, e.g. when integrating with pino-http.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#providing-custom-logger-per-request | Pino Integration - Providing Custom Logger per Request}
|
|
7
|
+
*/
|
|
8
|
+
export declare const LOGGER_CONTEXT_SYMBOL: unique symbol;
|
|
9
|
+
/**
|
|
10
|
+
* Context shape that carries the Pino logger injected by `PinoHandlerPlugin`.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
13
|
+
*/
|
|
14
|
+
export interface LoggerContext {
|
|
15
|
+
[LOGGER_CONTEXT_SYMBOL]?: Logger | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the Pino logger from the oRPC context, if available.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
21
|
+
*/
|
|
22
|
+
export declare function getLogger(context: LoggerContext): Logger | undefined;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context symbol under which the Pino logger is stored. Use it to provide a
|
|
3
|
+
* custom logger instance per request, e.g. when integrating with pino-http.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#providing-custom-logger-per-request | Pino Integration - Providing Custom Logger per Request}
|
|
6
|
+
*/
|
|
7
|
+
export const LOGGER_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_PINO_LOGGER_CONTEXT');
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves the Pino logger from the oRPC context, if available.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
12
|
+
*/
|
|
13
|
+
export function getLogger(context) {
|
|
14
|
+
return context[LOGGER_CONTEXT_SYMBOL];
|
|
15
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Context } from '@orpc/server';
|
|
2
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from '@orpc/server/standard';
|
|
3
|
+
import type { Logger } from 'pino';
|
|
4
|
+
import pino from 'pino';
|
|
5
|
+
export interface PinoHandlerPluginOptions<T extends Context> {
|
|
6
|
+
/**
|
|
7
|
+
* Logger instance to use for logging.
|
|
8
|
+
*
|
|
9
|
+
* @default pino()
|
|
10
|
+
*/
|
|
11
|
+
logger?: Logger | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Function to generate a unique ID for each request.
|
|
14
|
+
*
|
|
15
|
+
* @default ({ request }) => flattenStandardHeader(request.headers['x-request-id']) ?? crypto.randomUUID()
|
|
16
|
+
*/
|
|
17
|
+
generateRequestId?: ((options: StandardHandlerRoutingInterceptorOptions<T>) => string) | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* If true, this plugin will log information about request lifecycle,
|
|
20
|
+
* including when a request is received, handled, or no matching procedure is found.
|
|
21
|
+
*
|
|
22
|
+
* @default false
|
|
23
|
+
*/
|
|
24
|
+
logLifecycle?: boolean | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* If true, this plugin will log when a request signal is aborted.
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
logAbort?: boolean | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Customizes the log level for errors thrown from procedures;
|
|
33
|
+
* internal handler failures are always logged at error level.
|
|
34
|
+
* Receives the level applied by default; return it to keep the default behavior:
|
|
35
|
+
* 'info' for abort errors, 'warn' for ORPCError except INTERNAL_SERVER_ERROR, 'error' otherwise.
|
|
36
|
+
*
|
|
37
|
+
* @default (error, level) => level
|
|
38
|
+
*/
|
|
39
|
+
procedureErrorLevel?: ((error: unknown, level: pino.Level) => pino.Level) | undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Instruments an oRPC handler with Pino structured logging, request tracking,
|
|
43
|
+
* and error monitoring.
|
|
44
|
+
*
|
|
45
|
+
* @see {@link https://orpc.dev/docs/integrations/pino | Pino Integration}
|
|
46
|
+
*/
|
|
47
|
+
export declare class PinoHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
48
|
+
name: string;
|
|
49
|
+
/**
|
|
50
|
+
* - Logging interceptors should run after tracing interceptors
|
|
51
|
+
* so they execute within the active request span.
|
|
52
|
+
* - Logging interceptors should run after batch interceptors
|
|
53
|
+
* so they log each individual request instead of the batch request.
|
|
54
|
+
*/
|
|
55
|
+
before: string[];
|
|
56
|
+
private readonly logger;
|
|
57
|
+
private readonly generateRequestId;
|
|
58
|
+
private readonly logLifecycle;
|
|
59
|
+
private readonly logAbort;
|
|
60
|
+
private readonly procedureErrorLevel;
|
|
61
|
+
constructor(options?: PinoHandlerPluginOptions<T>);
|
|
62
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
63
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from '@orpc/client';
|
|
2
|
+
import { isAbortError, isAsyncIteratorObject, ORPC_NAME, override, toArray, wrapReadableStream, } from '@orpc/shared';
|
|
3
|
+
import { flattenStandardHeader } from '@standard-server/core';
|
|
4
|
+
import pino from 'pino';
|
|
5
|
+
import { getLogger, LOGGER_CONTEXT_SYMBOL } from './context.js';
|
|
6
|
+
/**
|
|
7
|
+
* Instruments an oRPC handler with Pino structured logging, request tracking,
|
|
8
|
+
* and error monitoring.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/integrations/pino | Pino Integration}
|
|
11
|
+
*/
|
|
12
|
+
export class PinoHandlerPlugin {
|
|
13
|
+
name = '~pino';
|
|
14
|
+
/**
|
|
15
|
+
* - Logging interceptors should run after tracing interceptors
|
|
16
|
+
* so they execute within the active request span.
|
|
17
|
+
* - Logging interceptors should run after batch interceptors
|
|
18
|
+
* so they log each individual request instead of the batch request.
|
|
19
|
+
*/
|
|
20
|
+
before = ['~tracing', '~batch', '~hibernation'];
|
|
21
|
+
logger;
|
|
22
|
+
generateRequestId;
|
|
23
|
+
logLifecycle;
|
|
24
|
+
logAbort;
|
|
25
|
+
procedureErrorLevel;
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
this.logger = options.logger ?? pino();
|
|
28
|
+
this.generateRequestId =
|
|
29
|
+
options.generateRequestId ??
|
|
30
|
+
(({ request }) => flattenStandardHeader(request.headers['x-request-id']) ?? crypto.randomUUID());
|
|
31
|
+
this.logLifecycle = options.logLifecycle ?? false;
|
|
32
|
+
this.logAbort = options.logAbort ?? false;
|
|
33
|
+
this.procedureErrorLevel = options.procedureErrorLevel ?? ((_, level) => level);
|
|
34
|
+
}
|
|
35
|
+
init(options) {
|
|
36
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
37
|
+
const startMs = Date.now();
|
|
38
|
+
const logger = (interceptorOptions.context[LOGGER_CONTEXT_SYMBOL] ?? this.logger).child({});
|
|
39
|
+
/**
|
|
40
|
+
* pino-http might have already set req info in bindings
|
|
41
|
+
*/
|
|
42
|
+
if (!logger.bindings().req) {
|
|
43
|
+
logger.setBindings({
|
|
44
|
+
req: {
|
|
45
|
+
id: this.generateRequestId(interceptorOptions),
|
|
46
|
+
url: interceptorOptions.request.url,
|
|
47
|
+
method: interceptorOptions.request.method,
|
|
48
|
+
headers: {
|
|
49
|
+
'content-type': interceptorOptions.request.headers['content-type'],
|
|
50
|
+
'content-length': interceptorOptions.request.headers['content-length'],
|
|
51
|
+
'content-disposition': interceptorOptions.request.headers['content-disposition'],
|
|
52
|
+
'standard-server': interceptorOptions.request.headers['standard-server'],
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
if (this.logLifecycle) {
|
|
59
|
+
logger?.info('request received');
|
|
60
|
+
}
|
|
61
|
+
const result = await next({
|
|
62
|
+
...interceptorOptions,
|
|
63
|
+
context: {
|
|
64
|
+
...interceptorOptions.context,
|
|
65
|
+
[LOGGER_CONTEXT_SYMBOL]: logger,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
if (this.logLifecycle) {
|
|
69
|
+
if (result.matched) {
|
|
70
|
+
logger?.info({
|
|
71
|
+
msg: 'request handled',
|
|
72
|
+
res: {
|
|
73
|
+
status: result.response.status,
|
|
74
|
+
},
|
|
75
|
+
responseTime: Date.now() - startMs,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
logger?.info('no matching procedure found');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
/**
|
|
86
|
+
* Any error here is internal (interceptor/framework), not business logic.
|
|
87
|
+
* Indicates unexpected handler failure.
|
|
88
|
+
*/
|
|
89
|
+
logger.error(error);
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const interceptor = async ({ next, context, path, request }) => {
|
|
94
|
+
const logger = getLogger(context);
|
|
95
|
+
logger?.setBindings({ rpc: { system: ORPC_NAME, method: path.join('.') } });
|
|
96
|
+
if (this.logAbort) {
|
|
97
|
+
const signal = request.signal;
|
|
98
|
+
if (signal?.aborted) {
|
|
99
|
+
logger?.info(`request was aborted before handling (${String(signal.reason)})`);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
signal?.addEventListener('abort', () => {
|
|
103
|
+
logger?.info(`request is aborted (${String(signal.reason)})`);
|
|
104
|
+
}, { once: true });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
return await next();
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
logProcedureError(logger, error, this.procedureErrorLevel);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const clientInterceptor = async ({ next, context, }) => {
|
|
116
|
+
const output = await next();
|
|
117
|
+
if (isAsyncIteratorObject(output)) {
|
|
118
|
+
/**
|
|
119
|
+
* @remarks
|
|
120
|
+
* **Warning**: Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
121
|
+
*/
|
|
122
|
+
return override(output, wrapAsyncIteratorPreservingEventMeta(output, {
|
|
123
|
+
onError: (error) => {
|
|
124
|
+
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
125
|
+
},
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
if (output instanceof ReadableStream) {
|
|
129
|
+
/**
|
|
130
|
+
* @remarks
|
|
131
|
+
* **Warning**: Remember use `override` for ReadableStream to remain other special properties
|
|
132
|
+
*/
|
|
133
|
+
return override(output, wrapReadableStream(output, {
|
|
134
|
+
onError: (error) => {
|
|
135
|
+
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
136
|
+
},
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
return output;
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
...options,
|
|
143
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
144
|
+
interceptors: [interceptor, ...toArray(options.interceptors)],
|
|
145
|
+
clientInterceptors: [clientInterceptor, ...toArray(options.clientInterceptors)],
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function logProcedureError(logger, error, procedureErrorLevel) {
|
|
150
|
+
logger?.[procedureErrorLevel(error, defaultProcedureErrorLevel(error))](error);
|
|
151
|
+
}
|
|
152
|
+
function defaultProcedureErrorLevel(error) {
|
|
153
|
+
// An abort means the client withdrew the request, not that something failed,
|
|
154
|
+
// so record it as normal operation.
|
|
155
|
+
if (isAbortError(error)) {
|
|
156
|
+
return 'info';
|
|
157
|
+
}
|
|
158
|
+
// A thrown ORPCError is a deliberate business rejection delivered to the client,
|
|
159
|
+
// so keep it reviewable without treating it as a failure. INTERNAL_SERVER_ERROR
|
|
160
|
+
// is the exception: it signals a server-side failure (oRPC itself throws it for
|
|
161
|
+
// failures like output validation), so it stays at error level along with
|
|
162
|
+
// anything unexpected.
|
|
163
|
+
if (error instanceof ORPCError && error.code !== 'INTERNAL_SERVER_ERROR') {
|
|
164
|
+
return 'warn';
|
|
165
|
+
}
|
|
166
|
+
return 'error';
|
|
167
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,52 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/pino",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.48",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
5
4
|
"description": "Pino integration for oRPC: structured logging for typesafe APIs",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"logger",
|
|
7
|
+
"logging",
|
|
8
|
+
"observability",
|
|
9
|
+
"orpc",
|
|
10
|
+
"pino",
|
|
11
|
+
"typescript"
|
|
10
12
|
],
|
|
11
13
|
"homepage": "https://orpc.dev",
|
|
14
|
+
"license": "MIT",
|
|
12
15
|
"repository": {
|
|
13
16
|
"type": "git",
|
|
14
17
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
18
|
"directory": "packages/pino"
|
|
16
19
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"logging",
|
|
21
|
-
"logger",
|
|
22
|
-
"observability",
|
|
23
|
-
"typescript"
|
|
20
|
+
"funding": [
|
|
21
|
+
"https://github.com/sponsors/dinwwwh",
|
|
22
|
+
"https://opencollective.com/middleapi"
|
|
24
23
|
],
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
25
28
|
"sideEffects": false,
|
|
26
29
|
"exports": {
|
|
27
30
|
"./package.json": "./package.json",
|
|
28
31
|
".": {
|
|
29
|
-
"types": "./dist/index.d.
|
|
30
|
-
"import": "./dist/index.
|
|
31
|
-
"default": "./dist/index.
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js",
|
|
34
|
+
"default": "./dist/index.js"
|
|
32
35
|
}
|
|
33
36
|
},
|
|
34
|
-
"files": [
|
|
35
|
-
"dist"
|
|
36
|
-
],
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"pino": ">=10.1.0"
|
|
39
|
-
},
|
|
40
37
|
"dependencies": {
|
|
41
|
-
"@orpc/client": "2.0.0-beta.
|
|
42
|
-
"@orpc/server": "2.0.0-beta.
|
|
43
|
-
"@orpc/shared": "2.0.0-beta.
|
|
44
|
-
"@standard-server/core": "~0.
|
|
38
|
+
"@orpc/client": "2.0.0-beta.49",
|
|
39
|
+
"@orpc/server": "2.0.0-beta.49",
|
|
40
|
+
"@orpc/shared": "2.0.0-beta.49",
|
|
41
|
+
"@standard-server/core": "~0.15.0"
|
|
45
42
|
},
|
|
46
43
|
"devDependencies": {
|
|
47
44
|
"pino": "^10.4.0"
|
|
48
45
|
},
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
}
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"pino": ">=10.1.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {}
|
|
52
50
|
}
|
package/dist/context.d.mts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Logger } from "pino";
|
|
2
|
-
/**
|
|
3
|
-
* Context symbol under which the Pino logger is stored. Use it to provide a
|
|
4
|
-
* custom logger instance per request, e.g. when integrating with pino-http.
|
|
5
|
-
*
|
|
6
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#providing-custom-logger-per-request | Pino Integration - Providing Custom Logger per Request}
|
|
7
|
-
*/
|
|
8
|
-
export declare const LOGGER_CONTEXT_SYMBOL: unique symbol;
|
|
9
|
-
/**
|
|
10
|
-
* Context shape that carries the Pino logger injected by `PinoHandlerPlugin`.
|
|
11
|
-
*
|
|
12
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
13
|
-
*/
|
|
14
|
-
export interface LoggerContext {
|
|
15
|
-
[LOGGER_CONTEXT_SYMBOL]?: Logger | undefined;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Retrieves the Pino logger from the oRPC context, if available.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/integrations/pino#using-the-logger-in-your-code | Pino Integration - Using the Logger in Your Code}
|
|
21
|
-
*/
|
|
22
|
-
export declare function getLogger(context: LoggerContext): Logger | undefined;
|
package/dist/context.mjs
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { Context } from "@orpc/server";
|
|
2
|
-
import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from "@orpc/server/standard";
|
|
3
|
-
import type { Logger } from "pino";
|
|
4
|
-
import pino from "pino";
|
|
5
|
-
export interface PinoHandlerPluginOptions<T extends Context> {
|
|
6
|
-
/**
|
|
7
|
-
* Logger instance to use for logging.
|
|
8
|
-
*
|
|
9
|
-
* @default pino()
|
|
10
|
-
*/
|
|
11
|
-
logger?: Logger | undefined;
|
|
12
|
-
/**
|
|
13
|
-
* Function to generate a unique ID for each request.
|
|
14
|
-
*
|
|
15
|
-
* @default ({ request }) => flattenStandardHeader(request.headers['x-request-id']) ?? crypto.randomUUID()
|
|
16
|
-
*/
|
|
17
|
-
generateRequestId?: ((options: StandardHandlerRoutingInterceptorOptions<T>) => string) | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* If true, this plugin will log information about request lifecycle,
|
|
20
|
-
* including when a request is received, handled, or no matching procedure is found.
|
|
21
|
-
*
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
logLifecycle?: boolean | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* If true, this plugin will log when a request signal is aborted.
|
|
27
|
-
*
|
|
28
|
-
* @default false
|
|
29
|
-
*/
|
|
30
|
-
logAbort?: boolean | undefined;
|
|
31
|
-
/**
|
|
32
|
-
* Customizes the log level for errors thrown from procedures;
|
|
33
|
-
* internal handler failures are always logged at error level.
|
|
34
|
-
* Receives the level applied by default; return it to keep the default behavior:
|
|
35
|
-
* 'info' for abort errors, 'warn' for ORPCError except INTERNAL_SERVER_ERROR, 'error' otherwise.
|
|
36
|
-
*
|
|
37
|
-
* @default (error, level) => level
|
|
38
|
-
*/
|
|
39
|
-
procedureErrorLevel?: ((error: unknown, level: pino.Level) => pino.Level) | undefined;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Instruments an oRPC handler with Pino structured logging, request tracking,
|
|
43
|
-
* and error monitoring.
|
|
44
|
-
*
|
|
45
|
-
* @see {@link https://orpc.dev/docs/integrations/pino | Pino Integration}
|
|
46
|
-
*/
|
|
47
|
-
export declare class PinoHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
48
|
-
name: string;
|
|
49
|
-
/**
|
|
50
|
-
* - Logging interceptors should run after tracing interceptors
|
|
51
|
-
* so they execute within the active request span.
|
|
52
|
-
* - Logging interceptors should run after batch interceptors
|
|
53
|
-
* so they log each individual request instead of the batch request.
|
|
54
|
-
*/
|
|
55
|
-
before: string[];
|
|
56
|
-
private readonly logger;
|
|
57
|
-
private readonly generateRequestId;
|
|
58
|
-
private readonly logLifecycle;
|
|
59
|
-
private readonly logAbort;
|
|
60
|
-
private readonly procedureErrorLevel;
|
|
61
|
-
constructor(options?: PinoHandlerPluginOptions<T>);
|
|
62
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
63
|
-
}
|
package/dist/handler-plugin.mjs
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from "@orpc/client";
|
|
2
|
-
import { isAbortError, isAsyncIteratorObject, ORPC_NAME, override, toArray, wrapReadableStream } from "@orpc/shared";
|
|
3
|
-
import { flattenStandardHeader } from "@standard-server/core";
|
|
4
|
-
import pino from "pino";
|
|
5
|
-
import { getLogger, LOGGER_CONTEXT_SYMBOL } from "./context.mjs";
|
|
6
|
-
|
|
7
|
-
export class PinoHandlerPlugin {
|
|
8
|
-
name = "~pino";
|
|
9
|
-
|
|
10
|
-
before = [
|
|
11
|
-
"~tracing",
|
|
12
|
-
"~batch",
|
|
13
|
-
"~hibernation"
|
|
14
|
-
];
|
|
15
|
-
logger;
|
|
16
|
-
generateRequestId;
|
|
17
|
-
logLifecycle;
|
|
18
|
-
logAbort;
|
|
19
|
-
procedureErrorLevel;
|
|
20
|
-
constructor(options = {}) {
|
|
21
|
-
this.logger = options.logger ?? pino();
|
|
22
|
-
this.generateRequestId = options.generateRequestId ?? (({ request }) => flattenStandardHeader(request.headers["x-request-id"]) ?? crypto.randomUUID());
|
|
23
|
-
this.logLifecycle = options.logLifecycle ?? false;
|
|
24
|
-
this.logAbort = options.logAbort ?? false;
|
|
25
|
-
this.procedureErrorLevel = options.procedureErrorLevel ?? ((_, level) => level);
|
|
26
|
-
}
|
|
27
|
-
init(options) {
|
|
28
|
-
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
29
|
-
const startMs = Date.now();
|
|
30
|
-
const logger = (interceptorOptions.context[LOGGER_CONTEXT_SYMBOL] ?? this.logger).child({});
|
|
31
|
-
|
|
32
|
-
if (!logger.bindings().req) {
|
|
33
|
-
logger.setBindings({ req: {
|
|
34
|
-
id: this.generateRequestId(interceptorOptions),
|
|
35
|
-
url: interceptorOptions.request.url,
|
|
36
|
-
method: interceptorOptions.request.method,
|
|
37
|
-
headers: {
|
|
38
|
-
"content-type": interceptorOptions.request.headers["content-type"],
|
|
39
|
-
"content-length": interceptorOptions.request.headers["content-length"],
|
|
40
|
-
"content-disposition": interceptorOptions.request.headers["content-disposition"],
|
|
41
|
-
"standard-server": interceptorOptions.request.headers["standard-server"]
|
|
42
|
-
}
|
|
43
|
-
} });
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
if (this.logLifecycle) {
|
|
47
|
-
logger?.info("request received");
|
|
48
|
-
}
|
|
49
|
-
const result = await next({
|
|
50
|
-
...interceptorOptions,
|
|
51
|
-
context: {
|
|
52
|
-
...interceptorOptions.context,
|
|
53
|
-
[LOGGER_CONTEXT_SYMBOL]: logger
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
if (this.logLifecycle) {
|
|
57
|
-
if (result.matched) {
|
|
58
|
-
logger?.info({
|
|
59
|
-
msg: "request handled",
|
|
60
|
-
res: { status: result.response.status },
|
|
61
|
-
responseTime: Date.now() - startMs
|
|
62
|
-
});
|
|
63
|
-
} else {
|
|
64
|
-
logger?.info("no matching procedure found");
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
} catch (error) {
|
|
69
|
-
|
|
70
|
-
logger.error(error);
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
const interceptor = async ({ next, context, path, request }) => {
|
|
75
|
-
const logger = getLogger(context);
|
|
76
|
-
logger?.setBindings({ rpc: {
|
|
77
|
-
system: ORPC_NAME,
|
|
78
|
-
method: path.join(".")
|
|
79
|
-
} });
|
|
80
|
-
if (this.logAbort) {
|
|
81
|
-
const signal = request.signal;
|
|
82
|
-
if (signal?.aborted) {
|
|
83
|
-
logger?.info(`request was aborted before handling (${String(signal.reason)})`);
|
|
84
|
-
} else {
|
|
85
|
-
signal?.addEventListener("abort", () => {
|
|
86
|
-
logger?.info(`request is aborted (${String(signal.reason)})`);
|
|
87
|
-
}, { once: true });
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
return await next();
|
|
92
|
-
} catch (error) {
|
|
93
|
-
logProcedureError(logger, error, this.procedureErrorLevel);
|
|
94
|
-
throw error;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
const clientInterceptor = async ({ next, context }) => {
|
|
98
|
-
const output = await next();
|
|
99
|
-
if (isAsyncIteratorObject(output)) {
|
|
100
|
-
|
|
101
|
-
return override(output, wrapAsyncIteratorPreservingEventMeta(output, { onError: (error) => {
|
|
102
|
-
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
103
|
-
} }));
|
|
104
|
-
}
|
|
105
|
-
if (output instanceof ReadableStream) {
|
|
106
|
-
|
|
107
|
-
return override(output, wrapReadableStream(output, { onError: (error) => {
|
|
108
|
-
logProcedureError(getLogger(context), error, this.procedureErrorLevel);
|
|
109
|
-
} }));
|
|
110
|
-
}
|
|
111
|
-
return output;
|
|
112
|
-
};
|
|
113
|
-
return {
|
|
114
|
-
...options,
|
|
115
|
-
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
116
|
-
interceptors: [interceptor, ...toArray(options.interceptors)],
|
|
117
|
-
clientInterceptors: [clientInterceptor, ...toArray(options.clientInterceptors)]
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
function logProcedureError(logger, error, procedureErrorLevel) {
|
|
122
|
-
logger?.[procedureErrorLevel(error, defaultProcedureErrorLevel(error))](error);
|
|
123
|
-
}
|
|
124
|
-
function defaultProcedureErrorLevel(error) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (isAbortError(error)) {
|
|
128
|
-
return "info";
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (error instanceof ORPCError && error.code !== "INTERNAL_SERVER_ERROR") {
|
|
136
|
-
return "warn";
|
|
137
|
-
}
|
|
138
|
-
return "error";
|
|
139
|
-
}
|
package/dist/index.d.mts
DELETED
package/dist/index.mjs
DELETED