aegis-desktop 0.8.22 → 0.8.24

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/vendor/platform.js +100 -9
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aegis-desktop",
3
3
  "productName": "AEGIS Desktop",
4
- "version": "0.8.22",
4
+ "version": "0.8.24",
5
5
  "description": "Thin Electron host for AEGIS — a local chat UI over the shared client/aegis.js transport. Ships transport + UI only; engine logic stays server-side.",
6
6
  "author": {
7
7
  "name": "AEGIS Code",
@@ -89,6 +89,24 @@ function defaultDirExists(dir) {
89
89
  }
90
90
  }
91
91
 
92
+ /**
93
+ * Does this directory hold anything? Used only to break the OneDrive-vs-plain
94
+ * Desktop tie below — OneDrive routinely scaffolds an empty `OneDrive\Desktop`
95
+ * folder on install whether or not the user ever turned Desktop redirection
96
+ * on, so mere existence cannot tell a genuinely redirected Desktop apart from
97
+ * that empty placeholder. An unreadable/missing directory counts as empty
98
+ * rather than throwing, since the caller already confirmed it exists with
99
+ * `dirExists` and a race or permissions quirk here should not crash a
100
+ * best-effort path guess.
101
+ */
102
+ function defaultDirNonEmpty(dir) {
103
+ try {
104
+ return fs.readdirSync(dir).length > 0;
105
+ } catch {
106
+ return false;
107
+ }
108
+ }
109
+
92
110
  /** Read a text file; `null` for every failure (missing, unreadable, not text). */
93
111
  function defaultReadText(file) {
94
112
  try {
@@ -475,17 +493,56 @@ function hasPosixModes(options = {}) {
475
493
  return !win32;
476
494
  }
477
495
 
496
+ /**
497
+ * Windows' own answer for "where is this user's Desktop", straight from the
498
+ * registry key Explorer itself reads and OneDrive itself rewrites when it
499
+ * redirects a folder. This is the one source immune to BOTH ways `~/Desktop`
500
+ * guesses wrong at once: it already names the OneDrive path when redirection
501
+ * is active, and it already names the LOCALISED folder — Swedish "Skrivbord",
502
+ * French "Bureau", whatever the session's physical folder is actually called
503
+ * — because it is Windows' own record of a real path, not an English-language
504
+ * guess with a `?` for an ending. Guessing per-language folder names instead
505
+ * would mean enumerating every language Windows ships, forever one behind;
506
+ * asking Windows is both simpler and exact.
507
+ *
508
+ * "Shell Folders" (not "User Shell Folders") is the one queried, because it
509
+ * holds the already-expanded absolute path — "User Shell Folders" is allowed
510
+ * to hold `%USERPROFILE%`-relative values, and this function has no reason to
511
+ * carry its own copy of Windows' expansion rules when the other key already
512
+ * did that work.
513
+ *
514
+ * Returns `''` on anything but a clean hit: `reg.exe` missing, a non-zero
515
+ * exit, unparsable output, or a path the filesystem does not actually have —
516
+ * each of those falls through to the candidate-guessing list in desktopDir()
517
+ * below, which is where this function's absence has always been tolerated.
518
+ */
519
+ function winShellFolderDesktop(probe, dirExists) {
520
+ const out = probe('reg', [
521
+ 'query',
522
+ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders',
523
+ '/v', 'Desktop',
524
+ ]);
525
+ if (!out) return '';
526
+ const m = /^\s*Desktop\s+REG_(?:EXPAND_)?SZ\s+(.+)$/im.exec(out);
527
+ const value = m && m[1].trim();
528
+ return value && dirExists(value) ? value : '';
529
+ }
530
+
478
531
  /**
479
532
  * This user's real Desktop directory, or `''` when none can be found.
480
533
  *
481
534
  * `~/Desktop` is a GUESS, and it is wrong on both of the hosts this function was
482
535
  * added for:
483
536
  *
484
- * - Windows with OneDrive. Folder redirection moves the live Desktop to
485
- * `%USERPROFILE%\OneDrive\Desktop` and commonly leaves an empty
486
- * `%USERPROFILE%\Desktop` behind, so a write to the obvious path lands in a
487
- * directory the user is not looking at. `%OneDrive%` (set while OneDrive
488
- * runs) is consulted first for exactly this case.
537
+ * - Windows with OneDrive, or a localised session. Folder redirection moves
538
+ * the live Desktop to `%USERPROFILE%\OneDrive\Desktop` (or renames it
539
+ * entirely, e.g. Swedish "Skrivbord"), and commonly leaves an empty
540
+ * `%USERPROFILE%\Desktop` behind, so a write to the obvious English path
541
+ * lands in a directory the user is not looking at — or at a name that
542
+ * does not exist on their machine at all. `winShellFolderDesktop()` above
543
+ * asks Windows directly and settles both cases at once; the `%OneDrive%`-
544
+ * and `home`-based guesses below only run when that lookup comes back
545
+ * empty (no `reg.exe`, or the path it names no longer exists).
489
546
  * - A localised Linux desktop. XDG translates the directory ITSELF, not just
490
547
  * its label: a Swedish session has `~/Skrivbord` and no `~/Desktop` at all.
491
548
  * `$XDG_DESKTOP_DIR` answers it, and `~/.config/user-dirs.dirs` is where
@@ -493,15 +550,24 @@ function hasPosixModes(options = {}) {
493
550
  *
494
551
  * macOS is the one host where `~/Desktop` is simply correct.
495
552
  *
496
- * Seams: `dirExists` (default: a real statSync isDirectory probe) and `readText`
497
- * (default: a real utf8 read, `null` on any failure), so all three branches are
498
- * exercised on one host.
553
+ * Seams: `dirExists` (default: a real statSync isDirectory probe), `dirNonEmpty`
554
+ * (default: a real readdirSync length check, used only to break the win32
555
+ * OneDrive-vs-plain guess below when the registry lookup did not settle it),
556
+ * `probe` (default: a real `reg.exe` spawn, `null` on any failure) and
557
+ * `readText` (default: a real utf8 read, `null` on any failure), so every
558
+ * branch is exercised on one host.
499
559
  */
500
560
  function desktopDir(options = {}) {
501
- const { platform, env, win32, o } = seams(options);
561
+ const { platform, env, win32, probe, o } = seams(options);
502
562
  const P = win32 ? path.win32 : path.posix;
503
563
  const home = homeDir(options);
504
564
  const dirExists = typeof o.dirExists === 'function' ? o.dirExists : defaultDirExists;
565
+ const dirNonEmpty = typeof o.dirNonEmpty === 'function' ? o.dirNonEmpty : defaultDirNonEmpty;
566
+
567
+ if (win32) {
568
+ const fromRegistry = winShellFolderDesktop(probe, dirExists);
569
+ if (fromRegistry) return fromRegistry;
570
+ }
505
571
  const readText = typeof o.readText === 'function' ? o.readText : defaultReadText;
506
572
 
507
573
  const candidates = [];
@@ -514,6 +580,31 @@ function desktopDir(options = {}) {
514
580
  if (oneDrive) push(P.join(oneDrive, 'Desktop'));
515
581
  if (home) push(P.join(home, 'OneDrive', 'Desktop'));
516
582
  if (home) push(P.join(home, 'Desktop'));
583
+ // OneDrive is tried first above — real folder redirection moves the live
584
+ // Desktop there and leaves the plain one empty, exactly as the module
585
+ // comment says. But OneDrive also scaffolds an empty `OneDrive\Desktop`
586
+ // on install whether redirection was ever turned on, and that empty
587
+ // placeholder must not outrank a Desktop the user is actually using —
588
+ // reported live: a write landed in an untouched `OneDrive\Desktop` while
589
+ // the populated plain Desktop sat right next to it. Demote every
590
+ // OneDrive candidate below the plain one only in that one unambiguous
591
+ // case (a OneDrive candidate exists but is empty, AND the plain Desktop
592
+ // exists and has something in it); every other combination — including
593
+ // "both empty", which is what a fresh profile and this file's own
594
+ // stubbed tests both look like — keeps the order above, since a guess is
595
+ // still owed and emptiness alone proves nothing there.
596
+ const plainDesktop = home ? P.join(home, 'Desktop') : '';
597
+ const plainIdx = candidates.indexOf(plainDesktop);
598
+ if (plainIdx > 0 && dirExists(plainDesktop) && dirNonEmpty(plainDesktop)) {
599
+ const oneDriveCandidates = candidates.slice(0, plainIdx);
600
+ const allOneDriveEmpty = oneDriveCandidates.every(
601
+ (c) => !dirExists(c) || !dirNonEmpty(c)
602
+ );
603
+ if (allOneDriveEmpty) {
604
+ candidates.splice(plainIdx, 1);
605
+ candidates.unshift(plainDesktop);
606
+ }
607
+ }
517
608
  } else if (platform === 'darwin') {
518
609
  if (home) push(P.join(home, 'Desktop'));
519
610
  } else {