@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.js CHANGED
@@ -2342,6 +2342,15 @@ var DropgateClient = class {
2342
2342
  }));
2343
2343
  } else if (serverMeta.files) {
2344
2344
  files = serverMeta.files;
2345
+ if (serverMeta.isEncrypted) {
2346
+ const encryptedChunkSize = this.chunkSize + ENCRYPTION_OVERHEAD_PER_CHUNK;
2347
+ for (const f of files) {
2348
+ if (f.sizeBytes > 0) {
2349
+ const numChunks = Math.ceil(f.sizeBytes / encryptedChunkSize);
2350
+ f.sizeBytes = f.sizeBytes - numChunks * ENCRYPTION_OVERHEAD_PER_CHUNK;
2351
+ }
2352
+ }
2353
+ }
2345
2354
  } else {
2346
2355
  throw new DropgateProtocolError("Invalid bundle metadata: missing files or manifest.");
2347
2356
  }
@@ -3023,7 +3032,14 @@ var DropgateClient = class {
3023
3032
  throw new DropgateNetworkError("Could not fetch file metadata.", { cause: err2 });
3024
3033
  }
3025
3034
  const isEncrypted = Boolean(metadata.isEncrypted);
3026
- const totalBytes = metadata.sizeBytes || 0;
3035
+ const encryptedTotalBytes = metadata.sizeBytes || 0;
3036
+ let totalBytes = encryptedTotalBytes;
3037
+ if (isEncrypted && encryptedTotalBytes > 0) {
3038
+ const downloadChunkSize = Number.isFinite(compat.serverInfo?.capabilities?.upload?.chunkSize) && compat.serverInfo.capabilities.upload.chunkSize > 0 ? compat.serverInfo.capabilities.upload.chunkSize : this.chunkSize;
3039
+ const encryptedChunkSize = downloadChunkSize + ENCRYPTION_OVERHEAD_PER_CHUNK;
3040
+ const numChunks = Math.ceil(encryptedTotalBytes / encryptedChunkSize);
3041
+ totalBytes = encryptedTotalBytes - numChunks * ENCRYPTION_OVERHEAD_PER_CHUNK;
3042
+ }
3027
3043
  if (!onData && totalBytes > MAX_IN_MEMORY_DOWNLOAD_BYTES) {
3028
3044
  const sizeMB = Math.round(totalBytes / (1024 * 1024));
3029
3045
  const limitMB = Math.round(MAX_IN_MEMORY_DOWNLOAD_BYTES / (1024 * 1024));
@@ -3136,8 +3152,6 @@ var DropgateClient = class {
3136
3152
  if (done) break;
3137
3153
  pendingChunks.push(value);
3138
3154
  pendingLength += value.length;
3139
- receivedBytes += value.length;
3140
- if (onBytesReceived) onBytesReceived(receivedBytes);
3141
3155
  while (pendingLength >= ENCRYPTED_CHUNK_SIZE) {
3142
3156
  const buffer = flushPending();
3143
3157
  const encryptedChunk = buffer.subarray(0, ENCRYPTED_CHUNK_SIZE);
@@ -3146,12 +3160,16 @@ var DropgateClient = class {
3146
3160
  pendingLength = buffer.length - ENCRYPTED_CHUNK_SIZE;
3147
3161
  }
3148
3162
  const decryptedBuffer = await decryptChunk(this.cryptoObj, encryptedChunk, cryptoKey);
3163
+ receivedBytes += decryptedBuffer.byteLength;
3164
+ if (onBytesReceived) onBytesReceived(receivedBytes);
3149
3165
  if (onChunk) await onChunk(new Uint8Array(decryptedBuffer));
3150
3166
  }
3151
3167
  }
3152
3168
  if (pendingLength > 0) {
3153
3169
  const buffer = flushPending();
3154
3170
  const decryptedBuffer = await decryptChunk(this.cryptoObj, buffer, cryptoKey);
3171
+ receivedBytes += decryptedBuffer.byteLength;
3172
+ if (onBytesReceived) onBytesReceived(receivedBytes);
3155
3173
  if (onChunk) await onChunk(new Uint8Array(decryptedBuffer));
3156
3174
  }
3157
3175
  } else {