Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/aws-s3: Fixed default shouldUseMultipart #5613

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions packages/@uppy/aws-s3/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import 'whatwg-fetch'
import nock from 'nock'
import Core from '@uppy/core'
import AwsS3Multipart, { type AwsBody } from './index.js'
import Core, { type UppyFile } from '@uppy/core'
import AwsS3Multipart, {
type AwsBody,
type AwsS3MultipartOptions,
} from './index.js'

const KB = 1024
const MB = KB * KB
Expand All @@ -21,6 +24,92 @@ describe('AwsS3Multipart', () => {
expect(pluginNames).toContain('AwsS3Multipart')
})

describe('defaultOptions', () => {
let opts: Partial<AwsS3MultipartOptions<any, any>>

beforeEach(() => {
const core = new Core<any, AwsBody>().use(AwsS3Multipart)
const awsS3Multipart = core.getPlugin('AwsS3Multipart') as any
opts = awsS3Multipart.opts
})

it('allowedMetaFields is true', () => {
expect(opts.allowedMetaFields).toBe(true)
})

it('limit is 6', () => {
expect(opts.limit).toBe(6)
})

it('getTemporarySecurityCredentials is false', () => {
expect(opts.getTemporarySecurityCredentials).toBe(false)
})

describe('shouldUseMultipart', () => {
const MULTIPART_THRESHOLD = 100 * MB

let shouldUseMultipart: (file: UppyFile<any, AwsBody>) => boolean

beforeEach(() => {
shouldUseMultipart = opts.shouldUseMultipart as (
file: UppyFile<any, AwsBody>,
) => boolean
})

const createFile = (size: number): UppyFile<any, any> => ({
size,
data: new Blob(),
extension: '',
id: '',
isRemote: false,
isGhost: false,
meta: undefined,
progress: {
percentage: 0,
bytesUploaded: 0,
bytesTotal: size,
uploadComplete: false,
uploadStarted: 0,
},
type: '',
})

it('returns true for files larger than 100MB', () => {
const file = createFile(MULTIPART_THRESHOLD + 1)
expect(shouldUseMultipart(file)).toBe(true)
})

it('returns false for files exactly 100MB', () => {
const file = createFile(MULTIPART_THRESHOLD)
expect(shouldUseMultipart(file)).toBe(false)
})

it('returns false for files smaller than 100MB', () => {
const file = createFile(MULTIPART_THRESHOLD - 1)
expect(shouldUseMultipart(file)).toBe(false)
})

it('returns true for large files (~70GB)', () => {
const file = createFile(70 * 1024 * MB)
expect(shouldUseMultipart(file)).toBe(true)
})

it('returns true for very large files (~400GB)', () => {
const file = createFile(400 * 1024 * MB)
expect(shouldUseMultipart(file)).toBe(true)
})

it('returns false for files with size 0', () => {
const file = createFile(0)
expect(shouldUseMultipart(file)).toBe(false)
})
})

it('retryDelays is [0, 1000, 3000, 5000]', () => {
expect(opts.retryDelays).toEqual([0, 1000, 3000, 5000])
})
})

describe('companionUrl assertion', () => {
it('Throws an error for main functions if configured without companionUrl', () => {
const core = new Core().use(AwsS3Multipart)
Expand Down
4 changes: 1 addition & 3 deletions packages/@uppy/aws-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,8 @@ const defaultOptions = {
allowedMetaFields: true,
limit: 6,
getTemporarySecurityCredentials: false as any,
// eslint-disable-next-line no-bitwise
shouldUseMultipart: ((file: UppyFile<any, any>) =>
// eslint-disable-next-line no-bitwise
(file.size! >> 10) >> 10 > 100) as any as true,
(file.size || 0) > 100 * 1024 * 1024) as any as true,
tvt-mika marked this conversation as resolved.
Show resolved Hide resolved
retryDelays: [0, 1000, 3000, 5000],
} satisfies Partial<AwsS3MultipartOptions<any, any>>

Expand Down
Loading