buckyos 0.7.200 → 0.7.203

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.
Files changed (75) hide show
  1. package/README.md +2 -0
  2. package/cli/dist/cli.mjs +42 -1
  3. package/cli/dist/cli.mjs.map +1 -1
  4. package/cli/modules/pikg.ts +7 -0
  5. package/cli/modules/pikg_protocol.ts +37 -0
  6. package/cli/version.ts +1 -1
  7. package/dist/{aiws_write-2f0baa6f.mjs → aiws_write-0606e098.mjs} +707 -291
  8. package/dist/aiws_write-0606e098.mjs.map +1 -0
  9. package/dist/aiws_write-7a9d71d1.js +13 -0
  10. package/dist/aiws_write-7a9d71d1.js.map +1 -0
  11. package/dist/app-frame.cjs +1 -1
  12. package/dist/app-frame.cjs.map +1 -1
  13. package/dist/app-frame.d.ts +129 -24
  14. package/dist/app-frame.d.ts.map +1 -1
  15. package/dist/app-frame.mjs +229 -70
  16. package/dist/app-frame.mjs.map +1 -1
  17. package/dist/audio.d.ts +84 -0
  18. package/dist/audio.d.ts.map +1 -0
  19. package/dist/browser.cjs +1 -1
  20. package/dist/browser.cjs.map +1 -1
  21. package/dist/browser.d.ts +5 -0
  22. package/dist/browser.d.ts.map +1 -1
  23. package/dist/browser.mjs +249 -219
  24. package/dist/browser.mjs.map +1 -1
  25. package/dist/content.cjs +1 -1
  26. package/dist/content.cjs.map +1 -1
  27. package/dist/content.mjs +357 -34
  28. package/dist/content.mjs.map +1 -1
  29. package/dist/{content_objid-0e63f1ff.js → content_objid-c69251aa.js} +2 -2
  30. package/dist/{content_objid-0e63f1ff.js.map → content_objid-c69251aa.js.map} +1 -1
  31. package/dist/{content_objid-8ebeead4.mjs → content_objid-e3fc56dd.mjs} +2 -2
  32. package/dist/{content_objid-8ebeead4.mjs.map → content_objid-e3fc56dd.mjs.map} +1 -1
  33. package/dist/index.cjs +1 -1
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.ts +5 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.mjs +249 -219
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/msg_center_client.d.ts +37 -0
  40. package/dist/msg_center_client.d.ts.map +1 -1
  41. package/dist/node.cjs +1 -1
  42. package/dist/node.cjs.map +1 -1
  43. package/dist/node.d.ts +5 -0
  44. package/dist/node.d.ts.map +1 -1
  45. package/dist/node.mjs +246 -215
  46. package/dist/node.mjs.map +1 -1
  47. package/dist/open-app.cjs +2 -0
  48. package/dist/open-app.cjs.map +1 -0
  49. package/dist/open-app.mjs +160 -0
  50. package/dist/open-app.mjs.map +1 -0
  51. package/dist/open_app.d.ts +95 -0
  52. package/dist/open_app.d.ts.map +1 -0
  53. package/dist/provision.cjs +1 -1
  54. package/dist/provision.cjs.map +1 -1
  55. package/dist/provision.mjs +1 -3
  56. package/dist/provision.mjs.map +1 -1
  57. package/dist/provision_activate.d.ts.map +1 -1
  58. package/dist/runtime_env-39e4df98.mjs +44 -0
  59. package/dist/runtime_env-39e4df98.mjs.map +1 -0
  60. package/dist/runtime_env-6396e1eb.js +2 -0
  61. package/dist/runtime_env-6396e1eb.js.map +1 -0
  62. package/dist/runtime_env.d.ts +53 -0
  63. package/dist/runtime_env.d.ts.map +1 -0
  64. package/dist/sdk_core.d.ts +4 -2
  65. package/dist/sdk_core.d.ts.map +1 -1
  66. package/dist/zone_clients.d.ts +35 -0
  67. package/dist/zone_clients.d.ts.map +1 -0
  68. package/package.json +6 -1
  69. package/dist/aiws_write-2f0baa6f.mjs.map +0 -1
  70. package/dist/aiws_write-ad97b561.js +0 -13
  71. package/dist/aiws_write-ad97b561.js.map +0 -1
  72. package/dist/content_mime-dc80d960.js +0 -2
  73. package/dist/content_mime-dc80d960.js.map +0 -1
  74. package/dist/content_mime-e42d5f8d.mjs +0 -335
  75. package/dist/content_mime-e42d5f8d.mjs.map +0 -1
@@ -1,8 +1,66 @@
1
1
  import { SystemDialogError, isOpenRequest, readContentHints, readSelectDialogResult, systemOpenUrl, SELECT_DIALOG_PATH, isTransferableRef, isTransferableSession, SYSTEM_DIALOG_ERROR_CODES } from "./content.mjs";
2
- import "./content_mime-e42d5f8d.mjs";
2
+ import { readAppOpenRequest, systemOpenAppUrl } from "./open-app.mjs";
3
+ import { g as getBuckyApi } from "./runtime_env-39e4df98.mjs";
3
4
  import "./object_url-20e94b67.mjs";
4
5
  import "./ndn_types-0fac2499.mjs";
5
6
  const dialogErrorCode = (value) => SYSTEM_DIALOG_ERROR_CODES.includes(value) ? value : "protocol";
7
+ const text = (value, max) => typeof value === "string" ? value.slice(0, max) : void 0;
8
+ function readHeaderList(value, read) {
9
+ if (!Array.isArray(value))
10
+ return [];
11
+ return value.slice(0, 32).flatMap((entry) => {
12
+ const v = entry;
13
+ if (!v || typeof v !== "object" || typeof v.id !== "string" || !v.id || v.id.length > 128 || typeof v.label !== "string")
14
+ return [];
15
+ return [read(v, { id: v.id, label: v.label.slice(0, 128) })];
16
+ });
17
+ }
18
+ function readHeader(p) {
19
+ const header = {};
20
+ const title = text(p.title, 256);
21
+ if (title !== void 0)
22
+ header.title = title;
23
+ if (Array.isArray(p.second_row))
24
+ header.second_row = readHeaderList(p.second_row, (v, base) => ({ ...base, ...v.active === true ? { active: true } : {} }));
25
+ if (Array.isArray(p.menu))
26
+ header.menu = readHeaderList(p.menu, (v, base) => ({ ...base, ...typeof v.icon === "string" ? { icon: v.icon.slice(0, 2048) } : {}, ...v.danger === true ? { danger: true } : {} }));
27
+ if (p.search === null)
28
+ header.search = null;
29
+ else if (p.search && typeof p.search === "object") {
30
+ const search = p.search;
31
+ const placeholder = text(search.placeholder, 128), value = text(search.value, 1024);
32
+ header.search = { ...placeholder !== void 0 ? { placeholder } : {}, ...value !== void 0 ? { value } : {} };
33
+ }
34
+ if (typeof p.back === "boolean")
35
+ header.back = p.back;
36
+ return header;
37
+ }
38
+ function windowTransport(peer, origin) {
39
+ return {
40
+ post: (message) => {
41
+ var _a;
42
+ return (_a = peer()) == null ? void 0 : _a.postMessage(message, origin);
43
+ },
44
+ onMessage: (cb) => {
45
+ const own = window;
46
+ const listener = (event) => {
47
+ if (event.source === peer() && event.origin === origin)
48
+ cb(event.data);
49
+ };
50
+ own.addEventListener("message", listener);
51
+ return () => own.removeEventListener("message", listener);
52
+ }
53
+ };
54
+ }
55
+ function bridgeTransport(api) {
56
+ return {
57
+ post: (message) => {
58
+ void Promise.resolve(api.call("frame.post", message)).catch(() => {
59
+ });
60
+ },
61
+ onMessage: (cb) => api.on("frame.message", cb)
62
+ };
63
+ }
6
64
  const envelope = (type, payload, extra = {}) => ({ protocol: "buckyos.app-frame", version: 1, type, payload, ...extra });
7
65
  const isMessage = (value) => {
8
66
  if (!value || typeof value !== "object")
@@ -10,51 +68,91 @@ const isMessage = (value) => {
10
68
  const v = value;
11
69
  return v.protocol === "buckyos.app-frame" && v.version === 1 && typeof v.type === "string" && !!v.payload && typeof v.payload === "object";
12
70
  };
71
+ const OPEN_LIMIT = 5;
13
72
  class AppFrameHost {
14
73
  constructor(options) {
15
- this.options = options;
16
74
  this.ready = false;
17
75
  this.capabilities = [];
76
+ this.header = {};
18
77
  this.pending = /* @__PURE__ */ new Map();
19
- this.listener = (event) => this.receive(event);
20
- this.opens = [];
78
+ this.opens = /* @__PURE__ */ new Map();
21
79
  this.dialogOpen = false;
22
- window.addEventListener("message", this.listener);
80
+ this.callbacks = options;
81
+ this.framed = !options.transport;
82
+ this.init = options.transport ? { ...options.init, nonce: "" } : options.init;
83
+ this.channel = options.transport ?? windowTransport(options.target, options.origin);
84
+ this.detach = this.channel.onMessage((message) => this.receive(message));
85
+ }
86
+ limited(type) {
87
+ const recent = (this.opens.get(type) ?? []).filter((t) => t > Date.now() - 1e4);
88
+ this.opens.set(type, recent);
89
+ if (recent.length >= OPEN_LIMIT)
90
+ return true;
91
+ recent.push(Date.now());
92
+ return false;
93
+ }
94
+ async appOpen(id, payload) {
95
+ const reply = (result) => {
96
+ if (this.ready)
97
+ this.channel.post(envelope("app.openResult", result, { replyTo: id }));
98
+ };
99
+ if (!this.callbacks.onAppOpen)
100
+ return reply({ ok: false, code: "unsupported" });
101
+ const request = readAppOpenRequest(payload);
102
+ if (!request || this.limited("app.open"))
103
+ return reply({ ok: false, code: "denied" });
104
+ try {
105
+ reply(await this.callbacks.onAppOpen(request));
106
+ } catch {
107
+ reply({ ok: false, code: "denied" });
108
+ }
23
109
  }
24
110
  async systemDialog(id, path, params) {
25
111
  const reply = (payload) => {
26
- var _a;
27
112
  if (this.ready)
28
- (_a = this.options.target()) == null ? void 0 : _a.postMessage(envelope("sysdlg.openResult", payload, { replyTo: id }), this.options.origin);
113
+ this.channel.post(envelope("sysdlg.openResult", payload, { replyTo: id }));
29
114
  };
30
- if (!this.options.onSystemDialog || typeof path !== "string")
115
+ if (!this.callbacks.onSystemDialog || typeof path !== "string")
31
116
  return reply({ ok: false, code: "unsupported" });
32
117
  if (this.dialogOpen)
33
118
  return reply({ ok: false, code: "busy" });
34
119
  this.dialogOpen = true;
35
120
  try {
36
- reply(await this.options.onSystemDialog(path, params));
121
+ reply(await this.callbacks.onSystemDialog(path, params));
37
122
  } catch (error) {
38
123
  reply({ ok: false, code: error instanceof SystemDialogError ? error.code : "closed" });
39
124
  } finally {
40
125
  this.dialogOpen = false;
41
126
  }
42
127
  }
43
- receive(event) {
44
- var _a, _b, _c, _d, _e, _f, _g, _h;
45
- if (event.source !== this.options.target() || event.origin !== this.options.origin || !isMessage(event.data))
128
+ receive(m) {
129
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
130
+ if (!isMessage(m))
46
131
  return;
47
- const m = event.data;
48
132
  const p = m.payload;
49
133
  if (m.type === "frame.hello") {
50
- if (p.nonce !== this.options.init.nonce || !Array.isArray(p.appCapabilities))
134
+ if (!Array.isArray(p.appCapabilities))
51
135
  return;
136
+ if (this.framed) {
137
+ if (p.nonce !== this.init.nonce)
138
+ return;
139
+ } else {
140
+ if (typeof p.nonce !== "string" || !p.nonce || p.nonce.length > 128)
141
+ return;
142
+ if (p.nonce !== this.init.nonce)
143
+ this.failPending("App frame reloaded");
144
+ this.init = { ...this.init, nonce: p.nonce };
145
+ }
52
146
  this.ready = true;
53
147
  this.capabilities = p.appCapabilities.filter((c) => typeof c === "string");
54
- this.send("frame.init", this.options.init);
148
+ this.send("frame.init", this.init);
55
149
  if (this.focused !== void 0)
56
150
  this.send("frame.focusChanged", { focused: this.focused });
57
- (_b = (_a = this.options).onReady) == null ? void 0 : _b.call(_a);
151
+ if (Object.keys(this.header).length) {
152
+ this.header = {};
153
+ (_b = (_a = this.callbacks).onHeader) == null ? void 0 : _b.call(_a, {});
154
+ }
155
+ (_d = (_c = this.callbacks).onReady) == null ? void 0 : _d.call(_c);
58
156
  return;
59
157
  }
60
158
  if (!this.ready)
@@ -69,24 +167,26 @@ class AppFrameHost {
69
167
  return;
70
168
  }
71
169
  if (m.type === "window.setTitle" && typeof p.title === "string")
72
- (_d = (_c = this.options).onTitle) == null ? void 0 : _d.call(_c, p.title.slice(0, 256), p.dirty === true);
170
+ (_f = (_e = this.callbacks).onTitle) == null ? void 0 : _f.call(_e, p.title.slice(0, 256), p.dirty === true);
73
171
  if (m.type === "window.requestClose")
74
- (_f = (_e = this.options).onRequestClose) == null ? void 0 : _f.call(_e);
172
+ (_h = (_g = this.callbacks).onRequestClose) == null ? void 0 : _h.call(_g);
173
+ if (m.type === "header.set") {
174
+ this.header = { ...this.header, ...readHeader(p) };
175
+ (_j = (_i = this.callbacks).onHeader) == null ? void 0 : _j.call(_i, this.header);
176
+ }
177
+ if (m.type === "app.open" && typeof m.id === "string")
178
+ void this.appOpen(m.id, p);
75
179
  if (m.type === "sysdlg.open" && typeof m.id === "string")
76
180
  void this.systemDialog(m.id, p.path, p.params);
77
181
  if (m.type === "content.open" && (p.target === "default" || p.target === "preview" || p.target === "chooser")) {
78
182
  const request = { ...p.request, requestId: crypto.randomUUID() };
79
- if (!isOpenRequest(request))
183
+ if (!isOpenRequest(request) || this.limited("content.open"))
80
184
  return;
81
- this.opens = this.opens.filter((t) => t > Date.now() - 1e4);
82
- if (this.opens.length >= 5)
83
- return;
84
- this.opens.push(Date.now());
85
- (_h = (_g = this.options).onContentOpen) == null ? void 0 : _h.call(_g, request, p.target, { newWindow: p.newWindow === true, hints: readContentHints(p.hints) });
185
+ (_l = (_k = this.callbacks).onContentOpen) == null ? void 0 : _l.call(_k, request, p.target, { newWindow: p.newWindow === true, hints: readContentHints(p.hints) });
86
186
  }
87
187
  }
88
188
  update(init) {
89
- this.options.init = { ...this.options.init, ...init };
189
+ this.init = { ...this.init, ...init };
90
190
  }
91
191
  /** Reports whether the App's window is the focused one; only a change is sent. */
92
192
  setFocused(focused) {
@@ -95,23 +195,25 @@ class AppFrameHost {
95
195
  this.focused = focused;
96
196
  this.send("frame.focusChanged", { focused });
97
197
  }
198
+ /** Tells the App what happened in the header the host draws for it. */
199
+ headerEvent(event) {
200
+ this.send("header.event", event);
201
+ }
98
202
  send(type, payload) {
99
- var _a;
100
203
  if (this.ready)
101
- (_a = this.options.target()) == null ? void 0 : _a.postMessage(envelope(type, payload), this.options.origin);
204
+ this.channel.post(envelope(type, payload));
102
205
  }
103
206
  request(type, payload, timeout = 3e3) {
104
207
  if (!this.ready)
105
208
  return Promise.reject(new Error("App frame is not ready"));
106
209
  const id = crypto.randomUUID();
107
210
  return new Promise((resolve, reject) => {
108
- var _a;
109
211
  const timer = setTimeout(() => {
110
212
  this.pending.delete(id);
111
213
  reject(new Error("App frame timed out"));
112
214
  }, timeout);
113
215
  this.pending.set(id, { resolve, reject, timer, type });
114
- (_a = this.options.target()) == null ? void 0 : _a.postMessage(envelope(type, payload, { id }), this.options.origin);
216
+ this.channel.post(envelope(type, payload, { id }));
115
217
  });
116
218
  }
117
219
  async open(request) {
@@ -130,20 +232,23 @@ class AppFrameHost {
130
232
  return false;
131
233
  return (await this.request("frame.back", {})).handled === true;
132
234
  }
133
- dispose() {
134
- window.removeEventListener("message", this.listener);
235
+ failPending(reason) {
135
236
  this.pending.forEach((p) => {
136
237
  clearTimeout(p.timer);
137
- p.reject(new Error("App frame disposed"));
238
+ p.reject(new Error(reason));
138
239
  });
139
240
  this.pending.clear();
241
+ }
242
+ dispose() {
243
+ this.detach();
244
+ this.failPending("App frame disposed");
140
245
  this.ready = false;
141
246
  }
142
247
  }
143
248
  function embedderOrigin(accept) {
144
249
  var _a;
145
250
  if (window.parent === window)
146
- return null;
251
+ return getBuckyApi() && accept(window.location.origin) ? window.location.origin : null;
147
252
  const candidates = [(_a = window.location.ancestorOrigins) == null ? void 0 : _a[0], document.referrer];
148
253
  for (const candidate of candidates) {
149
254
  if (!candidate)
@@ -168,29 +273,32 @@ const originInZone = (origin, zoneHost) => {
168
273
  class AppFrameClient {
169
274
  constructor(options) {
170
275
  this.options = options;
171
- this.listener = (event) => {
172
- void this.receive(event);
173
- };
276
+ this.header = {};
174
277
  this.dialogs = /* @__PURE__ */ new Map();
175
- this.origin = new URL(options.shellOrigin).origin;
176
- this.nonce = new URLSearchParams(window.location.hash.slice(1)).get("bfp");
177
- window.addEventListener("message", this.listener);
178
- if (window.parent !== window && this.nonce) {
179
- const hello = () => window.parent.postMessage(envelope("frame.hello", { nonce: this.nonce, appCapabilities: [
278
+ this.appOpens = /* @__PURE__ */ new Map();
279
+ const origin = new URL(options.shellOrigin).origin;
280
+ const bridge = window.parent === window ? getBuckyApi() : void 0;
281
+ this.native = !!bridge;
282
+ this.nonce = new URLSearchParams(window.location.hash.slice(1)).get("bfp") ?? (bridge ? crypto.randomUUID() : null);
283
+ this.channel = bridge ? bridgeTransport(bridge) : windowTransport(() => window.parent, origin);
284
+ this.detach = this.channel.onMessage((message) => {
285
+ void this.receive(message);
286
+ });
287
+ if ((bridge || window.parent !== window) && this.nonce) {
288
+ const hello = () => this.channel.post(envelope("frame.hello", { nonce: this.nonce, appCapabilities: [
180
289
  ...options.onOpen ? ["open"] : [],
181
290
  ...options.onBeforeClose ? ["beforeClose"] : [],
182
291
  ...options.onBack ? ["back"] : []
183
- ] }), this.origin);
292
+ ] }));
184
293
  hello();
185
294
  this.helloTimer = setInterval(hello, 250);
186
- this.helloDeadline = setTimeout(() => clearInterval(this.helloTimer), 1500);
295
+ this.helloDeadline = setTimeout(() => clearInterval(this.helloTimer), bridge ? 1e4 : 1500);
187
296
  }
188
297
  }
189
- async receive(event) {
190
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
191
- if (event.source !== window.parent || event.origin !== this.origin || !isMessage(event.data))
298
+ async receive(m) {
299
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
300
+ if (!isMessage(m))
192
301
  return;
193
- const m = event.data;
194
302
  const p = m.payload;
195
303
  if (m.type === "frame.init") {
196
304
  if (!this.nonce || p.nonce !== this.nonce || typeof p.locale !== "string" || !p.theme || typeof p.windowId !== "string" || p.launch && !isOpenRequest(p.launch))
@@ -200,6 +308,8 @@ class AppFrameClient {
200
308
  this.init = p;
201
309
  this.title = void 0;
202
310
  this.focused = void 0;
311
+ if (Object.keys(this.header).length)
312
+ this.send("header.set", this.header);
203
313
  (_b = (_a = this.options).onInit) == null ? void 0 : _b.call(_a, this.init);
204
314
  return;
205
315
  }
@@ -219,21 +329,33 @@ class AppFrameClient {
219
329
  pending.reject(new SystemDialogError("protocol"));
220
330
  return;
221
331
  }
222
- const reply = (payload) => window.parent.postMessage(envelope(m.type + "Result", payload, { replyTo: m.id }), this.origin);
332
+ if (m.type === "app.openResult") {
333
+ const code = p.code === "not_found" || p.code === "denied" ? p.code : "unsupported";
334
+ if (m.replyTo)
335
+ (_c = this.appOpens.get(m.replyTo)) == null ? void 0 : _c(p.ok === true ? { ok: true } : { ok: false, code });
336
+ return;
337
+ }
338
+ if (m.type === "header.event") {
339
+ if (p.kind !== "search" && p.kind !== "item" && p.kind !== "menu")
340
+ return;
341
+ (_e = (_d = this.options).onHeaderEvent) == null ? void 0 : _e.call(_d, { kind: p.kind, ...typeof p.id === "string" ? { id: p.id } : {}, ...typeof p.value === "string" ? { value: p.value } : {} });
342
+ return;
343
+ }
344
+ const reply = (payload) => this.channel.post(envelope(m.type + "Result", payload, { replyTo: m.id }));
223
345
  try {
224
346
  if (m.type === "frame.open" && isOpenRequest(p.request))
225
- reply(await ((_d = (_c = this.options).onOpen) == null ? void 0 : _d.call(_c, p.request)) ?? { accepted: false });
347
+ reply(await ((_g = (_f = this.options).onOpen) == null ? void 0 : _g.call(_f, p.request)) ?? { accepted: false });
226
348
  if (m.type === "frame.beforeClose")
227
- reply({ decision: await ((_f = (_e = this.options).onBeforeClose) == null ? void 0 : _f.call(_e, p.reason)) ?? "allow" });
349
+ reply({ decision: await ((_i = (_h = this.options).onBeforeClose) == null ? void 0 : _i.call(_h, p.reason)) ?? "allow" });
228
350
  if (m.type === "frame.back")
229
- reply({ handled: await ((_h = (_g = this.options).onBack) == null ? void 0 : _h.call(_g)) ?? false });
351
+ reply({ handled: await ((_k = (_j = this.options).onBack) == null ? void 0 : _k.call(_j)) ?? false });
230
352
  if (m.type === "frame.themeChanged" && (p.mode === "dark" || p.mode === "light"))
231
- (_j = (_i = this.options).onTheme) == null ? void 0 : _j.call(_i, p);
353
+ (_m = (_l = this.options).onTheme) == null ? void 0 : _m.call(_l, p);
232
354
  if (m.type === "frame.localeChanged" && typeof p.locale === "string")
233
- (_l = (_k = this.options).onLocale) == null ? void 0 : _l.call(_k, p.locale);
355
+ (_o = (_n = this.options).onLocale) == null ? void 0 : _o.call(_n, p.locale);
234
356
  if (m.type === "frame.focusChanged" && this.focused !== (p.focused === true)) {
235
357
  this.focused = p.focused === true;
236
- (_n = (_m = this.options).onFocus) == null ? void 0 : _n.call(_m, this.focused);
358
+ (_q = (_p = this.options).onFocus) == null ? void 0 : _q.call(_p, this.focused);
237
359
  }
238
360
  } catch {
239
361
  if (m.type === "frame.open")
@@ -244,7 +366,7 @@ class AppFrameClient {
244
366
  }
245
367
  send(type, payload) {
246
368
  if (this.init)
247
- window.parent.postMessage(envelope(type, payload), this.origin);
369
+ this.channel.post(envelope(type, payload));
248
370
  }
249
371
  setTitle(title, dirty = false) {
250
372
  const key = JSON.stringify([title, dirty]);
@@ -256,10 +378,44 @@ class AppFrameClient {
256
378
  requestClose() {
257
379
  this.send("window.requestClose", {});
258
380
  }
381
+ /** Sets what the host shows in the header above this App. It may be called before the handshake. */
382
+ setHeader(header) {
383
+ this.header = { ...this.header, ...header };
384
+ this.send("header.set", header);
385
+ }
386
+ /**
387
+ * Opens another App, or a page of one, in this App's Zone (or in `zone`). The host decides between a new window and
388
+ * an open one. In a standalone tab it opens the Zone's system entry (`systemOrigin`) in a new tab, which only reaches
389
+ * this App's own Zone. Never rejects.
390
+ */
391
+ openApp(request) {
392
+ const clean = readAppOpenRequest(request);
393
+ if (!clean)
394
+ return Promise.resolve({ ok: false, code: "denied" });
395
+ if (this.init) {
396
+ if (!this.init.shellCapabilities.includes("app.open"))
397
+ return Promise.resolve({ ok: false, code: "unsupported" });
398
+ const id = crypto.randomUUID();
399
+ return new Promise((resolve) => {
400
+ const settle = (reply) => {
401
+ clearTimeout(timer);
402
+ this.appOpens.delete(id);
403
+ resolve(reply);
404
+ };
405
+ const timer = setTimeout(() => settle({ ok: false, code: "unsupported" }), 3e4);
406
+ this.appOpens.set(id, settle);
407
+ this.channel.post(envelope("app.open", clean, { id }));
408
+ });
409
+ }
410
+ if (!this.options.systemOrigin || clean.zone)
411
+ return Promise.resolve({ ok: false, code: "unsupported" });
412
+ window.open(systemOpenAppUrl(this.options.systemOrigin, clean), "_blank", "noopener");
413
+ return Promise.resolve({ ok: true });
414
+ }
259
415
  /**
260
416
  * Asks the system to open content: with its default app, in Preview, or by letting the user choose (`chooser`).
261
- * Embedded in the shell the request goes over the frame; in a standalone tab it opens the Zone's system entry
262
- * (`systemOrigin`) in a new tab. Returns false when neither is available.
417
+ * Inside the shell or the BuckyOS client the request goes to the host; in a standalone tab it opens the Zone's system
418
+ * entry (`systemOrigin`) in a new tab. Returns false when neither is available.
263
419
  */
264
420
  openContent(request, target = "default", options = {}) {
265
421
  const hints = options.hints ?? {};
@@ -275,27 +431,29 @@ class AppFrameClient {
275
431
  /**
276
432
  * Shows the system file picker (`sysdlg/select`) and resolves with what the user chose; cancelling resolves with
277
433
  * `{ action: 'cancelled' }`. Embedded in the shell the shell draws the dialog over this App's window; in a
278
- * standalone tab the picker page of the Zone's system entry (`systemOrigin`) is framed over this page. Rejects
279
- * with a `SystemDialogError` when no picker is available or the request did not complete.
434
+ * standalone tab, and in the BuckyOS client unless it lists the dialog, the picker page of the Zone's system entry
435
+ * (`systemOrigin`) is framed over this page. Rejects with a `SystemDialogError` when no picker is available or the
436
+ * request did not complete.
280
437
  */
281
438
  selectContent(params = {}, options = {}) {
282
- var _a, _b;
283
- if (this.init) {
284
- if (!this.init.shellCapabilities.includes(SELECT_DIALOG_PATH))
439
+ var _a, _b, _c;
440
+ const offered = (_a = this.init) == null ? void 0 : _a.shellCapabilities.includes(SELECT_DIALOG_PATH);
441
+ if (this.init && (offered || !this.native)) {
442
+ if (!offered)
285
443
  return Promise.reject(new SystemDialogError("unsupported"));
286
444
  if (this.dialogs.size)
287
445
  return Promise.reject(new SystemDialogError("busy"));
288
446
  const id = crypto.randomUUID();
289
447
  return new Promise((resolve, reject) => {
290
448
  this.dialogs.set(id, { resolve, reject, allowLocal: params.allowLocal === true });
291
- window.parent.postMessage(envelope("sysdlg.open", { path: SELECT_DIALOG_PATH, params }, { id }), this.origin);
449
+ this.channel.post(envelope("sysdlg.open", { path: SELECT_DIALOG_PATH, params }, { id }));
292
450
  });
293
451
  }
294
452
  if (!this.options.systemOrigin)
295
453
  return Promise.reject(new SystemDialogError("unsupported"));
296
454
  const abort = this.embedAbort = new AbortController();
297
- (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => abort.abort(), { once: true });
298
- if ((_b = options.signal) == null ? void 0 : _b.aborted)
455
+ (_b = options.signal) == null ? void 0 : _b.addEventListener("abort", () => abort.abort(), { once: true });
456
+ if ((_c = options.signal) == null ? void 0 : _c.aborted)
299
457
  abort.abort();
300
458
  return openSelectEmbed(this.options.systemOrigin, params, { ...options, signal: abort.signal });
301
459
  }
@@ -303,7 +461,8 @@ class AppFrameClient {
303
461
  var _a;
304
462
  clearInterval(this.helloTimer);
305
463
  clearTimeout(this.helloDeadline);
306
- window.removeEventListener("message", this.listener);
464
+ this.detach();
465
+ this.appOpens.forEach((settle) => settle({ ok: false, code: "unsupported" }));
307
466
  this.dialogs.forEach((pending) => pending.reject(new SystemDialogError("closed")));
308
467
  this.dialogs.clear();
309
468
  (_a = this.embedAbort) == null ? void 0 : _a.abort();
@@ -519,9 +678,9 @@ function openSelectEmbed(systemOrigin, params, options = {}) {
519
678
  const panel = document.createElement("div");
520
679
  panel.setAttribute("role", "alert");
521
680
  panel.style.cssText = `max-width:22rem;margin:1rem;padding:1.25rem;border-radius:16px;font:14px/1.6 system-ui,sans-serif;background:${theme === "dark" ? "#1f2329" : "#fff"};color:${theme === "dark" ? "#f2f3f5" : "#1f2329"};box-shadow:0 24px 64px rgba(0,0,0,.24)`;
522
- const text = document.createElement("p");
523
- text.style.margin = "0 0 1rem";
524
- text.textContent = zh ? "无法加载文件选择器" : "Unable to load the file picker";
681
+ const text2 = document.createElement("p");
682
+ text2.style.margin = "0 0 1rem";
683
+ text2.textContent = zh ? "无法加载文件选择器" : "Unable to load the file picker";
525
684
  const actions = document.createElement("div");
526
685
  actions.style.cssText = "display:flex;gap:.5rem;justify-content:flex-end";
527
686
  const button = (label, action) => {
@@ -538,7 +697,7 @@ function openSelectEmbed(systemOrigin, params, options = {}) {
538
697
  panel.remove();
539
698
  load();
540
699
  });
541
- panel.append(text, actions);
700
+ panel.append(text2, actions);
542
701
  root.append(panel);
543
702
  retry.focus();
544
703
  };