1/**2 * Copyright (c) Meta Platforms, Inc. and affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 *7 */89'use strict';1011const babelRegister = require('@babel/register');12babelRegister({13 ignore: [/[\\\/](build|server\/server|node_modules)[\\\/]/],14 presets: [['react-app', {runtime: 'automatic'}]],15 plugins: ['@babel/transform-modules-commonjs'],16});1718const express = require('express');19const compress = require('compression');20const {readFileSync} = require('fs');21const path = require('path');22const renderToString = require('./render-to-string');23const renderToStream = require('./render-to-stream');24const renderToBuffer = require('./render-to-buffer');25const {JS_BUNDLE_DELAY} = require('./delays');2627const PORT = process.env.PORT || 4000;28const app = express();2930app.use(compress());31app.get(32 '/',33 handleErrors(async function (req, res) {34 await waitForWebpack();35 renderToStream(req.url, res);36 })37);38app.get(39 '/string',40 handleErrors(async function (req, res) {41 await waitForWebpack();42 renderToString(req.url, res);43 })44);45app.get(46 '/stream',47 handleErrors(async function (req, res) {48 await waitForWebpack();49 renderToStream(req.url, res);50 })51);52app.get(53 '/buffer',54 handleErrors(async function (req, res) {55 await waitForWebpack();56 renderToBuffer(req.url, res);57 })58);59app.use(express.static('build'));60app.use(express.static('public'));6162app63 .listen(PORT, () => {64 console.log(`Listening at ${PORT}...`);65 })66 .on('error', function (error) {67 if (error.syscall !== 'listen') {68 throw error;69 }70 const isPipe = portOrPipe => Number.isNaN(portOrPipe);71 const bind = isPipe(PORT) ? 'Pipe ' + PORT : 'Port ' + PORT;72 switch (error.code) {73 case 'EACCES':74 console.error(bind + ' requires elevated privileges');75 process.exit(1);76 break;77 case 'EADDRINUSE':78 console.error(bind + ' is already in use');79 process.exit(1);80 break;81 default:82 throw error;83 }84 });8586function handleErrors(fn) {87 return async function (req, res, next) {88 try {89 return await fn(req, res);90 } catch (x) {91 next(x);92 }93 };94}9596async function waitForWebpack() {97 while (true) {98 try {99 readFileSync(path.resolve(__dirname, '../build/main.js'));100 return;101 } catch (err) {102 console.log(103 'Could not find webpack build output. Will retry in a second...'104 );105 await new Promise(resolve => setTimeout(resolve, 1000));106 }107 }108}
Findings
✓ No findings reported for this file.