@gradial/aci 0.1.18 → 0.1.19
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/next/middleware.js +4 -35
- package/dist/next/preview-mode.js +3 -8
- package/dist/next/preview.js +4 -18
- package/package.json +1 -1
package/dist/next/middleware.js
CHANGED
|
@@ -7,15 +7,8 @@ const DISPATCH_HEADER = 'x-gradial-dispatch';
|
|
|
7
7
|
export function createGradialMiddleware(config) {
|
|
8
8
|
return async function gradialMiddleware(request) {
|
|
9
9
|
const url = request.nextUrl;
|
|
10
|
-
console.log('[gradial/mw] === middleware start ===');
|
|
11
|
-
console.log('[gradial/mw] path:', url.pathname, 'search:', url.search.slice(0, 100));
|
|
12
|
-
console.log('[gradial/mw] siteId:', config.siteId);
|
|
13
|
-
console.log('[gradial/mw] EDGE_CONFIG:', process.env.EDGE_CONFIG ? 'set' : 'NOT SET');
|
|
14
|
-
console.log('[gradial/mw] GRADIAL_PREVIEW_SIGN_KEY:', process.env.GRADIAL_PREVIEW_SIGN_KEY ? `set (${process.env.GRADIAL_PREVIEW_SIGN_KEY.length} chars, prefix: ${process.env.GRADIAL_PREVIEW_SIGN_KEY.slice(0, 6)})` : 'NOT SET');
|
|
15
|
-
console.log('[gradial/mw] VERCEL_DEPLOYMENT_ID:', process.env.VERCEL_DEPLOYMENT_ID || 'NOT SET');
|
|
16
10
|
// ?leave_preview=1 → clear cookies, redirect to clean URL
|
|
17
11
|
if (url.searchParams.get('leave_preview') === '1') {
|
|
18
|
-
console.log('[gradial/mw] leave_preview=1, clearing cookies and redirecting');
|
|
19
12
|
const cleanUrl = new URL(url.pathname, url.origin);
|
|
20
13
|
const response = NextResponse.redirect(cleanUrl);
|
|
21
14
|
response.cookies.delete('aci_preview');
|
|
@@ -26,11 +19,8 @@ export function createGradialMiddleware(config) {
|
|
|
26
19
|
// Try preview token first; if valid, use that release.
|
|
27
20
|
// If no token or invalid, fall through to active release from edge config.
|
|
28
21
|
const previewReleaseId = await releaseIdFromPreviewToken(request, config);
|
|
29
|
-
console.log('[gradial/mw] previewReleaseId from token:', previewReleaseId || '(none)');
|
|
30
22
|
const releaseId = previewReleaseId || await activeReleaseFromEdgeConfig(config);
|
|
31
|
-
console.log('[gradial/mw] final releaseId:', releaseId || '(none)');
|
|
32
23
|
if (!releaseId) {
|
|
33
|
-
console.log('[gradial/mw] no releaseId, passing through with no headers');
|
|
34
24
|
return NextResponse.next({ request: { headers: requestHeaders } });
|
|
35
25
|
}
|
|
36
26
|
requestHeaders.set(RELEASE_HEADER, releaseId);
|
|
@@ -38,16 +28,11 @@ export function createGradialMiddleware(config) {
|
|
|
38
28
|
requestHeaders.set(DISPATCH_HEADER, 'ssr-page');
|
|
39
29
|
if (previewReleaseId) {
|
|
40
30
|
requestHeaders.set('x-gradial-preview', '1');
|
|
41
|
-
console.log('[gradial/mw] set x-gradial-preview=1 and x-gradial-release-id:', releaseId);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
console.log('[gradial/mw] set x-gradial-release-id (active):', releaseId);
|
|
45
31
|
}
|
|
46
32
|
const response = NextResponse.next({ request: { headers: requestHeaders } });
|
|
47
33
|
// Persist preview token as cookie so navigation stays in preview
|
|
48
34
|
const queryToken = url.searchParams.get('preview_token');
|
|
49
35
|
if (queryToken && previewReleaseId) {
|
|
50
|
-
console.log('[gradial/mw] setting preview cookies for release:', previewReleaseId);
|
|
51
36
|
response.cookies.set('aci_preview', queryToken, {
|
|
52
37
|
httpOnly: true,
|
|
53
38
|
sameSite: 'lax',
|
|
@@ -61,7 +46,6 @@ export function createGradialMiddleware(config) {
|
|
|
61
46
|
path: '/',
|
|
62
47
|
});
|
|
63
48
|
}
|
|
64
|
-
console.log('[gradial/mw] === middleware end ===');
|
|
65
49
|
return response;
|
|
66
50
|
};
|
|
67
51
|
}
|
|
@@ -74,19 +58,15 @@ function gradialHeaders(headers) {
|
|
|
74
58
|
}
|
|
75
59
|
async function activeReleaseFromEdgeConfig(config) {
|
|
76
60
|
const edgeConfig = config.edgeConfig || process.env.EDGE_CONFIG || '';
|
|
77
|
-
if (!edgeConfig || !config.siteId)
|
|
78
|
-
console.log('[gradial/mw] activeRelease: no edgeConfig or siteId, skipping');
|
|
61
|
+
if (!edgeConfig || !config.siteId)
|
|
79
62
|
return '';
|
|
80
|
-
}
|
|
81
63
|
const active = await activeReleasePointer(edgeConfig, config.siteId);
|
|
82
|
-
console.log('[gradial/mw] activeRelease pointer:', JSON.stringify(active));
|
|
83
64
|
if (!active.releaseId)
|
|
84
65
|
return '';
|
|
85
66
|
const deploymentId = config.deploymentId || process.env.VERCEL_DEPLOYMENT_ID || '';
|
|
86
67
|
if (!deploymentId)
|
|
87
68
|
return active.releaseId;
|
|
88
69
|
const compatibility = await getEdgeConfigJSON(edgeConfig, deploymentCompatibilityKey(config.siteId, deploymentId));
|
|
89
|
-
console.log('[gradial/mw] compatibility:', compatibility ? JSON.stringify(compatibility) : '(none)');
|
|
90
70
|
if (!compatibility)
|
|
91
71
|
return active.releaseId;
|
|
92
72
|
return clampRelease(active.releaseId, active.sequence || 0, compatibility);
|
|
@@ -148,7 +128,6 @@ function edgeKeySegment(value) {
|
|
|
148
128
|
}
|
|
149
129
|
async function releaseIdFromPreviewToken(request, config) {
|
|
150
130
|
const token = previewTokenFromRequest(request);
|
|
151
|
-
console.log('[gradial/mw] previewToken: token found:', !!token, 'len:', token.length);
|
|
152
131
|
if (!token)
|
|
153
132
|
return '';
|
|
154
133
|
const signKey = process.env.GRADIAL_PREVIEW_SIGN_KEY || '';
|
|
@@ -156,23 +135,13 @@ async function releaseIdFromPreviewToken(request, config) {
|
|
|
156
135
|
console.warn('[gradial/mw] GRADIAL_PREVIEW_SIGN_KEY not set — preview tokens will be ignored');
|
|
157
136
|
return '';
|
|
158
137
|
}
|
|
159
|
-
console.log('[gradial/mw] signKey present, len:', signKey.length, 'prefix:', signKey.slice(0, 6));
|
|
160
|
-
console.log('[gradial/mw] calling validatePreviewToken...');
|
|
161
138
|
const claims = await validatePreviewToken(token, signKey);
|
|
162
|
-
|
|
163
|
-
if (!claims || claims.scope !== 'preview' || !claims.releaseId) {
|
|
164
|
-
console.log('[gradial/mw] claims invalid: scope=', claims?.scope, 'releaseId=', claims?.releaseId);
|
|
139
|
+
if (!claims || claims.scope !== 'preview' || !claims.releaseId)
|
|
165
140
|
return '';
|
|
166
|
-
|
|
167
|
-
if (claims.siteId && config.siteId && claims.siteId !== config.siteId) {
|
|
168
|
-
console.log('[gradial/mw] siteId mismatch: token=', claims.siteId, 'config=', config.siteId);
|
|
141
|
+
if (claims.siteId && config.siteId && claims.siteId !== config.siteId)
|
|
169
142
|
return '';
|
|
170
|
-
|
|
171
|
-
if (!claims.expiresAt || Date.parse(claims.expiresAt) <= Date.now()) {
|
|
172
|
-
console.log('[gradial/mw] token expired or no expiresAt:', claims.expiresAt);
|
|
143
|
+
if (!claims.expiresAt || Date.parse(claims.expiresAt) <= Date.now())
|
|
173
144
|
return '';
|
|
174
|
-
}
|
|
175
|
-
console.log('[gradial/mw] preview token valid! releaseId:', claims.releaseId);
|
|
176
145
|
return claims.releaseId;
|
|
177
146
|
}
|
|
178
147
|
function previewTokenFromRequest(request) {
|
|
@@ -4,15 +4,10 @@ const PREVIEW_HEADER = 'x-gradial-preview';
|
|
|
4
4
|
export async function isPreviewMode() {
|
|
5
5
|
try {
|
|
6
6
|
const current = await headers();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
console.log('[gradial/server] isPreviewMode: x-gradial-preview=', previewFlag, 'x-gradial-release-id=', releaseId || '(none)');
|
|
10
|
-
if (previewFlag === '1') {
|
|
11
|
-
return releaseId || '';
|
|
7
|
+
if (current.get(PREVIEW_HEADER) === '1') {
|
|
8
|
+
return current.get(RELEASE_HEADER) || '';
|
|
12
9
|
}
|
|
13
10
|
}
|
|
14
|
-
catch
|
|
15
|
-
console.log('[gradial/server] isPreviewMode error:', String(err));
|
|
16
|
-
}
|
|
11
|
+
catch { }
|
|
17
12
|
return '';
|
|
18
13
|
}
|
package/dist/next/preview.js
CHANGED
|
@@ -1,32 +1,18 @@
|
|
|
1
1
|
export async function validatePreviewToken(token, signKey) {
|
|
2
2
|
try {
|
|
3
|
-
console.log('[gradial/preview] validatePreviewToken start, token len:', token.length, 'key len:', signKey.length);
|
|
4
3
|
const parts = token.split('.');
|
|
5
|
-
|
|
6
|
-
if (parts.length !== 3) {
|
|
7
|
-
console.log('[gradial/preview] invalid token structure, parts:', parts.length);
|
|
4
|
+
if (parts.length !== 3)
|
|
8
5
|
return null;
|
|
9
|
-
}
|
|
10
6
|
const signingInput = `${parts[0]}.${parts[1]}`;
|
|
11
7
|
const signature = base64URLToBytes(parts[2]);
|
|
12
|
-
console.log('[gradial/preview] signature bytes:', signature.length);
|
|
13
8
|
const key = await crypto.subtle.importKey('raw', new TextEncoder().encode(signKey), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
14
9
|
const expected = new Uint8Array(await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(signingInput)));
|
|
15
|
-
|
|
16
|
-
console.log('[gradial/preview] sig match:', constantTimeEqual(signature, expected));
|
|
17
|
-
if (!constantTimeEqual(signature, expected)) {
|
|
18
|
-
console.log('[gradial/preview] signature mismatch!');
|
|
19
|
-
console.log('[gradial/preview] got sig:', Array.from(signature.slice(0, 8)).join(','));
|
|
20
|
-
console.log('[gradial/preview] exp sig:', Array.from(expected.slice(0, 8)).join(','));
|
|
10
|
+
if (!constantTimeEqual(signature, expected))
|
|
21
11
|
return null;
|
|
22
|
-
}
|
|
23
12
|
const payload = new TextDecoder().decode(base64URLToBytes(parts[1]));
|
|
24
|
-
|
|
25
|
-
console.log('[gradial/preview] valid token, claims:', JSON.stringify(claims));
|
|
26
|
-
return claims;
|
|
13
|
+
return JSON.parse(payload);
|
|
27
14
|
}
|
|
28
|
-
catch
|
|
29
|
-
console.log('[gradial/preview] validation error:', String(err));
|
|
15
|
+
catch {
|
|
30
16
|
return null;
|
|
31
17
|
}
|
|
32
18
|
}
|