Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

refactor(plugins): move the config-defaults seeder out of the loader

Yudhi Armyndharis777f12b15992
diff --git a/src/core/plugins/config-defaults.util.spec.ts b/src/core/plugins/config-defaults.util.spec.ts
new file mode 100644
index 000000000..6c008adb0
--- /dev/null
+++ b/src/core/plugins/config-defaults.util.spec.ts
@@ -0,0 +1,42 @@
+import { seedConfigDefaults } from './config-defaults.util';
+
+describe('seedConfigDefaults', () => {
+ const schema = {
+ type: 'object' as const,
+ properties: {
+ timezone: { type: 'string' as const, default: 'UTC' },
+ cooldownSec: { type: 'number' as const, default: 3600 },
+ schedule: { type: 'object' as const, required: true }, // no default — must stay absent
+ rules: { type: 'array' as const, default: [{ q: 'hi', a: 'hello' }] },
+ },
+ };
+
+ it('seeds schema defaults for absent keys only', () => {
+ const out = seedConfigDefaults(schema, { timezone: 'Asia/Jakarta' });
+ expect(out).toEqual({
+ timezone: 'Asia/Jakarta', // explicit value wins
+ cooldownSec: 3600,
+ rules: [{ q: 'hi', a: 'hello' }],
+ });
+ expect(out).not.toHaveProperty('schedule'); // required-without-default is never invented
+ });
+
+ it('returns the input unchanged when nothing is missing (and no schema)', () => {
+ const config = { timezone: 'UTC', cooldownSec: 1, rules: [] };
+ expect(seedConfigDefaults(schema, config)).toBe(config);
+ expect(seedConfigDefaults(undefined, config)).toBe(config);
+ });
+
+ it('deep-clones object/array defaults so runtime and persisted copies cannot share references', () => {
+ const out = seedConfigDefaults(schema, {});
+ const again = seedConfigDefaults(schema, {});
+ expect(out.rules).toEqual(again.rules);
+ expect(out.rules).not.toBe(again.rules);
+ expect((out.rules as unknown[])[0]).not.toBe((again.rules as unknown[])[0]);
+ });
+
+ it('null is an explicit value and is not overwritten by the default', () => {
+ const out = seedConfigDefaults(schema, { timezone: null });
+ expect(out.timezone).toBeNull();
+ });
+});
diff --git a/src/core/plugins/config-defaults.util.ts b/src/core/plugins/config-defaults.util.ts
new file mode 100644
index 000000000..94a3c1e4d
--- /dev/null
+++ b/src/core/plugins/config-defaults.util.ts
@@ -0,0 +1,27 @@
+import type { PluginConfigSchema } from './plugin.interfaces';
+
+/**
+ * Fill config keys the schema declares a `default` for and that are absent (undefined) in the
+ * stored config. Seeding happens at LOAD time (fresh installs and every boot), so a plugin whose
+ * schema fields carry defaults never runs its lifecycle with them missing — the failure class of
+ * "enable throws: <field> is required/has no value" for defaulted fields. Explicit values — even
+ * null — are never overwritten, and object/array defaults are deep-cloned so the seeded runtime
+ * config and the persisted entry can't share a mutable reference. Required fields WITHOUT a
+ * declared default stay absent on purpose: those need real operator input, not an invented value.
+ */
+export function seedConfigDefaults(
+ schema: PluginConfigSchema | undefined,
+ config: Record<string, unknown>,
+): Record<string, unknown> {
+ const properties = schema?.properties;
+ if (!properties) return config;
+ let seeded: Record<string, unknown> | undefined;
+ for (const [key, field] of Object.entries(properties)) {
+ if (config[key] !== undefined || field === null || typeof field !== 'object') continue;
+ const value = field.default;
+ if (value === undefined) continue;
+ if (!seeded) seeded = { ...config };
+ seeded[key] = value !== null && typeof value === 'object' ? structuredClone(value) : value;
+ }
+ return seeded ?? config;
+}
diff --git a/src/core/plugins/plugin-loader.service.spec.ts b/src/core/plugins/plugin-loader.service.spec.ts
index 435be66ac..3180ae26e 100644
--- a/src/core/plugins/plugin-loader.service.spec.ts
+++ b/src/core/plugins/plugin-loader.service.spec.ts
@@ -115,7 +115,7 @@ describe('dispatchConversationMedia', () => {
import * as fs from 'fs';
import * as os from 'os';
-import { PluginLoaderService, seedConfigDefaults } from './plugin-loader.service';
+import { PluginLoaderService } from './plugin-loader.service';
import { ConfigService } from '@nestjs/config';
import { ModuleRef } from '@nestjs/core';
import { HookManager } from '../hooks';
@@ -580,47 +580,6 @@ describe('PluginLoaderService — loadPlugin seeds configSchema defaults', () =>
});
});
-describe('seedConfigDefaults', () => {
- const schema = {
- type: 'object' as const,
- properties: {
- timezone: { type: 'string' as const, default: 'UTC' },
- cooldownSec: { type: 'number' as const, default: 3600 },
- schedule: { type: 'object' as const, required: true }, // no default — must stay absent
- rules: { type: 'array' as const, default: [{ q: 'hi', a: 'hello' }] },
- },
- };
-
- it('seeds schema defaults for absent keys only', () => {
- const out = seedConfigDefaults(schema, { timezone: 'Asia/Jakarta' });
- expect(out).toEqual({
- timezone: 'Asia/Jakarta', // explicit value wins
- cooldownSec: 3600,
- rules: [{ q: 'hi', a: 'hello' }],
- });
- expect(out).not.toHaveProperty('schedule'); // required-without-default is never invented
- });
-
- it('returns the input unchanged when nothing is missing (and no schema)', () => {
- const config = { timezone: 'UTC', cooldownSec: 1, rules: [] };
- expect(seedConfigDefaults(schema, config)).toBe(config);
- expect(seedConfigDefaults(undefined, config)).toBe(config);
- });
-
- it('deep-clones object/array defaults so runtime and persisted copies cannot share references', () => {
- const out = seedConfigDefaults(schema, {});
- const again = seedConfigDefaults(schema, {});
- expect(out.rules).toEqual(again.rules);
- expect(out.rules).not.toBe(again.rules);
- expect((out.rules as unknown[])[0]).not.toBe((again.rules as unknown[])[0]);
- });
-
- it('null is an explicit value and is not overwritten by the default', () => {
- const out = seedConfigDefaults(schema, { timezone: null });
- expect(out.timezone).toBeNull();
- });
-});
-
describe('PluginLoaderService — enable concurrency', () => {
let tmpDir: string;
let loader: PluginLoaderService;
diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.ts
index 0809f776b..f86226ea9 100644
--- a/src/core/plugins/plugin-loader.service.ts
+++ b/src/core/plugins/plugin-loader.service.ts
@@ -31,7 +31,6 @@ import {
IPlugin,
PluginType,
PluginLogger,
- PluginConfigSchema,
validateIngressManifest,
warnUnauthenticatedIngressRoutes,
warnUnsignedTimestampRoutes,
@@ -39,6 +38,7 @@ import {
import { validatePluginManifest } from './plugin-manifest';
import { effectiveNetAllow, isNetHostAllowed, performPluginFetch } from './plugin-net';
import { PluginStorageService } from './plugin-storage.service';
+import { seedConfigDefaults } from './config-defaults.util';
import { isPluginActiveForSession, resolvePluginConfig } from './plugin-activation';
import { PluginWorkerHost } from './sandbox/plugin-worker-host';
import { WorkerThreadChannel } from './sandbox/worker-thread-channel';
@@ -190,32 +190,6 @@ export function dispatchConversationMedia(
// temporarily-unreadable plugin dir (e.g. an unmounted volume) never loses its persisted config.
const LEGACY_REMOVED_PLUGIN_IDS = new Set(['auto-reply', 'translation']);
-/**
- * Fill config keys the schema declares a `default` for and that are absent (undefined) in the
- * stored config. Seeding happens at LOAD time (fresh installs and every boot), so a plugin whose
- * schema fields carry defaults never runs its lifecycle with them missing — the failure class of
- * "enable throws: <field> is required/has no value" for defaulted fields. Explicit values — even
- * null — are never overwritten, and object/array defaults are deep-cloned so the seeded runtime
- * config and the persisted entry can't share a mutable reference. Required fields WITHOUT a
- * declared default stay absent on purpose: those need real operator input, not an invented value.
- */
-export function seedConfigDefaults(
- schema: PluginConfigSchema | undefined,
- config: Record<string, unknown>,
-): Record<string, unknown> {
- const properties = schema?.properties;
- if (!properties) return config;
- let seeded: Record<string, unknown> | undefined;
- for (const [key, field] of Object.entries(properties)) {
- if (config[key] !== undefined || field === null || typeof field !== 'object') continue;
- const value = field.default;
- if (value === undefined) continue;
- if (!seeded) seeded = { ...config };
- seeded[key] = value !== null && typeof value === 'object' ? structuredClone(value) : value;
- }
- return seeded ?? config;
-}
-
@Injectable()
export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy {
private readonly logger = createLogger('PluginLoaderService');