diff --git a/profile-server.js b/profile-server.js new file mode 100644 index 0000000000..65d9c0d512 --- /dev/null +++ b/profile-server.js @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +// @noflow +const http = require('node:http'); +const fs = require('fs'); +const path = require('path'); + +module.exports = { + serveAndOpen(host, profilerUrl, profilePath, openOptions) { + // Create a simple http server serving the profile file. + const profileServer = http.createServer((req, res) => { + res.setHeader('Access-Control-Allow-Origin', profilerUrl); + const fileStream = fs.createReadStream(profilePath); + fileStream.pipe(res); + }); + + // Close the profile server on CTRL-C. + process.on('SIGINT', () => profileServer.close()); + process.on('SIGTERM', () => profileServer.close()); + + // Spin up the profile server. + profileServer.listen(0, host, () => { + const profileFromUrl = `${profilerUrl}/from-url/${encodeURIComponent( + `http://${host}:${profileServer.address().port}/${encodeURIComponent( + path.basename(profilePath) + )}` + )}`; + import('open').then((open) => open.default(profileFromUrl, openOptions)); + }); + }, +}; diff --git a/server.js b/server.js index 718a3dec6a..8fd72da838 100644 --- a/server.js +++ b/server.js @@ -4,7 +4,7 @@ // @noflow const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); -const http = require('node:http'); +const profileServer = require('./profile-server'); const config = require('./webpack.config'); const { oneLine, stripIndent } = require('common-tags'); const port = process.env.FX_PROFILER_PORT || 4242; @@ -102,17 +102,6 @@ if (argv.profile) { // Needed because of a later working directory change. argv.profile = path.resolve(argv.profile); - // Spin up a simple http server serving the profile file. - const profileServer = http.createServer((req, res) => { - res.setHeader('Access-Control-Allow-Origin', profilerUrl); - const fileStream = fs.createReadStream(argv.profile); - fileStream.pipe(res); - }); - - // Close the profile server on CTRL-C. - process.on('SIGINT', () => profileServer.close()); - process.on('SIGTERM', () => profileServer.close()); - // Delete "open" target (if any) in serverConfig. if ( typeof serverConfig.open === 'object' && @@ -129,15 +118,8 @@ if (argv.profile) { const openOptions = serverConfig.open; delete serverConfig.open; - // Open on profile. - profileServer.listen(0, host, () => { - const profileFromUrl = `${profilerUrl}/from-url/${encodeURIComponent( - `http://${host}:${profileServer.address().port}/${encodeURIComponent( - path.basename(argv.profile) - )}` - )}`; - import('open').then((open) => open.default(profileFromUrl, openOptions)); - }); + // Start profile server and open on profile. + profileServer.serveAndOpen(host, profilerUrl, argv.profile, openOptions); } process.chdir(__dirname); // Allow server.js to be run from anywhere.