@payloadcms/figma 1.0.0-beta.4 → 1.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/asset-collection.d.ts +15 -0
- package/dist/adapters/asset-collection.js +51 -45
- package/dist/adapters/build-lambda-zip.d.ts +8 -9
- package/dist/adapters/build-lambda-zip.js +36 -44
- package/dist/adapters/index.d.ts +4 -0
- package/dist/adapters/index.js +4 -0
- package/dist/adapters/lambdaLimits.d.ts +6 -0
- package/dist/adapters/lambdaLimits.js +5 -0
- package/dist/adapters/nextjs-assets.d.ts +7 -0
- package/dist/adapters/nextjs-assets.js +44 -0
- package/dist/adapters/nextjs.d.ts +8 -0
- package/dist/adapters/nextjs.js +19 -37
- package/dist/adapters/nitro.js +18 -43
- package/dist/adapters/omit-optional-sharp.d.ts +22 -0
- package/dist/adapters/omit-optional-sharp.js +158 -0
- package/dist/adapters/secret-files.js +8 -1
- package/dist/adapters/types.d.ts +8 -0
- package/dist/adapters/v2/core/build.d.ts +69 -0
- package/dist/adapters/v2/core/build.js +183 -0
- package/dist/adapters/v2/core/content-type.d.ts +3 -0
- package/dist/adapters/v2/core/content-type.js +41 -0
- package/dist/adapters/v2/core/getRelativePathError.d.ts +5 -0
- package/dist/adapters/v2/core/getRelativePathError.js +21 -0
- package/dist/adapters/v2/core/index.d.ts +15 -0
- package/dist/adapters/v2/core/index.js +13 -0
- package/dist/adapters/v2/core/limits.d.ts +28 -0
- package/dist/adapters/v2/core/limits.js +26 -0
- package/dist/adapters/v2/core/manifest-version.d.ts +10 -0
- package/dist/adapters/v2/core/manifest-version.js +14 -0
- package/dist/adapters/v2/core/server-zip.d.ts +58 -0
- package/dist/adapters/v2/core/server-zip.js +123 -0
- package/dist/adapters/v2/core/services.d.ts +46 -0
- package/dist/adapters/v2/core/services.js +202 -0
- package/dist/adapters/v2/core/static-path.d.ts +13 -0
- package/dist/adapters/v2/core/static-path.js +32 -0
- package/dist/adapters/v2/core/static-site.d.ts +26 -0
- package/dist/adapters/v2/core/static-site.js +95 -0
- package/dist/adapters/v2/core/types.d.ts +128 -0
- package/dist/adapters/v2/core/types.js +7 -0
- package/dist/adapters/v2/core/upload.d.ts +67 -0
- package/dist/adapters/v2/core/upload.js +198 -0
- package/dist/adapters/v2/core/validateDeployment/createIssue.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/createIssue.js +7 -0
- package/dist/adapters/v2/core/validateDeployment/escapePointer.d.ts +2 -0
- package/dist/adapters/v2/core/validateDeployment/escapePointer.js +3 -0
- package/dist/adapters/v2/core/validateDeployment/singletonHeaders.d.ts +2 -0
- package/dist/adapters/v2/core/validateDeployment/singletonHeaders.js +10 -0
- package/dist/adapters/v2/core/validateDeployment/types.d.ts +16 -0
- package/dist/adapters/v2/core/validateDeployment/types.js +8 -0
- package/dist/adapters/v2/core/validateDeployment/validateArtifacts.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateArtifacts.js +30 -0
- package/dist/adapters/v2/core/validateDeployment/validateCachePolicy.d.ts +8 -0
- package/dist/adapters/v2/core/validateDeployment/validateCachePolicy.js +26 -0
- package/dist/adapters/v2/core/validateDeployment/validateCounts/validateCount.d.ts +7 -0
- package/dist/adapters/v2/core/validateDeployment/validateCounts/validateCount.js +11 -0
- package/dist/adapters/v2/core/validateDeployment/validateCounts.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateCounts.js +41 -0
- package/dist/adapters/v2/core/validateDeployment/validateFallback.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateFallback.js +21 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderName.d.ts +6 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderName.js +13 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderRules/validateHeaderOp/isReservedRuleHeader.d.ts +2 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderRules/validateHeaderOp/isReservedRuleHeader.js +13 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderRules/validateHeaderOp.d.ts +11 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderRules/validateHeaderOp.js +38 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderRules.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderRules.js +17 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderValue.d.ts +6 -0
- package/dist/adapters/v2/core/validateDeployment/validateHeaderValue.js +24 -0
- package/dist/adapters/v2/core/validateDeployment/validateId.d.ts +7 -0
- package/dist/adapters/v2/core/validateDeployment/validateId.js +12 -0
- package/dist/adapters/v2/core/validateDeployment/validateMatch.d.ts +15 -0
- package/dist/adapters/v2/core/validateDeployment/validateMatch.js +74 -0
- package/dist/adapters/v2/core/validateDeployment/validatePublicPath.d.ts +7 -0
- package/dist/adapters/v2/core/validateDeployment/validatePublicPath.js +21 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateHeaderList/findRepeatedSingletons.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateHeaderList/findRepeatedSingletons.js +16 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateHeaderList.d.ts +11 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateHeaderList.js +48 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateResponseBody.d.ts +10 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateResponseBody.js +16 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateResponseStatus.d.ts +8 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses/validateResponseStatus.js +25 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses.d.ts +7 -0
- package/dist/adapters/v2/core/validateDeployment/validateResponses.js +59 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateCaptures.d.ts +10 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateCaptures.js +12 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateRedirectAction.d.ts +14 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateRedirectAction.js +59 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateRewriteAction.d.ts +11 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateRewriteAction.js +20 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateServerAction.d.ts +11 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateServerAction.js +26 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateStaticAction.d.ts +13 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction/validateStaticAction.js +25 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction.d.ts +12 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes/validateRouteAction.js +35 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes.d.ts +6 -0
- package/dist/adapters/v2/core/validateDeployment/validateRoutes.js +55 -0
- package/dist/adapters/v2/core/validateDeployment/validateServers/listReferencedServerIds.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateServers/listReferencedServerIds.js +12 -0
- package/dist/adapters/v2/core/validateDeployment/validateServers.d.ts +7 -0
- package/dist/adapters/v2/core/validateDeployment/validateServers.js +59 -0
- package/dist/adapters/v2/core/validateDeployment/validateStaticMap.d.ts +3 -0
- package/dist/adapters/v2/core/validateDeployment/validateStaticMap.js +22 -0
- package/dist/adapters/v2/core/validateDeployment.d.ts +7 -0
- package/dist/adapters/v2/core/validateDeployment.js +42 -0
- package/dist/adapters/v2/core/write.d.ts +21 -0
- package/dist/adapters/v2/core/write.js +30 -0
- package/dist/adapters/v2/nextjs/coverage.d.ts +45 -0
- package/dist/adapters/v2/nextjs/coverage.js +113 -0
- package/dist/adapters/v2/nextjs/read-build-output.d.ts +116 -0
- package/dist/adapters/v2/nextjs/read-build-output.js +368 -0
- package/dist/adapters/v2/nextjs/translate.d.ts +20 -0
- package/dist/adapters/v2/nextjs/translate.js +400 -0
- package/dist/adapters/vite.d.ts +7 -0
- package/dist/adapters/vite.js +47 -0
- package/dist/adapters/writeZip.d.ts +20 -0
- package/dist/adapters/writeZip.js +28 -0
- package/dist/api/control-plane.d.ts +28 -6
- package/dist/api/control-plane.js +67 -64
- package/dist/auth/cli-build-context.d.ts +3 -0
- package/dist/auth/cli-build-context.js +2 -0
- package/dist/auth/oauth-flow.d.ts +3 -3
- package/dist/auth/oauth-flow.js +13 -3
- package/dist/cli.js +15 -0
- package/dist/commands/build-manifest.d.ts +49 -0
- package/dist/commands/build-manifest.js +111 -0
- package/dist/commands/deploy.js +195 -26
- package/dist/db-content-api/generated/content-api-types.d.ts +18 -14
- package/dist/db-content-api/index.js +4 -1
- package/dist/db-content-api/utilities/where.js +2 -6
- package/dist/endpoints/client-config.d.ts +3 -0
- package/dist/endpoints/client-config.js +87 -0
- package/dist/image-transforms/constants.d.ts +8 -0
- package/dist/image-transforms/constants.js +26 -0
- package/dist/image-transforms/createImageTransformer.d.ts +6 -0
- package/dist/image-transforms/createImageTransformer.js +100 -0
- package/dist/image-transforms/deliverImageResponse.d.ts +11 -0
- package/dist/image-transforms/deliverImageResponse.js +43 -0
- package/dist/image-transforms/imageRevision.d.ts +14 -0
- package/dist/image-transforms/imageRevision.js +115 -0
- package/dist/image-transforms/normalizeMediaType.d.ts +2 -0
- package/dist/image-transforms/normalizeMediaType.js +3 -0
- package/dist/image-transforms/parseImageOverrides.d.ts +4 -0
- package/dist/image-transforms/parseImageOverrides.js +20 -0
- package/dist/image-transforms/preserveImageRedirectHeaders.d.ts +2 -0
- package/dist/image-transforms/preserveImageRedirectHeaders.js +30 -0
- package/dist/image-transforms/preventConflictingImageTransformers.d.ts +9 -0
- package/dist/image-transforms/preventConflictingImageTransformers.js +67 -0
- package/dist/image-transforms/serveImageRequest.d.ts +6 -0
- package/dist/image-transforms/serveImageRequest.js +97 -0
- package/dist/image-transforms/storedImage.d.ts +49 -0
- package/dist/image-transforms/storedImage.js +84 -0
- package/dist/image-transforms/types.d.ts +9 -0
- package/dist/image-transforms/types.js +1 -0
- package/dist/image-transforms/variantMetadata.d.ts +13 -0
- package/dist/image-transforms/variantMetadata.js +164 -0
- package/dist/imageSizeOptions.d.ts +8 -8
- package/dist/imageSizeOptions.js +0 -2
- package/dist/index.d.ts +1 -0
- package/dist/plugin/build-config.d.ts +3 -4
- package/dist/plugin/build-config.js +32 -16
- package/dist/storage-content-api/buildClientUploadFetchBackHeaders.d.ts +2 -1
- package/dist/storage-content-api/buildClientUploadFetchBackHeaders.js +2 -1
- package/dist/storage-content-api/buildMediaResponseHeaders.d.ts +2 -1
- package/dist/storage-content-api/buildMediaResponseHeaders.js +11 -5
- package/dist/storage-content-api/isRunningInLambda.d.ts +2 -0
- package/dist/storage-content-api/isRunningInLambda.js +3 -0
- package/dist/storage-content-api/staticHandler.js +13 -51
- package/dist/storage-content-api/streamMediaResponse.d.ts +17 -0
- package/dist/storage-content-api/streamMediaResponse.js +96 -0
- package/dist/utils/lambda-config.js +1 -1
- package/dist/utils/messages.js +8 -0
- package/dist/utils/next-image-loader-config.js +43 -6
- package/dist/utils/next-image-loader-file.js +8 -0
- package/package.json +7 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { UNRESIZABLE_IMAGE_MIME_TYPES } from './constants.js';
|
|
2
|
+
/** Formats Cloudflare always returns when asked. `getOutputMimeType` explains AVIF and PNG. */ const MIME_TYPE_BY_FORMAT = {
|
|
3
|
+
'baseline-jpeg': 'image/jpeg',
|
|
4
|
+
jpeg: 'image/jpeg',
|
|
5
|
+
'png-force': 'image/png',
|
|
6
|
+
webp: 'image/webp'
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Payload stores request-time variants with null dimensions and MIME type. Describe the image
|
|
10
|
+
* Cloudflare will render for each preset, so consumers can lay it out before loading it. A
|
|
11
|
+
* value stays null when it depends on rendered pixels or on the viewer's browser.
|
|
12
|
+
*/ export function addVariantMetadata({ doc, storedImage, variants }) {
|
|
13
|
+
const revisedDoc = {
|
|
14
|
+
...doc
|
|
15
|
+
};
|
|
16
|
+
if (storedImage.mainRenderedAtRequest && Object.hasOwn(doc, 'mimeType')) {
|
|
17
|
+
// The browser can change the output format of a request-time main file.
|
|
18
|
+
revisedDoc.mimeType = null;
|
|
19
|
+
}
|
|
20
|
+
if (!doc.variants) {
|
|
21
|
+
return revisedDoc;
|
|
22
|
+
}
|
|
23
|
+
const revisedVariants = {
|
|
24
|
+
...doc.variants
|
|
25
|
+
};
|
|
26
|
+
for (const [name, variantMetadata] of Object.entries(describeRequestTimeVariants({
|
|
27
|
+
storedImage,
|
|
28
|
+
variants
|
|
29
|
+
}))){
|
|
30
|
+
const variant = doc.variants[name];
|
|
31
|
+
if (variant) {
|
|
32
|
+
// Payload stores these fields as null and removes the ones a read did not select.
|
|
33
|
+
const selectedMetadata = Object.entries(variantMetadata).filter(([key])=>Object.hasOwn(variant, key));
|
|
34
|
+
revisedVariants[name] = {
|
|
35
|
+
...variant,
|
|
36
|
+
...Object.fromEntries(selectedMetadata)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
...revisedDoc,
|
|
42
|
+
variants: revisedVariants
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function describeRequestTimeVariants({ storedImage: { requestTimeVariants, savedEdits, upload }, variants }) {
|
|
46
|
+
const source = getRenderedSource({
|
|
47
|
+
crop: savedEdits?.crop,
|
|
48
|
+
upload
|
|
49
|
+
});
|
|
50
|
+
const metadata = {};
|
|
51
|
+
for (const preset of variants){
|
|
52
|
+
if (!requestTimeVariants.includes(preset.name)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
// These are served whole, and Payload rejects a saved crop on them, so `source` is the original.
|
|
56
|
+
metadata[preset.name] = UNRESIZABLE_IMAGE_MIME_TYPES.has(upload.mediaType) ? {
|
|
57
|
+
height: source?.height ?? null,
|
|
58
|
+
mimeType: upload.mediaType,
|
|
59
|
+
width: source?.width ?? null
|
|
60
|
+
} : {
|
|
61
|
+
...getPresetDimensions({
|
|
62
|
+
preset,
|
|
63
|
+
source
|
|
64
|
+
}),
|
|
65
|
+
mimeType: getOutputMimeType(preset)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return metadata;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The MIME type Cloudflare returns, when the preset guarantees one. Without a `format`, the
|
|
72
|
+
* Content API signs `fmt=auto`, which varies by browser. AVIF falls back to WebP or JPEG for
|
|
73
|
+
* images too large to encode quickly, and `compression: 'fast'` usually switches to JPEG:
|
|
74
|
+
* https://developers.cloudflare.com/images/transform-images/transform-via-url/
|
|
75
|
+
* Cloudflare does not document its PNG formats, but it offers `png-force` alongside `png`, so
|
|
76
|
+
* only `png-force` is taken to guarantee PNG.
|
|
77
|
+
*/ function getOutputMimeType(preset) {
|
|
78
|
+
if (!preset.format || preset.format === 'avif' || preset.format === 'png' || preset.compression === 'fast') {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
return MIME_TYPE_BY_FORMAT[preset.format];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Payload renders variants from the original with the saved crop applied. The saved crop is in
|
|
85
|
+
* original-file pixels: Payload converts the admin editor's percent crop before saving it.
|
|
86
|
+
*/ function getRenderedSource({ crop, upload }) {
|
|
87
|
+
const width = crop?.width ?? upload.width;
|
|
88
|
+
const height = crop?.height ?? upload.height;
|
|
89
|
+
return width && height ? {
|
|
90
|
+
height,
|
|
91
|
+
width
|
|
92
|
+
} : null;
|
|
93
|
+
}
|
|
94
|
+
function getPresetDimensions({ preset, source }) {
|
|
95
|
+
// A border changes the rendered size, so it is left unknown.
|
|
96
|
+
if (!source || preset.border !== undefined) {
|
|
97
|
+
return {
|
|
98
|
+
height: null,
|
|
99
|
+
width: null
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// Cloudflare rotates before resizing, so width and height refer to the rotated axes.
|
|
103
|
+
const rotated = preset.rotate === 90 || preset.rotate === 270 ? {
|
|
104
|
+
height: source.width,
|
|
105
|
+
width: source.height
|
|
106
|
+
} : source;
|
|
107
|
+
return fitDimensions({
|
|
108
|
+
fit: preset.fit ?? 'scale-down',
|
|
109
|
+
source: rotated,
|
|
110
|
+
target: {
|
|
111
|
+
height: preset.height ?? null,
|
|
112
|
+
width: preset.width ?? null
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/** Cloudflare's `fit` modes: https://developers.cloudflare.com/images/transform-images/transform-via-url/#fit */ function fitDimensions({ fit, source, target }) {
|
|
117
|
+
if (target.width === null && target.height === null) {
|
|
118
|
+
return source;
|
|
119
|
+
}
|
|
120
|
+
const widthScale = target.width === null ? null : target.width / source.width;
|
|
121
|
+
const heightScale = target.height === null ? null : target.height / source.height;
|
|
122
|
+
if (widthScale === null || heightScale === null) {
|
|
123
|
+
// With one target dimension, every mode scales proportionally within its enlargement rule.
|
|
124
|
+
return scale(source, limitEnlargement(fit, widthScale ?? heightScale));
|
|
125
|
+
}
|
|
126
|
+
switch(fit){
|
|
127
|
+
case 'contain':
|
|
128
|
+
case 'scale-down':
|
|
129
|
+
case 'scale-up':
|
|
130
|
+
return scale(source, limitEnlargement(fit, Math.min(widthScale, heightScale)));
|
|
131
|
+
case 'cover':
|
|
132
|
+
case 'pad':
|
|
133
|
+
case 'squeeze':
|
|
134
|
+
return target;
|
|
135
|
+
case 'crop':
|
|
136
|
+
{
|
|
137
|
+
// Like cover, then cropped to the target, but never enlarged.
|
|
138
|
+
const scaled = scale(source, Math.min(Math.max(widthScale, heightScale), 1));
|
|
139
|
+
return {
|
|
140
|
+
height: Math.min(target.height, scaled.height),
|
|
141
|
+
width: Math.min(target.width, scaled.width)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
default:
|
|
145
|
+
return {
|
|
146
|
+
height: null,
|
|
147
|
+
width: null
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function limitEnlargement(fit, factor) {
|
|
152
|
+
if (fit === 'scale-down' || fit === 'crop') {
|
|
153
|
+
return Math.min(factor, 1);
|
|
154
|
+
}
|
|
155
|
+
return fit === 'scale-up' ? Math.max(factor, 1) : factor;
|
|
156
|
+
}
|
|
157
|
+
/** No image is 0 pixels on a side, so a thin strip keeps at least one pixel. */ function scale(source, factor) {
|
|
158
|
+
// Avoid losing a pixel when an exact size comes out just below an integer.
|
|
159
|
+
const roundingTolerance = 1e-9;
|
|
160
|
+
return {
|
|
161
|
+
height: Math.max(1, Math.floor(source.height * factor + roundingTolerance)),
|
|
162
|
+
width: Math.max(1, Math.floor(source.width * factor + roundingTolerance))
|
|
163
|
+
};
|
|
164
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import type { RequestInitCfPropertiesImage } from '@cloudflare/workers-types';
|
|
2
2
|
type CloudflareWorkerImageOptions = RequestInitCfPropertiesImage;
|
|
3
|
-
export declare const supportedCloudflareImageOptionKeys: readonly ["anim", "background", "blur", "border", "brightness", "compression", "contrast", "
|
|
3
|
+
export declare const supportedCloudflareImageOptionKeys: readonly ["anim", "background", "blur", "border", "brightness", "compression", "contrast", "fit", "flip", "format", "gamma", "gravity", "height", "metadata", "quality", "rotate", "saturation", "segment", "sharpen", "upscale", "width", "zoom"];
|
|
4
4
|
type SupportedCloudflareImageOptionKey = (typeof supportedCloudflareImageOptionKeys)[number];
|
|
5
5
|
type SupportedCloudflareImageOptions = Pick<CloudflareWorkerImageOptions, SupportedCloudflareImageOptionKey>;
|
|
6
6
|
type CloudflareImageSizeFormat = Exclude<NonNullable<SupportedCloudflareImageOptions['format']>, 'json' | 'svg'>;
|
|
7
|
-
type
|
|
7
|
+
type PassthroughCloudflareImageOptions = Omit<SupportedCloudflareImageOptions, 'format' | 'rotate'>;
|
|
8
8
|
/**
|
|
9
9
|
* Options accepted for a reusable Payload image-size preset.
|
|
10
10
|
*
|
|
11
11
|
* Presets can control the generated image, but cannot use remote overlays or
|
|
12
12
|
* forward authentication to an image origin. Formats that do not produce a
|
|
13
13
|
* supported stored image are also excluded.
|
|
14
|
+
*
|
|
15
|
+
* Presets cannot set `trim` or `dpr`. An editor's saved crop is sent to Cloudflare as `trim`,
|
|
16
|
+
* which a preset's trim would replace and Cloudflare would multiply by `dpr`. Set a larger
|
|
17
|
+
* `width`, `height` and `border` instead of `dpr`.
|
|
14
18
|
*/
|
|
15
19
|
export type CloudflareImageSizeOptions = {
|
|
16
20
|
format?: CloudflareImageSizeFormat;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
interface RegisteredImageSizeOptions {
|
|
20
|
-
figma: CloudflareImageSizeOptions;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
21
|
+
rotate?: 90 | 180 | 270;
|
|
22
|
+
} & PassthroughCloudflareImageOptions;
|
|
23
23
|
export {};
|
package/dist/imageSizeOptions.js
CHANGED
|
@@ -6,7 +6,6 @@ export const supportedCloudflareImageOptionKeys = [
|
|
|
6
6
|
'brightness',
|
|
7
7
|
'compression',
|
|
8
8
|
'contrast',
|
|
9
|
-
'dpr',
|
|
10
9
|
'fit',
|
|
11
10
|
'flip',
|
|
12
11
|
'format',
|
|
@@ -19,7 +18,6 @@ export const supportedCloudflareImageOptionKeys = [
|
|
|
19
18
|
'saturation',
|
|
20
19
|
'segment',
|
|
21
20
|
'sharpen',
|
|
22
|
-
'trim',
|
|
23
21
|
'upscale',
|
|
24
22
|
'width',
|
|
25
23
|
'zoom'
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { contentAPIAdapter } from './db-content-api/index.js';
|
|
2
|
+
export type { FigmaImageTransformsConfig } from './image-transforms/types.js';
|
|
2
3
|
export type { CloudflareImageSizeOptions } from './imageSizeOptions.js';
|
|
3
4
|
export { figmaContentSystem } from './plugin/adapter.js';
|
|
4
5
|
export type { FigmaContentSystemOptions } from './plugin/adapter.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Config, SanitizedConfig, UploadInstructionsAccess } from 'payload';
|
|
2
|
+
import type { FigmaImageTransformsConfig } from '../image-transforms/types.js';
|
|
2
3
|
/**
|
|
3
4
|
* Worker-trusted marker injected by Figma's sites-worker after it validates the
|
|
4
5
|
* X-Figma-Job-Queues-Auth-Bypass secret on a Payload jobs endpoint. The default
|
|
@@ -18,10 +19,7 @@ export declare const FIGMA_INTERNAL_FORWARDED_HAS_AUTH_BYPASS_HEADER = "figma-in
|
|
|
18
19
|
* doesn't accept it back in. `buildFigmaConfig` injects the value at
|
|
19
20
|
* runtime from `process.env.PAYLOAD_SECRET` (Lambda) or the encrypted
|
|
20
21
|
* TokenStore cache (local dev) — never written to `.env`.
|
|
21
|
-
* - `previousSecrets`:
|
|
22
|
-
* data written under the old shared hosted secret keeps decrypting after
|
|
23
|
-
* the platform switched to per-tenant secrets; any tenant-supplied entries
|
|
24
|
-
* are preserved in order.
|
|
22
|
+
* - `previousSecrets`: Passed through unchanged; the plugin adds no shared secrets.
|
|
25
23
|
* - `editor`: Optional (defaults to lexicalEditor() if not provided)
|
|
26
24
|
* - `contentSystemId`: Optional (resolved automatically from env vars or bootstrap cache)
|
|
27
25
|
*
|
|
@@ -56,6 +54,7 @@ export type FigmaConfig = {
|
|
|
56
54
|
figma: {
|
|
57
55
|
contentSystemId?: string;
|
|
58
56
|
environmentId?: string;
|
|
57
|
+
imageTransforms?: FigmaImageTransformsConfig;
|
|
59
58
|
storage?: {
|
|
60
59
|
uploadInstructionsAccess?: UploadInstructionsAccess;
|
|
61
60
|
} | boolean;
|
|
@@ -3,13 +3,16 @@ import { lexicalEditor } from '@payloadcms/richtext-lexical';
|
|
|
3
3
|
import { buildConfig as payloadBuildConfig } from 'payload';
|
|
4
4
|
import { deepMergeSimple } from 'payload/shared';
|
|
5
5
|
import { getBootstrapInfo, getEnvironmentVariableByName } from '../api/control-plane.js';
|
|
6
|
+
import { CLI_BUILD_OAUTH_TOKEN_ENV_VAR_NAME } from '../auth/cli-build-context.js';
|
|
6
7
|
import { getValidCredential } from '../auth/oauth-flow.js';
|
|
7
8
|
import { getTokenStore } from '../auth/token-store.js';
|
|
8
9
|
import { getEnvConfig } from '../constants.js';
|
|
9
10
|
import { contentAPIAdapter } from '../db-content-api/index.js';
|
|
10
11
|
import { scheduleExtractPlugin } from '../deploy/schedule-extract-plugin.js';
|
|
12
|
+
import { clientConfig } from '../endpoints/client-config.js';
|
|
11
13
|
import { health } from '../endpoints/health.js';
|
|
12
14
|
import { schema } from '../endpoints/schema.js';
|
|
15
|
+
import { createImageTransformer } from '../image-transforms/createImageTransformer.js';
|
|
13
16
|
import { oAuth2Plugin } from '../oauth/index.js';
|
|
14
17
|
import { getAdminCollectionSlug } from '../oauth/utilities/getAdminCollectionSlug.js';
|
|
15
18
|
import { createStorageClient } from '../storage-content-api/client.js';
|
|
@@ -306,13 +309,13 @@ function messageOf(error) {
|
|
|
306
309
|
/**
|
|
307
310
|
* Plugin that applies cloud storage configuration to all upload-enabled collections.
|
|
308
311
|
* Runs at the end of the plugin chain so it sees collections added by user plugins too.
|
|
309
|
-
*/ function createStoragePlugin(url, contentSystemId, uploadInstructionsAccess) {
|
|
312
|
+
*/ function createStoragePlugin(url, contentSystemId, uploadInstructionsAccess, imageTransforms) {
|
|
310
313
|
return (incomingConfig)=>{
|
|
311
314
|
const uploadCollections = (incomingConfig.collections || []).filter((c)=>c.upload);
|
|
312
315
|
if (uploadCollections.length === 0) {
|
|
313
316
|
return incomingConfig;
|
|
314
317
|
}
|
|
315
|
-
const { adapter } = createStorageConfig(url, contentSystemId, uploadInstructionsAccess);
|
|
318
|
+
const { adapter, storageClient } = createStorageConfig(url, contentSystemId, uploadInstructionsAccess);
|
|
316
319
|
const collectionsMap = uploadCollections.reduce((acc, c)=>{
|
|
317
320
|
acc[c.slug] = {
|
|
318
321
|
adapter,
|
|
@@ -323,11 +326,28 @@ function messageOf(error) {
|
|
|
323
326
|
const storagePlugin = cloudStoragePlugin({
|
|
324
327
|
collections: collectionsMap
|
|
325
328
|
});
|
|
326
|
-
return storagePlugin(
|
|
329
|
+
return storagePlugin({
|
|
330
|
+
...incomingConfig,
|
|
331
|
+
upload: {
|
|
332
|
+
...incomingConfig.upload,
|
|
333
|
+
transformers: [
|
|
334
|
+
...incomingConfig.upload?.transformers ?? [],
|
|
335
|
+
...imageTransforms ? [
|
|
336
|
+
createImageTransformer({
|
|
337
|
+
client: storageClient,
|
|
338
|
+
...imageTransforms
|
|
339
|
+
})
|
|
340
|
+
] : []
|
|
341
|
+
]
|
|
342
|
+
}
|
|
343
|
+
});
|
|
327
344
|
};
|
|
328
345
|
}
|
|
329
346
|
export async function buildFigmaConfig(config) {
|
|
330
347
|
const envConfig = getEnvConfig();
|
|
348
|
+
if (config.figma.imageTransforms && config.figma.storage === false) {
|
|
349
|
+
throw new Error('Figma image transforms require Figma storage');
|
|
350
|
+
}
|
|
331
351
|
// Resolve contentSystemId: config first, env var, then local store fallback (dev)
|
|
332
352
|
let { contentSystemId } = config.figma;
|
|
333
353
|
let environmentId = config.figma.environmentId ?? process.env.FIGMA_EID;
|
|
@@ -390,7 +410,9 @@ export async function buildFigmaConfig(config) {
|
|
|
390
410
|
// (injected by the control plane); end-users can also set it locally to silence the notice.
|
|
391
411
|
const notifyLocalJobQueue = process.env.DISABLE_LOCAL_JOB_QUEUE_NOTIFICATIONS !== 'true';
|
|
392
412
|
const usesTokenStoreAuth = !process.env.FIGMA_CONTENT_API_ACCESS_KEY && process.env.FIGMA_DEV_JWT !== 'true';
|
|
393
|
-
|
|
413
|
+
const cliBuildOAuthToken = process.env[CLI_BUILD_OAUTH_TOKEN_ENV_VAR_NAME];
|
|
414
|
+
const hasCliBuildOAuthToken = cliBuildOAuthToken != null && cliBuildOAuthToken !== '';
|
|
415
|
+
if (usesTokenStoreAuth && !hasCliBuildOAuthToken) {
|
|
394
416
|
logMissingCliAuth(getTokenStore());
|
|
395
417
|
}
|
|
396
418
|
// Determine database adapter based on environment
|
|
@@ -432,11 +454,11 @@ export async function buildFigmaConfig(config) {
|
|
|
432
454
|
}
|
|
433
455
|
const userJobsAccessRun = config.jobs?.access?.run;
|
|
434
456
|
const adminCollectionSlug = getAdminCollectionSlug(config);
|
|
435
|
-
// Hosted runtimes
|
|
436
|
-
// the process env is authoritative. Ordinary local dev still checks the
|
|
457
|
+
// Hosted runtimes and Figma CLI builds inject credentials and PAYLOAD_SECRET
|
|
458
|
+
// together, so the process env is authoritative. Ordinary local dev still checks the
|
|
437
459
|
// TokenStore first so a leftover .env value cannot shadow the platform secret.
|
|
438
460
|
let resolvedSecret;
|
|
439
|
-
if (process.env.AWS_EXECUTION_ENV || process.env.FIGMA_CONTENT_API_ACCESS_KEY) {
|
|
461
|
+
if (process.env.AWS_EXECUTION_ENV || process.env.FIGMA_CONTENT_API_ACCESS_KEY || hasCliBuildOAuthToken) {
|
|
440
462
|
resolvedSecret = process.env.PAYLOAD_SECRET;
|
|
441
463
|
} else {
|
|
442
464
|
const projectId = process.env.FIGMA_PROJECT_ID ?? getEnvVarSync(process.cwd(), 'FIGMA_PROJECT_ID');
|
|
@@ -491,7 +513,8 @@ export async function buildFigmaConfig(config) {
|
|
|
491
513
|
endpoints: [
|
|
492
514
|
...config.endpoints ?? [],
|
|
493
515
|
health,
|
|
494
|
-
schema
|
|
516
|
+
schema,
|
|
517
|
+
clientConfig
|
|
495
518
|
],
|
|
496
519
|
i18n: {
|
|
497
520
|
...config.i18n,
|
|
@@ -532,18 +555,11 @@ export async function buildFigmaConfig(config) {
|
|
|
532
555
|
options: {}
|
|
533
556
|
}
|
|
534
557
|
} : {},
|
|
535
|
-
// The platform owns rotation of PAYLOAD_SECRET; this compat entry lets
|
|
536
|
-
// data encrypted under the old shared hosted secret keep decrypting.
|
|
537
|
-
// Appended last so any tenant-supplied previousSecrets are honored first.
|
|
538
|
-
previousSecrets: [
|
|
539
|
-
...config.previousSecrets ?? [],
|
|
540
|
-
Buffer.from('TVlfU0VDUkVU', 'base64').toString('utf8')
|
|
541
|
-
],
|
|
542
558
|
// Add oauth to plugins if not already present
|
|
543
559
|
plugins: [
|
|
544
560
|
...config.plugins ?? [],
|
|
545
561
|
...config.figma.storage !== false ? [
|
|
546
|
-
createStoragePlugin(url, contentSystemId, typeof config.figma.storage === 'object' ? config.figma.storage.uploadInstructionsAccess : undefined)
|
|
562
|
+
createStoragePlugin(url, contentSystemId, typeof config.figma.storage === 'object' ? config.figma.storage.uploadInstructionsAccess : undefined, config.figma.imageTransforms)
|
|
547
563
|
] : [],
|
|
548
564
|
oAuth2Plugin({
|
|
549
565
|
collections: [
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
* Attachment and sandbox protections remain. This helper is only used when uploadReference
|
|
6
6
|
* is present; normal file requests use buildMediaResponseHeaders.
|
|
7
7
|
*/
|
|
8
|
-
export declare function buildClientUploadFetchBackHeaders({ fallbackContentType, filename, upstreamHeaders, }: {
|
|
8
|
+
export declare function buildClientUploadFetchBackHeaders({ fallbackContentType, filename, status, upstreamHeaders, }: {
|
|
9
9
|
fallbackContentType?: string;
|
|
10
10
|
filename: string;
|
|
11
|
+
status: number;
|
|
11
12
|
upstreamHeaders: Headers;
|
|
12
13
|
}): Headers;
|
|
@@ -6,7 +6,7 @@ import { shouldAttachMedia } from './shouldAttachMedia.js';
|
|
|
6
6
|
* Content-Type so Payload uses the original upload MIME type instead of application/octet-stream.
|
|
7
7
|
* Attachment and sandbox protections remain. This helper is only used when uploadReference
|
|
8
8
|
* is present; normal file requests use buildMediaResponseHeaders.
|
|
9
|
-
*/ export function buildClientUploadFetchBackHeaders({ fallbackContentType, filename, upstreamHeaders }) {
|
|
9
|
+
*/ export function buildClientUploadFetchBackHeaders({ fallbackContentType, filename, status, upstreamHeaders }) {
|
|
10
10
|
const effectiveContentType = upstreamHeaders.get('Content-Type') || fallbackContentType;
|
|
11
11
|
const upstreamHeadersWithFallback = new Headers(upstreamHeaders);
|
|
12
12
|
if (effectiveContentType) {
|
|
@@ -14,6 +14,7 @@ import { shouldAttachMedia } from './shouldAttachMedia.js';
|
|
|
14
14
|
}
|
|
15
15
|
const headers = buildMediaResponseHeaders({
|
|
16
16
|
filename,
|
|
17
|
+
status,
|
|
17
18
|
upstreamHeaders: upstreamHeadersWithFallback
|
|
18
19
|
});
|
|
19
20
|
if (effectiveContentType && shouldAttachMedia(upstreamHeadersWithFallback)) {
|
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
import { buildBaseMediaResponseHeaders } from './buildBaseMediaResponseHeaders.js';
|
|
2
2
|
import { getMediaType } from './getMediaType.js';
|
|
3
3
|
import { shouldAttachMedia } from './shouldAttachMedia.js';
|
|
4
|
-
const
|
|
4
|
+
const FORWARDED_MEDIA_CACHE_HEADERS = [
|
|
5
5
|
'Cache-Control',
|
|
6
|
-
'Content-Length',
|
|
7
|
-
'Content-Range',
|
|
8
6
|
'ETag',
|
|
9
7
|
'Last-Modified'
|
|
10
8
|
];
|
|
11
9
|
const ATTACHMENT_CSP = "sandbox; default-src 'none'; base-uri 'none'; form-action 'none'";
|
|
12
|
-
export function buildMediaResponseHeaders({ filename, upstreamHeaders }) {
|
|
10
|
+
export function buildMediaResponseHeaders({ filename, status, upstreamHeaders }) {
|
|
13
11
|
const headers = buildBaseMediaResponseHeaders();
|
|
14
|
-
|
|
12
|
+
const forwardedHeaders = status === 304 ? FORWARDED_MEDIA_CACHE_HEADERS : [
|
|
13
|
+
...FORWARDED_MEDIA_CACHE_HEADERS,
|
|
14
|
+
'Content-Length',
|
|
15
|
+
'Content-Range'
|
|
16
|
+
];
|
|
17
|
+
for (const name of forwardedHeaders){
|
|
15
18
|
const value = upstreamHeaders.get(name);
|
|
16
19
|
if (value) {
|
|
17
20
|
headers.set(name, value);
|
|
18
21
|
}
|
|
19
22
|
}
|
|
23
|
+
if (status === 304) {
|
|
24
|
+
return headers;
|
|
25
|
+
}
|
|
20
26
|
if (shouldAttachMedia(upstreamHeaders)) {
|
|
21
27
|
headers.set('Content-Type', 'application/octet-stream');
|
|
22
28
|
headers.set('Content-Disposition', attachmentDisposition(filename));
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { buildStoragePathData, getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
|
-
import { buildClientUploadFetchBackHeaders } from './buildClientUploadFetchBackHeaders.js';
|
|
3
2
|
import { buildMediaRequestHeaders } from './buildMediaRequestHeaders.js';
|
|
4
|
-
import { buildMediaResponseHeaders } from './buildMediaResponseHeaders.js';
|
|
5
3
|
import { getDownloadResponseHeaderPolicy } from './getDownloadResponseHeaderPolicy.js';
|
|
6
|
-
import {
|
|
7
|
-
import { mediaErrorResponse } from './mediaErrorResponse.js';
|
|
4
|
+
import { isRunningInLambda } from './isRunningInLambda.js';
|
|
8
5
|
import { resolveDownloadResponseHeaders } from './resolveDownloadResponseHeaders.js';
|
|
6
|
+
import { streamMediaResponse } from './streamMediaResponse.js';
|
|
9
7
|
const PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS = 300;
|
|
10
8
|
export const getHandler = ({ client, collection, prefix = '' })=>{
|
|
11
9
|
return async (req, { doc, headers, params })=>{
|
|
@@ -56,45 +54,19 @@ export const getHandler = ({ client, collection, prefix = '' })=>{
|
|
|
56
54
|
return Response.redirect(data.url, 302);
|
|
57
55
|
}
|
|
58
56
|
const requestHeaders = buildMediaRequestHeaders(req.headers);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
headers: requestHeaders,
|
|
64
|
-
redirect: 'manual',
|
|
65
|
-
referrerPolicy: 'no-referrer',
|
|
66
|
-
signal: req.signal
|
|
67
|
-
});
|
|
68
|
-
} catch {
|
|
69
|
-
// Fetch errors can contain the signed URL; log only a fixed message to avoid exposing it.
|
|
70
|
-
req.payload.logger.error('Failed to fetch file from storage');
|
|
71
|
-
return mediaErrorResponse(500, 'Internal Server Error');
|
|
72
|
-
}
|
|
73
|
-
if (fileResponse.status === 416 && requestHeaders.has('Range')) {
|
|
74
|
-
await cancelResponseBody(fileResponse);
|
|
75
|
-
return mediaErrorResponse(416, 'Range Not Satisfiable', fileResponse.headers.get('Content-Range') ?? undefined);
|
|
76
|
-
}
|
|
77
|
-
if (!fileResponse.ok) {
|
|
78
|
-
await cancelResponseBody(fileResponse);
|
|
79
|
-
req.payload.logger.error(`Failed to fetch file from storage: ${fileResponse.status}`);
|
|
80
|
-
return mediaErrorResponse(500, 'Internal Server Error');
|
|
81
|
-
}
|
|
82
|
-
if (!hasSupportedContentEncoding(fileResponse.headers)) {
|
|
83
|
-
await cancelResponseBody(fileResponse);
|
|
84
|
-
req.payload.logger.error('Unsupported Content-Encoding from storage');
|
|
85
|
-
return mediaErrorResponse(502, 'Bad Gateway');
|
|
57
|
+
// Internal upload and transform reads need bytes even when the viewer has a cached file.
|
|
58
|
+
const ifNoneMatch = req.headers.get('If-None-Match');
|
|
59
|
+
if (!isClientUpload && operation !== 'transform' && ifNoneMatch) {
|
|
60
|
+
requestHeaders.set('If-None-Match', ifNoneMatch);
|
|
86
61
|
}
|
|
87
|
-
|
|
62
|
+
// Await so unexpected failures reach this handler's catch instead of escaping to Payload.
|
|
63
|
+
return await streamMediaResponse({
|
|
88
64
|
filename,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return new Response(fileResponse.body, {
|
|
95
|
-
headers: responseHeaders,
|
|
96
|
-
status: fileResponse.status,
|
|
97
|
-
statusText: fileResponse.statusText
|
|
65
|
+
kind: isClientUpload ? 'clientUpload' : 'storage',
|
|
66
|
+
req,
|
|
67
|
+
requestHeaders,
|
|
68
|
+
responseHeaderPolicy,
|
|
69
|
+
url: data.url
|
|
98
70
|
});
|
|
99
71
|
} catch (err) {
|
|
100
72
|
req.payload.logger.error({
|
|
@@ -107,13 +79,3 @@ export const getHandler = ({ client, collection, prefix = '' })=>{
|
|
|
107
79
|
}
|
|
108
80
|
};
|
|
109
81
|
};
|
|
110
|
-
function isRunningInLambda() {
|
|
111
|
-
return process.env.AWS_EXECUTION_ENV?.startsWith('AWS_Lambda_') === true;
|
|
112
|
-
}
|
|
113
|
-
async function cancelResponseBody(response) {
|
|
114
|
-
try {
|
|
115
|
-
await response.body?.cancel();
|
|
116
|
-
} catch {
|
|
117
|
-
// The response will not be consumed after an error; cancellation is best-effort cleanup.
|
|
118
|
-
}
|
|
119
|
-
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* What the streamed bytes are for: a client upload Payload reads back, a Cloudflare image
|
|
4
|
+
* transformation, or a stored file.
|
|
5
|
+
*/
|
|
6
|
+
export type MediaResponseKind = 'clientUpload' | 'imageTransform' | 'storage';
|
|
7
|
+
/** Fetch delivery bytes without exposing signed URLs or forwarding viewer credentials. */
|
|
8
|
+
export declare function streamMediaResponse({ filename, kind, req, requestHeaders, responseHeaderPolicy, url, }: {
|
|
9
|
+
filename: string;
|
|
10
|
+
kind: MediaResponseKind;
|
|
11
|
+
req: PayloadRequest;
|
|
12
|
+
requestHeaders: Headers;
|
|
13
|
+
responseHeaderPolicy: {
|
|
14
|
+
cacheControl: string;
|
|
15
|
+
};
|
|
16
|
+
url: string;
|
|
17
|
+
}): Promise<Response>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { buildClientUploadFetchBackHeaders } from './buildClientUploadFetchBackHeaders.js';
|
|
2
|
+
import { buildMediaResponseHeaders } from './buildMediaResponseHeaders.js';
|
|
3
|
+
import { getMediaType } from './getMediaType.js';
|
|
4
|
+
import { hasSupportedContentEncoding } from './hasSupportedContentEncoding.js';
|
|
5
|
+
import { mediaErrorResponse } from './mediaErrorResponse.js';
|
|
6
|
+
/** Fetch delivery bytes without exposing signed URLs or forwarding viewer credentials. */ export async function streamMediaResponse({ filename, kind, req, requestHeaders, responseHeaderPolicy, url }) {
|
|
7
|
+
const imageTransform = kind === 'imageTransform';
|
|
8
|
+
let fileResponse;
|
|
9
|
+
try {
|
|
10
|
+
fileResponse = await fetch(url, {
|
|
11
|
+
...imageTransform ? {
|
|
12
|
+
method: req.method
|
|
13
|
+
} : {},
|
|
14
|
+
credentials: 'omit',
|
|
15
|
+
headers: requestHeaders,
|
|
16
|
+
redirect: 'manual',
|
|
17
|
+
referrerPolicy: 'no-referrer',
|
|
18
|
+
signal: req.signal
|
|
19
|
+
});
|
|
20
|
+
} catch {
|
|
21
|
+
// Fetch errors can contain the signed URL; log only a fixed message to avoid exposing it.
|
|
22
|
+
req.payload.logger.error('Failed to fetch file from storage');
|
|
23
|
+
return mediaErrorResponse(500, 'Internal Server Error');
|
|
24
|
+
}
|
|
25
|
+
if (fileResponse.status === 416 && requestHeaders.has('Range')) {
|
|
26
|
+
await cancelResponseBody(fileResponse);
|
|
27
|
+
return mediaErrorResponse(416, 'Range Not Satisfiable', fileResponse.headers.get('Content-Range') ?? undefined);
|
|
28
|
+
}
|
|
29
|
+
if (fileResponse.status === 304 && requestHeaders.has('If-None-Match')) {
|
|
30
|
+
await cancelResponseBody(fileResponse);
|
|
31
|
+
const responseHeaders = buildMediaResponseHeaders({
|
|
32
|
+
filename,
|
|
33
|
+
status: fileResponse.status,
|
|
34
|
+
upstreamHeaders: fileResponse.headers
|
|
35
|
+
});
|
|
36
|
+
// S3 can omit signed response-header overrides on 304 responses.
|
|
37
|
+
responseHeaders.set('Cache-Control', responseHeaderPolicy.cacheControl);
|
|
38
|
+
if (imageTransform) {
|
|
39
|
+
setImageTransformHeaders(responseHeaders);
|
|
40
|
+
}
|
|
41
|
+
return new Response(null, {
|
|
42
|
+
headers: responseHeaders,
|
|
43
|
+
status: 304
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (!fileResponse.ok) {
|
|
47
|
+
await cancelResponseBody(fileResponse);
|
|
48
|
+
req.payload.logger.error(`Failed to fetch file from storage: ${fileResponse.status}`);
|
|
49
|
+
return mediaErrorResponse(500, 'Internal Server Error');
|
|
50
|
+
}
|
|
51
|
+
if (!hasSupportedContentEncoding(fileResponse.headers)) {
|
|
52
|
+
await cancelResponseBody(fileResponse);
|
|
53
|
+
req.payload.logger.error('Unsupported Content-Encoding from storage');
|
|
54
|
+
return mediaErrorResponse(502, 'Bad Gateway');
|
|
55
|
+
}
|
|
56
|
+
const responseHeaders = kind === 'clientUpload' ? buildClientUploadFetchBackHeaders({
|
|
57
|
+
filename,
|
|
58
|
+
status: fileResponse.status,
|
|
59
|
+
upstreamHeaders: fileResponse.headers
|
|
60
|
+
}) : buildMediaResponseHeaders({
|
|
61
|
+
filename,
|
|
62
|
+
status: fileResponse.status,
|
|
63
|
+
upstreamHeaders: fileResponse.headers
|
|
64
|
+
});
|
|
65
|
+
if (imageTransform) {
|
|
66
|
+
setImageTransformHeaders(responseHeaders);
|
|
67
|
+
const contentSecurityPolicy = fileResponse.headers.get('Content-Security-Policy');
|
|
68
|
+
if (contentSecurityPolicy) {
|
|
69
|
+
responseHeaders.set('Content-Security-Policy', contentSecurityPolicy);
|
|
70
|
+
if (getMediaType(fileResponse.headers) === 'image/svg+xml') {
|
|
71
|
+
// Cloudflare sanitizes SVG; Payload also applies its XML policy to the final response.
|
|
72
|
+
responseHeaders.set('Content-Type', 'image/svg+xml');
|
|
73
|
+
responseHeaders.delete('Content-Disposition');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return new Response(fileResponse.body, {
|
|
78
|
+
headers: responseHeaders,
|
|
79
|
+
status: fileResponse.status,
|
|
80
|
+
statusText: fileResponse.statusText
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Headers every transformation response shares, including a 304, whose headers update the cached
|
|
85
|
+
* response. The image varies with the viewer and `Accept`, and its fetch never forwards `Range`.
|
|
86
|
+
*/ function setImageTransformHeaders(headers) {
|
|
87
|
+
headers.set('Vary', 'Cookie, Accept, Origin');
|
|
88
|
+
headers.delete('Accept-Ranges');
|
|
89
|
+
}
|
|
90
|
+
async function cancelResponseBody(response) {
|
|
91
|
+
try {
|
|
92
|
+
await response.body?.cancel();
|
|
93
|
+
} catch {
|
|
94
|
+
// The response will not be consumed after an error; cancellation is best-effort cleanup.
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -51,7 +51,7 @@ NODE_ENV=production exec node server.js
|
|
|
51
51
|
/**
|
|
52
52
|
* Add required Next.js config properties using ts-morph AST parsing
|
|
53
53
|
* - output: 'standalone' (for Lambda deployment)
|
|
54
|
-
* - images.loader: 'custom' and images.loaderFile for Payload media
|
|
54
|
+
* - images.loader: 'custom' and images.loaderFile for Payload media when no loader is configured
|
|
55
55
|
*/ async function addNextConfigProperties(projectPath) {
|
|
56
56
|
const nextConfigPath = await findNextConfigPath(projectPath);
|
|
57
57
|
const project = new Project({
|