@inflatable-cookie/longhorn-tauri 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflatable-cookie/longhorn-tauri",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -74,8 +74,8 @@
74
74
  "LICENSE"
75
75
  ],
76
76
  "peerDependencies": {
77
- "@inflatable-cookie/longhorn": "0.6.0",
78
- "@tauri-apps/api": "^2.10.1"
77
+ "@inflatable-cookie/longhorn": "0.7.0",
78
+ "@tauri-apps/api": "^2.13.0"
79
79
  },
80
80
  "sideEffects": false,
81
81
  "publishConfig": {
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  ConnectionFailureReporter,
3
+ EventListenOptions,
3
4
  EventTransport,
4
5
  Unlisten,
5
6
  } from "@inflatable-cookie/longhorn/core";
@@ -25,6 +26,7 @@ import { TauriBridgeAdapter } from "./bridge.ts";
25
26
  export const BRIDGE_DOMAIN_EVENT = "longhorn://bridge/domain";
26
27
  export const BRIDGE_PROGRESS_EVENT = "longhorn://bridge/progress";
27
28
  export const BRIDGE_TERMINAL_EVENT = "longhorn://bridge/terminal";
29
+ const bridgeDelivery = { scope: "renderer-local" } satisfies EventListenOptions;
28
30
 
29
31
  /**
30
32
  * Event-capable stream source. `connectBridgeStream` installs `listen` before
@@ -48,7 +50,7 @@ export class TauriBridgeStreamSource implements BridgeStreamSource {
48
50
  }
49
51
 
50
52
  listen(listener: (value: unknown) => void): Promise<Unlisten> {
51
- return this.#transport.listen(BRIDGE_DOMAIN_EVENT, listener);
53
+ return this.#transport.listen(BRIDGE_DOMAIN_EVENT, listener, bridgeDelivery);
52
54
  }
53
55
 
54
56
  loadSnapshot(): Promise<unknown> {
@@ -63,7 +65,7 @@ export interface BridgeJobListeners<Progress, Success, Detail> {
63
65
  readonly progress?: (event: BridgeProgressEvent<Progress>) => void;
64
66
  readonly terminal: (event: BridgeJobTerminalEvent<Success, Detail>) => void;
65
67
  /**
66
- * Reports a malformed event or a teardown failure, mirroring
68
+ * Reports a malformed event for this job or a teardown failure, mirroring
67
69
  * `CheckedSnapshotConnection`. A malformed terminal event is the only way
68
70
  * the job would otherwise never terminate for the consumer, so one parse
69
71
  * failure reports `{ phase: "event" }` and tears both listeners down.
@@ -73,7 +75,9 @@ export interface BridgeJobListeners<Progress, Success, Detail> {
73
75
 
74
76
  /**
75
77
  * Installs request/job-correlated progress and terminal listeners.
76
- * Wrong-correlation and post-terminal messages never reach consumers.
78
+ * Wrong-correlation messages are dropped before their payload is decoded, and
79
+ * post-terminal messages are dropped without decoding, so neither can fail
80
+ * this job or reach consumers.
77
81
  *
78
82
  * Contract 010 makes events projections, not durable delivery, and the bridge
79
83
  * has no job-status query. A terminal that fires before its listener is
@@ -146,8 +150,21 @@ export async function listenTauriBridgeJob<Progress, Success, Detail>(
146
150
  }
147
151
  };
148
152
 
153
+ // The channels are shared by every job, so admission comes before any
154
+ // job-specific decode: another job's payload cannot fail this one. A value
155
+ // from another request is refused on its request id alone. Identifiers that
156
+ // cannot be read as part of this request are malformed.
157
+ const admits = (value: unknown): boolean => {
158
+ try {
159
+ return tracker.admits(value);
160
+ } catch (error) {
161
+ fail(error);
162
+ return false;
163
+ }
164
+ };
165
+
149
166
  const onProgress = (value: unknown): void => {
150
- if (stopped) {
167
+ if (stopped || tracker.isTerminal() || !admits(value)) {
151
168
  return;
152
169
  }
153
170
  let event: BridgeProgressEvent<Progress>;
@@ -163,7 +180,9 @@ export async function listenTauriBridgeJob<Progress, Success, Detail>(
163
180
  };
164
181
 
165
182
  const onTerminal = (value: unknown): void => {
166
- if (stopped) {
183
+ // After this job's terminal, input is not decoded at all: a late or
184
+ // malformed event cannot fail a job that has already settled.
185
+ if (stopped || tracker.isTerminal() || !admits(value)) {
167
186
  return;
168
187
  }
169
188
  let event: BridgeJobTerminalEvent<Success, Detail>;
@@ -184,8 +203,8 @@ export async function listenTauriBridgeJob<Progress, Success, Detail>(
184
203
  };
185
204
 
186
205
  try {
187
- await adopt(await transport.listen(BRIDGE_PROGRESS_EVENT, onProgress));
188
- await adopt(await transport.listen(BRIDGE_TERMINAL_EVENT, onTerminal));
206
+ await adopt(await transport.listen(BRIDGE_PROGRESS_EVENT, onProgress, bridgeDelivery));
207
+ await adopt(await transport.listen(BRIDGE_TERMINAL_EVENT, onTerminal, bridgeDelivery));
189
208
  registered = true;
190
209
  } catch (error) {
191
210
  stopped = true;
@@ -1,18 +1,36 @@
1
1
  import { listen } from "@tauri-apps/api/event";
2
+ import type { Options } from "@tauri-apps/api/event";
2
3
  import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
3
4
 
4
- import type { EventTransport, Unlisten } from "@inflatable-cookie/longhorn/core";
5
- import { TRANSFER_CLIENT_CHANGED_EVENT } from "@inflatable-cookie/longhorn/transfer";
5
+ import type {
6
+ EventListenOptions,
7
+ EventTransport,
8
+ Unlisten,
9
+ } from "@inflatable-cookie/longhorn/core";
6
10
 
7
11
  import { TauriTransport } from "./index.ts";
8
12
 
9
13
  /// Optional raw Tauri invoke/listen edge.
10
14
  export class TauriEventTransport extends TauriTransport implements EventTransport {
11
- listen(event: string, listener: (payload: unknown) => void): Promise<Unlisten> {
12
- const options =
13
- event === TRANSFER_CLIENT_CHANGED_EVENT
14
- ? { target: getCurrentWebviewWindow().label }
15
+ async listen(
16
+ event: string,
17
+ listener: (payload: unknown) => void,
18
+ options?: EventListenOptions,
19
+ ): Promise<Unlisten> {
20
+ if (options?.scope === "renderer-local") {
21
+ const webview = window;
22
+ const receive = (message: Event): void => {
23
+ if ("detail" in message) {
24
+ listener(message.detail);
25
+ }
26
+ };
27
+ webview.addEventListener(event, receive);
28
+ return () => webview.removeEventListener(event, receive);
29
+ }
30
+ const tauriOptions: Options | undefined =
31
+ options?.scope === "current-webview"
32
+ ? { target: { kind: "WebviewWindow", label: getCurrentWebviewWindow().label } }
15
33
  : undefined;
16
- return listen(event, ({ payload }) => listener(payload), options);
34
+ return listen(event, ({ payload }) => listener(payload), tauriOptions);
17
35
  }
18
36
  }
@@ -1,9 +1,74 @@
1
- import { invoke } from "@tauri-apps/api/core";
1
+ import { Channel, invoke } from "@tauri-apps/api/core";
2
2
 
3
3
  import type { InvokeTransport } from "@inflatable-cookie/longhorn/core";
4
4
 
5
+ /** An operational error returned by a Tauri host adapter. */
6
+ export class TauriHostError extends Error {
7
+ constructor(
8
+ readonly code: string,
9
+ message: string,
10
+ readonly retryable: boolean,
11
+ ) {
12
+ super(message);
13
+ this.name = "TauriHostError";
14
+ }
15
+ }
16
+
5
17
  export class TauriTransport implements InvokeTransport {
6
- invoke(command: string, arguments_: Record<string, unknown>): Promise<unknown> {
7
- return invoke(command, arguments_);
18
+ async invoke(command: string, arguments_: Record<string, unknown>): Promise<unknown> {
19
+ try {
20
+ return await invoke(command, arguments_);
21
+ } catch (error) {
22
+ if (isHostErrorEnvelope(error)) {
23
+ throw new TauriHostError(error.code, error.message, error.retryable);
24
+ }
25
+ throw error;
26
+ }
27
+ }
28
+
29
+ /** Invokes a command with a channel for its live progress. */
30
+ async invokeWithProgress(
31
+ command: string,
32
+ arguments_: Record<string, unknown>,
33
+ listener: (event: unknown) => void,
34
+ ): Promise<unknown> {
35
+ const onProgress = new Channel(listener);
36
+ try {
37
+ return await this.invoke(command, { ...arguments_, onProgress });
38
+ } finally {
39
+ // A rejected invoke may never create a Rust channel to close the callback.
40
+ const internals: unknown = "__TAURI_INTERNALS__" in window && window.__TAURI_INTERNALS__;
41
+ if (hasCallbackCleanup(internals)) {
42
+ internals.unregisterCallback(onProgress.id);
43
+ }
44
+ }
8
45
  }
9
46
  }
47
+
48
+ function isHostErrorEnvelope(
49
+ value: unknown,
50
+ ): value is Pick<TauriHostError, "code" | "message" | "retryable"> {
51
+ return (
52
+ typeof value === "object" &&
53
+ value !== null &&
54
+ Object.keys(value).length === 3 &&
55
+ Object.hasOwn(value, "code") &&
56
+ Object.hasOwn(value, "message") &&
57
+ Object.hasOwn(value, "retryable") &&
58
+ "code" in value &&
59
+ typeof value.code === "string" &&
60
+ "message" in value &&
61
+ typeof value.message === "string" &&
62
+ "retryable" in value &&
63
+ typeof value.retryable === "boolean"
64
+ );
65
+ }
66
+
67
+ function hasCallbackCleanup(value: unknown): value is { unregisterCallback(id: number): void } {
68
+ return (
69
+ typeof value === "object" &&
70
+ value !== null &&
71
+ "unregisterCallback" in value &&
72
+ typeof value.unregisterCallback === "function"
73
+ );
74
+ }
package/src/update.ts CHANGED
@@ -10,6 +10,7 @@ import type {
10
10
  UpdateSelectChannelCommand,
11
11
  UpdateUnlisten,
12
12
  } from "@inflatable-cookie/longhorn/update";
13
+ import type { TauriTransport } from "./transport/index.ts";
13
14
  export const UPDATE_SNAPSHOT_COMMAND = "longhorn_update_snapshot";
14
15
  export const UPDATE_CHECK_COMMAND = "longhorn_update_check";
15
16
  export const UPDATE_SELECT_CHANNEL_COMMAND = "longhorn_update_select_channel";
@@ -18,19 +19,11 @@ export const UPDATE_PREPARE_COMMAND = "longhorn_update_prepare";
18
19
  export const UPDATE_APPLY_COMMAND = "longhorn_update_apply";
19
20
  export const UPDATE_CANCEL_COMMAND = "longhorn_update_cancel";
20
21
  export const UPDATE_CHANGED_EVENT = "longhorn://update/changed";
21
- export const UPDATE_PROGRESS_EVENT = "longhorn://update/progress";
22
22
  /**
23
23
  * The update seam.
24
24
  *
25
- * Seven commands and two events, mirroring the staged `UpdateController`.
26
- * Each is its own invoke because each is its own capability on the host side:
27
- * reading state, reaching the network, changing what this install follows,
28
- * staging verified bytes, replacing the running application, and discarding
29
- * what was staged are six different grants.
30
- *
31
- * `UPDATE_PROGRESS_EVENT` carries a progress value rather than an invalidation
32
- * hint, because the host does not hold the authority lock across the transfer
33
- * and a snapshot read mid-transfer cannot show the bytes.
25
+ * Committed changes produce an invalidation hint. Live byte progress arrives
26
+ * on a channel owned by the prepare invocation.
34
27
  *
35
28
  * Results are `unknown`, as every other raw port's are. What comes back over a
36
29
  * transport is untrusted until a validator says otherwise; `UpdateClient` is
@@ -41,8 +34,10 @@ export function createTauriUpdatePort(options: {
41
34
  readonly transport: InvokeTransport;
42
35
  }): UpdatePort {
43
36
  const events = isEventTransport(options.transport) ? options.transport : undefined;
37
+ const transport = options.transport;
38
+ const progress = hasProgress(transport) ? transport : undefined;
44
39
  return {
45
- snapshot: () => options.transport.invoke(UPDATE_SNAPSHOT_COMMAND, {}),
40
+ snapshot: () => transport.invoke(UPDATE_SNAPSHOT_COMMAND, {}),
46
41
  check: (command: UpdateCheckCommand) =>
47
42
  options.transport.invoke(UPDATE_CHECK_COMMAND, { command }),
48
43
  selectChannel: (command: UpdateSelectChannelCommand) =>
@@ -51,6 +46,11 @@ export function createTauriUpdatePort(options: {
51
46
  options.transport.invoke(UPDATE_DEFER_COMMAND, { command }),
52
47
  prepare: (command: UpdatePrepareCommand) =>
53
48
  options.transport.invoke(UPDATE_PREPARE_COMMAND, { command }),
49
+ prepareWithProgress:
50
+ progress === undefined
51
+ ? undefined
52
+ : (command, listener) =>
53
+ progress.invokeWithProgress(UPDATE_PREPARE_COMMAND, { command }, listener),
54
54
  apply: (command: UpdateApplyCommand) =>
55
55
  options.transport.invoke(UPDATE_APPLY_COMMAND, { command }),
56
56
  cancel: (command: UpdateCancelCommand) =>
@@ -59,12 +59,11 @@ export function createTauriUpdatePort(options: {
59
59
  events === undefined
60
60
  ? undefined
61
61
  : (listener) => listen(events, UPDATE_CHANGED_EVENT, listener),
62
- listenProgress:
63
- events === undefined
64
- ? undefined
65
- : (listener) => listen(events, UPDATE_PROGRESS_EVENT, listener),
66
62
  };
67
63
  }
64
+ function hasProgress(transport: InvokeTransport): transport is TauriTransport {
65
+ return "invokeWithProgress" in transport && typeof transport.invokeWithProgress === "function";
66
+ }
68
67
  function listen(
69
68
  events: EventTransport,
70
69
  event: string,