@gradial/aci 0.1.14 → 0.1.15

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