Skip to content

Commit

Permalink
@uppy/aws-s3: Fixed default shouldUseMultipart (#5613)
Browse files Browse the repository at this point in the history
fixed shouldUseMultipart and added tests for defaultOptions
  • Loading branch information
tvt-mika authored Jan 20, 2025
1 parent 9f16760 commit b8dec76
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
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,
retryDelays: [0, 1000, 3000, 5000],
} satisfies Partial<AwsS3MultipartOptions<any, any>>

Expand Down

0 comments on commit b8dec76

Please sign in to comment.