mktcms 0.3.7 → 0.3.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/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mktcms",
3
3
  "configKey": "mktcms",
4
- "version": "0.3.7",
4
+ "version": "0.3.8",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -193,6 +193,10 @@ const module$1 = defineNuxtModule({
193
193
  route: "/api/content/:path",
194
194
  handler: resolver.resolve("./runtime/server/api/content/[path]")
195
195
  });
196
+ addServerHandler({
197
+ route: "/api/health",
198
+ handler: resolver.resolve("./runtime/server/api/health")
199
+ });
196
200
  extendPages((pages) => {
197
201
  pages.push({
198
202
  name: "Admin Dashboard",
@@ -1,87 +1,9 @@
1
- import { access, readdir, lstat } from "node:fs/promises";
2
- import { resolve } from "node:path";
3
- import { execFile } from "node:child_process";
4
- import { promisify } from "node:util";
5
1
  import { createError, defineEventHandler } from "h3";
6
- const execFileAsync = promisify(execFile);
7
- const CACHE_TTL_MS = 6e4;
8
- let cachedBytes = null;
9
- let inFlight = null;
10
- async function getDirectorySizeBytes(dirPath) {
11
- const dirsToVisit = [dirPath];
12
- let totalBytes = 0;
13
- while (dirsToVisit.length > 0) {
14
- const currentDir = dirsToVisit.pop();
15
- let entries;
16
- try {
17
- entries = await readdir(currentDir, { withFileTypes: true });
18
- } catch (error) {
19
- if (error?.code === "ENOENT") {
20
- continue;
21
- }
22
- throw error;
23
- }
24
- for (const entry of entries) {
25
- const entryPath = resolve(currentDir, entry.name);
26
- let stats;
27
- try {
28
- stats = await lstat(entryPath);
29
- } catch (error) {
30
- if (error?.code === "ENOENT") {
31
- continue;
32
- }
33
- throw error;
34
- }
35
- if (stats.isDirectory()) {
36
- dirsToVisit.push(entryPath);
37
- continue;
38
- }
39
- totalBytes += stats.size;
40
- }
41
- }
42
- return totalBytes;
43
- }
2
+ import { getStorageUsageBytes } from "../../utils/storageUsage.js";
44
3
  export default defineEventHandler(async () => {
45
4
  try {
46
- const now = Date.now();
47
- if (cachedBytes && now - cachedBytes.at < CACHE_TTL_MS) {
48
- return { bytes: cachedBytes.value };
49
- }
50
- const storageDir = resolve(process.cwd(), ".storage");
51
- try {
52
- await access(storageDir);
53
- } catch {
54
- cachedBytes = { value: 0, at: now };
55
- return { bytes: 0 };
56
- }
57
- try {
58
- if (!inFlight) {
59
- inFlight = (async () => {
60
- try {
61
- const { stdout } = await execFileAsync("du", ["-sb", storageDir], {
62
- timeout: 15e3,
63
- maxBuffer: 1024 * 1024
64
- });
65
- const bytesString = stdout.trim().split(/\s+/)[0];
66
- const bytes2 = Number.parseInt(bytesString || "", 10);
67
- if (!Number.isFinite(bytes2) || bytes2 < 0) {
68
- throw new Error("Unexpected du output");
69
- }
70
- return bytes2;
71
- } catch {
72
- return await getDirectorySizeBytes(storageDir);
73
- }
74
- })();
75
- }
76
- const bytes = await inFlight;
77
- cachedBytes = { value: bytes, at: Date.now() };
78
- return { bytes };
79
- } catch {
80
- inFlight = null;
81
- throw new Error("Failed to calculate storage usage");
82
- } finally {
83
- inFlight = null;
84
- }
5
+ const bytes = await getStorageUsageBytes();
6
+ return { bytes };
85
7
  } catch (error) {
86
8
  throw createError({
87
9
  statusCode: 500,
@@ -0,0 +1,2 @@
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<string>>;
2
+ export default _default;
@@ -0,0 +1,21 @@
1
+ import { createError, defineEventHandler, setHeader } from "h3";
2
+ import { getStorageUsageBytes } from "../utils/storageUsage.js";
3
+ export default defineEventHandler(async (event) => {
4
+ try {
5
+ const storageUsageBytes = await getStorageUsageBytes();
6
+ setHeader(event, "Content-Type", "text/plain; version=0.0.4; charset=utf-8");
7
+ return [
8
+ "# HELP mktcms_up Whether the mktcms API is healthy (1 = healthy).",
9
+ "# TYPE mktcms_up gauge",
10
+ "mktcms_up 1",
11
+ "# HELP mktcms_storage_usage_bytes Current .storage directory size in bytes.",
12
+ "# TYPE mktcms_storage_usage_bytes gauge",
13
+ `mktcms_storage_usage_bytes ${storageUsageBytes}`
14
+ ].join("\n") + "\n";
15
+ } catch (error) {
16
+ throw createError({
17
+ statusCode: 500,
18
+ statusMessage: error?.message || "Failed to build health metrics"
19
+ });
20
+ }
21
+ });
@@ -0,0 +1 @@
1
+ export declare function getStorageUsageBytes(): Promise<number>;
@@ -0,0 +1,44 @@
1
+ import { access } from "node:fs/promises";
2
+ import { resolve } from "node:path";
3
+ import { execFile } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ const execFileAsync = promisify(execFile);
6
+ const CACHE_TTL_MS = 9e5;
7
+ let cachedBytes = null;
8
+ let inFlight = null;
9
+ export async function getStorageUsageBytes() {
10
+ const now = Date.now();
11
+ if (cachedBytes && now - cachedBytes.at < CACHE_TTL_MS) {
12
+ return cachedBytes.value;
13
+ }
14
+ const storageDir = resolve(process.cwd(), ".storage");
15
+ try {
16
+ await access(storageDir);
17
+ } catch {
18
+ cachedBytes = { value: 0, at: now };
19
+ return 0;
20
+ }
21
+ try {
22
+ if (!inFlight) {
23
+ inFlight = (async () => {
24
+ const { stdout } = await execFileAsync("du", ["-sb", storageDir], {
25
+ timeout: 15e3,
26
+ maxBuffer: 1024 * 1024
27
+ });
28
+ const bytesString = stdout.trim().split(/\s+/)[0];
29
+ const bytes2 = Number.parseInt(bytesString || "", 10);
30
+ if (!Number.isFinite(bytes2) || bytes2 < 0) {
31
+ throw new Error("Unexpected du output");
32
+ }
33
+ return bytes2;
34
+ })();
35
+ }
36
+ const bytes = await inFlight;
37
+ cachedBytes = { value: bytes, at: Date.now() };
38
+ return bytes;
39
+ } catch {
40
+ throw new Error("Failed to calculate storage usage");
41
+ } finally {
42
+ inFlight = null;
43
+ }
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mktcms",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "Simple CMS module for Nuxt",
5
5
  "repository": "mktcode/mktcms",
6
6
  "license": "MIT",