fastify-txstate 3.1.6 → 3.1.8
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 +20 -9
- package/lib-esm/index.d.ts +12 -0
- package/package.json +1 -1
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 : [])]);
|
|
@@ -125,13 +126,13 @@ class Server {
|
|
|
125
126
|
}
|
|
126
127
|
this.app.addHook('onSend', this.https && process.env.NODE_ENV !== 'development'
|
|
127
128
|
? async (_, resp) => {
|
|
128
|
-
resp.removeHeader('X-Powered-By');
|
|
129
|
+
void resp.removeHeader('X-Powered-By');
|
|
129
130
|
void resp.header('Strict-Transport-Security', 'max-age=31536000');
|
|
130
131
|
if (resp.getHeader('content-type') === 'text/html')
|
|
131
132
|
void resp.type('text/html; charset=utf-8');
|
|
132
133
|
}
|
|
133
134
|
: async (_, resp) => {
|
|
134
|
-
resp.removeHeader('X-Powered-By');
|
|
135
|
+
void resp.removeHeader('X-Powered-By');
|
|
135
136
|
if (resp.getHeader('content-type') === 'text/html')
|
|
136
137
|
void resp.type('text/html; charset=utf-8');
|
|
137
138
|
});
|
|
@@ -160,14 +161,24 @@ 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
|
-
|
|
164
|
+
void res.status(503);
|
|
165
|
+
return 'MAINTENANCE';
|
|
164
166
|
}
|
|
165
167
|
else if (this.healthMessage) {
|
|
166
|
-
res.log.info(
|
|
167
|
-
|
|
168
|
+
res.log.info(this.healthMessage);
|
|
169
|
+
void res.status(500);
|
|
170
|
+
return this.healthMessage;
|
|
168
171
|
}
|
|
169
|
-
else
|
|
170
|
-
await
|
|
172
|
+
else if (this.healthCallback) {
|
|
173
|
+
const resp = await this.healthCallback();
|
|
174
|
+
const [status, msg] = typeof resp === 'string' ? [500, resp] : [resp === null || resp === void 0 ? void 0 : resp.status, resp === null || resp === void 0 ? void 0 : resp.message];
|
|
175
|
+
if (!!msg || !!status) {
|
|
176
|
+
res.log.info(resp, 'Health check callback failed.');
|
|
177
|
+
void res.status(status !== null && status !== void 0 ? status : 500);
|
|
178
|
+
return msg !== null && msg !== void 0 ? msg : 'FAIL';
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return 'OK';
|
|
171
182
|
});
|
|
172
183
|
this.sigHandler = () => {
|
|
173
184
|
this.close().then(() => {
|
package/lib-esm/index.d.ts
CHANGED
|
@@ -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,10 @@ export default class Server {
|
|
|
46
54
|
protected https: boolean;
|
|
47
55
|
protected errorHandlers: ErrorHandler[];
|
|
48
56
|
protected healthMessage?: string;
|
|
57
|
+
protected healthCallback?: () => Promise<string | {
|
|
58
|
+
status?: number;
|
|
59
|
+
message?: string;
|
|
60
|
+
} | undefined>;
|
|
49
61
|
protected shuttingDown: boolean;
|
|
50
62
|
protected sigHandler: (signal: any) => void;
|
|
51
63
|
protected validOrigins: Record<string, boolean>;
|