Skip to content

Commit

Permalink
Make allowedMetaFields consistent (#5011)
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <antoine@transloadit.com>
  • Loading branch information
Murderlon and aduh95 committed Mar 25, 2024
1 parent 63b999d commit 1aac94d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
23 changes: 13 additions & 10 deletions packages/@uppy/aws-s3-multipart/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
filterFilesToEmitUploadStarted,
} from '@uppy/utils/lib/fileFilters'
import { createAbortError } from '@uppy/utils/lib/AbortController'

import getAllowedMetaFields from '@uppy/utils/lib/getAllowedMetaFields'
import MultipartUploader from './MultipartUploader.ts'
import { throwIfAborted } from './utils.ts'
import type {
Expand Down Expand Up @@ -280,7 +280,7 @@ type RequestClientOptions = Partial<
>

interface _AwsS3MultipartOptions extends PluginOpts, RequestClientOptions {
allowedMetaFields?: string[] | null
allowedMetaFields?: string[] | boolean
limit?: number
retryDelays?: number[] | null
}
Expand All @@ -299,9 +299,7 @@ export type AwsS3MultipartOptions<
)

const defaultOptions = {
// TODO: null here means “include all”, [] means include none.
// This is inconsistent with @uppy/aws-s3 and @uppy/transloadit
allowedMetaFields: null,
allowedMetaFields: true,
limit: 6,
getTemporarySecurityCredentials: false as any,
shouldUseMultipart: ((file: UppyFile<any, any>) =>
Expand Down Expand Up @@ -487,10 +485,11 @@ export default class AwsS3Multipart<
this.assertHost('createMultipartUpload')
throwIfAborted(signal)

const metadata = getAllowedMetadata({
meta: file.meta,
allowedMetaFields: this.opts.allowedMetaFields,
})
const allowedMetaFields = getAllowedMetaFields(
this.opts.allowedMetaFields,
file.meta,
)
const metadata = getAllowedMetadata({ meta: file.meta, allowedMetaFields })

return this.#client
.post<UploadResult>(
Expand Down Expand Up @@ -653,9 +652,13 @@ export default class AwsS3Multipart<
): Promise<AwsS3UploadParameters> {
const { meta } = file
const { type, name: filename } = meta
const allowedMetaFields = getAllowedMetaFields(
this.opts.allowedMetaFields,
file.meta,
)
const metadata = getAllowedMetadata({
meta,
allowedMetaFields: this.opts.allowedMetaFields,
allowedMetaFields,
querify: true,
})

Expand Down
13 changes: 7 additions & 6 deletions packages/@uppy/tus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import type { Meta, Body, UppyFile } from '@uppy/utils/lib/UppyFile'
import type { Uppy } from '@uppy/core'
import type { RequestClient } from '@uppy/companion-client'
import getAllowedMetaFields from '@uppy/utils/lib/getAllowedMetaFields'
import getFingerprint from './getFingerprint.ts'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -56,7 +57,7 @@ export interface TusOpts<M extends Meta, B extends Body>
) => boolean
retryDelays?: number[]
withCredentials?: boolean
allowedMetaFields?: string[]
allowedMetaFields?: boolean | string[]
rateLimitedQueue?: RateLimitedQueue
}

Expand Down Expand Up @@ -92,6 +93,7 @@ const defaultOptions = {
limit: 20,
retryDelays: tusDefaultOptions.retryDelays,
withCredentials: false,
allowedMetaFields: true,
} satisfies Partial<TusOpts<any, any>>

type Opts<M extends Meta, B extends Body> = DefinePluginOpts<
Expand Down Expand Up @@ -427,11 +429,10 @@ export default class Tus<M extends Meta, B extends Body> extends BasePlugin<
// and we also don't care about the type specifically here,
// we just want to pass the meta fields along.
const meta: Record<string, string> = {}
const allowedMetaFields =
Array.isArray(opts.allowedMetaFields) ?
opts.allowedMetaFields
// Send along all fields by default.
: Object.keys(file.meta)
const allowedMetaFields = getAllowedMetaFields(
opts.allowedMetaFields,
file.meta,
)
allowedMetaFields.forEach((item) => {
// tus type definition for metadata only accepts `Record<string, string>`
// but in reality (at runtime) it accepts `Record<string, unknown>`
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"./lib/CompanionClientProvider": "./lib/CompanionClientProvider.js",
"./lib/FileProgress": "./lib/FileProgress.js",
"./src/microtip.scss": "./src/microtip.scss",
"./lib/UserFacingApiError": "./lib/UserFacingApiError.js"
"./lib/UserFacingApiError": "./lib/UserFacingApiError.js",
"./lib/getAllowedMetaFields": "./lib/getAllowedMetaFields.js"
},
"dependencies": {
"lodash": "^4.17.21",
Expand Down
14 changes: 14 additions & 0 deletions packages/@uppy/utils/src/getAllowedMetaFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Meta } from './UppyFile'

export default function getAllowedMetaFields<M extends Meta>(
fields: string[] | boolean,
meta: M,
): string[] {
if (fields === true) {
return Object.keys(meta)
}
if (Array.isArray(fields)) {
return fields
}
return []
}
19 changes: 8 additions & 11 deletions packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
// @ts-ignore We don't want TS to generate types for the package.json
import type { Meta, Body, UppyFile } from '@uppy/utils/lib/UppyFile'
import type { State, Uppy } from '@uppy/core'
import getAllowedMetaFields from '@uppy/utils/lib/getAllowedMetaFields'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
import packageJson from '../package.json'
Expand Down Expand Up @@ -61,7 +62,7 @@ export interface XhrUploadOpts<M extends Meta, B extends Body>
) => boolean
getResponseData?: (body: string, xhr: XMLHttpRequest) => B
getResponseError?: (body: string, xhr: XMLHttpRequest) => Error | NetworkError
allowedMetaFields?: string[] | null
allowedMetaFields?: string[] | boolean
bundle?: boolean
responseUrlFieldName?: string
}
Expand Down Expand Up @@ -105,7 +106,7 @@ const defaultOptions = {
formData: true,
fieldName: 'file',
method: 'post',
allowedMetaFields: null,
allowedMetaFields: true,
responseUrlFieldName: 'url',
bundle: false,
headers: {},
Expand Down Expand Up @@ -240,10 +241,7 @@ export default class XHRUpload<
meta: State<M, B>['meta'],
opts: Opts<M, B>,
): void {
const allowedMetaFields =
Array.isArray(opts.allowedMetaFields) ?
opts.allowedMetaFields
: Object.keys(meta) // Send along all fields by default.
const allowedMetaFields = getAllowedMetaFields(opts.allowedMetaFields, meta)

allowedMetaFields.forEach((item) => {
const value = meta[item]
Expand Down Expand Up @@ -560,11 +558,10 @@ export default class XHRUpload<

#getCompanionClientArgs(file: UppyFile<M, B>) {
const opts = this.getOptions(file)
const allowedMetaFields =
Array.isArray(opts.allowedMetaFields) ?
opts.allowedMetaFields
// Send along all fields by default.
: Object.keys(file.meta)
const allowedMetaFields = getAllowedMetaFields(
opts.allowedMetaFields,
file.meta,
)
return {
...file.remote?.body,
protocol: 'multipart',
Expand Down

0 comments on commit 1aac94d

Please sign in to comment.