Skip to content

Commit

Permalink
Make allowedMetaFields consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Mar 19, 2024
1 parent a787a64 commit b844de0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
11 changes: 6 additions & 5 deletions packages/@uppy/aws-s3-multipart/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import EventManager from '@uppy/utils/lib/EventManager'
import { RateLimitedQueue } from '@uppy/utils/lib/RateLimitedQueue'
import { filterNonFailedFiles, filterFilesToEmitUploadStarted } from '@uppy/utils/lib/fileFilters'
import { createAbortError } from '@uppy/utils/lib/AbortController'
import getAllowedMetaFields from '@uppy/utils/lib/getAllowedMetaFields'

import MultipartUploader, { pausingUploadReason } from './MultipartUploader.js'
import createSignedURL from './createSignedURL.js'
Expand Down Expand Up @@ -424,9 +425,7 @@ export default class AwsS3Multipart extends BasePlugin {
this.#client = new RequestClient(uppy, opts)

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,
shouldUseMultipart: (file) => file.size !== 0, // TODO: Switch default to:
// eslint-disable-next-line no-bitwise
Expand Down Expand Up @@ -513,7 +512,8 @@ export default class AwsS3Multipart extends BasePlugin {
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('s3/multipart', {
filename: file.name,
Expand Down Expand Up @@ -628,7 +628,8 @@ export default class AwsS3Multipart extends BasePlugin {
getUploadParameters (file, options) {
const { meta } = file
const { type, name: filename } = meta
const metadata = getAllowedMetadata({ meta, allowedMetaFields: this.opts.allowedMetaFields, querify: true })
const allowedMetaFields = getAllowedMetaFields(this.opts.allowedMetaFields, file.meta)
const metadata = getAllowedMetadata({ meta, allowedMetaFields, querify: true })

const query = new URLSearchParams({ filename, type, ...metadata })

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 (Array.isArray(fields)) {
return fields
}
if (fields === true) {
return Object.keys(meta)
}
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 @@ -22,6 +22,7 @@ import type { Meta, Body, UppyFile } from '@uppy/utils/lib/UppyFile'
import type { State, Uppy } from '@uppy/core'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
import getAllowedMetaFields from '@uppy/utils/lib/getAllowedMetaFields'
import packageJson from '../package.json'

Check failure on line 26 in packages/@uppy/xhr-upload/src/index.ts

View workflow job for this annotation

GitHub Actions / Type tests

Cannot find module '../package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.

Check failure on line 26 in packages/@uppy/xhr-upload/src/index.ts

View workflow job for this annotation

GitHub Actions / Browser tests

Cannot find module '../package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
import locale from './locale.ts'

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 b844de0

Please sign in to comment.