dsh-delete-session 0.2.2 → 0.2.4
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/README.md +4 -1
- package/package.json +1 -1
- package/src/client.js +295 -92
- package/src/index.js +73 -272
- package/test/artifacts.host.test.js +95 -0
- package/test/checkbox.dom.test.js +608 -50
package/src/index.js
CHANGED
|
@@ -258,6 +258,20 @@ public static class Program {
|
|
|
258
258
|
const RECYCLE_EXE_VERSION = 1
|
|
259
259
|
const RECYCLE_TIMEOUT_MS = 60 * 1000
|
|
260
260
|
|
|
261
|
+
/** The C# compiler that ships with .NET Framework, if this machine has it. */
|
|
262
|
+
function findCsc() {
|
|
263
|
+
const windir = process.env.WINDIR || 'C:\\Windows'
|
|
264
|
+
for (const flavor of ['Framework64', 'Framework']) {
|
|
265
|
+
const candidate = path.join(windir, 'Microsoft.NET', flavor, 'v4.0.30319', 'csc.exe')
|
|
266
|
+
try {
|
|
267
|
+
if (fs.statSync(candidate).isFile()) return candidate
|
|
268
|
+
} catch {
|
|
269
|
+
// try the next flavour
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return null
|
|
273
|
+
}
|
|
274
|
+
|
|
261
275
|
/** Compile the recycle helper once into the DSH cache; null when it cannot. */
|
|
262
276
|
async function ensureRecycleExe() {
|
|
263
277
|
if (process.platform !== 'win32') return null
|
|
@@ -300,7 +314,6 @@ async function ensureRecycleExe() {
|
|
|
300
314
|
function warmCompiledHelpers() {
|
|
301
315
|
if (process.platform !== 'win32') return
|
|
302
316
|
const warm = async () => {
|
|
303
|
-
try { await ensurePickerExe() } catch { /* the fallback covers it */ }
|
|
304
317
|
try { await ensureRecycleExe() } catch { /* the fallback covers it */ }
|
|
305
318
|
}
|
|
306
319
|
void warm()
|
|
@@ -393,7 +406,7 @@ async function sendToRecycleBin(targets) {
|
|
|
393
406
|
await fsp.writeFile(scriptPath, bytes)
|
|
394
407
|
output = await runPowershell(
|
|
395
408
|
['-NoProfile', '-STA', '-ExecutionPolicy', 'Bypass', '-File', scriptPath],
|
|
396
|
-
|
|
409
|
+
SHELL_TIMEOUT_MS,
|
|
397
410
|
)
|
|
398
411
|
} catch {
|
|
399
412
|
return false
|
|
@@ -849,234 +862,10 @@ const MAX_LISTED = 500
|
|
|
849
862
|
const RESOLVE_CONCURRENCY = 16
|
|
850
863
|
/** How many directories the folder picker lists before saying there are more. */
|
|
851
864
|
const MAX_BROWSE_ENTRIES = 500
|
|
852
|
-
/** How long
|
|
865
|
+
/** How long the OS chooser may stay open before the request gives up on it. */
|
|
853
866
|
const PICK_TIMEOUT_MS = 5 * 60 * 1000
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
* The folder picker.
|
|
857
|
-
*
|
|
858
|
-
* Windows offers its modern chooser through COM (`IFileOpenDialog` +
|
|
859
|
-
* `FOS_PICKFOLDERS`), and the interesting part is not the dialog but the wait
|
|
860
|
-
* before it. Measured on the target machine:
|
|
861
|
-
*
|
|
862
|
-
* PowerShell startup ......... ~520 ms
|
|
863
|
-
* + Windows.Forms assembly .... ~950 ms (left over from the old
|
|
864
|
-
* `FolderBrowserDialog` version)
|
|
865
|
-
* compiled .exe, no console ... ~120 ms
|
|
866
|
-
*
|
|
867
|
-
* So the picker is a tiny executable: compiled once into the DSH home, spawned
|
|
868
|
-
* directly afterwards. The PowerShell script below stays as the fallback for a
|
|
869
|
-
* machine without `csc.exe`.
|
|
870
|
-
*
|
|
871
|
-
* The executable runs with `windowsHide`, which hides the first window it shows,
|
|
872
|
-
* so a background thread calls `ShowWindow(SW_SHOW)` and raises it. That thread
|
|
873
|
-
* also centres the dialog and retries, because the window reports no final size
|
|
874
|
-
* on the first tick.
|
|
875
|
-
*/
|
|
876
|
-
const PICK_CSHARP = `
|
|
877
|
-
using System;
|
|
878
|
-
using System.Text;
|
|
879
|
-
using System.Threading;
|
|
880
|
-
using System.Runtime.InteropServices;
|
|
881
|
-
|
|
882
|
-
public static class DshFolderPick {
|
|
883
|
-
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr h, int cmd);
|
|
884
|
-
[DllImport("user32.dll")] static extern IntPtr SetWindowPos(IntPtr h, IntPtr after, int x, int y, int cx, int cy, uint flags);
|
|
885
|
-
[DllImport("user32.dll")] static extern bool BringWindowToTop(IntPtr h);
|
|
886
|
-
[DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr l);
|
|
887
|
-
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
|
|
888
|
-
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetClassNameW(IntPtr h, StringBuilder s, int n);
|
|
889
|
-
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
890
|
-
[DllImport("user32.dll")] static extern bool SystemParametersInfoW(uint action, uint param, out RECT rect, uint winIni);
|
|
891
|
-
[DllImport("kernel32.dll")] static extern uint GetCurrentProcessId();
|
|
892
|
-
delegate bool EnumProc(IntPtr h, IntPtr l);
|
|
893
|
-
|
|
894
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
895
|
-
struct RECT { public int Left, Top, Right, Bottom; }
|
|
896
|
-
|
|
897
|
-
static IntPtr FindDialog() {
|
|
898
|
-
uint me = GetCurrentProcessId();
|
|
899
|
-
IntPtr found = IntPtr.Zero;
|
|
900
|
-
EnumWindows((h, l) => {
|
|
901
|
-
uint pid;
|
|
902
|
-
GetWindowThreadProcessId(h, out pid);
|
|
903
|
-
if (pid == me) {
|
|
904
|
-
var cls = new StringBuilder(128, 128);
|
|
905
|
-
GetClassNameW(h, cls, 128);
|
|
906
|
-
if (cls.ToString().StartsWith("#32770") && found == IntPtr.Zero) found = h;
|
|
907
|
-
}
|
|
908
|
-
return true;
|
|
909
|
-
}, IntPtr.Zero);
|
|
910
|
-
return found;
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
static void Raise() {
|
|
914
|
-
bool centered = false;
|
|
915
|
-
for (int i = 0; i < 80; i++) {
|
|
916
|
-
Thread.Sleep(150);
|
|
917
|
-
IntPtr h = FindDialog();
|
|
918
|
-
if (h == IntPtr.Zero) continue;
|
|
919
|
-
ShowWindow(h, 5);
|
|
920
|
-
if (!centered) {
|
|
921
|
-
RECT work, box;
|
|
922
|
-
if (SystemParametersInfoW(0x0030, 0, out work, 0) && GetWindowRect(h, out box)) {
|
|
923
|
-
int w = box.Right - box.Left;
|
|
924
|
-
int ht = box.Bottom - box.Top;
|
|
925
|
-
int availW = work.Right - work.Left;
|
|
926
|
-
int availH = work.Bottom - work.Top;
|
|
927
|
-
// A half-built rect would park the window off screen, so only a
|
|
928
|
-
// believable size that actually fits is allowed to move it.
|
|
929
|
-
if (w > 600 && ht > 300 && w < availW && ht < availH) {
|
|
930
|
-
int x = work.Left + (availW - w) / 2;
|
|
931
|
-
int y = work.Top + (availH - ht) / 2;
|
|
932
|
-
SetWindowPos(h, (IntPtr)(-1), x, y, 0, 0, 0x0001);
|
|
933
|
-
centered = true;
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
SetWindowPos(h, (IntPtr)(-1), 0, 0, 0, 0, 0x0003);
|
|
938
|
-
BringWindowToTop(h);
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
[ComImport, Guid("42f85136-db7e-439c-85f1-e4075d135fc8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
943
|
-
interface IFileOpenDialog {
|
|
944
|
-
[PreserveSig] int Show(IntPtr parent);
|
|
945
|
-
void SetFileTypes(uint c, IntPtr f);
|
|
946
|
-
void SetFileTypeIndex(uint i);
|
|
947
|
-
void GetFileTypeIndex(out uint i);
|
|
948
|
-
void Advise(IntPtr p, out uint c);
|
|
949
|
-
void Unadvise(uint c);
|
|
950
|
-
void SetOptions(uint fos);
|
|
951
|
-
void GetOptions(out uint fos);
|
|
952
|
-
void SetDefaultFolder(IShellItem i);
|
|
953
|
-
void SetFolder(IShellItem i);
|
|
954
|
-
void GetFolder(out IShellItem i);
|
|
955
|
-
void GetCurrentSelection(out IShellItem i);
|
|
956
|
-
void SetFileName([MarshalAs(UnmanagedType.LPWStr)] string n);
|
|
957
|
-
void GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string n);
|
|
958
|
-
void SetTitle([MarshalAs(UnmanagedType.LPWStr)] string t);
|
|
959
|
-
void SetOkButtonLabel([MarshalAs(UnmanagedType.LPWStr)] string t);
|
|
960
|
-
void SetFileNameLabel([MarshalAs(UnmanagedType.LPWStr)] string t);
|
|
961
|
-
void GetResult(out IShellItem i);
|
|
962
|
-
void AddPlace(IShellItem i, uint f);
|
|
963
|
-
void SetDefaultExtension([MarshalAs(UnmanagedType.LPWStr)] string e);
|
|
964
|
-
void Close(int hr);
|
|
965
|
-
void SetClientGuid(ref Guid g);
|
|
966
|
-
void ClearClientData();
|
|
967
|
-
void SetFilter(IntPtr f);
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
[ComImport, Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
971
|
-
interface IShellItem {
|
|
972
|
-
void BindToHandler(IntPtr pbc, ref Guid bhid, ref Guid riid, out IntPtr ppv);
|
|
973
|
-
void GetParent(out IShellItem ppsi);
|
|
974
|
-
void GetDisplayName(uint sigdn, out IntPtr name);
|
|
975
|
-
void GetAttributes(uint mask, out uint attrs);
|
|
976
|
-
void Compare(IShellItem psi, uint hint, out int order);
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
[ComImport, Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
|
|
980
|
-
class FileOpenDialog { }
|
|
981
|
-
|
|
982
|
-
public static string Show(string title, string okLabel) {
|
|
983
|
-
var raiser = new Thread(Raise);
|
|
984
|
-
raiser.IsBackground = true;
|
|
985
|
-
raiser.Start();
|
|
986
|
-
|
|
987
|
-
var dialog = (IFileOpenDialog)new FileOpenDialog();
|
|
988
|
-
uint options;
|
|
989
|
-
dialog.GetOptions(out options);
|
|
990
|
-
dialog.SetOptions(options | 0x20u | 0x40u | 0x800u);
|
|
991
|
-
if (title != null) dialog.SetTitle(title);
|
|
992
|
-
if (okLabel != null) dialog.SetOkButtonLabel(okLabel);
|
|
993
|
-
|
|
994
|
-
int hr = dialog.Show(IntPtr.Zero);
|
|
995
|
-
if (hr != 0) return null;
|
|
996
|
-
|
|
997
|
-
IShellItem item;
|
|
998
|
-
dialog.GetResult(out item);
|
|
999
|
-
IntPtr name;
|
|
1000
|
-
item.GetDisplayName(0x80058000u, out name);
|
|
1001
|
-
string path = Marshal.PtrToStringUni(name);
|
|
1002
|
-
Marshal.FreeCoTaskMem(name);
|
|
1003
|
-
return path;
|
|
1004
|
-
}
|
|
1005
|
-
}`
|
|
1006
|
-
|
|
1007
|
-
/** The compiled form: the class above plus an entry point that prints one path. */
|
|
1008
|
-
const PICK_EXE_SOURCE = PICK_CSHARP + `
|
|
1009
|
-
|
|
1010
|
-
public static class Program {
|
|
1011
|
-
public static int Main(string[] args) {
|
|
1012
|
-
string picked = DshFolderPick.Show(args.Length > 0 ? args[0] : null, args.Length > 1 ? args[1] : null);
|
|
1013
|
-
if (picked == null) return 1;
|
|
1014
|
-
// Written as UTF-8 bytes rather than through Console.Out: its encoding
|
|
1015
|
-
// depends on a console this process does not own, and paths are not ASCII.
|
|
1016
|
-
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(picked);
|
|
1017
|
-
using (System.IO.Stream stdout = Console.OpenStandardOutput()) {
|
|
1018
|
-
stdout.Write(bytes, 0, bytes.Length);
|
|
1019
|
-
stdout.Flush();
|
|
1020
|
-
}
|
|
1021
|
-
return 0;
|
|
1022
|
-
}
|
|
1023
|
-
}`
|
|
1024
|
-
|
|
1025
|
-
/** The fallback for a host without `csc.exe`: the same class through PowerShell. */
|
|
1026
|
-
const PICK_SCRIPT = `[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
1027
|
-
$source = @'
|
|
1028
|
-
${PICK_CSHARP}
|
|
1029
|
-
'@
|
|
1030
|
-
Add-Type -TypeDefinition $source
|
|
1031
|
-
$picked = [DshFolderPick]::Show('选择隔离夹保存位置', '选择此文件夹')
|
|
1032
|
-
if ($picked) { [Console]::Out.Write($picked) }
|
|
1033
|
-
`
|
|
1034
|
-
|
|
1035
|
-
/** Bump when `PICK_EXE_SOURCE` changes, so a cached build is not reused. */
|
|
1036
|
-
const PICK_EXE_VERSION = 1
|
|
1037
|
-
/** Compiling once is slow; it happens on the first ever pick only. */
|
|
1038
|
-
const PICK_COMPILE_TIMEOUT_MS = 60 * 1000
|
|
1039
|
-
|
|
1040
|
-
/** The C# compiler that ships with .NET Framework, if this machine has it. */
|
|
1041
|
-
function findCsc() {
|
|
1042
|
-
const windir = process.env.WINDIR || 'C:\\Windows'
|
|
1043
|
-
for (const flavor of ['Framework64', 'Framework']) {
|
|
1044
|
-
const candidate = path.join(windir, 'Microsoft.NET', flavor, 'v4.0.30319', 'csc.exe')
|
|
1045
|
-
try {
|
|
1046
|
-
if (fs.statSync(candidate).isFile()) return candidate
|
|
1047
|
-
} catch {
|
|
1048
|
-
// try the next flavour
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
return null
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
/**
|
|
1055
|
-
* Compile the picker once into `$DSH_HOME/cache/dsh-delete-session`, then reuse
|
|
1056
|
-
* it. Returns null when this machine cannot compile, so the caller falls back to
|
|
1057
|
-
* the PowerShell path rather than failing the pick.
|
|
1058
|
-
*/
|
|
1059
|
-
async function ensurePickerExe() {
|
|
1060
|
-
if (process.platform !== 'win32') return null
|
|
1061
|
-
const dir = path.join(dshHome(), 'cache', 'dsh-delete-session', `v${PICK_EXE_VERSION}`)
|
|
1062
|
-
const exe = path.join(dir, 'dsh-folder-pick.exe')
|
|
1063
|
-
if (await pathExists(exe)) return exe
|
|
1064
|
-
const csc = findCsc()
|
|
1065
|
-
if (csc === null) return null
|
|
1066
|
-
try {
|
|
1067
|
-
await fsp.mkdir(dir, { recursive: true })
|
|
1068
|
-
const source = path.join(dir, 'picker.cs')
|
|
1069
|
-
await fsp.writeFile(source, PICK_EXE_SOURCE, 'utf8')
|
|
1070
|
-
const built = await runExecutable(csc, ['-nologo', '-target:exe', `-out:${exe}`, '-r:System.dll', source], PICK_COMPILE_TIMEOUT_MS)
|
|
1071
|
-
try { await fsp.rm(source, { force: true }) } catch { /* best effort */ }
|
|
1072
|
-
if (built.code === 0) {
|
|
1073
|
-
if (await pathExists(exe)) return exe
|
|
1074
|
-
}
|
|
1075
|
-
} catch {
|
|
1076
|
-
// fall through to the PowerShell path
|
|
1077
|
-
}
|
|
1078
|
-
return null
|
|
1079
|
-
}
|
|
867
|
+
/** How long a shell helper (the recycle-bin script) may run before giving up. */
|
|
868
|
+
const SHELL_TIMEOUT_MS = 5 * 60 * 1000
|
|
1080
869
|
|
|
1081
870
|
/** Run a program and collect its output, leaving the meaning of the exit to the caller. */
|
|
1082
871
|
function runExecutable(program, args, timeoutMs) {
|
|
@@ -1785,55 +1574,58 @@ function revealDirectory(directory) {
|
|
|
1785
1574
|
? { program: 'open', args: [directory] }
|
|
1786
1575
|
: { program: 'xdg-open', args: [directory] }
|
|
1787
1576
|
const child = spawn(opener.program, opener.args, { detached: true, stdio: 'ignore', windowsHide: false })
|
|
1577
|
+
// Without this listener a failed spawn (explorer missing, blocked by security
|
|
1578
|
+
// software) surfaces as an unhandled `error` event and takes the host down.
|
|
1579
|
+
child.on('error', () => {})
|
|
1788
1580
|
child.unref()
|
|
1789
1581
|
}
|
|
1790
1582
|
|
|
1791
1583
|
/**
|
|
1792
|
-
*
|
|
1793
|
-
*
|
|
1794
|
-
*
|
|
1584
|
+
* Ask the host for a directory.
|
|
1585
|
+
*
|
|
1586
|
+
* DSH mounts `ctx.directoryPicker` (`@deepseek-ai/dsh-host-directory-picker-auto`),
|
|
1587
|
+
* whose capability names the interaction this deployment supports: a native
|
|
1588
|
+
* deployment opens one OS chooser, a browse deployment serves a listing the page
|
|
1589
|
+
* draws itself. Consumers are meant to switch on `capability().kind`.
|
|
1590
|
+
*
|
|
1591
|
+
* This plugin used to compile its own C# chooser and fall back to PowerShell. That
|
|
1592
|
+
* was a reimplementation of something the host already ships, it could not follow a
|
|
1593
|
+
* remote deployment, and it needed a compiler on the machine at first use.
|
|
1594
|
+
*
|
|
1595
|
+
* @returns `{ cancelled, path }` from a native chooser, else `{ browse: true }` or
|
|
1596
|
+
* `{ unsupported: true }` so the page falls back to its own picker.
|
|
1795
1597
|
*/
|
|
1796
|
-
async function
|
|
1797
|
-
const
|
|
1798
|
-
|
|
1799
|
-
|
|
1598
|
+
async function pickFolder(ctx) {
|
|
1599
|
+
const picker = ctx !== null && ctx !== undefined && typeof ctx.get === 'function'
|
|
1600
|
+
? ctx.get('directoryPicker')
|
|
1601
|
+
: undefined
|
|
1602
|
+
if (picker === null || picker === undefined || typeof picker.capability !== 'function') {
|
|
1603
|
+
return { unsupported: true }
|
|
1604
|
+
}
|
|
1605
|
+
let capability
|
|
1800
1606
|
try {
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1607
|
+
capability = picker.capability()
|
|
1608
|
+
} catch {
|
|
1609
|
+
return { unsupported: true }
|
|
1610
|
+
}
|
|
1611
|
+
if (capability === null || capability === undefined) return { unsupported: true }
|
|
1612
|
+
if (capability.kind !== 'native' || typeof capability.pick !== 'function') return { browse: true }
|
|
1613
|
+
// `pick(signal)` is mandatory: the host implementation begins with
|
|
1614
|
+
// `if (signal.aborted) throw …`, so calling it bare throws a TypeError that
|
|
1615
|
+
// reads like an unrelated failure ("… reading 'aborted'"). The timeout matches
|
|
1616
|
+
// the one the previous PowerShell dialog used: a dialog nobody answers must not
|
|
1617
|
+
// hold the request forever.
|
|
1618
|
+
const controller = new AbortController()
|
|
1619
|
+
const timer = setTimeout(() => controller.abort(), PICK_TIMEOUT_MS)
|
|
1620
|
+
let picked
|
|
1621
|
+
try {
|
|
1622
|
+
picked = await capability.pick(controller.signal)
|
|
1805
1623
|
} finally {
|
|
1806
|
-
|
|
1624
|
+
clearTimeout(timer)
|
|
1807
1625
|
}
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
* Raise the real Windows folder picker and return what the user chose. The
|
|
1812
|
-
* browser cannot hand a page a filesystem path, but this process can ask the OS.
|
|
1813
|
-
* @returns `{ cancelled, path }`.
|
|
1814
|
-
*/
|
|
1815
|
-
async function pickFolderNative() {
|
|
1816
|
-
if (process.platform !== 'win32') {
|
|
1817
|
-
throw new HttpError(501, 'unsupported', '系统文件夹对话框只支持 Windows,请改用浏览…')
|
|
1818
|
-
}
|
|
1819
|
-
|
|
1820
|
-
// The compiled picker answers in ~120 ms; the PowerShell fallback costs a
|
|
1821
|
-
// second before the dialog even appears, so it is only the second choice.
|
|
1822
|
-
let picked = ''
|
|
1823
|
-
const exe = await ensurePickerExe()
|
|
1824
|
-
if (exe !== null) {
|
|
1825
|
-
const run = await runExecutable(exe, ['选择隔离夹保存位置', '选择此文件夹'], PICK_TIMEOUT_MS)
|
|
1826
|
-
// Exit 1 is a cancelled dialog; anything else means it never opened.
|
|
1827
|
-
if (run.code !== 0 && run.code !== 1) {
|
|
1828
|
-
throw new HttpError(500, 'shell', `系统对话框异常退出(${run.code})`)
|
|
1829
|
-
}
|
|
1830
|
-
picked = run.code === 0 ? run.stdout : ''
|
|
1831
|
-
} else {
|
|
1832
|
-
picked = await pickFolderWithPowershell()
|
|
1833
|
-
}
|
|
1834
|
-
if (picked === '') return { cancelled: true, path: null }
|
|
1835
|
-
|
|
1836
|
-
const resolved = path.resolve(picked)
|
|
1626
|
+
const chosen = typeof picked === 'string' && picked !== '' ? picked : null
|
|
1627
|
+
if (chosen === null) return { cancelled: true, path: null }
|
|
1628
|
+
const resolved = path.resolve(chosen)
|
|
1837
1629
|
let info
|
|
1838
1630
|
try {
|
|
1839
1631
|
info = await fsp.stat(resolved)
|
|
@@ -1844,6 +1636,7 @@ async function pickFolderNative() {
|
|
|
1844
1636
|
return { cancelled: false, path: resolved }
|
|
1845
1637
|
}
|
|
1846
1638
|
|
|
1639
|
+
|
|
1847
1640
|
// --- http --------------------------------------------------------------------
|
|
1848
1641
|
|
|
1849
1642
|
function isLoopbackAddress(address) {
|
|
@@ -1971,7 +1764,15 @@ export function apply(ctx) {
|
|
|
1971
1764
|
return
|
|
1972
1765
|
}
|
|
1973
1766
|
try {
|
|
1974
|
-
const picked = await
|
|
1767
|
+
const picked = await pickFolder(ctx)
|
|
1768
|
+
if (picked.unsupported === true) {
|
|
1769
|
+
sendJson(res, 200, { ok: false, code: 'unsupported', error: '宿主没有 directoryPicker 服务' })
|
|
1770
|
+
return
|
|
1771
|
+
}
|
|
1772
|
+
if (picked.browse === true) {
|
|
1773
|
+
sendJson(res, 200, { ok: false, code: 'browse', error: '浏览式宿主,请用页面内的选择器' })
|
|
1774
|
+
return
|
|
1775
|
+
}
|
|
1975
1776
|
sendJson(res, 200, { ok: true, ...picked })
|
|
1976
1777
|
} catch (error) {
|
|
1977
1778
|
const status = error instanceof HttpError ? error.status : 500
|
|
@@ -2205,7 +2006,7 @@ export function apply(ctx) {
|
|
|
2205
2006
|
// quarantine against a scratch tree and a stubbed persistence service. Nothing
|
|
2206
2007
|
// else in the host reads this face.
|
|
2207
2008
|
export const internals = {
|
|
2208
|
-
|
|
2009
|
+
pickFolder,
|
|
2209
2010
|
claimQuarantineDirectory,
|
|
2210
2011
|
classifyPaths,
|
|
2211
2012
|
collectSessionPaths,
|
|
@@ -1215,3 +1215,98 @@ test('a child that is still open is left alone and reported as skipped', async (
|
|
|
1215
1215
|
assert.equal(fs.existsSync(child.dir), true, 'a busy child is not deleted under its writer')
|
|
1216
1216
|
assert.equal(result.children[0].skipped, 'busy', 'and the report says why')
|
|
1217
1217
|
})
|
|
1218
|
+
|
|
1219
|
+
// --- the folder picker belongs to the host, not to this plugin ---------------
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* The host mounts `@deepseek-ai/dsh-host-directory-picker-auto` as
|
|
1223
|
+
* `ctx.directoryPicker`; its capability says which interaction the deployment
|
|
1224
|
+
* supports. Native deployments open one OS chooser, browse deployments serve a
|
|
1225
|
+
* listing the page renders itself.
|
|
1226
|
+
*/
|
|
1227
|
+
const pickerContext = (capability) => ({
|
|
1228
|
+
get: (name) => (name === 'directoryPicker' ? { capability: () => capability } : undefined),
|
|
1229
|
+
})
|
|
1230
|
+
|
|
1231
|
+
test('a native host capability answers with the chosen directory', async () => {
|
|
1232
|
+
// A real directory: the plugin still validates what the chooser hands back, so
|
|
1233
|
+
// a path that vanished between the click and the answer is not accepted.
|
|
1234
|
+
const chosen = fs.mkdtempSync(path.join(os.tmpdir(), 'dsdel-pick-'))
|
|
1235
|
+
const ctx = pickerContext({ kind: 'native', pick: async () => chosen })
|
|
1236
|
+
|
|
1237
|
+
const result = await internals.pickFolder(ctx)
|
|
1238
|
+
|
|
1239
|
+
assert.deepEqual(result, { cancelled: false, path: path.resolve(chosen) })
|
|
1240
|
+
fs.rmSync(chosen, { recursive: true, force: true })
|
|
1241
|
+
})
|
|
1242
|
+
|
|
1243
|
+
test('a cancelled native chooser is reported, not thrown', async () => {
|
|
1244
|
+
const ctx = pickerContext({ kind: 'native', pick: async () => null })
|
|
1245
|
+
|
|
1246
|
+
const result = await internals.pickFolder(ctx)
|
|
1247
|
+
|
|
1248
|
+
assert.deepEqual(result, { cancelled: true, path: null })
|
|
1249
|
+
})
|
|
1250
|
+
|
|
1251
|
+
test('a browse-only host asks the page to render its own picker', async () => {
|
|
1252
|
+
const ctx = pickerContext({ kind: 'browse', list: async () => [] })
|
|
1253
|
+
|
|
1254
|
+
const result = await internals.pickFolder(ctx)
|
|
1255
|
+
|
|
1256
|
+
assert.equal(result.browse, true, 'the in-page picker takes over')
|
|
1257
|
+
})
|
|
1258
|
+
|
|
1259
|
+
test('a host without the service is reported as unsupported', async () => {
|
|
1260
|
+
for (const ctx of [{ get: () => undefined }, {}, null]) {
|
|
1261
|
+
const result = await internals.pickFolder(ctx)
|
|
1262
|
+
assert.equal(result.unsupported, true)
|
|
1263
|
+
}
|
|
1264
|
+
})
|
|
1265
|
+
|
|
1266
|
+
test('a capability that throws does not take the request down', async () => {
|
|
1267
|
+
const ctx = { get: () => ({ capability: () => { throw new Error('boom') } }) }
|
|
1268
|
+
|
|
1269
|
+
const result = await internals.pickFolder(ctx)
|
|
1270
|
+
|
|
1271
|
+
assert.equal(result.unsupported, true)
|
|
1272
|
+
})
|
|
1273
|
+
|
|
1274
|
+
test('this plugin no longer compiles or shells out a picker of its own', () => {
|
|
1275
|
+
// The C# source, the compiler probe and the PowerShell fallback all existed
|
|
1276
|
+
// only because the host's own chooser was not being used.
|
|
1277
|
+
const source = fs.readFileSync(new URL('../src/index.js', import.meta.url), 'utf8')
|
|
1278
|
+
// `findCsc` stays: the recycle-bin helper still compiles a C# program. Only the
|
|
1279
|
+
// picker-specific machinery is gone.
|
|
1280
|
+
for (const gone of ['PICK_CSHARP', 'PICK_EXE_SOURCE', 'PICK_SCRIPT', 'ensurePickerExe', 'pickFolderWithPowershell', 'pickFolderNative']) {
|
|
1281
|
+
assert.equal(source.includes(gone), false, gone + ' should be gone')
|
|
1282
|
+
}
|
|
1283
|
+
})
|
|
1284
|
+
|
|
1285
|
+
// --- the picker call must hand the host a real signal ------------------------
|
|
1286
|
+
|
|
1287
|
+
test('the native chooser is given a live abort signal', async () => {
|
|
1288
|
+
// The host implementation starts with `if (signal.aborted) throw …`, so calling
|
|
1289
|
+
// `pick()` with no argument throws "Cannot read properties of undefined
|
|
1290
|
+
// (reading 'aborted')" — which surfaced as a bogus delete failure.
|
|
1291
|
+
let seen
|
|
1292
|
+
const ctx = {
|
|
1293
|
+
get: (name) =>
|
|
1294
|
+
name === 'directoryPicker'
|
|
1295
|
+
? {
|
|
1296
|
+
capability: () => ({
|
|
1297
|
+
kind: 'native',
|
|
1298
|
+
pick: async (signal) => {
|
|
1299
|
+
seen = signal
|
|
1300
|
+
return null
|
|
1301
|
+
},
|
|
1302
|
+
}),
|
|
1303
|
+
}
|
|
1304
|
+
: undefined,
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
await internals.pickFolder(ctx)
|
|
1308
|
+
|
|
1309
|
+
assert.ok(seen, 'a signal was passed')
|
|
1310
|
+
assert.equal(typeof seen.aborted, 'boolean', 'and it is an AbortSignal')
|
|
1311
|
+
assert.equal(seen.aborted, false, 'not already aborted')
|
|
1312
|
+
})
|