-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProxyServer.ts
More file actions
604 lines (526 loc) · 20.2 KB
/
ProxyServer.ts
File metadata and controls
604 lines (526 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createServer } from 'node:http';
import type { Server, IncomingMessage, ServerResponse } from 'node:http';
import { EventEmitter } from 'node:events';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import httpProxy from 'http-proxy';
import { Logger, SfError } from '@salesforce/core';
import type { OrgInfo } from '@salesforce/webapp-experimental/app';
import { getOrgInfo } from '@salesforce/webapp-experimental/app';
import type { ProxyHandler } from '@salesforce/webapp-experimental/proxy';
import { createProxyHandler } from '@salesforce/webapp-experimental/proxy';
import type { WebAppManifest } from '../config/manifest.js';
import type { DevServerError } from '../config/types.js';
import type { ErrorPageData } from '../templates/ErrorPageRenderer.js';
import { ErrorPageRenderer } from '../templates/ErrorPageRenderer.js';
/**
* Configuration for the proxy server
*/
type ProxyServerConfig = {
port: number;
devServerUrl: string;
salesforceInstanceUrl: string;
manifest?: WebAppManifest;
orgAlias?: string;
host?: string;
};
/**
* ProxyServer manages the HTTP proxy that sits between the web application
* and both the Salesforce instance and the local development server.
*/
export class ProxyServer extends EventEmitter {
// Instance fields
private config: ProxyServerConfig;
private readonly logger: Logger;
private readonly wsProxy: httpProxy;
private readonly errorPageRenderer: ErrorPageRenderer;
private server: Server | null = null;
private isCodeBuilder = false;
private healthCheckInterval: NodeJS.Timeout | null = null;
private devServerStatus: 'unknown' | 'up' | 'down' | 'error' = 'unknown';
private readonly workspaceScript: string;
private activeDevServerError: DevServerError | null = null;
private readonly activeConnections: Set<import('net').Socket> = new Set();
private proxyHandler: ProxyHandler | null = null;
private orgInfo: OrgInfo | undefined;
// AC1: Proxy-only mode (skip proxying to dev server, use proxy for Salesforce API only)
private proxyOnlyMode = false;
// Constructor
public constructor(config: ProxyServerConfig) {
super();
this.config = config;
this.logger = Logger.childFromRoot('ProxyServer');
this.errorPageRenderer = new ErrorPageRenderer();
this.workspaceScript = ProxyServer.detectWorkspaceScript();
this.isCodeBuilder = ProxyServer.detectCodeBuilder();
this.wsProxy = httpProxy.createProxyServer({
changeOrigin: true,
ws: true,
xfwd: true,
});
this.wsProxy.on('error', (err, req, res) => {
this.handleProxyError(err, req, res);
});
}
// Private static methods
private static detectCodeBuilder(): boolean {
const codeBuilderIndicators = ['SBQQ_STUDIO_WORKSPACE', 'SALESFORCE_PROJECT_ID', 'CODE_BUILDER_SESSION'];
return codeBuilderIndicators.some((indicator) => process.env[indicator] !== undefined);
}
private static detectWorkspaceScript(): string {
try {
const packageJsonPath = join(process.cwd(), 'package.json');
const packageJsonContent = readFileSync(packageJsonPath, 'utf-8');
const packageJson = JSON.parse(packageJsonContent) as { scripts?: Record<string, string> };
const commonScripts = ['dev', 'start', 'serve'];
for (const scriptName of commonScripts) {
if (packageJson.scripts?.[scriptName]) {
return `npm run ${scriptName}`;
}
}
return 'npm run dev';
} catch {
return 'npm run dev';
}
}
// Public instance methods
public clearActiveDevServerError(): void {
if (this.activeDevServerError) {
this.logger.debug('Dev server error cleared - dev server recovered');
this.activeDevServerError = null;
}
}
public getProxyUrl(): string {
const host = this.getBindHost();
const displayHost = host === '0.0.0.0' || host === '127.0.0.1' ? 'localhost' : host;
return `http://${displayHost}:${this.config.port}`;
}
public setActiveDevServerError(error: DevServerError): void {
this.activeDevServerError = error;
this.devServerStatus = 'error';
this.logger.debug(`Dev server error is now active: ${error.title}`);
}
public async start(): Promise<void> {
if (this.isCodeBuilder) {
this.logger.debug('Code Builder environment detected');
}
if (this.server) {
throw new SfError('Proxy server is already running', 'ProxyAlreadyRunning');
}
// Get org info for auth
if (this.config.orgAlias) {
try {
this.orgInfo = await getOrgInfo(this.config.orgAlias);
if (this.orgInfo) {
this.logger.debug(`Org info loaded for: ${this.orgInfo.username}`);
} else {
this.logger.warn(`Failed to get org info for: ${this.config.orgAlias}`);
}
} catch (error) {
this.logger.warn(`Failed to get org info: ${error instanceof Error ? error.message : String(error)}`);
}
}
this.initializeProxyHandler();
return new Promise((resolve, reject) => {
try {
this.server = createServer((req, res) => {
this.handleRequest(req, res).catch((error) => {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger.error(`Request handling error: ${errorMessage}`);
});
});
this.server.on('upgrade', (req, socket, head) => {
try {
this.handleWebSocketUpgrade(req, socket, head);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger.error(`WebSocket upgrade error: ${errorMessage}`);
socket.end();
}
});
const host = this.getBindHost();
this.server.on('connection', (socket) => {
this.activeConnections.add(socket);
socket.on('error', (err) => {
// Handle ECONNRESET and other socket errors gracefully
// These can happen when the dev server crashes or a client disconnects abruptly
this.logger.debug(`Socket error (${err.message}), cleaning up connection`);
});
socket.once('close', () => {
this.activeConnections.delete(socket);
});
});
this.server.listen(this.config.port, host, () => {
this.logger.debug(`Proxy server listening on http://${host}:${this.config.port}`);
this.logger.debug(`Forwarding to dev server: ${this.config.devServerUrl}`);
this.logger.debug(`Forwarding to Salesforce: ${this.config.salesforceInstanceUrl}`);
this.startHealthCheck();
resolve();
});
this.server.on('error', (error: NodeJS.ErrnoException) => {
if (error.code === 'EADDRINUSE') {
reject(
new SfError(
`Port ${this.config.port} is already in use. Please try a different port with the --port flag or stop the service using that port.`,
'PortInUseError'
)
);
} else if (error.code === 'EACCES') {
reject(
new SfError(
`Permission denied to bind to port ${this.config.port}. Try using a port number above 1024 or run with appropriate permissions.`,
'PortPermissionError'
)
);
} else {
reject(new SfError(`Failed to start proxy server: ${error.message}`, 'ProxyStartFailed'));
}
});
} catch (error) {
reject(
new SfError(
`Failed to create proxy server: ${error instanceof Error ? error.message : String(error)}`,
'ProxyCreateFailed'
)
);
}
});
}
public async stop(): Promise<void> {
if (!this.server) {
return;
}
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval);
this.healthCheckInterval = null;
}
for (const socket of this.activeConnections) {
socket.destroy();
}
this.activeConnections.clear();
return new Promise((resolve, reject) => {
const forceCloseTimeout = setTimeout(() => {
this.logger.debug('Proxy server stop timeout, forcing shutdown');
this.server = null;
resolve();
}, 2000);
this.server!.close((error) => {
clearTimeout(forceCloseTimeout);
if (error) {
if (error.message.includes('Server is not running')) {
this.logger.debug('Proxy server already stopped');
this.server = null;
resolve();
} else {
reject(new SfError(`Failed to stop proxy server: ${error.message}`, 'ProxyStopFailed'));
}
} else {
this.logger.debug('Proxy server stopped');
this.server = null;
resolve();
}
});
this.wsProxy.close();
});
}
public updateDevServerUrl(newDevServerUrl: string): void {
if (this.config.devServerUrl === newDevServerUrl) {
this.logger.debug(`Dev server URL unchanged: ${newDevServerUrl}`);
return;
}
this.logger.info(`Updating dev server URL: ${this.config.devServerUrl} → ${newDevServerUrl}`);
this.config.devServerUrl = newDevServerUrl;
this.initializeProxyHandler();
this.devServerStatus = 'unknown';
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval);
this.startHealthCheck();
}
this.checkDevServerHealth().catch((error) => {
this.logger.error(`Failed to check dev server health: ${error instanceof Error ? error.message : String(error)}`);
});
}
public updateManifest(manifest: WebAppManifest): void {
this.config.manifest = manifest;
this.initializeProxyHandler();
this.logger.debug('Proxy handler reinitialized with updated manifest');
}
// Private instance methods
private async checkDevServerHealth(): Promise<void> {
try {
await fetch(this.config.devServerUrl, {
method: 'HEAD',
signal: AbortSignal.timeout(3000),
});
if (this.devServerStatus !== 'up') {
this.devServerStatus = 'up';
this.emit('dev-server-up', this.config.devServerUrl);
this.logger.debug(`Dev server is UP: ${this.config.devServerUrl}`);
this.clearActiveDevServerError();
}
} catch {
if (this.devServerStatus !== 'down') {
this.devServerStatus = 'down';
this.emit('dev-server-down', this.config.devServerUrl);
this.logger.debug(`Dev server is DOWN: ${this.config.devServerUrl}`);
}
}
}
private getBindHost(): string {
if (this.config.host) {
return this.config.host;
}
if (this.isCodeBuilder) {
this.logger.debug('Code Builder: Binding to 0.0.0.0 (all interfaces)');
return '0.0.0.0';
}
return '127.0.0.1';
}
private handleProxyError(error: Error, req: IncomingMessage, res: ServerResponse | NodeJS.Socket): void {
const url = req.url ?? '/';
this.logger.error(`Proxy error for ${url}: ${error.message}`);
if ('writeHead' in res && 'headersSent' in res && !res.headersSent) {
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
error: 'Proxy Error',
message: error.message,
})
);
}
}
/**
* AC1: Handle internal proxy API requests from the interactive error page.
* Returns true if the request was handled, false if it should continue to the normal flow.
*/
private async handleProxyApi(req: IncomingMessage, res: ServerResponse, url: string): Promise<boolean> {
if (!url.startsWith('/_proxy/')) {
return false;
}
const setCorsHeaders = (): void => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
};
if (req.method === 'OPTIONS') {
setCorsHeaders();
res.writeHead(204);
res.end();
return true;
}
setCorsHeaders();
const sendJson = (statusCode: number, data: Record<string, unknown>): void => {
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
};
const readBody = (): Promise<string> =>
new Promise((resolve) => {
let body = '';
req.on('data', (chunk: Buffer) => {
body += chunk.toString();
});
req.on('end', () => resolve(body));
});
try {
switch (url) {
case '/_proxy/status': {
sendJson(200, {
devServerStatus: this.devServerStatus,
devServerUrl: this.config.devServerUrl,
proxyOnlyMode: this.proxyOnlyMode,
proxyUrl: this.getProxyUrl(),
workspaceScript: this.workspaceScript,
activeError: this.activeDevServerError
? { title: this.activeDevServerError.title, message: this.activeDevServerError.message }
: null,
});
return true;
}
case '/_proxy/set-url': {
const body = await readBody();
const parsed = JSON.parse(body) as { url?: string };
if (!parsed.url) {
sendJson(400, { error: 'Missing "url" in request body' });
return true;
}
this.logger.info(`[Proxy API] Updating dev server URL to: ${parsed.url}`);
this.updateDevServerUrl(parsed.url);
sendJson(200, { ok: true, devServerUrl: parsed.url });
return true;
}
case '/_proxy/retry': {
this.logger.info('[Proxy API] Retrying dev server detection');
await this.checkDevServerHealth();
sendJson(200, { ok: true, devServerStatus: this.devServerStatus });
return true;
}
case '/_proxy/start-dev': {
this.logger.info('[Proxy API] Request to start dev server');
this.emit('startDevServer');
sendJson(200, { ok: true, message: 'Dev server start requested' });
return true;
}
case '/_proxy/proxy-only': {
this.proxyOnlyMode = !this.proxyOnlyMode;
this.logger.info(`[Proxy API] Proxy-only mode: ${this.proxyOnlyMode ? 'ON' : 'OFF'}`);
sendJson(200, { ok: true, proxyOnlyMode: this.proxyOnlyMode });
return true;
}
case '/_proxy/restart': {
this.logger.info('[Proxy API] Request to restart dev server');
this.emit('restartDevServer');
sendJson(200, { ok: true, message: 'Dev server restart requested' });
return true;
}
case '/_proxy/force-kill': {
this.logger.info('[Proxy API] Request to force-kill dev server');
this.emit('forceKillDevServer');
sendJson(200, { ok: true, message: 'Dev server force-kill requested' });
return true;
}
default: {
sendJson(404, { error: `Unknown proxy API endpoint: ${url}` });
return true;
}
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger.error(`[Proxy API] Error handling ${url}: ${errorMessage}`);
sendJson(500, { error: errorMessage });
return true;
}
}
private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
const url = req.url ?? '/';
const method = req.method ?? 'GET';
this.logger.debug(`[${method}] ${url}`);
// AC1: Handle internal proxy API requests first
if (await this.handleProxyApi(req, res, url)) {
return;
}
if (this.activeDevServerError) {
this.logger.debug('Active dev server error - serving error page');
this.serveDevServerErrorPage(this.activeDevServerError, res);
return;
}
// AC1: In proxy-only mode, skip the dev server "down" check
if (this.devServerStatus === 'down' && !this.proxyOnlyMode && !url.includes('/services')) {
this.serveErrorPage(res);
return;
}
if (this.proxyHandler) {
// Package handles all errors internally and returns proper HTTP responses
await this.proxyHandler(req, res);
} else {
this.logger.error('Proxy handler not initialized');
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Proxy not initialized' }));
}
}
private handleWebSocketUpgrade(req: IncomingMessage, socket: NodeJS.Socket, head: Buffer): void {
const url = req.url ?? '/';
this.logger.debug(`[WebSocket] Upgrade request: ${url}`);
try {
const isSalesforceWs = url.includes('/cometd') || url.includes('/bayeux');
if (isSalesforceWs && this.orgInfo) {
this.logger.debug(`→ Salesforce WebSocket: ${url}`);
this.wsProxy.ws(
req,
socket,
head,
{
target: this.config.salesforceInstanceUrl,
headers: { Authorization: `Bearer ${this.orgInfo.accessToken}` },
},
(error) => {
if (error) {
this.logger.error(`WebSocket proxy error: ${error.message}`);
socket.end();
}
}
);
} else {
this.logger.debug(`→ Dev Server WebSocket: ${url}`);
this.wsProxy.ws(req, socket, head, {
target: this.config.devServerUrl,
});
}
} catch (error) {
this.logger.error(`WebSocket upgrade failed: ${error instanceof Error ? error.message : String(error)}`);
socket.end();
}
}
private initializeProxyHandler(): void {
const manifest: WebAppManifest = this.config.manifest ?? {
name: 'webapp',
outputDir: 'dist',
};
this.proxyHandler = createProxyHandler(manifest, this.orgInfo, this.config.devServerUrl, undefined, {
debug: process.env.SF_LOG_LEVEL === 'debug',
});
}
private serveDevServerErrorPage(error: DevServerError, res: ServerResponse): void {
try {
const html = this.errorPageRenderer.renderDevServerError(error);
res.writeHead(500, {
'Content-Type': 'text/html',
'Cache-Control': 'no-cache, no-store, must-revalidate',
});
res.end(html);
this.logger.debug('Served dev server error page to browser');
} catch (err) {
this.logger.error(`Failed to render dev server error page: ${err instanceof Error ? err.message : String(err)}`);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(`Dev Server Error: ${error.title}\n\n${error.message}\n\nCheck terminal for details.`);
}
}
private serveErrorPage(res: ServerResponse): void {
try {
const errorPageData: ErrorPageData = {
status: 'No Dev Server Detected',
devServerUrl: this.config.devServerUrl,
workspaceScript: this.workspaceScript,
proxyUrl: this.getProxyUrl(),
orgTarget: this.config.salesforceInstanceUrl.replace('https://', ''),
};
const html = this.errorPageRenderer.render(errorPageData);
res.writeHead(503, {
'Content-Type': 'text/html',
'Cache-Control': 'no-cache, no-store, must-revalidate',
});
res.end(html);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger.error(`CRITICAL: Failed to render dev server error page: ${errorMessage}`);
res.writeHead(503, { 'Content-Type': 'text/plain' });
res.end(
`Dev Server Unavailable\n\nCannot connect to: ${this.config.devServerUrl}\n\nStart your dev server and refresh this page.`
);
}
}
private startHealthCheck(): void {
this.checkDevServerHealth().catch((error) => {
this.logger.debug(`Initial health check error: ${String(error)}`);
});
this.healthCheckInterval = setInterval(() => {
this.checkDevServerHealth().catch((error) => {
this.logger.debug(`Health check error: ${String(error)}`);
});
}, 10_000);
}
}