@elumixor/devkit 0.12.10 → 0.12.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elumixor/devkit",
3
- "version": "0.12.10",
3
+ "version": "0.12.12",
4
4
  "description": "Dev orchestrator for bun monorepos — free ports, run apps side-by-side, open the browser when ready",
5
5
  "author": "elumixor",
6
6
  "license": "ISC",
@@ -88,7 +88,8 @@ function staticPath(uri, mode) {
88
88
  return "/index.html";
89
89
  }
90
90
 
91
- // The request here is the viewer's own, before the rewrite, so rules match the path the viewer asked for.
91
+ // Rules match the path the viewer asked for. The request here may carry the rewritten static path
92
+ // (`/<project>/<deploy>/...`), so that prefix comes off first.
92
93
  async function withHeaders(event) {
93
94
  var response = event.response;
94
95
  var request = event.request;
@@ -99,8 +100,11 @@ async function withHeaders(event) {
99
100
  if (!route.h) return response;
100
101
  var raw = await routes.get(HEADERS_PREFIX + route.p + "/" + route.d);
101
102
  var rules = JSON.parse(raw);
103
+ var uri = request.uri;
104
+ var rewritten = "/" + route.p + "/" + route.d + "/";
105
+ if (uri.indexOf(rewritten) === 0) uri = uri.slice(rewritten.length - 1);
102
106
  for (var i = 0; i < rules.length; i++) {
103
- if (!new RegExp(rules[i][0]).test(request.uri)) continue;
107
+ if (!new RegExp(rules[i][0]).test(uri)) continue;
104
108
  var names = Object.keys(rules[i][1]);
105
109
  for (var j = 0; j < names.length; j++) response.headers[names[j]] = { value: rules[i][1][names[j]] };
106
110
  }
@@ -0,0 +1,37 @@
1
+ import { existsSync } from "node:fs";
2
+ import { readdir } from "node:fs/promises";
3
+ import { join, resolve } from "node:path";
4
+ import { CloudError } from "./aws.ts";
5
+ import { type Env, parseDotenv } from "./env.ts";
6
+
7
+ const DOTENV_FILES = [".env", ".env.local", ".env.production", ".env.production.local"];
8
+ const TEXT_FILE = /\.(js|mjs|css|html|json|txt|map)$/;
9
+ /** Shorter values (`1`, `true`, a port) appear in any bundle by chance. */
10
+ const MIN_VALUE_LENGTH = 8;
11
+
12
+ /**
13
+ * A laptop's `.env` holds development values (a test Clerk key, a local URL). A deploy builds with the
14
+ * stage's variables, but a bundler that reads `.env` files itself can still bake a local value in, and
15
+ * the result is a production site talking to development services. This refuses such a build.
16
+ */
17
+ export async function assertNoLocalEnvInBuild(staticDir: string, dirs: readonly string[], stageEnv: Env) {
18
+ const leaks = new Map<string, string>();
19
+ for (const dir of dirs)
20
+ for (const name of DOTENV_FILES) {
21
+ const file = join(dir, name);
22
+ if (!existsSync(file)) continue;
23
+ for (const [key, value] of Object.entries(parseDotenv(await Bun.file(file).text())))
24
+ if (key in stageEnv && stageEnv[key] !== value && value.length >= MIN_VALUE_LENGTH) leaks.set(value, key);
25
+ }
26
+ if (!leaks.size || !existsSync(staticDir)) return;
27
+
28
+ for (const entry of await readdir(staticDir, { recursive: true, withFileTypes: true })) {
29
+ if (!entry.isFile() || !TEXT_FILE.test(entry.name)) continue;
30
+ const text = await Bun.file(resolve(entry.parentPath, entry.name)).text();
31
+ for (const [value, key] of leaks)
32
+ if (text.includes(value))
33
+ throw new CloudError(
34
+ `The build has the local .env value of ${key} in ${entry.name}, not the stage's — nothing was uploaded`,
35
+ );
36
+ }
37
+ }
@@ -1,5 +1,7 @@
1
- import { buildWeb } from "../build-targets/web.ts";
1
+ import { join, resolve } from "node:path";
2
+ import { AWS_OUTPUT, buildWeb } from "../build-targets/web.ts";
2
3
  import { readStageEnv } from "../cloud/env.ts";
4
+ import { assertNoLocalEnvInBuild } from "../cloud/env-leak.ts";
3
5
  import { STAGE_BRANCHES } from "../cloud/names.ts";
4
6
  import { type AwsTargetOptions, projectName, resolveStage, Target } from "../cloud/project.ts";
5
7
  import { release } from "../cloud/release.ts";
@@ -19,8 +21,14 @@ export async function deployAws(options: AwsTargetOptions) {
19
21
 
20
22
  // The stage's variables are the build's too, so `VITE_*` values reach the frontend bundle in CI
21
23
  // exactly as they do from a laptop's `.env`.
22
- Object.assign(process.env, await readStageEnv(target));
24
+ const stageEnv = await readStageEnv(target);
25
+ Object.assign(process.env, stageEnv);
23
26
 
24
27
  await buildWeb({ ...options, host: "aws", static: target.isStatic });
28
+ await assertNoLocalEnvInBuild(
29
+ join(root, AWS_OUTPUT, "static"),
30
+ [root, resolve(root, options.frontendDir ?? "apps/frontend")],
31
+ stageEnv,
32
+ );
25
33
  await release(target, { root });
26
34
  }