dsh-plugin-trim 0.3.7 → 0.3.9

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/index.js CHANGED
@@ -4588,7 +4588,7 @@ function registerCompatTool(ctx, config) {
4588
4588
  }
4589
4589
  // src/api.ts
4590
4590
  const API_PREFIX = "/api/dsh-experience-optimizer";
4591
- const CURRENT_VERSION = "0.3.7";
4591
+ const CURRENT_VERSION = "0.3.9";
4592
4592
  let updateCache;
4593
4593
  const TASK_TAGS = [
4594
4594
  "bugfix",
@@ -4609,11 +4609,11 @@ function registerTrimApi(ctx, config, ledger) {
4609
4609
  webCtx.effect(() => server.register({
4610
4610
  kind: "prefix",
4611
4611
  path: API_PREFIX,
4612
- handler: (req, res) => handle(req, res, ctx, config, ledger)
4612
+ handler: (req, res) => handleTrimRequest(req, res, ctx, config, ledger)
4613
4613
  }), "trim.webApi");
4614
4614
  });
4615
4615
  }
4616
- async function handle(req, res, ctx, config, ledger) {
4616
+ async function handleTrimRequest(req, res, ctx, config, ledger) {
4617
4617
  const connection = ctx.get("connection");
4618
4618
  if (connection === void 0 || typeof connection.requestRejection !== "function") {
4619
4619
  sendError(res, 401, "authentication unavailable");
@@ -4657,6 +4657,13 @@ async function handle(req, res, ctx, config, ledger) {
4657
4657
  });
4658
4658
  return;
4659
4659
  }
4660
+ if (req.method === "POST" && path === "/config") {
4661
+ sendJson(res, 200, {
4662
+ ok: true,
4663
+ data: publicConfig(ctx, normalizeConfig(await updateTrimConfig(ctx, sanitizeConfigPatch(await readJsonBody(req)))))
4664
+ });
4665
+ return;
4666
+ }
4660
4667
  if (req.method === "POST" && path === "/suggest") {
4661
4668
  sendJson(res, 200, {
4662
4669
  ok: true,
@@ -4730,6 +4737,68 @@ async function checkForUpdate() {
4730
4737
  };
4731
4738
  }
4732
4739
  }
4740
+ /** Validate the mutable config fields accepted from the authenticated Web UI. */
4741
+ function sanitizeConfigPatch(input) {
4742
+ if (!input || typeof input !== "object" || Array.isArray(input)) throw new Error("config patch must be an object");
4743
+ const raw = input;
4744
+ const patch = {};
4745
+ if ("mode" in raw) {
4746
+ if (raw.mode !== "advise" && raw.mode !== "auto-safe") throw new Error("invalid mode");
4747
+ patch.mode = raw.mode;
4748
+ }
4749
+ if ("approvalMode" in raw) {
4750
+ if (raw.approvalMode !== "always" && raw.approvalMode !== "risk-based") throw new Error("invalid approvalMode");
4751
+ patch.approvalMode = raw.approvalMode;
4752
+ }
4753
+ if ("suggestionPolicy" in raw) {
4754
+ if (raw.suggestionPolicy !== "conservative" && raw.suggestionPolicy !== "balanced") throw new Error("invalid suggestionPolicy");
4755
+ patch.suggestionPolicy = raw.suggestionPolicy;
4756
+ }
4757
+ if ("approvalThreshold" in raw) {
4758
+ const value = Number(raw.approvalThreshold);
4759
+ if (!Number.isInteger(value) || value < 1 || value > 100) throw new Error("invalid approvalThreshold");
4760
+ patch.approvalThreshold = value;
4761
+ }
4762
+ if ("trackUsage" in raw) {
4763
+ if (typeof raw.trackUsage !== "boolean") throw new Error("invalid trackUsage");
4764
+ patch.trackUsage = raw.trackUsage;
4765
+ }
4766
+ if ("usageRetentionDays" in raw) {
4767
+ const value = Number(raw.usageRetentionDays);
4768
+ if (!Number.isInteger(value) || value < 1 || value > 365) throw new Error("invalid usageRetentionDays");
4769
+ patch.usageRetentionDays = value;
4770
+ }
4771
+ if ("maxDrafts" in raw) {
4772
+ const value = Number(raw.maxDrafts);
4773
+ if (!Number.isInteger(value) || value < 1 || value > 200) throw new Error("invalid maxDrafts");
4774
+ patch.maxDrafts = value;
4775
+ }
4776
+ if ("protect" in raw) patch.protect = stringList(raw.protect, "protect");
4777
+ if ("pinned" in raw) patch.pinned = stringList(raw.pinned, "pinned");
4778
+ if (Object.keys(patch).length === 0) throw new Error("config patch is empty");
4779
+ return patch;
4780
+ }
4781
+ function stringList(value, field) {
4782
+ if (!Array.isArray(value)) throw new Error(`invalid ${field}`);
4783
+ return value.map((item) => typeof item === "string" ? item.trim() : "").filter(Boolean).slice(0, 200);
4784
+ }
4785
+ /** Write the validated patch through the DSH loader so the profile patch remains authoritative. */
4786
+ async function updateTrimConfig(ctx, patch) {
4787
+ const loader = ctx.get("loader");
4788
+ if (!loader || typeof loader.entries !== "function" || typeof loader.update !== "function") throw new Error("loader unavailable; config was not written");
4789
+ let entry;
4790
+ for (const candidate of loader.entries()) if (candidate.options?.id === "dsh-plugin-trim" || candidate.options?.name === "dsh-plugin-trim") {
4791
+ entry = candidate;
4792
+ break;
4793
+ }
4794
+ if (!entry?.options?.id) throw new Error("plugin entry not found; config was not written");
4795
+ const next = {
4796
+ ...entry.options.config && typeof entry.options.config === "object" ? entry.options.config : {},
4797
+ ...patch
4798
+ };
4799
+ await loader.update(entry.options.id, { config: next });
4800
+ return normalizeConfig(next);
4801
+ }
4733
4802
  function publicConfig(ctx, config) {
4734
4803
  const profile = resolveProfileFacts(ctx);
4735
4804
  return {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-plugin-trim",
3
- "version": "0.3.7",
4
- "description": "DSH plugin ecosystem diet and compatibility analyzer",
3
+ "version": "0.3.9",
4
+ "description": "DSH plugin checkup and compatibility analyzer",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",