Skip to content

Commit 60034b9

Browse files
committed
fix: inline dotenv parse to avoid its node deps leaking to the client
1 parent 752e49d commit 60034b9

File tree

1 file changed

+44
-2
lines changed
  • apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.v3.$projectParam.environment-variables.new

1 file changed

+44
-2
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.v3.$projectParam.environment-variables.new/route.tsx

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { cn } from "~/utils/cn";
3838
import { ProjectParamSchema, v3BillingPath, v3EnvironmentVariablesPath } from "~/utils/pathBuilder";
3939
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
4040
import { EnvironmentVariableKey } from "~/v3/environmentVariables/repository";
41-
import dotenv from "dotenv";
4241
import { Paragraph } from "~/components/primitives/Paragraph";
4342
import { TextLink } from "~/components/primitives/TextLink";
4443
import {
@@ -48,6 +47,49 @@ import {
4847
TooltipTrigger,
4948
} from "~/components/primitives/Tooltip";
5049

50+
// https://github.com/motdotla/dotenv/blob/master/lib/main.js
51+
// dotenv imports a bunch of stuff from node which gives annoying warnings when vite externalizes them
52+
// Not a real concern but if this function is all you're using, we can inline it
53+
function parseEnv(src: string) {
54+
const LINE =
55+
/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm;
56+
const obj = {};
57+
58+
// Convert buffer to string
59+
let lines = src.toString();
60+
61+
// Convert line breaks to same format
62+
lines = lines.replace(/\r\n?/gm, "\n");
63+
64+
let match;
65+
while ((match = LINE.exec(lines)) != null) {
66+
const key = match[1];
67+
68+
// Default undefined or null to empty string
69+
let value = match[2] || "";
70+
71+
// Remove whitespace
72+
value = value.trim();
73+
74+
// Check if double quoted
75+
const maybeQuote = value[0];
76+
77+
// Remove surrounding quotes
78+
value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2");
79+
80+
// Expand newlines if double quoted
81+
if (maybeQuote === '"') {
82+
value = value.replace(/\\n/g, "\n");
83+
value = value.replace(/\\r/g, "\r");
84+
}
85+
86+
// @ts-ignore
87+
obj[key] = value;
88+
}
89+
90+
return obj;
91+
}
92+
5193
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
5294
const userId = await requireUserId(request);
5395
const { projectParam } = ProjectParamSchema.parse(params);
@@ -364,7 +406,7 @@ function VariableFields({
364406
let text = clipboardData.getData("text");
365407
if (!text) return;
366408

367-
const variables = dotenv.parse(text);
409+
const variables = parseEnv(text);
368410
const keyValuePairs = Object.entries(variables).map(([key, value]) => ({ key, value }));
369411

370412
//do the default paste

0 commit comments

Comments
 (0)