Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Remix Vite #1496

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion apps/webapp/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RemixServer } from "@remix-run/react";
import { parseAcceptLanguage } from "intl-parse-accept-language";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
import { PassThrough } from "stream";
import { PassThrough } from "node:stream";
import * as Worker from "~/services/worker.server";
import { LocaleContextProvider } from "./components/primitives/LocaleProvider";
import {
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { UseDataFunctionReturn, typedjson, useTypedLoaderData } from "remix-type
import { ExternalScripts } from "remix-utils/external-scripts";
import type { ToastMessage } from "~/models/message.server";
import { commitSession, getSession } from "~/models/message.server";
import tailwindStylesheetUrl from "~/tailwind.css";
import tailwindStylesheetUrl from "~/tailwind.css?url";
import { RouteErrorDisplay } from "./components/ErrorDisplay";
import { HighlightInit } from "./components/HighlightInit";
import { AppContainer, MainCenteredContainer } from "./components/layout/AppLayout";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { cn } from "~/utils/cn";
import { ProjectParamSchema, v3BillingPath, v3EnvironmentVariablesPath } from "~/utils/pathBuilder";
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
import { EnvironmentVariableKey } from "~/v3/environmentVariables/repository";
import dotenv from "dotenv";
import { Paragraph } from "~/components/primitives/Paragraph";
import { TextLink } from "~/components/primitives/TextLink";
import {
Expand All @@ -48,6 +47,49 @@ import {
TooltipTrigger,
} from "~/components/primitives/Tooltip";

// https://github.com/motdotla/dotenv/blob/master/lib/main.js
// dotenv imports a bunch of stuff from node which gives annoying warnings when vite externalizes them
// Not a real concern but if this function is all you're using, we can inline it
function parseEnv(src: string) {
const LINE =
/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm;
const obj = {};

// Convert buffer to string
let lines = src.toString();

// Convert line breaks to same format
lines = lines.replace(/\r\n?/gm, "\n");

let match;
while ((match = LINE.exec(lines)) != null) {
const key = match[1];

// Default undefined or null to empty string
let value = match[2] || "";

// Remove whitespace
value = value.trim();

// Check if double quoted
const maybeQuote = value[0];

// Remove surrounding quotes
value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2");

// Expand newlines if double quoted
if (maybeQuote === '"') {
value = value.replace(/\\n/g, "\n");
value = value.replace(/\\r/g, "\r");
}

// @ts-ignore
obj[key] = value;
}

return obj;
}

export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const { projectParam } = ProjectParamSchema.parse(params);
Expand Down Expand Up @@ -364,7 +406,7 @@ function VariableFields({
let text = clipboardData.getData("text");
if (!text) return;

const variables = dotenv.parse(text);
const variables = parseEnv(text);
const keyValuePairs = Object.entries(variables).map(([key, value]) => ({ key, value }));

//do the default paste
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/app/routes/auth.github.callback.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LoaderFunction } from "@remix-run/node";
import { authenticator } from "~/services/auth.server";
import { redirectCookie } from "./auth.github";
import { redirectCookie } from "./auth.github.server";
import { logger } from "~/services/logger.server";

export let loader: LoaderFunction = async ({ request }) => {
Expand Down
6 changes: 6 additions & 0 deletions apps/webapp/app/routes/auth.github.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createCookie } from "@remix-run/node";

export const redirectCookie = createCookie("redirect-to", {
maxAge: 60 * 60, // 1 hour
httpOnly: true,
});
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider enhancing cookie security configuration

The basic implementation looks good, but consider adding these security-related cookie attributes:

  • secure: true to ensure HTTPS-only
  • sameSite: 'lax' to prevent CSRF attacks
  • path: '/auth' to restrict cookie scope
 export const redirectCookie = createCookie("redirect-to", {
   maxAge: 60 * 60, // 1 hour
   httpOnly: true,
+  secure: true,
+  sameSite: 'lax',
+  path: '/auth',
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const redirectCookie = createCookie("redirect-to", {
maxAge: 60 * 60, // 1 hour
httpOnly: true,
});
export const redirectCookie = createCookie("redirect-to", {
maxAge: 60 * 60, // 1 hour
httpOnly: true,
secure: true,
sameSite: 'lax',
path: '/auth',
});

7 changes: 1 addition & 6 deletions apps/webapp/app/routes/auth.github.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ActionFunction, LoaderFunction } from "@remix-run/node";
import { createCookie } from "@remix-run/node";
import { redirect } from "@remix-run/node";
import { authenticator } from "~/services/auth.server";
import { redirectCookie } from "./auth.github.server";

export let loader: LoaderFunction = () => redirect("/login");

Expand All @@ -27,8 +27,3 @@ export let action: ActionFunction = async ({ request }) => {
throw error;
}
};

export const redirectCookie = createCookie("redirect-to", {
maxAge: 60 * 60, // 1 hour
httpOnly: true,
});
2 changes: 1 addition & 1 deletion apps/webapp/app/v3/eventRepository.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { Prisma, TaskEvent, TaskEventStatus, type TaskEventKind } from "@trigger.dev/database";
import Redis, { RedisOptions } from "ioredis";
import { createHash } from "node:crypto";
import { EventEmitter } from "node:stream";
import { EventEmitter } from "node:events";
import { Gauge } from "prom-client";
import { $replica, PrismaClient, PrismaReplicaClient, prisma } from "~/db.server";
import { env } from "~/env.server";
Expand Down
6 changes: 4 additions & 2 deletions apps/webapp/app/v3/handleWebsockets.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { AuthenticatedSocketConnection } from "./authenticatedSocketConnection.s
import { Gauge } from "prom-client";
import { metricsRegister } from "~/metrics.server";

export const wss = singleton("wss", initalizeWebSocketServer);

// Moved in front of export const wss because
// ReferenceError: Cannot access 'authenticatedConnections' before initialization
let authenticatedConnections: Map<string, AuthenticatedSocketConnection>;

export const wss = singleton("wss", initalizeWebSocketServer);

function initalizeWebSocketServer() {
const server = new WebSocketServer({ noServer: true });

Expand Down
41 changes: 22 additions & 19 deletions apps/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
"name": "webapp",
"version": "1.0.0",
"sideEffects": false,
"type": "module",
"scripts": {
"build": "run-s build:**",
"build:db:seed": "esbuild --platform=node --bundle --minify --format=cjs ./prisma/seed.ts --outdir=prisma",
"build:remix": "remix build",
"build:server": "esbuild --platform=node --format=cjs ./server.ts --outdir=build",
"dev": "cross-env PORT=3030 remix dev -c \"node ./build/server.js\"",
"dev:worker": "cross-env NODE_PATH=../../node_modules/.pnpm/node_modules node ./build/server.js",
"build:remix": "remix vite:build",
"dev": "cross-env NODE_ENV=development PORT=3030 node ./server.js",
"dev:worker": "cross-env NODE_PATH=../../node_modules/.pnpm/node_modules NODE_ENV=development node ./server.js",
"format": "prettier --write .",
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
"start": "cross-env NODE_ENV=production node --max-old-space-size=8192 ./build/server.js",
"start:local": "cross-env node --max-old-space-size=8192 ./build/server.js",
"start": "cross-env NODE_ENV=production node --max-old-space-size=8192 ./server.js",
"start:local": "cross-env NODE_ENV=development node --max-old-space-size=8192 ./server.js",
"typecheck": "tsc -p ./tsconfig.check.json",
"db:seed": "node prisma/seed.js",
"db:seed:local": "ts-node prisma/seed.ts",
Expand Down Expand Up @@ -80,12 +79,12 @@
"@react-aria/datepicker": "^3.9.1",
"@react-stately/datepicker": "^3.9.1",
"@react-types/datepicker": "^3.7.1",
"@remix-run/express": "2.1.0",
"@remix-run/node": "2.1.0",
"@remix-run/react": "2.1.0",
"@remix-run/router": "^1.15.3",
"@remix-run/serve": "2.1.0",
"@remix-run/server-runtime": "2.1.0",
"@remix-run/express": "2.15.0",
"@remix-run/node": "2.15.0",
"@remix-run/react": "2.15.0",
"@remix-run/router": "^1.21.0",
"@remix-run/serve": "2.15.0",
"@remix-run/server-runtime": "2.15.0",
Comment on lines +82 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Verify Node.js version compatibility

The upgrade includes significant version changes:

  1. Remix packages upgraded to 2.15.0
  2. Node.js types upgraded to ^20.14.14
  3. Added Vite v5

However, the engines field still specifies "node": ">=16.0.0". This should be updated to match the Node.js version required by the new dependencies.

   "engines": {
-    "node": ">=16.0.0"
+    "node": ">=20.0.0"
   }

Also applies to: 191-193, 209-209, 254-255

"@remix-run/v1-meta": "^0.1.3",
"@slack/web-api": "^6.8.1",
"@socket.io/redis-adapter": "^8.3.0",
Expand Down Expand Up @@ -181,16 +180,17 @@
"tiny-invariant": "^1.2.0",
"ulid": "^2.3.0",
"ulidx": "^2.2.1",
"vite": "5",
"ws": "^8.11.0",
"zod": "3.23.8",
"zod-error": "1.5.0",
"zod-validation-error": "^1.5.0"
},
"devDependencies": {
"@internal/testcontainers": "workspace:*",
"@remix-run/dev": "2.1.0",
"@remix-run/eslint-config": "2.1.0",
"@remix-run/testing": "^2.1.0",
"@remix-run/dev": "2.15.0",
"@remix-run/eslint-config": "2.15.0",
"@remix-run/testing": "^2.15.0",
"@swc/core": "^1.3.4",
"@swc/helpers": "^0.4.11",
"@tailwindcss/forms": "^0.5.3",
Expand All @@ -206,7 +206,7 @@
"@types/lodash.omit": "^4.5.7",
"@types/marked": "^4.0.3",
"@types/morgan": "^1.9.3",
"@types/node": "^18.11.15",
"@types/node": "^20.14.14",
"@types/node-fetch": "^2.6.2",
"@types/prismjs": "^1.26.0",
"@types/qs": "^6.9.7",
Expand All @@ -228,6 +228,8 @@
"babel-preset-react-app": "^10.0.1",
"css-loader": "^6.10.0",
"datepicker": "link:@types/@react-aria/datepicker",
"dotenv-cli": "^7.4.4",
"dotenv-expand": "^12.0.1",
"engine.io": "^6.5.4",
"esbuild": "^0.15.10",
"eslint": "^8.24.0",
Expand All @@ -249,10 +251,11 @@
"ts-node": "^10.7.0",
"tsconfig-paths": "^3.14.1",
"typescript": "^5.1.6",
"vite-tsconfig-paths": "^4.0.5",
"vite-plugin-cjs-interop": "^2.1.5",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.4.0"
},
"engines": {
"node": ">=16.0.0"
}
}
}
2 changes: 1 addition & 1 deletion apps/webapp/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
Expand Down
File renamed without changes.
31 changes: 0 additions & 31 deletions apps/webapp/remix.config.js

This file was deleted.

2 changes: 0 additions & 2 deletions apps/webapp/remix.env.d.ts

This file was deleted.

Loading
Loading