@adea-ai/app-framework 0.25.1 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -21
- package/dist/app/application.d.ts +11 -4
- package/dist/app/application.js +24 -4
- package/dist/app/create-application.d.ts +9 -0
- package/dist/app/create-application.js +214 -143
- package/dist/app/private-group.d.ts +14 -0
- package/dist/app/private-group.js +39 -0
- package/dist/app/service-registry.d.ts +8 -4
- package/dist/app/service-registry.js +43 -6
- package/dist/appearance/custom-theme-import.d.ts +5 -7
- package/dist/appearance/custom-theme-import.js +66 -18
- package/dist/appearance/custom-theme-library.d.ts +51 -3
- package/dist/appearance/custom-theme-library.js +262 -33
- package/dist/appearance/editor.d.ts +3 -1
- package/dist/appearance/editor.js +37 -2
- package/dist/appearance/theme-file-importer.d.ts +7 -3
- package/dist/appearance/theme-file-importer.js +27 -7
- package/dist/appearance/theme-library-action-fence.d.ts +11 -0
- package/dist/appearance/theme-library-action-fence.js +29 -0
- package/dist/cli/framework-update-cli.js +16 -3
- package/dist/cli/release-step-cli.js +6 -1
- package/dist/desktop/application-command-client.d.ts +11 -1
- package/dist/desktop/application-command-client.js +66 -3
- package/dist/desktop/channel-handshake.d.ts +2 -1
- package/dist/desktop/channel-handshake.js +21 -5
- package/dist/desktop/client-asset-stream.d.ts +10 -0
- package/dist/desktop/client-asset-stream.js +97 -0
- package/dist/desktop/client-assets.d.ts +3 -1
- package/dist/desktop/client-assets.js +41 -6
- package/dist/desktop/desktop-client.d.ts +15 -1
- package/dist/desktop/desktop-client.js +235 -109
- package/dist/desktop/desktop-gateway.d.ts +2 -0
- package/dist/desktop/desktop-gateway.js +134 -22
- package/dist/desktop/desktop-host-websocket.d.ts +10 -0
- package/dist/desktop/desktop-host-websocket.js +28 -0
- package/dist/desktop/desktop-host.d.ts +9 -0
- package/dist/desktop/desktop-host.js +70 -61
- package/dist/desktop/desktop-port-file.d.ts +2 -0
- package/dist/desktop/desktop-port-file.js +23 -0
- package/dist/desktop/desktop-stream.d.ts +5 -0
- package/dist/desktop/desktop-stream.js +83 -15
- package/dist/desktop/gateway-limits.d.ts +2 -0
- package/dist/desktop/gateway-limits.js +6 -0
- package/dist/desktop/open-url-channel.d.ts +5 -3
- package/dist/desktop/open-url-channel.js +18 -5
- package/dist/desktop/typed-array-intrinsics.d.ts +6 -0
- package/dist/desktop/typed-array-intrinsics.js +19 -0
- package/dist/framework-source.json +1 -1
- package/dist/installer/installed-payload.js +1 -1
- package/dist/installer/installer-ci-bootstrap.js +2 -1
- package/dist/installer/installer-command.d.ts +13 -0
- package/dist/installer/installer-command.js +47 -0
- package/dist/installer/installer-extraction.js +27 -61
- package/dist/installer/installer-identity.d.ts +22 -3
- package/dist/installer/installer-identity.js +63 -72
- package/dist/installer/installer-image-mount.d.ts +48 -0
- package/dist/installer/installer-image-mount.js +347 -0
- package/dist/installer/installer-layout.d.ts +2 -0
- package/dist/installer/installer-layout.js +6 -2
- package/dist/installer/installer-process-cleanup.js +1 -1
- package/dist/installer/installer-renderer-evidence.js +6 -2
- package/dist/installer/upgrade-asset-download.js +30 -8
- package/dist/native/app-bundle.d.ts +2 -2
- package/dist/native/app-bundle.js +5 -4
- package/dist/native/cef-helper-download.d.ts +1 -0
- package/dist/native/cef-helper-download.js +35 -0
- package/dist/native/cef-helper.js +146 -61
- package/dist/native/exclusive-rename.d.ts +2 -0
- package/dist/native/exclusive-rename.js +52 -0
- package/dist/native/native-secret-store.js +5 -4
- package/dist/native/partitioned-window.d.ts +5 -2
- package/dist/native/partitioned-window.js +69 -9
- package/dist/native/sidecar-services.js +2 -2
- package/dist/native/sidecar.js +68 -25
- package/dist/native/window-bootstrap.js +2 -2
- package/dist/release/feed-state.js +2 -1
- package/dist/release/framework-update-lock.d.ts +2 -0
- package/dist/release/framework-update-lock.js +42 -0
- package/dist/release/framework-update.d.ts +6 -2
- package/dist/release/framework-update.js +165 -40
- package/dist/release/local-release.js +68 -39
- package/dist/release/release-config.js +1 -1
- package/dist/release/release-steps.d.ts +5 -0
- package/dist/release/release-steps.js +2 -1
- package/dist/shared/discard-stream.d.ts +4 -0
- package/dist/shared/discard-stream.js +9 -0
- package/dist/shell/app-shell.js +59 -7
- package/dist/shell/app-utilities.js +2 -2
- package/dist/shell/application-menu.d.ts +13 -0
- package/dist/shell/application-menu.js +21 -8
- package/dist/update-helper.bundle.js +40 -4
- package/dist/updates/channel-update-manager.d.ts +2 -0
- package/dist/updates/channel-update-manager.js +15 -3
- package/dist/updates/desktop-updater.d.ts +2 -0
- package/dist/updates/desktop-updater.js +36 -7
- package/dist/updates/linux-process-state.d.ts +2 -0
- package/dist/updates/linux-process-state.js +13 -0
- package/dist/updates/release-location.js +5 -4
- package/dist/updates/update-adapter.js +5 -0
- package/dist/updates/update-arm.d.ts +2 -0
- package/dist/updates/update-arm.js +58 -7
- package/dist/updates/update-discovery.js +2 -1
- package/dist/updates/update-download-cleanup.d.ts +7 -0
- package/dist/updates/update-download-cleanup.js +21 -0
- package/dist/updates/update-download.js +41 -20
- package/dist/updates/update-exclusive-rename.d.ts +3 -2
- package/dist/updates/update-exclusive-rename.js +3 -50
- package/dist/updates/update-extraction.js +1 -1
- package/dist/updates/update-manager.d.ts +17 -1
- package/dist/updates/update-manager.js +27 -8
- package/dist/updates/update-process-fence.js +18 -3
- package/dist/views/appearance-surface.js +51 -15
- package/package.json +8 -3
- package/src/app/application.tsx +49 -7
- package/src/appearance/custom-theme-import.ts +67 -7
- package/src/appearance/custom-theme-library.ts +391 -33
- package/src/appearance/editor.ts +49 -10
- package/src/appearance/theme-file-importer.ts +43 -12
- package/src/appearance/theme-library-action-fence.ts +28 -0
- package/src/desktop/client-asset-stream.ts +118 -0
- package/src/desktop/client-assets.ts +55 -5
- package/src/shell/app-shell.tsx +71 -8
- package/src/shell/app-utilities.tsx +2 -2
- package/src/updates/update-adapter.ts +5 -0
- package/src/views/appearance-surface.tsx +61 -29
- package/templates/application/README.md +5 -1
- package/templates/application/scripts/app-version.ts +7 -3
- package/templates/application/src/bun/index.ts +2 -0
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
import { createLoopbackPolicy } from "./loopback-policy.js";
|
|
2
2
|
// src/desktop/desktop-client.ts
|
|
3
|
+
function abortReason(signal) {
|
|
4
|
+
return signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
5
|
+
}
|
|
6
|
+
/** Settle client work on lifecycle cancellation even if a custom fetch ignores its signal. */
|
|
7
|
+
function awaitWithAbort(promise, signal) {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
if (signal.aborted) {
|
|
10
|
+
reject(abortReason(signal));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const onAbort = () => {
|
|
14
|
+
signal.removeEventListener("abort", onAbort);
|
|
15
|
+
reject(abortReason(signal));
|
|
16
|
+
};
|
|
17
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
18
|
+
Promise.resolve(promise).then((value) => {
|
|
19
|
+
signal.removeEventListener("abort", onAbort);
|
|
20
|
+
resolve(value);
|
|
21
|
+
}, (error) => {
|
|
22
|
+
signal.removeEventListener("abort", onAbort);
|
|
23
|
+
reject(error);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
3
27
|
function bytesToBase64Url(bytes) {
|
|
4
28
|
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
5
29
|
}
|
|
6
30
|
/** Binds a request body to the channel credential: an HMAC over the purpose, channel,
|
|
7
31
|
* credential, a fresh nonce, the time and the body's SHA-256. */
|
|
8
|
-
async function signRequest(active, purpose, body, time) {
|
|
32
|
+
async function signRequest(crypto, active, purpose, body, time) {
|
|
9
33
|
const nonce = bytesToBase64Url(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(24)));
|
|
10
34
|
const timestamp = String(time);
|
|
11
35
|
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(body)));
|
|
@@ -34,16 +58,22 @@ function createDesktopClient(options = {}) {
|
|
|
34
58
|
const request = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
35
59
|
const crypto = options.crypto ?? globalThis.crypto;
|
|
36
60
|
const now = options.now ?? Date.now;
|
|
37
|
-
const bootstrap = options.bootstrap ?? (() => {
|
|
61
|
+
const bootstrap = options.bootstrap ?? ((signal) => {
|
|
38
62
|
const acquire = globalThis.__frameworkAcquireBootstrap;
|
|
39
63
|
if (!acquire) throw new Error("Desktop window bootstrap is unavailable");
|
|
40
|
-
return acquire();
|
|
64
|
+
return acquire(signal);
|
|
41
65
|
});
|
|
42
66
|
let session;
|
|
43
67
|
let pending;
|
|
68
|
+
let pendingController;
|
|
44
69
|
let generation = 0;
|
|
45
70
|
let disposed = false;
|
|
46
71
|
const sockets = /* @__PURE__ */ new Set();
|
|
72
|
+
const invokes = /* @__PURE__ */ new Set();
|
|
73
|
+
let streamSessions = /* @__PURE__ */ new WeakMap();
|
|
74
|
+
function abortInvokes() {
|
|
75
|
+
for (const controller of invokes) controller.abort();
|
|
76
|
+
}
|
|
47
77
|
function closeSockets() {
|
|
48
78
|
for (const socket of sockets) socket.close();
|
|
49
79
|
sockets.clear();
|
|
@@ -53,13 +83,15 @@ function createDesktopClient(options = {}) {
|
|
|
53
83
|
if (session && session.expiresAt > now()) return session;
|
|
54
84
|
if (pending) return pending;
|
|
55
85
|
const current = generation;
|
|
86
|
+
const controller = new AbortController();
|
|
56
87
|
const task = (async () => {
|
|
57
|
-
const launch = await bootstrap();
|
|
88
|
+
const launch = await bootstrap(controller.signal);
|
|
58
89
|
if (disposed || generation !== current) throw new Error("Desktop channel attempt was superseded");
|
|
59
90
|
const result = await request(origin + "/__framework/handshake", {
|
|
60
91
|
method: "POST",
|
|
61
92
|
headers: { "content-type": "application/json" },
|
|
62
|
-
body: JSON.stringify({ bootstrap: launch.bootstrap })
|
|
93
|
+
body: JSON.stringify({ bootstrap: launch.bootstrap }),
|
|
94
|
+
signal: controller.signal
|
|
63
95
|
});
|
|
64
96
|
if (!result.ok) throw new Error("Desktop channel handshake failed");
|
|
65
97
|
const reply = await result.json();
|
|
@@ -86,45 +118,68 @@ function createDesktopClient(options = {}) {
|
|
|
86
118
|
};
|
|
87
119
|
})();
|
|
88
120
|
pending = task;
|
|
121
|
+
pendingController = controller;
|
|
89
122
|
try {
|
|
90
123
|
return await task;
|
|
91
124
|
} finally {
|
|
92
|
-
if (pending === task)
|
|
125
|
+
if (pending === task) {
|
|
126
|
+
pending = void 0;
|
|
127
|
+
pendingController = void 0;
|
|
128
|
+
}
|
|
93
129
|
}
|
|
94
130
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
131
|
+
/** Callbacks are not awaited: message failures close this socket and close callback failures are ignored. */
|
|
132
|
+
async function openStream(name, input, onMessage, onClose, bound) {
|
|
133
|
+
if (!/^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/.test(name) || name.length > 128 || typeof onMessage !== "function") throw new Error("Invalid stream request");
|
|
134
|
+
const body = JSON.stringify({
|
|
135
|
+
name,
|
|
136
|
+
input
|
|
137
|
+
});
|
|
138
|
+
if (new TextEncoder().encode(body).byteLength > 4096) throw new Error("Stream request too large");
|
|
139
|
+
const active = bound ? bound.active : await connect();
|
|
140
|
+
if (disposed || !active || active.expiresAt <= now()) throw new Error("Desktop channel is no longer active");
|
|
141
|
+
const streamGeneration = bound ? bound.generation : generation;
|
|
142
|
+
const isCurrent = () => {
|
|
143
|
+
try {
|
|
144
|
+
return !disposed && session === active && generation === streamGeneration && active.expiresAt > now();
|
|
145
|
+
} catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
if (!isCurrent()) throw new Error("Desktop channel is no longer active");
|
|
150
|
+
const { nonce, timestamp, proof } = await signRequest(crypto, active, "framework-stream-attach-v1", body, now());
|
|
151
|
+
if (!isCurrent()) throw new Error("Desktop channel is no longer active");
|
|
152
|
+
const socket = (options.createWebSocket ?? ((url) => new WebSocket(url)))(origin.replace(/^http:/, "ws:") + "/__framework/stream");
|
|
153
|
+
socket.binaryType = "arraybuffer";
|
|
154
|
+
sockets.add(socket);
|
|
155
|
+
return await new Promise((resolve, reject) => {
|
|
156
|
+
let ready = false, settled = false, failed = false;
|
|
157
|
+
const timer = setTimeout(() => {
|
|
158
|
+
fail();
|
|
159
|
+
closeSocket();
|
|
160
|
+
}, 5e3);
|
|
161
|
+
const fail = () => {
|
|
162
|
+
if (!settled) {
|
|
163
|
+
settled = true;
|
|
164
|
+
failed = true;
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
sockets.delete(socket);
|
|
167
|
+
reject(/* @__PURE__ */ new Error("Desktop stream unavailable"));
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const closeSocket = () => {
|
|
171
|
+
try {
|
|
112
172
|
socket.close();
|
|
173
|
+
} catch {}
|
|
174
|
+
};
|
|
175
|
+
socket.addEventListener("open", () => {
|
|
176
|
+
if (failed) return;
|
|
177
|
+
if (!isCurrent()) {
|
|
113
178
|
fail();
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
clearTimeout(timer);
|
|
119
|
-
reject(/* @__PURE__ */ new Error("Desktop stream unavailable"));
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
socket.addEventListener("open", () => {
|
|
123
|
-
if (disposed || session !== active) {
|
|
124
|
-
socket.close();
|
|
125
|
-
fail();
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
179
|
+
closeSocket();
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
128
183
|
socket.send(JSON.stringify({
|
|
129
184
|
channelId: active.channelId,
|
|
130
185
|
credentialId: active.credentialId,
|
|
@@ -133,53 +188,85 @@ function createDesktopClient(options = {}) {
|
|
|
133
188
|
body,
|
|
134
189
|
proof
|
|
135
190
|
}));
|
|
136
|
-
}
|
|
137
|
-
socket.addEventListener("message", (event) => {
|
|
138
|
-
if (!ready) {
|
|
139
|
-
if (event.data !== "{\"ok\":true}" || disposed || session !== active) {
|
|
140
|
-
socket.close();
|
|
141
|
-
fail();
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
ready = true;
|
|
145
|
-
settled = true;
|
|
146
|
-
clearTimeout(timer);
|
|
147
|
-
resolve({
|
|
148
|
-
send(payload) {
|
|
149
|
-
const length = typeof payload === "string" ? new TextEncoder().encode(payload).byteLength : payload instanceof ArrayBuffer ? payload.byteLength : Infinity;
|
|
150
|
-
if (disposed || session !== active || socket.readyState !== WebSocket.OPEN || length > 262144 || socket.bufferedAmount > 1048576) {
|
|
151
|
-
socket.close();
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
try {
|
|
155
|
-
socket.send(payload);
|
|
156
|
-
return true;
|
|
157
|
-
} catch {
|
|
158
|
-
socket.close();
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
close: () => socket.close()
|
|
163
|
-
});
|
|
164
|
-
} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) try {
|
|
165
|
-
onMessage(event.data);
|
|
166
|
-
} catch {
|
|
167
|
-
socket.close();
|
|
168
|
-
}
|
|
169
|
-
else socket.close();
|
|
170
|
-
});
|
|
171
|
-
socket.addEventListener("error", () => {
|
|
172
|
-
socket.close();
|
|
191
|
+
} catch {
|
|
173
192
|
fail();
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
193
|
+
closeSocket();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
socket.addEventListener("message", (event) => {
|
|
197
|
+
if (failed) return;
|
|
198
|
+
if (!isCurrent()) {
|
|
180
199
|
fail();
|
|
181
|
-
|
|
200
|
+
closeSocket();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (!ready) {
|
|
204
|
+
if (event.data !== "{\"ok\":true}") {
|
|
205
|
+
fail();
|
|
206
|
+
closeSocket();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
ready = true;
|
|
210
|
+
settled = true;
|
|
211
|
+
clearTimeout(timer);
|
|
212
|
+
resolve({
|
|
213
|
+
send(payload) {
|
|
214
|
+
const length = typeof payload === "string" ? new TextEncoder().encode(payload).byteLength : payload instanceof ArrayBuffer ? payload.byteLength : Infinity;
|
|
215
|
+
if (!isCurrent() || socket.readyState !== WebSocket.OPEN || length > 262144 || socket.bufferedAmount > 1048576) {
|
|
216
|
+
socket.close();
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
socket.send(payload);
|
|
221
|
+
return true;
|
|
222
|
+
} catch {
|
|
223
|
+
socket.close();
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
close: () => socket.close()
|
|
228
|
+
});
|
|
229
|
+
} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) try {
|
|
230
|
+
const result = onMessage(event.data);
|
|
231
|
+
if (result !== void 0) Promise.resolve(result).catch(() => {
|
|
232
|
+
try {
|
|
233
|
+
socket.close();
|
|
234
|
+
} catch {}
|
|
235
|
+
});
|
|
236
|
+
} catch {
|
|
237
|
+
socket.close();
|
|
238
|
+
}
|
|
239
|
+
else socket.close();
|
|
240
|
+
});
|
|
241
|
+
socket.addEventListener("error", () => {
|
|
242
|
+
fail();
|
|
243
|
+
closeSocket();
|
|
244
|
+
});
|
|
245
|
+
socket.addEventListener("close", () => {
|
|
246
|
+
sockets.delete(socket);
|
|
247
|
+
if (ready) try {
|
|
248
|
+
const result = onClose?.();
|
|
249
|
+
if (result !== void 0) Promise.resolve(result).catch(() => {});
|
|
250
|
+
} catch {}
|
|
251
|
+
fail();
|
|
182
252
|
});
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async function reconnectSession() {
|
|
256
|
+
if (disposed) throw new Error("Desktop client is closed");
|
|
257
|
+
generation++;
|
|
258
|
+
session = void 0;
|
|
259
|
+
abortInvokes();
|
|
260
|
+
pendingController?.abort();
|
|
261
|
+
pendingController = void 0;
|
|
262
|
+
pending = void 0;
|
|
263
|
+
streamSessions = /* @__PURE__ */ new WeakMap();
|
|
264
|
+
closeSockets();
|
|
265
|
+
return connect();
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
openStream(name, input, onMessage, onClose) {
|
|
269
|
+
return openStream(name, input, onMessage, onClose, void 0);
|
|
183
270
|
},
|
|
184
271
|
async invoke(name, input) {
|
|
185
272
|
if (!/^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/.test(name) || name.length > 128) throw new Error("Invalid service name");
|
|
@@ -187,43 +274,82 @@ function createDesktopClient(options = {}) {
|
|
|
187
274
|
name,
|
|
188
275
|
input
|
|
189
276
|
});
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
"
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
277
|
+
const controller = new AbortController();
|
|
278
|
+
invokes.add(controller);
|
|
279
|
+
try {
|
|
280
|
+
const active = await awaitWithAbort(connect(), controller.signal);
|
|
281
|
+
const throwIfAborted = () => {
|
|
282
|
+
if (controller.signal.aborted) throw abortReason(controller.signal);
|
|
283
|
+
};
|
|
284
|
+
const { nonce, timestamp, proof } = await awaitWithAbort(signRequest(crypto, active, "adea-invoke-v1", body, now()), controller.signal);
|
|
285
|
+
throwIfAborted();
|
|
286
|
+
if (disposed || session !== active) throw new Error("Desktop channel is no longer active");
|
|
287
|
+
const result = await awaitWithAbort(request(origin + "/__framework/invoke", {
|
|
288
|
+
method: "POST",
|
|
289
|
+
headers: {
|
|
290
|
+
"content-type": "application/json",
|
|
291
|
+
"x-adea-channel": active.channelId,
|
|
292
|
+
"x-adea-credential": active.credentialId,
|
|
293
|
+
"x-adea-nonce": nonce,
|
|
294
|
+
"x-adea-timestamp": timestamp,
|
|
295
|
+
"x-adea-proof": proof
|
|
296
|
+
},
|
|
297
|
+
body,
|
|
298
|
+
signal: controller.signal
|
|
299
|
+
}), controller.signal);
|
|
300
|
+
throwIfAborted();
|
|
301
|
+
if (result.status === 401) {
|
|
302
|
+
if (session === active) session = void 0;
|
|
303
|
+
throw new Error("Desktop channel is no longer active");
|
|
304
|
+
}
|
|
305
|
+
if (!result.ok) throw new Error("Desktop service request failed");
|
|
306
|
+
const value = await awaitWithAbort(result.json(), controller.signal);
|
|
307
|
+
throwIfAborted();
|
|
308
|
+
if (typeof value !== "object" || value === null || typeof value.ok !== "boolean") throw new Error("Invalid desktop service reply");
|
|
309
|
+
return value;
|
|
310
|
+
} finally {
|
|
311
|
+
invokes.delete(controller);
|
|
208
312
|
}
|
|
209
|
-
if (!result.ok) throw new Error("Desktop service request failed");
|
|
210
|
-
const value = await result.json();
|
|
211
|
-
if (typeof value !== "object" || value === null || typeof value.ok !== "boolean") throw new Error("Invalid desktop service reply");
|
|
212
|
-
return value;
|
|
213
313
|
},
|
|
214
314
|
async reconnect() {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
315
|
+
await reconnectSession();
|
|
316
|
+
},
|
|
317
|
+
/** Explicitly reconnect and return a token tied to that exact channel. */
|
|
318
|
+
async reconnectForStreams() {
|
|
319
|
+
const active = await reconnectSession();
|
|
320
|
+
const bound = {
|
|
321
|
+
active,
|
|
322
|
+
generation
|
|
323
|
+
};
|
|
324
|
+
if (disposed || session !== active || active.expiresAt <= now()) throw new Error("Desktop channel is no longer active");
|
|
325
|
+
const token = Object.freeze({});
|
|
326
|
+
streamSessions.set(token, bound);
|
|
327
|
+
return token;
|
|
328
|
+
},
|
|
329
|
+
/** Check token provenance and currentness without acquiring credentials. */
|
|
330
|
+
isStreamSessionCurrent(token) {
|
|
331
|
+
const bound = streamSessions.get(token);
|
|
332
|
+
try {
|
|
333
|
+
return !!bound && !disposed && session === bound.active && generation === bound.generation && bound.active.expiresAt > now();
|
|
334
|
+
} catch {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
/** Attach only through a current token from this client; never renew credentials. */
|
|
339
|
+
openStreamForSession(token, name, input, onMessage, onClose) {
|
|
340
|
+
const bound = streamSessions.get(token);
|
|
341
|
+
if (!bound) return Promise.reject(/* @__PURE__ */ new Error("Desktop channel is no longer active"));
|
|
342
|
+
return openStream(name, input, onMessage, onClose, bound);
|
|
221
343
|
},
|
|
222
344
|
dispose() {
|
|
223
345
|
disposed = true;
|
|
224
346
|
generation++;
|
|
225
347
|
session = void 0;
|
|
348
|
+
abortInvokes();
|
|
349
|
+
pendingController?.abort();
|
|
350
|
+
pendingController = void 0;
|
|
226
351
|
pending = void 0;
|
|
352
|
+
streamSessions = /* @__PURE__ */ new WeakMap();
|
|
227
353
|
closeSockets();
|
|
228
354
|
}
|
|
229
355
|
};
|
|
@@ -12,6 +12,8 @@ export declare function createDesktopGateway<Principal>(options: {
|
|
|
12
12
|
now?: () => number;
|
|
13
13
|
maxConcurrent?: number;
|
|
14
14
|
readTimeoutMs?: number;
|
|
15
|
+
/** Opt-in deadline for service parsing, authorization, and execution; omitted means no deadline. */
|
|
16
|
+
dispatchTimeoutMs?: number;
|
|
15
17
|
windowGrant?: ReturnType<typeof createNativeWindowGrant<Principal>>;
|
|
16
18
|
}): {
|
|
17
19
|
handle(request: Request): Promise<Response>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { MAX_SERVICE_BODY_BYTES } from "../app/service-registry.js";
|
|
2
2
|
import { createChannelProofVerifier } from "./channel-proof.js";
|
|
3
3
|
import { createLoopbackPolicy, matchesLoopbackOrigin } from "./loopback-policy.js";
|
|
4
|
+
import { isValidGatewayTimeoutMs } from "./gateway-limits.js";
|
|
4
5
|
// src/desktop/desktop-gateway.ts
|
|
5
6
|
var response = (status) => new Response(null, {
|
|
6
7
|
status,
|
|
@@ -17,45 +18,88 @@ var json = (value) => Response.json(value, { headers: {
|
|
|
17
18
|
async function readBody(request, limit, timeoutMs) {
|
|
18
19
|
const declared = request.headers.get("content-length");
|
|
19
20
|
if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > limit)) return { status: 413 };
|
|
21
|
+
if (request.signal.aborted) return { status: 408 };
|
|
20
22
|
const reader = request.body?.getReader();
|
|
21
23
|
if (!reader) return {
|
|
22
24
|
body: "",
|
|
23
25
|
length: 0
|
|
24
26
|
};
|
|
25
27
|
let timedOut = false;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
let interrupted = false;
|
|
29
|
+
let interruptActiveRead;
|
|
30
|
+
const interrupt = (timeout) => {
|
|
31
|
+
if (interrupted) return;
|
|
32
|
+
interrupted = true;
|
|
33
|
+
timedOut = timeout;
|
|
28
34
|
reader.cancel().catch(() => {});
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
interruptActiveRead?.();
|
|
36
|
+
};
|
|
37
|
+
const onAbort = () => interrupt(false);
|
|
38
|
+
request.signal.addEventListener("abort", onAbort, { once: true });
|
|
39
|
+
const timer = setTimeout(() => interrupt(true), timeoutMs);
|
|
40
|
+
let bytes = /* @__PURE__ */ new Uint8Array(0);
|
|
31
41
|
let length = 0;
|
|
42
|
+
let pendingRead;
|
|
32
43
|
try {
|
|
33
44
|
while (true) {
|
|
34
|
-
|
|
45
|
+
if (interrupted) return { status: 408 };
|
|
46
|
+
const read = reader.read();
|
|
47
|
+
pendingRead = read;
|
|
48
|
+
const outcome = await new Promise((resolve) => {
|
|
49
|
+
let settled = false;
|
|
50
|
+
const finish = (value) => {
|
|
51
|
+
if (settled) return;
|
|
52
|
+
settled = true;
|
|
53
|
+
if (interruptActiveRead === onInterrupt) interruptActiveRead = void 0;
|
|
54
|
+
resolve(value);
|
|
55
|
+
};
|
|
56
|
+
const onInterrupt = () => finish({ type: "interrupted" });
|
|
57
|
+
interruptActiveRead = onInterrupt;
|
|
58
|
+
read.then((value) => finish({
|
|
59
|
+
type: "read",
|
|
60
|
+
value
|
|
61
|
+
}), (error) => finish({
|
|
62
|
+
type: "error",
|
|
63
|
+
error
|
|
64
|
+
}));
|
|
65
|
+
});
|
|
66
|
+
if (interrupted || outcome.type === "interrupted") return { status: 408 };
|
|
67
|
+
pendingRead = void 0;
|
|
68
|
+
if (outcome.type === "error") throw outcome.error;
|
|
69
|
+
const next = outcome.value;
|
|
35
70
|
if (timedOut) return { status: 408 };
|
|
36
71
|
if (next.done) break;
|
|
37
|
-
length
|
|
38
|
-
if (
|
|
72
|
+
const nextLength = length + next.value.byteLength;
|
|
73
|
+
if (nextLength > limit) {
|
|
39
74
|
reader.cancel().catch(() => {});
|
|
40
75
|
return { status: 413 };
|
|
41
76
|
}
|
|
42
|
-
|
|
77
|
+
if (nextLength > bytes.byteLength) {
|
|
78
|
+
const capacity = Math.min(limit, Math.max(nextLength, Math.max(1024, bytes.byteLength * 2)));
|
|
79
|
+
const grown = new Uint8Array(capacity);
|
|
80
|
+
grown.set(bytes.subarray(0, length));
|
|
81
|
+
bytes = grown;
|
|
82
|
+
}
|
|
83
|
+
bytes.set(next.value, length);
|
|
84
|
+
length = nextLength;
|
|
43
85
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
86
|
+
let body;
|
|
87
|
+
try {
|
|
88
|
+
body = new TextDecoder("utf-8", { fatal: true }).decode(bytes.subarray(0, length));
|
|
89
|
+
} catch {
|
|
90
|
+
return { status: 400 };
|
|
49
91
|
}
|
|
50
92
|
return {
|
|
51
|
-
body
|
|
93
|
+
body,
|
|
52
94
|
length
|
|
53
95
|
};
|
|
54
96
|
} catch {
|
|
55
|
-
return { status: timedOut ? 408 : 413 };
|
|
97
|
+
return { status: timedOut || request.signal.aborted ? 408 : 413 };
|
|
56
98
|
} finally {
|
|
57
99
|
clearTimeout(timer);
|
|
58
|
-
|
|
100
|
+
request.signal.removeEventListener("abort", onAbort);
|
|
101
|
+
if (pendingRead) pendingRead.then(() => reader.releaseLock(), () => reader.releaseLock());
|
|
102
|
+
else reader.releaseLock();
|
|
59
103
|
}
|
|
60
104
|
}
|
|
61
105
|
function parse(body) {
|
|
@@ -73,7 +117,8 @@ function parse(body) {
|
|
|
73
117
|
function createDesktopGateway(options) {
|
|
74
118
|
const maxConcurrent = options.maxConcurrent ?? 64;
|
|
75
119
|
const readTimeoutMs = options.readTimeoutMs ?? 15e3;
|
|
76
|
-
|
|
120
|
+
const dispatchTimeoutMs = options.dispatchTimeoutMs;
|
|
121
|
+
if (!Number.isSafeInteger(maxConcurrent) || maxConcurrent < 1 || !Number.isSafeInteger(readTimeoutMs) || readTimeoutMs < 1 || readTimeoutMs > 2147483647 || !isValidGatewayTimeoutMs(dispatchTimeoutMs)) throw new Error("Invalid gateway limits");
|
|
77
122
|
let inFlight = 0;
|
|
78
123
|
const policy = createLoopbackPolicy(options.origin);
|
|
79
124
|
if (options.windowGrant && options.windowGrant.origin !== policy.origin) throw new Error("Window grant origin mismatch");
|
|
@@ -95,6 +140,13 @@ function createDesktopGateway(options) {
|
|
|
95
140
|
if (!/^application\/json(?:\s*;|\s*$)/i.test(request.headers.get("content-type") ?? "")) return response(415);
|
|
96
141
|
if (inFlight >= maxConcurrent) return response(429);
|
|
97
142
|
inFlight++;
|
|
143
|
+
let releaseOnExit = true;
|
|
144
|
+
let slotReleased = false;
|
|
145
|
+
const releaseSlot = () => {
|
|
146
|
+
if (slotReleased) return;
|
|
147
|
+
slotReleased = true;
|
|
148
|
+
inFlight--;
|
|
149
|
+
};
|
|
98
150
|
try {
|
|
99
151
|
const received = await readBody(request, url.pathname.endsWith("/invoke") ? options.services.maxBodyBytes() : 4096, readTimeoutMs);
|
|
100
152
|
if ("status" in received) return response(received.status);
|
|
@@ -131,12 +183,72 @@ function createDesktopGateway(options) {
|
|
|
131
183
|
if (!result.ok) return response(401);
|
|
132
184
|
if (typeof input.name !== "string") return response(400);
|
|
133
185
|
if (received.length > options.services.maxBodyBytes(input.name)) return response(413);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
186
|
+
if (request.signal.aborted) return response(408);
|
|
187
|
+
const controller = new AbortController();
|
|
188
|
+
let resolveInterruption;
|
|
189
|
+
let interruptionSettled = false;
|
|
190
|
+
const interrupted = new Promise((resolve) => {
|
|
191
|
+
resolveInterruption = resolve;
|
|
192
|
+
});
|
|
193
|
+
const deadlineAt = dispatchTimeoutMs === void 0 ? void 0 : performance.now() + dispatchTimeoutMs;
|
|
194
|
+
const serviceTimeoutReason = new DOMException("Desktop service dispatch timed out", "TimeoutError");
|
|
195
|
+
const interrupt = (reason, abortReason) => {
|
|
196
|
+
if (interruptionSettled) return;
|
|
197
|
+
interruptionSettled = true;
|
|
198
|
+
if (!controller.signal.aborted) controller.abort(abortReason);
|
|
199
|
+
resolveInterruption({
|
|
200
|
+
type: "interrupted",
|
|
201
|
+
reason
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
const expireIfOverdue = () => {
|
|
205
|
+
if (deadlineAt !== void 0 && performance.now() >= deadlineAt) interrupt("timeout", serviceTimeoutReason);
|
|
206
|
+
};
|
|
207
|
+
const onRequestAbort = () => interrupt("caller", request.signal.reason);
|
|
208
|
+
request.signal.addEventListener("abort", onRequestAbort, { once: true });
|
|
209
|
+
const timer = dispatchTimeoutMs === void 0 ? void 0 : setTimeout(() => interrupt("timeout", serviceTimeoutReason), dispatchTimeoutMs);
|
|
210
|
+
const cleanupDispatch = () => {
|
|
211
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
212
|
+
request.signal.removeEventListener("abort", onRequestAbort);
|
|
213
|
+
};
|
|
214
|
+
if (request.signal.aborted) onRequestAbort();
|
|
215
|
+
const settled = Promise.resolve().then(() => {
|
|
216
|
+
if (controller.signal.aborted) return {
|
|
217
|
+
ok: false,
|
|
218
|
+
code: "unauthorized"
|
|
219
|
+
};
|
|
220
|
+
return options.services.dispatch(input.name, input.input, result.principal, () => {
|
|
221
|
+
expireIfOverdue();
|
|
222
|
+
if (controller.signal.aborted) return false;
|
|
223
|
+
const channel = options.authority.resolve(identity);
|
|
224
|
+
return channel !== void 0 && Object.is(channel.principal, result.principal);
|
|
225
|
+
}, controller.signal, deadlineAt === void 0 ? void 0 : () => {
|
|
226
|
+
expireIfOverdue();
|
|
227
|
+
return !controller.signal.aborted;
|
|
228
|
+
});
|
|
229
|
+
}).then((service) => {
|
|
230
|
+
expireIfOverdue();
|
|
231
|
+
releaseSlot();
|
|
232
|
+
return {
|
|
233
|
+
type: "result",
|
|
234
|
+
service
|
|
235
|
+
};
|
|
236
|
+
}, () => {
|
|
237
|
+
expireIfOverdue();
|
|
238
|
+
releaseSlot();
|
|
239
|
+
return { type: "failed" };
|
|
240
|
+
});
|
|
241
|
+
releaseOnExit = false;
|
|
242
|
+
try {
|
|
243
|
+
const outcome = await Promise.race([settled, interrupted]);
|
|
244
|
+
if (outcome.type === "interrupted") return response(outcome.reason === "timeout" ? 504 : 408);
|
|
245
|
+
if (outcome.type === "failed") return response(500);
|
|
246
|
+
return json(outcome.service);
|
|
247
|
+
} finally {
|
|
248
|
+
cleanupDispatch();
|
|
249
|
+
}
|
|
138
250
|
} finally {
|
|
139
|
-
|
|
251
|
+
if (releaseOnExit) releaseSlot();
|
|
140
252
|
}
|
|
141
253
|
} catch {
|
|
142
254
|
return response(500);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type StreamPayload = string | Uint8Array;
|
|
2
|
+
type BunWebSocket = {
|
|
3
|
+
readyState: number;
|
|
4
|
+
getBufferedAmount(): number;
|
|
5
|
+
send(payload: StreamPayload): number;
|
|
6
|
+
};
|
|
7
|
+
export declare const DESKTOP_STREAM_BACKPRESSURE_LIMIT: number;
|
|
8
|
+
/** Adapt Bun's byte-count send result to the boolean contract used by streams. */
|
|
9
|
+
export declare function sendDesktopStreamPayload(socket: BunWebSocket, payload: StreamPayload): boolean;
|
|
10
|
+
export {};
|