fastify-actuator
Version:
A Fastify plugin that adds a customizable /health endpoint
133 lines (128 loc) • 4.66 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
envEndpoint: () => envEndpoint_default,
healthCheck: () => healthCheck_default,
multiRegister: () => multiRegister
});
module.exports = __toCommonJS(index_exports);
// src/healthCheck.ts
var import_fastify_plugin = __toESM(require("fastify-plugin"));
async function healthCheckPlugin(fastify, options) {
if (!options.prefix) {
fastify.register(
async function(instance) {
instance.get("/health", async () => {
return { status: "up" };
});
},
{ prefix: "/actuator" }
);
} else {
fastify.get("/health", async () => {
return { status: "up" };
});
}
}
var healthCheck_default = (0, import_fastify_plugin.default)(healthCheckPlugin, {
name: "fastify-health-check"
});
// src/envEndpoint.ts
var import_fastify_plugin2 = __toESM(require("fastify-plugin"));
var dotenv = __toESM(require("dotenv"));
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
function extractDefaultsFromSettingsFile(fileContent) {
const result = {};
const regex = /(\w+):\s*\w+\(\s*{[^}]*default:\s*([^,}]+)[^}]*}\s*\)/g;
let match;
while ((match = regex.exec(fileContent)) !== null) {
const key = match[1];
let value = match[2].trim();
if (value.startsWith("'") && value.endsWith("'") || value.startsWith('"') && value.endsWith('"')) {
value = value.slice(1, -1);
} else if (value === "true" || value === "false") {
value = value === "true" ? "true" : "false";
} else if (!isNaN(Number(value))) {
value = String(Number(value));
}
result[key] = value;
}
return result;
}
function readEnvFiles(settings) {
const inputFiles = settings ? Array.isArray(settings) ? settings : [settings] : ["./settings.ts"];
const filesSet = new Set(inputFiles);
filesSet.delete(".env");
const files = [...filesSet, ".env"];
return files.reduce((envVars, file) => {
const filePath = path.resolve(process.cwd(), file);
if (!fs.existsSync(filePath)) return envVars;
const content = fs.readFileSync(filePath, "utf8");
const parsed = file.endsWith(".env") ? dotenv.parse(content) : file.endsWith(".ts") || file.endsWith(".js") ? extractDefaultsFromSettingsFile(content) : {};
return { ...envVars, ...parsed };
}, {});
}
async function envEndpointPlugin(fastify, options) {
const envVars = readEnvFiles(options.settings);
if (!options.prefix) {
fastify.register(
async function(instance) {
instance.get("/env", async () => {
return envVars;
});
},
{ prefix: "/actuator" }
);
} else {
fastify.get("/env", async () => {
return envVars;
});
}
}
var envEndpoint_default = (0, import_fastify_plugin2.default)(envEndpointPlugin, {
name: "fastify-env-endpoint"
});
// src/multiRegister.ts
function multiRegister(pluginsOrPlugin) {
return async function(fastify, options) {
const plugins = Array.isArray(pluginsOrPlugin) ? pluginsOrPlugin : [pluginsOrPlugin];
for (const plugin of plugins) {
await fastify.register(plugin, options);
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
envEndpoint,
healthCheck,
multiRegister
});