Skip to content

Commit 8efc53c

Browse files
authored
Use chunked transfer encoding for tar uploads (#1118)
1 parent 18d8cb1 commit 8efc53c

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

.changeset/chunked-upload.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"e2b": patch
3+
---
4+
5+
Use chunked transfer encoding for tar uploads to reduce memory usage

packages/js-sdk/src/template/buildApi.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export async function uploadFile(
111111
const { fileName, url, fileContextPath, ignorePatterns, resolveSymlinks } =
112112
options
113113
try {
114-
const { contentLength, uploadStream } = await tarFileStreamUpload(
114+
const uploadStream = await tarFileStreamUpload(
115115
fileName,
116116
fileContextPath,
117117
ignorePatterns,
@@ -123,9 +123,6 @@ export async function uploadFile(
123123
method: 'PUT',
124124
// @ts-expect-error
125125
body: uploadStream,
126-
headers: {
127-
'Content-Length': contentLength.toString(),
128-
},
129126
duplex: 'half',
130127
})
131128

packages/js-sdk/src/template/utils.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export async function tarFileStream(
373373
// gzip.portable ensures deterministic gzip header without affecting file modes
374374
return create(
375375
{
376-
gzip: { portable: true },
376+
gzip: true,
377377
cwd: fileContextPath,
378378
follow: resolveSymlinks,
379379
noDirRecurse: true,
@@ -383,40 +383,25 @@ export async function tarFileStream(
383383
}
384384

385385
/**
386-
* Create a tar stream and calculate its compressed size for upload.
386+
* Create a tar stream for upload using chunked transfer encoding.
387387
*
388388
* @param fileName Glob pattern for files to include
389389
* @param fileContextPath Base directory for resolving file paths
390390
* @param resolveSymlinks Whether to follow symbolic links
391-
* @returns Object containing the content length and upload stream
391+
* @returns A readable stream of the gzipped tar archive
392392
*/
393393
export async function tarFileStreamUpload(
394394
fileName: string,
395395
fileContextPath: string,
396396
ignorePatterns: string[],
397397
resolveSymlinks: boolean
398398
) {
399-
// First pass: calculate the compressed size
400-
const sizeCalculationStream = await tarFileStream(
399+
return tarFileStream(
401400
fileName,
402401
fileContextPath,
403402
ignorePatterns,
404403
resolveSymlinks
405404
)
406-
let contentLength = 0
407-
for await (const chunk of sizeCalculationStream as unknown as AsyncIterable<Buffer>) {
408-
contentLength += chunk.length
409-
}
410-
411-
return {
412-
contentLength,
413-
uploadStream: await tarFileStream(
414-
fileName,
415-
fileContextPath,
416-
ignorePatterns,
417-
resolveSymlinks
418-
),
419-
}
420405
}
421406

422407
/**

0 commit comments

Comments
 (0)