Skip to content

Commit ec2a42e

Browse files
authored
Merge pull request #6413 from Shopify/file-bytecount-uploader
Improve upload reliability with accurate bytesize calculation
2 parents a315f1d + 8702cdc commit ec2a42e

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

.changeset/short-owls-drop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme': patch
3+
---
4+
5+
Fix theme file size calculation to improve upload batching reliability

packages/theme/src/cli/utilities/theme-uploader.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,17 @@ describe('theme-uploader', () => {
437437
})
438438

439439
test('should create batches for files when bulk upload request size limit is reached', async () => {
440+
const originalByteLength = Buffer.byteLength
441+
const byteLengthSpy = vi.spyOn(Buffer, 'byteLength')
442+
443+
// Mock the byte length to return the max batch bytesize
444+
byteLengthSpy.mockImplementation((value: any, encoding?: BufferEncoding) => {
445+
if (typeof value === 'string' && value === 'some_settings_data') {
446+
return MAX_BATCH_BYTESIZE
447+
}
448+
return originalByteLength(value, encoding)
449+
})
450+
440451
// Given
441452
const remoteChecksums: Checksum[] = []
442453
const themeFileSystem = fakeThemeFileSystem(
@@ -448,7 +459,6 @@ describe('theme-uploader', () => {
448459
key: 'config/settings_data.json',
449460
checksum: '2',
450461
value: 'some_settings_data',
451-
stats: {size: MAX_BATCH_BYTESIZE, mtime: 0},
452462
},
453463
],
454464
['config/settings_schema.json', {key: 'config/settings_schema.json', checksum: '3', value: 'settings_schema'}],
@@ -467,6 +477,8 @@ describe('theme-uploader', () => {
467477

468478
// Then
469479
expect(bulkUploadThemeAssets).toHaveBeenCalledTimes(3)
480+
481+
byteLengthSpy.mockRestore()
470482
})
471483

472484
test('should retry failed uploads', async () => {

packages/theme/src/cli/utilities/theme-uploader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,14 @@ function calculateLocalChecksums(localThemeFileSystem: ThemeFileSystem): Checksu
357357
const checksums: ChecksumWithSize[] = []
358358

359359
localThemeFileSystem.files.forEach((file, key) => {
360+
// Text files: use UTF-8 byte count
361+
// Binary files: use base64 length
362+
const size = file.value ? Buffer.byteLength(file.value, 'utf8') : file.attachment?.length ?? 0
363+
360364
checksums.push({
361365
key,
362366
checksum: file.checksum,
363-
size: file.stats?.size ?? 0,
367+
size,
364368
})
365369
})
366370

0 commit comments

Comments
 (0)