fastify-txstate 3.1.6 → 3.1.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/lib/index.js CHANGED
@@ -31,11 +31,11 @@ exports.prodLogger = {
31
31
  };
32
32
  },
33
33
  res(res) {
34
- var _a;
34
+ var _a, _b;
35
35
  return {
36
36
  statusCode: res.statusCode,
37
37
  url: (_a = res.request) === null || _a === void 0 ? void 0 : _a.url.replace(/token=[\w.]+/, 'token=redacted'),
38
- length: res.getHeader('content-length'),
38
+ length: (_b = res.getHeader) === null || _b === void 0 ? void 0 : _b.call(res, 'content-length'),
39
39
  ...res.extraLogInfo
40
40
  };
41
41
  }
@@ -78,6 +78,7 @@ class Server {
78
78
  else
79
79
  config.trustProxy = process.env.TRUST_PROXY;
80
80
  }
81
+ this.healthCallback = config.checkHealth;
81
82
  this.app = (0, fastify_1.fastify)(config);
82
83
  if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
83
84
  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 : [])]);
@@ -160,14 +161,23 @@ class Server {
160
161
  this.app.get('/health', { logLevel: 'warn' }, async (req, res) => {
161
162
  if (this.shuttingDown) {
162
163
  res.log.info('Returning 503 on /health because we are shutting down/restarting.');
163
- await res.status(503).send('Service is shutting down/restarting.');
164
+ void res.status(503);
165
+ return 'Service is shutting down/restarting.';
164
166
  }
165
167
  else if (this.healthMessage) {
166
- res.log.info('Returning 500 on health with the message:', this.healthMessage);
167
- await res.status(500).send(this.healthMessage);
168
+ res.log.info(this.healthMessage);
169
+ void res.status(500);
170
+ return this.healthMessage;
168
171
  }
169
- else
170
- await res.status(200).send('OK');
172
+ else if (this.healthCallback) {
173
+ const msg = await this.healthCallback();
174
+ if (msg) {
175
+ res.log.info(msg);
176
+ void res.status(500);
177
+ return msg;
178
+ }
179
+ }
180
+ return 'OK';
171
181
  });
172
182
  this.sigHandler = () => {
173
183
  this.close().then(() => {
@@ -9,6 +9,14 @@ export interface FastifyTxStateOptions extends Partial<FastifyServerOptions> {
9
9
  validOriginSuffixes?: string[];
10
10
  skipOriginCheck?: boolean;
11
11
  checkOrigin?: (req: FastifyRequest) => boolean;
12
+ /**
13
+ * Run an asynchronous function to check the health of the service.
14
+ *
15
+ * Return a non-empty error message to trigger unhealthy status.
16
+ *
17
+ * Setting a health message with setUnhealthy will override this and prevent it from being executed.
18
+ */
19
+ checkHealth?: () => Promise<string | undefined>;
12
20
  }
13
21
  declare module 'fastify' {
14
22
  interface FastifyReply {
@@ -46,6 +54,7 @@ export default class Server {
46
54
  protected https: boolean;
47
55
  protected errorHandlers: ErrorHandler[];
48
56
  protected healthMessage?: string;
57
+ protected healthCallback?: () => Promise<string | undefined>;
49
58
  protected shuttingDown: boolean;
50
59
  protected sigHandler: (signal: any) => void;
51
60
  protected validOrigins: Record<string, boolean>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.1.6",
3
+ "version": "3.1.7",
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",