Skip to content

Commit b8dec76

Browse files
authored
@uppy/aws-s3: Fixed default shouldUseMultipart (#5613)
fixed shouldUseMultipart and added tests for defaultOptions
1 parent 9f16760 commit b8dec76

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

packages/@uppy/aws-s3/src/index.test.ts

+91-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22

33
import 'whatwg-fetch'
44
import nock from 'nock'
5-
import Core from '@uppy/core'
6-
import AwsS3Multipart, { type AwsBody } from './index.js'
5+
import Core, { type UppyFile } from '@uppy/core'
6+
import AwsS3Multipart, {
7+
type AwsBody,
8+
type AwsS3MultipartOptions,
9+
} from './index.js'
710

811
const KB = 1024
912
const MB = KB * KB
@@ -21,6 +24,92 @@ describe('AwsS3Multipart', () => {
2124
expect(pluginNames).toContain('AwsS3Multipart')
2225
})
2326

27+
describe('defaultOptions', () => {
28+
let opts: Partial<AwsS3MultipartOptions<any, any>>
29+
30+
beforeEach(() => {
31+
const core = new Core<any, AwsBody>().use(AwsS3Multipart)
32+
const awsS3Multipart = core.getPlugin('AwsS3Multipart') as any
33+
opts = awsS3Multipart.opts
34+
})
35+
36+
it('allowedMetaFields is true', () => {
37+
expect(opts.allowedMetaFields).toBe(true)
38+
})
39+
40+
it('limit is 6', () => {
41+
expect(opts.limit).toBe(6)
42+
})
43+
44+
it('getTemporarySecurityCredentials is false', () => {
45+
expect(opts.getTemporarySecurityCredentials).toBe(false)
46+
})
47+
48+
describe('shouldUseMultipart', () => {
49+
const MULTIPART_THRESHOLD = 100 * MB
50+
51+
let shouldUseMultipart: (file: UppyFile<any, AwsBody>) => boolean
52+
53+
beforeEach(() => {
54+
shouldUseMultipart = opts.shouldUseMultipart as (
55+
file: UppyFile<any, AwsBody>,
56+
) => boolean
57+
})
58+
59+
const createFile = (size: number): UppyFile<any, any> => ({
60+
size,
61+
data: new Blob(),
62+
extension: '',
63+
id: '',
64+
isRemote: false,
65+
isGhost: false,
66+
meta: undefined,
67+
progress: {
68+
percentage: 0,
69+
bytesUploaded: 0,
70+
bytesTotal: size,
71+
uploadComplete: false,
72+
uploadStarted: 0,
73+
},
74+
type: '',
75+
})
76+
77+
it('returns true for files larger than 100MB', () => {
78+
const file = createFile(MULTIPART_THRESHOLD + 1)
79+
expect(shouldUseMultipart(file)).toBe(true)
80+
})
81+
82+
it('returns false for files exactly 100MB', () => {
83+
const file = createFile(MULTIPART_THRESHOLD)
84+
expect(shouldUseMultipart(file)).toBe(false)
85+
})
86+
87+
it('returns false for files smaller than 100MB', () => {
88+
const file = createFile(MULTIPART_THRESHOLD - 1)
89+
expect(shouldUseMultipart(file)).toBe(false)
90+
})
91+
92+
it('returns true for large files (~70GB)', () => {
93+
const file = createFile(70 * 1024 * MB)
94+
expect(shouldUseMultipart(file)).toBe(true)
95+
})
96+
97+
it('returns true for very large files (~400GB)', () => {
98+
const file = createFile(400 * 1024 * MB)
99+
expect(shouldUseMultipart(file)).toBe(true)
100+
})
101+
102+
it('returns false for files with size 0', () => {
103+
const file = createFile(0)
104+
expect(shouldUseMultipart(file)).toBe(false)
105+
})
106+
})
107+
108+
it('retryDelays is [0, 1000, 3000, 5000]', () => {
109+
expect(opts.retryDelays).toEqual([0, 1000, 3000, 5000])
110+
})
111+
})
112+
24113
describe('companionUrl assertion', () => {
25114
it('Throws an error for main functions if configured without companionUrl', () => {
26115
const core = new Core().use(AwsS3Multipart)

packages/@uppy/aws-s3/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,8 @@ const defaultOptions = {
284284
allowedMetaFields: true,
285285
limit: 6,
286286
getTemporarySecurityCredentials: false as any,
287-
// eslint-disable-next-line no-bitwise
288287
shouldUseMultipart: ((file: UppyFile<any, any>) =>
289-
// eslint-disable-next-line no-bitwise
290-
(file.size! >> 10) >> 10 > 100) as any as true,
288+
(file.size || 0) > 100 * 1024 * 1024) as any as true,
291289
retryDelays: [0, 1000, 3000, 5000],
292290
} satisfies Partial<AwsS3MultipartOptions<any, any>>
293291

0 commit comments

Comments
 (0)