@gradial/aci 0.1.35 → 0.1.36

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.
@@ -1,6 +1,13 @@
1
1
  import { type NextRequest } from 'next/server.js';
2
2
  export interface GradialMiddlewareConfig {
3
- siteId: string;
3
+ /**
4
+ * Site identity for this deployment. Env-injected identity is valid —
5
+ * the platform sets ACI_SITE_ID per deployment, so passing a bare
6
+ * `process.env.ACI_SITE_ID` (which types as `string | undefined`) is
7
+ * supported. A missing value 500s cleanly at runtime
8
+ * ("ACI site id is not configured").
9
+ */
10
+ siteId: string | undefined;
4
11
  edgeConfig?: string;
5
12
  previewSignKey?: string;
6
13
  deploymentId?: string;
@@ -186,7 +186,7 @@ async function activeReleaseFromEdgeConfig(config) {
186
186
  return clampRelease(active.releaseId, active.sequence || 0, compatibility);
187
187
  }
188
188
  function siteIdForConfig(config) {
189
- return config.siteId;
189
+ return config.siteId ?? '';
190
190
  }
191
191
  async function getEdgeConfigJSON(edgeConfig, key) {
192
192
  const value = await getEdgeConfigValue(edgeConfig, key);
@@ -89,6 +89,19 @@ function walkFiles(dir, predicate) {
89
89
  // ---------------------------------------------------------------------------
90
90
  export function checkConfig(ctx) {
91
91
  const results = [];
92
+ // Env-only site identity: in siteIdMode 'env' the platform owns tenant
93
+ // identity (terraform injects ACI_SITE_ID per deployment), so .aci.yaml
94
+ // must not carry a siteId at all — a leftover label pins the repo to one
95
+ // site and drifts silently from the deployed identity.
96
+ if (ctx.siteIdMode === 'env') {
97
+ const aciYaml = readTextFile(join(ctx.rootDir, '.aci.yaml'));
98
+ if (aciYaml !== null) {
99
+ const pinsSiteId = /^siteId\s*:/m.test(aciYaml);
100
+ results.push(!pinsSiteId
101
+ ? ok('config', '.aci.yaml does not pin a siteId (env-only identity)')
102
+ : fail('config', '.aci.yaml does not pin a siteId (env-only identity)', '.aci.yaml declares siteId but this repo uses siteIdMode "env": the platform owns site identity (ACI_SITE_ID is injected per deployment), so the manifest must not pin a site — delete the siteId line'));
103
+ }
104
+ }
92
105
  if (ctx.framework === 'next') {
93
106
  const configFile = findFirstExisting(ctx.rootDir, [
94
107
  'next.config.ts',
@@ -242,13 +255,65 @@ export function checkMiddleware(ctx) {
242
255
  results.push(usesSDKMiddleware
243
256
  ? ok('middleware', 'middleware uses createGradialMiddleware', rel)
244
257
  : fail('middleware', 'middleware uses createGradialMiddleware', rel + ' should use createGradialMiddleware from @gradial/aci/next/middleware'));
245
- // Check if siteId is configured as a literal (no env var indirection)
246
- if (ctx.siteId) {
247
- const hasSiteIdLiteral = middlewareFile.content.includes(`siteId`) &&
248
- !middlewareFile.content.includes('process.env');
249
- results.push(hasSiteIdLiteral
250
- ? ok('middleware', 'middleware configures siteId (literal)', rel)
251
- : fail('middleware', 'middleware configures siteId (literal)', rel + ' should pass a literal siteId string to createGradialMiddleware (no env var indirection)'));
258
+ // Check that the siteId passed to createGradialMiddleware is canonical.
259
+ // Accepted forms depend on ctx.siteIdMode (default 'literal'):
260
+ //
261
+ // 'literal':
262
+ // (a) a string literal exactly equal to ctx.siteId (the tenant's
263
+ // canonical site id) — this is what the template stamps;
264
+ // (b) process.env.ACI_SITE_ID || '<literal>' (or ?? instead of ||)
265
+ // where the literal default is exactly ctx.siteId — the
266
+ // multi-tenant/dev-mirror form: the deployment env supplies the
267
+ // per-tenant identity, the default keeps local dev correct.
268
+ //
269
+ // 'env' (tenant-agnostic repos deployed to multiple sites): the ONLY
270
+ // accepted form is exactly `siteId: process.env.ACI_SITE_ID`. The
271
+ // platform owns tenant identity — terraform sets ACI_SITE_ID on every
272
+ // deployment — and the runtime 500s cleanly if it is missing, so any
273
+ // literal, fallback (|| / ??), other env var name, or computed
274
+ // expression fails.
275
+ //
276
+ // Anything else (placeholder literal, wrong/missing default, other env
277
+ // var names, computed expressions) fails: a wrong siteId means the edge
278
+ // release-pointer key never matches and every live route 503s.
279
+ const siteIdMode = ctx.siteIdMode ?? 'literal';
280
+ if (siteIdMode === 'env') {
281
+ const label = 'middleware configures canonical siteId';
282
+ // Narrow the search to the createGradialMiddleware call when present so
283
+ // an unrelated siteId reference elsewhere in the file can't satisfy us.
284
+ const callIdx = middlewareFile.content.indexOf('createGradialMiddleware(');
285
+ const region = callIdx >= 0 ? middlewareFile.content.slice(callIdx) : middlewareFile.content;
286
+ // Exactly `siteId: process.env.ACI_SITE_ID` followed by the end of the
287
+ // property (`,` or `}`) — same object-property shape the literal-mode
288
+ // regexes target, but with nothing allowed after the env access.
289
+ const bareEnv = /siteId\s*:\s*process\.env\.ACI_SITE_ID\s*(?=[,}])/.test(region);
290
+ results.push(bareEnv
291
+ ? ok('middleware', label, `${rel} (env-only siteId)`)
292
+ : fail('middleware', label, `${rel}: middleware must read siteId only from process.env.ACI_SITE_ID (no literal or fallback) — the platform owns tenant identity and injects ACI_SITE_ID per deployment`));
293
+ }
294
+ else if (ctx.siteId) {
295
+ const label = 'middleware configures canonical siteId';
296
+ const expected = ctx.siteId;
297
+ const acceptedForms = `accepted forms: siteId: '${expected}' or ` +
298
+ `siteId: process.env.ACI_SITE_ID || '${expected}' (?? also accepted)`;
299
+ // Narrow the search to the createGradialMiddleware call when present so
300
+ // an unrelated siteId reference elsewhere in the file can't satisfy us.
301
+ const callIdx = middlewareFile.content.indexOf('createGradialMiddleware(');
302
+ const region = callIdx >= 0 ? middlewareFile.content.slice(callIdx) : middlewareFile.content;
303
+ // Form (b): process.env.ACI_SITE_ID || '<default>' (or ??).
304
+ const envMatch = region.match(/siteId\s*:\s*process\.env\.ACI_SITE_ID\s*(?:\|\||\?\?)\s*(['"`])((?:(?!\1).)*)\1/);
305
+ // Form (a): bare string literal (single/double quotes or backticks).
306
+ const literalMatch = region.match(/siteId\s*:\s*(['"`])((?:(?!\1).)*)\1/);
307
+ const configured = envMatch ? envMatch[2] : literalMatch ? literalMatch[2] : null;
308
+ if (configured === expected) {
309
+ results.push(ok('middleware', label, `${rel} (${envMatch ? 'env with canonical default' : 'canonical literal'})`));
310
+ }
311
+ else if (configured !== null) {
312
+ results.push(fail('middleware', label, `${rel}: siteId ${envMatch ? 'default ' : ''}is '${configured}' but the canonical site id is '${expected}'; ${acceptedForms}`));
313
+ }
314
+ else {
315
+ results.push(fail('middleware', label, `${rel}: siteId passed to createGradialMiddleware is not an accepted form (placeholder/computed expressions, other env vars, and process.env.ACI_SITE_ID without a literal default all fail); ${acceptedForms}`));
316
+ }
252
317
  }
253
318
  // Check that the middleware matcher is inlined (not imported) and matches
254
319
  // the SDK's canonical value. Next.js requires config.matcher to be
@@ -58,6 +58,7 @@ export function runConformanceChecks(options) {
58
58
  framework: options.framework,
59
59
  siteId: options.siteId,
60
60
  rootDir: options.rootDir || process.cwd(),
61
+ siteIdMode: options.siteIdMode || 'literal',
61
62
  };
62
63
  const checks = resolveChecks(options.checks);
63
64
  const allResults = checks.flatMap((name) => {
@@ -84,6 +85,7 @@ export function conformancePreset(options) {
84
85
  framework: options.framework,
85
86
  siteId: options.siteId,
86
87
  rootDir: options.rootDir || process.cwd(),
88
+ siteIdMode: options.siteIdMode || 'literal',
87
89
  };
88
90
  for (const checkName of checks) {
89
91
  await t.test(checkName, () => {
@@ -16,6 +16,21 @@ export interface ConformancePresetOptions {
16
16
  siteId: string;
17
17
  /** Root directory of the site project. Defaults to process.cwd(). */
18
18
  rootDir?: string;
19
+ /**
20
+ * How the middleware is expected to obtain its siteId. Defaults to 'literal'.
21
+ *
22
+ * - 'literal': the middleware passes the canonical siteId as a string
23
+ * literal (or `process.env.ACI_SITE_ID || '<canonical>'` / `??` with the
24
+ * canonical literal as the default). This is what the template stamps for
25
+ * single-tenant repos.
26
+ * - 'env': the middleware must read siteId ONLY from the environment —
27
+ * exactly `siteId: process.env.ACI_SITE_ID`, with no literal default, no
28
+ * `||`/`??` fallback, no other env var names, and no computed
29
+ * expressions. Use this for tenant-agnostic repos deployed to multiple
30
+ * sites (the platform owns tenant identity and injects ACI_SITE_ID per
31
+ * deployment; a missing env produces a clean 500 at runtime).
32
+ */
33
+ siteIdMode?: 'literal' | 'env';
19
34
  /**
20
35
  * Which checks to run. Defaults to all checks.
21
36
  * Pass an array to run only those checks, or an object to enable/disable individually.
@@ -42,4 +57,6 @@ export interface ConformanceContext {
42
57
  framework: ConformanceFramework;
43
58
  siteId: string;
44
59
  rootDir: string;
60
+ /** See ConformancePresetOptions.siteIdMode. Defaults to 'literal'. */
61
+ siteIdMode?: 'literal' | 'env';
45
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradial/aci",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -127,12 +127,12 @@
127
127
  "zod": "^4.0.0"
128
128
  },
129
129
  "optionalDependencies": {
130
- "@gradial/aci-cli-darwin-arm64": "0.1.35",
131
- "@gradial/aci-cli-darwin-x64": "0.1.35",
132
- "@gradial/aci-cli-linux-arm64": "0.1.35",
133
- "@gradial/aci-cli-linux-x64": "0.1.35",
134
- "@gradial/aci-cli-win32-arm64": "0.1.35",
135
- "@gradial/aci-cli-win32-x64": "0.1.35"
130
+ "@gradial/aci-cli-darwin-arm64": "0.1.36",
131
+ "@gradial/aci-cli-darwin-x64": "0.1.36",
132
+ "@gradial/aci-cli-linux-arm64": "0.1.36",
133
+ "@gradial/aci-cli-linux-x64": "0.1.36",
134
+ "@gradial/aci-cli-win32-arm64": "0.1.36",
135
+ "@gradial/aci-cli-win32-x64": "0.1.36"
136
136
  },
137
137
  "scripts": {
138
138
  "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.json",