fix(upload): wrap gzip chunk output in Buffer so Telegraf uploads work
Root cause of '400: Bad Request: there is no document in the request' on chunked uploads of compressible files (MP4, zip, text, etc.): Bun.gzipSync() returns a plain Uint8Array, NOT a Buffer. Telegraf (sendDocument/sendVideo) only recognises Buffer/Blob/stream sources as file uploads — a plain Uint8Array produces an empty multipart body and Telegram rejects the request. Verified via isolated forwardToStorage probes: - raw Buffer chunk (.bin, MP4) -> OK - plain Uint8Array chunk -> FAIL: there is no document - Buffer.from(gzip) chunk -> OK (after fix) - .gz extension on Uint8Array did NOT help (rules out MIME/extension) This only affected chunked uploads of compressible files; incompressible files (random .bin) worked by accident because gzip didn't shrink them so the raw Buffer was forwarded unchanged.
This commit is contained in:
@@ -23,10 +23,16 @@ export const maybeCompressChunk = (
|
||||
return { bytes: chunk, compressionAlgorithm: null };
|
||||
}
|
||||
|
||||
const gzipped = Bun.gzipSync(chunk);
|
||||
const gzipped = Bun.gzipSync(chunk as Uint8Array<ArrayBuffer>);
|
||||
if (gzipped.byteLength >= chunk.byteLength) {
|
||||
return { bytes: chunk, compressionAlgorithm: null };
|
||||
}
|
||||
|
||||
return { bytes: gzipped, compressionAlgorithm: 'gzip' };
|
||||
// CRITICAL: Bun.gzipSync returns a plain Uint8Array, NOT a Buffer.
|
||||
// Telegraf (telegram.sendDocument) only recognises Buffer/Blob/stream
|
||||
// sources as file uploads — a plain Uint8Array yields an empty multipart
|
||||
// body and Telegram rejects with "400: there is no document in the request".
|
||||
// Wrap in Buffer.from(...) so chunked uploads of compressible files (MP4,
|
||||
// zip, text, etc.) actually work.
|
||||
return { bytes: Buffer.from(gzipped), compressionAlgorithm: 'gzip' };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user