Prechádzať zdrojové kódy

Make `allowedMetaFields` consistent (#5011)

Co-authored-by: Antoine du Hamel <antoine@transloadit.com>
Merlijn Vos 1 rok pred
rodič
commit
1aac94df2c

+ 13 - 10
packages/@uppy/aws-s3-multipart/src/index.ts

@@ -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 {
@@ -280,7 +280,7 @@ type RequestClientOptions = Partial<
 >
 
 interface _AwsS3MultipartOptions extends PluginOpts, RequestClientOptions {
-  allowedMetaFields?: string[] | null
+  allowedMetaFields?: string[] | boolean
   limit?: number
   retryDelays?: number[] | null
 }
@@ -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>) =>
@@ -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>(
@@ -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,
     })
 

+ 7 - 6
packages/@uppy/tus/src/index.ts

@@ -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
@@ -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
 }
 
@@ -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<
@@ -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>`

+ 2 - 1
packages/@uppy/utils/package.json

@@ -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",

+ 14 - 0
packages/@uppy/utils/src/getAllowedMetaFields.ts

@@ -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 []
+}

+ 8 - 11
packages/@uppy/xhr-upload/src/index.ts

@@ -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'
@@ -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
 }
@@ -105,7 +106,7 @@ const defaultOptions = {
   formData: true,
   fieldName: 'file',
   method: 'post',
-  allowedMetaFields: null,
+  allowedMetaFields: true,
   responseUrlFieldName: 'url',
   bundle: false,
   headers: {},
@@ -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]
@@ -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',