@tianacloud/dsh-remote 0.0.0-stage → 0.2.2

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.
@@ -0,0 +1,46 @@
1
+ import { cpSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
2
+ import { join, resolve } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const projectRoot = fileURLToPath(new URL('..', import.meta.url));
6
+ const webSource = join(projectRoot, 'web-src');
7
+ const MAX_BYTES = 200 * 1024;
8
+ const REQUIRED_MODULES = ['dsh-remote-entry.mjs', 'dsh-remote-sw.js', 'shell-ui.js'];
9
+
10
+ export function buildWebDist({ outDir, config = {}, clean = true } = {}) {
11
+ if (!outDir) throw new Error('buildWebDist requires outDir');
12
+ const output = resolve(outDir);
13
+ if (clean) rmSync(output, { recursive: true, force: true });
14
+ mkdirSync(output, { recursive: true });
15
+
16
+ const modules = readdirSync(webSource, { withFileTypes: true })
17
+ .filter((entry) => entry.isFile() && /\.(mjs|js)$/u.test(entry.name))
18
+ .map((entry) => entry.name)
19
+ .sort();
20
+ for (const required of REQUIRED_MODULES) {
21
+ if (!modules.includes(required)) {
22
+ throw new Error(`Missing required web-src module: ${required}`);
23
+ }
24
+ }
25
+ for (const file of modules) cpSync(join(webSource, file), join(output, file));
26
+ cpSync(join(projectRoot, 'icon.svg'), join(output, 'icon.svg'));
27
+ const finalConfig = {
28
+ functionId: config.functionId || '',
29
+ webId: config.webId || '',
30
+ defaultHostId: config.defaultHostId || '',
31
+ appTitle: config.appTitle || 'DSH Remote',
32
+ localRelayUrl: config.localRelayUrl || '',
33
+ };
34
+ writeFileSync(join(output, 'remote-config.json'), JSON.stringify(finalConfig) + '\n');
35
+ const files = [...modules, 'icon.svg', 'remote-config.json'];
36
+ const totalBytes = files.reduce((sum, file) => sum + statSync(join(output, file)).size, 0);
37
+ if (totalBytes >= MAX_BYTES) {
38
+ throw new Error(`The DSH Remote shell must be smaller than 200 KiB (got ${totalBytes} bytes)`);
39
+ }
40
+ return { outDir: output, fileCount: files.length, totalBytes, files, config: finalConfig };
41
+ }
42
+
43
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
44
+ const result = buildWebDist({ outDir: process.argv[2] || join(projectRoot, '.dist-tweb', 'preview') });
45
+ console.log(`[dsh-remote] Built web shell at ${result.outDir}: ${result.fileCount} files, ${result.totalBytes} bytes (${(result.totalBytes / 1024).toFixed(1)} KiB)`);
46
+ }
@@ -0,0 +1,94 @@
1
+ import { spawn } from 'node:child_process';
2
+
3
+ /**
4
+ * Resolve ordered candidate clipboard commands for the given platform & environment.
5
+ * Always writes via stdin pipe — never passes secrets as command-line arguments.
6
+ */
7
+ export function clipboardCandidates(platform = process.platform, env = process.env) {
8
+ if (platform === 'darwin') {
9
+ return [{ bin: 'pbcopy', args: [] }];
10
+ }
11
+ if (platform === 'win32') {
12
+ return [{ bin: 'clip.exe', args: [] }];
13
+ }
14
+ const list = [];
15
+ if (env.WAYLAND_DISPLAY) {
16
+ list.push({ bin: 'wl-copy', args: [] });
17
+ }
18
+ if (env.DISPLAY || !env.WAYLAND_DISPLAY) {
19
+ list.push({ bin: 'xclip', args: ['-selection', 'clipboard'] });
20
+ list.push({ bin: 'xsel', args: ['--clipboard', '--input'] });
21
+ }
22
+ if (!env.WAYLAND_DISPLAY) {
23
+ list.push({ bin: 'wl-copy', args: [] });
24
+ }
25
+ return list;
26
+ }
27
+
28
+ function defaultSpawnWrite(bin, args, text, timeoutMs) {
29
+ return new Promise((resolve, reject) => {
30
+ const child = spawn(bin, args, {
31
+ stdio: ['pipe', 'ignore', 'pipe'],
32
+ windowsHide: true,
33
+ });
34
+ let stderr = '';
35
+ let settled = false;
36
+ const timer = setTimeout(() => {
37
+ if (settled) return;
38
+ settled = true;
39
+ try { child.kill('SIGKILL'); } catch { /* ignore */ }
40
+ reject(new Error(`${bin} timed out`));
41
+ }, timeoutMs);
42
+
43
+ child.stderr?.on('data', (chunk) => { stderr += chunk.toString(); });
44
+ child.on('error', (err) => {
45
+ if (settled) return;
46
+ settled = true;
47
+ clearTimeout(timer);
48
+ reject(err);
49
+ });
50
+ child.on('close', (code) => {
51
+ if (settled) return;
52
+ settled = true;
53
+ clearTimeout(timer);
54
+ if (code === 0) resolve();
55
+ else reject(new Error(`${bin} exited with code ${code}: ${stderr.trim()}`));
56
+ });
57
+ child.stdin.on('error', () => {});
58
+ child.stdin.end(text, 'utf8');
59
+ });
60
+ }
61
+
62
+ /**
63
+ * Copy sensitive text (such as pairing key) to system clipboard via stdin pipe.
64
+ */
65
+ export async function copyToClipboard(text, options = {}) {
66
+ const platform = options.platform ?? process.platform;
67
+ const env = options.env ?? process.env;
68
+ const timeoutMs = options.timeoutMs ?? 3000;
69
+ const writeFn = options.writeFn ?? defaultSpawnWrite;
70
+ const candidates = clipboardCandidates(platform, env);
71
+
72
+ let lastError = 'No clipboard utility available';
73
+ for (const { bin, args } of candidates) {
74
+ try {
75
+ await writeFn(bin, args, String(text), timeoutMs);
76
+ return { ok: true, tool: bin };
77
+ } catch (err) {
78
+ lastError = err instanceof Error ? err.message : String(err);
79
+ }
80
+ }
81
+ return { ok: false, error: lastError };
82
+ }
83
+
84
+ export function maskPairingKey(key) {
85
+ const str = String(key || '').trim();
86
+ if (!str) return '••••••••••••';
87
+ if (str.startsWith('dshk1_') && str.length > 12) {
88
+ return `dshk1_••••••••••••${str.slice(-4)}`;
89
+ }
90
+ if (str.length > 8) {
91
+ return `••••••••••••${str.slice(-4)}`;
92
+ }
93
+ return '••••••••••••';
94
+ }