@jetpack-desktop/client 0.1.2-rc.1 → 0.1.3-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -9
- package/dist/client.d.ts +48 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +395 -49
- package/dist/client.js.map +1 -1
- package/dist/control-bar.d.ts +56 -9
- package/dist/control-bar.d.ts.map +1 -1
- package/dist/control-bar.js +932 -75
- package/dist/control-bar.js.map +1 -1
- package/dist/download-url.d.ts +16 -0
- package/dist/download-url.d.ts.map +1 -0
- package/dist/download-url.js +18 -0
- package/dist/download-url.js.map +1 -0
- package/dist/i18n.d.ts +59 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +192 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/log-terminal.d.ts +8 -0
- package/dist/log-terminal.d.ts.map +1 -1
- package/dist/log-terminal.js +157 -17
- package/dist/log-terminal.js.map +1 -1
- package/dist/types.d.ts +109 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/wasm/jetpack_wasm.d.ts +18 -6
- package/wasm/jetpack_wasm.js +56 -23
- package/wasm/jetpack_wasm_bg.wasm +0 -0
- package/wasm/jetpack_wasm_bg.wasm.d.ts +9 -6
package/dist/client.js
CHANGED
|
@@ -1,11 +1,179 @@
|
|
|
1
1
|
import { JetPackControlBar } from './control-bar.js';
|
|
2
|
+
import { DEFAULT_JETPACK_DOWNLOAD_URL, resolveJetPackDownloadUrl } from './download-url.js';
|
|
3
|
+
import { normalizeLanguageTag, resolveLanguage } from './i18n.js';
|
|
2
4
|
import { JetPackLogTerminal } from './log-terminal.js';
|
|
3
5
|
import { JetPackClientError, } from './types.js';
|
|
4
6
|
const DEFAULT_DAEMON_PORT = 9470;
|
|
5
7
|
const DEFAULT_HEALTH_PROBE_TIMEOUT_MS = 2000;
|
|
8
|
+
const DEFAULT_DAEMON_POLL_INTERVAL_MS = 2000;
|
|
6
9
|
const DEFAULT_PAIRING_POLL_MS = 1500;
|
|
7
10
|
const DEFAULT_PAIRING_TIMEOUT_MS = 60_000;
|
|
8
|
-
export
|
|
11
|
+
export { DEFAULT_JETPACK_DOWNLOAD_URL };
|
|
12
|
+
const DEV_IDENTIFIER_PATTERN = /^[a-z0-9][a-z0-9._-]{0,126}[a-z0-9]$/;
|
|
13
|
+
function sanitizeDevSlugComponent(raw) {
|
|
14
|
+
let out = '';
|
|
15
|
+
let prevDash = false;
|
|
16
|
+
for (const ch of raw.toLowerCase()) {
|
|
17
|
+
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
|
|
18
|
+
out += ch;
|
|
19
|
+
prevDash = false;
|
|
20
|
+
}
|
|
21
|
+
else if (out.length > 0 && !prevDash) {
|
|
22
|
+
out += '-';
|
|
23
|
+
prevDash = true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
let trimmed = out.replace(/^-+|-+$/g, '');
|
|
27
|
+
if (trimmed.length > 64) {
|
|
28
|
+
trimmed = trimmed.slice(0, 64).replace(/-+$/g, '');
|
|
29
|
+
}
|
|
30
|
+
return trimmed;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Derives a safe local development `appId` in the `com.dev.<slug>` namespace
|
|
34
|
+
* from a production reverse-DNS identifier.
|
|
35
|
+
*
|
|
36
|
+
* Identifiers already starting with `com.dev.` (and containing valid characters)
|
|
37
|
+
* are preserved unchanged.
|
|
38
|
+
*/
|
|
39
|
+
export function deriveDevAppId(prodAppId) {
|
|
40
|
+
const trimmed = String(prodAppId ?? '').trim().toLowerCase();
|
|
41
|
+
if (trimmed.startsWith('com.dev.') &&
|
|
42
|
+
trimmed.length > 'com.dev.'.length &&
|
|
43
|
+
!trimmed.includes('..') &&
|
|
44
|
+
DEV_IDENTIFIER_PATTERN.test(trimmed)) {
|
|
45
|
+
return trimmed;
|
|
46
|
+
}
|
|
47
|
+
const segments = trimmed.split('.').filter((segment) => segment.length > 0);
|
|
48
|
+
const lastSegment = segments.length > 0 ? segments[segments.length - 1] : trimmed;
|
|
49
|
+
const slugFromLast = sanitizeDevSlugComponent(lastSegment);
|
|
50
|
+
let slug;
|
|
51
|
+
if (slugFromLast.length >= 2) {
|
|
52
|
+
slug = slugFromLast;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const fullSlug = sanitizeDevSlugComponent(trimmed);
|
|
56
|
+
slug = fullSlug.length >= 2 ? fullSlug : 'app';
|
|
57
|
+
}
|
|
58
|
+
return `com.dev.${slug}`;
|
|
59
|
+
}
|
|
60
|
+
function parseEnvironmentSignal(raw) {
|
|
61
|
+
if (typeof raw !== 'string') {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const normalized = raw.trim().toLowerCase();
|
|
65
|
+
if (normalized === 'dev' || normalized === 'development') {
|
|
66
|
+
return 'dev';
|
|
67
|
+
}
|
|
68
|
+
if (normalized === 'prod' || normalized === 'production') {
|
|
69
|
+
return 'prod';
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
function detectRuntimeEnvironment() {
|
|
74
|
+
const metaEnv = import.meta.env;
|
|
75
|
+
const procEnv = typeof globalThis !== 'undefined'
|
|
76
|
+
? globalThis.process?.env
|
|
77
|
+
: undefined;
|
|
78
|
+
const viteJetPackEnv = parseEnvironmentSignal(metaEnv?.VITE_JETPACK_ENV);
|
|
79
|
+
if (viteJetPackEnv) {
|
|
80
|
+
return viteJetPackEnv;
|
|
81
|
+
}
|
|
82
|
+
const procJetPackEnv = parseEnvironmentSignal(procEnv?.JETPACK_ENV);
|
|
83
|
+
if (procJetPackEnv) {
|
|
84
|
+
return procJetPackEnv;
|
|
85
|
+
}
|
|
86
|
+
if (metaEnv?.DEV === true && metaEnv?.PROD !== true) {
|
|
87
|
+
return 'dev';
|
|
88
|
+
}
|
|
89
|
+
if (metaEnv?.PROD === true) {
|
|
90
|
+
return 'prod';
|
|
91
|
+
}
|
|
92
|
+
const nodeEnv = parseEnvironmentSignal(procEnv?.NODE_ENV);
|
|
93
|
+
if (nodeEnv) {
|
|
94
|
+
return nodeEnv;
|
|
95
|
+
}
|
|
96
|
+
return 'prod';
|
|
97
|
+
}
|
|
98
|
+
function mergeObjectOrFlag(baseVal, overrideVal) {
|
|
99
|
+
if (overrideVal === undefined) {
|
|
100
|
+
return baseVal;
|
|
101
|
+
}
|
|
102
|
+
if (typeof overrideVal === 'boolean') {
|
|
103
|
+
return overrideVal;
|
|
104
|
+
}
|
|
105
|
+
if (typeof baseVal === 'object' && baseVal !== null) {
|
|
106
|
+
return { ...baseVal, ...overrideVal };
|
|
107
|
+
}
|
|
108
|
+
return { ...overrideVal };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Resolves `ConnectAndEnsureAppOptions` for either `'dev'` or `'prod'`, merging
|
|
112
|
+
* `environments[resolvedEnv]` overrides and automatically deriving `com.dev.<slug>`
|
|
113
|
+
* when `env` is `'dev'` and `environments.dev.appId` is omitted.
|
|
114
|
+
*
|
|
115
|
+
* When neither `env` nor `environments` is passed, preserves 100% backwards-compatible
|
|
116
|
+
* single-`appId` behavior verbatim (with `env: 'prod'`).
|
|
117
|
+
*/
|
|
118
|
+
export function resolveEnvironmentOptions(options) {
|
|
119
|
+
if (!options || typeof options !== 'object') {
|
|
120
|
+
throw new JetPackClientError('INVALID_OPTIONS', 'connectAndEnsureApp requires an options object');
|
|
121
|
+
}
|
|
122
|
+
const hasEnvConfig = options.env !== undefined || options.environments !== undefined;
|
|
123
|
+
if (!hasEnvConfig) {
|
|
124
|
+
const rawAppId = typeof options.appId === 'string' ? options.appId.trim() : '';
|
|
125
|
+
if (!rawAppId) {
|
|
126
|
+
throw new JetPackClientError('INVALID_OPTIONS', 'connectAndEnsureApp requires a non-empty appId');
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
...options,
|
|
130
|
+
appId: rawAppId,
|
|
131
|
+
env: 'prod',
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
let resolvedEnv;
|
|
135
|
+
if (options.env === 'dev' || options.env === 'prod') {
|
|
136
|
+
resolvedEnv = options.env;
|
|
137
|
+
}
|
|
138
|
+
else if (options.env === 'auto' || options.env === undefined) {
|
|
139
|
+
resolvedEnv = detectRuntimeEnvironment();
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
throw new JetPackClientError('INVALID_OPTIONS', `Unsupported environment '${String(options.env)}'; expected 'dev', 'prod', or 'auto'`);
|
|
143
|
+
}
|
|
144
|
+
const envOverride = options.environments?.[resolvedEnv] ?? {};
|
|
145
|
+
const explicitEnvAppId = typeof envOverride.appId === 'string' ? envOverride.appId.trim() : '';
|
|
146
|
+
let resolvedAppId = explicitEnvAppId;
|
|
147
|
+
if (!resolvedAppId) {
|
|
148
|
+
const baseAppId = typeof options.appId === 'string' && options.appId.trim().length > 0
|
|
149
|
+
? options.appId.trim()
|
|
150
|
+
: resolvedEnv === 'dev' && typeof options.environments?.prod?.appId === 'string'
|
|
151
|
+
? options.environments.prod.appId.trim()
|
|
152
|
+
: '';
|
|
153
|
+
if (baseAppId) {
|
|
154
|
+
resolvedAppId = resolvedEnv === 'dev' ? deriveDevAppId(baseAppId) : baseAppId;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!resolvedAppId) {
|
|
158
|
+
throw new JetPackClientError('INVALID_OPTIONS', `connectAndEnsureApp requires a non-empty appId for '${resolvedEnv}' environment`);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
...options,
|
|
162
|
+
appId: resolvedAppId,
|
|
163
|
+
env: resolvedEnv,
|
|
164
|
+
daemonPort: envOverride.daemonPort ?? options.daemonPort,
|
|
165
|
+
wsUrl: envOverride.wsUrl ?? options.wsUrl,
|
|
166
|
+
origin: envOverride.origin ?? options.origin,
|
|
167
|
+
ociReference: envOverride.ociReference ?? options.ociReference,
|
|
168
|
+
autoPair: envOverride.autoPair ?? options.autoPair,
|
|
169
|
+
resetVolumes: envOverride.resetVolumes ?? options.resetVolumes,
|
|
170
|
+
downloadUrl: envOverride.downloadUrl ?? options.downloadUrl,
|
|
171
|
+
posterUrl: envOverride.posterUrl ?? options.posterUrl,
|
|
172
|
+
installBanner: mergeObjectOrFlag(options.installBanner, envOverride.installBanner),
|
|
173
|
+
controlBar: mergeObjectOrFlag(options.controlBar, envOverride.controlBar),
|
|
174
|
+
logTerminal: mergeObjectOrFlag(options.logTerminal, envOverride.logTerminal),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
9
177
|
function delay(ms) {
|
|
10
178
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
179
|
}
|
|
@@ -15,33 +183,31 @@ function isNoActiveWorkerError(err) {
|
|
|
15
183
|
msg.includes('Worker job not found') ||
|
|
16
184
|
msg.includes('Worker IPC channel closed'));
|
|
17
185
|
}
|
|
18
|
-
function formatStopDetail(reason) {
|
|
186
|
+
function formatStopDetail(reason, language = 'en') {
|
|
187
|
+
const isUk = normalizeLanguageTag(language) === 'uk';
|
|
19
188
|
switch (reason) {
|
|
20
189
|
case 'idle_timeout':
|
|
21
|
-
return
|
|
190
|
+
return isUk
|
|
191
|
+
? 'Таймаут простою — виконайте дію або натисніть «Запустити»'
|
|
192
|
+
: 'Idle timeout — perform an action or click Launch';
|
|
22
193
|
case 'process_exited':
|
|
23
|
-
return 'Worker exited unexpectedly';
|
|
194
|
+
return isUk ? 'Воркер несподівано завершив роботу' : 'Worker exited unexpectedly';
|
|
24
195
|
case 'terminated':
|
|
25
196
|
default:
|
|
26
|
-
return 'Stopped';
|
|
197
|
+
return isUk ? 'Зупинено' : 'Stopped';
|
|
27
198
|
}
|
|
28
199
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
// Fall through to default download URL
|
|
41
|
-
}
|
|
42
|
-
}
|
|
200
|
+
function formatDaemonConnectionLost(language = 'en') {
|
|
201
|
+
return normalizeLanguageTag(language) === 'uk'
|
|
202
|
+
? 'Втрачено зʼєднання з демоном JetPack'
|
|
203
|
+
: 'Connection to JetPack daemon lost';
|
|
204
|
+
}
|
|
205
|
+
function buildDefaultControlCenterUrl(daemonPort, language = 'en') {
|
|
206
|
+
const normalized = normalizeLanguageTag(language) ?? 'en';
|
|
207
|
+
if (normalized === 'en') {
|
|
208
|
+
return `http://127.0.0.1:${daemonPort}/#apps`;
|
|
43
209
|
}
|
|
44
|
-
return
|
|
210
|
+
return `http://127.0.0.1:${daemonPort}/?lang=${encodeURIComponent(normalized)}#apps`;
|
|
45
211
|
}
|
|
46
212
|
function resolveHealthProbeUrl(wsUrl, daemonPort) {
|
|
47
213
|
try {
|
|
@@ -150,6 +316,7 @@ export class JetPackAppClient {
|
|
|
150
316
|
wasmClient = null;
|
|
151
317
|
identity = null;
|
|
152
318
|
appId = null;
|
|
319
|
+
env = null;
|
|
153
320
|
lastPairingUri = null;
|
|
154
321
|
closed = false;
|
|
155
322
|
workerRunning = false;
|
|
@@ -161,9 +328,16 @@ export class JetPackAppClient {
|
|
|
161
328
|
streamListeners = new Map();
|
|
162
329
|
workerStoppedListeners = new Set();
|
|
163
330
|
workerStartedListeners = new Set();
|
|
331
|
+
languageListeners = new Set();
|
|
332
|
+
currentLanguage = 'en';
|
|
333
|
+
hasCustomControlCenterUrl = false;
|
|
334
|
+
activeDaemonPort = DEFAULT_DAEMON_PORT;
|
|
164
335
|
inFlightRequests = new Set();
|
|
165
336
|
controlBar = null;
|
|
166
337
|
logTerminal = null;
|
|
338
|
+
daemonPollTimer = null;
|
|
339
|
+
daemonPollGeneration = 0;
|
|
340
|
+
lastConnectOptions = null;
|
|
167
341
|
/** Returns `true` when an authenticated daemon session is open and bound to an app. */
|
|
168
342
|
isConnected() {
|
|
169
343
|
return !this.closed && this.wasmClient !== null && this.wasmClient.is_connected() && this.appId !== null;
|
|
@@ -180,6 +354,14 @@ export class JetPackAppClient {
|
|
|
180
354
|
getAppId() {
|
|
181
355
|
return this.appId;
|
|
182
356
|
}
|
|
357
|
+
/** Returns the resolved environment (`'dev'` or `'prod'`) while connected, or `null` before connect / after `close()`. */
|
|
358
|
+
getEnvironment() {
|
|
359
|
+
return this.env;
|
|
360
|
+
}
|
|
361
|
+
/** Returns the resolved UI language tag (BCP-47 lowercase primary subtag, defaulting to `'en'`). */
|
|
362
|
+
getLanguage() {
|
|
363
|
+
return this.currentLanguage;
|
|
364
|
+
}
|
|
183
365
|
/** Returns the active `WasmClientIdentity` instance, if initialized. */
|
|
184
366
|
getIdentity() {
|
|
185
367
|
return this.identity;
|
|
@@ -200,6 +382,16 @@ export class JetPackAppClient {
|
|
|
200
382
|
getLogTerminal() {
|
|
201
383
|
return this.logTerminal;
|
|
202
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* Registers a callback invoked when the resolved language updates during `connectAndEnsureApp()`
|
|
387
|
+
* (e.g., from `?lang=` or `DaemonSettings.language`). Returns an unsubscribe closure.
|
|
388
|
+
*/
|
|
389
|
+
onLanguageChanged(callback) {
|
|
390
|
+
this.languageListeners.add(callback);
|
|
391
|
+
return () => {
|
|
392
|
+
this.languageListeners.delete(callback);
|
|
393
|
+
};
|
|
394
|
+
}
|
|
203
395
|
/**
|
|
204
396
|
* Registers a callback invoked when the daemon reports `"app.worker.stopped"` for this app
|
|
205
397
|
* (e.g. idle timeout, manual Stop, or unexpected process exit). Returns an unsubscribe closure.
|
|
@@ -212,7 +404,7 @@ export class JetPackAppClient {
|
|
|
212
404
|
}
|
|
213
405
|
/**
|
|
214
406
|
* Registers a callback invoked when `ensureWorkerRunning()` successfully launches or wakes
|
|
215
|
-
* the worker for this app (including via the `JetPackControlBar` `"
|
|
407
|
+
* the worker for this app (including via the `JetPackControlBar` `"Launch"` button or
|
|
216
408
|
* transparent `request()` auto-wake). Returns an unsubscribe closure.
|
|
217
409
|
*/
|
|
218
410
|
onWorkerStarted(callback) {
|
|
@@ -221,6 +413,21 @@ export class JetPackAppClient {
|
|
|
221
413
|
this.workerStartedListeners.delete(callback);
|
|
222
414
|
};
|
|
223
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Handles clicks on the unified `JetPackControlBar` `"Launch"` button in `'Stopped'` state:
|
|
418
|
+
* wakes the worker over the active session if connected, or transparently reconnects via
|
|
419
|
+
* `connectAndEnsureApp(lastConnectOptions)` when disconnected or closed via `close()`.
|
|
420
|
+
*/
|
|
421
|
+
async launchOrReconnectFromControlBar() {
|
|
422
|
+
if (!this.closed && this.wasmClient?.is_connected() && this.appId) {
|
|
423
|
+
await this.ensureWorkerRunning();
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (!this.lastConnectOptions) {
|
|
427
|
+
throw new JetPackClientError('NOT_CONNECTED', 'JetPackAppClient is not connected. Call connectAndEnsureApp() first.');
|
|
428
|
+
}
|
|
429
|
+
await this.connectAndEnsureApp(this.lastConnectOptions);
|
|
430
|
+
}
|
|
224
431
|
/**
|
|
225
432
|
* Ensures the sandboxed worker for the connected app is running, relaunching it via
|
|
226
433
|
* `launch_app` if it was stopped due to idle timeout or user termination. Concurrent calls
|
|
@@ -233,6 +440,10 @@ export class JetPackAppClient {
|
|
|
233
440
|
const client = this.wasmClient;
|
|
234
441
|
const appId = this.appId;
|
|
235
442
|
if (!client || !client.is_connected() || !appId) {
|
|
443
|
+
if (appId) {
|
|
444
|
+
this.workerRunning = false;
|
|
445
|
+
this.controlBar?.setStatus('Error', formatDaemonConnectionLost(this.currentLanguage));
|
|
446
|
+
}
|
|
236
447
|
throw new JetPackClientError('NOT_CONNECTED', 'JetPackAppClient is not connected. Call connectAndEnsureApp() first.');
|
|
237
448
|
}
|
|
238
449
|
if (this.workerRunning) {
|
|
@@ -279,23 +490,29 @@ export class JetPackAppClient {
|
|
|
279
490
|
* ensures the worker process is running inside its sandbox, and mounts `JetPackControlBar`
|
|
280
491
|
* in browser DOM environments unless `controlBar: false`.
|
|
281
492
|
*/
|
|
282
|
-
async connectAndEnsureApp(
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
493
|
+
async connectAndEnsureApp(rawOptions) {
|
|
494
|
+
const options = resolveEnvironmentOptions(rawOptions);
|
|
495
|
+
// Cancel any background /health poll and reset prior session state cleanly
|
|
496
|
+
this.cancelBackgroundReconnect();
|
|
497
|
+
this.lastConnectOptions = {
|
|
498
|
+
...rawOptions,
|
|
499
|
+
controlBar: typeof rawOptions.controlBar === 'object' && rawOptions.controlBar !== null
|
|
500
|
+
? { ...rawOptions.controlBar }
|
|
501
|
+
: rawOptions.controlBar,
|
|
502
|
+
};
|
|
287
503
|
if (this.wasmClient) {
|
|
288
504
|
this.cleanupConnectionOnly();
|
|
289
505
|
}
|
|
290
506
|
this.closed = false;
|
|
291
|
-
const appId = options.appId
|
|
507
|
+
const appId = options.appId;
|
|
508
|
+
const resolvedEnv = options.env;
|
|
292
509
|
const daemonPort = options.daemonPort ?? DEFAULT_DAEMON_PORT;
|
|
293
510
|
const wsUrl = options.wsUrl ?? `ws://127.0.0.1:${daemonPort}`;
|
|
294
511
|
const origin = options.origin ?? resolveDefaultOrigin();
|
|
295
512
|
const autoPair = options.autoPair ?? true;
|
|
296
513
|
const pollIntervalMs = Math.max(50, options.pairingPollIntervalMs ?? DEFAULT_PAIRING_POLL_MS);
|
|
297
514
|
const timeoutMs = Math.max(pollIntervalMs, options.pairingTimeoutMs ?? DEFAULT_PAIRING_TIMEOUT_MS);
|
|
298
|
-
const downloadUrl =
|
|
515
|
+
const downloadUrl = resolveJetPackDownloadUrl(options.downloadUrl);
|
|
299
516
|
const healthProbeTimeoutMs = options.healthProbeTimeoutMs === 0
|
|
300
517
|
? 0
|
|
301
518
|
: Math.max(50, options.healthProbeTimeoutMs ?? DEFAULT_HEALTH_PROBE_TIMEOUT_MS);
|
|
@@ -303,19 +520,39 @@ export class JetPackAppClient {
|
|
|
303
520
|
(typeof globalThis !== 'undefined' && typeof globalThis.fetch === 'function'
|
|
304
521
|
? globalThis.fetch.bind(globalThis)
|
|
305
522
|
: null);
|
|
523
|
+
const userControlBarOpts = typeof options.controlBar === 'object' && options.controlBar !== null
|
|
524
|
+
? options.controlBar
|
|
525
|
+
: null;
|
|
526
|
+
const userOnSuperseded = userControlBarOpts?.onSuperseded;
|
|
527
|
+
this.hasCustomControlCenterUrl = Boolean(userControlBarOpts?.controlCenterUrl);
|
|
528
|
+
this.activeDaemonPort = daemonPort;
|
|
529
|
+
const initialLanguage = resolveLanguage({
|
|
530
|
+
preferredLanguage: userControlBarOpts?.language,
|
|
531
|
+
});
|
|
532
|
+
this.updateActiveLanguage(initialLanguage);
|
|
306
533
|
const barConfig = options.controlBar === false || typeof document === 'undefined'
|
|
307
534
|
? null
|
|
308
535
|
: {
|
|
309
|
-
...(
|
|
310
|
-
? options.controlBar
|
|
311
|
-
: {}),
|
|
536
|
+
...(userControlBarOpts ?? {}),
|
|
312
537
|
appId,
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
538
|
+
env: userControlBarOpts?.env ?? resolvedEnv,
|
|
539
|
+
appName: userControlBarOpts?.appName || appId,
|
|
540
|
+
language: this.currentLanguage,
|
|
541
|
+
controlCenterUrl: userControlBarOpts?.controlCenterUrl ||
|
|
542
|
+
buildDefaultControlCenterUrl(daemonPort, this.currentLanguage),
|
|
543
|
+
posterUrl: userControlBarOpts?.posterUrl ?? options.posterUrl,
|
|
544
|
+
installBanner: userControlBarOpts?.installBanner !== undefined
|
|
545
|
+
? userControlBarOpts.installBanner
|
|
546
|
+
: options.installBanner,
|
|
316
547
|
initialStatus: 'Connecting',
|
|
317
548
|
onStop: () => this.stopApp(),
|
|
318
|
-
|
|
549
|
+
onLaunch: () => this.launchOrReconnectFromControlBar(),
|
|
550
|
+
onRelaunch: () => this.launchOrReconnectFromControlBar(),
|
|
551
|
+
onSuperseded: () => {
|
|
552
|
+
this.cancelBackgroundReconnect();
|
|
553
|
+
this.controlBar = null;
|
|
554
|
+
userOnSuperseded?.();
|
|
555
|
+
},
|
|
319
556
|
};
|
|
320
557
|
if (options.controlBar === false && this.controlBar) {
|
|
321
558
|
this.controlBar.unmount();
|
|
@@ -391,6 +628,7 @@ export class JetPackAppClient {
|
|
|
391
628
|
}
|
|
392
629
|
this.wasmClient = client;
|
|
393
630
|
this.appId = appId;
|
|
631
|
+
this.env = resolvedEnv;
|
|
394
632
|
const apps = (await client.list_apps());
|
|
395
633
|
let targetApp = Array.isArray(apps) ? apps.find((a) => a.id === appId) : undefined;
|
|
396
634
|
const progressAdapter = options.onProgress
|
|
@@ -431,15 +669,45 @@ export class JetPackAppClient {
|
|
|
431
669
|
});
|
|
432
670
|
this.workerRunning = true;
|
|
433
671
|
this.lastStopReason = null;
|
|
434
|
-
|
|
672
|
+
let daemonSettings = null;
|
|
673
|
+
if (typeof client.get_settings === 'function') {
|
|
674
|
+
try {
|
|
675
|
+
daemonSettings = await client.get_settings();
|
|
676
|
+
}
|
|
677
|
+
catch {
|
|
678
|
+
daemonSettings = null;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const resolvedLanguage = resolveLanguage({
|
|
682
|
+
preferredLanguage: userControlBarOpts?.language,
|
|
683
|
+
daemonLanguage: daemonSettings?.language,
|
|
684
|
+
});
|
|
685
|
+
this.updateActiveLanguage(resolvedLanguage);
|
|
686
|
+
await this.syncLogTerminalForSession(client, resolvedAppName, options.logTerminal, daemonSettings);
|
|
435
687
|
this.controlBar?.setStatus('Running');
|
|
436
688
|
return this;
|
|
437
689
|
}
|
|
438
690
|
catch (err) {
|
|
439
691
|
const msg = err instanceof Error ? err.message : String(err);
|
|
692
|
+
const isDaemonNotRunning = err instanceof JetPackClientError && err.code === 'DAEMON_NOT_RUNNING';
|
|
440
693
|
const errDownloadUrl = err instanceof JetPackClientError && err.downloadUrl ? err.downloadUrl : undefined;
|
|
441
|
-
this.
|
|
694
|
+
if (!this.closed) {
|
|
695
|
+
this.controlBar?.setStatus('Error', msg, errDownloadUrl ? { downloadUrl: errDownloadUrl } : undefined);
|
|
696
|
+
}
|
|
442
697
|
this.cleanupConnectionOnly();
|
|
698
|
+
if (isDaemonNotRunning &&
|
|
699
|
+
!this.closed &&
|
|
700
|
+
typeof document !== 'undefined' &&
|
|
701
|
+
fetchImpl &&
|
|
702
|
+
options.autoReconnectOnDaemonStart !== false) {
|
|
703
|
+
this.startBackgroundDaemonPoll({
|
|
704
|
+
savedOptions: this.lastConnectOptions ?? { ...rawOptions },
|
|
705
|
+
healthUrl: resolveHealthProbeUrl(wsUrl, daemonPort),
|
|
706
|
+
healthProbeTimeoutMs,
|
|
707
|
+
pollIntervalMs: Math.max(50, options.daemonPollIntervalMs ?? DEFAULT_DAEMON_POLL_INTERVAL_MS),
|
|
708
|
+
fetchImpl,
|
|
709
|
+
});
|
|
710
|
+
}
|
|
443
711
|
throw err;
|
|
444
712
|
}
|
|
445
713
|
}
|
|
@@ -452,6 +720,10 @@ export class JetPackAppClient {
|
|
|
452
720
|
throw new JetPackClientError('CLIENT_CLOSED', 'Cannot stop app after JetPackAppClient.close()');
|
|
453
721
|
}
|
|
454
722
|
if (!this.wasmClient || !this.wasmClient.is_connected() || !this.appId) {
|
|
723
|
+
if (this.appId) {
|
|
724
|
+
this.workerRunning = false;
|
|
725
|
+
this.controlBar?.setStatus('Error', formatDaemonConnectionLost(this.currentLanguage));
|
|
726
|
+
}
|
|
455
727
|
throw new JetPackClientError('NOT_CONNECTED', 'JetPackAppClient is not connected. Call connectAndEnsureApp() first.');
|
|
456
728
|
}
|
|
457
729
|
const activeAppId = this.appId;
|
|
@@ -490,6 +762,10 @@ export class JetPackAppClient {
|
|
|
490
762
|
throw new JetPackClientError('CLIENT_CLOSED', 'Cannot send request after JetPackAppClient.close()');
|
|
491
763
|
}
|
|
492
764
|
if (!this.wasmClient || !this.wasmClient.is_connected() || !this.appId) {
|
|
765
|
+
if (this.appId) {
|
|
766
|
+
this.workerRunning = false;
|
|
767
|
+
this.controlBar?.setStatus('Error', formatDaemonConnectionLost(this.currentLanguage));
|
|
768
|
+
}
|
|
493
769
|
throw new JetPackClientError('NOT_CONNECTED', 'JetPackAppClient is not connected. Call connectAndEnsureApp() first.');
|
|
494
770
|
}
|
|
495
771
|
const [meta = null, binaryPayload = new Uint8Array(0), options] = args;
|
|
@@ -649,9 +925,11 @@ export class JetPackAppClient {
|
|
|
649
925
|
*/
|
|
650
926
|
close() {
|
|
651
927
|
this.closed = true;
|
|
928
|
+
this.cancelBackgroundReconnect();
|
|
652
929
|
this.streamListeners.clear();
|
|
653
930
|
this.workerStoppedListeners.clear();
|
|
654
931
|
this.workerStartedListeners.clear();
|
|
932
|
+
this.languageListeners.clear();
|
|
655
933
|
const pending = Array.from(this.inFlightRequests);
|
|
656
934
|
this.inFlightRequests.clear();
|
|
657
935
|
for (const req of pending) {
|
|
@@ -667,6 +945,7 @@ export class JetPackAppClient {
|
|
|
667
945
|
*/
|
|
668
946
|
destroy() {
|
|
669
947
|
this.close();
|
|
948
|
+
this.lastConnectOptions = null;
|
|
670
949
|
if (this.controlBar) {
|
|
671
950
|
this.controlBar.unmount();
|
|
672
951
|
this.controlBar = null;
|
|
@@ -676,7 +955,75 @@ export class JetPackAppClient {
|
|
|
676
955
|
this.logTerminal = null;
|
|
677
956
|
}
|
|
678
957
|
}
|
|
679
|
-
|
|
958
|
+
updateActiveLanguage(rawLanguage) {
|
|
959
|
+
const normalized = normalizeLanguageTag(rawLanguage) ?? 'en';
|
|
960
|
+
const changed = normalized !== this.currentLanguage;
|
|
961
|
+
this.currentLanguage = normalized;
|
|
962
|
+
if (this.controlBar) {
|
|
963
|
+
this.controlBar.setLanguage(normalized);
|
|
964
|
+
if (!this.hasCustomControlCenterUrl) {
|
|
965
|
+
this.controlBar.setControlCenterUrl(buildDefaultControlCenterUrl(this.activeDaemonPort, normalized));
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
if (this.logTerminal) {
|
|
969
|
+
this.logTerminal.setLanguage(normalized);
|
|
970
|
+
}
|
|
971
|
+
if (changed) {
|
|
972
|
+
for (const cb of Array.from(this.languageListeners)) {
|
|
973
|
+
cb(normalized);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
cancelBackgroundReconnect() {
|
|
978
|
+
this.daemonPollGeneration += 1;
|
|
979
|
+
if (this.daemonPollTimer !== null) {
|
|
980
|
+
clearTimeout(this.daemonPollTimer);
|
|
981
|
+
this.daemonPollTimer = null;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
startBackgroundDaemonPoll(params) {
|
|
985
|
+
this.cancelBackgroundReconnect();
|
|
986
|
+
const generation = this.daemonPollGeneration;
|
|
987
|
+
const scheduleNext = () => {
|
|
988
|
+
if (this.closed || this.daemonPollGeneration !== generation) {
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
const timer = setTimeout(() => {
|
|
992
|
+
this.daemonPollTimer = null;
|
|
993
|
+
void tick();
|
|
994
|
+
}, params.pollIntervalMs);
|
|
995
|
+
const maybeUnref = timer.unref;
|
|
996
|
+
if (typeof maybeUnref === 'function') {
|
|
997
|
+
maybeUnref.call(timer);
|
|
998
|
+
}
|
|
999
|
+
this.daemonPollTimer = timer;
|
|
1000
|
+
};
|
|
1001
|
+
const tick = async () => {
|
|
1002
|
+
if (this.closed || this.daemonPollGeneration !== generation) {
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
const health = await probeDaemonHealth(params.healthUrl, params.healthProbeTimeoutMs, params.fetchImpl);
|
|
1006
|
+
if (this.closed || this.daemonPollGeneration !== generation) {
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
if (!health.reachable) {
|
|
1010
|
+
scheduleNext();
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
this.cancelBackgroundReconnect();
|
|
1014
|
+
try {
|
|
1015
|
+
await this.connectAndEnsureApp(params.savedOptions);
|
|
1016
|
+
if (!this.closed && this.isConnected()) {
|
|
1017
|
+
await params.savedOptions.onDaemonReconnect?.(this);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
catch {
|
|
1021
|
+
// connectAndEnsureApp sets Error status and restarts polling if DAEMON_NOT_RUNNING recurs
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
scheduleNext();
|
|
1025
|
+
}
|
|
1026
|
+
async syncLogTerminalForSession(client, resolvedAppName, logTerminalOpt, daemonSettings) {
|
|
680
1027
|
if (logTerminalOpt === false || typeof document === 'undefined') {
|
|
681
1028
|
if (this.logTerminal) {
|
|
682
1029
|
this.logTerminal.unmount();
|
|
@@ -685,16 +1032,10 @@ export class JetPackAppClient {
|
|
|
685
1032
|
return;
|
|
686
1033
|
}
|
|
687
1034
|
let devMode = false;
|
|
688
|
-
if (
|
|
689
|
-
|
|
690
|
-
const settings = await client.get_settings();
|
|
691
|
-
devMode = Boolean(settings?.developer_mode);
|
|
692
|
-
}
|
|
693
|
-
catch {
|
|
694
|
-
devMode = false;
|
|
695
|
-
}
|
|
1035
|
+
if (daemonSettings) {
|
|
1036
|
+
devMode = Boolean(daemonSettings.developer_mode);
|
|
696
1037
|
}
|
|
697
|
-
else if (typeof logTerminalOpt === 'object' && logTerminalOpt !== null) {
|
|
1038
|
+
else if (typeof client.get_settings !== 'function' && typeof logTerminalOpt === 'object' && logTerminalOpt !== null) {
|
|
698
1039
|
devMode = Boolean(logTerminalOpt.developerMode);
|
|
699
1040
|
}
|
|
700
1041
|
if (!devMode) {
|
|
@@ -706,8 +1047,12 @@ export class JetPackAppClient {
|
|
|
706
1047
|
return;
|
|
707
1048
|
}
|
|
708
1049
|
const customOpts = typeof logTerminalOpt === 'object' && logTerminalOpt !== null ? logTerminalOpt : {};
|
|
1050
|
+
const defaultTitle = normalizeLanguageTag(this.currentLanguage) === 'uk'
|
|
1051
|
+
? `${resolvedAppName} · Живі журнали`
|
|
1052
|
+
: `${resolvedAppName} · Live Logs`;
|
|
709
1053
|
const mergedOpts = {
|
|
710
|
-
title:
|
|
1054
|
+
title: defaultTitle,
|
|
1055
|
+
language: this.currentLanguage,
|
|
711
1056
|
...customOpts,
|
|
712
1057
|
developerMode: true,
|
|
713
1058
|
};
|
|
@@ -755,6 +1100,7 @@ export class JetPackAppClient {
|
|
|
755
1100
|
this.wasmClient = null;
|
|
756
1101
|
}
|
|
757
1102
|
this.appId = null;
|
|
1103
|
+
this.env = null;
|
|
758
1104
|
}
|
|
759
1105
|
handleWorkerStopped(event) {
|
|
760
1106
|
if (this.closed || !this.appId || event.app_id !== this.appId) {
|
|
@@ -762,7 +1108,7 @@ export class JetPackAppClient {
|
|
|
762
1108
|
}
|
|
763
1109
|
this.workerRunning = false;
|
|
764
1110
|
this.lastStopReason = event.reason;
|
|
765
|
-
this.controlBar?.setStatus('Stopped', formatStopDetail(event.reason));
|
|
1111
|
+
this.controlBar?.setStatus('Stopped', formatStopDetail(event.reason, this.currentLanguage));
|
|
766
1112
|
if (event.reason === 'terminated' || event.reason === 'process_exited') {
|
|
767
1113
|
const pending = Array.from(this.inFlightRequests);
|
|
768
1114
|
this.inFlightRequests.clear();
|