buckyos 0.7.205 → 0.7.206
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/cli/dist/cli.mjs +1 -1
- package/cli/dist/cli.mjs.map +1 -1
- package/cli/version.ts +1 -1
- package/dist/app-frame.cjs +1 -1
- package/dist/app-frame.cjs.map +1 -1
- package/dist/app-frame.d.ts +218 -2
- package/dist/app-frame.d.ts.map +1 -1
- package/dist/app-frame.mjs +517 -12
- package/dist/app-frame.mjs.map +1 -1
- package/dist/content.cjs +1 -1
- package/dist/content.cjs.map +1 -1
- package/dist/content.d.ts +2 -0
- package/dist/content.d.ts.map +1 -1
- package/dist/content.mjs +14 -7
- package/dist/content.mjs.map +1 -1
- package/dist/internal/html_tokens.d.ts +13 -0
- package/dist/internal/html_tokens.d.ts.map +1 -0
- package/dist/transfer.cjs +2 -0
- package/dist/transfer.cjs.map +1 -0
- package/dist/transfer.d.ts +148 -0
- package/dist/transfer.d.ts.map +1 -0
- package/dist/transfer.mjs +655 -0
- package/dist/transfer.mjs.map +1 -0
- package/package.json +6 -1
package/dist/app-frame.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { SystemDialogError, isOpenRequest, readContentHints, readSelectDialogResult, systemOpenUrl, SELECT_DIALOG_PATH, isTransferableRef, isTransferableSession, SYSTEM_DIALOG_ERROR_CODES } from "./content.mjs";
|
|
1
|
+
import { readZoneContent, SystemDialogError, isOpenRequest, readContentHints, readSelectDialogResult, systemOpenUrl, SELECT_DIALOG_PATH, isTransferableRef, isTransferableSession, SYSTEM_DIALOG_ERROR_CODES } from "./content.mjs";
|
|
2
2
|
import { readAppOpenRequest, systemOpenAppUrl } from "./open-app.mjs";
|
|
3
3
|
import { g as getBuckyApi } from "./runtime_env-39e4df98.mjs";
|
|
4
|
+
import { MAX_CONTENT_REFS, CONTENT_REFS_FORMAT, readContentRefs, newOfferId } from "./transfer.mjs";
|
|
4
5
|
import "./object_url-20e94b67.mjs";
|
|
5
6
|
import "./ndn_types-0fac2499.mjs";
|
|
6
7
|
const dialogErrorCode = (value) => SYSTEM_DIALOG_ERROR_CODES.includes(value) ? value : "protocol";
|
|
@@ -37,15 +38,15 @@ function readHeader(p) {
|
|
|
37
38
|
}
|
|
38
39
|
function windowTransport(peer, origin) {
|
|
39
40
|
return {
|
|
40
|
-
post: (message) => {
|
|
41
|
+
post: (message, transfer) => {
|
|
41
42
|
var _a;
|
|
42
|
-
return (_a = peer()) == null ? void 0 : _a.postMessage(message, origin);
|
|
43
|
+
return (_a = peer()) == null ? void 0 : _a.postMessage(message, origin, transfer ?? []);
|
|
43
44
|
},
|
|
44
45
|
onMessage: (cb) => {
|
|
45
46
|
const own = window;
|
|
46
47
|
const listener = (event) => {
|
|
47
48
|
if (event.source === peer() && event.origin === origin)
|
|
48
|
-
cb(event.data);
|
|
49
|
+
cb(event.data, event.ports);
|
|
49
50
|
};
|
|
50
51
|
own.addEventListener("message", listener);
|
|
51
52
|
return () => own.removeEventListener("message", listener);
|
|
@@ -68,6 +69,219 @@ const isMessage = (value) => {
|
|
|
68
69
|
const v = value;
|
|
69
70
|
return v.protocol === "buckyos.app-frame" && v.version === 1 && typeof v.type === "string" && !!v.payload && typeof v.payload === "object";
|
|
70
71
|
};
|
|
72
|
+
const TRANSFER_CAPABILITY = "transfer.v1";
|
|
73
|
+
const TRANSFER_ERROR_CODES = ["unsupported", "stale", "denied"];
|
|
74
|
+
class TransferError extends Error {
|
|
75
|
+
constructor(code, message) {
|
|
76
|
+
super(message ?? `Transfer: ${code}`);
|
|
77
|
+
this.code = code;
|
|
78
|
+
this.name = "TransferError";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const TRANSFER_ID = /^[A-Za-z0-9_-]{16,128}$/;
|
|
82
|
+
const DOCUMENT_ID = /^[A-Za-z0-9_-]{8,128}$/;
|
|
83
|
+
const FORMAT_NAME = /^(?:files|[a-z0-9][a-z0-9!#$&^_.+-]{0,126}\/[a-z0-9][a-z0-9!#$&^_.+-]{0,126})$/;
|
|
84
|
+
const ITEM_KINDS = ["zone-file", "zone-folder", "local-file", "text", "object"];
|
|
85
|
+
const OPERATIONS = ["copy", "move", "link"];
|
|
86
|
+
const ITEM_STATUSES = ["done", "accepted", "skipped", "failed", "cancelled"];
|
|
87
|
+
const MAX_FORMATS = 32;
|
|
88
|
+
const MAX_INLINE = 1024 * 1024;
|
|
89
|
+
const MAX_STRING_VALUE = 16 * 1024 * 1024;
|
|
90
|
+
const TRANSFER_TIMEOUT = { offer: 5e3, probe: 1500, read: 1e4, deliver: 15e3, status: 3e3 };
|
|
91
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
92
|
+
const isTransferId = (v) => typeof v === "string" && TRANSFER_ID.test(v);
|
|
93
|
+
const isItemKey = (v) => typeof v === "string" && /^(0|[1-9]\d{0,5})$/.test(v);
|
|
94
|
+
const isFormat = (v) => typeof v === "string" && FORMAT_NAME.test(v);
|
|
95
|
+
const readOperations = (v) => {
|
|
96
|
+
if (!Array.isArray(v) || !v.length || v.length > 3 || !v.every((o) => OPERATIONS.includes(o)))
|
|
97
|
+
return void 0;
|
|
98
|
+
return [...new Set(v)];
|
|
99
|
+
};
|
|
100
|
+
const readFormats = (v) => Array.isArray(v) && v.length && v.length <= MAX_FORMATS && v.every(isFormat) ? [...new Set(v)] : void 0;
|
|
101
|
+
const readPoint = (v) => {
|
|
102
|
+
if (v === "focus")
|
|
103
|
+
return "focus";
|
|
104
|
+
return isObject(v) && Number.isFinite(v.x) && Number.isFinite(v.y) ? { x: v.x, y: v.y } : void 0;
|
|
105
|
+
};
|
|
106
|
+
const readStringMap = (v, maxEntries, maxLength) => {
|
|
107
|
+
if (!isObject(v))
|
|
108
|
+
return void 0;
|
|
109
|
+
const entries = Object.entries(v);
|
|
110
|
+
if (entries.length > maxEntries || !entries.every(([key, value]) => key.length <= 128 && typeof value === "string" && value.length <= maxLength))
|
|
111
|
+
return void 0;
|
|
112
|
+
return Object.fromEntries(entries);
|
|
113
|
+
};
|
|
114
|
+
const transferCode = (v) => TRANSFER_ERROR_CODES.includes(v) ? v : "unsupported";
|
|
115
|
+
function readTransferOffer(value) {
|
|
116
|
+
if (!isObject(value) || !isTransferId(value.offerId) || !["copy", "cut", "drag"].includes(value.purpose))
|
|
117
|
+
return void 0;
|
|
118
|
+
if (!Number.isSafeInteger(value.generation) || value.generation < 0)
|
|
119
|
+
return void 0;
|
|
120
|
+
if (!Array.isArray(value.items) || !value.items.length || value.items.length > MAX_CONTENT_REFS)
|
|
121
|
+
return void 0;
|
|
122
|
+
let inlineSize = 0;
|
|
123
|
+
const items = [];
|
|
124
|
+
for (const raw of value.items) {
|
|
125
|
+
if (!isObject(raw) || !ITEM_KINDS.includes(raw.kind))
|
|
126
|
+
return void 0;
|
|
127
|
+
const formats = readFormats(raw.formats), operations = readOperations(raw.operations);
|
|
128
|
+
if (!formats || !operations)
|
|
129
|
+
return void 0;
|
|
130
|
+
const item = { kind: raw.kind, formats, operations };
|
|
131
|
+
if (raw.inline !== void 0) {
|
|
132
|
+
const inline = readStringMap(raw.inline, MAX_FORMATS, MAX_INLINE);
|
|
133
|
+
if (!inline || !Object.keys(inline).every((f) => formats.includes(f)))
|
|
134
|
+
return void 0;
|
|
135
|
+
inlineSize += Object.values(inline).reduce((n, s) => n + s.length, 0);
|
|
136
|
+
if (inlineSize > MAX_INLINE)
|
|
137
|
+
return void 0;
|
|
138
|
+
item.inline = inline;
|
|
139
|
+
}
|
|
140
|
+
items.push(item);
|
|
141
|
+
}
|
|
142
|
+
const offer = { offerId: value.offerId, purpose: value.purpose, generation: value.generation, items };
|
|
143
|
+
if (value.scope !== void 0) {
|
|
144
|
+
const scope = readStringMap(value.scope, 8, 512);
|
|
145
|
+
if (!scope)
|
|
146
|
+
return void 0;
|
|
147
|
+
offer.scope = scope;
|
|
148
|
+
}
|
|
149
|
+
return offer;
|
|
150
|
+
}
|
|
151
|
+
function readProbeItems(value) {
|
|
152
|
+
if (!Array.isArray(value) || !value.length || value.length > MAX_CONTENT_REFS)
|
|
153
|
+
return void 0;
|
|
154
|
+
const items = [];
|
|
155
|
+
for (const raw of value) {
|
|
156
|
+
if (!isObject(raw) || !isItemKey(raw.itemKey) || !ITEM_KINDS.includes(raw.kind))
|
|
157
|
+
return void 0;
|
|
158
|
+
const formats = readFormats(raw.formats), operations = readOperations(raw.operations);
|
|
159
|
+
if (!formats || !operations)
|
|
160
|
+
return void 0;
|
|
161
|
+
items.push({ itemKey: raw.itemKey, kind: raw.kind, formats, operations });
|
|
162
|
+
}
|
|
163
|
+
return items;
|
|
164
|
+
}
|
|
165
|
+
function readTransferProbe(value) {
|
|
166
|
+
if (!isObject(value) || !isTransferId(value.operationId) || !Number.isSafeInteger(value.seq))
|
|
167
|
+
return void 0;
|
|
168
|
+
const items = readProbeItems(value.items), at = readPoint(value.at);
|
|
169
|
+
return items && at ? { operationId: value.operationId, seq: value.seq, items, at } : void 0;
|
|
170
|
+
}
|
|
171
|
+
function readTransferProbeAnswer(value, probe) {
|
|
172
|
+
if (!isObject(value) || typeof value.targetId !== "string" || !value.targetId || value.targetId.length > 256 || !Array.isArray(value.accept))
|
|
173
|
+
return null;
|
|
174
|
+
const shown = new Map(probe.items.map((item) => [item.itemKey, item]));
|
|
175
|
+
const seen = /* @__PURE__ */ new Set();
|
|
176
|
+
const accept = [];
|
|
177
|
+
for (const raw of value.accept) {
|
|
178
|
+
if (!isObject(raw) || typeof raw.itemKey !== "string" || seen.has(raw.itemKey))
|
|
179
|
+
continue;
|
|
180
|
+
const item = shown.get(raw.itemKey);
|
|
181
|
+
if (!item || !item.formats.includes(raw.format) || !item.operations.includes(raw.operation))
|
|
182
|
+
continue;
|
|
183
|
+
seen.add(raw.itemKey);
|
|
184
|
+
accept.push({ itemKey: raw.itemKey, format: raw.format, operation: raw.operation });
|
|
185
|
+
}
|
|
186
|
+
if (!accept.length)
|
|
187
|
+
return null;
|
|
188
|
+
return { targetId: value.targetId, accept, action: typeof value.action === "string" ? value.action.slice(0, 200) : "" };
|
|
189
|
+
}
|
|
190
|
+
function readTransferReadRequest(value) {
|
|
191
|
+
if (!isObject(value) || !isTransferId(value.operationId) || !isTransferId(value.offerId) || !Number.isSafeInteger(value.generation))
|
|
192
|
+
return void 0;
|
|
193
|
+
if (!Array.isArray(value.items) || !value.items.length || value.items.length > MAX_CONTENT_REFS)
|
|
194
|
+
return void 0;
|
|
195
|
+
const items = [];
|
|
196
|
+
for (const raw of value.items) {
|
|
197
|
+
if (!isObject(raw) || !isItemKey(raw.itemKey) || !isFormat(raw.format))
|
|
198
|
+
return void 0;
|
|
199
|
+
items.push({ itemKey: raw.itemKey, format: raw.format });
|
|
200
|
+
}
|
|
201
|
+
return { operationId: value.operationId, offerId: value.offerId, generation: value.generation, items };
|
|
202
|
+
}
|
|
203
|
+
const isFileValue = (v) => typeof File !== "undefined" && v instanceof File;
|
|
204
|
+
const isBlobValue = (v) => typeof Blob !== "undefined" && v instanceof Blob;
|
|
205
|
+
function readTransferValue(format, value) {
|
|
206
|
+
if (format === CONTENT_REFS_FORMAT) {
|
|
207
|
+
if (typeof value === "string") {
|
|
208
|
+
const items = readContentRefs(value);
|
|
209
|
+
return (items == null ? void 0 : items.length) === 1 ? items[0] : void 0;
|
|
210
|
+
}
|
|
211
|
+
return readZoneContent(value);
|
|
212
|
+
}
|
|
213
|
+
if (format === "files")
|
|
214
|
+
return isFileValue(value) ? value : void 0;
|
|
215
|
+
if (typeof value === "string")
|
|
216
|
+
return value.length <= MAX_STRING_VALUE ? value : void 0;
|
|
217
|
+
return isBlobValue(value) && !format.startsWith("text/") ? value : void 0;
|
|
218
|
+
}
|
|
219
|
+
function readDeliveryItems(value) {
|
|
220
|
+
if (!Array.isArray(value) || !value.length || value.length > MAX_CONTENT_REFS)
|
|
221
|
+
return void 0;
|
|
222
|
+
const items = [];
|
|
223
|
+
for (const raw of value) {
|
|
224
|
+
if (!isObject(raw) || !isItemKey(raw.itemKey) || !isFormat(raw.format))
|
|
225
|
+
return void 0;
|
|
226
|
+
const clean = readTransferValue(raw.format, raw.value);
|
|
227
|
+
if (clean === void 0)
|
|
228
|
+
return void 0;
|
|
229
|
+
items.push({ itemKey: raw.itemKey, format: raw.format, value: clean });
|
|
230
|
+
}
|
|
231
|
+
return items;
|
|
232
|
+
}
|
|
233
|
+
function readTransferDelivery(value) {
|
|
234
|
+
if (!isObject(value) || !isTransferId(value.operationId) || typeof value.targetId !== "string" || !value.targetId || value.targetId.length > 256)
|
|
235
|
+
return void 0;
|
|
236
|
+
if (!OPERATIONS.includes(value.operation))
|
|
237
|
+
return void 0;
|
|
238
|
+
const at = readPoint(value.at), items = readDeliveryItems(value.items);
|
|
239
|
+
return at && items ? { operationId: value.operationId, targetId: value.targetId, operation: value.operation, at, items } : void 0;
|
|
240
|
+
}
|
|
241
|
+
function readTransferDeliveryResult(value, delivered) {
|
|
242
|
+
if (!isObject(value) || !["done", "accepted", "cancelled", "failed"].includes(value.status) || !Array.isArray(value.items))
|
|
243
|
+
return void 0;
|
|
244
|
+
const given = /* @__PURE__ */ new Map();
|
|
245
|
+
for (const raw of value.items) {
|
|
246
|
+
if (!isObject(raw) || typeof raw.itemKey !== "string" || !ITEM_STATUSES.includes(raw.status))
|
|
247
|
+
continue;
|
|
248
|
+
given.set(raw.itemKey, { status: raw.status, ...typeof raw.error === "string" ? { error: raw.error.slice(0, 200) } : {} });
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
status: value.status,
|
|
252
|
+
items: delivered.map(({ itemKey }) => ({ itemKey, ...given.get(itemKey) ?? { status: "failed", error: "no_result" } }))
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function readTransferReadValues(value, request, options) {
|
|
256
|
+
const given = /* @__PURE__ */ new Map();
|
|
257
|
+
if (Array.isArray(value))
|
|
258
|
+
for (const raw of value) {
|
|
259
|
+
if (isObject(raw) && typeof raw.itemKey === "string" && typeof raw.format === "string")
|
|
260
|
+
given.set(`${raw.itemKey}
|
|
261
|
+
${raw.format}`, raw);
|
|
262
|
+
}
|
|
263
|
+
return request.items.map(({ itemKey, format }) => {
|
|
264
|
+
const raw = given.get(`${itemKey}
|
|
265
|
+
${format}`);
|
|
266
|
+
if (!raw)
|
|
267
|
+
return { itemKey, format, error: "failed" };
|
|
268
|
+
if ("error" in raw && raw.error !== void 0)
|
|
269
|
+
return { itemKey, format, error: raw.error === "failed" ? "failed" : transferCode(raw.error) };
|
|
270
|
+
if (format === "files" && !options.files)
|
|
271
|
+
return { itemKey, format, error: "unsupported" };
|
|
272
|
+
const clean = readTransferValue(format, raw.value);
|
|
273
|
+
return clean === void 0 ? { itemKey, format, error: "failed" } : { itemKey, format, value: clean };
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
const portLink = (documentId, port, receive) => {
|
|
277
|
+
var _a;
|
|
278
|
+
port.onmessage = (event) => receive(event.data);
|
|
279
|
+
(_a = port.start) == null ? void 0 : _a.call(port);
|
|
280
|
+
return { documentId, post: (message) => port.postMessage(message), close: () => {
|
|
281
|
+
port.onmessage = null;
|
|
282
|
+
port.close();
|
|
283
|
+
} };
|
|
284
|
+
};
|
|
71
285
|
const FRAME_NAME_PREFIX = "buckyos.app-frame:";
|
|
72
286
|
function appFrameName(nonce) {
|
|
73
287
|
return FRAME_NAME_PREFIX + nonce;
|
|
@@ -81,11 +295,17 @@ class AppFrameHost {
|
|
|
81
295
|
this.pending = /* @__PURE__ */ new Map();
|
|
82
296
|
this.opens = /* @__PURE__ */ new Map();
|
|
83
297
|
this.dialogOpen = false;
|
|
298
|
+
this.transferPending = /* @__PURE__ */ new Map();
|
|
84
299
|
this.callbacks = options;
|
|
85
300
|
this.framed = !options.transport;
|
|
86
301
|
this.init = options.transport ? { ...options.init, nonce: "" } : options.init;
|
|
87
302
|
this.channel = options.transport ?? windowTransport(options.target, options.origin);
|
|
88
|
-
this.detach = this.channel.onMessage((message) => this.receive(message));
|
|
303
|
+
this.detach = this.channel.onMessage((message, ports) => this.receive(message, ports));
|
|
304
|
+
}
|
|
305
|
+
/** The App document whose transfer link is bound (`transfer.v1`); undefined when none is. */
|
|
306
|
+
get transferDocument() {
|
|
307
|
+
var _a;
|
|
308
|
+
return (_a = this.transferLink) == null ? void 0 : _a.documentId;
|
|
89
309
|
}
|
|
90
310
|
limited(type) {
|
|
91
311
|
const recent = (this.opens.get(type) ?? []).filter((t) => t > Date.now() - 1e4);
|
|
@@ -129,7 +349,7 @@ class AppFrameHost {
|
|
|
129
349
|
this.dialogOpen = false;
|
|
130
350
|
}
|
|
131
351
|
}
|
|
132
|
-
receive(m) {
|
|
352
|
+
receive(m, ports) {
|
|
133
353
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
134
354
|
if (!isMessage(m))
|
|
135
355
|
return;
|
|
@@ -143,10 +363,14 @@ class AppFrameHost {
|
|
|
143
363
|
} else {
|
|
144
364
|
if (typeof p.nonce !== "string" || !p.nonce || p.nonce.length > 128)
|
|
145
365
|
return;
|
|
146
|
-
if (p.nonce !== this.init.nonce)
|
|
366
|
+
if (p.nonce !== this.init.nonce) {
|
|
147
367
|
this.failPending("App frame reloaded");
|
|
368
|
+
this.dropTransfer();
|
|
369
|
+
}
|
|
148
370
|
this.init = { ...this.init, nonce: p.nonce };
|
|
149
371
|
}
|
|
372
|
+
if (this.transferLink && this.transferLink.documentId !== p.documentId)
|
|
373
|
+
this.dropTransfer();
|
|
150
374
|
this.ready = true;
|
|
151
375
|
this.capabilities = p.appCapabilities.filter((c) => typeof c === "string");
|
|
152
376
|
this.send("frame.init", this.init);
|
|
@@ -161,6 +385,15 @@ class AppFrameHost {
|
|
|
161
385
|
}
|
|
162
386
|
if (!this.ready)
|
|
163
387
|
return;
|
|
388
|
+
if (m.type === "transfer.connect") {
|
|
389
|
+
this.connectTransfer(p, ports);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (m.type.startsWith("transfer.")) {
|
|
393
|
+
if (!this.framed && this.transferLink)
|
|
394
|
+
this.receiveTransfer(m, this.transferLink);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
164
397
|
if (m.replyTo) {
|
|
165
398
|
const pending = this.pending.get(m.replyTo);
|
|
166
399
|
if (pending && m.type === pending.type + "Result") {
|
|
@@ -243,12 +476,132 @@ class AppFrameHost {
|
|
|
243
476
|
});
|
|
244
477
|
this.pending.clear();
|
|
245
478
|
}
|
|
479
|
+
/** Binds the transfer link of the document that just shook hands: its port (framed) or the bridge. */
|
|
480
|
+
connectTransfer(p, ports) {
|
|
481
|
+
var _a, _b;
|
|
482
|
+
const port = (ports == null ? void 0 : ports.length) === 1 ? ports[0] : void 0;
|
|
483
|
+
const refuse = () => ports == null ? void 0 : ports.forEach((x) => x.close());
|
|
484
|
+
if (!this.init.shellCapabilities.includes(TRANSFER_CAPABILITY) || !this.capabilities.includes(TRANSFER_CAPABILITY))
|
|
485
|
+
return refuse();
|
|
486
|
+
if (typeof p.documentId !== "string" || !DOCUMENT_ID.test(p.documentId) || (this.framed ? !port : !!(ports == null ? void 0 : ports.length)))
|
|
487
|
+
return refuse();
|
|
488
|
+
this.dropTransfer();
|
|
489
|
+
const documentId = p.documentId;
|
|
490
|
+
let link;
|
|
491
|
+
if (port)
|
|
492
|
+
link = portLink(documentId, port, (message) => {
|
|
493
|
+
if (this.transferLink === link)
|
|
494
|
+
this.receiveTransfer(message, link);
|
|
495
|
+
});
|
|
496
|
+
else
|
|
497
|
+
link = { documentId, post: (message) => this.channel.post(message), close: () => {
|
|
498
|
+
} };
|
|
499
|
+
this.transferLink = link;
|
|
500
|
+
(_b = (_a = this.callbacks).onTransferConnect) == null ? void 0 : _b.call(_a, documentId);
|
|
501
|
+
}
|
|
502
|
+
/** Drops the transfer link: every request waiting on it fails `stale`, its offers are void. */
|
|
503
|
+
dropTransfer() {
|
|
504
|
+
var _a, _b;
|
|
505
|
+
const link = this.transferLink;
|
|
506
|
+
if (!link)
|
|
507
|
+
return;
|
|
508
|
+
this.transferLink = void 0;
|
|
509
|
+
link.close();
|
|
510
|
+
this.transferPending.forEach((p) => {
|
|
511
|
+
clearTimeout(p.timer);
|
|
512
|
+
p.reject(new TransferError("stale", "The App document changed"));
|
|
513
|
+
});
|
|
514
|
+
this.transferPending.clear();
|
|
515
|
+
(_b = (_a = this.callbacks).onTransferDisconnect) == null ? void 0 : _b.call(_a, link.documentId);
|
|
516
|
+
}
|
|
517
|
+
receiveTransfer(m, link) {
|
|
518
|
+
var _a, _b;
|
|
519
|
+
if (!isMessage(m))
|
|
520
|
+
return;
|
|
521
|
+
const p = m.payload;
|
|
522
|
+
if (m.replyTo) {
|
|
523
|
+
const pending = this.transferPending.get(m.replyTo);
|
|
524
|
+
if (pending && m.type === pending.type + "Result") {
|
|
525
|
+
clearTimeout(pending.timer);
|
|
526
|
+
this.transferPending.delete(m.replyTo);
|
|
527
|
+
pending.resolve(m.payload);
|
|
528
|
+
}
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (m.type === "transfer.offer" && typeof m.id === "string") {
|
|
532
|
+
const id = m.id;
|
|
533
|
+
const reply = (payload) => {
|
|
534
|
+
if (this.transferLink === link)
|
|
535
|
+
link.post(envelope("transfer.offerResult", payload, { replyTo: id }));
|
|
536
|
+
};
|
|
537
|
+
const offer = readTransferOffer(p);
|
|
538
|
+
if (!offer)
|
|
539
|
+
return reply({ ok: false, code: "unsupported" });
|
|
540
|
+
if (!this.callbacks.onTransferOffer)
|
|
541
|
+
return reply({ ok: false, code: "unsupported" });
|
|
542
|
+
void Promise.resolve().then(() => this.callbacks.onTransferOffer(offer, link.documentId)).then((result) => reply(result.ok ? { ok: true } : { ok: false, code: transferCode(result.code) }), () => reply({ ok: false, code: "denied" }));
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
if (m.type === "transfer.cancel" && isTransferId(p.offerId))
|
|
546
|
+
(_b = (_a = this.callbacks).onTransferWithdraw) == null ? void 0 : _b.call(_a, p.offerId, link.documentId);
|
|
547
|
+
}
|
|
548
|
+
transferRequest(type, payload, timeout) {
|
|
549
|
+
const link = this.transferLink;
|
|
550
|
+
if (!this.ready || !link)
|
|
551
|
+
return Promise.reject(new TransferError("stale", "No transfer link"));
|
|
552
|
+
const id = crypto.randomUUID();
|
|
553
|
+
return new Promise((resolve, reject) => {
|
|
554
|
+
const timer = setTimeout(() => {
|
|
555
|
+
this.transferPending.delete(id);
|
|
556
|
+
reject(new TransferError("timeout"));
|
|
557
|
+
}, timeout);
|
|
558
|
+
this.transferPending.set(id, { resolve, reject, timer, type });
|
|
559
|
+
link.post(envelope(type, payload, { id }));
|
|
560
|
+
}).then((reply) => {
|
|
561
|
+
const r = isObject(reply) ? reply : {};
|
|
562
|
+
if (r.ok !== true)
|
|
563
|
+
throw new TransferError(transferCode(r.code));
|
|
564
|
+
return r;
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
/** Asks the App what it would do with these items at a point of its page or at its focus; null when nothing. */
|
|
568
|
+
async transferProbe(probe, timeout = TRANSFER_TIMEOUT.probe) {
|
|
569
|
+
const r = await this.transferRequest("transfer.probe", probe, timeout);
|
|
570
|
+
return r.answer === null ? null : readTransferProbeAnswer(r.answer, probe);
|
|
571
|
+
}
|
|
572
|
+
/** Reads representations of a selection the App offered. Each value is checked by its format. */
|
|
573
|
+
async transferRead(read, timeout = TRANSFER_TIMEOUT.read) {
|
|
574
|
+
const r = await this.transferRequest("transfer.read", read, timeout);
|
|
575
|
+
return readTransferReadValues(r.items, read, { files: this.framed });
|
|
576
|
+
}
|
|
577
|
+
/** Hands the chosen representations to the App; a retry with the same `operationId` must not commit twice. */
|
|
578
|
+
async transferDeliver(delivery, timeout = TRANSFER_TIMEOUT.deliver) {
|
|
579
|
+
if (!this.framed && delivery.items.some((item) => item.format === "files"))
|
|
580
|
+
throw new TransferError("unsupported", "Local files cannot cross the bridge");
|
|
581
|
+
const r = await this.transferRequest("transfer.deliver", delivery, timeout);
|
|
582
|
+
const result = readTransferDeliveryResult(r.result, delivery.items);
|
|
583
|
+
if (!result)
|
|
584
|
+
throw new TransferError("unsupported", "Invalid delivery result");
|
|
585
|
+
return result;
|
|
586
|
+
}
|
|
587
|
+
/** The final result of a delivery, by its `operationId`; null when the App does not know it. */
|
|
588
|
+
async transferStatus(operationId, delivered, timeout = TRANSFER_TIMEOUT.status) {
|
|
589
|
+
const r = await this.transferRequest("transfer.status", { operationId }, timeout);
|
|
590
|
+
return r.result === null ? null : readTransferDeliveryResult(r.result, delivered) ?? null;
|
|
591
|
+
}
|
|
592
|
+
/** Tells the App an operation it took part in was dropped before it committed. */
|
|
593
|
+
transferCancel(operationId) {
|
|
594
|
+
var _a;
|
|
595
|
+
(_a = this.transferLink) == null ? void 0 : _a.post(envelope("transfer.cancel", { operationId }));
|
|
596
|
+
}
|
|
246
597
|
dispose() {
|
|
247
598
|
this.detach();
|
|
248
599
|
this.failPending("App frame disposed");
|
|
600
|
+
this.dropTransfer();
|
|
249
601
|
this.ready = false;
|
|
250
602
|
}
|
|
251
603
|
}
|
|
604
|
+
const DELIVERY_MEMORY = 64;
|
|
252
605
|
function embedderOrigin(accept) {
|
|
253
606
|
var _a;
|
|
254
607
|
if (window.parent === window)
|
|
@@ -280,6 +633,10 @@ class AppFrameClient {
|
|
|
280
633
|
this.header = {};
|
|
281
634
|
this.dialogs = /* @__PURE__ */ new Map();
|
|
282
635
|
this.appOpens = /* @__PURE__ */ new Map();
|
|
636
|
+
this.documentId = newOfferId();
|
|
637
|
+
this.transferPending = /* @__PURE__ */ new Map();
|
|
638
|
+
this.deliveries = /* @__PURE__ */ new Map();
|
|
639
|
+
this.generation = 0;
|
|
283
640
|
const origin = new URL(options.shellOrigin).origin;
|
|
284
641
|
const bridge = window.parent === window ? getBuckyApi() : void 0;
|
|
285
642
|
this.native = !!bridge;
|
|
@@ -293,8 +650,9 @@ class AppFrameClient {
|
|
|
293
650
|
const hello = () => this.channel.post(envelope("frame.hello", { nonce: this.nonce, appCapabilities: [
|
|
294
651
|
...options.onOpen ? ["open"] : [],
|
|
295
652
|
...options.onBeforeClose ? ["beforeClose"] : [],
|
|
296
|
-
...options.onBack ? ["back"] : []
|
|
297
|
-
|
|
653
|
+
...options.onBack ? ["back"] : [],
|
|
654
|
+
...options.transfer ? [TRANSFER_CAPABILITY] : []
|
|
655
|
+
], ...options.transfer ? { documentId: this.documentId } : {} }));
|
|
298
656
|
hello();
|
|
299
657
|
this.helloTimer = setInterval(hello, 250);
|
|
300
658
|
this.helloDeadline = setTimeout(() => clearInterval(this.helloTimer), bridge ? 1e4 : 1500);
|
|
@@ -316,6 +674,12 @@ class AppFrameClient {
|
|
|
316
674
|
if (Object.keys(this.header).length)
|
|
317
675
|
this.send("header.set", this.header);
|
|
318
676
|
(_b = (_a = this.options).onInit) == null ? void 0 : _b.call(_a, this.init);
|
|
677
|
+
this.connectTransfer();
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (m.type.startsWith("transfer.")) {
|
|
681
|
+
if (this.native && this.transferLink)
|
|
682
|
+
void this.receiveTransfer(m, this.transferLink);
|
|
319
683
|
return;
|
|
320
684
|
}
|
|
321
685
|
if (!this.init)
|
|
@@ -462,15 +826,146 @@ class AppFrameClient {
|
|
|
462
826
|
abort.abort();
|
|
463
827
|
return openSelectEmbed(this.options.systemOrigin, params, { ...options, signal: abort.signal });
|
|
464
828
|
}
|
|
465
|
-
|
|
829
|
+
/** Whether the Shell takes offers from this document (`transfer.v1` on both sides, link connected). */
|
|
830
|
+
get transferAvailable() {
|
|
831
|
+
return !!this.transferLink;
|
|
832
|
+
}
|
|
833
|
+
/** Once per document, after the first `frame.init` that lists `transfer.v1`: one port, to the exact Shell origin. */
|
|
834
|
+
connectTransfer() {
|
|
835
|
+
var _a, _b, _c;
|
|
836
|
+
if (!this.options.transfer || this.transferLink || !((_a = this.init) == null ? void 0 : _a.shellCapabilities.includes(TRANSFER_CAPABILITY)))
|
|
837
|
+
return;
|
|
838
|
+
let link;
|
|
839
|
+
if (this.native) {
|
|
840
|
+
link = { documentId: this.documentId, post: (message) => this.channel.post(message), close: () => {
|
|
841
|
+
} };
|
|
842
|
+
this.channel.post(envelope("transfer.connect", { documentId: this.documentId }));
|
|
843
|
+
} else {
|
|
844
|
+
const channel = new MessageChannel();
|
|
845
|
+
link = portLink(this.documentId, channel.port1, (message) => {
|
|
846
|
+
void this.receiveTransfer(message, link);
|
|
847
|
+
});
|
|
848
|
+
this.channel.post(envelope("transfer.connect", { documentId: this.documentId }), [channel.port2]);
|
|
849
|
+
}
|
|
850
|
+
this.transferLink = link;
|
|
851
|
+
try {
|
|
852
|
+
(_c = (_b = this.options.transfer).onAvailable) == null ? void 0 : _c.call(_b, true);
|
|
853
|
+
} catch {
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
async receiveTransfer(m, link) {
|
|
466
857
|
var _a;
|
|
858
|
+
if (!isMessage(m) || this.transferLink !== link)
|
|
859
|
+
return;
|
|
860
|
+
const p = m.payload;
|
|
861
|
+
if (m.replyTo) {
|
|
862
|
+
const pending = this.transferPending.get(m.replyTo);
|
|
863
|
+
if (pending && m.type === "transfer.offerResult") {
|
|
864
|
+
clearTimeout(pending.timer);
|
|
865
|
+
this.transferPending.delete(m.replyTo);
|
|
866
|
+
pending.resolve(m.payload);
|
|
867
|
+
}
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
const handlers = this.options.transfer ?? {};
|
|
871
|
+
if (m.type === "transfer.cancel") {
|
|
872
|
+
if (isTransferId(p.operationId))
|
|
873
|
+
(_a = handlers.onCancel) == null ? void 0 : _a.call(handlers, p.operationId);
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
if (typeof m.id !== "string")
|
|
877
|
+
return;
|
|
878
|
+
const reply = (payload) => {
|
|
879
|
+
if (this.transferLink === link)
|
|
880
|
+
link.post(envelope(m.type + "Result", payload, { replyTo: m.id }));
|
|
881
|
+
};
|
|
882
|
+
const fail = (error) => reply({ ok: false, code: error instanceof TransferError && error.code !== "timeout" ? error.code : "denied" });
|
|
883
|
+
try {
|
|
884
|
+
if (m.type === "transfer.probe") {
|
|
885
|
+
const probe = readTransferProbe(p);
|
|
886
|
+
if (!probe || !handlers.onProbe)
|
|
887
|
+
return reply({ ok: false, code: "unsupported" });
|
|
888
|
+
const answer = await handlers.onProbe(probe);
|
|
889
|
+
return reply({ ok: true, answer: answer ? readTransferProbeAnswer(answer, probe) : null });
|
|
890
|
+
}
|
|
891
|
+
if (m.type === "transfer.read") {
|
|
892
|
+
const read = readTransferReadRequest(p);
|
|
893
|
+
if (!read || !handlers.onRead)
|
|
894
|
+
return reply({ ok: false, code: "unsupported" });
|
|
895
|
+
return reply({ ok: true, items: readTransferReadValues(await handlers.onRead(read), read, { files: !this.native }) });
|
|
896
|
+
}
|
|
897
|
+
if (m.type === "transfer.deliver") {
|
|
898
|
+
const delivery = readTransferDelivery(p);
|
|
899
|
+
if (!delivery || !handlers.onDeliver)
|
|
900
|
+
return reply({ ok: false, code: "unsupported" });
|
|
901
|
+
let result = this.deliveries.get(delivery.operationId);
|
|
902
|
+
if (!result) {
|
|
903
|
+
const onDeliver = handlers.onDeliver;
|
|
904
|
+
result = Promise.resolve().then(() => onDeliver(delivery));
|
|
905
|
+
this.deliveries.set(delivery.operationId, result);
|
|
906
|
+
result.catch(() => this.deliveries.delete(delivery.operationId));
|
|
907
|
+
for (const key of [...this.deliveries.keys()].slice(0, Math.max(0, this.deliveries.size - DELIVERY_MEMORY)))
|
|
908
|
+
this.deliveries.delete(key);
|
|
909
|
+
}
|
|
910
|
+
return reply({ ok: true, result: readTransferDeliveryResult(await result, delivery.items) });
|
|
911
|
+
}
|
|
912
|
+
if (m.type === "transfer.status") {
|
|
913
|
+
if (!isTransferId(p.operationId))
|
|
914
|
+
return reply({ ok: false, code: "unsupported" });
|
|
915
|
+
const result = this.deliveries.get(p.operationId);
|
|
916
|
+
return reply({ ok: true, result: result ? await result : null });
|
|
917
|
+
}
|
|
918
|
+
reply({ ok: false, code: "unsupported" });
|
|
919
|
+
} catch (error) {
|
|
920
|
+
fail(error);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Registers a selection with the Shell so another App can receive it (the Shell's drag handle, "Paste desktop
|
|
925
|
+
* object…"). The offer id is generated here; withdraw it when the selection goes away. Never rejects.
|
|
926
|
+
*/
|
|
927
|
+
offerTransfer(offer) {
|
|
928
|
+
const link = this.transferLink;
|
|
929
|
+
if (!link)
|
|
930
|
+
return Promise.resolve({ ok: false, code: "unsupported" });
|
|
931
|
+
const message = readTransferOffer({ ...offer, offerId: newOfferId(), generation: offer.generation ?? ++this.generation });
|
|
932
|
+
if (!message)
|
|
933
|
+
return Promise.resolve({ ok: false, code: "unsupported" });
|
|
934
|
+
const id = crypto.randomUUID();
|
|
935
|
+
return new Promise((resolve) => {
|
|
936
|
+
const timer = setTimeout(() => {
|
|
937
|
+
this.transferPending.delete(id);
|
|
938
|
+
resolve({ ok: false, code: "unsupported" });
|
|
939
|
+
}, TRANSFER_TIMEOUT.offer);
|
|
940
|
+
this.transferPending.set(id, { timer, resolve: (reply) => {
|
|
941
|
+
const r = isObject(reply) ? reply : {};
|
|
942
|
+
resolve(r.ok === true ? { ok: true, offerId: message.offerId } : { ok: false, code: transferCode(r.code) });
|
|
943
|
+
} });
|
|
944
|
+
link.post(envelope("transfer.offer", message, { id }));
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
/** Withdraws a selection offered with `offerTransfer`. */
|
|
948
|
+
withdrawTransfer(offerId) {
|
|
949
|
+
var _a;
|
|
950
|
+
if (isTransferId(offerId))
|
|
951
|
+
(_a = this.transferLink) == null ? void 0 : _a.post(envelope("transfer.cancel", { offerId }));
|
|
952
|
+
}
|
|
953
|
+
dispose() {
|
|
954
|
+
var _a, _b;
|
|
467
955
|
clearInterval(this.helloTimer);
|
|
468
956
|
clearTimeout(this.helloDeadline);
|
|
469
957
|
this.detach();
|
|
958
|
+
(_a = this.transferLink) == null ? void 0 : _a.close();
|
|
959
|
+
this.transferLink = void 0;
|
|
960
|
+
this.transferPending.forEach((p) => {
|
|
961
|
+
clearTimeout(p.timer);
|
|
962
|
+
p.resolve({ ok: false, code: "stale" });
|
|
963
|
+
});
|
|
964
|
+
this.transferPending.clear();
|
|
470
965
|
this.appOpens.forEach((settle) => settle({ ok: false, code: "unsupported" }));
|
|
471
966
|
this.dialogs.forEach((pending) => pending.reject(new SystemDialogError("closed")));
|
|
472
967
|
this.dialogs.clear();
|
|
473
|
-
(
|
|
968
|
+
(_b = this.embedAbort) == null ? void 0 : _b.abort();
|
|
474
969
|
}
|
|
475
970
|
}
|
|
476
971
|
const PREVIEW_EMBED = "buckyos.preview-embed";
|
|
@@ -974,9 +1469,19 @@ export {
|
|
|
974
1469
|
PreviewEmbedHost,
|
|
975
1470
|
SELECT_EMBED_PATH,
|
|
976
1471
|
SelectEmbedHost,
|
|
1472
|
+
TRANSFER_CAPABILITY,
|
|
1473
|
+
TRANSFER_ERROR_CODES,
|
|
1474
|
+
TransferError,
|
|
977
1475
|
appFrameName,
|
|
978
1476
|
embedderOrigin,
|
|
979
1477
|
openSelectEmbed,
|
|
980
|
-
originInZone
|
|
1478
|
+
originInZone,
|
|
1479
|
+
readTransferDelivery,
|
|
1480
|
+
readTransferDeliveryResult,
|
|
1481
|
+
readTransferOffer,
|
|
1482
|
+
readTransferProbe,
|
|
1483
|
+
readTransferProbeAnswer,
|
|
1484
|
+
readTransferReadRequest,
|
|
1485
|
+
readTransferValue
|
|
981
1486
|
};
|
|
982
1487
|
//# sourceMappingURL=app-frame.mjs.map
|