@marktiderman/genesis-cli 1.10.0 → 1.11.1

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/vite.js CHANGED
@@ -963,7 +963,8 @@ var BUILTIN_RULES = [
963
963
  "migration-rls",
964
964
  "migration-definer-search-path",
965
965
  "migration-edited",
966
- "migration-testing-kit"
966
+ "migration-testing-kit",
967
+ "migration-view-rls"
967
968
  ];
968
969
  var DEFAULT_LOCAL_PRIMITIVE_DIRS = ["src/components/ui"];
969
970
  var STARTER_LEFTOVERS = ["components.json", "src/lib/utils.ts"];
@@ -1792,6 +1793,7 @@ function checkSupabasePack(ctx) {
1792
1793
  checkGrants(ctx, migrations.filter((m) => isNew(m.file)), exposed);
1793
1794
  }
1794
1795
  checkRls(ctx, migrations, exposed);
1796
+ checkViewInvoker(ctx, migrations, exposed);
1795
1797
  checkDefinerSearchPath(ctx, migrations);
1796
1798
  checkTestingKit(ctx, migrations);
1797
1799
  }
@@ -1921,6 +1923,50 @@ function checkRls(ctx, migrations, exposed) {
1921
1923
  });
1922
1924
  }
1923
1925
  }
1926
+ function checkViewInvoker(ctx, migrations, exposed) {
1927
+ const views = /* @__PURE__ */ new Map();
1928
+ const name = String.raw`([\w$.]+)`;
1929
+ const invokerOn = /\bsecurity_invoker\s*(?:=\s*(?:true|on|yes|1|'true'|'on'))?\s*(?:,|\))/;
1930
+ for (const { file, statements } of migrations) {
1931
+ for (const { sql, line } of statements) {
1932
+ const create = new RegExp(String.raw`^create (?:or replace )?(?:temp |temporary )?(?:recursive )?view ${name}`).exec(sql);
1933
+ if (create) {
1934
+ if (/^create (?:or replace )?(?:temp|temporary) /.test(sql)) continue;
1935
+ const options = /^[^(]*?\bwith\s*(\([^)]*\))\s*as\b/.exec(sql)?.[1] ?? "";
1936
+ views.set(qualify(create[1]), { invoker: invokerOn.test(options), file, line });
1937
+ continue;
1938
+ }
1939
+ const alter = new RegExp(String.raw`^alter view (?:if exists )?${name} (set|reset) (\([^)]*\))`).exec(sql);
1940
+ if (alter && /\bsecurity_invoker\b/.test(alter[3])) {
1941
+ const state = views.get(qualify(alter[1]));
1942
+ if (state) state.invoker = alter[2] === "set" && invokerOn.test(alter[3]);
1943
+ continue;
1944
+ }
1945
+ const rename = new RegExp(String.raw`^alter view (?:if exists )?${name} rename to ([\w$]+)$`).exec(sql);
1946
+ if (rename) {
1947
+ const from = qualify(rename[1]);
1948
+ const state = views.get(from);
1949
+ if (state) {
1950
+ views.delete(from);
1951
+ views.set(`${schemaOf(from)}.${rename[2]}`, state);
1952
+ }
1953
+ continue;
1954
+ }
1955
+ const drop = /^drop view (?:if exists )?(.+?)(?: cascade| restrict)?$/.exec(sql);
1956
+ if (drop) for (const n of drop[1].split(",")) views.delete(qualify(n));
1957
+ }
1958
+ }
1959
+ for (const [v, at] of views) {
1960
+ if (at.invoker || !exposed.has(schemaOf(v))) continue;
1961
+ ctx.report({
1962
+ rule: "migration-view-rls",
1963
+ file: at.file,
1964
+ line: at.line,
1965
+ subject: v,
1966
+ message: `${v} runs as its owner, so it reads past row level security; create it "with (security_invoker = true)" or add "alter view ${v} set (security_invoker = true);"`
1967
+ });
1968
+ }
1969
+ }
1924
1970
  function checkDefinerSearchPath(ctx, migrations) {
1925
1971
  const fns = /* @__PURE__ */ new Map();
1926
1972
  const keysFor = (sql, nameEnd, fnName) => {
@@ -2131,6 +2177,7 @@ function satisfies(version, range) {
2131
2177
  // src/doctor/installs.ts
2132
2178
  var GENESIS = /^@marktiderman\/genesis(?:-[\w.-]+)?$/;
2133
2179
  var DEV_TOOLS = /* @__PURE__ */ new Set(["@marktiderman/genesis-cli"]);
2180
+ var NON_SHIPPING = /^(?:@marktiderman\/genesis-cli|vitest|@vitest\/.+|jest|jest-.+|@jest\/.+|ts-jest|babel-jest|mocha|ava|c8|nyc|@playwright\/test|playwright|cypress|@testing-library\/.+|happy-dom|jsdom|msw|@types\/.+|typescript|tsx|ts-node|eslint|eslint-.+|@eslint\/.+|@typescript-eslint\/.+|typescript-eslint|prettier|prettier-.+|@biomejs\/.+|oxlint|vite|@vitejs\/.+|vite-plugin-.+|webpack|webpack-.+|rollup|@rollup\/.+|esbuild|tsup|turbo|@storybook\/.+|storybook|@changesets\/.+|husky|lint-staged|wrangler|knip|concurrently|npm-run-all2?|rimraf|cross-env|dotenv-cli)$/;
2134
2181
  var readManifest = (dir) => {
2135
2182
  try {
2136
2183
  return JSON.parse(readFileSync(path14.join(dir, "package.json"), "utf8"));
@@ -2145,6 +2192,11 @@ var real = (p) => {
2145
2192
  return p;
2146
2193
  }
2147
2194
  };
2195
+ var LOCKFILES = ["bun.lock", "bun.lockb", "package-lock.json", "npm-shrinkwrap.json", "yarn.lock", "pnpm-lock.yaml"];
2196
+ function nodeModulesOf(dir) {
2197
+ const parent = path14.dirname(dir);
2198
+ return path14.basename(parent).startsWith("@") ? path14.dirname(parent) : parent;
2199
+ }
2148
2200
  function packagesIn(nodeModules) {
2149
2201
  if (!existsSync(nodeModules)) return [];
2150
2202
  const out = [];
@@ -2193,24 +2245,41 @@ function genesisInstalls(root) {
2193
2245
  for (const r of roots) visit(r.dir, r.by, r.range);
2194
2246
  while (queue.length) {
2195
2247
  const { real: from, manifest, label } = queue.shift();
2196
- const deps = { ...manifest.peerDependencies, ...manifest.optionalDependencies, ...manifest.dependencies };
2248
+ const deps = { ...manifest.optionalDependencies, ...manifest.dependencies };
2197
2249
  for (const [name, range] of Object.entries(deps)) {
2198
2250
  const dir = findPackageDir(from, name);
2199
2251
  if (dir) visit(dir, label, range);
2200
2252
  }
2253
+ for (const [name, range] of Object.entries(manifest.peerDependencies ?? {})) {
2254
+ if (name in deps) continue;
2255
+ const dir = peerOf(from, name);
2256
+ if (dir) visit(dir, label, range);
2257
+ }
2201
2258
  }
2202
2259
  };
2260
+ const peerOf = (from, name) => {
2261
+ const dir = findPackageDir(from, name);
2262
+ if (!dir) return null;
2263
+ if (!ownInstall || name in declared) return dir;
2264
+ const realDir = real(dir);
2265
+ if (realDir.startsWith(realRoot + path14.sep)) return dir;
2266
+ return nodeModulesOf(realDir) === nodeModulesOf(from) ? dir : null;
2267
+ };
2203
2268
  const rootsOf = (deps, by) => Object.entries(deps).flatMap(([name, range]) => {
2204
2269
  const dir = findPackageDir(root, name);
2205
2270
  return dir ? [{ dir, by, ...range ? { range } : {} }] : [];
2206
2271
  });
2207
2272
  const app = readManifest(root);
2208
- let runtimeDeps = { ...app?.peerDependencies, ...app?.optionalDependencies, ...app?.dependencies };
2209
- let devDeps = Object.fromEntries(Object.entries(app?.devDependencies ?? {}).filter(([n]) => !(n in runtimeDeps)));
2210
- if (Object.keys(runtimeDeps).length === 0) {
2211
- runtimeDeps = Object.fromEntries(Object.entries(devDeps).filter(([n]) => !DEV_TOOLS.has(n)));
2212
- devDeps = Object.fromEntries(Object.entries(devDeps).filter(([n]) => DEV_TOOLS.has(n)));
2213
- }
2273
+ const ownInstall = LOCKFILES.some((f) => existsSync(path14.join(root, f)));
2274
+ const declared = { ...app?.devDependencies, ...app?.peerDependencies, ...app?.optionalDependencies, ...app?.dependencies };
2275
+ const declaredDev = Object.entries(app?.devDependencies ?? {});
2276
+ const runtimeDeps = {
2277
+ ...Object.fromEntries(declaredDev.filter(([n]) => !NON_SHIPPING.test(n))),
2278
+ ...app?.peerDependencies,
2279
+ ...app?.optionalDependencies,
2280
+ ...app?.dependencies
2281
+ };
2282
+ const devDeps = Object.fromEntries(declaredDev.filter(([n]) => NON_SHIPPING.test(n) && !(n in runtimeDeps)));
2214
2283
  walk("runtime", rootsOf(runtimeDeps, "the app"));
2215
2284
  const top = path14.join(root, "node_modules");
2216
2285
  walk(
@@ -2221,7 +2290,7 @@ function genesisInstalls(root) {
2221
2290
  }).map((dir) => ({ dir, by: "hoisted, not a direct dependency" }))
2222
2291
  );
2223
2292
  const devRoots = { ...devDeps };
2224
- for (const tool of DEV_TOOLS) if (!(tool in devRoots)) devRoots[tool] = runtimeDeps[tool];
2293
+ for (const tool of DEV_TOOLS) if (!(tool in devRoots) && tool in runtimeDeps) devRoots[tool] = runtimeDeps[tool];
2225
2294
  walk("dev", rootsOf(devRoots, "the app (dev)"));
2226
2295
  for (const [key2, copy] of copies) {
2227
2296
  const kinds = via.get(key2);
@@ -2293,11 +2362,21 @@ function stripForImports(code) {
2293
2362
  const templates = [];
2294
2363
  let depth = 0;
2295
2364
  let i = 0;
2365
+ let sure = true;
2296
2366
  const isSpecifier = () => /(?:\bfrom|(?:^|[;{}\n])\s*import|\bimport\s*\(|\brequire\s*\()\s*$/.test(out.slice(-40).join(""));
2297
- const opensString = () => {
2298
- const before = out.slice(-12).join("");
2299
- if (!/[\w$]$/.test(before)) return true;
2300
- return /(?:^|[^\w$])(?:return|from|import|case|typeof|in|of|else|yield|await|export|default|void|require)$/.test(before);
2367
+ const before = () => out.slice(-24).join("");
2368
+ const KEYWORD = /(?:^|[^\w$.])(?:return|typeof|instanceof|case|do|else|in|of|new|delete|void|throw|yield|await|from|import|export|default|require)\s*$/;
2369
+ const startsExpression = (b) => {
2370
+ const t = b.replace(/\s+$/, "");
2371
+ if (!t) return true;
2372
+ if (KEYWORD.test(b)) return true;
2373
+ return /[([{,;:=!&|?+\-*%<>~^}]$/.test(t);
2374
+ };
2375
+ const exprStart = () => startsExpression(before());
2376
+ const taggedTemplate = () => {
2377
+ const b = out.slice(-48).join("");
2378
+ const m = /([A-Za-z_$][\w$.]*)$/.exec(b);
2379
+ return m !== null && startsExpression(b.slice(0, b.length - m[1].length));
2301
2380
  };
2302
2381
  const templateBody = () => {
2303
2382
  while (i < n) {
@@ -2320,8 +2399,9 @@ function stripForImports(code) {
2320
2399
  i++;
2321
2400
  }
2322
2401
  }
2402
+ sure = false;
2323
2403
  };
2324
- while (i < n) {
2404
+ while (i < n && sure) {
2325
2405
  const ch = code[i];
2326
2406
  const next = code[i + 1];
2327
2407
  if (ch === "/" && next === "/") {
@@ -2340,7 +2420,45 @@ function stripForImports(code) {
2340
2420
  out.push(" ");
2341
2421
  i += 2;
2342
2422
  }
2343
- } else if ((ch === '"' || ch === "'") && opensString()) {
2423
+ } else if (ch === "/" && next !== ">" && code[i - 1] !== "<" && exprStart()) {
2424
+ const afterBrace = /[}>]\s*$/.test(before());
2425
+ const start = i;
2426
+ const mark = out.length;
2427
+ out.push("/");
2428
+ i++;
2429
+ let inClass = false;
2430
+ for (; ; ) {
2431
+ const c = code[i];
2432
+ if (c === void 0 || c === "\n") {
2433
+ if (afterBrace) {
2434
+ out.length = mark;
2435
+ out.push("/");
2436
+ i = start + 1;
2437
+ } else sure = false;
2438
+ break;
2439
+ }
2440
+ if (c === "\\") {
2441
+ out.push(" ", blank(code[i + 1] ?? ""));
2442
+ i += 2;
2443
+ continue;
2444
+ }
2445
+ i++;
2446
+ if (c === "[") inClass = true;
2447
+ else if (c === "]") inClass = false;
2448
+ else if (c === "/" && !inClass) {
2449
+ if (afterBrace && /[<>]/.test(code.slice(start + 1, i - 1))) {
2450
+ out.length = mark;
2451
+ out.push("/");
2452
+ i = start + 1;
2453
+ break;
2454
+ }
2455
+ out.push("/");
2456
+ break;
2457
+ }
2458
+ out.push(" ");
2459
+ }
2460
+ if (out.length > mark + 1 || i > start + 1) while (i < n && /[a-z]/.test(code[i])) out.push(code[i++]);
2461
+ } else if ((ch === '"' || ch === "'") && exprStart()) {
2344
2462
  const keep = isSpecifier();
2345
2463
  out.push(ch);
2346
2464
  i++;
@@ -2353,11 +2471,12 @@ function stripForImports(code) {
2353
2471
  i++;
2354
2472
  }
2355
2473
  }
2356
- if (i < n) {
2474
+ if (code[i] !== ch) sure = false;
2475
+ else {
2357
2476
  out.push(code[i]);
2358
2477
  i++;
2359
2478
  }
2360
- } else if (ch === "`") {
2479
+ } else if (ch === "`" && (exprStart() || taggedTemplate())) {
2361
2480
  out.push("`");
2362
2481
  i++;
2363
2482
  templateBody();
@@ -2374,7 +2493,7 @@ function stripForImports(code) {
2374
2493
  i++;
2375
2494
  }
2376
2495
  }
2377
- return out.join("");
2496
+ return sure && templates.length === 0 ? out.join("") : null;
2378
2497
  }
2379
2498
  function bindings(clause) {
2380
2499
  const out = [];
@@ -2488,6 +2607,11 @@ function createTrail(ctx) {
2488
2607
  const files = [];
2489
2608
  let lost = false;
2490
2609
  const code = stripped(file);
2610
+ if (code === null) {
2611
+ const out2 = { files: [], lost: true };
2612
+ importsCache.set(file.absolutePath, out2);
2613
+ return out2;
2614
+ }
2491
2615
  const used = new Set(renderedElements(code).map((e) => e.split(".")[0]));
2492
2616
  for (const m of code.matchAll(/\b\w*[cC]omponent\s*:\s*([A-Za-z_$][\w$]*)/g)) used.add(m[1]);
2493
2617
  for (const m of code.matchAll(/\bexport\s+default\s+([A-Za-z_$][\w$]*)/g)) used.add(m[1]);
@@ -2524,20 +2648,23 @@ function createTrail(ctx) {
2524
2648
  };
2525
2649
  const strippedCache = /* @__PURE__ */ new Map();
2526
2650
  const stripped = (file) => {
2527
- let c = strippedCache.get(file.absolutePath);
2528
- if (c === void 0) strippedCache.set(file.absolutePath, c = stripForImports(file.code));
2529
- return c;
2651
+ if (!strippedCache.has(file.absolutePath)) strippedCache.set(file.absolutePath, stripForImports(file.code));
2652
+ return strippedCache.get(file.absolutePath);
2530
2653
  };
2531
2654
  const memo = /* @__PURE__ */ new WeakMap();
2532
2655
  const reach = (file, test2, depth, seen) => {
2533
2656
  let cache = memo.get(test2);
2534
- if (!cache) memo.set(test2, cache = /* @__PURE__ */ new Map());
2657
+ if (!cache) memo.set(test2, cache = { free: /* @__PURE__ */ new Map(), cut: /* @__PURE__ */ new Map() });
2535
2658
  const k = `${file.absolutePath}\0${depth}`;
2536
- const cached = cache.get(k);
2537
- if (cached) return [cached, false];
2659
+ const free = cache.free.get(k);
2660
+ if (free) return [free, /* @__PURE__ */ new Set()];
2661
+ const reusable = cache.cut.get(k)?.find((e) => e.cut.every((m) => seen.has(m)));
2662
+ if (reusable) return [reusable.result, new Set(reusable.cut)];
2538
2663
  let result;
2539
- let cut = false;
2540
- if (test2(stripped(file))) result = "yes";
2664
+ const cut = /* @__PURE__ */ new Set();
2665
+ const code = stripped(file);
2666
+ if (code === null) result = "unknown";
2667
+ else if (test2(code)) result = "yes";
2541
2668
  else {
2542
2669
  const { files, lost } = importsOf(file);
2543
2670
  let unknown = lost && depth > 0;
@@ -2546,7 +2673,7 @@ function createTrail(ctx) {
2546
2673
  seen.add(file.absolutePath);
2547
2674
  for (const abs of files) {
2548
2675
  if (seen.has(abs)) {
2549
- cut = true;
2676
+ cut.add(abs);
2550
2677
  continue;
2551
2678
  }
2552
2679
  const target = read(abs);
@@ -2555,7 +2682,7 @@ function createTrail(ctx) {
2555
2682
  continue;
2556
2683
  }
2557
2684
  const [r, c] = reach(target, test2, depth - 1, seen);
2558
- if (c) cut = true;
2685
+ for (const m of c) cut.add(m);
2559
2686
  if (r === "yes") {
2560
2687
  result = "yes";
2561
2688
  break;
@@ -2563,11 +2690,18 @@ function createTrail(ctx) {
2563
2690
  if (r === "unknown") unknown = true;
2564
2691
  }
2565
2692
  seen.delete(file.absolutePath);
2693
+ cut.delete(file.absolutePath);
2566
2694
  }
2567
2695
  if (result === "no" && unknown) result = "unknown";
2568
2696
  }
2569
- if (result === "yes" || !cut) cache.set(k, result);
2570
- return [result, cut && result !== "yes"];
2697
+ if (result === "yes" || cut.size === 0) {
2698
+ cache.free.set(k, result);
2699
+ return [result, /* @__PURE__ */ new Set()];
2700
+ }
2701
+ const entries = cache.cut.get(k) ?? [];
2702
+ entries.push({ cut: [...cut], result });
2703
+ cache.cut.set(k, entries);
2704
+ return [result, cut];
2571
2705
  };
2572
2706
  return { reach: (file, test2, depth = TRAIL_DEPTH) => reach(file, test2, depth, /* @__PURE__ */ new Set())[0] };
2573
2707
  }