@akinon/next 2.0.79-beta.1 → 2.0.79-rc.0
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 +28 -8
- package/api/client-env.ts +78 -0
- package/bin/pz-generate-routes.js +4 -1
- package/components/plugin-module.tsx +0 -1
- package/data/client/checkout.ts +0 -1
- package/data/client/wishlist.ts +22 -0
- package/data/server/category.ts +14 -2
- package/data/server/list.ts +13 -1
- package/data/server/product.ts +10 -0
- package/data/server/special-page.ts +14 -1
- package/data/server/widget.ts +20 -265
- package/data/urls.ts +13 -4
- package/hocs/server/with-segment-defaults.tsx +0 -9
- package/hooks/index.ts +1 -0
- package/hooks/use-captcha.tsx +1 -1
- package/hooks/use-client-env.ts +265 -0
- package/middlewares/default.ts +254 -247
- package/middlewares/pretty-url.ts +3 -1
- package/package.json +2 -2
- package/plugins.d.ts +10 -0
- package/plugins.js +1 -0
- package/redux/middlewares/checkout.ts +45 -3
- package/redux/middlewares/pre-order/installment-option.ts +9 -1
- package/types/index.ts +20 -9
- package/utils/app-fetch.ts +1 -3
- package/utils/csrf.ts +1 -1
- package/utils/payload-optimizer.ts +481 -0
- package/utils/pz-segments.ts +1 -30
- package/utils/server-variables.ts +1 -8
- package/with-pz-config.js +6 -1
- package/utils/preview-token.ts +0 -111
- package/utils/preview.ts +0 -83
package/utils/pz-segments.ts
CHANGED
|
@@ -40,44 +40,15 @@ export function encodePzValue(
|
|
|
40
40
|
.join(config.separator);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
/**
|
|
44
|
-
* Inverse of `encodePzValue`.
|
|
45
|
-
*
|
|
46
|
-
* The `url` segment is `encodeURIComponent(fullUrl)`, and that leaves `-`
|
|
47
|
-
* alone — so a pathname containing the separator (`/collections/summer--sale`)
|
|
48
|
-
* splits into extra parts. Only `url` can do that (locale, currency and the
|
|
49
|
-
* custom segments are validated slugs), so the segments before it are read
|
|
50
|
-
* from the front, the ones after it from the back, and whatever is left in
|
|
51
|
-
* the middle is the url re-joined. A purely positional read would hand the
|
|
52
|
-
* url's tail to the segment after it — with a preview segment present that
|
|
53
|
-
* meant rendering the wrong preview (or live) with a misleading banner.
|
|
54
|
-
*/
|
|
55
43
|
export function decodePzValue(
|
|
56
44
|
pzValue: string,
|
|
57
45
|
config: PzSegmentsConfig
|
|
58
46
|
): Record<string, string> {
|
|
59
47
|
const parts = pzValue.split(config.separator);
|
|
60
48
|
const result: Record<string, string> = {};
|
|
61
|
-
const urlIndex = config.segments.findIndex((seg) => seg.name === 'url');
|
|
62
49
|
|
|
63
|
-
if (urlIndex === -1 || parts.length <= config.segments.length) {
|
|
64
|
-
config.segments.forEach((seg, index) => {
|
|
65
|
-
result[seg.name] = parts[index] ?? '';
|
|
66
|
-
});
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const trailing = config.segments.length - urlIndex - 1;
|
|
71
50
|
config.segments.forEach((seg, index) => {
|
|
72
|
-
|
|
73
|
-
result[seg.name] = parts[index] ?? '';
|
|
74
|
-
} else if (index === urlIndex) {
|
|
75
|
-
result[seg.name] = parts
|
|
76
|
-
.slice(urlIndex, parts.length - trailing)
|
|
77
|
-
.join(config.separator);
|
|
78
|
-
} else {
|
|
79
|
-
result[seg.name] = parts[parts.length - trailing + (index - urlIndex - 1)] ?? '';
|
|
80
|
-
}
|
|
51
|
+
result[seg.name] = parts[index] ?? '';
|
|
81
52
|
});
|
|
82
53
|
|
|
83
54
|
return result;
|
|
@@ -6,12 +6,5 @@ const { locales, defaultLocaleValue, defaultCurrencyCode } =
|
|
|
6
6
|
export const ServerVariables = {
|
|
7
7
|
locale: locales.find((l) => l.value === defaultLocaleValue)?.value ?? '',
|
|
8
8
|
currency: defaultCurrencyCode,
|
|
9
|
-
globalHeaders: {}
|
|
10
|
-
/**
|
|
11
|
-
* Which named widget preview the current render belongs to, decoded from
|
|
12
|
-
* the pz route segment by withSegmentDefaults. Empty for ordinary traffic.
|
|
13
|
-
* Only ever acted on together with draft mode, so a value left behind by a
|
|
14
|
-
* concurrent request cannot leak preview content to a normal visitor.
|
|
15
|
-
*/
|
|
16
|
-
previewId: ''
|
|
9
|
+
globalHeaders: {}
|
|
17
10
|
};
|
package/with-pz-config.js
CHANGED
|
@@ -30,6 +30,10 @@ const defaultConfig = {
|
|
|
30
30
|
{
|
|
31
31
|
protocol: 'https',
|
|
32
32
|
hostname: '**.akinoncdn.com'
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
protocol: 'https',
|
|
36
|
+
hostname: '**.akinoncloudcdn.com'
|
|
33
37
|
}
|
|
34
38
|
]
|
|
35
39
|
},
|
|
@@ -74,7 +78,8 @@ const defaultConfig = {
|
|
|
74
78
|
acc[`@akinon/${plugin}`] = false;
|
|
75
79
|
return acc;
|
|
76
80
|
}, {}),
|
|
77
|
-
translations: false
|
|
81
|
+
translations: false,
|
|
82
|
+
'@opentelemetry/exporter-jaeger': false
|
|
78
83
|
};
|
|
79
84
|
// Ensure webpack can resolve deps from the app's node_modules when
|
|
80
85
|
// compiling transpiled packages (e.g. @akinon/next) whose imports
|
package/utils/preview-token.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { isValidPreviewId } from './preview';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Signed, expiring preview entry tokens.
|
|
5
|
-
*
|
|
6
|
-
* The storefront never hands out its PREVIEW_SECRET: the editor asks
|
|
7
|
-
* `/api/preview/link` (proving it is an editor user with its Omnitron token)
|
|
8
|
-
* and receives a token that names ONE preview and dies at `expiresAt`. A leaked
|
|
9
|
-
* link therefore exposes nothing lasting, and rotating the secret invalidates
|
|
10
|
-
* every outstanding link at once.
|
|
11
|
-
*
|
|
12
|
-
* Format: `v1.<previewId>.<expiresAt unix seconds>.<base64url HMAC-SHA256>`.
|
|
13
|
-
* Web Crypto only, so the same code runs in Node route handlers and on the
|
|
14
|
-
* edge.
|
|
15
|
-
*/
|
|
16
|
-
export const PREVIEW_TOKEN_VERSION = 'v1';
|
|
17
|
-
|
|
18
|
-
/** How long an editor-issued preview link stays valid unless configured. */
|
|
19
|
-
export const DEFAULT_PREVIEW_LINK_TTL_SECONDS = 24 * 60 * 60;
|
|
20
|
-
|
|
21
|
-
export interface PreviewTokenClaims {
|
|
22
|
-
previewId: string;
|
|
23
|
-
/** Unix time in SECONDS. */
|
|
24
|
-
expiresAt: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type PreviewTokenVerdict =
|
|
28
|
-
| { ok: true; claims: PreviewTokenClaims }
|
|
29
|
-
| { ok: false; reason: 'malformed' | 'expired' | 'invalid-signature' };
|
|
30
|
-
|
|
31
|
-
// Created per call: the module is also imported where TextEncoder only shows
|
|
32
|
-
// up after a polyfill (jsdom), and the cost is negligible.
|
|
33
|
-
const encode = (value: string) => new TextEncoder().encode(value);
|
|
34
|
-
|
|
35
|
-
const base64url = (bytes: ArrayBuffer): string => {
|
|
36
|
-
const view = new Uint8Array(bytes);
|
|
37
|
-
let binary = '';
|
|
38
|
-
for (let i = 0; i < view.length; i++) {
|
|
39
|
-
binary += String.fromCharCode(view[i]);
|
|
40
|
-
}
|
|
41
|
-
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const hmac = async (secret: string, payload: string): Promise<string> => {
|
|
45
|
-
const key = await crypto.subtle.importKey(
|
|
46
|
-
'raw',
|
|
47
|
-
encode(secret),
|
|
48
|
-
{ name: 'HMAC', hash: 'SHA-256' },
|
|
49
|
-
false,
|
|
50
|
-
['sign']
|
|
51
|
-
);
|
|
52
|
-
return base64url(await crypto.subtle.sign('HMAC', key, encode(payload)));
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/** Constant-time string equality — a signature check must not leak by timing. */
|
|
56
|
-
const timingSafeEqual = (a: string, b: string): boolean => {
|
|
57
|
-
if (a.length !== b.length) return false;
|
|
58
|
-
let diff = 0;
|
|
59
|
-
for (let i = 0; i < a.length; i++) {
|
|
60
|
-
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
61
|
-
}
|
|
62
|
-
return diff === 0;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const payloadOf = ({ previewId, expiresAt }: PreviewTokenClaims): string =>
|
|
66
|
-
`${PREVIEW_TOKEN_VERSION}.${previewId}.${expiresAt}`;
|
|
67
|
-
|
|
68
|
-
export const signPreviewToken = async (
|
|
69
|
-
claims: PreviewTokenClaims,
|
|
70
|
-
secret: string
|
|
71
|
-
): Promise<string> => {
|
|
72
|
-
if (!isValidPreviewId(claims.previewId)) {
|
|
73
|
-
throw new Error('signPreviewToken: invalid preview id');
|
|
74
|
-
}
|
|
75
|
-
if (!Number.isInteger(claims.expiresAt) || claims.expiresAt <= 0) {
|
|
76
|
-
throw new Error('signPreviewToken: invalid expiry');
|
|
77
|
-
}
|
|
78
|
-
const payload = payloadOf(claims);
|
|
79
|
-
return `${payload}.${await hmac(secret, payload)}`;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const verifyPreviewToken = async (
|
|
83
|
-
token: string,
|
|
84
|
-
secret: string,
|
|
85
|
-
nowMs: number = Date.now()
|
|
86
|
-
): Promise<PreviewTokenVerdict> => {
|
|
87
|
-
const parts = typeof token === 'string' ? token.split('.') : [];
|
|
88
|
-
if (parts.length !== 4 || parts[0] !== PREVIEW_TOKEN_VERSION) {
|
|
89
|
-
return { ok: false, reason: 'malformed' };
|
|
90
|
-
}
|
|
91
|
-
const [, previewId, expiresAtRaw, signature] = parts;
|
|
92
|
-
const expiresAt = Number(expiresAtRaw);
|
|
93
|
-
if (
|
|
94
|
-
!isValidPreviewId(previewId) ||
|
|
95
|
-
!/^\d+$/.test(expiresAtRaw) ||
|
|
96
|
-
!Number.isSafeInteger(expiresAt) ||
|
|
97
|
-
!signature
|
|
98
|
-
) {
|
|
99
|
-
return { ok: false, reason: 'malformed' };
|
|
100
|
-
}
|
|
101
|
-
// Signature before expiry: an attacker must not learn which expired tokens
|
|
102
|
-
// were once valid.
|
|
103
|
-
const expected = await hmac(secret, payloadOf({ previewId, expiresAt }));
|
|
104
|
-
if (!timingSafeEqual(signature, expected)) {
|
|
105
|
-
return { ok: false, reason: 'invalid-signature' };
|
|
106
|
-
}
|
|
107
|
-
if (expiresAt * 1000 <= nowMs) {
|
|
108
|
-
return { ok: false, reason: 'expired' };
|
|
109
|
-
}
|
|
110
|
-
return { ok: true, claims: { previewId, expiresAt } };
|
|
111
|
-
};
|
package/utils/preview.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Widget preview contract — shared by the middleware/route handler that
|
|
3
|
-
* enters preview, the pz segment that carries which preview is being viewed,
|
|
4
|
-
* and the widget fetch layer that resolves the preview copies.
|
|
5
|
-
*
|
|
6
|
-
* The theme editor writes the other half of this contract: it creates named
|
|
7
|
-
* previews and stores each one's pages under `<slug>-preview-<id>`. The two
|
|
8
|
-
* sides must never drift.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/** Suffix before the preview id — hyphenated because Omnitron slugifies. */
|
|
12
|
-
export const PREVIEW_SLUG_SUFFIX = '-preview';
|
|
13
|
-
|
|
14
|
-
/** Remembers which named preview this browser is viewing. */
|
|
15
|
-
export const PREVIEW_ID_COOKIE = 'pz-preview-id';
|
|
16
|
-
|
|
17
|
-
/** Next.js's own draft-mode cookie — set by `draftMode().enable()`. */
|
|
18
|
-
export const DRAFT_MODE_COOKIE = '__prerender_bypass';
|
|
19
|
-
|
|
20
|
-
/** Query key on the editor's link, consumed by the /api/preview handler. */
|
|
21
|
-
export const PREVIEW_ID_QUERY_PARAM = 'preview_id';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The pz segment carrying the preview id into the render.
|
|
25
|
-
*
|
|
26
|
-
* A cookie cannot be used directly: the pages that render widgets are
|
|
27
|
-
* `dynamic = 'force-static'`, and Next.js blanks `cookies()`/`headers()`
|
|
28
|
-
* under that config (draftMode is the exception). Route params always
|
|
29
|
-
* survive, so the middleware resolves the cookie into this segment and the
|
|
30
|
-
* server components read it from there. See settings.js `pzSegments`.
|
|
31
|
-
*/
|
|
32
|
-
export const PREVIEW_PZ_SEGMENT = 'preview';
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Ids come from a cookie, so they are validated everywhere they are used:
|
|
36
|
-
* lowercase slug words joined by single hyphens, bounded length — exactly
|
|
37
|
-
* what the editor produces (see slugifyPreviewId there). No `--` anywhere:
|
|
38
|
-
* that is the pz segment separator, and an id containing it would make the
|
|
39
|
-
* segment undecodable.
|
|
40
|
-
*/
|
|
41
|
-
export const isValidPreviewId = (id: unknown): id is string =>
|
|
42
|
-
typeof id === 'string' &&
|
|
43
|
-
id.length <= 32 &&
|
|
44
|
-
/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(id);
|
|
45
|
-
|
|
46
|
-
/** The slug a widget's copy lives under inside one named preview. */
|
|
47
|
-
export const previewSlug = (slug: string, previewId: string): string =>
|
|
48
|
-
`${slug}${PREVIEW_SLUG_SUFFIX}-${previewId}`;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* True for slugs shaped like a preview copy of something. A HEURISTIC — a
|
|
52
|
-
* live slug can legitimately end this way (a custom page named
|
|
53
|
-
* `summer-preview-2026`), so the fetch layer does not use it to decide
|
|
54
|
-
* whether to overlay; see `isPreviewCopySlug`.
|
|
55
|
-
*/
|
|
56
|
-
export const isPreviewSlug = (slug: string): boolean =>
|
|
57
|
-
/-preview-[a-z0-9-]+$/.test(slug);
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* True when `slug` is already the copy of something inside THIS preview —
|
|
61
|
-
* the exact check the fetch layer needs so preview slugs are never nested,
|
|
62
|
-
* without misreading live slugs that merely look like one.
|
|
63
|
-
*/
|
|
64
|
-
export const isPreviewCopySlug = (slug: string, previewId: string): boolean =>
|
|
65
|
-
previewId !== '' && slug.endsWith(previewSlug('', previewId));
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Resolves the pz segment value from the request — used by settings.js.
|
|
69
|
-
* An invalid or absent cookie yields '' (no preview), which the fetch layer
|
|
70
|
-
* treats as "render live".
|
|
71
|
-
*
|
|
72
|
-
* The id only counts alongside Next's draft-mode cookie: without it the
|
|
73
|
-
* request renders live anyway, and folding an unauthenticated cookie value
|
|
74
|
-
* into the route segment would let any visitor mint a fresh ISR/Redis cache
|
|
75
|
-
* key per request.
|
|
76
|
-
*/
|
|
77
|
-
export const resolvePreviewSegment = (req: {
|
|
78
|
-
cookies: { get: (name: string) => { value: string } | undefined };
|
|
79
|
-
}): string => {
|
|
80
|
-
if (!req.cookies.get(DRAFT_MODE_COOKIE)?.value) return '';
|
|
81
|
-
const value = req.cookies.get(PREVIEW_ID_COOKIE)?.value ?? '';
|
|
82
|
-
return isValidPreviewId(value) ? value : '';
|
|
83
|
-
};
|