Skip to content

Commit

Permalink
server.js: Refactor the "opening a profile file" logic in the start s…
Browse files Browse the repository at this point in the history
…erver code (PR #4656)
  • Loading branch information
kazarmy authored Oct 3, 2023
1 parent 7104031 commit 9406a98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
32 changes: 32 additions & 0 deletions profile-server.js
Original file line number Diff line number Diff line change
@@ -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));
});
},
};
24 changes: 3 additions & 21 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' &&
Expand All @@ -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.
Expand Down

0 comments on commit 9406a98

Please sign in to comment.