Skip to content

Commit 349370c

Browse files
committed
fix: stop the preview proxy from eating this server's own routes
1 parent 38a95f3 commit 349370c

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎backend/src/main.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ async function bootstrap() {
3737
// straight past Express's 100kb default. Bounded by ArrayMaxSize(4) on the DTO.
3838
app.use(json({ limit: '25mb' }));
3939

40-
// After Nest's router, so a real route always wins and only unclaimed
41-
// paths — a preview's own assets — reach the proxy.
40+
// Necessarily *before* Nest's router: Nest answers an unmatched path with
41+
// its own 404 rather than calling next(), so a proxy mounted after it never
42+
// runs. Being first means it must decline this server's own routes itself —
43+
// see OURS in preview-proxy.
4244
mountPreviewProxy(
4345
app.getHttpAdapter().getInstance(),
4446
app.get(PreviewService),

‎backend/src/project/preview-proxy.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ import { PreviewService } from './preview.service';
55
/** Identifies which project's dev server unclaimed requests belong to. */
66
export const PREVIEW_COOKIE = 'codefox_preview';
77

8+
/**
9+
* Paths this server answers itself, which the proxy must never take.
10+
*
11+
* The proxy has to run before Nest's router — Nest replies to an unmatched
12+
* path with its own 404 instead of calling next(), so anything mounted after
13+
* it is unreachable. Running first means every request carrying the preview
14+
* cookie went to the project's dev server, including the calls that make the
15+
* page work: /api/chat answered 404 and the chat never replied, /api/project
16+
* answered 404 and the file tree spun forever.
17+
*
18+
* Deliberately the specific routes rather than all of `/api`: a generated app
19+
* has its own API routes, and those are the preview's to serve.
20+
*/
21+
const OURS =
22+
/^\/(graphql|health|download|api\/(chat|project|file|media|preview|screenshot))(\/|\?|$)/;
23+
824
const logger = new Logger('PreviewProxy');
925

1026
/**
@@ -25,6 +41,8 @@ const logger = new Logger('PreviewProxy');
2541
*/
2642
export const mountPreviewProxy = (app: Express, previews: PreviewService) => {
2743
app.use(async (req: Request, res: Response, next: NextFunction) => {
44+
if (OURS.test(req.path)) return next();
45+
2846
const projectPath = (req.headers.cookie ?? '')
2947
.split(';')
3048
.map((part) => part.trim().split('='))

‎backend/src/project/project.service.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
} from '../common/utils/common-path';
3333
// import { GitHubService } from 'src/github/github.service';
3434
import { UserService } from 'src/user/user.service';
35+
import { PreviewService } from './preview.service';
3536

3637
@Injectable()
3738
export class ProjectService {
@@ -46,6 +47,7 @@ export class ProjectService {
4647
private uploadService: UploadService,
4748
// private readonly gitHubService: GitHubService,
4849
private userService: UserService,
50+
private previews: PreviewService,
4951
) {}
5052

5153
async getProjectsByUser(userId: string): Promise<Project[]> {
@@ -243,6 +245,12 @@ export class ProjectService {
243245
// leak on the volume. Best effort: a failure here must not fail the
244246
// delete the user asked for.
245247
if (project.projectPath) {
248+
// Stop the dev server first. It holds the directory open and keeps
249+
// writing to it, so removing the files under a running preview freed
250+
// nothing — the server rebuilt `.next` moments later — and left the
251+
// process, its port and its memory behind for the life of the box.
252+
this.previews.stop(project.projectPath);
253+
246254
const dir = path.join(getProjectsDir(), project.projectPath);
247255
await fs.promises
248256
.rm(dir, { recursive: true, force: true })

0 commit comments

Comments
 (0)