@finogeeks2026/finshield-opencode 0.0.0-stage → 0.11.50
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/DESIGN.md +593 -0
- package/LICENSE.rtf +28 -0
- package/README.md +132 -2
- package/THIRD-PARTY-NOTICES.md +3131 -0
- package/bin/node.exe +4 -0
- package/dist/gui-experimental.cjs +96 -0
- package/dist/updater/server.js +1046 -0
- package/install/download-source.json +3 -0
- package/install/runtime.js +60 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/LICENSE.rtf +28 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/README.md +116 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/bin/finsafe_native.dll +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/native-build.json +5 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/README.md +6 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/index.js +12 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/package.json +24 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.lib +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.node +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/CHANGELOG.md +1183 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/LICENSE.txt +22 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/README.md +42 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/cnoke.cjs +816 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/benchmarks.md +80 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/callbacks.md +182 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/composites.md +549 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/contribute.md +163 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/index.md +46 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/load.md +191 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/migration.md +256 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/misc.md +76 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/output.md +299 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/pointers.md +385 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/primitives.md +151 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/start.md +110 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/types.md +88 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/unions.md +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/values.md +134 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.cjs +1 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.d.ts +389 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.js +2 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.cjs +1 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.js +2 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.cc +11064 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.hh +5951 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/unicode.inc +408 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/package.json +68 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/CMakeLists.txt +249 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.cjs +296 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.js +281 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.cjs +295 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.js +280 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64.cc +1408 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.S +216 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.asm +181 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64.cc +5 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64_asm.S +200 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64.cc +1345 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64_asm.S +200 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv.cc +1434 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv_asm.S +224 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win.cc +1086 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.S +162 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.asm +189 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86.cc +1185 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.S +217 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.asm +174 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.cc +2094 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.hh +179 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/errno.inc +462 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.cc +3009 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.hh +461 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.cc +218 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.hh +54 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/primitives.inc +36 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.cjs +130 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.js +133 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/trampolines.cjs +77 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.cc +725 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.hh +71 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.cc +1040 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.hh +493 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.cc +214 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.def +11 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.hh +112 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.cc +198 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.hh +148 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/LICENSE.md +9 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/README.md +95 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.deprecated.h +186 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.h +7211 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi.h +3421 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/LICENSE +21 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/README.md +95 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/js_native_api.def +125 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/node_api.def +157 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api.h +591 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api_types.h +210 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api.h +265 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api_types.h +58 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/package.json +36 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/release.json +4 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/src/native.js +85 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/src/shell.js +20 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/src/status.js +57 -0
- package/package.json +47 -4
- package/policy/candidate.yaml +347 -0
- package/release.json +4 -0
- package/src/activation.js +2 -0
- package/src/attachment.js +34 -0
- package/src/connector.js +91 -0
- package/src/contract.js +8 -0
- package/src/desktop-contract.js +4 -0
- package/src/desktop.js +25 -0
- package/src/gui.tsx +58 -0
- package/src/host.js +38 -0
- package/src/index.js +12 -0
- package/src/instance-settings.js +27 -0
- package/src/known-folders.js +22 -0
- package/src/management.js +17 -0
- package/src/preparation.js +18 -0
- package/src/presentation.js +153 -0
- package/src/settings.js +18 -0
- package/ui/feedback.cjs +45 -0
- package/ui/page.cjs +225 -0
- package/ui/state.cjs +44 -0
- package/updater/bootstrap.js +41 -0
- package/updater/common/activation.js +81 -0
- package/updater/common/coordinator.js +329 -0
- package/updater/common/download.js +57 -0
- package/updater/common/index.js +12 -0
- package/updater/common/package.js +53 -0
- package/updater/common/protocol.js +38 -0
- package/updater/common/transport.js +65 -0
- package/updater/coordinator.js +2 -0
- package/updater/launch.js +40 -0
- package/updater/management.js +9 -0
- package/updater/native.js +18 -0
- package/updater/operations.js +45 -0
- package/updater/protocol.js +1 -0
- package/updater/server.js +100 -0
- package/updater/transport.js +10 -0
- package/updater/windows-launch-reader.js +40 -0
- package/updater/windows-launch.js +96 -0
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
import {createRequire as __createRequire} from 'node:module'; const require=__createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// updater/common/coordinator.js
|
|
4
|
+
function decideStartup({ hostInSelectedAppContainer, authorizationComplete }) {
|
|
5
|
+
return !hostInSelectedAppContainer && authorizationComplete ? "wrap" : "keep";
|
|
6
|
+
}
|
|
7
|
+
function createUpdater({ ports: ports2, startup: startup2, application = "application", prepareUpdates = false }) {
|
|
8
|
+
const clients2 = /* @__PURE__ */ new Map(), sessions = /* @__PURE__ */ new Map(), tasks = /* @__PURE__ */ new Map(), operations2 = /* @__PURE__ */ new Set(), requests = /* @__PURE__ */ new Set();
|
|
9
|
+
let sequence = 0, draining2 = false, startupReleased = false, finish;
|
|
10
|
+
const closed = new Promise((resolve2) => {
|
|
11
|
+
finish = resolve2;
|
|
12
|
+
});
|
|
13
|
+
const resource = (b) => JSON.stringify([b.installationResource ?? b.installation, b.configurationResource ?? b.configuration, b.profile]);
|
|
14
|
+
const taskKey = (b) => JSON.stringify([resource(b), b.requestedRelease]);
|
|
15
|
+
let shell;
|
|
16
|
+
async function obtainTargetShell(binding) {
|
|
17
|
+
const target = resource(binding);
|
|
18
|
+
if (shell?.target === target) return shell.connection;
|
|
19
|
+
const connection = await ports2.backend?.(binding);
|
|
20
|
+
if (connection) shell = { target, connection };
|
|
21
|
+
return connection;
|
|
22
|
+
}
|
|
23
|
+
const failure = (error) => error.result ?? { phase: error.authority === "uac_cancelled" ? "cancelled" : "failed", error: error.message };
|
|
24
|
+
const active = (s) => s?.authorization_in_progress === true;
|
|
25
|
+
const attemptFor = (value) => {
|
|
26
|
+
const attempt = value.session.attempt;
|
|
27
|
+
return attempt && (attempt.transition || attempt.binding.pid === value.binding.pid) ? attempt : void 0;
|
|
28
|
+
};
|
|
29
|
+
function tracked(binding, operation) {
|
|
30
|
+
const work = { binding };
|
|
31
|
+
operations2.add(work);
|
|
32
|
+
work.promise = Promise.resolve().then(operation).finally(() => operations2.delete(work));
|
|
33
|
+
work.promise.catch(() => {
|
|
34
|
+
});
|
|
35
|
+
return work.promise;
|
|
36
|
+
}
|
|
37
|
+
function status(value, authorization) {
|
|
38
|
+
const session = value.session, task = session.task ?? value.task, attempt = attemptFor(value);
|
|
39
|
+
const observed = session.wrapping ? { phase: "restarting" } : attempt?.state ?? authorization ?? task?.state;
|
|
40
|
+
const matchingProtected = value.binding.protected && !task?.update && (task?.target ? resource(value.binding) === resource(task.target) : authorization !== void 0);
|
|
41
|
+
const state = observed?.phase === "ready" && matchingProtected ? { phase: "protected" } : observed?.phase === "protected" && !value.binding.protected ? { phase: "starting" } : observed;
|
|
42
|
+
const result = value.result ?? state ?? { phase: session.startupError ? "failed" : "starting" };
|
|
43
|
+
const error = [result.error, session.startupError, task?.callError].filter(Boolean).join("; ");
|
|
44
|
+
const reminderId = authorization ? authorization.attempt || resource(task?.target ?? value.binding) : task?.id;
|
|
45
|
+
return {
|
|
46
|
+
...result,
|
|
47
|
+
...error ? { error } : {},
|
|
48
|
+
...authorization && task?.reason ? { reason: task.reason } : {},
|
|
49
|
+
currentProtected: value.binding.protected === true,
|
|
50
|
+
currentRelease: session.currentRelease,
|
|
51
|
+
requestedRelease: task?.binding.requestedRelease ?? value.binding.requestedRelease,
|
|
52
|
+
reminderDismissed: authorization ? session.dismissedReminder === reminderId : task?.dismissed === true,
|
|
53
|
+
reminderId,
|
|
54
|
+
reopenRequired: !draining2 && !session.closed && !attempt && state?.phase === "ready"
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async function observedStatus(value) {
|
|
58
|
+
const result = status(value), task = value.session.task ?? value.task, target = task?.target ?? value.binding;
|
|
59
|
+
if (task && !task.authorization || result.phase !== "starting" && !task?.authorization || value.result || value.session.wrapping || attemptFor(value) || !shell || shell.target !== resource(target)) return result;
|
|
60
|
+
if (task?.observeAfter > Date.now()) return result;
|
|
61
|
+
try {
|
|
62
|
+
const state = await ports2.preparation(target, shell.connection);
|
|
63
|
+
value.authorizationAttempt ??= active(state) ? state.attempt : void 0;
|
|
64
|
+
const observed = value.authorizationAttempt && ports2.progress ? await readTask(target, shell.connection, state, value.authorizationAttempt) : state;
|
|
65
|
+
if (!active(observed)) delete value.authorizationAttempt;
|
|
66
|
+
return status(value, observed);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error.code === "observation_superseded") delete value.authorizationAttempt;
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function sessionFor(binding) {
|
|
73
|
+
let session = sessions.get(binding.mainKey) ?? [...sessions.values()].find((current) => current.wrapping);
|
|
74
|
+
if (!session) {
|
|
75
|
+
session = { mainKey: binding.mainKey, binding, mainBinding: binding, bindings: /* @__PURE__ */ new Set(), closed: false, wrapping: false };
|
|
76
|
+
sessions.set(binding.mainKey, session);
|
|
77
|
+
}
|
|
78
|
+
session.binding = binding;
|
|
79
|
+
if (session.attempt && !session.attempt.transition && session.attempt.state.phase === "completed") session.attempt = void 0;
|
|
80
|
+
return session;
|
|
81
|
+
}
|
|
82
|
+
async function readTask(binding, connection, state, attempt) {
|
|
83
|
+
const progress = await ports2.progress(binding, connection, attempt);
|
|
84
|
+
if (active(progress)) return { ...state, progress };
|
|
85
|
+
const current = await ports2.preparation(binding, connection);
|
|
86
|
+
if (current.attempt !== attempt) throw Object.assign(
|
|
87
|
+
Error("Authorization observation was superseded"),
|
|
88
|
+
{ status: 409, code: "observation_superseded" }
|
|
89
|
+
);
|
|
90
|
+
return current;
|
|
91
|
+
}
|
|
92
|
+
async function wrapRestart(session, target, onStopped) {
|
|
93
|
+
session.wrapping = true;
|
|
94
|
+
try {
|
|
95
|
+
await ports2.wrap(target, (mainKey) => {
|
|
96
|
+
session.mainBinding = target;
|
|
97
|
+
if (mainKey && mainKey !== session.mainKey) {
|
|
98
|
+
sessions.delete(session.mainKey);
|
|
99
|
+
session.mainKey = mainKey;
|
|
100
|
+
sessions.set(mainKey, session);
|
|
101
|
+
for (const value of clients2.values()) if (value.session === session && value.binding.mainKey !== mainKey) value.released = true;
|
|
102
|
+
}
|
|
103
|
+
}, onStopped);
|
|
104
|
+
} finally {
|
|
105
|
+
session.wrapping = false;
|
|
106
|
+
}
|
|
107
|
+
await ports2.watchMain?.(session.mainBinding);
|
|
108
|
+
}
|
|
109
|
+
function executeStartup(binding, target) {
|
|
110
|
+
const session = sessionFor(binding);
|
|
111
|
+
if (session.wrapping || session.attempt?.transition) return { accepted: true };
|
|
112
|
+
const attempt = { binding, state: { phase: "restarting" }, transition: true };
|
|
113
|
+
session.attempt = attempt;
|
|
114
|
+
session.wrapping = true;
|
|
115
|
+
attempt.promise = tracked(binding, async () => {
|
|
116
|
+
try {
|
|
117
|
+
await wrapRestart(session, target, () => {
|
|
118
|
+
});
|
|
119
|
+
attempt.state = { phase: "completed" };
|
|
120
|
+
} catch (error) {
|
|
121
|
+
attempt.state = failure(error);
|
|
122
|
+
ports2.report?.(error, target, "wrap");
|
|
123
|
+
if (!await ports2.mainAlive(startup2 ?? binding)) stop();
|
|
124
|
+
} finally {
|
|
125
|
+
attempt.transition = false;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return { accepted: true };
|
|
129
|
+
}
|
|
130
|
+
function exit2(session) {
|
|
131
|
+
const attempt = { binding: session.binding, state: { phase: "exiting" }, transition: true };
|
|
132
|
+
session.attempt = attempt;
|
|
133
|
+
attempt.promise = tracked(session.binding, async () => {
|
|
134
|
+
try {
|
|
135
|
+
await ports2.exit(session.binding);
|
|
136
|
+
stop();
|
|
137
|
+
} catch (error) {
|
|
138
|
+
attempt.state = failure(error);
|
|
139
|
+
ports2.report?.(error, session.binding, "exit");
|
|
140
|
+
} finally {
|
|
141
|
+
attempt.transition = false;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return { accepted: true };
|
|
145
|
+
}
|
|
146
|
+
async function assess(task, session, value) {
|
|
147
|
+
try {
|
|
148
|
+
const b = task.binding, release = b.requestedRelease;
|
|
149
|
+
if (!/^\d+\.\d+\.\d+$/.test(release)) throw Error("A requested stable native release is required");
|
|
150
|
+
task.target = await ports2.select(b, release);
|
|
151
|
+
const current = await ports2.installation(b);
|
|
152
|
+
session.currentRelease = current?.version;
|
|
153
|
+
const readOnly = b.manageInstallation === false || b.settings?.manageInstallation === false;
|
|
154
|
+
if (readOnly && task.installOnly) throw Error("The selected FinSAFE installation is read-only to this plugin");
|
|
155
|
+
task.update = !readOnly && Boolean(current && current.version !== release);
|
|
156
|
+
const alreadyProtected = b.protected && (readOnly || current?.version === release) && resource(task.target) === resource(b);
|
|
157
|
+
if (alreadyProtected && !task.installOnly) {
|
|
158
|
+
task.state = { phase: "protected" };
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const installed = readOnly ? current : await ports2.installation(task.target);
|
|
162
|
+
let needsSetup = !readOnly && (!installed || installed.version !== release), backendObtained = false, connection;
|
|
163
|
+
if (!needsSetup && !task.installOnly) {
|
|
164
|
+
try {
|
|
165
|
+
connection = await obtainTargetShell(task.target);
|
|
166
|
+
backendObtained = true;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (readOnly || error.code !== "ENOENT" || error.path !== task.target.settings?.executable) throw error;
|
|
169
|
+
needsSetup = true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (needsSetup) {
|
|
173
|
+
task.state = { phase: "downloading" };
|
|
174
|
+
task.download = new AbortController();
|
|
175
|
+
task.staged = await ports2.stage(task.target, release, task.download.signal);
|
|
176
|
+
task.download = void 0;
|
|
177
|
+
if (draining2 || session.closed || !await ports2.mainAlive(b)) return;
|
|
178
|
+
task.state = { phase: "installing" };
|
|
179
|
+
const setup = await ports2.install(task.target, release, task.staged);
|
|
180
|
+
if (setup.phase !== "completed") {
|
|
181
|
+
task.state = setup;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
Object.assign(task.target, await ports2.select(b, release));
|
|
185
|
+
await ports2.selected?.(b, task.target);
|
|
186
|
+
}
|
|
187
|
+
if (draining2 || session.closed || !await ports2.mainAlive(b)) return;
|
|
188
|
+
if (task.installOnly && !prepareUpdates) {
|
|
189
|
+
if (!needsSetup) await ports2.selected?.(b, task.target);
|
|
190
|
+
task.state = { phase: "ready", reason: "update", authorizationPending: true };
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!backendObtained) connection = await obtainTargetShell(task.target);
|
|
194
|
+
task.reason = task.update ? "update" : needsSetup ? "install" : "authorization";
|
|
195
|
+
task.state = { phase: "authorizing", reason: task.reason };
|
|
196
|
+
const submission = readOnly ? void 0 : await ports2.prepare(task.target, connection);
|
|
197
|
+
task.authorization = true;
|
|
198
|
+
if (!readOnly) task.observeAfter = Date.now() + 3e3;
|
|
199
|
+
if (!readOnly) {
|
|
200
|
+
const completion = submission?.completion ? await submission.completion : submission;
|
|
201
|
+
if (Number.isInteger(completion?.exit_code) && completion.exit_code !== 0)
|
|
202
|
+
throw Error(completion.error || `Desktop authorization command exited (${completion.exit_code})`);
|
|
203
|
+
}
|
|
204
|
+
} catch (error) {
|
|
205
|
+
if (task.authorization) task.callError = error.message;
|
|
206
|
+
else task.state = failure(error);
|
|
207
|
+
ports2.report?.(error, task.target ?? session.binding, "prepare");
|
|
208
|
+
} finally {
|
|
209
|
+
task.done = true;
|
|
210
|
+
tasks.delete(taskKey(task.binding));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function stop() {
|
|
214
|
+
draining2 = true;
|
|
215
|
+
for (const session of new Set(sessions.values())) session.closed = true;
|
|
216
|
+
for (const task of tasks.values()) task.download?.abort();
|
|
217
|
+
finish();
|
|
218
|
+
}
|
|
219
|
+
async function drain() {
|
|
220
|
+
stop();
|
|
221
|
+
for (; ; ) {
|
|
222
|
+
const work = [...requests, ...operations2].map((item) => item.promise ?? item);
|
|
223
|
+
if (!work.length) break;
|
|
224
|
+
await Promise.allSettled(work);
|
|
225
|
+
}
|
|
226
|
+
for (const session of new Set(sessions.values())) for (const binding of session.bindings) {
|
|
227
|
+
await ports2.detach(binding);
|
|
228
|
+
}
|
|
229
|
+
if (startup2 && !startupReleased) {
|
|
230
|
+
startupReleased = true;
|
|
231
|
+
await ports2.detach(startup2);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function beginAssessment(value, installOnly = false) {
|
|
235
|
+
const { binding, session } = value;
|
|
236
|
+
const attempt = attemptFor(value);
|
|
237
|
+
if (value.task && !value.task.done || session.wrapping || attempt?.transition) return { accepted: true };
|
|
238
|
+
session.startupError = void 0;
|
|
239
|
+
session.attempt = void 0;
|
|
240
|
+
let task = tasks.get(taskKey(binding)) ?? [...tasks.values()].find((t) => !t.done && t.target && t.binding.requestedRelease === binding.requestedRelease && resource(t.target) === resource(binding));
|
|
241
|
+
if (!task || task.done) {
|
|
242
|
+
const ownInstall = binding.installationResource ?? binding.installation;
|
|
243
|
+
const ownConfig = binding.configurationResource ?? binding.configuration;
|
|
244
|
+
if ([...tasks.values()].some((t) => !t.done && ((t.binding.installationResource ?? t.binding.installation) === ownInstall || (t.binding.configurationResource ?? t.binding.configuration) === ownConfig) && resource(t.binding) !== resource(binding)))
|
|
245
|
+
throw Error("Selected installation/configuration conflict with active native work");
|
|
246
|
+
task = { id: String(++sequence), binding, installOnly, state: { phase: "starting" }, done: false };
|
|
247
|
+
tasks.set(taskKey(binding), task);
|
|
248
|
+
task.promise = tracked(binding, () => assess(task, session, value));
|
|
249
|
+
}
|
|
250
|
+
value.task = task;
|
|
251
|
+
session.task = task;
|
|
252
|
+
return { accepted: true };
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
closed,
|
|
256
|
+
async admit(binding) {
|
|
257
|
+
if (sessions.size && !sessions.has(binding.mainKey) && ![...sessions.values()].some((session2) => session2.wrapping))
|
|
258
|
+
throw Error(`Updater already owns another ${application} Main`);
|
|
259
|
+
const session = sessionFor(binding);
|
|
260
|
+
if (session.wrapping || session.attempt?.transition) return { period: "keep" };
|
|
261
|
+
session.startupError = void 0;
|
|
262
|
+
let target, authorizationComplete = false;
|
|
263
|
+
try {
|
|
264
|
+
if (!binding.protected) {
|
|
265
|
+
target = await ports2.select(binding, binding.requestedRelease, { startup: true });
|
|
266
|
+
if (ports2.startupReady) {
|
|
267
|
+
const connection = await obtainTargetShell(target);
|
|
268
|
+
authorizationComplete = await ports2.startupReady(target, connection);
|
|
269
|
+
} else {
|
|
270
|
+
const connection = await obtainTargetShell(target);
|
|
271
|
+
const state = await ports2.preparation(target, connection);
|
|
272
|
+
authorizationComplete = state?.prepared === true && !active(state);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error.cancelled === true) return { period: "keep", cancelled: true };
|
|
277
|
+
if (error.code !== "ENOENT" || error.path !== target?.settings?.executable) {
|
|
278
|
+
session.startupError = error.message;
|
|
279
|
+
ports2.report?.(error, target ?? binding, "startup_readiness");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const period = decideStartup({ hostInSelectedAppContainer: binding.protected === true, authorizationComplete });
|
|
283
|
+
return { period, ...period === "wrap" ? { target } : {}, ...session.startupError ? { error: session.startupError } : {} };
|
|
284
|
+
},
|
|
285
|
+
executeStartup,
|
|
286
|
+
async bind(peer) {
|
|
287
|
+
const binding = await ports2.bind(peer), id = String(++sequence);
|
|
288
|
+
const session = sessionFor(binding);
|
|
289
|
+
session.bindings.add(binding);
|
|
290
|
+
session.binding = binding;
|
|
291
|
+
clients2.set(id, { binding, session, released: false });
|
|
292
|
+
return id;
|
|
293
|
+
},
|
|
294
|
+
request(id, operation, fields = {}) {
|
|
295
|
+
const request = (async () => {
|
|
296
|
+
if (Object.keys(fields).some((key) => operation !== "install" || key !== "release")) throw Error("Unknown operation fields");
|
|
297
|
+
if (!["status", "ensure", "install", "exit", "later", "restore", "release", "uninstall"].includes(operation)) throw Error("Unknown operation");
|
|
298
|
+
const value = clients2.get(id);
|
|
299
|
+
if (!value || value.released) throw Error("Unknown client binding");
|
|
300
|
+
const { binding, session } = value;
|
|
301
|
+
if (operation === "status") return observedStatus(value);
|
|
302
|
+
if (operation === "exit") return exit2(session);
|
|
303
|
+
if (operation === "restore" || operation === "release") {
|
|
304
|
+
if (operation === "release") value.released = true;
|
|
305
|
+
const pendingWork = [
|
|
306
|
+
session.attempt?.transition && session.attempt.promise,
|
|
307
|
+
session.task && !session.task.done && session.task.promise
|
|
308
|
+
].filter(Boolean);
|
|
309
|
+
if (pendingWork.length && operation === "release") {
|
|
310
|
+
void tracked(binding, async () => {
|
|
311
|
+
await Promise.allSettled(pendingWork);
|
|
312
|
+
await ports2.restore(binding);
|
|
313
|
+
}).catch((error) => ports2.report?.(error));
|
|
314
|
+
return { released: true, cleanupPending: true };
|
|
315
|
+
}
|
|
316
|
+
await tracked(binding, async () => {
|
|
317
|
+
await Promise.allSettled(pendingWork);
|
|
318
|
+
await ports2.restore(binding);
|
|
319
|
+
});
|
|
320
|
+
return operation === "release" ? { released: true } : { restored: true };
|
|
321
|
+
}
|
|
322
|
+
if (draining2 || session.closed) throw Error("Updater is draining");
|
|
323
|
+
if (!await ports2.alive(binding)) throw Error(`Bound ${application} process departed`);
|
|
324
|
+
if (operation === "ensure") {
|
|
325
|
+
value.ensureIntent = true;
|
|
326
|
+
return beginAssessment(value);
|
|
327
|
+
}
|
|
328
|
+
if (operation === "install") return beginAssessment({
|
|
329
|
+
...value,
|
|
330
|
+
task: void 0,
|
|
331
|
+
binding: { ...binding, requestedRelease: fields.release }
|
|
332
|
+
}, true);
|
|
333
|
+
if (operation === "later") {
|
|
334
|
+
const task = session.task ?? value.task;
|
|
335
|
+
const observed = await observedStatus(value);
|
|
336
|
+
if (observed.phase !== "ready") throw Error("No current reopen reminder");
|
|
337
|
+
session.dismissedReminder = observed.reminderId;
|
|
338
|
+
if (task) task.dismissed = true;
|
|
339
|
+
return { dismissed: true };
|
|
340
|
+
}
|
|
341
|
+
if (operation === "uninstall") {
|
|
342
|
+
if (binding.protected) throw Error("Native uninstall requires an ordinary session");
|
|
343
|
+
if ([...operations2].some((work) => resource(work.binding) === resource(binding))) throw Error("Native operation conflict");
|
|
344
|
+
try {
|
|
345
|
+
await ports2.restore(binding);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
ports2.report?.(error, binding, "uninstall_cleanup");
|
|
348
|
+
}
|
|
349
|
+
void tracked(binding, async () => {
|
|
350
|
+
try {
|
|
351
|
+
const result = await ports2.uninstall(binding);
|
|
352
|
+
value.result = { phase: result?.code ? "failed" : "completed", nativeUninstall: result };
|
|
353
|
+
} catch (error) {
|
|
354
|
+
value.result = failure(error);
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
return { accepted: true };
|
|
358
|
+
}
|
|
359
|
+
})();
|
|
360
|
+
if (operation === "status") return request;
|
|
361
|
+
requests.add(request);
|
|
362
|
+
return request.finally(() => requests.delete(request));
|
|
363
|
+
},
|
|
364
|
+
async observeMainExits() {
|
|
365
|
+
if (draining2) return true;
|
|
366
|
+
const current = [...new Set(sessions.values())];
|
|
367
|
+
const replacingMain = (session) => session.wrapping && !ports2.retainsMainDuringWrap;
|
|
368
|
+
if (!current.length) {
|
|
369
|
+
if (startup2 && !await ports2.mainAlive(startup2)) {
|
|
370
|
+
stop();
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
for (const session of current) {
|
|
376
|
+
if (replacingMain(session)) continue;
|
|
377
|
+
if (!await ports2.mainAlive(session.mainBinding)) session.closed = true;
|
|
378
|
+
}
|
|
379
|
+
if (current.every((session) => session.closed || replacingMain(session))) {
|
|
380
|
+
if (current.every(replacingMain)) return false;
|
|
381
|
+
stop();
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
return false;
|
|
385
|
+
},
|
|
386
|
+
stop,
|
|
387
|
+
drain
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// updater/common/protocol.js
|
|
392
|
+
var protocol = 1;
|
|
393
|
+
var operations = ["hello", "bind", "admit", "status", "ensure", "install", "exit", "later", "restore", "release", "uninstall"];
|
|
394
|
+
function projectState(value) {
|
|
395
|
+
if (typeof value?.phase !== "string" || value.phase.length > 64) throw Error("Invalid native phase");
|
|
396
|
+
const result = { phase: value.phase, reopenRequired: value.reopenRequired };
|
|
397
|
+
for (const name of ["currentProtected", "authorizationPending", "reminderDismissed"]) if (typeof value[name] === "boolean") result[name] = value[name];
|
|
398
|
+
for (const name of ["currentRelease", "requestedRelease", "reason", "reminderId"]) if (typeof value[name] === "string") result[name] = value[name];
|
|
399
|
+
if (value.error) result.error = String(value.error).slice(0, 2048);
|
|
400
|
+
if (value.authorization) {
|
|
401
|
+
result.authorization = {};
|
|
402
|
+
for (const name of ["phase", "prepared", "authorization_in_progress", "exit_code"])
|
|
403
|
+
if (value.authorization[name] !== void 0) result.authorization[name] = value.authorization[name];
|
|
404
|
+
if (value.authorization.error) result.authorization.error = String(value.authorization.error).slice(0, 2048);
|
|
405
|
+
}
|
|
406
|
+
if (value.progress) {
|
|
407
|
+
const percent = value.progress.overall_percent ?? null;
|
|
408
|
+
if (percent !== null && (typeof percent !== "number" || !Number.isFinite(percent))) throw Error("Invalid native percentage");
|
|
409
|
+
result.progress = { overall_percent: percent };
|
|
410
|
+
const current = Array.isArray(value.progress.items) ? value.progress.items.find((item) => item.state === "start") : void 0;
|
|
411
|
+
if (typeof current?.path === "string") result.progress.items = [{
|
|
412
|
+
path: current.path.slice(0, 1024),
|
|
413
|
+
...typeof current.state === "string" ? { state: current.state.slice(0, 64) } : {}
|
|
414
|
+
}];
|
|
415
|
+
}
|
|
416
|
+
for (const name of ["prepared", "authorization_in_progress"]) if (typeof value[name] === "boolean") result[name] = value[name];
|
|
417
|
+
if (value.nativeSetup) result.nativeSetup = { code: value.nativeSetup.code, phase: value.nativeSetup.phase };
|
|
418
|
+
if (value.nativeUninstall) result.nativeUninstall = { code: value.nativeUninstall.code };
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
function validateRequest(value) {
|
|
422
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw Error("Invalid updater request");
|
|
423
|
+
if (value.protocol !== void 0 && !Number.isSafeInteger(value.protocol)) throw Error("Invalid updater framing label");
|
|
424
|
+
if (!operations.includes(value.operation)) throw Error("Unknown updater operation");
|
|
425
|
+
const allowed = ["hello", "bind", "admit"].includes(value.operation) ? value.operation === "hello" ? ["protocol", "operation"] : ["protocol", "operation", "context"] : value.operation === "install" ? ["protocol", "operation", "client", "release"] : ["protocol", "operation", "client"];
|
|
426
|
+
if (Object.keys(value).some((key) => !allowed.includes(key))) throw Error("Unknown updater request fields");
|
|
427
|
+
if (!["hello", "bind", "admit"].includes(value.operation) && typeof value.client !== "string") throw Error("Missing updater client binding");
|
|
428
|
+
if (value.operation === "install" && typeof value.release !== "string") throw Error("Missing installation release");
|
|
429
|
+
return value;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// updater/common/transport.js
|
|
433
|
+
async function handleRequest(message, { dispatch: dispatch2, reply }) {
|
|
434
|
+
let continuation, response;
|
|
435
|
+
try {
|
|
436
|
+
response = { ok: true, value: await dispatch2(message, (action) => {
|
|
437
|
+
continuation = action;
|
|
438
|
+
}) };
|
|
439
|
+
} catch (error) {
|
|
440
|
+
response = { ok: false, error: error.message.slice(0, 2048) };
|
|
441
|
+
}
|
|
442
|
+
await reply(response);
|
|
443
|
+
if (response.ok) continuation?.();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// updater/common/index.js
|
|
447
|
+
import { appendFile } from "node:fs/promises";
|
|
448
|
+
function createDiagnostics(destination, { append = appendFile } = {}) {
|
|
449
|
+
return (fields) => {
|
|
450
|
+
try {
|
|
451
|
+
if (destination) void append(destination, JSON.stringify(fields) + "\n").catch(() => {
|
|
452
|
+
});
|
|
453
|
+
} catch {
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// updater/server.js
|
|
459
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
460
|
+
import { join as join5, dirname as dirname2 } from "node:path";
|
|
461
|
+
import { fileURLToPath } from "node:url";
|
|
462
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
463
|
+
|
|
464
|
+
// updater/native.js
|
|
465
|
+
import { createRequire as createRequire2 } from "node:module";
|
|
466
|
+
import { join as join2 } from "node:path";
|
|
467
|
+
|
|
468
|
+
// updater/common/package.js
|
|
469
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
470
|
+
import { createRequire } from "node:module";
|
|
471
|
+
import { join, isAbsolute, dirname, resolve } from "node:path";
|
|
472
|
+
var sdkName = "@finogeeks2026/finsafe-sdk";
|
|
473
|
+
function packageSdk(root2, { sourceRoot = root2 } = {}) {
|
|
474
|
+
const read = (directory) => JSON.parse(readFileSync(join(directory, "package.json"), "utf8"));
|
|
475
|
+
const plugin = read(root2);
|
|
476
|
+
const sdkRoot = join(sourceRoot, "node_modules", sdkName);
|
|
477
|
+
return {
|
|
478
|
+
root: sdkRoot,
|
|
479
|
+
version: plugin.version,
|
|
480
|
+
load: () => createRequire(join(root2, "package.json"))(join(sdkRoot, "src", "native.js"))
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// updater/native.js
|
|
485
|
+
function nativeAdapter(root2, {
|
|
486
|
+
require: require2 = createRequire2(join2(root2, "package.json")),
|
|
487
|
+
sdk: sdk2 = packageSdk(root2).load()
|
|
488
|
+
} = {}) {
|
|
489
|
+
const native2 = sdk2.createClient();
|
|
490
|
+
const call2 = (request) => native2({
|
|
491
|
+
module: "app_seam.opencode",
|
|
492
|
+
request: request.action === "stage_configuration" ? { ...request, candidate: join2(root2, "policy", "candidate.yaml") } : request
|
|
493
|
+
});
|
|
494
|
+
call2.watchMain = (request, onExit) => native2.watchMain({ module: "app_seam.opencode", request }, onExit);
|
|
495
|
+
call2.buildInfo = native2.buildInfo;
|
|
496
|
+
call2.libraryPath = native2.libraryPath;
|
|
497
|
+
call2.sdkRoot = native2.sdkRoot;
|
|
498
|
+
return call2;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// src/known-folders.js
|
|
502
|
+
import { loadKoffi } from "../node_modules/@finogeeks2026/finsafe-sdk/src/native.js";
|
|
503
|
+
function readKnownFolders() {
|
|
504
|
+
const koffi = loadKoffi();
|
|
505
|
+
const ole = koffi.load("ole32.dll"), shell = koffi.load("shell32.dll");
|
|
506
|
+
const guid = ole.func("int32_t __stdcall CLSIDFromString(str16 text, void* value)");
|
|
507
|
+
const folder = shell.func("int32_t __stdcall SHGetKnownFolderPath(void* id, uint32_t flags, void* token, void* path)");
|
|
508
|
+
const free = ole.func("void __stdcall CoTaskMemFree(void* value)");
|
|
509
|
+
const result = {};
|
|
510
|
+
for (const [key, id] of Object.entries({
|
|
511
|
+
programFiles: "{905E63B6-C1BF-494E-B29C-65B732D3D21A}",
|
|
512
|
+
programData: "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}",
|
|
513
|
+
localAppData: "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"
|
|
514
|
+
})) {
|
|
515
|
+
const value = Buffer.alloc(16), out = Buffer.alloc(koffi.sizeof("void*"));
|
|
516
|
+
if (guid(id, value) !== 0) throw new Error("Invalid known-folder identifier");
|
|
517
|
+
const code = folder(value, 65536, null, out);
|
|
518
|
+
if (code !== 0) throw new Error(`SHGetKnownFolderPath ${key}: HRESULT ${code}`);
|
|
519
|
+
const pointer = koffi.decode(out, "void*");
|
|
520
|
+
try {
|
|
521
|
+
result[key] = koffi.decode(out, "str16");
|
|
522
|
+
} finally {
|
|
523
|
+
free(pointer);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return result;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// updater/coordinator.js
|
|
530
|
+
var createUpdater2 = (options2) => createUpdater({ ...options2, application: "OpenCode" });
|
|
531
|
+
|
|
532
|
+
// updater/management.js
|
|
533
|
+
function createManagement(options2) {
|
|
534
|
+
const updater2 = createUpdater2(options2), request = updater2.request;
|
|
535
|
+
updater2.request = (id, operation, fields) => operation === "uninstall" ? Promise.reject(Error("Plugin removal retains FinSAFE runtime, configuration and authorization")) : request(id, operation, fields);
|
|
536
|
+
return updater2;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// src/preparation.js
|
|
540
|
+
import { createRequire as createRequire3 } from "node:module";
|
|
541
|
+
|
|
542
|
+
// src/connector.js
|
|
543
|
+
import { execFile, spawn } from "node:child_process";
|
|
544
|
+
import { StringDecoder } from "node:string_decoder";
|
|
545
|
+
import { EventEmitter } from "node:events";
|
|
546
|
+
function nativeDesktopChild({ start, read }) {
|
|
547
|
+
const child = new EventEmitter();
|
|
548
|
+
child.stdout = new EventEmitter();
|
|
549
|
+
child.stderr = new EventEmitter();
|
|
550
|
+
queueMicrotask(async () => {
|
|
551
|
+
try {
|
|
552
|
+
const owner = await start();
|
|
553
|
+
for (; ; ) {
|
|
554
|
+
const result = await read(owner.owner);
|
|
555
|
+
if (result.stdout) child.stdout.emit("data", result.stdout);
|
|
556
|
+
if (result.stderr) child.stderr.emit("data", result.stderr);
|
|
557
|
+
if (result.eof) {
|
|
558
|
+
child.emit("close", result.code, null);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
} catch (error) {
|
|
563
|
+
child.emit("error", error);
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
return child;
|
|
567
|
+
}
|
|
568
|
+
function submitDesktopAuthorization(executable, args, { spawnChild = spawn, env } = {}) {
|
|
569
|
+
return new Promise((resolve2, reject) => {
|
|
570
|
+
const child = spawnChild(executable, args, { env, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
571
|
+
const decoder = new StringDecoder("utf8");
|
|
572
|
+
let buffer = "", diagnostics = "", ack, terminal, resolveCompletion, rejectCompletion, transportError;
|
|
573
|
+
const completion = new Promise((yes, no) => {
|
|
574
|
+
resolveCompletion = yes;
|
|
575
|
+
rejectCompletion = no;
|
|
576
|
+
});
|
|
577
|
+
completion.catch(() => {
|
|
578
|
+
});
|
|
579
|
+
child.stderr.on("data", (bytes) => {
|
|
580
|
+
diagnostics = (diagnostics + bytes).slice(-65536);
|
|
581
|
+
});
|
|
582
|
+
child.stdout.on("data", (bytes) => {
|
|
583
|
+
if (transportError) return;
|
|
584
|
+
buffer += Buffer.isBuffer(bytes) ? decoder.write(bytes) : bytes;
|
|
585
|
+
try {
|
|
586
|
+
let end;
|
|
587
|
+
while ((end = buffer.indexOf("\n")) >= 0) {
|
|
588
|
+
if (Buffer.byteLength(buffer.slice(0, end)) > 16384) throw Error("Desktop result frame exceeds 16 KiB");
|
|
589
|
+
const frame = JSON.parse(buffer.slice(0, end));
|
|
590
|
+
buffer = buffer.slice(end + 1);
|
|
591
|
+
if (!frame.value || typeof frame.value !== "object") throw Error("Invalid Desktop result");
|
|
592
|
+
if (frame.record === "ack" && !ack && !terminal) {
|
|
593
|
+
ack = frame.value;
|
|
594
|
+
if (ack.error) throw Error(ack.error);
|
|
595
|
+
resolve2({ ...ack, completion });
|
|
596
|
+
} else if (frame.record === "terminal" && ack && !terminal) terminal = frame.value;
|
|
597
|
+
else throw Error("Unexpected Desktop result frame");
|
|
598
|
+
}
|
|
599
|
+
if (Buffer.byteLength(buffer) > 16384) throw Error("Desktop result frame exceeds 16 KiB");
|
|
600
|
+
} catch (error) {
|
|
601
|
+
transportError = error;
|
|
602
|
+
if (!ack) reject(error);
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
child.on("error", (error) => {
|
|
606
|
+
transportError = error;
|
|
607
|
+
reject(error);
|
|
608
|
+
rejectCompletion(error);
|
|
609
|
+
});
|
|
610
|
+
child.on("close", (code, signal) => {
|
|
611
|
+
buffer += decoder.end();
|
|
612
|
+
const error = transportError ?? (buffer.length ? Error("Desktop result ended with a truncated frame") : signal || !Number.isInteger(code) ? Error(`Desktop task ended by ${signal ?? "unknown exit"}`) : terminal && Number.isInteger(terminal.exit_code) && terminal.exit_code !== code ? Error(`Desktop task exit ${code} differs from terminal ${terminal.exit_code}`) : !ack ? Error(`Desktop acknowledgment missing (${code}): ${diagnostics}`) : ack.accepted && !terminal ? Error(`Desktop task exited without terminal result (${code}): ${diagnostics}`) : code && !terminal ? Error(`Desktop task exited (${code}): ${diagnostics}`) : void 0);
|
|
613
|
+
if (error) {
|
|
614
|
+
reject(error);
|
|
615
|
+
rejectCompletion(error);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
resolveCompletion(terminal ?? ack);
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// src/preparation.js
|
|
624
|
+
var sdk = () => createRequire3(import.meta.url)("../node_modules/@finogeeks2026/finsafe-sdk/src/native.js");
|
|
625
|
+
var options = (settings) => ({ env: {
|
|
626
|
+
...process.env,
|
|
627
|
+
LOCALAPPDATA: settings.localAppData
|
|
628
|
+
} });
|
|
629
|
+
function prepareRuntime(settings, { run = submitDesktopAuthorization } = {}) {
|
|
630
|
+
return run(
|
|
631
|
+
settings.executable,
|
|
632
|
+
[
|
|
633
|
+
"desktop",
|
|
634
|
+
"configuration",
|
|
635
|
+
"apply",
|
|
636
|
+
"--json",
|
|
637
|
+
"--yaml-path",
|
|
638
|
+
settings.yamlPath,
|
|
639
|
+
...settings.retainedWorker ? ["--retained-worker", settings.retainedWorker] : []
|
|
640
|
+
],
|
|
641
|
+
options(settings)
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
function readPreparation(connection, { attempt, client = sdk().createStatusClient } = {}) {
|
|
645
|
+
const status = client(connection);
|
|
646
|
+
return attempt === void 0 ? status.authorizationState() : status.authorizationProgress(attempt);
|
|
647
|
+
}
|
|
648
|
+
var obtainShell = (settings) => sdk().obtainShell(settings);
|
|
649
|
+
|
|
650
|
+
// updater/common/download.js
|
|
651
|
+
import { createHash } from "node:crypto";
|
|
652
|
+
function verifyPayload(bytes, sums, name) {
|
|
653
|
+
const matches = sums.split(/\r?\n/).map((line) => /^([a-f0-9]{64})\s+\*?(.+)$/i.exec(line)).filter((match) => match?.[2] === name);
|
|
654
|
+
if (matches.length !== 1) throw new Error("Expected exactly one official Windows payload checksum");
|
|
655
|
+
if (createHash("sha256").update(bytes).digest("hex") !== matches[0][1].toLowerCase())
|
|
656
|
+
throw new Error("FinSAFE payload SHA256 mismatch");
|
|
657
|
+
}
|
|
658
|
+
async function downloadInstaller(version2, cdnBaseUrl, options2 = {}) {
|
|
659
|
+
options2.signal?.throwIfAborted();
|
|
660
|
+
const controller = new AbortController();
|
|
661
|
+
const signal = options2.signal ? AbortSignal.any([options2.signal, controller.signal]) : controller.signal;
|
|
662
|
+
const name = `finshield-${version2}-x64-setup.exe`;
|
|
663
|
+
async function download(url, format) {
|
|
664
|
+
try {
|
|
665
|
+
const response = await fetch(url, { signal: AbortSignal.any([signal, AbortSignal.timeout(3e4)]) });
|
|
666
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
667
|
+
return await response[format]();
|
|
668
|
+
} catch (error) {
|
|
669
|
+
throw new Error(`FinShield download failed: ${error.message}: ${url}`, { cause: error });
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
async function cdn() {
|
|
673
|
+
const base = `${cdnBaseUrl.replace(/\/$/, "")}/releases/download/v${version2}`;
|
|
674
|
+
const metadata = await download(`${base}/release.json`, "json");
|
|
675
|
+
const assets = metadata?.version === version2 && Array.isArray(metadata.assets) ? metadata.assets.filter((asset2) => asset2?.name === name && asset2.kind === "windows-installer") : [];
|
|
676
|
+
const asset = assets[0], prefix = `finshield-${version2}-x64-setup-`;
|
|
677
|
+
if (assets.length !== 1 || typeof asset.mirror_name !== "string" || !asset.mirror_name.startsWith(prefix) || !/^[a-f0-9]{32}\.exe$/.test(asset.mirror_name.slice(prefix.length)) || !/^[a-f0-9]{64}$/i.test(asset.sha256))
|
|
678
|
+
throw new Error("Invalid FinShield release metadata for requested Windows installer");
|
|
679
|
+
const bytes = Buffer.from(await download(`${base}/${asset.mirror_name}`, "arrayBuffer"));
|
|
680
|
+
verifyPayload(bytes, `${asset.sha256} ${name}`, name);
|
|
681
|
+
return bytes;
|
|
682
|
+
}
|
|
683
|
+
async function github() {
|
|
684
|
+
const base = `https://github.com/finogeeks/finsafe/releases/download/v${version2}`;
|
|
685
|
+
const [payload, sums] = await Promise.all([
|
|
686
|
+
download(`${base}/${name}`, "arrayBuffer"),
|
|
687
|
+
download(`${base}/SHA256SUMS`, "text")
|
|
688
|
+
]);
|
|
689
|
+
const bytes = Buffer.from(payload);
|
|
690
|
+
verifyPayload(bytes, sums, name);
|
|
691
|
+
return bytes;
|
|
692
|
+
}
|
|
693
|
+
try {
|
|
694
|
+
return await Promise.any([cdn(), github()]);
|
|
695
|
+
} catch (error) {
|
|
696
|
+
options2.signal?.throwIfAborted();
|
|
697
|
+
throw new AggregateError(
|
|
698
|
+
error.errors,
|
|
699
|
+
`FinShield CDN and GitHub download failed: ${error.errors.map((e) => e.message).join("; ")}`
|
|
700
|
+
);
|
|
701
|
+
} finally {
|
|
702
|
+
controller.abort();
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// install/runtime.js
|
|
707
|
+
import { randomUUID } from "node:crypto";
|
|
708
|
+
import { existsSync as existsSync2, mkdirSync, mkdtempSync, readFileSync as readFileSync2, writeFileSync } from "node:fs";
|
|
709
|
+
import { join as join3 } from "node:path";
|
|
710
|
+
async function stageRuntime(destination, version2, options2 = {}) {
|
|
711
|
+
if (options2.confined) throw new Error("Protected Host cannot install or prepare native FinSAFE; reopen ordinary OpenCode");
|
|
712
|
+
if (!/^\d+\.\d+\.\d+$/.test(version2)) throw new Error("A stable FinSAFE version is required");
|
|
713
|
+
const name = `finshield-${version2}-x64-setup.exe`;
|
|
714
|
+
const { cdnBaseUrl } = JSON.parse(readFileSync2(new URL("./download-source.json", import.meta.url), "utf8"));
|
|
715
|
+
const bytes = await downloadInstaller(version2, cdnBaseUrl, options2);
|
|
716
|
+
mkdirSync(destination, { recursive: true });
|
|
717
|
+
const root2 = mkdtempSync(join3(destination, version2 + "-"));
|
|
718
|
+
writeFileSync(join3(root2, name), bytes);
|
|
719
|
+
return { version: version2, package: join3(root2, name) };
|
|
720
|
+
}
|
|
721
|
+
async function installRuntime(destination, version2, options2 = {}) {
|
|
722
|
+
if (options2.confined) throw Error("Protected Host cannot install native FinSAFE");
|
|
723
|
+
if (!options2.execute || !options2.installation || !options2.configuration || !options2.candidate)
|
|
724
|
+
throw Error("OpenCode setup requires an SDK-bound target and its staged Candidate");
|
|
725
|
+
const staged = options2.staged ?? await stageRuntime(destination, version2, options2);
|
|
726
|
+
if (staged.version !== version2) throw Error("Staged release differs from requested native target");
|
|
727
|
+
const packagePath = staged.package;
|
|
728
|
+
const operationId = randomUUID();
|
|
729
|
+
const request = {
|
|
730
|
+
version: version2,
|
|
731
|
+
package: packagePath,
|
|
732
|
+
operation_id: operationId,
|
|
733
|
+
installation: options2.installation,
|
|
734
|
+
configuration: options2.configuration,
|
|
735
|
+
previousInstallation: options2.previousInstallation,
|
|
736
|
+
candidate: options2.candidate,
|
|
737
|
+
integration_outputs: false,
|
|
738
|
+
self_update_enabled: false
|
|
739
|
+
};
|
|
740
|
+
options2.onRequest?.(request);
|
|
741
|
+
const result = await options2.execute(request);
|
|
742
|
+
if (![0, 3010, 1641].includes(result.code)) {
|
|
743
|
+
const error = new Error(`Windows setup returned ${result.code}: ${result.detail || result.stderr || ""}`);
|
|
744
|
+
error.setupCode = result.code;
|
|
745
|
+
const observed = observeSetupResult({ phase: "installing" }, result.resultPath);
|
|
746
|
+
error.result = {
|
|
747
|
+
...observed,
|
|
748
|
+
phase: observed.nativeSetup ? observed.phase : "failed",
|
|
749
|
+
error: observed.error || error.message,
|
|
750
|
+
nativeSetup: observed.nativeSetup ?? { code: result.code }
|
|
751
|
+
};
|
|
752
|
+
throw error;
|
|
753
|
+
}
|
|
754
|
+
return {
|
|
755
|
+
executable: join3(request.installation, "finsafe.exe"),
|
|
756
|
+
yamlPath: join3(request.configuration, "effective.yaml"),
|
|
757
|
+
configuration: request.configuration,
|
|
758
|
+
exitCode: result.code,
|
|
759
|
+
resultPath: result.resultPath,
|
|
760
|
+
retainedWorker: result.retainedWorker,
|
|
761
|
+
rebootRequired: result.code !== 0
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
function observeSetupResult(state, path) {
|
|
765
|
+
if (!path || !existsSync2(path)) return state;
|
|
766
|
+
const result = JSON.parse(readFileSync2(path, "utf8"));
|
|
767
|
+
if (!Number.isInteger(result.code) || !["completed", "cancelled", "failed", "reboot-required"].includes(result.phase)) {
|
|
768
|
+
throw new Error("Invalid native setup result");
|
|
769
|
+
}
|
|
770
|
+
return { ...state, ...state.phase === "installing" ? { phase: result.phase, error: result.detail || void 0 } : {}, nativeSetup: result };
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// src/instance-settings.js
|
|
774
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
775
|
+
import { isAbsolute as isAbsolute2, join as join4 } from "node:path";
|
|
776
|
+
function saveInstanceSettings(settings, home) {
|
|
777
|
+
const directory = join4(home, "finsafe-windows");
|
|
778
|
+
mkdirSync2(directory, { recursive: true });
|
|
779
|
+
writeFileSync2(join4(directory, "config.json"), JSON.stringify(settings));
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// updater/operations.js
|
|
783
|
+
function createOperations({ native: native2, call: call2, read = readPreparation, stage = stageRuntime, install = installRuntime, authorize }) {
|
|
784
|
+
const external = (b) => b.manageInstallation === false;
|
|
785
|
+
const requireManaged = (b) => {
|
|
786
|
+
if (external(b)) throw Error("The selected external FinSAFE installation is managed outside this plugin");
|
|
787
|
+
};
|
|
788
|
+
const submit = authorize ?? ((b) => prepareRuntime({ ...b.settings, retainedWorker: b.retainedWorker }, {
|
|
789
|
+
run: (exe, args, options2) => submitDesktopAuthorization(exe, args, {
|
|
790
|
+
...options2,
|
|
791
|
+
spawnChild: () => nativeDesktopChild({
|
|
792
|
+
start: () => native2(b, "authorization_start", { retainedWorker: b.retainedWorker }),
|
|
793
|
+
read: (owner) => call2({ action: "authorization_read", owner })
|
|
794
|
+
})
|
|
795
|
+
})
|
|
796
|
+
}));
|
|
797
|
+
return {
|
|
798
|
+
installation: (b) => native2(b, "installation", { selected: b.selected === true }),
|
|
799
|
+
async select(b, release, { startup: startup2 = false } = {}) {
|
|
800
|
+
const selected = await native2(b, "select_target", { release, startup: startup2, ...b.roots });
|
|
801
|
+
return { ...b, ...selected, selected: true, settings: { ...b.settings, ...selected, version: release } };
|
|
802
|
+
},
|
|
803
|
+
async stage(b, release, signal) {
|
|
804
|
+
requireManaged(b);
|
|
805
|
+
return stage(b.settings.downloads, release, { signal });
|
|
806
|
+
},
|
|
807
|
+
async install(b, release, staged) {
|
|
808
|
+
requireManaged(b);
|
|
809
|
+
if (staged.installed) return { phase: "completed" };
|
|
810
|
+
const candidate = await native2(b, "stage_configuration"), inputs = await native2(b, "setup_inputs");
|
|
811
|
+
const result = await install(b.settings.downloads, release, {
|
|
812
|
+
...b.settings,
|
|
813
|
+
...candidate,
|
|
814
|
+
...inputs,
|
|
815
|
+
staged,
|
|
816
|
+
execute: (request) => native2(b, "run_setup", { request })
|
|
817
|
+
});
|
|
818
|
+
b.retainedWorker = result.retainedWorker;
|
|
819
|
+
return { phase: result.rebootRequired ? "reboot-required" : "completed", nativeSetup: { code: result.exitCode } };
|
|
820
|
+
},
|
|
821
|
+
async selected(b, target) {
|
|
822
|
+
saveInstanceSettings(target.settings, b.home);
|
|
823
|
+
},
|
|
824
|
+
async prepare(b, connection) {
|
|
825
|
+
if (external(b)) return read(connection);
|
|
826
|
+
const result = await submit(b);
|
|
827
|
+
b.retainedWorker = void 0;
|
|
828
|
+
return result;
|
|
829
|
+
},
|
|
830
|
+
preparation: (_b, connection) => read(connection),
|
|
831
|
+
progress: (_b, connection, attempt) => read(connection, { attempt })
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// updater/launch.js
|
|
836
|
+
async function launchHost(binding, { replace, confirm, record: record2 = () => {
|
|
837
|
+
}, listen, onStopped = () => {
|
|
838
|
+
}, observeReplacement = () => {
|
|
839
|
+
} }) {
|
|
840
|
+
if (listen) await listen();
|
|
841
|
+
const oldHostPid = binding.pid, result = await replace();
|
|
842
|
+
if (result.main_pid !== binding.main || result.mainKey !== binding.mainKey)
|
|
843
|
+
throw Error("OpenCode Main identity changed during Host handoff");
|
|
844
|
+
onStopped(false);
|
|
845
|
+
record2("ac-host-ready", {
|
|
846
|
+
oldMainPid: binding.main,
|
|
847
|
+
oldHostPid,
|
|
848
|
+
mainPid: result.main_pid,
|
|
849
|
+
mainKey: result.mainKey,
|
|
850
|
+
newHostPid: result.host_pid,
|
|
851
|
+
corePid: result.core_pid
|
|
852
|
+
});
|
|
853
|
+
await confirm(result);
|
|
854
|
+
binding.pid = result.host_pid;
|
|
855
|
+
binding.protected = true;
|
|
856
|
+
observeReplacement(result.mainKey);
|
|
857
|
+
record2("main-host-connected", { mainKey: result.mainKey, hostPid: result.host_pid });
|
|
858
|
+
return result;
|
|
859
|
+
}
|
|
860
|
+
function createConnectionConfirmation({ timeout = 45e3 } = {}) {
|
|
861
|
+
const attempts = /* @__PURE__ */ new Map();
|
|
862
|
+
return {
|
|
863
|
+
begin(mainKey) {
|
|
864
|
+
if (attempts.has(mainKey)) throw Error("OpenCode Host handoff is already active");
|
|
865
|
+
let resolve2, reject, timer, expected;
|
|
866
|
+
const completion = new Promise((yes, no) => {
|
|
867
|
+
resolve2 = yes;
|
|
868
|
+
reject = no;
|
|
869
|
+
}), seen = /* @__PURE__ */ new Set();
|
|
870
|
+
const attempt = { seen, observe(pid) {
|
|
871
|
+
seen.add(pid);
|
|
872
|
+
if (pid === expected) resolve2();
|
|
873
|
+
} };
|
|
874
|
+
attempts.set(mainKey, attempt);
|
|
875
|
+
return { async wait(result) {
|
|
876
|
+
expected = result.host_pid;
|
|
877
|
+
if (seen.has(expected)) return;
|
|
878
|
+
timer = setTimeout(() => reject(Error("OpenCode Main did not confirm its business connection to the protected Host")), timeout);
|
|
879
|
+
try {
|
|
880
|
+
await completion;
|
|
881
|
+
} finally {
|
|
882
|
+
clearTimeout(timer);
|
|
883
|
+
}
|
|
884
|
+
}, dispose() {
|
|
885
|
+
clearTimeout(timer);
|
|
886
|
+
attempts.delete(mainKey);
|
|
887
|
+
} };
|
|
888
|
+
},
|
|
889
|
+
observe(binding) {
|
|
890
|
+
if (binding.protected && binding.context?.connectedHostPid === binding.pid)
|
|
891
|
+
attempts.get(binding.mainKey)?.observe(binding.pid);
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// updater/server.js
|
|
897
|
+
var root = fileURLToPath(new URL("../", import.meta.url));
|
|
898
|
+
var diagnostic = createDiagnostics(process.argv[7]);
|
|
899
|
+
var record = (event, fields = {}) => diagnostic({ at: (/* @__PURE__ */ new Date()).toISOString(), event, ...fields });
|
|
900
|
+
var report = (error) => record("error", { error: error.message, stack: error.stack });
|
|
901
|
+
process.on("uncaughtExceptionMonitor", report);
|
|
902
|
+
var version = JSON.parse(readFileSync4(join5(root, "package.json"), "utf8")).version;
|
|
903
|
+
var call = nativeAdapter(root);
|
|
904
|
+
var folders = readKnownFolders(root);
|
|
905
|
+
var build = { version };
|
|
906
|
+
await call({ action: "select_slot", installation: process.argv[2], configuration: process.argv[3] });
|
|
907
|
+
var startup = await call({ action: "retain_startup_main", pid: Number(process.argv[4]), created: process.argv[5] });
|
|
908
|
+
process.env.OPENCODE_INSTALL_DIR = dirname2(startup.image);
|
|
909
|
+
await call({ action: "initialize_session_job", deadline: Number(process.argv[6]) });
|
|
910
|
+
var clients = /* @__PURE__ */ new Map();
|
|
911
|
+
var roots = /* @__PURE__ */ new Map();
|
|
912
|
+
var confirmations = createConnectionConfirmation();
|
|
913
|
+
var mainWait;
|
|
914
|
+
var protectedEndpoint;
|
|
915
|
+
var draining = false;
|
|
916
|
+
var exit = (code) => call({ action: "exit_updater", code }).catch(report);
|
|
917
|
+
var native = (b, action, fields = {}) => {
|
|
918
|
+
if (b.closing) return Promise.reject(Error("Native binding is closing"));
|
|
919
|
+
const previous = [...b.calls].at(-1);
|
|
920
|
+
const pending = Promise.resolve(previous).catch(() => {
|
|
921
|
+
}).then(() => call({ action, native: b.native, ...fields }));
|
|
922
|
+
b.calls.add(pending);
|
|
923
|
+
return pending.finally(() => b.calls.delete(pending));
|
|
924
|
+
};
|
|
925
|
+
var ports = {
|
|
926
|
+
backend: (b) => obtainShell(b.settings),
|
|
927
|
+
exit: (b) => native(b, "stop_main"),
|
|
928
|
+
retainsMainDuringWrap: true,
|
|
929
|
+
async watchMain(b) {
|
|
930
|
+
const previous = mainWait;
|
|
931
|
+
mainWait = void 0;
|
|
932
|
+
await previous?.();
|
|
933
|
+
mainWait = call.watchMain(b.native ? { native: b.native } : { startup: startup.startup }, () => {
|
|
934
|
+
void updater.observeMainExits().catch((error) => {
|
|
935
|
+
report(error);
|
|
936
|
+
void exit(1);
|
|
937
|
+
});
|
|
938
|
+
});
|
|
939
|
+
},
|
|
940
|
+
bind: async (b) => b,
|
|
941
|
+
alive: (b) => native(b, "alive"),
|
|
942
|
+
mainAlive: (b) => b.native ? native(b, "main_alive") : call({ action: "startup_main_alive", startup: startup.startup }),
|
|
943
|
+
...createOperations({ native, call }),
|
|
944
|
+
restore: async () => {
|
|
945
|
+
},
|
|
946
|
+
async detach(b) {
|
|
947
|
+
if (!b.native) return call({ action: "detach_startup_main", startup: startup.startup });
|
|
948
|
+
b.closing = true;
|
|
949
|
+
await Promise.allSettled([...b.calls]);
|
|
950
|
+
return call({ action: "detach", native: b.native });
|
|
951
|
+
},
|
|
952
|
+
async wrap(b, observeReplacement, onStopped) {
|
|
953
|
+
saveInstanceSettings(b.settings, b.home);
|
|
954
|
+
const confirmation = confirmations.begin(b.mainKey);
|
|
955
|
+
try {
|
|
956
|
+
return await launchHost(b, {
|
|
957
|
+
record,
|
|
958
|
+
observeReplacement,
|
|
959
|
+
onStopped,
|
|
960
|
+
confirm: confirmation.wait,
|
|
961
|
+
replace: () => native(b, "replace_host"),
|
|
962
|
+
listen: async () => {
|
|
963
|
+
protectedEndpoint ??= call({ action: "server", container: true }).then((server) => {
|
|
964
|
+
void serve(server.server).catch((error) => {
|
|
965
|
+
report(error);
|
|
966
|
+
void exit(1);
|
|
967
|
+
});
|
|
968
|
+
});
|
|
969
|
+
await protectedEndpoint;
|
|
970
|
+
}
|
|
971
|
+
});
|
|
972
|
+
} finally {
|
|
973
|
+
confirmation.dispose();
|
|
974
|
+
}
|
|
975
|
+
},
|
|
976
|
+
report
|
|
977
|
+
};
|
|
978
|
+
var updater = createManagement({ ports, startup });
|
|
979
|
+
await ports.watchMain(startup);
|
|
980
|
+
void updater.closed.then(() => {
|
|
981
|
+
if (!draining) {
|
|
982
|
+
draining = true;
|
|
983
|
+
void exit(0);
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
async function materialize(message, context) {
|
|
987
|
+
const b = await call({ action: "bind", connection: message.connection, context });
|
|
988
|
+
const c = b.context;
|
|
989
|
+
record("host-bound", { hostPid: b.pid, mainPid: b.main, protected: b.protected });
|
|
990
|
+
if (!b.protected) roots.set(b.profile, { nativeVersionRoot: c.nativeVersionRoot, configurationVersionRoot: c.configurationVersionRoot, downloads: join5(folders.localAppData, "FinSAFE", "opencode-plugin/downloads") });
|
|
991
|
+
const trusted = roots.get(b.profile);
|
|
992
|
+
if (!trusted) throw Error("No ordinary-bound OpenCode version roots");
|
|
993
|
+
const binding = { ...b, ...c, calls: /* @__PURE__ */ new Set(), roots: trusted, settings: { ...c, ...folders, executable: join5(c.installation, "finsafe.exe"), yamlPath: join5(c.configuration, "effective.yaml"), downloads: trusted.downloads } };
|
|
994
|
+
return binding;
|
|
995
|
+
}
|
|
996
|
+
async function dispatch(message, afterReply) {
|
|
997
|
+
const request = validateRequest(message.request);
|
|
998
|
+
if (request.operation === "hello") return {
|
|
999
|
+
version,
|
|
1000
|
+
protocol,
|
|
1001
|
+
draining,
|
|
1002
|
+
sdkManagement: true,
|
|
1003
|
+
retainedMainHost: true,
|
|
1004
|
+
usageContext: true,
|
|
1005
|
+
sdk: { ...build, root: call.sdkRoot, libraryPath: call.libraryPath }
|
|
1006
|
+
};
|
|
1007
|
+
if (["admit", "bind"].includes(request.operation)) {
|
|
1008
|
+
if (draining) throw Error("Updater is draining");
|
|
1009
|
+
const peer = await materialize(message, request.context);
|
|
1010
|
+
if (request.operation === "admit") {
|
|
1011
|
+
const { target, ...decision } = await updater.admit(peer);
|
|
1012
|
+
if (decision.period === "wrap") afterReply(() => updater.executeStartup(peer, target));
|
|
1013
|
+
return decision;
|
|
1014
|
+
}
|
|
1015
|
+
const client = randomUUID2();
|
|
1016
|
+
clients.set(client, { id: await updater.bind(peer), peer: message.peer });
|
|
1017
|
+
confirmations.observe(peer);
|
|
1018
|
+
return { client };
|
|
1019
|
+
}
|
|
1020
|
+
const binding = clients.get(request.client);
|
|
1021
|
+
if (!binding || binding.peer !== message.peer) throw Error("Updater client identity mismatch");
|
|
1022
|
+
try {
|
|
1023
|
+
const result = await updater.request(binding.id, request.operation, request.operation === "install" ? { release: request.release } : {});
|
|
1024
|
+
return request.operation === "status" ? projectState(result) : result;
|
|
1025
|
+
} finally {
|
|
1026
|
+
if (request.operation === "release") clients.delete(request.client);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
async function serve(server) {
|
|
1030
|
+
for (; ; ) {
|
|
1031
|
+
let message;
|
|
1032
|
+
try {
|
|
1033
|
+
message = await call({ action: "receive", server });
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
if (error.message.includes("updater listener")) throw error;
|
|
1036
|
+
report(error);
|
|
1037
|
+
continue;
|
|
1038
|
+
}
|
|
1039
|
+
if (!message) continue;
|
|
1040
|
+
void handleRequest(message, { dispatch, reply: (response) => call({ action: "reply", connection: message.connection, response }) }).catch(report);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
for (const signal of ["SIGINT", "SIGTERM", "SIGBREAK"]) process.on(signal, () => {
|
|
1044
|
+
updater.stop();
|
|
1045
|
+
});
|
|
1046
|
+
await serve((await call({ action: "server", container: false })).server);
|