@beanx/cathygo-protocol 0.1.3 → 0.1.4
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/dist/index.js +16 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -330,6 +330,9 @@ async function uploadAttachmentViaRelay(client, file, options = {}) {
|
|
|
330
330
|
data_base64: bytesToBase64(chunk)
|
|
331
331
|
});
|
|
332
332
|
seq += 1;
|
|
333
|
+
if (seq % 2 === 0) {
|
|
334
|
+
await yieldToMainThread();
|
|
335
|
+
}
|
|
333
336
|
options.onProgress?.({
|
|
334
337
|
transfer_id: transferId,
|
|
335
338
|
received_bytes: Number(pushPayload.received_bytes ?? offset + chunk.byteLength),
|
|
@@ -368,12 +371,23 @@ function percent(receivedBytes, totalBytes) {
|
|
|
368
371
|
return Math.min(100, Math.max(0, Math.round(receivedBytes * 100 / totalBytes)));
|
|
369
372
|
}
|
|
370
373
|
function bytesToBase64(bytes) {
|
|
374
|
+
const chunkSize = 32768;
|
|
371
375
|
let binary = "";
|
|
372
|
-
for (let
|
|
373
|
-
|
|
376
|
+
for (let offset = 0; offset < bytes.length; offset += chunkSize) {
|
|
377
|
+
const chunk = bytes.subarray(offset, Math.min(offset + chunkSize, bytes.length));
|
|
378
|
+
binary += String.fromCharCode.apply(null, chunk);
|
|
374
379
|
}
|
|
375
380
|
return btoa(binary);
|
|
376
381
|
}
|
|
382
|
+
async function yieldToMainThread() {
|
|
383
|
+
await new Promise((resolve) => {
|
|
384
|
+
if (typeof requestAnimationFrame === "function") {
|
|
385
|
+
requestAnimationFrame(() => resolve());
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
setTimeout(resolve, 0);
|
|
389
|
+
});
|
|
390
|
+
}
|
|
377
391
|
async function sha256Hex(buffer) {
|
|
378
392
|
const digest = await crypto.subtle.digest("SHA-256", buffer);
|
|
379
393
|
return Array.from(new Uint8Array(digest)).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|