@jini-ai/desktop-host 0.7.2 → 0.7.3-next.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.
- package/CHANGELOG.md +1 -1
- package/README.md +3 -1
- package/dist/app-version/index.d.ts +3 -0
- package/dist/app-version/index.js +3 -0
- package/dist/app-version/loader.d.ts +23 -0
- package/dist/app-version/loader.js +38 -0
- package/dist/app-version/store.d.ts +16 -0
- package/dist/app-version/store.js +23 -0
- package/dist/app-version/update-check.d.ts +30 -0
- package/dist/app-version/update-check.js +35 -0
- package/dist/electron/app-version.d.ts +46 -0
- package/dist/electron/app-version.js +38 -0
- package/dist/electron/electron-url-capture.js +40 -37
- package/dist/electron/index.d.ts +1 -0
- package/dist/electron/index.js +1 -0
- package/dist/electron/testing.js +96 -79
- package/dist/electron/updates/auto-update-controller.js +168 -154
- package/dist/electron/updates/instance-presence.js +22 -19
- package/dist/electron/usability/index.d.ts +1 -0
- package/dist/electron/usability/index.js +1 -0
- package/dist/electron/usability/workspace-menu.d.ts +125 -0
- package/dist/electron/usability/workspace-menu.js +71 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/logging.d.ts +8 -1
- package/dist/logging.js +49 -33
- package/dist/preview-scheduler.d.ts +28 -0
- package/dist/preview-scheduler.js +36 -0
- package/dist/protocol.d.ts +2 -1
- package/dist/protocol.js +1 -1
- package/dist/shell.d.ts +1 -1
- package/dist/shell.js +1 -1
- package/dist/shutdown/index.d.ts +1 -0
- package/dist/shutdown/index.js +1 -0
- package/dist/shutdown/last-window-quit.d.ts +125 -0
- package/dist/shutdown/last-window-quit.js +130 -0
- package/dist/sidecar.js +13 -9
- package/package.json +22 -5
package/dist/logging.js
CHANGED
|
@@ -13,29 +13,34 @@
|
|
|
13
13
|
* package should not hardcode that a caller's error taxonomy matches OD's.
|
|
14
14
|
*/
|
|
15
15
|
import { appendFileSync } from 'node:fs';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
import { describeErrorForLog } from '@jini-ai/core/model-facing-tool-errors';
|
|
17
|
+
import { describeLogMeta } from '@jini-ai/core/primitives';
|
|
18
|
+
// The file line and the console echo carry safe metadata only: `describeLogMeta` projects meta at
|
|
19
|
+
// every depth (`error`/`reason` and any Error become class + approved code, opaque objects become
|
|
20
|
+
// fixed words, plain error-shaped objects and payload keys become their type, hostile getters/proxies
|
|
21
|
+
// become `[unreadable]`), and the error bag is described the
|
|
22
|
+
// same way. A caught error's text can quote untrusted input or a credential, and a host's console
|
|
23
|
+
// tee cannot protect this file write. Full detail goes only to an injected `diagnostics` sink.
|
|
24
|
+
/** The meta and error bag of one call, projected once for both the file line and the echo. */
|
|
25
|
+
function projectForLog({ meta, error }, _optional = {}) {
|
|
26
|
+
// Never spread the caller's meta: a hostile getter would throw here, before the host sink runs.
|
|
27
|
+
const safeMeta = meta === undefined ? undefined : describeLogMeta({ meta });
|
|
28
|
+
return error === undefined ? safeMeta : { ...safeMeta, error: describeErrorForLog({ err: error }) };
|
|
20
29
|
}
|
|
21
|
-
// `
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
// than duplicating a defensive (and therefore untestable/dead) check.
|
|
25
|
-
function normalizeMeta(meta) {
|
|
26
|
-
return Object.fromEntries(Object.entries(meta).map(([key, value]) => [key, key === 'error' || key === 'reason' ? normalizeError(value) : value]));
|
|
27
|
-
}
|
|
28
|
-
function serializeMessage(level, message, meta) {
|
|
30
|
+
// `safeMeta` is already projected (plain JSON values only), so the stringify below cannot throw on
|
|
31
|
+
// it; the catch stays for a host that hands a non-string message.
|
|
32
|
+
function serializeMessage({ level, message }, { safeMeta } = {}) {
|
|
29
33
|
const timestamp = new Date().toISOString();
|
|
30
34
|
try {
|
|
31
|
-
return `${JSON.stringify({ level, message, timestamp, ...(
|
|
35
|
+
return `${JSON.stringify({ level, message, timestamp, ...(safeMeta === undefined ? {} : { meta: safeMeta }) })}\n`;
|
|
32
36
|
}
|
|
33
37
|
catch (error) {
|
|
38
|
+
// The serializer's own error is described like any other: its text can name meta content.
|
|
34
39
|
return `${JSON.stringify({
|
|
35
40
|
level,
|
|
36
41
|
message,
|
|
37
42
|
timestamp,
|
|
38
|
-
meta: { serializationError:
|
|
43
|
+
meta: { serializationError: describeErrorForLog({ err: error }) },
|
|
39
44
|
})}\n`;
|
|
40
45
|
}
|
|
41
46
|
}
|
|
@@ -48,30 +53,41 @@ export function appendLogLine({ logPath, line }, { append = ({ path, data, encod
|
|
|
48
53
|
return false;
|
|
49
54
|
}
|
|
50
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* File logger (JSON lines) with an optional console echo. Both carry safe metadata only (see
|
|
58
|
+
* `serializeMessage`). A host that wants the full error injects `diagnostics`: it receives every
|
|
59
|
+
* call unchanged, after the file line; a throwing sink is recorded (class only) and never throws.
|
|
60
|
+
* @complexity O(projected meta, see `describeLogMeta`) per call plus one append.
|
|
61
|
+
*/
|
|
51
62
|
export function createFileLogger({ logPath }, options = {}) {
|
|
52
63
|
const echo = options.echoToConsole ?? true;
|
|
53
64
|
const out = options.console ?? console;
|
|
54
|
-
const
|
|
55
|
-
|
|
65
|
+
const appendOptions = options.append === undefined ? {} : { append: options.append };
|
|
66
|
+
const write = ({ level, message }, { meta, error } = {}) => {
|
|
67
|
+
// exactOptionalPropertyTypes: absent stays absent, so forward only the fields that were given.
|
|
68
|
+
const given = { ...(meta === undefined ? {} : { meta }), ...(error === undefined ? {} : { error }) };
|
|
69
|
+
const safe = projectForLog(given);
|
|
70
|
+
appendLogLine({ logPath, line: serializeMessage({ level, message }, safe === undefined ? {} : { safeMeta: safe }) }, appendOptions);
|
|
71
|
+
if (echo)
|
|
72
|
+
out[level](message, safe === undefined ? '' : safe);
|
|
73
|
+
if (options.diagnostics !== undefined)
|
|
74
|
+
deliverDiagnostics({ diagnostics: options.diagnostics, level, message, ...given }, { write: line => appendLogLine({ logPath, line }, appendOptions) });
|
|
56
75
|
};
|
|
57
|
-
|
|
58
|
-
error({ message }, {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
out.error(message, error === undefined ? meta ?? '' : { ...meta, error });
|
|
62
|
-
},
|
|
63
|
-
info({ message }, { meta, error } = {}) {
|
|
64
|
-
write('info', message, error === undefined ? meta : { ...meta, error });
|
|
65
|
-
if (echo)
|
|
66
|
-
out.info(message, error === undefined ? meta ?? '' : { ...meta, error });
|
|
67
|
-
},
|
|
68
|
-
warn({ message }, { meta, error } = {}) {
|
|
69
|
-
write('warn', message, error === undefined ? meta : { ...meta, error });
|
|
70
|
-
if (echo)
|
|
71
|
-
out.warn(message, error === undefined ? meta ?? '' : { ...meta, error });
|
|
72
|
-
},
|
|
76
|
+
return {
|
|
77
|
+
error: ({ message }, optional = {}) => write({ level: 'error', message }, optional),
|
|
78
|
+
info: ({ message }, optional = {}) => write({ level: 'info', message }, optional),
|
|
79
|
+
warn: ({ message }, optional = {}) => write({ level: 'warn', message }, optional),
|
|
73
80
|
};
|
|
74
|
-
|
|
81
|
+
}
|
|
82
|
+
function deliverDiagnostics({ diagnostics, level, message, meta, error }, { write }) {
|
|
83
|
+
try {
|
|
84
|
+
diagnostics({ level, message, ...(meta === undefined ? {} : { meta }), ...(error === undefined ? {} : { error }) });
|
|
85
|
+
}
|
|
86
|
+
catch (sinkError) {
|
|
87
|
+
// The host sink failed: the file line above already recorded the call; record the sink's own
|
|
88
|
+
// failure (class only) so it is not lost, and never throw from a log call.
|
|
89
|
+
write(serializeMessage({ level: 'warn', message: 'diagnostics sink failed' }, { safeMeta: { error: describeErrorForLog({ err: sinkError }) } }));
|
|
90
|
+
}
|
|
75
91
|
}
|
|
76
92
|
/**
|
|
77
93
|
* Matches the known-harmless undici `setTypeOfService EINVAL` shape (see
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface PreviewSchedulerPorts<Timer, Target> {
|
|
2
|
+
readonly current: (required: {
|
|
3
|
+
key: string;
|
|
4
|
+
}, optional: {}) => Target | undefined;
|
|
5
|
+
readonly capture: (required: Target, optional: {
|
|
6
|
+
isCurrent?: () => boolean;
|
|
7
|
+
}) => Promise<boolean>;
|
|
8
|
+
readonly schedule: (required: {
|
|
9
|
+
work: () => void;
|
|
10
|
+
delayMs: number;
|
|
11
|
+
}, optional: {}) => Timer;
|
|
12
|
+
readonly cancel: (required: {
|
|
13
|
+
timer: Timer;
|
|
14
|
+
}, optional: {}) => void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createPreviewScheduler<Timer, Target>({ ports }: {
|
|
17
|
+
ports: PreviewSchedulerPorts<Timer, Target>;
|
|
18
|
+
}, { debounceMs }?: {
|
|
19
|
+
debounceMs?: number;
|
|
20
|
+
}): {
|
|
21
|
+
schedule(required: {
|
|
22
|
+
key: string;
|
|
23
|
+
}, optional?: {}): void;
|
|
24
|
+
cancel(required: {
|
|
25
|
+
key: string;
|
|
26
|
+
}, optional?: {}): void;
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=preview-scheduler.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { orFallback } from "@jini-ai/core/errors/idioms";
|
|
2
|
+
export function createPreviewScheduler({ ports }, { debounceMs = 1500 } = {}) {
|
|
3
|
+
const attempts = new Map();
|
|
4
|
+
async function fire({ key, attempt }, _optional = {}) {
|
|
5
|
+
if (attempts.get(key) !== attempt)
|
|
6
|
+
return;
|
|
7
|
+
attempt.timer = undefined;
|
|
8
|
+
const target = ports.current({ key }, {});
|
|
9
|
+
if (!target) {
|
|
10
|
+
attempts.delete(key);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
await orFallback({ work: () => ports.capture(target, { isCurrent: () => attempts.get(key) === attempt }), fallback: false, step: "capture preview" }); // A preview is decoration. A failed port must neither fail Start nor reserve a retry.
|
|
14
|
+
// A stop/restart can replace this attempt while capture awaits; its completion owns no
|
|
15
|
+
// state in the replacement lifecycle, whether it succeeded or failed.
|
|
16
|
+
if (attempts.get(key) === attempt)
|
|
17
|
+
attempts.delete(key);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
schedule({ key }, _optional = {}) {
|
|
21
|
+
if (attempts.has(key))
|
|
22
|
+
return;
|
|
23
|
+
const attempt = {};
|
|
24
|
+
// Coalesce one visit's readiness events; the next public visit may refresh even after success.
|
|
25
|
+
attempts.set(key, attempt);
|
|
26
|
+
attempt.timer = ports.schedule({ work: () => { void fire({ key, attempt }); }, delayMs: debounceMs }, {});
|
|
27
|
+
},
|
|
28
|
+
cancel({ key }, _optional = {}) {
|
|
29
|
+
const attempt = attempts.get(key);
|
|
30
|
+
if (attempt?.timer !== undefined)
|
|
31
|
+
ports.cancel({ timer: attempt.timer }, {});
|
|
32
|
+
attempts.delete(key);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=preview-scheduler.js.map
|
package/dist/protocol.d.ts
CHANGED
|
@@ -24,7 +24,8 @@ export type ProtocolFetch = (args: {
|
|
|
24
24
|
export declare function handleProtocolProxyRequest({ request, targetBaseUrl }: {
|
|
25
25
|
request: Request;
|
|
26
26
|
targetBaseUrl: string;
|
|
27
|
-
}, { fetchImpl }?: {
|
|
27
|
+
}, { nativeFetch, fetchImpl }?: {
|
|
28
|
+
nativeFetch?: typeof globalThis.fetch;
|
|
28
29
|
fetchImpl?: ProtocolFetch;
|
|
29
30
|
}): Promise<Response>;
|
|
30
31
|
export interface ProtocolSchemeRegistration {
|
package/dist/protocol.js
CHANGED
|
@@ -32,7 +32,7 @@ export function buildProtocolProxyTargetUrl({ targetBaseUrl, requestUrl }) {
|
|
|
32
32
|
target.hash = incoming.hash;
|
|
33
33
|
return target.toString();
|
|
34
34
|
}
|
|
35
|
-
export async function handleProtocolProxyRequest({ request, targetBaseUrl }, { fetchImpl = ({ request }) =>
|
|
35
|
+
export async function handleProtocolProxyRequest({ request, targetBaseUrl }, { nativeFetch = globalThis.fetch, fetchImpl = ({ request }) => nativeFetch(request) } = {}) {
|
|
36
36
|
const target = buildProtocolProxyTargetUrl({ targetBaseUrl, requestUrl: request.url });
|
|
37
37
|
try {
|
|
38
38
|
return await fetchImpl({ request: new Request(target, request) });
|
package/dist/shell.d.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* desktop/Electron-bridge concept and never will, so this is where the
|
|
17
17
|
* capability actually belongs: the same `ShellPort` a chat UI's
|
|
18
18
|
* `window.__jini__` bridge already reaches for `openExternal`/`openPath`.
|
|
19
|
-
* See `packages/desktop-host/
|
|
19
|
+
* See `070311a07^:packages/desktop-host/source-map.md`'s dated entry for the full
|
|
20
20
|
* per-adapter provenance, including the one deliberate gap:
|
|
21
21
|
* `recentDirs` has no Tauri equivalent and throws `NotImplementedError`
|
|
22
22
|
* there rather than being silently stubbed to an empty array.
|
package/dist/shell.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* desktop/Electron-bridge concept and never will, so this is where the
|
|
17
17
|
* capability actually belongs: the same `ShellPort` a chat UI's
|
|
18
18
|
* `window.__jini__` bridge already reaches for `openExternal`/`openPath`.
|
|
19
|
-
* See `packages/desktop-host/
|
|
19
|
+
* See `070311a07^:packages/desktop-host/source-map.md`'s dated entry for the full
|
|
20
20
|
* per-adapter provenance, including the one deliberate gap:
|
|
21
21
|
* `recentDirs` has no Tauri equivalent and throws `NotImplementedError`
|
|
22
22
|
* there rather than being silently stubbed to an empty array.
|
package/dist/shutdown/index.d.ts
CHANGED
package/dist/shutdown/index.js
CHANGED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The ask before the app's last window closes: "Quit and stop N running sites?".
|
|
3
|
+
* Closing that window with sites running otherwise either stops them with no warning
|
|
4
|
+
* (Windows, Linux) or leaves them running behind a Dock icon (macOS). The host passes the native
|
|
5
|
+
* box, the open servers and `app.quit` in, so every rule here runs under plain node with fakes.
|
|
6
|
+
*
|
|
7
|
+
* **Why Keep running minimizes on Windows and Linux.** macOS keeps an app alive with no windows, so
|
|
8
|
+
* there the window closes and the Dock icon reopens it. Elsewhere a closed last window fires
|
|
9
|
+
* `window-all-closed`, which quits and stops every site, and this app has no tray. A hidden window
|
|
10
|
+
* would leave no way back; a minimized one stays on the taskbar, and closing it asks again, so Quit
|
|
11
|
+
* is always reachable. It also needs no icon, menu or Linux tray support the way a tray would.
|
|
12
|
+
*
|
|
13
|
+
* **Why it latches on `before-quit`, not on `quitPhase`.** Every quit route (Cmd+Q, menu Quit, a
|
|
14
|
+
* signal, Restart to update, this box's own Quit) reaches `before-quit` before any window closes,
|
|
15
|
+
* so none of them may meet this box. `quitPhase` cannot say so: it stays `"idle"` when there was
|
|
16
|
+
* nothing to drain, including while a macOS update is staging, and Electron's `quitAndInstall` then
|
|
17
|
+
* closes the windows BEFORE its own `before-quit`. Holding a close there would abort the update.
|
|
18
|
+
*
|
|
19
|
+
* **Why it keeps its own window set.** `BrowserWindow.getAllWindows()` also lists the hidden capture
|
|
20
|
+
* windows, so "am I the last?" would never be true while one exists.
|
|
21
|
+
* `isVisible()` cannot filter them: on macOS it answers false for a window another one covers.
|
|
22
|
+
*/
|
|
23
|
+
interface LastWindowQuitLabels {
|
|
24
|
+
quitOne: string;
|
|
25
|
+
quitMany: string;
|
|
26
|
+
keepsOne: string;
|
|
27
|
+
keepsMany: string;
|
|
28
|
+
quit: string;
|
|
29
|
+
keepInBackground: string;
|
|
30
|
+
cancel: string;
|
|
31
|
+
}
|
|
32
|
+
interface LastWindowQuitDialogOptions {
|
|
33
|
+
type: "question";
|
|
34
|
+
title: string;
|
|
35
|
+
message: string;
|
|
36
|
+
detail?: string;
|
|
37
|
+
buttons: string[];
|
|
38
|
+
defaultId: number;
|
|
39
|
+
cancelId: number;
|
|
40
|
+
noLink: boolean;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
}
|
|
43
|
+
/** The slice of a `BrowserWindow` this ask touches. */
|
|
44
|
+
interface QuitPromptWindow {
|
|
45
|
+
on(event: "close", listener: (event: {
|
|
46
|
+
preventDefault(): void;
|
|
47
|
+
}) => void): unknown;
|
|
48
|
+
on(event: "closed", listener: () => void): unknown;
|
|
49
|
+
close(): void;
|
|
50
|
+
minimize(): void;
|
|
51
|
+
}
|
|
52
|
+
/** The slice of a child process handle counted here: `external` marks one this app only attached to. */
|
|
53
|
+
interface CountedServer {
|
|
54
|
+
external?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/** Everything the ask needs from the host. */
|
|
57
|
+
interface LastWindowQuitPorts<W> {
|
|
58
|
+
/** Every server `before-quit`'s drain would call `stop()` on. */
|
|
59
|
+
openChildren: () => readonly CountedServer[];
|
|
60
|
+
/** `process.platform`. */
|
|
61
|
+
platform: string;
|
|
62
|
+
/** Dialog copy, read when the box opens. */
|
|
63
|
+
dialogCopy: () => {
|
|
64
|
+
title: string;
|
|
65
|
+
labels: LastWindowQuitLabels;
|
|
66
|
+
};
|
|
67
|
+
/** `dialog.showMessageBox`, parented so it is a sheet on macOS and modal to the window on Windows. */
|
|
68
|
+
showMessageBox: (required: {
|
|
69
|
+
window: W;
|
|
70
|
+
options: LastWindowQuitDialogOptions;
|
|
71
|
+
}) => Promise<{
|
|
72
|
+
response: number;
|
|
73
|
+
}>;
|
|
74
|
+
/** `app.quit()`: the same graceful drain Cmd+Q runs. */
|
|
75
|
+
quit: () => void;
|
|
76
|
+
/** Keep running in background: idle sleep must not stop these sites later. */
|
|
77
|
+
keepChildrenRunning: () => void;
|
|
78
|
+
reportError: (required: {
|
|
79
|
+
error: unknown;
|
|
80
|
+
}) => void;
|
|
81
|
+
}
|
|
82
|
+
/** The ask, as the host uses it. */
|
|
83
|
+
interface LastWindowQuitPrompt<W> {
|
|
84
|
+
/** Count this window as open and ask before it closes last. Only app windows belong here. */
|
|
85
|
+
watch(required: {
|
|
86
|
+
window: W;
|
|
87
|
+
}): void;
|
|
88
|
+
/** How many app windows are open. Capture windows are not counted, so a Dock click can reopen the app while one exists. */
|
|
89
|
+
openCount(): number;
|
|
90
|
+
/** A quit has started: dismiss an open box, and never ask again. Idempotent. */
|
|
91
|
+
onQuitStarted(): void;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* What a quit does to the open servers: stopping an attached one only ends this app's watch on it
|
|
95
|
+
* so that site keeps running by design.
|
|
96
|
+
* @returns How many sites stop, and how many keep running. @complexity O(n) open servers.
|
|
97
|
+
*/
|
|
98
|
+
declare function quitChildCounts({ children }: {
|
|
99
|
+
children: readonly CountedServer[];
|
|
100
|
+
}, _optional?: {}): {
|
|
101
|
+
stops: number;
|
|
102
|
+
keeps: number;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* The native box. `stops` and `keeps` are the two {@link quitChildCounts} numbers.
|
|
106
|
+
* @returns The box's options, with the supplied copy. @complexity O(1).
|
|
107
|
+
*/
|
|
108
|
+
declare function lastWindowQuitDialog({ title, labels: copy, stops, keeps }: {
|
|
109
|
+
title: string;
|
|
110
|
+
labels: LastWindowQuitLabels;
|
|
111
|
+
stops: number;
|
|
112
|
+
keeps: number;
|
|
113
|
+
}, _optional?: {}): LastWindowQuitDialogOptions;
|
|
114
|
+
/**
|
|
115
|
+
* Binds the ask to its ports. A close is held first and answered later, because `close` can only be
|
|
116
|
+
* prevented synchronously; Quit then runs the ordinary quit, so nothing here stops a site, and
|
|
117
|
+
* Keep running leaves them all as they are.
|
|
118
|
+
* @returns The ask. @complexity O(1) per close, plus O(n) open servers when it asks.
|
|
119
|
+
*/
|
|
120
|
+
declare function createLastWindowQuitPrompt<W extends QuitPromptWindow>({ ports }: {
|
|
121
|
+
ports: LastWindowQuitPorts<W>;
|
|
122
|
+
}, _optional?: {}): LastWindowQuitPrompt<W>;
|
|
123
|
+
export { createLastWindowQuitPrompt, lastWindowQuitDialog, quitChildCounts };
|
|
124
|
+
export type { LastWindowQuitPorts, LastWindowQuitPrompt, QuitPromptWindow, LastWindowQuitLabels, LastWindowQuitDialogOptions };
|
|
125
|
+
//# sourceMappingURL=last-window-quit.d.ts.map
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The ask before the app's last window closes: "Quit and stop N running sites?".
|
|
3
|
+
* Closing that window with sites running otherwise either stops them with no warning
|
|
4
|
+
* (Windows, Linux) or leaves them running behind a Dock icon (macOS). The host passes the native
|
|
5
|
+
* box, the open servers and `app.quit` in, so every rule here runs under plain node with fakes.
|
|
6
|
+
*
|
|
7
|
+
* **Why Keep running minimizes on Windows and Linux.** macOS keeps an app alive with no windows, so
|
|
8
|
+
* there the window closes and the Dock icon reopens it. Elsewhere a closed last window fires
|
|
9
|
+
* `window-all-closed`, which quits and stops every site, and this app has no tray. A hidden window
|
|
10
|
+
* would leave no way back; a minimized one stays on the taskbar, and closing it asks again, so Quit
|
|
11
|
+
* is always reachable. It also needs no icon, menu or Linux tray support the way a tray would.
|
|
12
|
+
*
|
|
13
|
+
* **Why it latches on `before-quit`, not on `quitPhase`.** Every quit route (Cmd+Q, menu Quit, a
|
|
14
|
+
* signal, Restart to update, this box's own Quit) reaches `before-quit` before any window closes,
|
|
15
|
+
* so none of them may meet this box. `quitPhase` cannot say so: it stays `"idle"` when there was
|
|
16
|
+
* nothing to drain, including while a macOS update is staging, and Electron's `quitAndInstall` then
|
|
17
|
+
* closes the windows BEFORE its own `before-quit`. Holding a close there would abort the update.
|
|
18
|
+
*
|
|
19
|
+
* **Why it keeps its own window set.** `BrowserWindow.getAllWindows()` also lists the hidden capture
|
|
20
|
+
* windows, so "am I the last?" would never be true while one exists.
|
|
21
|
+
* `isVisible()` cannot filter them: on macOS it answers false for a window another one covers.
|
|
22
|
+
*/
|
|
23
|
+
const QUIT_BUTTON = 0;
|
|
24
|
+
const KEEP_RUNNING_BUTTON = 1;
|
|
25
|
+
const CANCEL_BUTTON = 2;
|
|
26
|
+
/**
|
|
27
|
+
* What a quit does to the open servers: stopping an attached one only ends this app's watch on it
|
|
28
|
+
* so that site keeps running by design.
|
|
29
|
+
* @returns How many sites stop, and how many keep running. @complexity O(n) open servers.
|
|
30
|
+
*/
|
|
31
|
+
function quitChildCounts({ children }, _optional = {}) {
|
|
32
|
+
const keeps = children.filter((server) => server.external).length;
|
|
33
|
+
return { stops: children.length - keeps, keeps };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The native box. `stops` and `keeps` are the two {@link quitChildCounts} numbers.
|
|
37
|
+
* @returns The box's options, with the supplied copy. @complexity O(1).
|
|
38
|
+
*/
|
|
39
|
+
function lastWindowQuitDialog({ title, labels: copy, stops, keeps }, _optional = {}) {
|
|
40
|
+
return {
|
|
41
|
+
type: "question",
|
|
42
|
+
title,
|
|
43
|
+
message: (stops === 1 ? copy.quitOne : copy.quitMany).replace("{count}", () => String(stops)),
|
|
44
|
+
...(keeps > 0 ? { detail: (keeps === 1 ? copy.keepsOne : copy.keepsMany).replace("{count}", () => String(keeps)) } : {}),
|
|
45
|
+
buttons: [copy.quit, copy.keepInBackground, copy.cancel],
|
|
46
|
+
defaultId: QUIT_BUTTON,
|
|
47
|
+
cancelId: CANCEL_BUTTON,
|
|
48
|
+
// Windows would otherwise turn "Quit", which it does not recognise as a common button, into a
|
|
49
|
+
// large command link above a lone Cancel. Ignored on macOS.
|
|
50
|
+
noLink: true,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Binds the ask to its ports. A close is held first and answered later, because `close` can only be
|
|
55
|
+
* prevented synchronously; Quit then runs the ordinary quit, so nothing here stops a site, and
|
|
56
|
+
* Keep running leaves them all as they are.
|
|
57
|
+
* @returns The ask. @complexity O(1) per close, plus O(n) open servers when it asks.
|
|
58
|
+
*/
|
|
59
|
+
function createLastWindowQuitPrompt({ ports }, _optional = {}) {
|
|
60
|
+
const windows = new Set();
|
|
61
|
+
// One-shot: a page's `beforeunload` can veto the close Keep running starts, and the next must ask.
|
|
62
|
+
const released = new WeakSet();
|
|
63
|
+
const state = {
|
|
64
|
+
quitStarted: false,
|
|
65
|
+
openBox: null,
|
|
66
|
+
};
|
|
67
|
+
const keepRunning = createBackgroundWindowKeeper({ ports, released });
|
|
68
|
+
const ask = createAsk({ state, ports, keepRunning });
|
|
69
|
+
function onClose({ window, event }) {
|
|
70
|
+
if (state.quitStarted || released.delete(window))
|
|
71
|
+
return;
|
|
72
|
+
// While the box is open the answer decides; the close that opened it is already held.
|
|
73
|
+
if (state.openBox) {
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (windows.size > 1)
|
|
78
|
+
return;
|
|
79
|
+
const { stops, keeps } = quitChildCounts({ children: ports.openChildren() });
|
|
80
|
+
if (stops === 0)
|
|
81
|
+
return;
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
ask({ window, stops, keeps });
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
openCount: () => windows.size,
|
|
87
|
+
watch({ window }) {
|
|
88
|
+
windows.add(window);
|
|
89
|
+
window.on("close", (event) => onClose({ window, event }));
|
|
90
|
+
// `closed`, not `close`: a page's `beforeunload` can still veto a close that passed here.
|
|
91
|
+
window.on("closed", () => windows.delete(window));
|
|
92
|
+
},
|
|
93
|
+
onQuitStarted() {
|
|
94
|
+
state.quitStarted = true;
|
|
95
|
+
state.openBox?.abort();
|
|
96
|
+
state.openBox = null;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function createAsk({ state, ports, keepRunning }) {
|
|
101
|
+
return function ask({ window, stops, keeps }) {
|
|
102
|
+
const box = new AbortController();
|
|
103
|
+
state.openBox = box;
|
|
104
|
+
ports.showMessageBox({ window, options: { ...lastWindowQuitDialog({ ...ports.dialogCopy(), stops, keeps }), signal: box.signal } })
|
|
105
|
+
.then(({ response }) => {
|
|
106
|
+
state.openBox = null;
|
|
107
|
+
if (response === QUIT_BUTTON)
|
|
108
|
+
ports.quit();
|
|
109
|
+
if (response === KEEP_RUNNING_BUTTON)
|
|
110
|
+
keepRunning({ window });
|
|
111
|
+
})
|
|
112
|
+
.catch((error) => {
|
|
113
|
+
state.openBox = null;
|
|
114
|
+
ports.reportError({ error });
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function createBackgroundWindowKeeper({ ports, released }) {
|
|
119
|
+
return function keepRunning({ window }) {
|
|
120
|
+
ports.keepChildrenRunning();
|
|
121
|
+
if (ports.platform !== "darwin") {
|
|
122
|
+
window.minimize();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
released.add(window);
|
|
126
|
+
window.close();
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
export { createLastWindowQuitPrompt, lastWindowQuitDialog, quitChildCounts };
|
|
130
|
+
//# sourceMappingURL=last-window-quit.js.map
|
package/dist/sidecar.js
CHANGED
|
@@ -50,15 +50,7 @@ async function waitUntilReady(child, logPath, options) {
|
|
|
50
50
|
throw exitError(child.exitCode, child.signalCode);
|
|
51
51
|
if (timeoutMs <= 0)
|
|
52
52
|
throw timeoutError();
|
|
53
|
-
|
|
54
|
-
const exited = new Promise((_resolve, reject) => {
|
|
55
|
-
onExit = (code, signal) => reject(exitError(code, signal));
|
|
56
|
-
child.once('exit', onExit);
|
|
57
|
-
});
|
|
58
|
-
let timer;
|
|
59
|
-
const deadline = new Promise((_resolve, reject) => {
|
|
60
|
-
timer = setTimeout(() => reject(timeoutError()), timeoutMs);
|
|
61
|
-
});
|
|
53
|
+
const { exited, deadline, timer, onExit } = createReadyWaiters(child, timeoutMs, { exitError, timeoutError });
|
|
62
54
|
let stopped = false;
|
|
63
55
|
const controller = new AbortController();
|
|
64
56
|
const poll = async () => {
|
|
@@ -87,6 +79,18 @@ async function waitUntilReady(child, logPath, options) {
|
|
|
87
79
|
child.off('exit', onExit);
|
|
88
80
|
}
|
|
89
81
|
}
|
|
82
|
+
function createReadyWaiters(child, timeoutMs, { exitError, timeoutError }) {
|
|
83
|
+
let onExit;
|
|
84
|
+
const exited = new Promise((_resolve, reject) => {
|
|
85
|
+
onExit = (code, signal) => reject(exitError(code, signal));
|
|
86
|
+
child.once('exit', onExit);
|
|
87
|
+
});
|
|
88
|
+
let timer;
|
|
89
|
+
const deadline = new Promise((_resolve, reject) => {
|
|
90
|
+
timer = setTimeout(() => reject(timeoutError()), timeoutMs);
|
|
91
|
+
});
|
|
92
|
+
return { exited, deadline, timer, onExit };
|
|
93
|
+
}
|
|
90
94
|
/** Use the host's shutdown request or SIGTERM, then bound the wait before SIGKILL. */
|
|
91
95
|
async function shutdownChild({ child }, options = {}) {
|
|
92
96
|
if (child.exitCode !== null || child.signalCode !== null)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/desktop-host",
|
|
3
|
-
"version": "0.7.2",
|
|
3
|
+
"version": "0.7.3-next.2",
|
|
4
4
|
"description": "Shell-agnostic desktop-host ports: single-instance lock, window lifecycle, protocol handling, sidecar launch, paths, and config. Electron and Tauri assemblies live behind ./electron and ./tauri.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"entries": {
|
|
16
16
|
".": "desktop",
|
|
17
17
|
"./bridge-testing": "universal",
|
|
18
|
+
"./app-version": "universal",
|
|
18
19
|
"./render-bridge": "node",
|
|
19
20
|
"./electron": "desktop",
|
|
20
21
|
"./tauri": "desktop",
|
|
@@ -25,7 +26,13 @@
|
|
|
25
26
|
"./electron/usability": "desktop",
|
|
26
27
|
"./speech": "desktop",
|
|
27
28
|
"./speech/macos": "node",
|
|
28
|
-
"./speech/macos/speech-helper.swift": "node"
|
|
29
|
+
"./speech/macos/speech-helper.swift": "node",
|
|
30
|
+
"./bridge": "universal"
|
|
31
|
+
},
|
|
32
|
+
"isolation": {
|
|
33
|
+
"entries": {
|
|
34
|
+
"./bridge-testing": "bridge"
|
|
35
|
+
}
|
|
29
36
|
}
|
|
30
37
|
},
|
|
31
38
|
"type": "module",
|
|
@@ -43,11 +50,21 @@
|
|
|
43
50
|
"import": "./dist/index.js",
|
|
44
51
|
"default": "./dist/index.js"
|
|
45
52
|
},
|
|
53
|
+
"./bridge": {
|
|
54
|
+
"types": "./dist/bridge.d.ts",
|
|
55
|
+
"import": "./dist/bridge.js",
|
|
56
|
+
"default": "./dist/bridge.js"
|
|
57
|
+
},
|
|
46
58
|
"./bridge-testing": {
|
|
47
59
|
"types": "./dist/bridge-testing.d.ts",
|
|
48
60
|
"import": "./dist/bridge-testing.js",
|
|
49
61
|
"default": "./dist/bridge-testing.js"
|
|
50
62
|
},
|
|
63
|
+
"./app-version": {
|
|
64
|
+
"types": "./dist/app-version/index.d.ts",
|
|
65
|
+
"import": "./dist/app-version/index.js",
|
|
66
|
+
"default": "./dist/app-version/index.js"
|
|
67
|
+
},
|
|
51
68
|
"./electron": {
|
|
52
69
|
"types": "./dist/electron/index.d.ts",
|
|
53
70
|
"import": "./dist/electron/index.js",
|
|
@@ -112,11 +129,11 @@
|
|
|
112
129
|
"registry": "https://registry.npmjs.org"
|
|
113
130
|
},
|
|
114
131
|
"devDependencies": {
|
|
115
|
-
"@vitest/coverage-v8": "^2.
|
|
116
|
-
"@jini-ai/core": "^0.7.2"
|
|
132
|
+
"@vitest/coverage-v8": "^3.2.6",
|
|
133
|
+
"@jini-ai/core": "^0.7.3-next.2"
|
|
117
134
|
},
|
|
118
135
|
"dependencies": {
|
|
119
|
-
"@jini-ai/core": "^0.7.2"
|
|
136
|
+
"@jini-ai/core": "^0.7.3-next.2"
|
|
120
137
|
},
|
|
121
138
|
"peerDependencies": {},
|
|
122
139
|
"peerDependenciesMeta": {},
|