fastify-txstate 3.0.8 → 3.0.11

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 CHANGED
@@ -80,4 +80,13 @@ You can authorize more subdomains with the `validOriginHosts` configuration opti
80
80
  You can disable these origin checks entirely with the `skipOriginCheck` configuration or `SKIP_ORIGIN_CHECK` environment variable.
81
81
 
82
82
  # Reverse Proxy
83
- If your application is behind a reverse proxy, you'll want to set the `trustProxy` configuration to true so that variables like `request.protocol` get set correctly. You can also set the `TRUST_PROXY` environment variable.
83
+ If your application is behind a reverse proxy, you'll want to set the `trustProxy` configuration to true so that variables like `request.protocol` get set correctly. You can also set the `TRUST_PROXY` environment variable. `true` or `1` will translate to `{ trustProxy: true }`; anything else will be passed unchanged as a string.
84
+
85
+ # Logging
86
+ We try to set up logging well by default, including things like the HTTP traceparent header, and putting the url in both the incoming and outgoing access log entries so that it's easy to grep for certain routes/params.
87
+
88
+ Development and production logs are different, based on the `NODE_ENV` environment variable. The development logger is designed to be extremely brief and not in JSON format, so that you can see errors clearly.
89
+
90
+ If you want to manipulate the logging you can import the `devLogger` and `prodLogger` into your project, manipulate them, and pass them into the server constructor configuration.
91
+
92
+ You can also simply add information to the `reply.extraLogInfo` object and it will automatically appear in the outgoing access log in production.
package/lib/index.js CHANGED
@@ -3,14 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FailedValidationError = exports.HttpError = exports.devLogger = void 0;
6
+ exports.FailedValidationError = exports.HttpError = exports.prodLogger = exports.devLogger = void 0;
7
7
  const fastify_1 = require("fastify");
8
8
  const fs_1 = __importDefault(require("fs"));
9
9
  const http_1 = __importDefault(require("http"));
10
10
  const http_status_codes_1 = require("http-status-codes");
11
11
  exports.devLogger = {
12
12
  level: 'info',
13
- info: (msg) => console.info(msg.req ? `${msg.req.method} ${msg.req.url}` : msg.res ? `${msg.res.statusCode}` : msg),
13
+ info: (msg) => console.info(msg.req ? `${msg.req.method} ${msg.req.url}` : msg.res ? `${msg.res.statusCode} - ${msg.responseTime}` : msg),
14
14
  error: console.error,
15
15
  debug: console.debug,
16
16
  fatal: console.error,
@@ -19,6 +19,26 @@ exports.devLogger = {
19
19
  silent: (msg) => { },
20
20
  child(bindings, options) { return exports.devLogger; }
21
21
  };
22
+ exports.prodLogger = {
23
+ level: 'info',
24
+ serializers: {
25
+ req(req) {
26
+ return {
27
+ method: req.method,
28
+ url: req.url,
29
+ remoteAddress: req.ip,
30
+ traceparent: req.headers.traceparent
31
+ };
32
+ },
33
+ res(res) {
34
+ return {
35
+ statusCode: res.statusCode,
36
+ url: res.request.url,
37
+ ...res.extraLogInfo
38
+ };
39
+ }
40
+ }
41
+ };
22
42
  class Server {
23
43
  constructor(config = {}) {
24
44
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
@@ -48,10 +68,14 @@ class Server {
48
68
  if (typeof config.logger === 'undefined') {
49
69
  config.logger = process.env.NODE_ENV === 'development'
50
70
  ? exports.devLogger
51
- : { level: 'info' };
71
+ : exports.prodLogger;
72
+ }
73
+ if (process.env.TRUST_PROXY != null) {
74
+ if (['true', '1'].includes(process.env.TRUST_PROXY))
75
+ config.trustProxy = true;
76
+ else
77
+ config.trustProxy = process.env.TRUST_PROXY;
52
78
  }
53
- if (process.env.TRUST_PROXY)
54
- config.trustProxy = true;
55
79
  this.app = (0, fastify_1.fastify)(config);
56
80
  if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
57
81
  this.setValidOrigins([...((_a = config.validOrigins) !== null && _a !== void 0 ? _a : []), ...((_c = (_b = process.env.VALID_ORIGINS) === null || _b === void 0 ? void 0 : _b.split(',')) !== null && _c !== void 0 ? _c : [])]);
@@ -59,6 +83,7 @@ class Server {
59
83
  this.setValidOriginSuffixes([...((_g = config.validOriginSuffixes) !== null && _g !== void 0 ? _g : []), ...((_j = (_h = process.env.VALID_ORIGIN_SUFFIXES) === null || _h === void 0 ? void 0 : _h.split(',')) !== null && _j !== void 0 ? _j : [])]);
60
84
  this.app.addHook('preHandler', async (req, res) => {
61
85
  var _a;
86
+ res.extraLogInfo = {};
62
87
  if (!req.headers.origin)
63
88
  return;
64
89
  let passed = this.validOrigins[req.headers.origin];
@@ -126,11 +151,15 @@ class Server {
126
151
  }
127
152
  }
128
153
  });
129
- this.app.get('/health', async (req, res) => {
130
- if (this.shuttingDown)
154
+ this.app.get('/health', { logLevel: 'warn' }, async (req, res) => {
155
+ if (this.shuttingDown) {
156
+ res.log.info('Returning 503 on /health because we are shutting down/restarting.');
131
157
  await res.status(503).send('Service is shutting down/restarting.');
132
- else if (this.healthMessage)
158
+ }
159
+ else if (this.healthMessage) {
160
+ res.log.info('Returning 500 on health with the message:', this.healthMessage);
133
161
  await res.status(500).send(this.healthMessage);
162
+ }
134
163
  else
135
164
  await res.status(200).send('OK');
136
165
  });
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { FastifyInstance, FastifyRequest, FastifyReply, FastifyServerOptions } from 'fastify';
2
+ import { FastifyInstance, FastifyRequest, FastifyReply, FastifyServerOptions, FastifyLoggerOptions } from 'fastify';
3
3
  import http2 from 'http2';
4
4
  declare type ErrorHandler = (error: Error, req: FastifyRequest, res: FastifyReply) => Promise<void>;
5
5
  export interface FastifyTxStateOptions extends Partial<FastifyServerOptions> {
@@ -10,6 +10,11 @@ export interface FastifyTxStateOptions extends Partial<FastifyServerOptions> {
10
10
  skipOriginCheck?: boolean;
11
11
  checkOrigin?: (req: FastifyRequest) => boolean;
12
12
  }
13
+ declare module 'fastify' {
14
+ interface FastifyReply {
15
+ extraLogInfo: any;
16
+ }
17
+ }
13
18
  export declare const devLogger: {
14
19
  level: string;
15
20
  info: (msg: any) => void;
@@ -36,6 +41,7 @@ export declare const devLogger: {
36
41
  silent: (msg: any) => void;
37
42
  child(bindings: any, options?: any): any;
38
43
  };
44
+ export declare const prodLogger: FastifyLoggerOptions;
39
45
  export default class Server {
40
46
  protected https: boolean;
41
47
  protected errorHandlers: ErrorHandler[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.0.8",
3
+ "version": "3.0.11",
4
4
  "description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
5
5
  "exports": {
6
6
  "types": "./lib-esm/index.d.ts",
@@ -21,11 +21,11 @@
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/chai": "^4.2.14",
24
- "@types/mocha": "^9.1.1",
24
+ "@types/mocha": "^10.0.0",
25
25
  "@types/node": "^16.7.2",
26
26
  "axios": ">=0.20.0",
27
27
  "chai": "^4.2.0",
28
- "eslint-config-standard-with-typescript": "^22.0.0",
28
+ "eslint-config-standard-with-typescript": "^23.0.0",
29
29
  "mocha": "^10.0.0",
30
30
  "ts-node": "^10.2.1",
31
31
  "typescript": "^4.4.2"