dsh-any-background 0.2.4 → 0.2.5

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/lib/index.js CHANGED
@@ -21,6 +21,7 @@ const inject = ["connection", "webServer"];
21
21
  const DATA_DIR = ".dsh-any-background-data";
22
22
  const CONFIG_FILE = "theme-config.json";
23
23
  const WALLPAPER_FILE = "wallpaper.jpg";
24
+ const ROTATION_DIR = "rotation";
24
25
  const VIDEO_ROUTE = "/dsh-any-background/video";
25
26
  const UPLOAD_ROUTE = "/dsh-any-background/video/upload";
26
27
  const UPLOAD_TMP = "wallpaper.upload.tmp";
@@ -84,7 +85,26 @@ const DEFAULT_CONFIG = {
84
85
  generatedBg: null,
85
86
  regenerateOnReload: false,
86
87
  chatTextOpacity: 0,
87
- trajectoryOpacity: 1
88
+ trajectoryOpacity: 1,
89
+ profiles: [],
90
+ rotation: {
91
+ enabled: false,
92
+ mode: "shuffle",
93
+ interval: "daily",
94
+ current: 0,
95
+ items: [],
96
+ lastRotate: null
97
+ },
98
+ schedule: {
99
+ enabled: false,
100
+ mode: "time",
101
+ dayProfile: null,
102
+ nightProfile: null,
103
+ dayStart: "07:00",
104
+ nightStart: "19:00"
105
+ },
106
+ schemeOverride: "auto",
107
+ activeProfile: null
88
108
  };
89
109
  const dataDir = () => dshHomePath(DATA_DIR);
90
110
  const configPath = () => dshHomePath(DATA_DIR, CONFIG_FILE);
@@ -196,7 +216,12 @@ function normalizeConfig(raw) {
196
216
  generatedBg,
197
217
  regenerateOnReload: typeof r.regenerateOnReload === "boolean" ? r.regenerateOnReload : DEFAULT_CONFIG.regenerateOnReload,
198
218
  chatTextOpacity: clamp(r.chatTextOpacity, 0, 1, DEFAULT_CONFIG.chatTextOpacity),
199
- trajectoryOpacity: clamp(r.trajectoryOpacity, 0, 1, DEFAULT_CONFIG.trajectoryOpacity)
219
+ trajectoryOpacity: clamp(r.trajectoryOpacity, 0, 1, DEFAULT_CONFIG.trajectoryOpacity),
220
+ profiles: normalizeProfiles(r.profiles),
221
+ rotation: normalizeRotation(r.rotation),
222
+ schedule: normalizeSchedule(r.schedule),
223
+ schemeOverride: r.schemeOverride === "light" || r.schemeOverride === "dark" ? r.schemeOverride : "auto",
224
+ activeProfile: typeof r.activeProfile === "string" ? r.activeProfile : null
200
225
  };
201
226
  }
202
227
  function normalizeGeneratedBg(p) {
@@ -230,6 +255,101 @@ function normalizeGeneratedBg(p) {
230
255
  };
231
256
  return null;
232
257
  }
258
+ const MAX_PROFILES = 20;
259
+ const MAX_ROTATION_ITEMS = 30;
260
+ const MAX_THUMB_BYTES = 65536;
261
+ const HHMM_RE = /^([01]\d|2[0-3]):[0-5]\d$/;
262
+ /** Coerce an unknown value into a ProfileAppearance (appearance subset only). */
263
+ function normalizeProfileAppearance(raw) {
264
+ const a = raw ?? {};
265
+ const c = a.color;
266
+ const ops = a.opacities ?? {};
267
+ const bl = a.blurs ?? {};
268
+ const blurs = {};
269
+ for (const k of [
270
+ "bg",
271
+ "sidebar",
272
+ "card",
273
+ "settings",
274
+ "chat",
275
+ "trajectory",
276
+ "input"
277
+ ]) blurs[k] = clamp(bl[k], 0, 60, DEFAULT_CONFIG.blurs[k]);
278
+ return {
279
+ color: Array.isArray(c) && c.length === 3 && c.every((x) => typeof x === "number" && isFinite(x)) ? [
280
+ clamp(c[0], 0, 360, 220),
281
+ clamp(c[1], 0, 1, .55),
282
+ clamp(c[2], 0, 1, .25)
283
+ ] : null,
284
+ opacities: {
285
+ bg: clamp(ops.bg, 0, 1, DEFAULT_CONFIG.opacities.bg),
286
+ sidebar: clamp(ops.sidebar, 0, 1, DEFAULT_CONFIG.opacities.sidebar),
287
+ card: clamp(ops.card, 0, 1, DEFAULT_CONFIG.opacities.card),
288
+ input: clamp(ops.input, 0, 1, DEFAULT_CONFIG.opacities.input)
289
+ },
290
+ blurs,
291
+ settingsOpacity: clamp(a.settingsOpacity, 0, 1, DEFAULT_CONFIG.settingsOpacity),
292
+ wallpaperOpacity: clamp(a.wallpaperOpacity, 0, 1, DEFAULT_CONFIG.wallpaperOpacity),
293
+ blur: clamp(a.blur, 0, 60, DEFAULT_CONFIG.blur),
294
+ chatTextOpacity: clamp(a.chatTextOpacity, 0, 1, DEFAULT_CONFIG.chatTextOpacity),
295
+ trajectoryOpacity: clamp(a.trajectoryOpacity, 0, 1, DEFAULT_CONFIG.trajectoryOpacity)
296
+ };
297
+ }
298
+ function normalizeProfiles(raw) {
299
+ if (!Array.isArray(raw)) return [];
300
+ const out = [];
301
+ for (const item of raw.slice(0, MAX_PROFILES)) {
302
+ const p = item ?? {};
303
+ if (typeof p.id !== "string" || p.id.length === 0 || p.id.length > 64) continue;
304
+ if (out.some((e) => e.id === p.id)) continue;
305
+ out.push({
306
+ id: p.id,
307
+ name: typeof p.name === "string" && p.name.trim() ? p.name.slice(0, 60) : "Profile",
308
+ createdAt: typeof p.createdAt === "string" ? p.createdAt : "",
309
+ config: normalizeProfileAppearance(p.config)
310
+ });
311
+ }
312
+ return out;
313
+ }
314
+ /** Rotation items live as files under the rotation dir; only the server
315
+ * creates those names, so a stored `file` is accepted only when it is a bare
316
+ * filename with a known image extension (no path traversal). */
317
+ function safeRotationFile(name) {
318
+ if (typeof name !== "string" || !/^[\w-]+\.(jpg|jpeg|png|gif|webp)$/i.test(name)) return null;
319
+ return name;
320
+ }
321
+ function normalizeRotation(raw) {
322
+ const r = raw ?? {};
323
+ const items = [];
324
+ if (Array.isArray(r.items)) for (const item of r.items.slice(0, MAX_ROTATION_ITEMS)) {
325
+ const it = item ?? {};
326
+ const file = safeRotationFile(it.file);
327
+ if (file === null) continue;
328
+ items.push({
329
+ file,
330
+ thumb: typeof it.thumb === "string" && it.thumb.startsWith("data:image/") && it.thumb.length <= MAX_THUMB_BYTES ? it.thumb : ""
331
+ });
332
+ }
333
+ return {
334
+ enabled: r.enabled === true,
335
+ mode: r.mode === "order" ? "order" : "shuffle",
336
+ interval: r.interval === "reload" || r.interval === "weekly" ? r.interval : "daily",
337
+ current: typeof r.current === "number" && isFinite(r.current) && r.current >= 0 ? Math.floor(r.current) : 0,
338
+ items,
339
+ lastRotate: typeof r.lastRotate === "string" ? r.lastRotate : null
340
+ };
341
+ }
342
+ function normalizeSchedule(raw) {
343
+ const r = raw ?? {};
344
+ return {
345
+ enabled: r.enabled === true,
346
+ mode: r.mode === "system" ? "system" : "time",
347
+ dayProfile: typeof r.dayProfile === "string" ? r.dayProfile : null,
348
+ nightProfile: typeof r.nightProfile === "string" ? r.nightProfile : null,
349
+ dayStart: typeof r.dayStart === "string" && HHMM_RE.test(r.dayStart) ? r.dayStart : DEFAULT_CONFIG.schedule.dayStart,
350
+ nightStart: typeof r.nightStart === "string" && HHMM_RE.test(r.nightStart) ? r.nightStart : DEFAULT_CONFIG.schedule.nightStart
351
+ };
352
+ }
233
353
  async function ensureDir() {
234
354
  try {
235
355
  await mkdir(dataDir(), { recursive: true });
@@ -258,7 +378,8 @@ async function writeConfig(config) {
258
378
  }
259
379
  async function readWallpaper() {
260
380
  try {
261
- return `data:image/jpeg;base64,${(await readFile(wallpaperPath())).toString("base64")}`;
381
+ const buf = await readFile(wallpaperPath());
382
+ return `data:${sniffImageMime(buf)};base64,${buf.toString("base64")}`;
262
383
  } catch {
263
384
  return null;
264
385
  }
@@ -368,6 +489,132 @@ async function writeWallpaperFromUrl(url) {
368
489
  error: "write failed"
369
490
  };
370
491
  }
492
+ const rotationDir = () => dshHomePath(DATA_DIR, ROTATION_DIR);
493
+ async function ensureRotationDir() {
494
+ try {
495
+ await mkdir(rotationDir(), { recursive: true });
496
+ } catch {}
497
+ }
498
+ function imageExtFor(mime) {
499
+ if (mime === "image/png") return "png";
500
+ if (mime === "image/gif") return "gif";
501
+ if (mime === "image/webp") return "webp";
502
+ return "jpg";
503
+ }
504
+ /** Accept only an inline base64 image data URL (same fence as writeWallpaper). */
505
+ function decodeImageDataUrl(dataUrl) {
506
+ if (typeof dataUrl !== "string") return null;
507
+ const m = /^data:image\/[a-zA-Z0-9.+-]+;base64,([A-Za-z0-9+/=]+)$/.exec(dataUrl);
508
+ if (!m) return null;
509
+ const buf = Buffer.from(m[1], "base64");
510
+ return buf.length > 0 ? buf : null;
511
+ }
512
+ /** Persist a thumbnail string only when it is a small inline image data URL. */
513
+ function sanitizeThumb(thumb) {
514
+ return typeof thumb === "string" && thumb.startsWith("data:image/") && thumb.length <= MAX_THUMB_BYTES ? thumb : "";
515
+ }
516
+ async function handleRotationAdd(payload) {
517
+ const buf = decodeImageDataUrl(payload?.dataUrl);
518
+ if (buf === null) return {
519
+ ok: false,
520
+ error: "invalid image"
521
+ };
522
+ const thumb = sanitizeThumb(payload?.thumb);
523
+ await ensureDir();
524
+ await ensureRotationDir();
525
+ if ((await readConfig()).rotation.items.length >= MAX_ROTATION_ITEMS) return {
526
+ ok: false,
527
+ error: "too many items"
528
+ };
529
+ const file = `wp-${Date.now().toString(36)}.${imageExtFor(sniffImageMime(buf))}`;
530
+ try {
531
+ await writeFile(dshHomePath(DATA_DIR, ROTATION_DIR, file), buf);
532
+ } catch (e) {
533
+ console.error("dsh-any-background: failed to write a rotation wallpaper", e);
534
+ return {
535
+ ok: false,
536
+ error: "write failed"
537
+ };
538
+ }
539
+ const fresh = await readConfig();
540
+ fresh.rotation.items.push({
541
+ file,
542
+ thumb
543
+ });
544
+ if (!await writeConfig(fresh)) return {
545
+ ok: false,
546
+ error: "config write failed"
547
+ };
548
+ return {
549
+ ok: true,
550
+ index: fresh.rotation.items.length - 1,
551
+ items: fresh.rotation.items
552
+ };
553
+ }
554
+ async function handleRotationRemove(payload) {
555
+ const idx = payload?.index;
556
+ if (typeof idx !== "number" || !isFinite(idx)) return {
557
+ ok: false,
558
+ error: "invalid index"
559
+ };
560
+ const cfg = await readConfig();
561
+ const i = Math.floor(idx);
562
+ if (i < 0 || i >= cfg.rotation.items.length) return {
563
+ ok: false,
564
+ error: "not found"
565
+ };
566
+ const [removed] = cfg.rotation.items.splice(i, 1);
567
+ cfg.rotation.current = Math.max(0, Math.min(cfg.rotation.current >= i ? cfg.rotation.current - 1 : cfg.rotation.current, Math.max(0, cfg.rotation.items.length - 1)));
568
+ if (removed !== void 0) try {
569
+ await rm(dshHomePath(DATA_DIR, ROTATION_DIR, removed.file), { force: true });
570
+ } catch {}
571
+ if (!await writeConfig(cfg)) return {
572
+ ok: false,
573
+ error: "config write failed"
574
+ };
575
+ return {
576
+ ok: true,
577
+ items: cfg.rotation.items
578
+ };
579
+ }
580
+ /** Activate a rotation item: copy its bytes over the active wallpaper slot and
581
+ * return the freshly sniffed data URL so the client can apply it live. */
582
+ async function handleRotationSet(payload) {
583
+ const idx = payload?.index;
584
+ if (typeof idx !== "number" || !isFinite(idx)) return {
585
+ ok: false,
586
+ error: "invalid index"
587
+ };
588
+ const cfg = await readConfig();
589
+ const i = Math.floor(idx);
590
+ const item = cfg.rotation.items[i];
591
+ if (item === void 0) return {
592
+ ok: false,
593
+ error: "not found"
594
+ };
595
+ let buf;
596
+ try {
597
+ buf = await readFile(dshHomePath(DATA_DIR, ROTATION_DIR, item.file));
598
+ } catch {
599
+ return {
600
+ ok: false,
601
+ error: "file missing"
602
+ };
603
+ }
604
+ try {
605
+ await writeFile(wallpaperPath(), buf);
606
+ } catch (e) {
607
+ console.error("dsh-any-background: failed to activate a rotation wallpaper", e);
608
+ return {
609
+ ok: false,
610
+ error: "write failed"
611
+ };
612
+ }
613
+ return {
614
+ ok: true,
615
+ dataUrl: `data:${sniffImageMime(buf)};base64,${buf.toString("base64")}`
616
+ };
617
+ }
371
618
  async function videoUrl() {
372
619
  return await findVideoFile() ? VIDEO_ROUTE : null;
373
620
  }
@@ -539,20 +786,67 @@ async function handleVideoUpload(req, res) {
539
786
  const NS = "dshAnyBackground";
540
787
  const RPC_CHANNEL = "/dsh-any-background";
541
788
  const RPC_BODY_MAX = 314572800;
789
+ /** ISO week key — mirrors the client's rotationDue so daily/weekly cadence
790
+ * decisions agree across both halves. */
791
+ function isoWeekKey(d) {
792
+ const t = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
793
+ const day = t.getUTCDay() || 7;
794
+ t.setUTCDate(t.getUTCDate() + 4 - day);
795
+ const yearStart = new Date(Date.UTC(t.getUTCFullYear(), 0, 1));
796
+ const week = Math.ceil(((t.getTime() - yearStart.getTime()) / 864e5 + 1) / 7);
797
+ return `${t.getUTCFullYear()}-W${week}`;
798
+ }
799
+ /** Advance the rotation pool when its cadence is due: pick the next item,
800
+ * copy it over the active wallpaper slot, and persist the rotation state.
801
+ * Runs inside `read` so a reload restores the NEW wallpaper directly — the
802
+ * old one never reaches the screen. The client's maybeRotate stays as a
803
+ * fallback and skips when this already advanced (the `rotated` flag). */
804
+ async function advanceRotationIfDue() {
805
+ const cfg = await readConfig();
806
+ const rot = cfg.rotation;
807
+ if (!rot.enabled || rot.items.length === 0) return false;
808
+ const now = /* @__PURE__ */ new Date();
809
+ const last = rot.lastRotate !== null ? new Date(rot.lastRotate) : null;
810
+ if (!(rot.interval === "reload" || last === null || isNaN(last.getTime()) || (rot.interval === "daily" ? last.toDateString() !== now.toDateString() : isoWeekKey(last) !== isoWeekKey(now)))) return false;
811
+ const n = rot.items.length;
812
+ let idx = rot.current;
813
+ if (rot.mode === "shuffle" && n > 1) while (idx === rot.current) idx = Math.floor(Math.random() * n);
814
+ else idx = (rot.current + 1) % n;
815
+ const item = rot.items[idx];
816
+ if (item === void 0) return false;
817
+ try {
818
+ const buf = await readFile(dshHomePath(DATA_DIR, ROTATION_DIR, item.file));
819
+ await writeFile(wallpaperPath(), buf);
820
+ } catch (e) {
821
+ console.error("dsh-any-background: failed to advance the rotation pool", e);
822
+ return false;
823
+ }
824
+ cfg.rotation = {
825
+ ...rot,
826
+ current: idx,
827
+ lastRotate: now.toISOString()
828
+ };
829
+ if (!await writeConfig(cfg)) return false;
830
+ return true;
831
+ }
542
832
  /** Dispatch one decoded RPC method to the matching persistence routine and
543
833
  * return the wire `result` half of the server-response envelope. */
544
834
  async function handleRpcMethod(endpoint, payload) {
545
835
  const method = endpoint.slice(`${NS}/`.length);
546
836
  try {
547
837
  switch (method) {
548
- case "read": return {
549
- ok: true,
550
- value: {
551
- config: await readConfig(),
552
- wallpaper: await readWallpaper(),
553
- videoUrl: await videoUrl()
554
- }
555
- };
838
+ case "read": {
839
+ const rotated = await advanceRotationIfDue();
840
+ return {
841
+ ok: true,
842
+ value: {
843
+ config: await readConfig(),
844
+ wallpaper: await readWallpaper(),
845
+ videoUrl: await videoUrl(),
846
+ rotated
847
+ }
848
+ };
849
+ }
556
850
  case "writeConfig": return {
557
851
  ok: true,
558
852
  value: await writeConfig(payload?.config ?? {})
@@ -569,6 +863,18 @@ async function handleRpcMethod(endpoint, payload) {
569
863
  ok: true,
570
864
  value: await writeWallpaperFromUrl(payload?.url ?? null)
571
865
  };
866
+ case "rotationAdd": return {
867
+ ok: true,
868
+ value: await handleRotationAdd(payload)
869
+ };
870
+ case "rotationRemove": return {
871
+ ok: true,
872
+ value: await handleRotationRemove(payload)
873
+ };
874
+ case "rotationSet": return {
875
+ ok: true,
876
+ value: await handleRotationSet(payload)
877
+ };
572
878
  default: return {
573
879
  ok: false,
574
880
  error: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-any-background",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Appearance plugin for DeepSeek Harness: custom theme color (PS-style color wheel), background wallpaper with opacity/blur controls, and separate main/settings interface opacity.",
5
5
  "keywords": [
6
6
  "deepseek-harness",