@tianacloud/dsh-remote 0.2.5 → 0.2.7
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/client.js +306 -32
- package/host-main.js +196 -15
- package/package.json +1 -1
- package/src/connect.js +55 -5
- package/src/dsh-packages.js +96 -7
- package/src/frontend-assets.js +90 -32
- package/src/host-bridge.js +76 -12
- package/src/remote-api.js +60 -1
- package/src/state.js +90 -2
- package/src/tiana-cli.js +126 -86
package/host-main.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
1
2
|
import { existsSync, mkdirSync, watch } from 'node:fs';
|
|
2
3
|
import { dirname } from 'node:path';
|
|
4
|
+
import { isProtectedWebPlugin, normalizePluginPolicy } from './src/frontend-assets.js';
|
|
3
5
|
import { HostBridge } from './src/host-bridge.js';
|
|
4
6
|
import { registerRemoteRoutes, registerWebServerRoutes } from './src/remote-api.js';
|
|
5
7
|
import {
|
|
8
|
+
claimBridgeOwnership,
|
|
6
9
|
defaultRuntimePath,
|
|
7
10
|
defaultStatePath,
|
|
8
11
|
DEFAULT_STUN_URLS,
|
|
9
12
|
ensureE2eeConfig,
|
|
10
13
|
loadNormalizedRemoteState,
|
|
11
14
|
loadRemoteState,
|
|
15
|
+
PLUGIN_ACCESS_MODE_ALLOW_ALL,
|
|
16
|
+
PLUGIN_ACCESS_MODE_DENY_ALL,
|
|
17
|
+
releaseBridgeOwnership,
|
|
12
18
|
removeRuntimeInfo,
|
|
13
19
|
saveRemoteState,
|
|
14
20
|
withStateLock,
|
|
@@ -29,6 +35,17 @@ export const inject = ['webServer', 'connection'];
|
|
|
29
35
|
|
|
30
36
|
const TOKEN_CHECK_INTERVAL_MS = 12 * 60 * 60 * 1000;
|
|
31
37
|
const TOKEN_RENEW_LEAD_SECONDS = 7 * 86400;
|
|
38
|
+
const BRIDGE_CONFLICT_RETRY_MS = 10_000;
|
|
39
|
+
|
|
40
|
+
function computeEffectiveAllowedPlugins(installedWebPlugins, policy) {
|
|
41
|
+
const normalized = normalizePluginPolicy(policy);
|
|
42
|
+
if (normalized.mode === PLUGIN_ACCESS_MODE_ALLOW_ALL) {
|
|
43
|
+
const blocked = new Set(normalized.blockedWebPlugins);
|
|
44
|
+
return installedWebPlugins.filter((id) => !blocked.has(id));
|
|
45
|
+
}
|
|
46
|
+
const allowed = new Set(normalized.allowedWebPlugins);
|
|
47
|
+
return installedWebPlugins.filter((id) => allowed.has(id));
|
|
48
|
+
}
|
|
32
49
|
|
|
33
50
|
export function createHostController(options = {}) {
|
|
34
51
|
const statePath = options.statePath || defaultStatePath();
|
|
@@ -39,9 +56,16 @@ export function createHostController(options = {}) {
|
|
|
39
56
|
const getConnection = options.getConnection || (() => null);
|
|
40
57
|
const getClientModules = options.getClientModules || (() => null);
|
|
41
58
|
const getWebApp = options.getWebApp || (() => null);
|
|
59
|
+
const instanceId = randomBytes(8).toString('hex');
|
|
60
|
+
const bridgeConflictRetryMs = options.bridgeConflictRetryMs ?? BRIDGE_CONFLICT_RETRY_MS;
|
|
61
|
+
const claimLock = options.claimBridgeOwnership || claimBridgeOwnership;
|
|
62
|
+
const releaseLock = options.releaseBridgeOwnership || releaseBridgeOwnership;
|
|
42
63
|
|
|
43
64
|
let bridge = null;
|
|
44
65
|
let activeStateKey = '';
|
|
66
|
+
let bridgeLockHeld = false;
|
|
67
|
+
let bridgeConflict = null;
|
|
68
|
+
let conflictRetryTimer = null;
|
|
45
69
|
let disposed = false;
|
|
46
70
|
let watcher = null;
|
|
47
71
|
let reloadTimer = null;
|
|
@@ -66,8 +90,15 @@ export function createHostController(options = {}) {
|
|
|
66
90
|
function effectiveConfig(state) {
|
|
67
91
|
const p2p = process.env.DSH_REMOTE_P2P ?? state?.network?.p2pEnabled ?? true;
|
|
68
92
|
const stun = process.env.DSH_REMOTE_STUN ?? state?.network?.stunUrls ?? DEFAULT_STUN_URLS;
|
|
69
|
-
const
|
|
70
|
-
|
|
93
|
+
const envPlugins = process.env.DSH_REMOTE_WEB_PLUGINS;
|
|
94
|
+
const pluginPolicy = envPlugins !== undefined
|
|
95
|
+
? normalizePluginPolicy(envPlugins)
|
|
96
|
+
: normalizePluginPolicy({
|
|
97
|
+
mode: state?.security?.pluginAccessMode,
|
|
98
|
+
blockedWebPlugins: state?.security?.blockedWebPlugins,
|
|
99
|
+
allowedWebPlugins: state?.security?.allowedWebPlugins,
|
|
100
|
+
});
|
|
101
|
+
return { p2p, stun, pluginPolicy };
|
|
71
102
|
}
|
|
72
103
|
|
|
73
104
|
function stateSignature(state) {
|
|
@@ -117,17 +148,35 @@ export function createHostController(options = {}) {
|
|
|
117
148
|
webId: state.web?.id || '',
|
|
118
149
|
p2p: eff.p2p,
|
|
119
150
|
stun: eff.stun,
|
|
120
|
-
frontendOptions: {
|
|
151
|
+
frontendOptions: { pluginPolicy: eff.pluginPolicy },
|
|
121
152
|
log,
|
|
122
153
|
};
|
|
123
154
|
}
|
|
124
155
|
|
|
125
|
-
function stopBridge() {
|
|
156
|
+
function stopBridge({ releaseOwnership = true } = {}) {
|
|
157
|
+
clearTimeout(conflictRetryTimer);
|
|
158
|
+
conflictRetryTimer = null;
|
|
126
159
|
if (bridge) {
|
|
127
160
|
bridge.stop();
|
|
128
161
|
bridge = null;
|
|
129
162
|
}
|
|
130
163
|
activeStateKey = '';
|
|
164
|
+
if (releaseOwnership) {
|
|
165
|
+
bridgeConflict = null;
|
|
166
|
+
if (bridgeLockHeld) {
|
|
167
|
+
bridgeLockHeld = false;
|
|
168
|
+
void releaseLock(instanceId, statePath);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function scheduleConflictRetry() {
|
|
174
|
+
if (disposed || conflictRetryTimer) return;
|
|
175
|
+
conflictRetryTimer = setTimeout(() => {
|
|
176
|
+
conflictRetryTimer = null;
|
|
177
|
+
void syncBridgeWithState().catch(() => {});
|
|
178
|
+
}, bridgeConflictRetryMs);
|
|
179
|
+
conflictRetryTimer.unref?.();
|
|
131
180
|
}
|
|
132
181
|
|
|
133
182
|
async function syncBridgeWithState() {
|
|
@@ -163,13 +212,48 @@ export function createHostController(options = {}) {
|
|
|
163
212
|
hostName: state.host.hostName,
|
|
164
213
|
p2pEnabled: eff.p2p,
|
|
165
214
|
stunUrls: eff.stun,
|
|
166
|
-
|
|
215
|
+
pluginPolicy: eff.pluginPolicy,
|
|
167
216
|
relayToken: state.function.token,
|
|
168
217
|
});
|
|
169
218
|
return;
|
|
170
219
|
}
|
|
171
220
|
|
|
172
|
-
|
|
221
|
+
if (!bridgeLockHeld && options.disableBridgeLock !== true) {
|
|
222
|
+
const webServer = getWebServer();
|
|
223
|
+
const webApp = getWebApp();
|
|
224
|
+
const claim = await claimLock({
|
|
225
|
+
instanceId,
|
|
226
|
+
hostId: state.host.hostId,
|
|
227
|
+
origin: webServer?.getOrigin?.() || '',
|
|
228
|
+
advertisedOrigin: webApp?.getAdvertisedUrl?.() || '',
|
|
229
|
+
}, statePath);
|
|
230
|
+
if (disposed) {
|
|
231
|
+
if (claim.acquired) void releaseLock(instanceId, statePath);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (!claim.acquired) {
|
|
235
|
+
const ownerPid = claim.owner?.pid || null;
|
|
236
|
+
const ownerOrigin = claim.owner?.advertisedOrigin || claim.owner?.origin || '';
|
|
237
|
+
const shouldLog = !bridgeConflict || bridgeConflict.ownerPid !== ownerPid;
|
|
238
|
+
bridgeConflict = {
|
|
239
|
+
ownerPid,
|
|
240
|
+
ownerOrigin,
|
|
241
|
+
hostId: claim.owner?.hostId || state.host.hostId,
|
|
242
|
+
detectedAt: new Date().toISOString(),
|
|
243
|
+
};
|
|
244
|
+
if (shouldLog) {
|
|
245
|
+
log(
|
|
246
|
+
`[dsh-remote] Another live DSH process (PID ${ownerPid ?? 'unknown'}${ownerOrigin ? `, ${ownerOrigin}` : ''}) already owns the Relay bridge; skipping duplicate HostBridge startup.`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
scheduleConflictRetry();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
bridgeLockHeld = true;
|
|
253
|
+
bridgeConflict = null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
stopBridge({ releaseOwnership: false });
|
|
173
257
|
bridge = createBridge(buildBridgeOptions(state));
|
|
174
258
|
activeStateKey = sig;
|
|
175
259
|
await bridge.start();
|
|
@@ -223,19 +307,46 @@ export function createHostController(options = {}) {
|
|
|
223
307
|
if (!Array.isArray(graph?.entries)) return [];
|
|
224
308
|
return graph.entries
|
|
225
309
|
.map((e) => e.id)
|
|
226
|
-
.filter((id) => typeof id === 'string' && !
|
|
310
|
+
.filter((id) => typeof id === 'string' && !isProtectedWebPlugin(id));
|
|
227
311
|
} catch {
|
|
228
312
|
return [];
|
|
229
313
|
}
|
|
230
314
|
}
|
|
231
315
|
|
|
316
|
+
function buildPluginAccessSnapshot(state, installedWebPlugins) {
|
|
317
|
+
const mode = state?.security?.pluginAccessMode === PLUGIN_ACCESS_MODE_DENY_ALL
|
|
318
|
+
? PLUGIN_ACCESS_MODE_DENY_ALL
|
|
319
|
+
: PLUGIN_ACCESS_MODE_ALLOW_ALL;
|
|
320
|
+
const blockedWebPlugins = Array.isArray(state?.security?.blockedWebPlugins)
|
|
321
|
+
? state.security.blockedWebPlugins.filter((id) => !isProtectedWebPlugin(id))
|
|
322
|
+
: [];
|
|
323
|
+
const allowedWebPlugins = Array.isArray(state?.security?.allowedWebPlugins)
|
|
324
|
+
? state.security.allowedWebPlugins.filter((id) => !isProtectedWebPlugin(id))
|
|
325
|
+
: [];
|
|
326
|
+
const eff = effectiveConfig(state);
|
|
327
|
+
return {
|
|
328
|
+
ok: true,
|
|
329
|
+
mode,
|
|
330
|
+
blockedWebPlugins,
|
|
331
|
+
allowedWebPlugins,
|
|
332
|
+
installedWebPlugins,
|
|
333
|
+
effectiveMode: eff.pluginPolicy.mode,
|
|
334
|
+
effectiveAllowedPlugins: computeEffectiveAllowedPlugins(installedWebPlugins, eff.pluginPolicy),
|
|
335
|
+
envOverride: process.env.DSH_REMOTE_WEB_PLUGINS !== undefined,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
232
339
|
const controller = {
|
|
233
340
|
getBridge: () => bridge,
|
|
234
341
|
|
|
235
342
|
async boot() {
|
|
236
343
|
const webServer = getWebServer();
|
|
344
|
+
const webApp = getWebApp();
|
|
237
345
|
try {
|
|
238
|
-
writeRuntimeInfo({
|
|
346
|
+
writeRuntimeInfo({
|
|
347
|
+
origin: webServer?.getOrigin?.() || '',
|
|
348
|
+
advertisedOrigin: webApp?.getAdvertisedUrl?.() || '',
|
|
349
|
+
}, runtimePath);
|
|
239
350
|
} catch { /* non-fatal */ }
|
|
240
351
|
if (process.env.DSH_REMOTE_DISABLE !== '1') {
|
|
241
352
|
const existing = loadRemoteState(statePath);
|
|
@@ -254,6 +365,7 @@ export function createHostController(options = {}) {
|
|
|
254
365
|
dispose() {
|
|
255
366
|
disposed = true;
|
|
256
367
|
clearTimeout(reloadTimer);
|
|
368
|
+
clearTimeout(conflictRetryTimer);
|
|
257
369
|
clearInterval(tokenTimer);
|
|
258
370
|
try { watcher?.close(); } catch { /* ignore */ }
|
|
259
371
|
watcher = null;
|
|
@@ -277,11 +389,58 @@ export function createHostController(options = {}) {
|
|
|
277
389
|
};
|
|
278
390
|
},
|
|
279
391
|
|
|
392
|
+
async getPluginAccess() {
|
|
393
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
394
|
+
const installedWebPlugins = await listLocalWebPlugins();
|
|
395
|
+
return buildPluginAccessSnapshot(state, installedWebPlugins);
|
|
396
|
+
},
|
|
397
|
+
|
|
398
|
+
async updatePluginAccess(patch = {}) {
|
|
399
|
+
const saved = await withStateLock(() => {
|
|
400
|
+
const state = loadNormalizedRemoteState(statePath);
|
|
401
|
+
if (!state) throw Object.assign(new Error('Remote access is not configured yet'), { status: 400 });
|
|
402
|
+
const nextSecurity = {
|
|
403
|
+
...(state.security && typeof state.security === 'object' ? state.security : {}),
|
|
404
|
+
};
|
|
405
|
+
if (patch.mode !== undefined) {
|
|
406
|
+
nextSecurity.pluginAccessMode = patch.mode === PLUGIN_ACCESS_MODE_DENY_ALL
|
|
407
|
+
? PLUGIN_ACCESS_MODE_DENY_ALL
|
|
408
|
+
: PLUGIN_ACCESS_MODE_ALLOW_ALL;
|
|
409
|
+
}
|
|
410
|
+
if (patch.blockedWebPlugins !== undefined) {
|
|
411
|
+
nextSecurity.blockedWebPlugins = [...new Set(
|
|
412
|
+
patch.blockedWebPlugins.map((p) => String(p).trim()).filter((p) => p && !isProtectedWebPlugin(p)),
|
|
413
|
+
)];
|
|
414
|
+
}
|
|
415
|
+
if (patch.allowedWebPlugins !== undefined) {
|
|
416
|
+
nextSecurity.allowedWebPlugins = [...new Set(
|
|
417
|
+
patch.allowedWebPlugins.map((p) => String(p).trim()).filter((p) => p && !isProtectedWebPlugin(p)),
|
|
418
|
+
)];
|
|
419
|
+
state.allowedWebPlugins = nextSecurity.allowedWebPlugins;
|
|
420
|
+
}
|
|
421
|
+
state.security = nextSecurity;
|
|
422
|
+
state.updatedAt = new Date().toISOString();
|
|
423
|
+
saveRemoteState(state, statePath);
|
|
424
|
+
return state;
|
|
425
|
+
}, statePath);
|
|
426
|
+
|
|
427
|
+
if (bridge) {
|
|
428
|
+
const eff = effectiveConfig(saved);
|
|
429
|
+
bridge.updateSettings({ pluginPolicy: eff.pluginPolicy });
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const installedWebPlugins = await listLocalWebPlugins();
|
|
433
|
+
return buildPluginAccessSnapshot(saved, installedWebPlugins);
|
|
434
|
+
},
|
|
435
|
+
|
|
280
436
|
async getAdminOverview() {
|
|
281
437
|
const state = loadNormalizedRemoteState(statePath);
|
|
282
438
|
const snap = bridge?.getStatusSnapshot() || {
|
|
283
439
|
running: false,
|
|
284
440
|
relayConnected: false,
|
|
441
|
+
hostConflict: Boolean(bridgeConflict),
|
|
442
|
+
lastCloseCode: null,
|
|
443
|
+
lastCloseReason: '',
|
|
285
444
|
sessions: [],
|
|
286
445
|
p2pEnabled: state?.network?.p2pEnabled !== false,
|
|
287
446
|
stunUrls: state?.network?.stunUrls || DEFAULT_STUN_URLS,
|
|
@@ -300,6 +459,7 @@ export function createHostController(options = {}) {
|
|
|
300
459
|
// leave loggedIn: false
|
|
301
460
|
}
|
|
302
461
|
const installedWebPlugins = await listLocalWebPlugins();
|
|
462
|
+
const pluginAccess = buildPluginAccessSnapshot(state, installedWebPlugins);
|
|
303
463
|
const configured = Boolean(state?.function?.url && state?.web?.id && state?.host?.hostId);
|
|
304
464
|
return {
|
|
305
465
|
ok: true,
|
|
@@ -307,6 +467,7 @@ export function createHostController(options = {}) {
|
|
|
307
467
|
enabled: configured ? state.enabled !== false : false,
|
|
308
468
|
envOverrides: getEnvOverrides(),
|
|
309
469
|
bridge: snap,
|
|
470
|
+
bridgeConflict,
|
|
310
471
|
tiana,
|
|
311
472
|
owner: state?.owner ? {
|
|
312
473
|
userId: state.owner.userId,
|
|
@@ -337,7 +498,12 @@ export function createHostController(options = {}) {
|
|
|
337
498
|
createdAt: state.e2ee.createdAt,
|
|
338
499
|
} : null,
|
|
339
500
|
network: state?.network || { p2pEnabled: true, stunUrls: [...DEFAULT_STUN_URLS] },
|
|
340
|
-
security: state?.security || {
|
|
501
|
+
security: state?.security || {
|
|
502
|
+
pluginAccessMode: PLUGIN_ACCESS_MODE_ALLOW_ALL,
|
|
503
|
+
blockedWebPlugins: [],
|
|
504
|
+
allowedWebPlugins: [],
|
|
505
|
+
},
|
|
506
|
+
pluginAccess,
|
|
341
507
|
allowedWebPlugins: state?.security?.allowedWebPlugins || [],
|
|
342
508
|
installedWebPlugins,
|
|
343
509
|
};
|
|
@@ -423,12 +589,27 @@ export function createHostController(options = {}) {
|
|
|
423
589
|
stunUrls: patch.stunUrls !== undefined ? patch.stunUrls : state.network.stunUrls,
|
|
424
590
|
};
|
|
425
591
|
}
|
|
426
|
-
if (patch.allowedWebPlugins !== undefined) {
|
|
427
|
-
|
|
428
|
-
...state.security,
|
|
429
|
-
allowedWebPlugins: patch.allowedWebPlugins,
|
|
592
|
+
if (patch.pluginAccessMode !== undefined || patch.blockedWebPlugins !== undefined || patch.allowedWebPlugins !== undefined) {
|
|
593
|
+
const nextSecurity = {
|
|
594
|
+
...(state.security && typeof state.security === 'object' ? state.security : {}),
|
|
430
595
|
};
|
|
431
|
-
|
|
596
|
+
if (patch.pluginAccessMode !== undefined) {
|
|
597
|
+
nextSecurity.pluginAccessMode = patch.pluginAccessMode === PLUGIN_ACCESS_MODE_DENY_ALL
|
|
598
|
+
? PLUGIN_ACCESS_MODE_DENY_ALL
|
|
599
|
+
: PLUGIN_ACCESS_MODE_ALLOW_ALL;
|
|
600
|
+
}
|
|
601
|
+
if (patch.blockedWebPlugins !== undefined) {
|
|
602
|
+
nextSecurity.blockedWebPlugins = [...new Set(
|
|
603
|
+
patch.blockedWebPlugins.map((p) => String(p).trim()).filter((p) => p && !isProtectedWebPlugin(p)),
|
|
604
|
+
)];
|
|
605
|
+
}
|
|
606
|
+
if (patch.allowedWebPlugins !== undefined) {
|
|
607
|
+
nextSecurity.allowedWebPlugins = [...new Set(
|
|
608
|
+
patch.allowedWebPlugins.map((p) => String(p).trim()).filter((p) => p && !isProtectedWebPlugin(p)),
|
|
609
|
+
)];
|
|
610
|
+
state.allowedWebPlugins = nextSecurity.allowedWebPlugins;
|
|
611
|
+
}
|
|
612
|
+
state.security = nextSecurity;
|
|
432
613
|
}
|
|
433
614
|
state.updatedAt = new Date().toISOString();
|
|
434
615
|
saveRemoteState(state, statePath);
|
|
@@ -441,7 +622,7 @@ export function createHostController(options = {}) {
|
|
|
441
622
|
hostName: saved.host?.hostName,
|
|
442
623
|
p2pEnabled: eff.p2p,
|
|
443
624
|
stunUrls: eff.stun,
|
|
444
|
-
|
|
625
|
+
pluginPolicy: eff.pluginPolicy,
|
|
445
626
|
});
|
|
446
627
|
}
|
|
447
628
|
|
package/package.json
CHANGED
package/src/connect.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { execFile, spawn } from 'node:child_process';
|
|
2
2
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { createConnection } from 'node:net';
|
|
3
4
|
import { homedir } from 'node:os';
|
|
4
5
|
import { dirname, join } from 'node:path';
|
|
5
6
|
import { createInterface } from 'node:readline/promises';
|
|
6
7
|
import { promisify } from 'node:util';
|
|
7
8
|
import { copyToClipboard, maskPairingKey } from './clipboard.js';
|
|
8
|
-
import { formatPairingKey, readRuntimeInfo, resolveDshHome } from './state.js';
|
|
9
|
+
import { formatPairingKey, isPidAlive, readRuntimeInfo, resolveDshHome } from './state.js';
|
|
9
10
|
import { bindTianaRemoteWeb, getTianaStatus, tianaBinary } from './tiana-cli.js';
|
|
10
11
|
|
|
11
12
|
const execFileAsync = promisify(execFile);
|
|
@@ -394,6 +395,33 @@ export async function isLocalDshResponsive(origin, { fetchFn = fetch, timeoutMs
|
|
|
394
395
|
}
|
|
395
396
|
}
|
|
396
397
|
|
|
398
|
+
export function isTcpEndpointReachable(origin, { timeoutMs = 1500 } = {}) {
|
|
399
|
+
if (!origin) return Promise.resolve(false);
|
|
400
|
+
let parsed;
|
|
401
|
+
try {
|
|
402
|
+
parsed = new URL(origin);
|
|
403
|
+
} catch {
|
|
404
|
+
return Promise.resolve(false);
|
|
405
|
+
}
|
|
406
|
+
const host = parsed.hostname.replace(/^\[|\]$/gu, '');
|
|
407
|
+
const port = Number(parsed.port || (parsed.protocol === 'https:' ? 443 : 80));
|
|
408
|
+
if (!host || !Number.isInteger(port) || port <= 0) return Promise.resolve(false);
|
|
409
|
+
return new Promise((resolve) => {
|
|
410
|
+
let done = false;
|
|
411
|
+
const finish = (ok) => {
|
|
412
|
+
if (done) return;
|
|
413
|
+
done = true;
|
|
414
|
+
socket.destroy();
|
|
415
|
+
resolve(ok);
|
|
416
|
+
};
|
|
417
|
+
const socket = createConnection({ host, port });
|
|
418
|
+
socket.setTimeout(timeoutMs);
|
|
419
|
+
socket.once('connect', () => finish(true));
|
|
420
|
+
socket.once('timeout', () => finish(false));
|
|
421
|
+
socket.once('error', () => finish(false));
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
397
425
|
export function buildDshStartArgs({ profile = 'web', port = 3080 } = {}) {
|
|
398
426
|
return ['--profile', String(profile || 'web'), '--port', String(port), '--no-open'];
|
|
399
427
|
}
|
|
@@ -499,12 +527,34 @@ export async function runConnect(options = {}, deps = {}) {
|
|
|
499
527
|
const runtime = deps.readRuntime ? deps.readRuntime() : readRuntimeInfo();
|
|
500
528
|
let runtimeAlive = false;
|
|
501
529
|
if (runtime) {
|
|
530
|
+
const probeOrigins = [...new Set([
|
|
531
|
+
runtime.advertisedOrigin,
|
|
532
|
+
runtime.origin,
|
|
533
|
+
`http://127.0.0.1:${port}`,
|
|
534
|
+
].filter(Boolean))];
|
|
502
535
|
if (deps.checkDshResponsive) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
536
|
+
for (const candidate of probeOrigins) {
|
|
537
|
+
if (await deps.checkDshResponsive(candidate)) {
|
|
538
|
+
runtimeAlive = true;
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
506
542
|
} else {
|
|
507
|
-
|
|
543
|
+
for (const candidate of probeOrigins) {
|
|
544
|
+
if (await isLocalDshResponsive(candidate, { fetchFn })) {
|
|
545
|
+
runtimeAlive = true;
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
// If HTTPS probe failed (e.g. TLS SAN mismatch on raw Tailscale/LAN IP) while runtime.pid
|
|
550
|
+
// is alive and its HTTPS port accepts TCP connections, reuse the live DSH instance instead
|
|
551
|
+
// of spawning a competing duplicate process.
|
|
552
|
+
const checkPidAlive = deps.isPidAlive ?? isPidAlive;
|
|
553
|
+
const checkTcpReachable = deps.checkTcpReachable ?? isTcpEndpointReachable;
|
|
554
|
+
if (!runtimeAlive && checkPidAlive(runtime.pid) &&
|
|
555
|
+
typeof runtime.origin === 'string' && runtime.origin.startsWith('https://')) {
|
|
556
|
+
runtimeAlive = await checkTcpReachable(runtime.origin);
|
|
557
|
+
}
|
|
508
558
|
}
|
|
509
559
|
}
|
|
510
560
|
|
package/src/dsh-packages.js
CHANGED
|
@@ -2,7 +2,82 @@ import { existsSync, readdirSync, realpathSync } from 'node:fs';
|
|
|
2
2
|
import { createRequire } from 'node:module';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
function parseSemverTag(raw) {
|
|
6
|
+
const m = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?/u.exec(String(raw || ''));
|
|
7
|
+
if (!m) return null;
|
|
8
|
+
const pre = m[4]
|
|
9
|
+
? m[4].split('.').map((part) => (/^\d+$/u.test(part) ? Number(part) : part))
|
|
10
|
+
: [];
|
|
11
|
+
return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]), pre };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function comparePnpmPackageEntriesDesc(a, b, pnpmPrefix) {
|
|
15
|
+
const tagA = a.slice(pnpmPrefix.length).split('_', 1)[0];
|
|
16
|
+
const tagB = b.slice(pnpmPrefix.length).split('_', 1)[0];
|
|
17
|
+
const sa = parseSemverTag(tagA);
|
|
18
|
+
const sb = parseSemverTag(tagB);
|
|
19
|
+
if (sa && sb) {
|
|
20
|
+
if (sa.major !== sb.major) return sb.major - sa.major;
|
|
21
|
+
if (sa.minor !== sb.minor) return sb.minor - sa.minor;
|
|
22
|
+
if (sa.patch !== sb.patch) return sb.patch - sa.patch;
|
|
23
|
+
// Stable release (empty pre) ranks higher than prerelease
|
|
24
|
+
if (sa.pre.length === 0 && sb.pre.length > 0) return -1;
|
|
25
|
+
if (sa.pre.length > 0 && sb.pre.length === 0) return 1;
|
|
26
|
+
const len = Math.max(sa.pre.length, sb.pre.length);
|
|
27
|
+
for (let i = 0; i < len; i += 1) {
|
|
28
|
+
const pa = sa.pre[i];
|
|
29
|
+
const pb = sb.pre[i];
|
|
30
|
+
if (pa === undefined) return 1;
|
|
31
|
+
if (pb === undefined) return -1;
|
|
32
|
+
if (pa === pb) continue;
|
|
33
|
+
if (typeof pa === 'number' && typeof pb === 'number') return pb - pa;
|
|
34
|
+
if (typeof pa === 'number') return 1;
|
|
35
|
+
if (typeof pb === 'number') return -1;
|
|
36
|
+
return pb.localeCompare(pa);
|
|
37
|
+
}
|
|
38
|
+
} else if (sa) {
|
|
39
|
+
return -1;
|
|
40
|
+
} else if (sb) {
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
return b.localeCompare(a);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveViaAnchor(anchorPath, packageName, unique) {
|
|
47
|
+
const found = [];
|
|
48
|
+
if (!anchorPath || !existsSync(anchorPath)) return found;
|
|
49
|
+
let anchorReal = anchorPath;
|
|
50
|
+
try {
|
|
51
|
+
anchorReal = realpathSync(anchorPath);
|
|
52
|
+
} catch {
|
|
53
|
+
return found;
|
|
54
|
+
}
|
|
55
|
+
let req;
|
|
56
|
+
try {
|
|
57
|
+
req = createRequire(anchorReal);
|
|
58
|
+
} catch {
|
|
59
|
+
return found;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const direct = unique(dirname(req.resolve(packageName + '/package.json')));
|
|
63
|
+
if (direct) found.push(direct);
|
|
64
|
+
} catch {
|
|
65
|
+
// Try resolving through @deepseek-ai/dsh-web-app when pnpm strict layout nests peer deps.
|
|
66
|
+
}
|
|
67
|
+
if (packageName !== '@deepseek-ai/dsh-web-app') {
|
|
68
|
+
try {
|
|
69
|
+
const webAppPkg = req.resolve('@deepseek-ai/dsh-web-app/package.json');
|
|
70
|
+
const webAppReq = createRequire(webAppPkg);
|
|
71
|
+
const nested = unique(dirname(webAppReq.resolve(packageName + '/package.json')));
|
|
72
|
+
if (nested) found.push(nested);
|
|
73
|
+
} catch {
|
|
74
|
+
// ignore
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function* resolveDshPackageDirs(packageName, options = {}) {
|
|
6
81
|
const seen = new Set();
|
|
7
82
|
function unique(path) {
|
|
8
83
|
if (!existsSync(join(path, 'package.json'))) return undefined;
|
|
@@ -12,6 +87,18 @@ export function* resolveDshPackageDirs(packageName) {
|
|
|
12
87
|
return real;
|
|
13
88
|
}
|
|
14
89
|
|
|
90
|
+
const home = options.home ?? (process.env.HOME || '');
|
|
91
|
+
const activeEntry = options.activeEntry ?? process.argv[1];
|
|
92
|
+
const anchors = [
|
|
93
|
+
activeEntry,
|
|
94
|
+
join(home, '.local/share/pnpm/global/5/node_modules/@deepseek-ai/dsh/package.json'),
|
|
95
|
+
];
|
|
96
|
+
for (const anchor of anchors) {
|
|
97
|
+
for (const dir of resolveViaAnchor(anchor, packageName, unique)) {
|
|
98
|
+
yield dir;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
15
102
|
const require = createRequire(import.meta.url);
|
|
16
103
|
try {
|
|
17
104
|
const path = unique(dirname(require.resolve(packageName + '/package.json')));
|
|
@@ -20,8 +107,7 @@ export function* resolveDshPackageDirs(packageName) {
|
|
|
20
107
|
// The installed DSH profile normally owns these packages.
|
|
21
108
|
}
|
|
22
109
|
|
|
23
|
-
const
|
|
24
|
-
const stores = [
|
|
110
|
+
const stores = options.stores ?? [
|
|
25
111
|
process.env.DSH_PNPM_STORE,
|
|
26
112
|
join(process.env.DSH_HOME || join(home, '.dsh'), 'profiles/web/node_modules'),
|
|
27
113
|
join(home, '.local/share/pnpm/global/5/.pnpm'),
|
|
@@ -31,16 +117,19 @@ export function* resolveDshPackageDirs(packageName) {
|
|
|
31
117
|
if (!base || !existsSync(base)) continue;
|
|
32
118
|
const direct = unique(join(base, packageName));
|
|
33
119
|
if (direct) yield direct;
|
|
34
|
-
|
|
35
|
-
|
|
120
|
+
const matched = readdirSync(base)
|
|
121
|
+
.filter((entry) => entry.startsWith(pnpmPrefix))
|
|
122
|
+
.sort((a, b) => comparePnpmPackageEntriesDesc(a, b, pnpmPrefix));
|
|
123
|
+
for (const entry of matched) {
|
|
36
124
|
const path = unique(join(base, entry, 'node_modules', packageName));
|
|
37
125
|
if (path) yield path;
|
|
38
126
|
}
|
|
39
127
|
}
|
|
40
128
|
}
|
|
41
129
|
|
|
42
|
-
export function resolveDshPackageDir(packageName) {
|
|
43
|
-
const first = resolveDshPackageDirs(packageName).next();
|
|
130
|
+
export function resolveDshPackageDir(packageName, options = {}) {
|
|
131
|
+
const first = resolveDshPackageDirs(packageName, options).next();
|
|
44
132
|
if (first.done) throw new Error('Unable to locate ' + packageName + '. Ensure DSH is installed.');
|
|
45
133
|
return first.value;
|
|
46
134
|
}
|
|
135
|
+
|