@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,43 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
import { isRunningInLambda } from '../storage-content-api/isRunningInLambda.js';
|
|
3
|
+
import { streamMediaResponse } from '../storage-content-api/streamMediaResponse.js';
|
|
4
|
+
export async function deliverImageResponse({ deliveryOrigin, filename, path, req, responseHeaderPolicy }) {
|
|
5
|
+
// The hosting proxy follows this redirect and serves the image at the Payload URL.
|
|
6
|
+
// This avoids Lambda's response size limit.
|
|
7
|
+
if (isRunningInLambda()) {
|
|
8
|
+
// Signed paths expire quickly. Vary: Origin tells the hosting proxy to prevent caching,
|
|
9
|
+
// even if a collection hook changes Cache-Control.
|
|
10
|
+
return new Response(null, {
|
|
11
|
+
headers: {
|
|
12
|
+
'Cache-Control': 'private, no-store',
|
|
13
|
+
Location: path,
|
|
14
|
+
Vary: 'Origin'
|
|
15
|
+
},
|
|
16
|
+
status: 302
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
// Without a hosting proxy, fetch the signed image and stream it through the Payload URL.
|
|
20
|
+
if (!deliveryOrigin) {
|
|
21
|
+
throw new APIError('Image delivery is not available in this environment', 503);
|
|
22
|
+
}
|
|
23
|
+
const requestHeaders = new Headers({
|
|
24
|
+
'Accept-Encoding': 'identity'
|
|
25
|
+
});
|
|
26
|
+
for (const name of [
|
|
27
|
+
'Accept',
|
|
28
|
+
'If-None-Match'
|
|
29
|
+
]){
|
|
30
|
+
const value = req.headers.get(name);
|
|
31
|
+
if (value) {
|
|
32
|
+
requestHeaders.set(name, value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return streamMediaResponse({
|
|
36
|
+
filename,
|
|
37
|
+
kind: 'imageTransform',
|
|
38
|
+
req,
|
|
39
|
+
requestHeaders,
|
|
40
|
+
responseHeaderPolicy,
|
|
41
|
+
url: new URL(path, deliveryOrigin).href
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Document } from 'payload';
|
|
2
|
+
import type { StoredImage } from './storedImage.js';
|
|
3
|
+
import type { ImageVariantPreset } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Add a `v` revision to each image URL a read returned. Payload derives file URLs from filenames,
|
|
6
|
+
* and a request-time file keeps its filename when its saved crop or preset changes, so browsers and
|
|
7
|
+
* CDNs would keep serving the old pixels. Each revision hashes what shapes that file's pixels: it
|
|
8
|
+
* stays the same across reads and changes when the rendered image would.
|
|
9
|
+
*/
|
|
10
|
+
export declare function addImageRevision({ doc, storedImage, variants, }: {
|
|
11
|
+
doc: Document;
|
|
12
|
+
storedImage: StoredImage;
|
|
13
|
+
variants: ImageVariantPreset[];
|
|
14
|
+
}): Document;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Add a `v` revision to each image URL a read returned. Payload derives file URLs from filenames,
|
|
4
|
+
* and a request-time file keeps its filename when its saved crop or preset changes, so browsers and
|
|
5
|
+
* CDNs would keep serving the old pixels. Each revision hashes what shapes that file's pixels: it
|
|
6
|
+
* stays the same across reads and changes when the rendered image would.
|
|
7
|
+
*/ export function addImageRevision({ doc, storedImage, variants }) {
|
|
8
|
+
const revisions = getImageRevisions({
|
|
9
|
+
storedImage,
|
|
10
|
+
variants
|
|
11
|
+
});
|
|
12
|
+
const revisedDoc = {
|
|
13
|
+
...doc
|
|
14
|
+
};
|
|
15
|
+
if (doc.original?.url) {
|
|
16
|
+
revisedDoc.original = {
|
|
17
|
+
...doc.original,
|
|
18
|
+
url: withRevision(doc.original.url, revisions.original)
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
// Only revise what the read returned, so unselected URLs stay absent.
|
|
22
|
+
if (Object.hasOwn(doc, 'url')) {
|
|
23
|
+
revisedDoc.url = withRevision(doc.url, revisions.main);
|
|
24
|
+
}
|
|
25
|
+
if (doc.variants) {
|
|
26
|
+
revisedDoc.variants = reviseRequestTimeVariantURLs({
|
|
27
|
+
doc,
|
|
28
|
+
revisions
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (Object.hasOwn(doc, 'thumbnailURL')) {
|
|
32
|
+
revisedDoc.thumbnailURL = getRevisedThumbnailURL({
|
|
33
|
+
doc,
|
|
34
|
+
revisedDoc,
|
|
35
|
+
variants
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return revisedDoc;
|
|
39
|
+
}
|
|
40
|
+
function getImageRevisions({ storedImage: { mainRenderedAtRequest, requestTimeVariants, savedEdits, upload }, variants }) {
|
|
41
|
+
// Every file renders from the upload, and each upload gets a new object key.
|
|
42
|
+
const uploadIdentity = [
|
|
43
|
+
upload.prefix,
|
|
44
|
+
upload._objectKey,
|
|
45
|
+
upload.filename,
|
|
46
|
+
upload.width,
|
|
47
|
+
upload.height
|
|
48
|
+
];
|
|
49
|
+
return {
|
|
50
|
+
// A request-time main file renders the saved crop, so editing the crop changes its pixels.
|
|
51
|
+
main: hashRevision([
|
|
52
|
+
uploadIdentity,
|
|
53
|
+
mainRenderedAtRequest ? savedEdits : null
|
|
54
|
+
]),
|
|
55
|
+
original: hashRevision(uploadIdentity),
|
|
56
|
+
// Stored variants are real files. Request-time ones render the saved crop with their preset.
|
|
57
|
+
variants: Object.fromEntries(variants.filter(({ name })=>requestTimeVariants.includes(name)).map((preset)=>[
|
|
58
|
+
preset.name,
|
|
59
|
+
hashRevision([
|
|
60
|
+
uploadIdentity,
|
|
61
|
+
savedEdits,
|
|
62
|
+
preset
|
|
63
|
+
])
|
|
64
|
+
]))
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function reviseRequestTimeVariantURLs({ doc, revisions }) {
|
|
68
|
+
const revisedVariants = {
|
|
69
|
+
...doc.variants
|
|
70
|
+
};
|
|
71
|
+
for (const [name, revision] of Object.entries(revisions.variants)){
|
|
72
|
+
const variant = doc.variants[name];
|
|
73
|
+
if (variant?.url) {
|
|
74
|
+
revisedVariants[name] = {
|
|
75
|
+
...variant,
|
|
76
|
+
url: withRevision(variant.url, revision)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return revisedVariants;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Payload computes `thumbnailURL` separately, as a copy of the main file's URL or the admin
|
|
84
|
+
* thumbnail variant's, so point it at the revised copy of whichever URL it repeats.
|
|
85
|
+
*/ function getRevisedThumbnailURL({ doc, revisedDoc, variants }) {
|
|
86
|
+
if (doc.thumbnailURL === doc.url) {
|
|
87
|
+
return revisedDoc.url;
|
|
88
|
+
}
|
|
89
|
+
const thumbnailVariant = variants.find(({ name })=>doc.variants?.[name]?.url === doc.thumbnailURL);
|
|
90
|
+
return thumbnailVariant ? revisedDoc.variants?.[thumbnailVariant.name]?.url : doc.thumbnailURL;
|
|
91
|
+
}
|
|
92
|
+
/** A short hash of `identity`. */ function hashRevision(identity) {
|
|
93
|
+
return createHash('sha256').update(JSON.stringify(canonicalize(identity))).digest('hex').slice(0, 24);
|
|
94
|
+
}
|
|
95
|
+
/** Set `v` to `revision`, keeping relative paths relative. */ function withRevision(value, revision) {
|
|
96
|
+
if (typeof value !== 'string' || !value) {
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
const url = new URL(value, 'http://payload.local');
|
|
100
|
+
// Payload reserves `version` for historical document selection; `v` is the image cache revision.
|
|
101
|
+
url.searchParams.set('v', revision);
|
|
102
|
+
return value.startsWith('/') ? `${url.pathname}${url.search}${url.hash}` : url.href;
|
|
103
|
+
}
|
|
104
|
+
/** Sort object keys so equal settings hash the same whatever order they were written in. */ function canonicalize(value) {
|
|
105
|
+
if (Array.isArray(value)) {
|
|
106
|
+
return value.map(canonicalize);
|
|
107
|
+
}
|
|
108
|
+
if (value && typeof value === 'object') {
|
|
109
|
+
return Object.fromEntries(Object.keys(value).sort().map((key)=>[
|
|
110
|
+
key,
|
|
111
|
+
canonicalize(value[key])
|
|
112
|
+
]));
|
|
113
|
+
}
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
import { IMAGE_OVERRIDE_KEYS } from './constants.js';
|
|
3
|
+
export function parseImageOverrides(searchParams) {
|
|
4
|
+
return Object.fromEntries(IMAGE_OVERRIDE_KEYS.flatMap((key)=>{
|
|
5
|
+
if (!searchParams?.has(key)) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
const values = searchParams.getAll(key);
|
|
9
|
+
const value = Number(values[0]);
|
|
10
|
+
if (values.length !== 1 || !/^\d+$/.test(values[0]) || !Number.isSafeInteger(value) || value <= 0 || key === 'q' && value > 100) {
|
|
11
|
+
throw new APIError(`Invalid image parameter: ${key}`, 400);
|
|
12
|
+
}
|
|
13
|
+
return [
|
|
14
|
+
[
|
|
15
|
+
key,
|
|
16
|
+
value
|
|
17
|
+
]
|
|
18
|
+
];
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function preserveImageRedirectHeaders(collection) {
|
|
2
|
+
const upload = collection.upload;
|
|
3
|
+
if (typeof upload !== 'object' || typeof upload.modifyResponseHeaders !== 'function') {
|
|
4
|
+
return collection;
|
|
5
|
+
}
|
|
6
|
+
const modifyResponseHeaders = upload.modifyResponseHeaders;
|
|
7
|
+
return {
|
|
8
|
+
...collection,
|
|
9
|
+
upload: {
|
|
10
|
+
...upload,
|
|
11
|
+
modifyResponseHeaders: ({ headers })=>{
|
|
12
|
+
const location = headers.get('Location');
|
|
13
|
+
const modifiedHeaders = modifyResponseHeaders({
|
|
14
|
+
headers
|
|
15
|
+
}) || headers;
|
|
16
|
+
if (!location?.startsWith('/__figma__/uploads/')) {
|
|
17
|
+
return modifiedHeaders;
|
|
18
|
+
}
|
|
19
|
+
// Keep the redirect target and cache protection even if the hook removes them.
|
|
20
|
+
modifiedHeaders.set('Location', location);
|
|
21
|
+
const vary = modifiedHeaders.get('Vary');
|
|
22
|
+
const varyFields = (vary ?? '').split(',').map((field)=>field.trim().toLowerCase());
|
|
23
|
+
if (!varyFields.includes('origin') && !varyFields.includes('*')) {
|
|
24
|
+
modifiedHeaders.set('Vary', vary ? `${vary}, Origin` : 'Origin');
|
|
25
|
+
}
|
|
26
|
+
return modifiedHeaders;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Config } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Figma renders the stored original, so it cannot use another transformer's output.
|
|
4
|
+
* Reject other transformers only when they select an image in a managed collection.
|
|
5
|
+
*/
|
|
6
|
+
export declare function preventConflictingImageTransformers({ collectionSlugs, config, }: {
|
|
7
|
+
collectionSlugs: Set<string>;
|
|
8
|
+
config: Config;
|
|
9
|
+
}): Config;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { APIError, ValidationError } from 'payload';
|
|
2
|
+
import { IMAGE_MEDIA_TYPES, IMAGE_TRANSFORMER_SLUG } from './constants.js';
|
|
3
|
+
import { normalizeMediaType } from './normalizeMediaType.js';
|
|
4
|
+
/**
|
|
5
|
+
* Figma renders the stored original, so it cannot use another transformer's output.
|
|
6
|
+
* Reject other transformers only when they select an image in a managed collection.
|
|
7
|
+
*/ export function preventConflictingImageTransformers({ collectionSlugs, config }) {
|
|
8
|
+
return {
|
|
9
|
+
...config,
|
|
10
|
+
upload: {
|
|
11
|
+
...config.upload,
|
|
12
|
+
transformers: config.upload?.transformers?.map((transformer)=>withImageConflictCheck({
|
|
13
|
+
collectionSlugs,
|
|
14
|
+
transformer
|
|
15
|
+
}))
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function withImageConflictCheck({ collectionSlugs, transformer }) {
|
|
20
|
+
// Leave Figma's transformer unchanged; add a conflict check to the others.
|
|
21
|
+
if (transformer.slug === IMAGE_TRANSFORMER_SLUG) {
|
|
22
|
+
return transformer;
|
|
23
|
+
}
|
|
24
|
+
const canTransform = async (args)=>{
|
|
25
|
+
// Payload selects transformers without a canTransform callback.
|
|
26
|
+
let result = true;
|
|
27
|
+
if (typeof transformer.canTransform === 'function') {
|
|
28
|
+
result = await transformer.canTransform(args);
|
|
29
|
+
}
|
|
30
|
+
const selected = result === true || typeof result === 'object' && result.canTransform;
|
|
31
|
+
if (!selected || !collectionSlugs.has(args.collectionSlug) || !IMAGE_MEDIA_TYPES.includes(normalizeMediaType(args.mimeType))) {
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
throw createConflictError({
|
|
35
|
+
args,
|
|
36
|
+
transformerSlug: transformer.slug
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
// Keep Payload's hidden metadata and the transformer's prototype.
|
|
40
|
+
return Object.create(Object.getPrototypeOf(transformer), {
|
|
41
|
+
...Object.getOwnPropertyDescriptors(transformer),
|
|
42
|
+
canTransform: {
|
|
43
|
+
configurable: true,
|
|
44
|
+
enumerable: true,
|
|
45
|
+
value: canTransform,
|
|
46
|
+
writable: true
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function createConflictError({ args, transformerSlug }) {
|
|
51
|
+
const stage = args.operation === 'upload' ? 'Upload' : 'Request';
|
|
52
|
+
const message = `${stage} transformer "${transformerSlug}" conflicts with Figma request-time images on "${args.collectionSlug}"`;
|
|
53
|
+
// Payload needs a ValidationError to show the reason during uploads and crop saves.
|
|
54
|
+
if (args.operation === 'upload' || args.purpose === 'persisted-default') {
|
|
55
|
+
return new ValidationError({
|
|
56
|
+
collection: args.collectionSlug,
|
|
57
|
+
errors: [
|
|
58
|
+
{
|
|
59
|
+
message,
|
|
60
|
+
path: 'file'
|
|
61
|
+
}
|
|
62
|
+
],
|
|
63
|
+
req: args.req
|
|
64
|
+
}, args.req.t);
|
|
65
|
+
}
|
|
66
|
+
return new APIError(message, 400);
|
|
67
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HandleTransformRequestArgs } from 'payload';
|
|
2
|
+
import type { StorageClient } from '../storage-content-api/client.js';
|
|
3
|
+
import type { FigmaImageTransformsConfig } from './types.js';
|
|
4
|
+
export declare function serveImageRequest({ client, collections, collectionSlug, doc, req, variantName, }: {
|
|
5
|
+
client: StorageClient;
|
|
6
|
+
} & FigmaImageTransformsConfig & HandleTransformRequestArgs): Promise<Response>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { buildStoragePathData, getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
2
|
+
import { APIError } from 'payload';
|
|
3
|
+
import { getDownloadResponseHeaderPolicy } from '../storage-content-api/getDownloadResponseHeaderPolicy.js';
|
|
4
|
+
import { resolveDownloadResponseHeaders } from '../storage-content-api/resolveDownloadResponseHeaders.js';
|
|
5
|
+
import { deliverImageResponse } from './deliverImageResponse.js';
|
|
6
|
+
import { parseImageOverrides } from './parseImageOverrides.js';
|
|
7
|
+
export async function serveImageRequest({ client, collections, collectionSlug, doc, req, variantName }) {
|
|
8
|
+
const variants = collections[collectionSlug].variants;
|
|
9
|
+
const size = variantName === undefined ? undefined : variants.find(({ name })=>name === variantName);
|
|
10
|
+
if (variantName !== undefined && !size) {
|
|
11
|
+
throw new APIError('Unknown image preset', 400);
|
|
12
|
+
}
|
|
13
|
+
const request = parseImageOverrides(req.searchParams);
|
|
14
|
+
const collection = req.payload.collections[collectionSlug].config;
|
|
15
|
+
const original = await findStoredOriginal({
|
|
16
|
+
collection,
|
|
17
|
+
doc,
|
|
18
|
+
req
|
|
19
|
+
});
|
|
20
|
+
const responseHeaderPolicy = getDownloadResponseHeaderPolicy({
|
|
21
|
+
logger: req.payload.logger,
|
|
22
|
+
responseHeaders: resolveDownloadResponseHeaders({
|
|
23
|
+
collection,
|
|
24
|
+
payloadResponseHeaders: undefined
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
const { data, error, response } = await client.POST('/api/v0/uploads/transform/sign', {
|
|
28
|
+
body: {
|
|
29
|
+
collectionId: collectionSlug,
|
|
30
|
+
filename: original.storageFilePath,
|
|
31
|
+
method: req.method === 'HEAD' ? 'HEAD' : 'GET',
|
|
32
|
+
mimeType: original.mimeType,
|
|
33
|
+
request,
|
|
34
|
+
responseHeaders: responseHeaderPolicy,
|
|
35
|
+
size,
|
|
36
|
+
source: {
|
|
37
|
+
// Payload stores no size for an image it cannot measure, such as an SVG without a numeric
|
|
38
|
+
// size or viewBox. The Content API needs one only to place a saved crop.
|
|
39
|
+
...original.width && original.height ? {
|
|
40
|
+
height: original.height,
|
|
41
|
+
width: original.width
|
|
42
|
+
} : {},
|
|
43
|
+
...doc._transforms?.crop ? {
|
|
44
|
+
crop: doc._transforms.crop
|
|
45
|
+
} : {},
|
|
46
|
+
...doc._transforms?.focalPoint ? {
|
|
47
|
+
focalPoint: doc._transforms.focalPoint
|
|
48
|
+
} : {}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
signal: req.signal
|
|
52
|
+
});
|
|
53
|
+
if (error || !data) {
|
|
54
|
+
// Never log the upstream response: it can contain signed source URLs.
|
|
55
|
+
throw new APIError('Image planning is unavailable', response.status === 400 ? 400 : 502);
|
|
56
|
+
}
|
|
57
|
+
if (!data.path.startsWith('/__figma__/uploads/') || data.path.includes('#')) {
|
|
58
|
+
throw new APIError('Invalid image delivery path', 502);
|
|
59
|
+
}
|
|
60
|
+
return deliverImageResponse({
|
|
61
|
+
deliveryOrigin: data.deliveryOrigin,
|
|
62
|
+
filename: original.filename,
|
|
63
|
+
path: data.path,
|
|
64
|
+
req,
|
|
65
|
+
responseHeaderPolicy
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** Finds the uploaded file in storage. With a saved crop, Payload keeps it under `original`. */ async function findStoredOriginal({ collection, doc, req }) {
|
|
69
|
+
const original = doc.original?.filename ? doc.original : doc;
|
|
70
|
+
if (!original.filename || !original.mimeType) {
|
|
71
|
+
throw new APIError('Image original metadata is missing', 400);
|
|
72
|
+
}
|
|
73
|
+
// Figma storage configures no collection prefix, so the document's prefix and `_objectKey`
|
|
74
|
+
// locate the original, as in the static handler. A document without a nested `original` is its
|
|
75
|
+
// own original, and `getFilePrefix` then resolves the main file whatever `isOriginal` says.
|
|
76
|
+
const docPrefix = await getFilePrefix({
|
|
77
|
+
collection,
|
|
78
|
+
doc: {
|
|
79
|
+
...doc,
|
|
80
|
+
id: doc.id
|
|
81
|
+
},
|
|
82
|
+
filename: original.filename,
|
|
83
|
+
isOriginal: true,
|
|
84
|
+
req
|
|
85
|
+
});
|
|
86
|
+
const { storageFilePath } = buildStoragePathData({
|
|
87
|
+
docPrefix,
|
|
88
|
+
filename: original.filename
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
filename: original.filename,
|
|
92
|
+
height: original.height,
|
|
93
|
+
mimeType: original.mimeType,
|
|
94
|
+
storageFilePath,
|
|
95
|
+
width: original.width
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Document } from 'payload';
|
|
2
|
+
/** Payload's saved edits: a crop in original-file pixels and a focal point in percent. */
|
|
3
|
+
type SavedEdits = {
|
|
4
|
+
crop?: {
|
|
5
|
+
height: number;
|
|
6
|
+
width: number;
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
};
|
|
10
|
+
focalPoint?: {
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
/** The stored fields that decide what each of an image's files looks like. */
|
|
16
|
+
export type StoredImage = {
|
|
17
|
+
/** Whether Payload renders the main file at request time, as it does with a saved crop. */
|
|
18
|
+
mainRenderedAtRequest: boolean;
|
|
19
|
+
/** The variants Payload renders at request time instead of storing. */
|
|
20
|
+
requestTimeVariants: string[];
|
|
21
|
+
savedEdits: null | SavedEdits;
|
|
22
|
+
/** The uploaded file. With a saved crop, the main file is the crop and this is `original`. */
|
|
23
|
+
upload: {
|
|
24
|
+
_objectKey?: null | string;
|
|
25
|
+
filename?: null | string;
|
|
26
|
+
height?: null | number;
|
|
27
|
+
mediaType: string;
|
|
28
|
+
prefix?: null | string;
|
|
29
|
+
width?: null | number;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Record the stored image in `beforeRead`, while the document still has every stored field. This
|
|
34
|
+
* relies on the database returning unselected fields; the Content API adapter does not forward
|
|
35
|
+
* `select`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function captureStoredImage(doc: Document): Document;
|
|
38
|
+
/**
|
|
39
|
+
* Separate what a read returns from the stored image its files render. Creates and updates skip
|
|
40
|
+
* `beforeRead`, so their stored image comes from the returned document, unless the operation's
|
|
41
|
+
* `select` removed part of it: then there is none, and the document keeps what Payload wrote rather
|
|
42
|
+
* than values derived from partial inputs. The Content API adapter returns every unset field as null,
|
|
43
|
+
* nested ones included, so a missing key was not selected.
|
|
44
|
+
*/
|
|
45
|
+
export declare function takeStoredImage(doc: Document): {
|
|
46
|
+
readDoc: Document;
|
|
47
|
+
storedImage: null | StoredImage;
|
|
48
|
+
};
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { normalizeMediaType } from './normalizeMediaType.js';
|
|
2
|
+
/**
|
|
3
|
+
* Carries the stored image from `captureStoredImage` to `takeStoredImage`. Between them, Payload
|
|
4
|
+
* removes the fields a read did not select, which can include all of them. A symbol is not a field,
|
|
5
|
+
* so selection keeps it, and JSON responses never include it.
|
|
6
|
+
*/ const capturedImage = Symbol('storedImage');
|
|
7
|
+
/**
|
|
8
|
+
* Record the stored image in `beforeRead`, while the document still has every stored field. This
|
|
9
|
+
* relies on the database returning unselected fields; the Content API adapter does not forward
|
|
10
|
+
* `select`.
|
|
11
|
+
*/ export function captureStoredImage(doc) {
|
|
12
|
+
const storedImage = getStoredImage(doc);
|
|
13
|
+
return storedImage ? {
|
|
14
|
+
...doc,
|
|
15
|
+
[capturedImage]: storedImage
|
|
16
|
+
} : doc;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Separate what a read returns from the stored image its files render. Creates and updates skip
|
|
20
|
+
* `beforeRead`, so their stored image comes from the returned document, unless the operation's
|
|
21
|
+
* `select` removed part of it: then there is none, and the document keeps what Payload wrote rather
|
|
22
|
+
* than values derived from partial inputs. The Content API adapter returns every unset field as null,
|
|
23
|
+
* nested ones included, so a missing key was not selected.
|
|
24
|
+
*/ export function takeStoredImage(doc) {
|
|
25
|
+
const { [capturedImage]: captured, ...readDoc } = doc;
|
|
26
|
+
if (captured) {
|
|
27
|
+
return {
|
|
28
|
+
readDoc,
|
|
29
|
+
storedImage: captured
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
readDoc,
|
|
34
|
+
storedImage: returnedEveryStoredField(readDoc) ? getStoredImage(readDoc) : null
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** The fields of the uploaded file's entry that `getStoredImage` reads. */ const UPLOAD_FIELDS = [
|
|
38
|
+
'_objectKey',
|
|
39
|
+
'filename',
|
|
40
|
+
'height',
|
|
41
|
+
'mimeType',
|
|
42
|
+
'prefix',
|
|
43
|
+
'width'
|
|
44
|
+
];
|
|
45
|
+
/**
|
|
46
|
+
* Whether the document still has every field `getStoredImage` reads. `select` can remove fields
|
|
47
|
+
* inside `original` and each variant while keeping the group itself, so check inside them too.
|
|
48
|
+
*/ function returnedEveryStoredField(doc) {
|
|
49
|
+
return hasFields(doc, [
|
|
50
|
+
'_stored',
|
|
51
|
+
'_transforms'
|
|
52
|
+
]) && // Without `original.filename`, `getUploadEntry` would pick the main file instead.
|
|
53
|
+
hasFields(doc.original, [
|
|
54
|
+
'filename'
|
|
55
|
+
]) && hasFields(getUploadEntry(doc), UPLOAD_FIELDS) && Object.values(doc.variants ?? {}).every((variant)=>hasFields(variant, [
|
|
56
|
+
'_stored'
|
|
57
|
+
]));
|
|
58
|
+
}
|
|
59
|
+
function hasFields(entry, fields) {
|
|
60
|
+
return typeof entry === 'object' && entry !== null && fields.every((field)=>Object.hasOwn(entry, field));
|
|
61
|
+
}
|
|
62
|
+
/** With a saved crop, the main file is the crop and Payload keeps the upload under `original`. */ function getUploadEntry(doc) {
|
|
63
|
+
return doc.original?.filename ? doc.original : doc;
|
|
64
|
+
}
|
|
65
|
+
/** Copies the stored fields, because Payload removes unselected nested fields in place. */ function getStoredImage(doc) {
|
|
66
|
+
const upload = getUploadEntry(doc);
|
|
67
|
+
const mediaType = normalizeMediaType(upload.mimeType ?? '');
|
|
68
|
+
if (!mediaType.startsWith('image/')) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
mainRenderedAtRequest: doc._stored === false,
|
|
73
|
+
requestTimeVariants: Object.keys(doc.variants ?? {}).filter((name)=>doc.variants[name]?._stored === false),
|
|
74
|
+
savedEdits: structuredClone(doc._transforms ?? null),
|
|
75
|
+
upload: {
|
|
76
|
+
_objectKey: upload._objectKey,
|
|
77
|
+
filename: upload.filename,
|
|
78
|
+
height: upload.height,
|
|
79
|
+
mediaType,
|
|
80
|
+
prefix: upload.prefix,
|
|
81
|
+
width: upload.width
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CloudflareImageSizeOptions } from '../imageSizeOptions.js';
|
|
2
|
+
export type FigmaImageTransformsConfig = {
|
|
3
|
+
collections: Record<string, {
|
|
4
|
+
variants: ({
|
|
5
|
+
name: string;
|
|
6
|
+
} & CloudflareImageSizeOptions)[];
|
|
7
|
+
}>;
|
|
8
|
+
};
|
|
9
|
+
export type ImageVariantPreset = FigmaImageTransformsConfig['collections'][string]['variants'][number];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Document } from 'payload';
|
|
2
|
+
import type { StoredImage } from './storedImage.js';
|
|
3
|
+
import type { ImageVariantPreset } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Payload stores request-time variants with null dimensions and MIME type. Describe the image
|
|
6
|
+
* Cloudflare will render for each preset, so consumers can lay it out before loading it. A
|
|
7
|
+
* value stays null when it depends on rendered pixels or on the viewer's browser.
|
|
8
|
+
*/
|
|
9
|
+
export declare function addVariantMetadata({ doc, storedImage, variants, }: {
|
|
10
|
+
doc: Document;
|
|
11
|
+
storedImage: StoredImage;
|
|
12
|
+
variants: ImageVariantPreset[];
|
|
13
|
+
}): Document;
|