@klnap/payload-redirect 0.4.0 → 0.4.2
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/CHANGELOG.md +12 -0
- package/README.md +261 -312
- package/dist/core/redirects/auto/create-redirect.js +2 -4
- package/dist/core/redirects/auto/create-redirect.js.map +1 -1
- package/dist/core/redirects/auto/persist-auto-rule.js +3 -0
- package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -1
- package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +5 -0
- package/dist/core/redirects/auto/publication/get-publication-transition.js +20 -8
- package/dist/core/redirects/auto/publication/get-publication-transition.js.map +1 -1
- package/dist/core/redirects/auto/publication/global-transition.js +9 -7
- package/dist/core/redirects/auto/publication/global-transition.js.map +1 -1
- package/dist/core/redirects/auto/publication/locales.d.ts +0 -1
- package/dist/core/redirects/auto/publication/locales.js +0 -28
- package/dist/core/redirects/auto/publication/locales.js.map +1 -1
- package/dist/core/redirects/auto/publication/versions/version-rows.d.ts +2 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.js +2 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.js.map +1 -1
- package/dist/core/redirects/collection/collection.js +5 -0
- package/dist/core/redirects/collection/collection.js.map +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +0 -4
- package/dist/core/redirects/collection/duplicate-guard.js.map +1 -1
- package/dist/core/redirects/collection/graph-lock.d.ts +6 -1
- package/dist/core/redirects/collection/graph-lock.js +11 -0
- package/dist/core/redirects/collection/graph-lock.js.map +1 -1
- package/docs/FAILURE_SEMANTICS.md +5 -4
- package/package.json +4 -2
|
@@ -9,10 +9,8 @@ export const createRedirectsFromTransitions = async ({ req, redirectCollectionSl
|
|
|
9
9
|
if (!currentPath)
|
|
10
10
|
continue;
|
|
11
11
|
const to = normalizePathOrUrl(currentPath);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return !path || normalizePathOrUrl(path) !== to;
|
|
15
|
-
});
|
|
12
|
+
// publishedDocs is newest first and starts with the publication that just ran.
|
|
13
|
+
const previousPublishedDoc = publishedDocs[1];
|
|
16
14
|
if (!previousPublishedDoc)
|
|
17
15
|
continue;
|
|
18
16
|
const previousPath = buildPublishedPath(pathOptions, previousPublishedDoc, locale, localePrefixes);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-redirect.js","sourceRoot":"","sources":["../../../../src/core/redirects/auto/create-redirect.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AACrF,OAAO,EAAE,wBAAwB,EAAE,MAAM,sEAAsE,CAAC;AAChH,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAuB,MAAM,mCAAmC,CAAC;AAS5F,MAAM,CAAC,MAAM,8BAA8B,GAAG,KAAK,EAAa,EAC9D,GAAG,EACH,sBAAsB,EACtB,WAAW,EACX,cAAc,EACd,WAAW,GAIZ,EAAiB,EAAE;IAClB,KAAK,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,EAAE,IAAI,WAAW,EAAE,CAAC;QAChE,MAAM,WAAW,GAAG,kBAAkB,CAAC,WAAW,EAAE,UAAU,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC;QACxF,IAAI,CAAC,WAAW;YAAE,SAAS;QAE3B,MAAM,EAAE,GAAG,kBAAkB,CAAC,WAAW,CAAC,CAAC;QAC3C,MAAM,oBAAoB,GAAG,aAAa,CAAC,
|
|
1
|
+
{"version":3,"file":"create-redirect.js","sourceRoot":"","sources":["../../../../src/core/redirects/auto/create-redirect.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AACrF,OAAO,EAAE,wBAAwB,EAAE,MAAM,sEAAsE,CAAC;AAChH,OAAO,EAAE,qB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W,CAAC,OAAO,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC;IACnD,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,qBAAqB,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC;IAExE,MAAM,WAAW,GAAG,MAAM,wBAAwB,CAAC,IAAI,CAAC,CAAC;IACzD,MAAM,8BAA8B,CAAC;QACnC,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,sBAAsB;QACtB,WAAW;QACX,cAAc;QACd,WAAW;KACZ,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,GAAG,CAAC;AAClB,CAAC,CAAC","sourcesContent":["import type { CollectionAfterChangeHook, PayloadRequest } from \"payload\";\nimport { ensureAutoRedirect } from \"#redirect/core/redirects/auto/persist-auto-rule\";\nimport { getPublicationTransition } from \"#redirect/core/redirects/auto/publication/get-publication-transition\";\nimport { REDIRECT_CONTEXT_KEYS } from \"#redirect/core/redirects/context-keys\";\nimport { normalizePathOrUrl } from \"#redirect/core/redirects/normalize-path\";\nimport { buildPublishedPath, type LocalePrefixes } from \"#redirect/core/url/published-path\";\nimport type { PublicationTransition, RedirectPathOptions } from \"./types.js\";\n\ntype CreateRedirectOptions<TDocument = Record<string, unknown>> = {\n redirectCollectionSlug: string;\n pathOptions: RedirectPathOptions<TDocument>;\n localePrefixes?: LocalePrefixes;\n};\n\nexport const createRedirectsFromTransitions = async <TDocument>({\n req,\n redirectCollectionSlug,\n pathOptions,\n localePrefixes,\n transitions,\n}: CreateRedirectOptions<TDocument> & {\n req: PayloadRequest;\n transitions: PublicationTransition[];\n}): Promise<void> => {\n for (const { currentDoc, publishedDocs, locale } of transitions) {\n const currentPath = buildPublishedPath(pathOptions, currentDoc, locale, localePrefixes);\n if (!currentPath) continue;\n\n const to = normalizePathOrUrl(currentPath);\n // publishedDocs is newest first and starts with the publication that just ran.\n const previousPublishedDoc = publishedDocs[1];\n if (!previousPublishedDoc) continue;\n\n const previousPath = buildPublishedPath(\n pathOptions,\n previousPublishedDoc,\n locale,\n localePrefixes,\n );\n if (!previousPath) continue;\n\n const from = normalizePathOrUrl(previousPath);\n if (from === to) continue;\n\n await ensureAutoRedirect({\n req,\n collectionSlug: redirectCollectionSlug,\n rule: { from, to },\n context: req.context,\n });\n }\n};\n\nexport const createRedirect =\n <TDocument = Record<string, unknown>>({\n redirectCollectionSlug,\n pathOptions,\n localePrefixes,\n }: CreateRedirectOptions<TDocument>): CollectionAfterChangeHook =>\n async (args) => {\n if (pathOptions.enabled === false) return args.doc;\n if (args.req.context?.[REDIRECT_CONTEXT_KEYS.skipAuto]) return args.doc;\n\n const transitions = await getPublicationTransition(args);\n await createRedirectsFromTransitions({\n req: args.req,\n redirectCollectionSlug,\n pathOptions,\n localePrefixes,\n transitions,\n });\n\n return args.doc;\n };\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isIdenticalAutoRule } from "./match-existing-rule.js";
|
|
2
|
+
import { lockRedirectRuleGraph } from "../collection/graph-lock.js";
|
|
2
3
|
import { REDIRECT_CONTEXT_KEYS } from "../context-keys.js";
|
|
3
4
|
import { findRedirectByFrom } from "../queries.js";
|
|
4
5
|
/**
|
|
@@ -18,6 +19,8 @@ export const ensureAutoRedirect = async ({ req, collectionSlug, rule, context, }
|
|
|
18
19
|
origin: "auto",
|
|
19
20
|
};
|
|
20
21
|
const payload = req.payload;
|
|
22
|
+
// Lock before the lookup: the ownership decision must read rows committed before this write.
|
|
23
|
+
await lockRedirectRuleGraph(req);
|
|
21
24
|
const existing = await findRedirectByFrom(req.payload, {
|
|
22
25
|
collectionSlug,
|
|
23
26
|
from: rule.from,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist-auto-rule.js","sourceRoot":"","sources":["../../../../src/core/redirects/auto/persist-auto-rule.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAC;AACxF,OAAO,EA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OAAO,EAAE,IAAI;iBACd;gBACD,OAAO,EAAE,eAAe;gBACxB,cAAc,EAAE,IAAI;gBACpB,GAAG;aACJ,CAAC,CAAC;YACH,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,OAAO,CAAC,MAAM,CAAC;QACnB,UAAU,EAAE,cAAc;QAC1B,IAAI;QACJ,OAAO,EAAE,eAAe;QACxB,cAAc,EAAE,IAAI;QACpB,GAAG;KACJ,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import type { PayloadRequest } from \"payload\";\nimport { isIdenticalAutoRule } from \"#redirect/core/redirects/auto/match-existing-rule\";\nimport { REDIRECT_CONTEXT_KEYS } from \"#redirect/core/redirects/context-keys\";\nimport { findRedirectByFrom, type RedirectLookupDoc } from \"#redirect/core/redirects/queries\";\n\ntype AutoRedirectRule = {\n from: string;\n to: string;\n};\n\ntype EnsureAutoRedirectArgs = {\n req: PayloadRequest;\n collectionSlug: string;\n rule: AutoRedirectRule;\n context?: Record<string, unknown>;\n};\n\n/**\n * Idempotent create for auto 301 rules. Plugin-owned integrity operations use trusted Local API access.\n */\nexport const ensureAutoRedirect = async ({\n req,\n collectionSlug,\n rule,\n context,\n}: EnsureAutoRedirectArgs): Promise<\"created\" | \"skipped\" | \"updated\"> => {\n const internalContext = {\n ...context,\n [REDIRECT_CONTEXT_KEYS.autoWrite]: true,\n [REDIRECT_CONTEXT_KEYS.skipAuto]: true,\n };\n\n const data = {\n from: rule.from,\n to: rule.to,\n statusCode: \"301\",\n enabled: true,\n origin: \"auto\" as const,\n };\n const payload = req.payload as unknown as {\n create: (args: Record<string, unknown>) => Promise<unknown>;\n update: (args: Record<string, unknown>) => Promise<unknown>;\n };\n\n const existing = await findRedirectByFrom(req.payload, {\n collectionSlug,\n from: rule.from,\n req,\n overrideAccess: true,\n context: internalContext,\n });\n\n const doc = existing.docs[0] as RedirectLookupDoc | undefined;\n if (doc) {\n if (isIdenticalAutoRule(doc, rule)) {\n return \"skipped\";\n }\n if (doc.origin === \"auto\") {\n await payload.update({\n collection: collectionSlug,\n id: doc.id,\n data: {\n to: rule.to,\n statusCode: \"301\",\n enabled: true,\n },\n context: internalContext,\n overrideAccess: true,\n req,\n });\n return \"updated\";\n }\n return \"skipped\";\n }\n\n await payload.create({\n collection: collectionSlug,\n data,\n context: internalContext,\n overrideAccess: true,\n req,\n });\n\n return \"created\";\n};\n"]}
|
|
1
|
+
{"version":3,"file":"persist-auto-rule.js","sourceRoot":"","sources":["../../../../src/core/redirects/auto/persist-auto-rule.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAC;AACxF,OAAO,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AACvF,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAA0B,MAAM,kCAAkC,CAAC;AAc9F;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,EACvC,GAAG,EACH,cAAc,EACd,IAAI,EACJ,OAAO,GACgB,EAA8C,EAAE;IACvE,MAAM,eAAe,GAAG;QACtB,GAAG,OAAO;QACV,CAAC,qBAAqB,CAAC,SAAS,CAAC,EAAE,IAAI;QACvC,CAAC,qBAAqB,CAAC,QAAQ,CAAC,EAAE,IAAI;KACvC,CAAC;IAEF,MAAM,IAAI,GAAG;QACX,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,EAAE,EAAE,IAAI,CAAC,EAAE;QACX,UAAU,EAAE,KAAK;QACjB,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,MAAe;KACxB,CAAC;IACF,MAAM,OAAO,GAAG,GAAG,CAAC,OAGnB,CAAC;IAEF,6FAA6F;IAC7F,MAAM,qBAAqB,CAAC,GAAG,CAAC,CAAC;IACjC,MAAM,QAAQ,GAAG,MAAM,kBAAkB,CAAC,GAAG,CAAC,OAAO,EAAE;QACrD,cAAc;QACd,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,GAAG;QACH,cAAc,EAAE,IAAI;QACpB,OAAO,EAAE,eAAe;KACzB,CAAC,CAAC;IAEH,MAAM,GAAG,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAkC,CAAC;IAC9D,IAAI,GAAG,EAAE,CAAC;QACR,IAAI,mBAAmB,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC;YACnC,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YAC1B,MAAM,OAAO,CAAC,MAAM,CAAC;gBACnB,UAAU,EAAE,cAAc;gBAC1B,EAAE,EAAE,GAAG,CAAC,EAAE;gBACV,IAAI,EAAE;oBACJ,EAAE,EAAE,IAAI,CAAC,EAAE;oBACX,UAAU,EAAE,KAAK;oBACjB,OAAO,EAAE,IAAI;iBACd;gBACD,OAAO,EAAE,eAAe;gBACxB,cAAc,EAAE,IAAI;gBACpB,GAAG;aACJ,CAAC,CAAC;YACH,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,OAAO,CAAC,MAAM,CAAC;QACnB,UAAU,EAAE,cAAc;QAC1B,IAAI;QACJ,OAAO,EAAE,eAAe;QACxB,cAAc,EAAE,IAAI;QACpB,GAAG;KACJ,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import type { PayloadRequest } from \"payload\";\nimport { isIdenticalAutoRule } from \"#redirect/core/redirects/auto/match-existing-rule\";\nimport { lockRedirectRuleGraph } from \"#redirect/core/redirects/collection/graph-lock\";\nimport { REDIRECT_CONTEXT_KEYS } from \"#redirect/core/redirects/context-keys\";\nimport { findRedirectByFrom, type RedirectLookupDoc } from \"#redirect/core/redirects/queries\";\n\ntype AutoRedirectRule = {\n from: string;\n to: string;\n};\n\ntype EnsureAutoRedirectArgs = {\n req: PayloadRequest;\n collectionSlug: string;\n rule: AutoRedirectRule;\n context?: Record<string, unknown>;\n};\n\n/**\n * Idempotent create for auto 301 rules. Plugin-owned integrity operations use trusted Local API access.\n */\nexport const ensureAutoRedirect = async ({\n req,\n collectionSlug,\n rule,\n context,\n}: EnsureAutoRedirectArgs): Promise<\"created\" | \"skipped\" | \"updated\"> => {\n const internalContext = {\n ...context,\n [REDIRECT_CONTEXT_KEYS.autoWrite]: true,\n [REDIRECT_CONTEXT_KEYS.skipAuto]: true,\n };\n\n const data = {\n from: rule.from,\n to: rule.to,\n statusCode: \"301\",\n enabled: true,\n origin: \"auto\" as const,\n };\n const payload = req.payload as unknown as {\n create: (args: Record<string, unknown>) => Promise<unknown>;\n update: (args: Record<string, unknown>) => Promise<unknown>;\n };\n\n // Lock before the lookup: the ownership decision must read rows committed before this write.\n await lockRedirectRuleGraph(req);\n const existing = await findRedirectByFrom(req.payload, {\n collectionSlug,\n from: rule.from,\n req,\n overrideAccess: true,\n context: internalContext,\n });\n\n const doc = existing.docs[0] as RedirectLookupDoc | undefined;\n if (doc) {\n if (isIdenticalAutoRule(doc, rule)) {\n return \"skipped\";\n }\n if (doc.origin === \"auto\") {\n await payload.update({\n collection: collectionSlug,\n id: doc.id,\n data: {\n to: rule.to,\n statusCode: \"301\",\n enabled: true,\n },\n context: internalContext,\n overrideAccess: true,\n req,\n });\n return \"updated\";\n }\n return \"skipped\";\n }\n\n await payload.create({\n collection: collectionSlug,\n data,\n context: internalContext,\n overrideAccess: true,\n req,\n });\n\n return \"created\";\n};\n"]}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import type { CollectionAfterChangeHook } from "payload";
|
|
2
2
|
import type { PublicationTransition } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* A locale takes part only when its newest published version was saved by this operation. That version
|
|
5
|
+
* carries the operation's `updatedAt`; unchanged locales keep an older one, so republishing them creates
|
|
6
|
+
* nothing and never relinks an earlier change.
|
|
7
|
+
*/
|
|
3
8
|
export declare const getPublicationTransition: (args: Parameters<CollectionAfterChangeHook>[0]) => Promise<PublicationTransition[]>;
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { REDIRECT_CONTEXT_KEYS } from "../../context-keys.js";
|
|
2
|
-
import {
|
|
2
|
+
import { getConfiguredLocales, hasLocalizedDraftStatus, resolveDefaultLocale } from "./locales.js";
|
|
3
3
|
import { isDraftSave, isLocalePublished } from "./publish-intent.js";
|
|
4
4
|
import { fetchLocalePublishedDoc } from "./versions/document-locale.js";
|
|
5
5
|
import { fetchPublishedVersionPages } from "./versions/version-pages.js";
|
|
6
|
+
import { isThisPublication } from "./versions/version-rows.js";
|
|
7
|
+
/**
|
|
8
|
+
* A locale takes part only when its newest published version was saved by this operation. That version
|
|
9
|
+
* carries the operation's `updatedAt`; unchanged locales keep an older one, so republishing them creates
|
|
10
|
+
* nothing and never relinks an earlier change.
|
|
11
|
+
*/
|
|
6
12
|
export const getPublicationTransition = async (args) => {
|
|
7
13
|
const { req, doc, collection } = args;
|
|
8
14
|
if (!collection.versions?.drafts)
|
|
@@ -10,24 +16,30 @@ export const getPublicationTransition = async (args) => {
|
|
|
10
16
|
if (isDraftSave(req) || req.context?.[REDIRECT_CONTEXT_KEYS.skipAuto])
|
|
11
17
|
return [];
|
|
12
18
|
const docId = doc.id;
|
|
13
|
-
const locales = getRequestLocales(req, doc._status);
|
|
14
19
|
const localizeStatus = hasLocalizedDraftStatus(collection);
|
|
15
20
|
const defaultLocale = resolveDefaultLocale(req);
|
|
16
21
|
const transitions = [];
|
|
17
|
-
for (const locale of
|
|
22
|
+
for (const locale of getConfiguredLocales(req)) {
|
|
18
23
|
const currentDoc = await fetchLocalePublishedDoc({ req, collection, docId, locale });
|
|
19
24
|
if (!currentDoc || !isLocalePublished(currentDoc._status, locale ?? ""))
|
|
20
25
|
continue;
|
|
21
|
-
const
|
|
26
|
+
const rows = await fetchPublishedVersionPages({
|
|
22
27
|
req,
|
|
23
28
|
collectionSlug: collection.slug,
|
|
24
29
|
docId,
|
|
25
30
|
locale,
|
|
26
31
|
versionFilter: { localizeStatus, defaultLocale },
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
});
|
|
33
|
+
// currentDoc is re-read from the database, so hook changes and Local API selects cannot hide updatedAt.
|
|
34
|
+
if (!isThisPublication(rows[0], currentDoc.updatedAt))
|
|
35
|
+
continue;
|
|
36
|
+
transitions.push({
|
|
37
|
+
locale,
|
|
38
|
+
currentDoc,
|
|
39
|
+
publishedDocs: rows
|
|
40
|
+
.map((row) => row.version)
|
|
41
|
+
.filter((version) => Boolean(version)),
|
|
42
|
+
});
|
|
31
43
|
}
|
|
32
44
|
return transitions;
|
|
33
45
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-publication-transition.js","sourceRoot":"","sources":["../../../../../src/core/redirects/auto/publication/get-publication-transition.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAE9E,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"get-publication-transition.js","sourceRoot":"","sources":["../../../../../src/core/redirects/auto/publication/get-publication-transition.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAE9E,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACnG,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,iBAAiB,EAAmB,MAAM,4BAA4B,CAAC;AAEhF;;;;GAIG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,EAC3C,IAA8C,EACZ,EAAE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,OAAO,EAAsC,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC7E,CAAC,CAAC;IACL,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC","sourcesContent":["import type { CollectionAfterChangeHook } from \"payload\";\nimport { REDIRECT_CONTEXT_KEYS } from \"#redirect/core/redirects/context-keys\";\nimport type { PublicationTransition } from \"../types.js\";\nimport { getConfiguredLocales, hasLocalizedDraftStatus, resolveDefaultLocale } from \"./locales.js\";\nimport { isDraftSave, isLocalePublished } from \"./publish-intent.js\";\nimport { fetchLocalePublishedDoc } from \"./versions/document-locale.js\";\nimport { fetchPublishedVersionPages } from \"./versions/version-pages.js\";\nimport { isThisPublication, type VersionRow } from \"./versions/version-rows.js\";\n\n/**\n * A locale takes part only when its newest published version was saved by this operation. That version\n * carries the operation's `updatedAt`; unchanged locales keep an older one, so republishing them creates\n * nothing and never relinks an earlier change.\n */\nexport const getPublicationTransition = async (\n args: Parameters<CollectionAfterChangeHook>[0],\n): Promise<PublicationTransition[]> => {\n const { req, doc, collection } = args;\n if (!collection.versions?.drafts) return [];\n if (isDraftSave(req) || req.context?.[REDIRECT_CONTEXT_KEYS.skipAuto]) return [];\n\n const docId = doc.id;\n const localizeStatus = hasLocalizedDraftStatus(collection);\n const defaultLocale = resolveDefaultLocale(req);\n const transitions: PublicationTransition[] = [];\n\n for (const locale of getConfiguredLocales(req)) {\n const currentDoc = await fetchLocalePublishedDoc({ req, collection, docId, locale });\n if (!currentDoc || !isLocalePublished(currentDoc._status, locale ?? \"\")) continue;\n\n const rows = await fetchPublishedVersionPages({\n req,\n collectionSlug: collection.slug,\n docId,\n locale,\n versionFilter: { localizeStatus, defaultLocale },\n });\n // currentDoc is re-read from the database, so hook changes and Local API selects cannot hide updatedAt.\n if (!isThisPublication(rows[0], currentDoc.updatedAt)) continue;\n\n transitions.push({\n locale,\n currentDoc,\n publishedDocs: rows\n .map((row) => row.version)\n .filter((version): version is Record<string, unknown> => Boolean(version)),\n });\n }\n\n return transitions;\n};\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { REDIRECT_CONTEXT_KEYS } from "../../context-keys.js";
|
|
2
2
|
import { getConfiguredLocales, hasLocalizedDraftStatus, resolveDefaultLocale } from "./locales.js";
|
|
3
3
|
import { isDraftSave, isLocalePublished } from "./publish-intent.js";
|
|
4
|
-
import { isPublishedVersionRow } from "./versions/version-rows.js";
|
|
4
|
+
import { isPublishedVersionRow, isThisPublication, } from "./versions/version-rows.js";
|
|
5
5
|
const VERSION_PAGE_SIZE = 50;
|
|
6
6
|
export const getGlobalPublicationTransition = async ({ req, globalSlug, }) => {
|
|
7
7
|
if (isDraftSave(req) ||
|
|
@@ -30,7 +30,7 @@ export const getGlobalPublicationTransition = async ({ req, globalSlug, }) => {
|
|
|
30
30
|
continue;
|
|
31
31
|
if (!isLocalePublished(currentDoc._status, locale ?? ""))
|
|
32
32
|
continue;
|
|
33
|
-
const
|
|
33
|
+
const rows = [];
|
|
34
34
|
let page = 1;
|
|
35
35
|
while (true) {
|
|
36
36
|
const result = await req.payload.findGlobalVersions({
|
|
@@ -44,18 +44,20 @@ export const getGlobalPublicationTransition = async ({ req, globalSlug, }) => {
|
|
|
44
44
|
overrideAccess: true,
|
|
45
45
|
req,
|
|
46
46
|
});
|
|
47
|
-
|
|
48
|
-
.filter((row) => isPublishedVersionRow(row, locale, { localizeStatus, defaultLocale }))
|
|
49
|
-
.map((row) => row.version)
|
|
50
|
-
.filter((version) => Boolean(version)));
|
|
47
|
+
rows.push(...result.docs.filter((row) => isPublishedVersionRow(row, locale, { localizeStatus, defaultLocale })));
|
|
51
48
|
if (!result.hasNextPage)
|
|
52
49
|
break;
|
|
53
50
|
page += 1;
|
|
54
51
|
}
|
|
52
|
+
// Only a locale whose newest published version is this operation's takes part; see getPublicationTransition.
|
|
53
|
+
if (!isThisPublication(rows[0], currentDoc.updatedAt))
|
|
54
|
+
continue;
|
|
55
55
|
transitions.push({
|
|
56
56
|
locale,
|
|
57
57
|
currentDoc: currentDoc,
|
|
58
|
-
publishedDocs
|
|
58
|
+
publishedDocs: rows
|
|
59
|
+
.map((row) => row.version)
|
|
60
|
+
.filter((version) => Boolean(version)),
|
|
59
61
|
});
|
|
60
62
|
}
|
|
61
63
|
return transitions;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-transition.js","sourceRoot":"","sources":["../../../../../src/core/redirects/auto/publication/global-transition.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAE9E,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACnG,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,
|
|
1
|
+
{"version":3,"file":"global-transition.js","sourceRoot":"","sources":["../../../../../src/core/redirects/auto/publication/global-transition.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAE9E,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACnG,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,EACL,qBAAqB,EACrB,iBAAiB,GAElB,MAAM,4BAA4B,CAAC;AAEpC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,MAAM,CAAC,MAAM,8BAA8B,GAAG,KAAK,EAAE,EACnD,GAAG,EACH,UAAU,GAIX,EAAoC,EAAE;IACrC,IACE,WAAW,CAAC,GAAG,CAAC;QAChB,GAAG,CAAC,KAAK,EAAE,QAAQ,KAAK,MAAM;QAC9B,GAAG,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI;QAC5B,GAAG,CAAC,OAAO,EAAE,CAAC,qBAAqB,CAAC,QAAQ,CAAC,EAC7C,CAAC;QACD,OAAO,EAAE,C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AAqB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpD,IAAI,EAAE,YAAY;gBAClB,IAAI;gBACJ,KAAK,EAAE,iBAAiB;gBACxB,KAAK,EAAE,CAAC;gBACR,cAAc,EAAE,KAAK;gBACrB,cAAc,EAAE,IAAI;gBACpB,GAAG;aACJ,CAAC,CAAC;YAEH,IAAI,CAAC,IAAI,CACP,GAAI,MAAM,CAAC,IAAqB,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAC9C,qBAAqB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,cAAc,EAAE,aAAa,EAAE,CAAC,CACtE,CACF,CAAC;YACF,IAAI,CAAC,MAAM,CAAC,WAAW;gBAAE,MAAM;YAC/B,IAAI,IAAI,CAAC,CAAC;QACZ,CAAC;QACD,6GAA6G;QAC7G,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,SAAS,CAAC;YAAE,SAAS;QAEhE,WAAW,CAAC,IAAI,CAAC;YACf,MAAM;YACN,UAAU,EAAE,UAAqC;YACjD,aAAa,EAAE,IAAI;iBAChB,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC;iBACzB,MAAM,CAAC,CAAC,OAAO,EAAsC,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC7E,CAAC,CAAC;IACL,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC","sourcesContent":["import type { GlobalSlug, PayloadRequest, TypedLocale } from \"payload\";\nimport { REDIRECT_CONTEXT_KEYS } from \"#redirect/core/redirects/context-keys\";\nimport type { PublicationTransition } from \"../types.js\";\nimport { getConfiguredLocales, hasLocalizedDraftStatus, resolveDefaultLocale } from \"./locales.js\";\nimport { isDraftSave, isLocalePublished } from \"./publish-intent.js\";\nimport {\n isPublishedVersionRow,\n isThisPublication,\n type VersionRow,\n} from \"./versions/version-rows.js\";\n\nconst VERSION_PAGE_SIZE = 50;\n\nexport const getGlobalPublicationTransition = async ({\n req,\n globalSlug,\n}: {\n req: PayloadRequest;\n globalSlug: string;\n}): Promise<PublicationTransition[]> => {\n if (\n isDraftSave(req) ||\n req.query?.autosave === \"true\" ||\n req.query?.autosave === true ||\n req.context?.[REDIRECT_CONTEXT_KEYS.skipAuto]\n ) {\n return [];\n }\n\n const global = req.payload.globals.config.find(({ slug }) => slug === globalSlug);\n if (!global?.versions?.drafts) return [];\n\n const slug = globalSlug as GlobalSlug;\n const localizeStatus = hasLocalizedDraftStatus(global);\n const defaultLocale = resolveDefaultLocale(req);\n const transitions: PublicationTransition[] = [];\n\n for (const locale of getConfiguredLocales(req)) {\n const currentDoc = await req.payload.findGlobal({\n slug,\n ...(locale ? { locale: locale as TypedLocale } : {}),\n fallbackLocale: false,\n depth: 0,\n overrideAccess: true,\n req,\n });\n if (!currentDoc || Object.keys(currentDoc).length === 0) continue;\n if (!isLocalePublished(currentDoc._status, locale ?? \"\")) continue;\n\n const rows: VersionRow[] = [];\n let page = 1;\n while (true) {\n const result = await req.payload.findGlobalVersions({\n slug,\n ...(locale ? { locale: locale as TypedLocale } : {}),\n sort: \"-updatedAt\",\n page,\n limit: VERSION_PAGE_SIZE,\n depth: 0,\n fallbackLocale: false,\n overrideAccess: true,\n req,\n });\n\n rows.push(\n ...(result.docs as VersionRow[]).filter((row) =>\n isPublishedVersionRow(row, locale, { localizeStatus, defaultLocale }),\n ),\n );\n if (!result.hasNextPage) break;\n page += 1;\n }\n // Only a locale whose newest published version is this operation's takes part; see getPublicationTransition.\n if (!isThisPublication(rows[0], currentDoc.updatedAt)) continue;\n\n transitions.push({\n locale,\n currentDoc: currentDoc as Record<string, unknown>,\n publishedDocs: rows\n .map((row) => row.version)\n .filter((version): version is Record<string, unknown> => Boolean(version)),\n });\n }\n\n return transitions;\n};\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { PayloadRequest } from "payload";
|
|
2
|
-
export declare const getRequestLocales: (req: PayloadRequest, status: unknown) => Array<string | undefined>;
|
|
3
2
|
export declare const getConfiguredLocales: (req: PayloadRequest) => Array<string | undefined>;
|
|
4
3
|
export declare const hasLocalizedDraftStatus: (collection: {
|
|
5
4
|
versions?: {
|
|
@@ -4,34 +4,6 @@ const listLocales = (req) => {
|
|
|
4
4
|
return [];
|
|
5
5
|
return localization.locales.map((locale) => (typeof locale === "string" ? locale : locale.code));
|
|
6
6
|
};
|
|
7
|
-
export const getRequestLocales = (req, status) => {
|
|
8
|
-
const localization = listLocales(req);
|
|
9
|
-
if (localization.length === 0)
|
|
10
|
-
return [undefined];
|
|
11
|
-
const specificLocale = typeof req.query?.publishSpecificLocale === "string"
|
|
12
|
-
? req.query.publishSpecificLocale
|
|
13
|
-
: undefined;
|
|
14
|
-
if (specificLocale && specificLocale !== "all")
|
|
15
|
-
return [specificLocale];
|
|
16
|
-
if (specificLocale === "all" ||
|
|
17
|
-
req.query?.publishAllLocales === "true" ||
|
|
18
|
-
req.query?.publishAllLocales === true) {
|
|
19
|
-
return localization;
|
|
20
|
-
}
|
|
21
|
-
const requestedLocale = typeof req.query?.locale === "string" ? req.query.locale : undefined;
|
|
22
|
-
if (requestedLocale && requestedLocale !== "all")
|
|
23
|
-
return [requestedLocale];
|
|
24
|
-
if (requestedLocale === "all")
|
|
25
|
-
return localization;
|
|
26
|
-
if (status && typeof status === "object" && !Array.isArray(status)) {
|
|
27
|
-
return Object.entries(status)
|
|
28
|
-
.filter(([, value]) => value === "published")
|
|
29
|
-
.map(([locale]) => locale);
|
|
30
|
-
}
|
|
31
|
-
if (typeof req.locale === "string")
|
|
32
|
-
return [req.locale];
|
|
33
|
-
return status === "published" ? localization : [];
|
|
34
|
-
};
|
|
35
7
|
export const getConfiguredLocales = (req) => {
|
|
36
8
|
const locales = listLocales(req);
|
|
37
9
|
return locales.length > 0 ? locales : [undefined];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"locales.js","sourceRoot":"","sources":["../../../../../src/core/redirects/auto/publication/locales.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,GAAG,CAAC,GAAmB,EAAY,EAAE;IACpD,MAAM,YAAY,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC;IACrD,IAAI,YAAY,KAAK,KAAK,IAAI,CAAC,YAAY;QAAE,OAAO,EAAE,CAAC;IACvD,OAAO,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACnG,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"locales.js","sourceRoot":"","sources":["../../../../../src/core/redirects/auto/publication/locales.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,GAAG,CAAC,GAAmB,EAAY,EAAE;IACpD,MAAM,YAAY,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC;IACrD,IAAI,YAAY,KAAK,KAAK,IAAI,CAAC,YAAY;QAAE,OAAO,EAAE,CAAC;IACvD,OAAO,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACnG,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,GAAmB,EAA6B,EAAE;IACrF,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;IACjC,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AACpD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,UAEvC,EAAW,EAAE;IACZ,MAAM,MAAM,GAAG,UAAU,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC;IACvF,OAAO,OAAO,CACZ,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,gBAAgB,IAAI,MAAM,IAAI,MAAM,CAAC,cAAc,CAC5F,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,GAAmB,EAAsB,EAAE;IAC9E,MAAM,YAAY,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC;IACrD,OAAO,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/D,CAAC,CAAC","sourcesContent":["import type { PayloadRequest } from \"payload\";\n\nconst listLocales = (req: PayloadRequest): string[] => {\n const localization = req.payload.config.localization;\n if (localization === false || !localization) return [];\n return localization.locales.map((locale) => (typeof locale === \"string\" ? locale : locale.code));\n};\n\nexport const getConfiguredLocales = (req: PayloadRequest): Array<string | undefined> => {\n const locales = listLocales(req);\n return locales.length > 0 ? locales : [undefined];\n};\n\nexport const hasLocalizedDraftStatus = (collection: {\n versions?: { drafts?: unknown } | false;\n}): boolean => {\n const drafts = collection.versions === false ? undefined : collection.versions?.drafts;\n return Boolean(\n drafts && typeof drafts === \"object\" && \"localizeStatus\" in drafts && drafts.localizeStatus,\n );\n};\n\nexport const resolveDefaultLocale = (req: PayloadRequest): string | undefined => {\n const localization = req.payload.config.localization;\n return localization ? localization.defaultLocale : undefined;\n};\n"]}
|
|
@@ -11,3 +11,5 @@ export declare const isPublishedVersionRow: (row: VersionRow, locale: string | u
|
|
|
11
11
|
localizeStatus?: boolean;
|
|
12
12
|
defaultLocale?: string;
|
|
13
13
|
}) => boolean;
|
|
14
|
+
/** True when `row` is the publication saved by the operation that produced `updatedAt`. */
|
|
15
|
+
export declare const isThisPublication: (row: VersionRow | undefined, updatedAt: unknown) => boolean;
|
|
@@ -15,4 +15,6 @@ export const isPublishedVersionRow = (row, locale, options) => {
|
|
|
15
15
|
}
|
|
16
16
|
return false;
|
|
17
17
|
};
|
|
18
|
+
/** True when `row` is the publication saved by the operation that produced `updatedAt`. */
|
|
19
|
+
export const isThisPublication = (row, updatedAt) => row !== undefined && typeof updatedAt === "string" && row.version?.updatedAt === updatedAt;
|
|
18
20
|
//# sourceMappingURL=version-rows.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version-rows.js","sourceRoot":"","sources":["../../../../../../src/core/redirects/auto/publication/versions/version-rows.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,GAAe,EACf,MAA0B,EAC1B,OAA8D,EACrD,EAAE;IACX,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IAE/C,MAAM,eAAe,GAAG,GAAG,CAAC,eAAe,IAAI,GAAG,CAAC,gBAAgB,CAAC;IACpE,IAAI,eAAe;QAAE,OAAO,eAAe,KAAK,MAAM,CAAC;IAEvD,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC;IACpC,IAAI,MAAM,KAAK,OAAO;QAAE,OAAO,KAAK,CAAC;IACrC,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACnE,OAAO,MAAM,KAAK,SAAS,IAAK,MAAkC,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC;IAC7F,CAAC;IACD,IAAI,MAAM,KAAK,WAAW,EAAE,CAAC;QAC3B,OAAO,CAAC,OAAO,EAAE,cAAc,IAAI,MAAM,KAAK,SAAS,CAAC;IAC1D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC","sourcesContent":["export type VersionRow = {\n id?: string | number;\n updatedAt?: string;\n snapshot?: boolean;\n autosave?: boolean;\n publishedLocale?: string;\n published_locale?: string;\n version?: Record<string, unknown>;\n};\n\nexport const isPublishedVersionRow = (\n row: VersionRow,\n locale: string | undefined,\n options?: { localizeStatus?: boolean; defaultLocale?: string },\n): boolean => {\n if (row.snapshot || row.autosave) return false;\n\n const publishedLocale = row.publishedLocale ?? row.published_locale;\n if (publishedLocale) return publishedLocale === locale;\n\n const status = row.version?._status;\n if (status === \"draft\") return false;\n if (status && typeof status === \"object\" && !Array.isArray(status)) {\n return locale !== undefined && (status as Record<string, unknown>)[locale] === \"published\";\n }\n if (status === \"published\") {\n return !options?.localizeStatus || locale !== undefined;\n }\n return false;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"version-rows.js","sourceRoot":"","sources":["../../../../../../src/core/redirects/auto/publication/versions/version-rows.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,GAAe,EACf,MAA0B,EAC1B,OAA8D,EACrD,EAAE;IACX,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IAE/C,MAAM,eAAe,GAAG,GAAG,CAAC,eAAe,IAAI,GAAG,CAAC,gBAAgB,CAAC;IACpE,IAAI,eAAe;QAAE,OAAO,eAAe,KAAK,MAAM,CAAC;IAEvD,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC;IACpC,IAAI,MAAM,KAAK,OAAO;QAAE,OAAO,KAAK,CAAC;IACrC,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACnE,OAAO,MAAM,KAAK,SAAS,IAAK,MAAkC,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC;IAC7F,CAAC;IACD,IAAI,MAAM,KAAK,WAAW,EAAE,CAAC;QAC3B,OAAO,CAAC,OAAO,EAAE,cAAc,IAAI,MAAM,KAAK,SAAS,CAAC;IAC1D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,2FAA2F;AAC3F,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAA2B,EAAE,SAAkB,EAAW,EAAE,CAC5F,GAAG,KAAK,SAAS,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,GAAG,CAAC,OAAO,EAAE,SAAS,KAAK,SAAS,CAAC","sourcesContent":["export type VersionRow = {\n id?: string | number;\n updatedAt?: string;\n snapshot?: boolean;\n autosave?: boolean;\n publishedLocale?: string;\n published_locale?: string;\n version?: Record<string, unknown>;\n};\n\nexport const isPublishedVersionRow = (\n row: VersionRow,\n locale: string | undefined,\n options?: { localizeStatus?: boolean; defaultLocale?: string },\n): boolean => {\n if (row.snapshot || row.autosave) return false;\n\n const publishedLocale = row.publishedLocale ?? row.published_locale;\n if (publishedLocale) return publishedLocale === locale;\n\n const status = row.version?._status;\n if (status === \"draft\") return false;\n if (status && typeof status === \"object\" && !Array.isArray(status)) {\n return locale !== undefined && (status as Record<string, unknown>)[locale] === \"published\";\n }\n if (status === \"published\") {\n return !options?.localizeStatus || locale !== undefined;\n }\n return false;\n};\n\n/** True when `row` is the publication saved by the operation that produced `updatedAt`. */\nexport const isThisPublication = (row: VersionRow | undefined, updatedAt: unknown): boolean =>\n row !== undefined && typeof updatedAt === \"string\" && row.version?.updatedAt === updatedAt;\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createDuplicateBeforeChange } from "./duplicate-guard.js";
|
|
2
|
+
import { createRedirectGraphLockBeforeOperation } from "./graph-lock.js";
|
|
2
3
|
import { assertRedirectFields, buildRedirectFields, mergeRedirectFields } from "./fields.js";
|
|
3
4
|
import { createRedirectOriginBeforeChange } from "./origin-hook.js";
|
|
4
5
|
const DEFAULT_SLUG = "redirects";
|
|
@@ -26,6 +27,10 @@ export const createRedirectsCollection = (overrides = {}) => {
|
|
|
26
27
|
fields,
|
|
27
28
|
hooks: {
|
|
28
29
|
...existingHooks,
|
|
30
|
+
beforeOperation: [
|
|
31
|
+
...(existingHooks.beforeOperation ?? []),
|
|
32
|
+
createRedirectGraphLockBeforeOperation(),
|
|
33
|
+
],
|
|
29
34
|
beforeChange: [
|
|
30
35
|
...(existingHooks.beforeChange ?? []),
|
|
31
36
|
createRedirectOriginBeforeChange(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection.js","sourceRoot":"","sources":["../../../../src/core/redirects/collection/collection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAC7F,OAAO,EAAE,gCAAgC,EAAE,MAAM,kBAAkB,CAAC;AAGpE,MAAM,YAAY,GAAG,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,SAAS,GAAgC,EAAE,EACzB,EAAE;IACpB,MAAM,aAAa,GAAG,mBAAmB,EAAE,CAAC;IAC5C,MAAM,MAAM,GAAY,SAAS,CAAC,MAAM;QACtC,CAAC,CAAC,mBAAmB,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;QAC1D,CAAC,CAAC,aAAa,CAAC;IAElB,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAE7B,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,IAAI,YAAY,CAAC;IAC5C,MAAM,aAAa,GAAG,SAAS,CAAC,KAAK,IAAI,EAAE,CAAC;IAE5C,OAAO;QACL,GAAG,SAAS;QACZ,IAAI;QACJ,KAAK,EAAE,SAAS,CAAC,KAAK,IAAI,IAAI;QAC9B,QAAQ,EAAE,SAAS,CAAC,QAAQ,IAAI,KAAK;QACrC,UAAU,EAAE,SAAS,CAAC,UAAU,IAAI,IAAI;QACxC,KAAK,EAAE;YACL,UAAU,EAAE,MAAM;YAClB,cAAc,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,CAAC;YAC9E,oBAAoB,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC;YACpC,GAAG,SAAS,CAAC,KAAK;SACnB;QACD,WAAW,EAAE,SAAS,CAAC,WAAW,IAAI,MAAM;QAC5C,MAAM;QACN,KAAK,EAAE;YACL,GAAG,aAAa;YAChB,YAAY,EAAE;gBACZ,GAAG,CAAC,aAAa,CAAC,YAAY,IAAI,EAAE,CAAC;gBACrC,gCAAgC,EAAE;gBAClC,2BAA2B,CAAC,IAAI,CAAC;aAClC;SACF;KACF,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { CollectionConfig, Field } from \"payload\";\nimport { createDuplicateBeforeChange } from \"./duplicate-guard.js\";\nimport { assertRedirectFields, buildRedirectFields, mergeRedirectFields } from \"./fields.js\";\nimport { createRedirectOriginBeforeChange } from \"./origin-hook.js\";\nimport type { RedirectCollectionOverrides } from \"./types.js\";\n\nconst DEFAULT_SLUG = \"redirects\";\n\nexport const createRedirectsCollection = (\n overrides: RedirectCollectionOverrides = {},\n): CollectionConfig => {\n const defaultFields = buildRedirectFields();\n const fields: Field[] = overrides.fields\n ? mergeRedirectFields(overrides.fields({ defaultFields }))\n : defaultFields;\n\n assertRedirectFields(fields);\n\n const slug = overrides.slug ?? DEFAULT_SLUG;\n const existingHooks = overrides.hooks ?? {};\n\n return {\n ...overrides,\n slug,\n trash: overrides.trash ?? true,\n versions: overrides.versions ?? false,\n timestamps: overrides.timestamps ?? true,\n admin: {\n useAsTitle: \"from\",\n defaultColumns: [\"from\", \"to\", \"statusCode\", \"origin\", \"enabled\", \"updatedAt\"],\n listSearchableFields: [\"from\", \"to\"],\n ...overrides.admin,\n },\n defaultSort: overrides.defaultSort ?? \"from\",\n fields,\n hooks: {\n ...existingHooks,\n beforeChange: [\n ...(existingHooks.beforeChange ?? []),\n createRedirectOriginBeforeChange(),\n createDuplicateBeforeChange(slug),\n ],\n },\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"collection.js","sourceRoot":"","sources":["../../../../src/core/redirects/collection/collection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,sCAAsC,EAAE,MAAM,iBAAiB,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAC7F,OAAO,EAAE,gCAAgC,EAAE,MAAM,kBAAkB,CAAC;AAGpE,MAAM,YAAY,GAAG,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,SAAS,GAAgC,EAAE,EACzB,EAAE;IACpB,MAAM,aAAa,GAAG,mBAAmB,EAAE,CAAC;IAC5C,MAAM,MAAM,GAAY,SAAS,CAAC,MAAM;QACtC,CAAC,CAAC,mBAAmB,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;QAC1D,CAAC,CAAC,aAAa,CAAC;IAElB,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAE7B,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,IAAI,YAAY,CAAC;IAC5C,MAAM,aAAa,GAAG,SAAS,CAAC,KAAK,IAAI,EAAE,CAAC;IAE5C,OAAO;QACL,GAAG,SAAS;QACZ,IAAI;QACJ,KAAK,EAAE,SAAS,CAAC,KAAK,IAAI,IAAI;QAC9B,QAAQ,EAAE,SAAS,CAAC,QAAQ,IAAI,KAAK;QACrC,UAAU,EAAE,SAAS,CAAC,UAAU,IAAI,IAAI;QACxC,KAAK,EAAE;YACL,UAAU,EAAE,MAAM;YAClB,cAAc,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,CAAC;YAC9E,oBAAoB,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC;YACpC,GAAG,SAAS,CAAC,KAAK;SACnB;QACD,WAAW,EAAE,SAAS,CAAC,WAAW,IAAI,MAAM;QAC5C,MAAM;QACN,KAAK,EAAE;YACL,GAAG,aAAa;YAChB,eAAe,EAAE;gBACf,GAAG,CAAC,aAAa,CAAC,eAAe,IAAI,EAAE,CAAC;gBACxC,sCAAsC,EAAE;aACzC;YACD,YAAY,EAAE;gBACZ,GAAG,CAAC,aAAa,CAAC,YAAY,IAAI,EAAE,CAAC;gBACrC,gCAAgC,EAAE;gBAClC,2BAA2B,CAAC,IAAI,CAAC;aAClC;SACF;KACF,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { CollectionConfig, Field } from \"payload\";\nimport { createDuplicateBeforeChange } from \"./duplicate-guard.js\";\nimport { createRedirectGraphLockBeforeOperation } from \"./graph-lock.js\";\nimport { assertRedirectFields, buildRedirectFields, mergeRedirectFields } from \"./fields.js\";\nimport { createRedirectOriginBeforeChange } from \"./origin-hook.js\";\nimport type { RedirectCollectionOverrides } from \"./types.js\";\n\nconst DEFAULT_SLUG = \"redirects\";\n\nexport const createRedirectsCollection = (\n overrides: RedirectCollectionOverrides = {},\n): CollectionConfig => {\n const defaultFields = buildRedirectFields();\n const fields: Field[] = overrides.fields\n ? mergeRedirectFields(overrides.fields({ defaultFields }))\n : defaultFields;\n\n assertRedirectFields(fields);\n\n const slug = overrides.slug ?? DEFAULT_SLUG;\n const existingHooks = overrides.hooks ?? {};\n\n return {\n ...overrides,\n slug,\n trash: overrides.trash ?? true,\n versions: overrides.versions ?? false,\n timestamps: overrides.timestamps ?? true,\n admin: {\n useAsTitle: \"from\",\n defaultColumns: [\"from\", \"to\", \"statusCode\", \"origin\", \"enabled\", \"updatedAt\"],\n listSearchableFields: [\"from\", \"to\"],\n ...overrides.admin,\n },\n defaultSort: overrides.defaultSort ?? \"from\",\n fields,\n hooks: {\n ...existingHooks,\n beforeOperation: [\n ...(existingHooks.beforeOperation ?? []),\n createRedirectGraphLockBeforeOperation(),\n ],\n beforeChange: [\n ...(existingHooks.beforeChange ?? []),\n createRedirectOriginBeforeChange(),\n createDuplicateBeforeChange(slug),\n ],\n },\n };\n};\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ValidationError } from "payload";
|
|
2
|
-
import { lockRedirectRuleGraph } from "./graph-lock.js";
|
|
3
2
|
import { assertRedirectStatusCode, DEFAULT_REDIRECT_STATUS_CODE, validateDestinationForStatus, } from "../model/status-code.js";
|
|
4
3
|
import { normalizeRedirectSourcePath, normalizeRelativeRedirectPath, } from "../normalize-path.js";
|
|
5
4
|
import { findRedirectByFrom } from "../queries.js";
|
|
@@ -162,9 +161,6 @@ export const effectiveRuleData = (operation, data, originalDoc) => {
|
|
|
162
161
|
export const createDuplicateBeforeChange = (collectionSlug = "redirects") => {
|
|
163
162
|
return async (args) => {
|
|
164
163
|
const { data, req, operation, originalDoc } = args;
|
|
165
|
-
if (operation === "create" || operation === "update") {
|
|
166
|
-
await lockRedirectRuleGraph(req);
|
|
167
|
-
}
|
|
168
164
|
const effective = effectiveRuleData(operation, data, originalDoc);
|
|
169
165
|
if (effective && (operation === "create" || operation === "update")) {
|
|
170
166
|
normalizeRulePaths(effective);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duplicate-guard.js","sourceRoot":"","sources":["../../../../src/core/redirects/collection/duplicate-guard.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AACvF,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,4BAA4B,GAC7B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACL,2BAA2B,EAC3B,6BAA6B,GAC9B,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCA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AAS;YACT,WAAW,EAAE,IAAI,CAAC,WAAW;SAC9B,EACD,cAAc,CACf,CAAC;QAEF,IAAI,IAAI,IAAI,cAAc,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC5D,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC7B,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { CollectionBeforeChangeHook, PayloadRequest } from \"payload\";\nimport { ValidationError } from \"payload\";\nimport { lockRedirectRuleGraph } from \"#redirect/core/redirects/collection/graph-lock\";\nimport {\n assertRedirectStatusCode,\n DEFAULT_REDIRECT_STATUS_CODE,\n validateDestinationForStatus,\n} from \"#redirect/core/redirects/model/status-code\";\nimport {\n normalizeRedirectSourcePath,\n normalizeRelativeRedirectPath,\n} from \"#redirect/core/redirects/normalize-path\";\nimport { findRedirectByFrom } from \"#redirect/core/redirects/queries\";\n\n/** Enabled rules followed from `to` before a write is refused as unverifiable. Bounds the lookups per write. */\nexport const MAX_REDIRECT_CHAIN_HOPS = 1000;\n\nconst DUPLICATE_MESSAGE = \"A redirect rule already exists for this source path\";\nconst LOOP_MESSAGE =\n \"Enabled redirect rules already lead from this destination back to this source path\";\nconst CHAIN_LOOP_MESSAGE = \"Enabled redirect rules from this destination already contain a loop\";\nconst CHAIN_TOO_LONG_MESSAGE = `Enabled redirect rules from this destination chain longer than ${MAX_REDIRECT_CHAIN_HOPS} hops`;\n\nconst normalizeOrUndefined = (raw: unknown): string | undefined => {\n if (typeof raw !== \"string\" || raw.trim().length === 0) return undefined;\n try {\n return normalizeRelativeRedirectPath(raw).split(\"#\", 1)[0];\n } catch {\n return undefined;\n }\n};\n\nexport const resolveNormalizedFrom = (data: Record<string, unknown>): string | undefined => {\n if (typeof data.from !== \"string\" || data.from.trim().length === 0) return undefined;\n try {\n return normalizeRedirectSourcePath(data.from);\n } catch {\n return undefined;\n }\n};\n\nexport const findDuplicateFrom = async ({\n req,\n collectionSlug,\n normalizedFrom,\n excludeId,\n}: {\n req: PayloadRequest;\n collectionSlug: string;\n normalizedFrom: string;\n excludeId?: string | number;\n}): Promise<boolean> => {\n // Integrity lookup: it must see every rule, whatever the requester can read. It returns only a boolean.\n const result = await findRedirectByFrom(req.payload, {\n collectionSlug,\n from: normalizedFrom,\n excludeId,\n req,\n overrideAccess: true,\n select: { id: true },\n });\n\n return result.docs.length > 0;\n};\n\ntype HookOperationArgs = {\n data: Record<string, unknown> | undefined;\n req: PayloadRequest;\n operation: string;\n originalDoc?: { id?: string | number } | null;\n};\n\n/** Payload collection `beforeChange` passes `originalDoc`, not top-level `id` (see Payload 3 update op). */\nexport const resolveDuplicateGuardExcludeId = (\n operation: string,\n originalDoc?: { id?: string | number } | null,\n): string | number | undefined => {\n if (operation !== \"update\" || !originalDoc) return undefined;\n const { id } = originalDoc;\n if (typeof id === \"string\" || typeof id === \"number\") return id;\n return undefined;\n};\n\nconst resolveExcludeId = (args: HookOperationArgs): string | number | undefined =>\n resolveDuplicateGuardExcludeId(args.operation, args.originalDoc);\n\n/**\n * Rejects a write whose enabled chain from `to` returns to `from`, of any length. A chain that\n * repeats a hop without reaching `from` predates this write, so it is accepted. Runs past\n * MAX_REDIRECT_CHAIN_HOPS are refused as unverifiable. One indexed lookup on `from` per hop.\n */\nconst assertNoLoop = async (\n args: HookOperationArgs,\n normalizedFrom: string,\n collectionSlug: string,\n): Promise<void> => {\n const { data, req } = args;\n if (!data || data.enabled === false) return;\n\n const normalizedTo = normalizeOrUndefined(data.to);\n if (!normalizedTo) return;\n if (normalizedTo === normalizedFrom) {\n throw new ValidationError({ errors: [{ message: LOOP_MESSAGE, path: \"to\" }] });\n }\n\n const excludeId = resolveExcludeId(args);\n const visited = new Set<string>();\n let current = normalizedTo;\n for (let hop = 0; hop <= MAX_REDIRECT_CHAIN_HOPS; hop++) {\n if (visited.has(current)) {\n throw new ValidationError({ errors: [{ message: CHAIN_LOOP_MESSAGE, path: \"to\" }] });\n }\n visited.add(current);\n // Integrity lookup: a requester without read access must not hide a rule from the cycle walk.\n const { docs } = await findRedirectByFrom(req.payload, {\n collectionSlug,\n from: current,\n excludeId,\n req,\n overrideAccess: true,\n });\n\n const rule = docs[0];\n if (!rule?.enabled) return;\n const next = normalizeOrUndefined(rule.to);\n if (!next) return;\n if (next === normalizedFrom) {\n throw new ValidationError({ errors: [{ message: LOOP_MESSAGE, path: \"to\" }] });\n }\n current = next;\n }\n\n throw new ValidationError({ errors: [{ message: CHAIN_TOO_LONG_MESSAGE, path: \"to\" }] });\n};\n\nconst assertNoDuplicateFrom = async (\n args: HookOperationArgs,\n collectionSlug: string,\n): Promise<string | undefined> => {\n const { data, req } = args;\n if (!data) return undefined;\n\n const normalizedFrom = resolveNormalizedFrom(data);\n if (!normalizedFrom) return undefined;\n\n const duplicate = await findDuplicateFrom({\n req,\n collectionSlug,\n normalizedFrom,\n excludeId: resolveExcludeId(args),\n });\n\n if (duplicate) {\n throw new ValidationError({ errors: [{ message: DUPLICATE_MESSAGE, path: \"from\" }] });\n }\n\n await assertNoLoop(args, normalizedFrom, collectionSlug);\n\n return normalizedFrom;\n};\n\nconst normalizeRulePaths = (data: Record<string, unknown>): void => {\n const normalize = (value: unknown, path: \"from\" | \"to\"): string => {\n if (typeof value !== \"string\") {\n throw new ValidationError({ errors: [{ message: `${path} is required`, path }] });\n }\n try {\n return normalizeRelativeRedirectPath(value);\n } catch (error) {\n const message = error instanceof Error ? error.message : `Invalid ${path} path`;\n throw new ValidationError({ errors: [{ message, path }] });\n }\n };\n\n if (typeof data.from !== \"string\") {\n throw new ValidationError({ errors: [{ message: \"from is required\", path: \"from\" }] });\n }\n try {\n data.from = normalizeRedirectSourcePath(data.from);\n } catch (error) {\n const message = error instanceof Error ? error.message : \"Invalid from path\";\n throw new ValidationError({ errors: [{ message, path: \"from\" }] });\n }\n data.to = normalize(data.to, \"to\");\n\n try {\n const statusCode = assertRedirectStatusCode(data.statusCode ?? DEFAULT_REDIRECT_STATUS_CODE);\n validateDestinationForStatus(statusCode, data.to);\n } catch (error) {\n const message =\n error instanceof Error ? error.message : \"Invalid redirect status or destination\";\n throw new ValidationError({ errors: [{ message, path: \"to\" }] });\n }\n};\n\n/**\n * The rule as it will be stored: on update, the stored document overlaid with the incoming change.\n * Payload passes only the incoming fields to beforeChange, so checking `data` alone would skip the\n * loop walk for any update that omits `from` or `to`.\n */\nexport const effectiveRuleData = (\n operation: string,\n data: Record<string, unknown> | undefined,\n originalDoc: Record<string, unknown> | null | undefined,\n): Record<string, unknown> | undefined => {\n if (!data) return undefined;\n if (operation !== \"update\" || !originalDoc) return data;\n return { ...originalDoc, ...data };\n};\n\nexport const createDuplicateBeforeChange = (\n collectionSlug = \"redirects\",\n): CollectionBeforeChangeHook => {\n return async (args) => {\n const { data, req, operation, originalDoc } = args;\n if (operation === \"create\" || operation === \"update\") {\n await lockRedirectRuleGraph(req);\n }\n const effective = effectiveRuleData(\n operation,\n data as Record<string, unknown> | undefined,\n originalDoc as Record<string, unknown> | null | undefined,\n );\n if (effective && (operation === \"create\" || operation === \"update\")) {\n normalizeRulePaths(effective);\n if (data) {\n if (typeof data.from === \"string\") data.from = effective.from;\n if (typeof data.to === \"string\") data.to = effective.to;\n }\n }\n const normalizedFrom = await assertNoDuplicateFrom(\n {\n data: effective,\n req,\n operation,\n originalDoc: args.originalDoc,\n },\n collectionSlug,\n );\n\n if (data && normalizedFrom && typeof data.from === \"string\") {\n data.from = normalizedFrom;\n }\n\n return data;\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"duplicate-guard.js","sourceRoot":"","sources":["../../../../src/core/redirects/collection/duplicate-guard.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,4BAA4B,GAC7B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACL,2BAA2B,EAC3B,6BAA6B,GAC9B,MAAM,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;QAEF,IAAI,IAAI,IAAI,cAAc,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC5D,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC7B,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { CollectionBeforeChangeHook, PayloadRequest } from \"payload\";\nimport { ValidationError } from \"payload\";\nimport {\n assertRedirectStatusCode,\n DEFAULT_REDIRECT_STATUS_CODE,\n validateDestinationForStatus,\n} from \"#redirect/core/redirects/model/status-code\";\nimport {\n normalizeRedirectSourcePath,\n normalizeRelativeRedirectPath,\n} from \"#redirect/core/redirects/normalize-path\";\nimport { findRedirectByFrom } from \"#redirect/core/redirects/queries\";\n\n/** Enabled rules followed from `to` before a write is refused as unverifiable. Bounds the lookups per write. */\nexport const MAX_REDIRECT_CHAIN_HOPS = 1000;\n\nconst DUPLICATE_MESSAGE = \"A redirect rule already exists for this source path\";\nconst LOOP_MESSAGE =\n \"Enabled redirect rules already lead from this destination back to this source path\";\nconst CHAIN_LOOP_MESSAGE = \"Enabled redirect rules from this destination already contain a loop\";\nconst CHAIN_TOO_LONG_MESSAGE = `Enabled redirect rules from this destination chain longer than ${MAX_REDIRECT_CHAIN_HOPS} hops`;\n\nconst normalizeOrUndefined = (raw: unknown): string | undefined => {\n if (typeof raw !== \"string\" || raw.trim().length === 0) return undefined;\n try {\n return normalizeRelativeRedirectPath(raw).split(\"#\", 1)[0];\n } catch {\n return undefined;\n }\n};\n\nexport const resolveNormalizedFrom = (data: Record<string, unknown>): string | undefined => {\n if (typeof data.from !== \"string\" || data.from.trim().length === 0) return undefined;\n try {\n return normalizeRedirectSourcePath(data.from);\n } catch {\n return undefined;\n }\n};\n\nexport const findDuplicateFrom = async ({\n req,\n collectionSlug,\n normalizedFrom,\n excludeId,\n}: {\n req: PayloadRequest;\n collectionSlug: string;\n normalizedFrom: string;\n excludeId?: string | number;\n}): Promise<boolean> => {\n // Integrity lookup: it must see every rule, whatever the requester can read. It returns only a boolean.\n const result = await findRedirectByFrom(req.payload, {\n collectionSlug,\n from: normalizedFrom,\n excludeId,\n req,\n overrideAccess: true,\n select: { id: true },\n });\n\n return result.docs.length > 0;\n};\n\ntype HookOperationArgs = {\n data: Record<string, unknown> | undefined;\n req: PayloadRequest;\n operation: string;\n originalDoc?: { id?: string | number } | null;\n};\n\n/** Payload collection `beforeChange` passes `originalDoc`, not top-level `id` (see Payload 3 update op). */\nexport const resolveDuplicateGuardExcludeId = (\n operation: string,\n originalDoc?: { id?: string | number } | null,\n): string | number | undefined => {\n if (operation !== \"update\" || !originalDoc) return undefined;\n const { id } = originalDoc;\n if (typeof id === \"string\" || typeof id === \"number\") return id;\n return undefined;\n};\n\nconst resolveExcludeId = (args: HookOperationArgs): string | number | undefined =>\n resolveDuplicateGuardExcludeId(args.operation, args.originalDoc);\n\n/**\n * Rejects a write whose enabled chain from `to` returns to `from`, of any length. A chain that\n * repeats a hop without reaching `from` predates this write, so it is accepted. Runs past\n * MAX_REDIRECT_CHAIN_HOPS are refused as unverifiable. One indexed lookup on `from` per hop.\n */\nconst assertNoLoop = async (\n args: HookOperationArgs,\n normalizedFrom: string,\n collectionSlug: string,\n): Promise<void> => {\n const { data, req } = args;\n if (!data || data.enabled === false) return;\n\n const normalizedTo = normalizeOrUndefined(data.to);\n if (!normalizedTo) return;\n if (normalizedTo === normalizedFrom) {\n throw new ValidationError({ errors: [{ message: LOOP_MESSAGE, path: \"to\" }] });\n }\n\n const excludeId = resolveExcludeId(args);\n const visited = new Set<string>();\n let current = normalizedTo;\n for (let hop = 0; hop <= MAX_REDIRECT_CHAIN_HOPS; hop++) {\n if (visited.has(current)) {\n throw new ValidationError({ errors: [{ message: CHAIN_LOOP_MESSAGE, path: \"to\" }] });\n }\n visited.add(current);\n // Integrity lookup: a requester without read access must not hide a rule from the cycle walk.\n const { docs } = await findRedirectByFrom(req.payload, {\n collectionSlug,\n from: current,\n excludeId,\n req,\n overrideAccess: true,\n });\n\n const rule = docs[0];\n if (!rule?.enabled) return;\n const next = normalizeOrUndefined(rule.to);\n if (!next) return;\n if (next === normalizedFrom) {\n throw new ValidationError({ errors: [{ message: LOOP_MESSAGE, path: \"to\" }] });\n }\n current = next;\n }\n\n throw new ValidationError({ errors: [{ message: CHAIN_TOO_LONG_MESSAGE, path: \"to\" }] });\n};\n\nconst assertNoDuplicateFrom = async (\n args: HookOperationArgs,\n collectionSlug: string,\n): Promise<string | undefined> => {\n const { data, req } = args;\n if (!data) return undefined;\n\n const normalizedFrom = resolveNormalizedFrom(data);\n if (!normalizedFrom) return undefined;\n\n const duplicate = await findDuplicateFrom({\n req,\n collectionSlug,\n normalizedFrom,\n excludeId: resolveExcludeId(args),\n });\n\n if (duplicate) {\n throw new ValidationError({ errors: [{ message: DUPLICATE_MESSAGE, path: \"from\" }] });\n }\n\n await assertNoLoop(args, normalizedFrom, collectionSlug);\n\n return normalizedFrom;\n};\n\nconst normalizeRulePaths = (data: Record<string, unknown>): void => {\n const normalize = (value: unknown, path: \"from\" | \"to\"): string => {\n if (typeof value !== \"string\") {\n throw new ValidationError({ errors: [{ message: `${path} is required`, path }] });\n }\n try {\n return normalizeRelativeRedirectPath(value);\n } catch (error) {\n const message = error instanceof Error ? error.message : `Invalid ${path} path`;\n throw new ValidationError({ errors: [{ message, path }] });\n }\n };\n\n if (typeof data.from !== \"string\") {\n throw new ValidationError({ errors: [{ message: \"from is required\", path: \"from\" }] });\n }\n try {\n data.from = normalizeRedirectSourcePath(data.from);\n } catch (error) {\n const message = error instanceof Error ? error.message : \"Invalid from path\";\n throw new ValidationError({ errors: [{ message, path: \"from\" }] });\n }\n data.to = normalize(data.to, \"to\");\n\n try {\n const statusCode = assertRedirectStatusCode(data.statusCode ?? DEFAULT_REDIRECT_STATUS_CODE);\n validateDestinationForStatus(statusCode, data.to);\n } catch (error) {\n const message =\n error instanceof Error ? error.message : \"Invalid redirect status or destination\";\n throw new ValidationError({ errors: [{ message, path: \"to\" }] });\n }\n};\n\n/**\n * The rule as it will be stored: on update, the stored document overlaid with the incoming change.\n * Payload passes only the incoming fields to beforeChange, so checking `data` alone would skip the\n * loop walk for any update that omits `from` or `to`.\n */\nexport const effectiveRuleData = (\n operation: string,\n data: Record<string, unknown> | undefined,\n originalDoc: Record<string, unknown> | null | undefined,\n): Record<string, unknown> | undefined => {\n if (!data) return undefined;\n if (operation !== \"update\" || !originalDoc) return data;\n return { ...originalDoc, ...data };\n};\n\nexport const createDuplicateBeforeChange = (\n collectionSlug = \"redirects\",\n): CollectionBeforeChangeHook => {\n return async (args) => {\n const { data, req, operation, originalDoc } = args;\n const effective = effectiveRuleData(\n operation,\n data as Record<string, unknown> | undefined,\n originalDoc as Record<string, unknown> | null | undefined,\n );\n if (effective && (operation === \"create\" || operation === \"update\")) {\n normalizeRulePaths(effective);\n if (data) {\n if (typeof data.from === \"string\") data.from = effective.from;\n if (typeof data.to === \"string\") data.to = effective.to;\n }\n }\n const normalizedFrom = await assertNoDuplicateFrom(\n {\n data: effective,\n req,\n operation,\n originalDoc: args.originalDoc,\n },\n collectionSlug,\n );\n\n if (data && normalizedFrom && typeof data.from === \"string\") {\n data.from = normalizedFrom;\n }\n\n return data;\n };\n};\n"]}
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
import type { PayloadRequest } from "payload";
|
|
1
|
+
import type { CollectionBeforeOperationHook, PayloadRequest } from "payload";
|
|
2
2
|
export declare function lockRedirectRuleGraph(req: PayloadRequest): Promise<void>;
|
|
3
|
+
/**
|
|
4
|
+
* Takes the graph lock in `beforeOperation`, before Payload reads `originalDoc` and before any ownership
|
|
5
|
+
* or graph decision. A lock taken in `beforeChange` would let a stale `originalDoc` drive the loop walk.
|
|
6
|
+
*/
|
|
7
|
+
export declare const createRedirectGraphLockBeforeOperation: () => CollectionBeforeOperationHook;
|
|
@@ -29,4 +29,15 @@ export async function lockRedirectRuleGraph(req) {
|
|
|
29
29
|
throw new Error("Redirect rule lock requires READ COMMITTED transactions.");
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Takes the graph lock in `beforeOperation`, before Payload reads `originalDoc` and before any ownership
|
|
34
|
+
* or graph decision. A lock taken in `beforeChange` would let a stale `originalDoc` drive the loop walk.
|
|
35
|
+
*/
|
|
36
|
+
export const createRedirectGraphLockBeforeOperation = () => {
|
|
37
|
+
return async ({ operation, req }) => {
|
|
38
|
+
if (operation === "create" || operation === "update") {
|
|
39
|
+
await lockRedirectRuleGraph(req);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
32
43
|
//# sourceMappingURL=graph-lock.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph-lock.js","sourceRoot":"","sources":["../../../../src/core/redirects/collection/graph-lock.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,MAAM,QAAQ,GAAG,6BAA6B,CAAC;AAC/C,MAAM,wBAAwB,GAAG,UAAU,CAAC;AAW5C,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,GAAmB;IAC7D,MAAM,EAAE,GAAG,GAAG,CAAC,OAAO,EAAE,EAA2C,CAAC;IACpE,gFAAgF;IAChF,IAAI,EAAE,IAAI,IAAI;QAAE,OAAO;IACvB,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC,WAAW,IAAI,EAAE,CAAC;QAAE,OAAO;IAEjE,MAAM,aAAa,GAAG,MAAM,GAAG,CAAC,aAAa,CAAC;IAC9C,MAAM,OAAO,GAAG,aAAa,IAAI,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;IACzF,MAAM,EAAE,GAAG,OAAO,EAAE,EAAsC,CAAC;IAC3D,IAAI,EAAE,IAAI,IAAI,IAAI,OAAO,EAAE,CAAC,OAAO,KAAK,UAAU,EAAE,CAAC;QACnD,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;IAC5F,CAAC;IAED,oEAAoE;IACpE,MAAM,EAAE,CAAC,OAAO,CAAC,0CAA0C,QAAQ,KAAK,CAAC,CAAC;IAE1E,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAC;IACjE,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,qBAAqB,KAAK,gBAAgB,EAAE,CAAC;QAClE,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;IAC9E,CAAC;AACH,CAAC","sourcesContent":["import type { PayloadRequest } from \"payload\";\n\n/**\n * Serializes writes to the redirect rule graph. The loop and duplicate checks read committed rows,\n * so two concurrent writes that each close a cycle could both pass unless the second waits for the\n * first to commit. The lock is transaction-scoped and released at commit or rollback.\n *\n * Postgres is the supported adapter: the lock needs the request transaction and READ COMMITTED\n * isolation, and the function throws when either is missing rather than checking without it.\n * Adapters without advisory locks return without locking.\n */\nconst LOCK_KEY = \"payload-redirect:rule-graph\";\nconst POSTGRES_ADAPTER_PATTERN = /postgres/;\n\ntype TransactionQueryable = {\n execute: (query: string) => Promise<{ rows: Array<Record<string, unknown>> }>;\n};\n\ntype DbWithSessions = {\n packageName?: string;\n sessions?: Record<string, { db?: unknown } | undefined>;\n};\n\nexport async function lockRedirectRuleGraph(req: PayloadRequest): Promise<void> {\n const db = req.payload?.db as unknown as DbWithSessions | undefined;\n // No database adapter means there is no transaction graph to serialize against.\n if (db == null) return;\n if (!POSTGRES_ADAPTER_PATTERN.test(db.packageName ?? \"\")) return;\n\n const transactionID = await req.transactionID;\n const session = transactionID == null ? undefined : db.sessions?.[String(transactionID)];\n const tx = session?.db as TransactionQueryable | undefined;\n if (tx == null || typeof tx.execute !== \"function\") {\n throw new Error(\"Redirect rule lock needs the request transaction; refusing without it.\");\n }\n\n // LOCK_KEY is a constant, so inlining it into the SQL text is safe.\n await tx.execute(`select pg_advisory_xact_lock(hashtext('${LOCK_KEY}'))`);\n\n const isolation = await tx.execute(\"show transaction_isolation\");\n if (isolation.rows[0]?.transaction_isolation !== \"read committed\") {\n throw new Error(\"Redirect rule lock requires READ COMMITTED transactions.\");\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"graph-lock.js","sourceRoot":"","sources":["../../../../src/core/redirects/collection/graph-lock.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,MAAM,QAAQ,GAAG,6BAA6B,CAAC;AAC/C,MAAM,wBAAwB,GAAG,UAAU,CAAC;AAW5C,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,GAAmB;IAC7D,MAAM,EAAE,GAAG,GAAG,CAAC,OAAO,EAAE,EAA2C,CAAC;IACpE,gFAAgF;IAChF,IAAI,EAAE,IAAI,IAAI;QAAE,OAAO;IACvB,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC,WAAW,IAAI,EAAE,CAAC;QAAE,OAAO;IAEjE,MAAM,aAAa,GAAG,MAAM,GAAG,CAAC,aAAa,CAAC;IAC9C,MAAM,OAAO,GAAG,aAAa,IAAI,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;IACzF,MAAM,EAAE,GAAG,OAAO,EAAE,EAAsC,CAAC;IAC3D,IAAI,EAAE,IAAI,IAAI,IAAI,OAAO,EAAE,CAAC,OAAO,KAAK,UAAU,EAAE,CAAC;QACnD,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;IAC5F,CAAC;IAED,oEAAoE;IACpE,MAAM,EAAE,CAAC,OAAO,CAAC,0CAA0C,QAAQ,KAAK,CAAC,CAAC;IAE1E,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAC;IACjE,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,qBAAqB,KAAK,gBAAgB,EAAE,CAAC;QAClE,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;IAC9E,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,sCAAsC,GAAG,GAAkC,EAAE;IACxF,OAAO,KAAK,EAAE,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE,EAAE;QAClC,IAAI,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,QAAQ,EAAE,CAAC;YACrD,MAAM,qBAAqB,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { CollectionBeforeOperationHook, PayloadRequest } from \"payload\";\n\n/**\n * Serializes writes to the redirect rule graph. The loop and duplicate checks read committed rows,\n * so two concurrent writes that each close a cycle could both pass unless the second waits for the\n * first to commit. The lock is transaction-scoped and released at commit or rollback.\n *\n * Postgres is the supported adapter: the lock needs the request transaction and READ COMMITTED\n * isolation, and the function throws when either is missing rather than checking without it.\n * Adapters without advisory locks return without locking.\n */\nconst LOCK_KEY = \"payload-redirect:rule-graph\";\nconst POSTGRES_ADAPTER_PATTERN = /postgres/;\n\ntype TransactionQueryable = {\n execute: (query: string) => Promise<{ rows: Array<Record<string, unknown>> }>;\n};\n\ntype DbWithSessions = {\n packageName?: string;\n sessions?: Record<string, { db?: unknown } | undefined>;\n};\n\nexport async function lockRedirectRuleGraph(req: PayloadRequest): Promise<void> {\n const db = req.payload?.db as unknown as DbWithSessions | undefined;\n // No database adapter means there is no transaction graph to serialize against.\n if (db == null) return;\n if (!POSTGRES_ADAPTER_PATTERN.test(db.packageName ?? \"\")) return;\n\n const transactionID = await req.transactionID;\n const session = transactionID == null ? undefined : db.sessions?.[String(transactionID)];\n const tx = session?.db as TransactionQueryable | undefined;\n if (tx == null || typeof tx.execute !== \"function\") {\n throw new Error(\"Redirect rule lock needs the request transaction; refusing without it.\");\n }\n\n // LOCK_KEY is a constant, so inlining it into the SQL text is safe.\n await tx.execute(`select pg_advisory_xact_lock(hashtext('${LOCK_KEY}'))`);\n\n const isolation = await tx.execute(\"show transaction_isolation\");\n if (isolation.rows[0]?.transaction_isolation !== \"read committed\") {\n throw new Error(\"Redirect rule lock requires READ COMMITTED transactions.\");\n }\n}\n\n/**\n * Takes the graph lock in `beforeOperation`, before Payload reads `originalDoc` and before any ownership\n * or graph decision. A lock taken in `beforeChange` would let a stale `originalDoc` drive the loop walk.\n */\nexport const createRedirectGraphLockBeforeOperation = (): CollectionBeforeOperationHook => {\n return async ({ operation, req }) => {\n if (operation === \"create\" || operation === \"update\") {\n await lockRedirectRuleGraph(req);\n }\n };\n};\n"]}
|
|
@@ -59,10 +59,11 @@ Automatic rules use one of `path`, `paths`, or `getPath`, configured centrally o
|
|
|
59
59
|
Draft saves, first publications, unpublishes, and deletions create no automatic rules. Existing
|
|
60
60
|
redirects remain when a document is unpublished or deleted.
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
A publication writes a rule only when it changes the published path. The source is the path of
|
|
63
|
+
the published version immediately before it. Republishing an unchanged path writes nothing: a rule
|
|
64
|
+
an editor deleted stays deleted, and an enabled rule is left as it is. Editing or disabling an
|
|
65
|
+
automatic rule changes its origin to `manual`, so the plugin never re-enables a rule an editor
|
|
66
|
+
turned off.
|
|
66
67
|
|
|
67
68
|
An automatic write skips a source path owned by a manual rule. Editing an automatic rule changes
|
|
68
69
|
its origin to `manual`, so later publications do not update it. A path-template change does not
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klnap/payload-redirect",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Redirect management for Payload CMS.",
|
|
5
5
|
"homepage": "https://github.com/klnap/payload-redirect#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
],
|
|
40
40
|
"scripts": {
|
|
41
41
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
-
"typecheck:consumer": "tsc -p tsconfig.type-fixture.json && tsc -p tsconfig.type-fixture.no-generated.json",
|
|
42
|
+
"typecheck:consumer": "tsc -p tsconfig.type-fixture.json && tsc -p tsconfig.type-fixture.no-generated.json && tsc -p tsconfig.type-fixture.readme.json",
|
|
43
43
|
"test": "bun test src tests/unit",
|
|
44
44
|
"test:int": "bun scripts/with-ephemeral-postgres.mjs scripts/ephemeral-postgres.config.mjs -- bun test tests/integration tests/security --max-concurrency 1 --timeout=15000",
|
|
45
45
|
"check:boundaries": "depcruise --config dependency-cruiser.config.mjs --output-type err-long src",
|
|
@@ -69,10 +69,12 @@
|
|
|
69
69
|
"@changesets/cli": "3.0.3",
|
|
70
70
|
"@swc/core": "1.16.22",
|
|
71
71
|
"@payloadcms/db-postgres": "3.94.1",
|
|
72
|
+
"@payloadcms/graphql": "3.94.1",
|
|
72
73
|
"@payloadcms/richtext-lexical": "3.94.1",
|
|
73
74
|
"@types/bun": "1.4.5",
|
|
74
75
|
"@types/pg": "^8.23.1",
|
|
75
76
|
"dependency-cruiser": "^18.8.0",
|
|
77
|
+
"graphql": "16.15.0",
|
|
76
78
|
"npm": "12.2.3",
|
|
77
79
|
"publint": "0.3.25",
|
|
78
80
|
"payload": "3.94.1",
|