# Breaking Changes
### New file import locations
Exports from the `payload` package have been _significantly_ cleaned up.
Now, just about everything is able to be imported from `payload`
directly, rather than an assortment of subpath exports. This means that
things like `import { buildConfig } from 'payload/config'` are now just
imported via `import { buildConfig } from 'payload'`. The mental model
is significantly simpler for developers, but you might need to update
some of your imports.
Payload now exposes only three exports:
1. `payload` - all types and server-only Payload code
2. `payload/shared` - utilities that can be used in either the browser
or in Node environments
3. `payload/node` - heavy utilities that should only be imported in Node
scripts and never be imported into bundled code like Next.js
### UI library pre-bundling
With this release, we've dramatically sped up the compile time for
Payload by pre-bundling our entire UI package for use inside of the
Payload admin itself. There are new exports that should be used within
Payload custom components:
1. `@payloadcms/ui/client` - all client components
2. `@payloadcms/ui/server` - all server components
For all of your custom Payload admin UI components, you should be
importing from one of these two pre-compiled barrel files rather than
importing from the more deeply nested exports directly. That will keep
compile times nice and speedy, and will also make sure that the bundled
JS for your admin UI is kept small.
For example, whereas before, if you imported the Payload `Button`, you
would have imported it like this:
```ts
import { Button } from '@payloadcms/ui/elements/Button'
```
Now, you would import it like this:
```ts
import { Button } from '@payloadcms/ui/client'
```
This is a significant DX / performance optimization that we're pretty
pumped about.
However, if you are importing or re-using Payload UI components
_outside_ of the Payload admin UI, for example in your own frontend
apps, you can import from the individual component exports which will
make sure that the bundled JS is kept to a minimum in your frontend
apps. So in your own frontend, you can continue to import directly to
the components that you want to consume rather than importing from the
pre-compiled barrel files.
Individual component exports will now come with their corresponding CSS
and everything will work perfectly as-expected.
### Specific exports have changed
- `'@payloadcms/ui/templates/Default'` and
`'@payloadcms/ui/templates/Minimal`' are now exported from
`'@payloadcms/next/templates'`
- Old: `import { LogOut } from '@payloadcms/ui/icons/LogOut'` new:
`import { LogOutIcon } from '@payloadcms/ui/icons/LogOut'`
## Background info
In effort to make local dev as fast as possible, we need to import as
few files as possible so that the compiler has less to process. One way
we've achieved this in the Admin Panel was to _remove_ all .scss imports
from all components in the `@payloadcms/ui` module using a build
process. This stripped all `import './index.scss'` statements out of
each component before injecting them into `dist`. Instead, it bundles
all of the CSS into a single `main.css` file, and we import _that_ at
the root of the app.
While this concept is _still_ the right solution to the problem, this
particular approach is not viable when using these components outside
the Admin Panel, where not only does this root stylesheet not exist, but
where it would also bloat your app with unused styles. Instead, we need
to _keep_ these .scss imports in place so they are imported directly
alongside your components, as expected. Then, we need create a _new_
build step that _separately_ compiles the components _without_ their
stylesheets—this way your app can consume either as needed from the new
`client` and `server` barrel files within `@payloadcms/ui`, i.e. from
within `@payloadcms/next` and all other admin-specific packages and
plugins.
This way, all other applications will simply import using the direct
file paths, just as they did before. Except now they come with
stylesheets.
And we've gotten a pretty awesome initial compilation performance boost.
---------
Co-authored-by: James <james@trbl.design>
Co-authored-by: Alessio Gravili <alessio@gravili.de>
167 lines
5.3 KiB
TypeScript
167 lines
5.3 KiB
TypeScript
import type { BusboyConfig } from 'busboy'
|
|
|
|
import path from 'path'
|
|
import { APIError } from 'payload'
|
|
|
|
import { isEligibleRequest } from './isEligibleRequest.js'
|
|
import { processMultipart } from './processMultipart.js'
|
|
import { debugLog } from './utilities.js'
|
|
|
|
const DEFAULT_OPTIONS = {
|
|
abortOnLimit: false,
|
|
createParentPath: false,
|
|
debug: false,
|
|
fileHandler: false,
|
|
limitHandler: false,
|
|
parseNested: false,
|
|
preserveExtension: false,
|
|
responseOnLimit: 'File size limit has been reached',
|
|
safeFileNames: false,
|
|
tempFileDir: path.join(process.cwd(), 'tmp'),
|
|
uploadTimeout: 60000,
|
|
uriDecodeFileNames: false,
|
|
useTempFiles: false,
|
|
}
|
|
|
|
export type FileShape = {
|
|
data: Buffer
|
|
encoding: string
|
|
md5: Buffer | string
|
|
mimetype: string
|
|
mv: (filePath: string, callback: () => void) => Promise<void> | void
|
|
name: string
|
|
size: number
|
|
tempFilePath: string
|
|
truncated: boolean
|
|
}
|
|
|
|
export type FetchAPIFileUploadOptions = {
|
|
/**
|
|
* Returns a HTTP 413 when the file is bigger than the size limit if `true`.
|
|
* Otherwise, it will add a `truncated = true` to the resulting file structure.
|
|
* @default false
|
|
*/
|
|
abortOnLimit?: boolean | undefined
|
|
/**
|
|
* Automatically creates the directory path specified in `.mv(filePathName)`
|
|
* @default false
|
|
*/
|
|
createParentPath?: boolean | undefined
|
|
/**
|
|
* Turn on/off upload process logging. Can be useful for troubleshooting.
|
|
* @default false
|
|
*/
|
|
debug?: boolean | undefined
|
|
/**
|
|
* User defined limit handler which will be invoked if the file is bigger than configured limits.
|
|
* @default false
|
|
*/
|
|
limitHandler?: ((args: { request: Request; size: number }) => void) | boolean | undefined
|
|
/**
|
|
* By default, `req.body` and `req.files` are flattened like this:
|
|
* `{'name': 'John', 'hobbies[0]': 'Cinema', 'hobbies[1]': 'Bike'}
|
|
*
|
|
* When this option is enabled they are parsed in order to be nested like this:
|
|
* `{'name': 'John', 'hobbies': ['Cinema', 'Bike']}`
|
|
* @default false
|
|
*/
|
|
parseNested?: boolean | undefined
|
|
/**
|
|
* Preserves filename extension when using `safeFileNames` option.
|
|
* If set to `true`, will default to an extension length of `3`.
|
|
* If set to `number`, this will be the max allowable extension length.
|
|
* If an extension is smaller than the extension length, it remains untouched. If the extension is longer,
|
|
* it is shifted.
|
|
* @default false
|
|
*
|
|
* @example
|
|
* // true
|
|
* app.use(fileUpload({ safeFileNames: true, preserveExtension: true }));
|
|
* // myFileName.ext --> myFileName.ext
|
|
*
|
|
* @example
|
|
* // max extension length 2, extension shifted
|
|
* app.use(fileUpload({ safeFileNames: true, preserveExtension: 2 }));
|
|
* // myFileName.ext --> myFileNamee.xt
|
|
*/
|
|
preserveExtension?: boolean | number | undefined
|
|
/**
|
|
* Response which will be send to client if file size limit exceeded when `abortOnLimit` set to `true`.
|
|
* @default 'File size limit has been reached'
|
|
*/
|
|
responseOnLimit?: string | undefined
|
|
/**
|
|
* Strips characters from the upload's filename.
|
|
* You can use custom regex to determine what to strip.
|
|
* If set to `true`, non-alphanumeric characters _except_ dashes and underscores will be stripped.
|
|
* This option is off by default.
|
|
* @default false
|
|
*
|
|
* @example
|
|
* // strip slashes from file names
|
|
* app.use(fileUpload({ safeFileNames: /\\/g }))
|
|
*
|
|
* @example
|
|
* app.use(fileUpload({ safeFileNames: true }))
|
|
*/
|
|
safeFileNames?: RegExp | boolean | undefined
|
|
/**
|
|
* Path to store temporary files.
|
|
* Used along with the `useTempFiles` option. By default this module uses `'tmp'` folder
|
|
* in the current working directory.
|
|
* You can use trailing slash, but it is not necessary.
|
|
* @default './tmp'
|
|
*/
|
|
tempFileDir?: string | undefined
|
|
/**
|
|
* This defines how long to wait for data before aborting. Set to `0` if you want to turn off timeout checks.
|
|
* @default 60_000
|
|
*/
|
|
uploadTimeout?: number | undefined
|
|
/**
|
|
* Applies uri decoding to file names if set `true`.
|
|
* @default false
|
|
*/
|
|
uriDecodeFileNames?: boolean | undefined
|
|
/**
|
|
* By default this module uploads files into RAM.
|
|
* Setting this option to `true` turns on using temporary files instead of utilising RAM.
|
|
* This avoids memory overflow issues when uploading large files or in case of uploading
|
|
* lots of files at same time.
|
|
* @default false
|
|
*/
|
|
useTempFiles?: boolean | undefined
|
|
} & Partial<BusboyConfig>
|
|
|
|
type FetchAPIFileUploadResponseFile = {
|
|
data: Buffer
|
|
mimetype: string
|
|
name: string
|
|
size: number
|
|
tempFilePath?: string
|
|
}
|
|
|
|
export type FetchAPIFileUploadResponse = {
|
|
error?: APIError
|
|
fields: Record<string, string>
|
|
files: Record<string, FetchAPIFileUploadResponseFile>
|
|
}
|
|
|
|
type FetchAPIFileUpload = (args: {
|
|
options?: FetchAPIFileUploadOptions
|
|
request: Request
|
|
}) => Promise<FetchAPIFileUploadResponse>
|
|
export const fetchAPIFileUpload: FetchAPIFileUpload = async ({ options, request }) => {
|
|
const uploadOptions: FetchAPIFileUploadOptions = { ...DEFAULT_OPTIONS, ...options }
|
|
if (!isEligibleRequest(request)) {
|
|
debugLog(uploadOptions, 'Request is not eligible for file upload!')
|
|
return {
|
|
error: new APIError('Request is not eligible for file upload', 500),
|
|
fields: undefined,
|
|
files: undefined,
|
|
}
|
|
} else {
|
|
return processMultipart({ options: uploadOptions, request })
|
|
}
|
|
}
|