-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathworker.ts
More file actions
527 lines (464 loc) · 18.6 KB
/
worker.ts
File metadata and controls
527 lines (464 loc) · 18.6 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
/**
* Entry point for worker process that handles requests.
* @module worker
*/
import path from 'path';
import cluster from 'cluster';
import { randomUUID } from 'crypto';
import { rm } from 'fs/promises';
import fastify from 'fastify';
import fastifyFormbody from '@fastify/formbody';
import fastifyMultipart from '@fastify/multipart';
import log, { sharedLoggerOptions } from './shared/log.js';
import packageJson from './shared/packageJson.js';
import { buildConfig, Config, getConfig } from './shared/configBuilder.js';
import fileExistsAsync from './shared/fileExistsAsync.js';
import type { FastifyInstance, FastifyReply, FastifyRequest } from './worker/types.js';
import checkProtocolVersion from './worker/checkProtocolVersionHandler.js';
import authenticate from './worker/authHandler.js';
import {
handleRenderRequest,
handleNewBundlesProvided,
type ProvidedNewBundle,
} from './worker/handleRenderRequest.js';
import handleGracefulShutdown from './worker/handleGracefulShutdown.js';
import { handleStartupListenError } from './worker/startupErrorHandler.js';
import {
badRequestResponseResult,
errorResponseResult,
formatExceptionMessage,
ResponseResult,
saveMultipartFile,
Asset,
getAssetPath,
} from './shared/utils.js';
import { startSsrRequestOptions, trace } from './shared/tracing.js';
// Uncomment the below for testing timeouts:
// import { delay } from './shared/utils.js';
//
// function getRandomInt(max) {
// return Math.floor(Math.random() * Math.floor(max));
// }
declare module '@fastify/multipart' {
interface MultipartFile {
// We save all uploaded files and store this value
value: Asset;
}
}
declare module 'fastify' {
// eslint-disable-next-line @typescript-eslint/no-shadow
interface FastifyRequest {
uploadDir: string;
}
}
export type FastifyConfigFunction = (app: FastifyInstance) => void;
const fastifyConfigFunctions: FastifyConfigFunction[] = [];
/**
* Configures Fastify instance before starting the server.
* @param configFunction The configuring function. Normally it will be something like `(app) => { app.register(...); }`
* or `(app) => { app.addHook(...); }` to report data from Fastify to an external service.
* Note that we call `await app.ready()` in our code, so you don't need to `await` the results.
*/
export function configureFastify(configFunction: FastifyConfigFunction) {
fastifyConfigFunctions.push(configFunction);
}
function setHeaders(headers: ResponseResult['headers'], res: FastifyReply) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- fixing it with `void` just violates no-void
Object.entries(headers).forEach(([key, header]) => res.header(key, header));
}
const setResponse = async (result: ResponseResult, res: FastifyReply) => {
const { status, data, headers, stream } = result;
if (status !== 200 && status !== 410) {
log.info({ msg: 'Sending non-200, non-410 data back', data });
}
setHeaders(headers, res);
res.status(status);
if (stream) {
await res.send(stream);
} else {
res.send(data);
}
};
const isAsset = (value: unknown): value is Asset => (value as { type?: string }).type === 'asset';
function assertAsset(value: unknown, key: string): asserts value is Asset {
if (!isAsset(value)) {
throw new Error(`React On Rails Error: Expected an asset for key: ${key}`);
}
}
/**
* Parses the multipart form body to separate bundle files from shared assets.
* Used by both the render and /upload-assets endpoints to avoid duplicating
* bundle-vs-asset classification logic.
*
* @param body The parsed multipart request body.
* @param primaryBundleTimestamp If provided, a field with key `"bundle"` is
* treated as a bundle for this timestamp (render endpoint convention).
*/
function extractBundlesAndAssets(
body: Record<string, unknown>,
primaryBundleTimestamp?: string | number,
): { providedNewBundles: ProvidedNewBundle[]; assetsToCopy: Asset[] } {
const providedNewBundles: ProvidedNewBundle[] = [];
const assetsToCopy: Asset[] = [];
Object.entries(body).forEach(([key, value]) => {
if (key === 'bundle' && primaryBundleTimestamp != null) {
assertAsset(value, key);
providedNewBundles.push({ timestamp: primaryBundleTimestamp, bundle: value });
} else if (key.startsWith('bundle_')) {
const timestamp = key.slice('bundle_'.length);
if (!timestamp) {
log.warn(
'Received form field with key "bundle_" but no hash suffix — possible bug in the Ruby client',
);
} else {
assertAsset(value, key);
providedNewBundles.push({ timestamp, bundle: value });
}
} else if (isAsset(value)) {
assetsToCopy.push(value);
}
});
return { providedNewBundles, assetsToCopy };
}
// Remove after this issue is resolved: https://github.com/fastify/light-my-request/issues/315
let useHttp2 = true;
// Call before any test using `app.inject()`
export const disableHttp2 = () => {
useHttp2 = false;
};
type WithBodyArrayField<T, K extends string> = T & { [P in K | `${K}[]`]?: string | string[] };
const INVALID_CONTENT_LENGTH_ERROR_CODE = 'FST_ERR_CTP_INVALID_CONTENT_LENGTH';
const errorCode = (error: unknown): string | undefined => {
const code = (error as { code?: unknown })?.code;
return typeof code === 'string' ? code : undefined;
};
const isValidRenderingRequest = (value: unknown): value is string =>
typeof value === 'string' && value.length > 0;
const SENSITIVE_REQUEST_BODY_KEYS = new Set([
'password',
'token',
'secret',
'api_key',
'api-key',
'apikey',
'authorization',
'auth_token',
'auth-token',
'authtoken',
'access_token',
'accesstoken',
'bearer',
]);
const invalidRenderingRequestMessage = (body: Record<string, unknown>) => {
const { renderingRequest } = body;
let renderingRequestType: string = renderingRequest === '' ? 'string (empty)' : typeof renderingRequest;
if (renderingRequest === null) {
renderingRequestType = 'null';
} else if (Array.isArray(renderingRequest)) {
renderingRequestType = 'array';
}
const bodyKeys = Object.keys(body).filter((key) => !SENSITIVE_REQUEST_BODY_KEYS.has(key.toLowerCase()));
return [
'Invalid "renderingRequest" field in render request.',
'Expected a non-empty string of JavaScript to execute in the SSR VM.',
`Received type: ${renderingRequestType}.`,
`Received body keys: ${bodyKeys.length > 0 ? bodyKeys.join(', ') : '(none)'}.`,
'Likely causes: request body truncation, malformed multipart form data, or Content-Length mismatch in a proxy/client.',
].join('\n');
};
const extractBodyArrayField = <Key extends string>(
body: WithBodyArrayField<Record<string, unknown>, Key>,
key: Key,
): string[] | undefined => {
const value = body[key] ?? body[`${key}[]`];
if (Array.isArray(value)) {
return value;
}
if (typeof value === 'string' && value.length > 0) {
return [value];
}
return undefined;
};
export default function run(config: Partial<Config>) {
// Store config in app state. From now it can be loaded by any module using
// getConfig():
buildConfig(config);
const { serverBundleCachePath, logHttpLevel, port, host, fastifyServerOptions, workersCount } = getConfig();
// The renderer uses cleartext HTTP/2 (h2c). Node's `allowHTTP1` option only
// applies to TLS servers (http2.createSecureServer), so it cannot enable
// HTTP/1.1 Kubernetes httpGet probes on this listener.
const app = fastify({
http2: useHttp2 as true,
bodyLimit: 104857600, // 100 MB
logger:
logHttpLevel !== 'silent' ? { name: 'RORP HTTP', level: logHttpLevel, ...sharedLoggerOptions } : false,
...fastifyServerOptions,
});
handleGracefulShutdown(app);
// We shouldn't have unhandled errors here, but just in case
app.addHook('onError', (req, res, err, done) => {
// Not errorReporter.error so that integrations can decide how to log the errors.
if (errorCode(err) === INVALID_CONTENT_LENGTH_ERROR_CODE) {
app.log.error({
msg: 'Invalid request body framing',
hint: 'Body size did not match Content-Length. Check client/proxy truncation and Content-Length handling.',
err,
req,
res,
});
} else {
app.log.error({ msg: 'Unhandled Fastify error', err, req, res });
}
done();
});
// Each request gets its own upload directory to prevent concurrent requests
// from overwriting each other's files (GitHub issue #2449).
// The directory path is lazily assigned in onFile (only for requests with file uploads).
app.decorateRequest('uploadDir', '');
// Clean up the per-request upload directory after the response is sent.
// Safe from a rate-limiting perspective (CodeQL js/missing-rate-limiting):
// this is an internal service not exposed to the internet, the path is
// server-generated (uploads/<UUID>), and the hook only runs rm when files
// were actually uploaded (uploadDir is non-empty).
app.addHook('onResponse', async (req) => {
if (req.uploadDir) {
await rm(req.uploadDir, { recursive: true, force: true }).catch((err: unknown) => {
log.warn({ msg: 'Failed to clean up per-request upload directory', uploadDir: req.uploadDir, err });
});
}
});
// 10 MB limit for code including props
const fieldSizeLimit = 1024 * 1024 * 10;
// Supports application/x-www-form-urlencoded
void app.register(fastifyFormbody);
// Supports multipart/form-data
void app.register(fastifyMultipart, {
attachFieldsToBody: 'keyValues',
limits: {
fieldSize: fieldSizeLimit,
// For bundles and assets
fileSize: Infinity,
},
// Use regular function (not arrow) because @fastify/multipart binds `this`
// to the Fastify request in attachFieldsToBody mode.
// Note: do NOT annotate `this` with the local Http2Server-typed FastifyRequest;
// the plugin types expect the default (RawServerDefault) FastifyRequest.
async onFile(part) {
if (typeof this?.uploadDir !== 'string') {
throw new Error('onFile: expected `this` to be bound to the Fastify request');
}
// Lazily assign a per-request upload directory on first file upload
if (this.uploadDir === '') {
this.uploadDir = path.join(serverBundleCachePath, 'uploads', randomUUID());
}
// Use path.basename to strip any directory components from the filename,
// preventing path traversal attacks (e.g. filename "../../etc/shadow").
const safeFilename = path.basename(part.filename);
if (!safeFilename) {
throw new Error(
`onFile: received file with empty or invalid filename: ${JSON.stringify(part.filename)}`,
);
}
const destinationPath = path.join(this.uploadDir, safeFilename);
await saveMultipartFile(part, destinationPath);
// eslint-disable-next-line no-param-reassign
part.value = {
filename: safeFilename,
savedFilePath: destinationPath,
type: 'asset',
};
},
});
const isProtocolVersionMatch = async (req: FastifyRequest, res: FastifyReply) => {
// Check protocol version
const protocolVersionCheckingResult = checkProtocolVersion(req);
if (typeof protocolVersionCheckingResult === 'object') {
await setResponse(protocolVersionCheckingResult, res);
return false;
}
return true;
};
const isAuthenticated = async (req: FastifyRequest, res: FastifyReply) => {
// Authenticate Ruby client
const authResult = authenticate(req);
if (typeof authResult === 'object') {
await setResponse(authResult, res);
return false;
}
return true;
};
const requestPrechecks = async (req: FastifyRequest, res: FastifyReply) => {
if (!(await isProtocolVersionMatch(req, res))) {
return false;
}
if (!(await isAuthenticated(req, res))) {
return false;
}
return true;
};
// See https://github.com/shakacode/react_on_rails_pro/issues/119 for why
// the digest is part of the request URL. Yes, it's not used here, but the
// server logs might show it to distinguish different requests.
app.post<{
Body: WithBodyArrayField<Record<string, unknown>, 'dependencyBundleTimestamps'>;
// Can't infer from the route like Express can
Params: { bundleTimestamp: string; renderRequestDigest: string };
}>('/bundles/:bundleTimestamp/render/:renderRequestDigest', async (req, res) => {
if (!(await requestPrechecks(req, res))) {
return;
}
// DO NOT REMOVE (REQUIRED FOR TIMEOUT TESTING)
// if(TESTING_TIMEOUTS && getRandomInt(2) === 1) {
// console.log(
// 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ');
// console.log(`Sleeping, to test timeouts`);
// console.log(
// 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ');
//
// await delay(100000);
// }
const { body } = req;
if (!body || typeof body !== 'object') {
await setResponse(badRequestResponseResult('Invalid or missing request body.'), res);
return;
}
const { renderingRequest } = body;
if (!isValidRenderingRequest(renderingRequest)) {
await setResponse(badRequestResponseResult(invalidRenderingRequestMessage(body)), res);
return;
}
const { bundleTimestamp } = req.params;
const { providedNewBundles, assetsToCopy } = extractBundlesAndAssets(body, bundleTimestamp);
try {
const dependencyBundleTimestamps = extractBodyArrayField(body, 'dependencyBundleTimestamps');
await trace(async (context) => {
try {
const result = await handleRenderRequest({
renderingRequest,
bundleTimestamp,
dependencyBundleTimestamps,
providedNewBundles,
assetsToCopy,
tracingContext: context,
});
await setResponse(result, res);
} catch (err) {
const exceptionMessage = formatExceptionMessage(
renderingRequest,
err,
'UNHANDLED error in handleRenderRequest',
);
await setResponse(errorResponseResult(exceptionMessage, context), res);
}
}, startSsrRequestOptions({ renderingRequest }));
} catch (theErr) {
const exceptionMessage = formatExceptionMessage(renderingRequest, theErr);
await setResponse(errorResponseResult(`Unhandled top level error: ${exceptionMessage}`), res);
}
});
// There can be additional files that might be required at the runtime.
// Since the remote renderer doesn't contain any assets, they must be uploaded manually.
// Bundle files use the form key convention "bundle_<hash>" and are placed in
// their own directory; remaining assets are copied to every bundle directory.
app.post<{
Body: Record<string, unknown>;
}>('/upload-assets', async (req, res) => {
if (!(await requestPrechecks(req, res))) {
return;
}
const { providedNewBundles, assetsToCopy } = extractBundlesAndAssets(req.body);
if (providedNewBundles.length === 0) {
const errorMsg =
'No bundle_<hash> fields provided. ' +
'The /upload-assets endpoint requires at least one bundle file with a "bundle_<hash>" form key.';
log.error(errorMsg);
await setResponse(errorResponseResult(errorMsg), res);
return;
}
const bundleNames = providedNewBundles.map((b) => b.bundle.filename);
const assetNames = assetsToCopy.map((a) => a.filename);
const taskDescription = `Uploading bundles [${bundleNames.join(', ')}] with assets [${assetNames.join(', ')}]`;
log.info(taskDescription);
try {
// Reuses the same per-bundle lock + move/copy logic as the render
// endpoint so that concurrent /upload-assets and render requests
// targeting the same bundle directory are mutually exclusive.
// See https://github.com/shakacode/react_on_rails/issues/2463
const result = await handleNewBundlesProvided(taskDescription, providedNewBundles, assetsToCopy);
if (result) {
await setResponse(result, res);
return;
}
await setResponse({ status: 200, headers: {} }, res);
} catch (err) {
const msg = 'ERROR when trying to upload bundles and assets';
const message = `${msg}. ${err}. Task: ${taskDescription}`;
log.error({ msg, err, task: taskDescription });
await setResponse(errorResponseResult(message), res);
}
});
// Checks if file exist
app.post<{
Querystring: { filename: string };
Body: WithBodyArrayField<Record<string, unknown>, 'targetBundles'>;
}>('/asset-exists', async (req, res) => {
if (!(await isAuthenticated(req, res))) {
return;
}
const { filename } = req.query;
if (!filename) {
const message = `ERROR: filename param not provided to GET /asset-exists`;
log.info(message);
await setResponse(errorResponseResult(message), res);
return;
}
// Handle targetBundles as either a string or an array
const targetBundles = extractBodyArrayField(req.body, 'targetBundles');
if (!targetBundles || targetBundles.length === 0) {
const errorMsg = 'No targetBundles provided. As of protocol version 2.0.0, targetBundles is required.';
log.error(errorMsg);
await setResponse(errorResponseResult(errorMsg), res);
return;
}
// Check if the asset exists in each of the target bundles
const results = await Promise.all(
targetBundles.map(async (bundleHash) => {
const assetPath = getAssetPath(bundleHash, filename);
const exists = await fileExistsAsync(assetPath);
if (exists) {
log.info(`/asset-exists Uploaded asset DOES exist in bundle ${bundleHash}: ${assetPath}`);
} else {
log.info(`/asset-exists Uploaded asset DOES NOT exist in bundle ${bundleHash}: ${assetPath}`);
}
return { bundleHash, exists };
}),
);
// Asset exists if it exists in all target bundles
const allExist = results.every((result) => result.exists);
await setResponse({ status: 200, data: { exists: allExist, results }, headers: {} }, res);
});
app.get('/info', (_req, res) => {
res.send({
node_version: process.version,
renderer_version: packageJson.version,
});
});
// In tests we will run worker in master thread, so we need to ensure server
// will not listen:
// we are extracting worker from cluster to avoid false TS error
const { worker } = cluster;
if (workersCount === 0 || cluster.isWorker) {
app.listen({ port, host }, (err, address) => {
if (err) {
handleStartupListenError({ err, host, port });
return;
}
const workerName = worker ? `worker #${worker.id}` : 'master (single-process)';
log.info({ workerName, address }, 'Node renderer listening');
});
}
fastifyConfigFunctions.forEach((configFunction) => {
configFunction(app);
});
return app;
}