@dropgate/core 3.0.4 → 3.0.6

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.cjs CHANGED
@@ -2405,6 +2405,15 @@ var DropgateClient = class {
2405
2405
  }));
2406
2406
  } else if (serverMeta.files) {
2407
2407
  files = serverMeta.files;
2408
+ if (serverMeta.isEncrypted) {
2409
+ const encryptedChunkSize = this.chunkSize + ENCRYPTION_OVERHEAD_PER_CHUNK;
2410
+ for (const f of files) {
2411
+ if (f.sizeBytes > 0) {
2412
+ const numChunks = Math.ceil(f.sizeBytes / encryptedChunkSize);
2413
+ f.sizeBytes = f.sizeBytes - numChunks * ENCRYPTION_OVERHEAD_PER_CHUNK;
2414
+ }
2415
+ }
2416
+ }
2408
2417
  } else {
2409
2418
  throw new DropgateProtocolError("Invalid bundle metadata: missing files or manifest.");
2410
2419
  }
@@ -3086,7 +3095,14 @@ var DropgateClient = class {
3086
3095
  throw new DropgateNetworkError("Could not fetch file metadata.", { cause: err2 });
3087
3096
  }
3088
3097
  const isEncrypted = Boolean(metadata.isEncrypted);
3089
- const totalBytes = metadata.sizeBytes || 0;
3098
+ const encryptedTotalBytes = metadata.sizeBytes || 0;
3099
+ let totalBytes = encryptedTotalBytes;
3100
+ if (isEncrypted && encryptedTotalBytes > 0) {
3101
+ const downloadChunkSize = Number.isFinite(compat.serverInfo?.capabilities?.upload?.chunkSize) && compat.serverInfo.capabilities.upload.chunkSize > 0 ? compat.serverInfo.capabilities.upload.chunkSize : this.chunkSize;
3102
+ const encryptedChunkSize = downloadChunkSize + ENCRYPTION_OVERHEAD_PER_CHUNK;
3103
+ const numChunks = Math.ceil(encryptedTotalBytes / encryptedChunkSize);
3104
+ totalBytes = encryptedTotalBytes - numChunks * ENCRYPTION_OVERHEAD_PER_CHUNK;
3105
+ }
3090
3106
  if (!onData && totalBytes > MAX_IN_MEMORY_DOWNLOAD_BYTES) {
3091
3107
  const sizeMB = Math.round(totalBytes / (1024 * 1024));
3092
3108
  const limitMB = Math.round(MAX_IN_MEMORY_DOWNLOAD_BYTES / (1024 * 1024));
@@ -3199,8 +3215,6 @@ var DropgateClient = class {
3199
3215
  if (done) break;
3200
3216
  pendingChunks.push(value);
3201
3217
  pendingLength += value.length;
3202
- receivedBytes += value.length;
3203
- if (onBytesReceived) onBytesReceived(receivedBytes);
3204
3218
  while (pendingLength >= ENCRYPTED_CHUNK_SIZE) {
3205
3219
  const buffer = flushPending();
3206
3220
  const encryptedChunk = buffer.subarray(0, ENCRYPTED_CHUNK_SIZE);
@@ -3209,12 +3223,16 @@ var DropgateClient = class {
3209
3223
  pendingLength = buffer.length - ENCRYPTED_CHUNK_SIZE;
3210
3224
  }
3211
3225
  const decryptedBuffer = await decryptChunk(this.cryptoObj, encryptedChunk, cryptoKey);
3226
+ receivedBytes += decryptedBuffer.byteLength;
3227
+ if (onBytesReceived) onBytesReceived(receivedBytes);
3212
3228
  if (onChunk) await onChunk(new Uint8Array(decryptedBuffer));
3213
3229
  }
3214
3230
  }
3215
3231
  if (pendingLength > 0) {
3216
3232
  const buffer = flushPending();
3217
3233
  const decryptedBuffer = await decryptChunk(this.cryptoObj, buffer, cryptoKey);
3234
+ receivedBytes += decryptedBuffer.byteLength;
3235
+ if (onBytesReceived) onBytesReceived(receivedBytes);
3218
3236
  if (onChunk) await onChunk(new Uint8Array(decryptedBuffer));
3219
3237
  }
3220
3238
  } else {