@evlog/nuxthub 0.0.1-alpha.1 → 0.0.1-alpha.2
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.mjs +4 -3
- package/dist/module.mjs.map +1 -1
- package/package.json +1 -1
package/dist/module.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, promises } from "node:fs";
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
3
|
import { join, resolve } from "node:path";
|
|
4
|
-
import { addServerHandler, addServerPlugin, defineNuxtModule } from "@nuxt/kit";
|
|
4
|
+
import { addServerHandler, addServerPlugin, defineNuxtModule, hasNuxtModule, installModule } from "@nuxt/kit";
|
|
5
5
|
import { consola } from "consola";
|
|
6
6
|
import { createEvlogError } from "evlog";
|
|
7
7
|
|
|
@@ -45,7 +45,6 @@ var module_default = defineNuxtModule({
|
|
|
45
45
|
name: "@evlog/nuxthub",
|
|
46
46
|
version: "0.0.1-alpha.1"
|
|
47
47
|
},
|
|
48
|
-
moduleDependencies: { "evlog/nuxt": {} },
|
|
49
48
|
async onInstall(nuxt) {
|
|
50
49
|
const shouldSetup = await consola.prompt("Do you want to create a vercel.json with a cron schedule for evlog cleanup?", {
|
|
51
50
|
type: "confirm",
|
|
@@ -67,7 +66,9 @@ var module_default = defineNuxtModule({
|
|
|
67
66
|
await promises.writeFile(vercelJsonPath, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
|
|
68
67
|
consola.success("Created vercel.json with evlog cleanup cron schedule");
|
|
69
68
|
},
|
|
70
|
-
setup(_moduleOptions, nuxt) {
|
|
69
|
+
async setup(_moduleOptions, nuxt) {
|
|
70
|
+
if (!hasNuxtModule("evlog/nuxt")) await installModule("evlog/nuxt");
|
|
71
|
+
if (!hasNuxtModule("@nuxthub/core")) await installModule("@nuxthub/core");
|
|
71
72
|
const options = { retention: (nuxt.options.evlog || {}).retention ?? "30d" };
|
|
72
73
|
const srcDir = resolve(fileURLToPath(new URL(".", import.meta.url)), "..", "src");
|
|
73
74
|
const runtimeDir = join(srcDir, "runtime");
|
package/dist/module.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.mjs","names":["fsp"],"sources":["../src/module.ts"],"sourcesContent":["import { existsSync, promises as fsp } from 'node:fs'\nimport { fileURLToPath } from 'node:url'\nimport { join, resolve } from 'node:path'\nimport { addServerHandler, addServerPlugin, defineNuxtModule } from '@nuxt/kit'\nimport { consola } from 'consola'\nimport type { NitroConfig } from 'nitropack'\nimport { createEvlogError } from 'evlog'\n\nexport interface ModuleOptions {\n /**\n * How long to retain events before cleanup.\n * Supports \"30d\" (days), \"24h\" (hours), \"60m\" (minutes).\n * @default '30d'\n */\n retention?: string\n}\n\nfunction retentionToCron(retention: string): string {\n const match = retention.match(/^(\\d+)(d|h|m)$/)\n if (!match) {\n throw createEvlogError({\n message: `[evlog/nuxthub] Invalid retention format: \"${retention}\"`,\n why: 'The retention value must be a number followed by a unit: d (days), h (hours), or m (minutes)',\n fix: `Change retention to a valid format, e.g., \"30d\", \"24h\", or \"60m\"`,\n link: 'https://evlog.dev/nuxthub/retention',\n })\n }\n\n const [, numStr, unit] = match\n const num = Number(numStr)\n\n // Convert retention to minutes\n let totalMinutes: number\n switch (unit) {\n case 'm':\n totalMinutes = num\n break\n case 'h':\n totalMinutes = num * 60\n break\n case 'd':\n totalMinutes = num * 24 * 60\n break\n default:\n throw createEvlogError({\n message: `[evlog/nuxthub] Unknown retention unit: \"${unit}\"`,\n why: 'The retention value must use one of the supported units: d (days), h (hours), or m (minutes)',\n fix: `Change retention to a valid format, e.g., \"30d\", \"24h\", or \"60m\"`,\n link: 'https://evlog.dev/nuxthub/retention',\n })\n }\n\n // Cleanup runs every half-retention period\n const halfMinutes = Math.max(1, Math.floor(totalMinutes / 2))\n\n if (halfMinutes < 60) {\n return `*/${halfMinutes} * * * *`\n }\n\n const halfHours = Math.floor(halfMinutes / 60)\n if (halfHours >= 24) {\n return '0 3 * * *'\n }\n\n return `0 */${halfHours} * * *`\n}\n\nexport default defineNuxtModule<ModuleOptions>({\n meta: {\n name: '@evlog/nuxthub',\n version: '0.0.1-alpha.1',\n },\n
|
|
1
|
+
{"version":3,"file":"module.mjs","names":["fsp"],"sources":["../src/module.ts"],"sourcesContent":["import { existsSync, promises as fsp } from 'node:fs'\nimport { fileURLToPath } from 'node:url'\nimport { join, resolve } from 'node:path'\nimport { addServerHandler, addServerPlugin, defineNuxtModule, hasNuxtModule, installModule } from '@nuxt/kit'\nimport { consola } from 'consola'\nimport type { NitroConfig } from 'nitropack'\nimport { createEvlogError } from 'evlog'\n\nexport interface ModuleOptions {\n /**\n * How long to retain events before cleanup.\n * Supports \"30d\" (days), \"24h\" (hours), \"60m\" (minutes).\n * @default '30d'\n */\n retention?: string\n}\n\nfunction retentionToCron(retention: string): string {\n const match = retention.match(/^(\\d+)(d|h|m)$/)\n if (!match) {\n throw createEvlogError({\n message: `[evlog/nuxthub] Invalid retention format: \"${retention}\"`,\n why: 'The retention value must be a number followed by a unit: d (days), h (hours), or m (minutes)',\n fix: `Change retention to a valid format, e.g., \"30d\", \"24h\", or \"60m\"`,\n link: 'https://evlog.dev/nuxthub/retention',\n })\n }\n\n const [, numStr, unit] = match\n const num = Number(numStr)\n\n // Convert retention to minutes\n let totalMinutes: number\n switch (unit) {\n case 'm':\n totalMinutes = num\n break\n case 'h':\n totalMinutes = num * 60\n break\n case 'd':\n totalMinutes = num * 24 * 60\n break\n default:\n throw createEvlogError({\n message: `[evlog/nuxthub] Unknown retention unit: \"${unit}\"`,\n why: 'The retention value must use one of the supported units: d (days), h (hours), or m (minutes)',\n fix: `Change retention to a valid format, e.g., \"30d\", \"24h\", or \"60m\"`,\n link: 'https://evlog.dev/nuxthub/retention',\n })\n }\n\n // Cleanup runs every half-retention period\n const halfMinutes = Math.max(1, Math.floor(totalMinutes / 2))\n\n if (halfMinutes < 60) {\n return `*/${halfMinutes} * * * *`\n }\n\n const halfHours = Math.floor(halfMinutes / 60)\n if (halfHours >= 24) {\n return '0 3 * * *'\n }\n\n return `0 */${halfHours} * * *`\n}\n\nexport default defineNuxtModule<ModuleOptions>({\n meta: {\n name: '@evlog/nuxthub',\n version: '0.0.1-alpha.1',\n },\n async onInstall(nuxt) {\n const shouldSetup = await consola.prompt(\n 'Do you want to create a vercel.json with a cron schedule for evlog cleanup?',\n { type: 'confirm', initial: false },\n )\n if (typeof shouldSetup !== 'boolean' || !shouldSetup) return\n\n const vercelJsonPath = resolve(nuxt.options.rootDir, 'vercel.json')\n let config: Record<string, any> = {}\n if (existsSync(vercelJsonPath)) {\n config = JSON.parse(await fsp.readFile(vercelJsonPath, 'utf-8'))\n }\n\n const evlogConfig = (nuxt.options as any).evlog || {}\n const retention = evlogConfig.retention ?? '30d'\n const cron = retentionToCron(retention)\n\n const crons: Array<{ path: string, schedule: string }> = config.crons || []\n const existing = crons.findIndex(c => c.path === '/api/_cron/evlog-cleanup')\n if (existing >= 0) {\n crons[existing].schedule = cron\n } else {\n crons.push({ path: '/api/_cron/evlog-cleanup', schedule: cron })\n }\n config.crons = crons\n\n await fsp.writeFile(vercelJsonPath, `${JSON.stringify(config, null, 2)}\\n`, 'utf-8')\n consola.success('Created vercel.json with evlog cleanup cron schedule')\n },\n async setup(_moduleOptions, nuxt) {\n // Auto-install evlog/nuxt and @nuxthub/core if not already registered\n if (!hasNuxtModule('evlog/nuxt')) {\n await installModule('evlog/nuxt')\n }\n if (!hasNuxtModule('@nuxthub/core')) {\n await installModule('@nuxthub/core')\n }\n\n // Read nuxthub options from evlog config key\n const evlogConfig = (nuxt.options as any).evlog || {}\n const options: Required<ModuleOptions> = {\n retention: evlogConfig.retention ?? '30d',\n }\n\n // Runtime files must be resolved from src/ so Nitro can bundle them\n // and resolve virtual imports like @nuxthub/db\n const distDir = fileURLToPath(new URL('.', import.meta.url))\n const srcDir = resolve(distDir, '..', 'src')\n const runtimeDir = join(srcDir, 'runtime')\n\n // Extend NuxtHub DB schema with dialect-specific evlog_events table\n // @ts-expect-error hub:db:schema:extend hook exists but is not in NuxtHooks type\n nuxt.hook('hub:db:schema:extend', ({ paths, dialect }: { paths: string[], dialect: string }) => {\n paths.push(resolve(srcDir, 'schema', `${dialect}.ts`))\n })\n\n // Register the drain server plugin\n addServerPlugin(join(runtimeDir, 'drain'))\n\n // Register the cron API route (works as Vercel cron target or manual trigger)\n addServerHandler({\n route: '/api/_cron/evlog-cleanup',\n handler: join(runtimeDir, 'api', '_cron', 'evlog-cleanup'),\n })\n\n // Register the cleanup task with automatic cron schedule based on retention\n // @ts-expect-error nitro:config hook exists but is not in NuxtHooks type\n nuxt.hook('nitro:config', (nitroConfig: NitroConfig) => {\n // Enable experimental tasks\n nitroConfig.experimental = nitroConfig.experimental || {}\n nitroConfig.experimental.tasks = true\n\n // Register the task handler\n nitroConfig.tasks = nitroConfig.tasks || {}\n nitroConfig.tasks['evlog:cleanup'] = {\n handler: join(runtimeDir, 'tasks', 'evlog-cleanup'),\n }\n\n // Schedule based on retention (e.g., 1m → every 1 min, 1h → every 30 min, 30d → daily 3AM)\n const cron = retentionToCron(options.retention!)\n nitroConfig.scheduledTasks = nitroConfig.scheduledTasks || {}\n const existing = nitroConfig.scheduledTasks[cron]\n if (Array.isArray(existing)) {\n existing.push('evlog:cleanup')\n } else if (existing) {\n nitroConfig.scheduledTasks[cron] = [existing, 'evlog:cleanup']\n } else {\n nitroConfig.scheduledTasks[cron] = ['evlog:cleanup']\n }\n })\n },\n})\n"],"mappings":";;;;;;;;AAiBA,SAAS,gBAAgB,WAA2B;CAClD,MAAM,QAAQ,UAAU,MAAM,iBAAiB;AAC/C,KAAI,CAAC,MACH,OAAM,iBAAiB;EACrB,SAAS,8CAA8C,UAAU;EACjE,KAAK;EACL,KAAK;EACL,MAAM;EACP,CAAC;CAGJ,MAAM,GAAG,QAAQ,QAAQ;CACzB,MAAM,MAAM,OAAO,OAAO;CAG1B,IAAI;AACJ,SAAQ,MAAR;EACE,K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