dsh-convenient-window 0.2.0-beta.3 → 0.2.0
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 +117 -26
- package/host/assets.js +33 -33
- package/host/helper.js +214 -214
- package/host/native-service.js +156 -156
- package/host/panel-controller.js +493 -493
- package/host/settings.js +98 -98
- package/lib/client.js +1 -1
- package/lib/cw-dsh-native.exe +0 -0
- package/lib/model.mjs +651 -540
- package/lib/native-identity.json +1 -1
- package/lib/panel.html +14 -14
- package/lib/panel.js +51 -51
- package/package.json +79 -79
- package/payload/magic-corners-helper.exe +0 -0
- package/runtime-assets.json +29 -29
package/host/native-service.js
CHANGED
|
@@ -1,156 +1,156 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
|
-
import { createInterface } from 'node:readline';
|
|
3
|
-
import { readFile, lstat } from 'node:fs/promises';
|
|
4
|
-
import { createHash, randomUUID } from 'node:crypto';
|
|
5
|
-
import { join, dirname, basename } from 'node:path';
|
|
6
|
-
import { root } from './assets.js';
|
|
7
|
-
|
|
8
|
-
const lib = join(root, 'lib');
|
|
9
|
-
const file = 'cw-dsh-native.exe';
|
|
10
|
-
export async function verifyNative(dir = lib) {
|
|
11
|
-
try {
|
|
12
|
-
const identity = JSON.parse(
|
|
13
|
-
await readFile(join(dir, 'native-identity.json'), 'utf8'),
|
|
14
|
-
);
|
|
15
|
-
const path = join(dir, file);
|
|
16
|
-
const info = await lstat(path);
|
|
17
|
-
if (
|
|
18
|
-
identity.name === file &&
|
|
19
|
-
info.isFile() &&
|
|
20
|
-
info.size === identity.bytes &&
|
|
21
|
-
createHash('sha256')
|
|
22
|
-
.update(await readFile(path))
|
|
23
|
-
.digest('hex') === identity.sha256
|
|
24
|
-
)
|
|
25
|
-
return identity;
|
|
26
|
-
} catch {
|
|
27
|
-
/* Missing or truncated bytes are treated alike. */
|
|
28
|
-
}
|
|
29
|
-
throw Error('payload-invalid');
|
|
30
|
-
}
|
|
31
|
-
function dshExecutable(path) {
|
|
32
|
-
if (basename(path).toLowerCase() === 'deepseek harness.exe') return path;
|
|
33
|
-
// The backend may use DSH's embedded Node process. Native code checks this sibling before registration.
|
|
34
|
-
for (let dir = dirname(path); dir !== dirname(dir); dir = dirname(dir)) {
|
|
35
|
-
if (basename(dir).toLowerCase() === 'resources')
|
|
36
|
-
return join(dirname(dir), 'DeepSeek Harness.exe');
|
|
37
|
-
}
|
|
38
|
-
return path;
|
|
39
|
-
}
|
|
40
|
-
function errorCode(raw) {
|
|
41
|
-
if (typeof raw !== 'string') return 'native-failed';
|
|
42
|
-
if (
|
|
43
|
-
/^(?:admin[A-Z]\w+|invalid-state|entry-changed|pick-again|cancelled|missing-dsh|missing-profile|native-busy|invalid-command)$/.test(
|
|
44
|
-
raw,
|
|
45
|
-
)
|
|
46
|
-
)
|
|
47
|
-
return raw;
|
|
48
|
-
return /HELPER_INSTANCE_CONFLICT|实例占用/.test(raw)
|
|
49
|
-
? 'helper-in-use'
|
|
50
|
-
: 'native-failed';
|
|
51
|
-
}
|
|
52
|
-
export class NativeService {
|
|
53
|
-
constructor(dir) {
|
|
54
|
-
this.dir = dir;
|
|
55
|
-
this.lang = 'zh-CN';
|
|
56
|
-
this.closed = false;
|
|
57
|
-
}
|
|
58
|
-
async open() {
|
|
59
|
-
if (this.closed) throw Error('unloaded');
|
|
60
|
-
if (this.session) return this.session;
|
|
61
|
-
await verifyNative();
|
|
62
|
-
if (this.closed) throw Error('unloaded');
|
|
63
|
-
if (this.session) return this.session;
|
|
64
|
-
const env = {
|
|
65
|
-
...process.env,
|
|
66
|
-
CW_DSH_DATA: this.dir,
|
|
67
|
-
CW_DSH_EXECUTABLE: dshExecutable(process.execPath),
|
|
68
|
-
CW_DSH_LANG: this.lang,
|
|
69
|
-
};
|
|
70
|
-
delete env.ELECTRON_RUN_AS_NODE;
|
|
71
|
-
const child = spawn(join(lib, file), ['--dsh-host'], {
|
|
72
|
-
cwd: root,
|
|
73
|
-
windowsHide: true,
|
|
74
|
-
stdio: ['pipe', 'pipe', 'ignore'],
|
|
75
|
-
env,
|
|
76
|
-
});
|
|
77
|
-
const session = { child, pending: new Map() };
|
|
78
|
-
this.session = session;
|
|
79
|
-
session.done = new Promise((resolve) => {
|
|
80
|
-
const finish = () => {
|
|
81
|
-
for (const p of session.pending.values())
|
|
82
|
-
p.reject(
|
|
83
|
-
Error(
|
|
84
|
-
child.exitCode === 78
|
|
85
|
-
? 'ordinary-host-required'
|
|
86
|
-
: 'native-disconnected',
|
|
87
|
-
),
|
|
88
|
-
);
|
|
89
|
-
session.pending.clear();
|
|
90
|
-
if (this.session === session) {
|
|
91
|
-
this.session = null;
|
|
92
|
-
this.onExit?.(child.exitCode);
|
|
93
|
-
}
|
|
94
|
-
resolve();
|
|
95
|
-
};
|
|
96
|
-
child.once('close', finish);
|
|
97
|
-
child.on('error', () => {}); // A spawn error also closes all three streams.
|
|
98
|
-
});
|
|
99
|
-
child.stdin.on('error', () => {});
|
|
100
|
-
createInterface({ input: child.stdout, crlfDelay: Infinity }).on(
|
|
101
|
-
'line',
|
|
102
|
-
(line) => {
|
|
103
|
-
let m;
|
|
104
|
-
try {
|
|
105
|
-
m = JSON.parse(line);
|
|
106
|
-
} catch {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
const p = session.pending.get(m.id);
|
|
110
|
-
if (!p) return;
|
|
111
|
-
session.pending.delete(m.id);
|
|
112
|
-
if (m.ok) p.resolve(m.data);
|
|
113
|
-
else p.reject(Error(errorCode(m.error)));
|
|
114
|
-
},
|
|
115
|
-
);
|
|
116
|
-
return session;
|
|
117
|
-
}
|
|
118
|
-
get child() {
|
|
119
|
-
return this.session?.child;
|
|
120
|
-
}
|
|
121
|
-
async call(op, data = null) {
|
|
122
|
-
return this.send(await this.open(), op, data);
|
|
123
|
-
}
|
|
124
|
-
send(session, op, data = null) {
|
|
125
|
-
if (this.closed || !session || session !== this.session)
|
|
126
|
-
return Promise.reject(Error('native-disconnected'));
|
|
127
|
-
if (session.pending.size >= 12) return Promise.reject(Error('native-busy'));
|
|
128
|
-
const id = randomUUID();
|
|
129
|
-
return new Promise((resolve, reject) => {
|
|
130
|
-
session.pending.set(id, { resolve, reject });
|
|
131
|
-
session.child.stdin.write(
|
|
132
|
-
JSON.stringify({ id, op, data }) + '\n',
|
|
133
|
-
(e) => {
|
|
134
|
-
if (e && session.pending.delete(id))
|
|
135
|
-
reject(Error('native-disconnected'));
|
|
136
|
-
},
|
|
137
|
-
);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
async close() {
|
|
141
|
-
this.closed = true;
|
|
142
|
-
const s = this.session;
|
|
143
|
-
if (!s) return;
|
|
144
|
-
for (const p of s.pending.values()) p.reject(Error('unloaded'));
|
|
145
|
-
s.pending.clear();
|
|
146
|
-
s.child.stdin.end();
|
|
147
|
-
const timer = setTimeout(() => {
|
|
148
|
-
if (this.session === s) s.child.kill();
|
|
149
|
-
}, 6000);
|
|
150
|
-
try {
|
|
151
|
-
await s.done;
|
|
152
|
-
} finally {
|
|
153
|
-
clearTimeout(timer);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { readFile, lstat } from 'node:fs/promises';
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
5
|
+
import { join, dirname, basename } from 'node:path';
|
|
6
|
+
import { root } from './assets.js';
|
|
7
|
+
|
|
8
|
+
const lib = join(root, 'lib');
|
|
9
|
+
const file = 'cw-dsh-native.exe';
|
|
10
|
+
export async function verifyNative(dir = lib) {
|
|
11
|
+
try {
|
|
12
|
+
const identity = JSON.parse(
|
|
13
|
+
await readFile(join(dir, 'native-identity.json'), 'utf8'),
|
|
14
|
+
);
|
|
15
|
+
const path = join(dir, file);
|
|
16
|
+
const info = await lstat(path);
|
|
17
|
+
if (
|
|
18
|
+
identity.name === file &&
|
|
19
|
+
info.isFile() &&
|
|
20
|
+
info.size === identity.bytes &&
|
|
21
|
+
createHash('sha256')
|
|
22
|
+
.update(await readFile(path))
|
|
23
|
+
.digest('hex') === identity.sha256
|
|
24
|
+
)
|
|
25
|
+
return identity;
|
|
26
|
+
} catch {
|
|
27
|
+
/* Missing or truncated bytes are treated alike. */
|
|
28
|
+
}
|
|
29
|
+
throw Error('payload-invalid');
|
|
30
|
+
}
|
|
31
|
+
function dshExecutable(path) {
|
|
32
|
+
if (basename(path).toLowerCase() === 'deepseek harness.exe') return path;
|
|
33
|
+
// The backend may use DSH's embedded Node process. Native code checks this sibling before registration.
|
|
34
|
+
for (let dir = dirname(path); dir !== dirname(dir); dir = dirname(dir)) {
|
|
35
|
+
if (basename(dir).toLowerCase() === 'resources')
|
|
36
|
+
return join(dirname(dir), 'DeepSeek Harness.exe');
|
|
37
|
+
}
|
|
38
|
+
return path;
|
|
39
|
+
}
|
|
40
|
+
function errorCode(raw) {
|
|
41
|
+
if (typeof raw !== 'string') return 'native-failed';
|
|
42
|
+
if (
|
|
43
|
+
/^(?:admin[A-Z]\w+|invalid-state|entry-changed|pick-again|cancelled|missing-dsh|missing-profile|native-busy|invalid-command)$/.test(
|
|
44
|
+
raw,
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
return raw;
|
|
48
|
+
return /HELPER_INSTANCE_CONFLICT|实例占用/.test(raw)
|
|
49
|
+
? 'helper-in-use'
|
|
50
|
+
: 'native-failed';
|
|
51
|
+
}
|
|
52
|
+
export class NativeService {
|
|
53
|
+
constructor(dir) {
|
|
54
|
+
this.dir = dir;
|
|
55
|
+
this.lang = 'zh-CN';
|
|
56
|
+
this.closed = false;
|
|
57
|
+
}
|
|
58
|
+
async open() {
|
|
59
|
+
if (this.closed) throw Error('unloaded');
|
|
60
|
+
if (this.session) return this.session;
|
|
61
|
+
await verifyNative();
|
|
62
|
+
if (this.closed) throw Error('unloaded');
|
|
63
|
+
if (this.session) return this.session;
|
|
64
|
+
const env = {
|
|
65
|
+
...process.env,
|
|
66
|
+
CW_DSH_DATA: this.dir,
|
|
67
|
+
CW_DSH_EXECUTABLE: dshExecutable(process.execPath),
|
|
68
|
+
CW_DSH_LANG: this.lang,
|
|
69
|
+
};
|
|
70
|
+
delete env.ELECTRON_RUN_AS_NODE;
|
|
71
|
+
const child = spawn(join(lib, file), ['--dsh-host'], {
|
|
72
|
+
cwd: root,
|
|
73
|
+
windowsHide: true,
|
|
74
|
+
stdio: ['pipe', 'pipe', 'ignore'],
|
|
75
|
+
env,
|
|
76
|
+
});
|
|
77
|
+
const session = { child, pending: new Map() };
|
|
78
|
+
this.session = session;
|
|
79
|
+
session.done = new Promise((resolve) => {
|
|
80
|
+
const finish = () => {
|
|
81
|
+
for (const p of session.pending.values())
|
|
82
|
+
p.reject(
|
|
83
|
+
Error(
|
|
84
|
+
child.exitCode === 78
|
|
85
|
+
? 'ordinary-host-required'
|
|
86
|
+
: 'native-disconnected',
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
session.pending.clear();
|
|
90
|
+
if (this.session === session) {
|
|
91
|
+
this.session = null;
|
|
92
|
+
this.onExit?.(child.exitCode);
|
|
93
|
+
}
|
|
94
|
+
resolve();
|
|
95
|
+
};
|
|
96
|
+
child.once('close', finish);
|
|
97
|
+
child.on('error', () => {}); // A spawn error also closes all three streams.
|
|
98
|
+
});
|
|
99
|
+
child.stdin.on('error', () => {});
|
|
100
|
+
createInterface({ input: child.stdout, crlfDelay: Infinity }).on(
|
|
101
|
+
'line',
|
|
102
|
+
(line) => {
|
|
103
|
+
let m;
|
|
104
|
+
try {
|
|
105
|
+
m = JSON.parse(line);
|
|
106
|
+
} catch {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const p = session.pending.get(m.id);
|
|
110
|
+
if (!p) return;
|
|
111
|
+
session.pending.delete(m.id);
|
|
112
|
+
if (m.ok) p.resolve(m.data);
|
|
113
|
+
else p.reject(Error(errorCode(m.error)));
|
|
114
|
+
},
|
|
115
|
+
);
|
|
116
|
+
return session;
|
|
117
|
+
}
|
|
118
|
+
get child() {
|
|
119
|
+
return this.session?.child;
|
|
120
|
+
}
|
|
121
|
+
async call(op, data = null) {
|
|
122
|
+
return this.send(await this.open(), op, data);
|
|
123
|
+
}
|
|
124
|
+
send(session, op, data = null) {
|
|
125
|
+
if (this.closed || !session || session !== this.session)
|
|
126
|
+
return Promise.reject(Error('native-disconnected'));
|
|
127
|
+
if (session.pending.size >= 12) return Promise.reject(Error('native-busy'));
|
|
128
|
+
const id = randomUUID();
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
session.pending.set(id, { resolve, reject });
|
|
131
|
+
session.child.stdin.write(
|
|
132
|
+
JSON.stringify({ id, op, data }) + '\n',
|
|
133
|
+
(e) => {
|
|
134
|
+
if (e && session.pending.delete(id))
|
|
135
|
+
reject(Error('native-disconnected'));
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
async close() {
|
|
141
|
+
this.closed = true;
|
|
142
|
+
const s = this.session;
|
|
143
|
+
if (!s) return;
|
|
144
|
+
for (const p of s.pending.values()) p.reject(Error('unloaded'));
|
|
145
|
+
s.pending.clear();
|
|
146
|
+
s.child.stdin.end();
|
|
147
|
+
const timer = setTimeout(() => {
|
|
148
|
+
if (this.session === s) s.child.kill();
|
|
149
|
+
}, 6000);
|
|
150
|
+
try {
|
|
151
|
+
await s.done;
|
|
152
|
+
} finally {
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|