From 91b485c464eff46d7191aaa93dc0852b8107f83c Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 4 Jun 2023 23:49:18 +0800 Subject: [PATCH 1/4] server.js: Open local `from-url` URL in separate script --- .eslintrc.js | 1 + bin/launch-fp.sh | 2 +- launch-fp.mjs | 209 +++++++++++++++++++++++++++++++++++++++++++++++ server.js | 53 ++---------- 4 files changed, 216 insertions(+), 49 deletions(-) create mode 100644 launch-fp.mjs diff --git a/.eslintrc.js b/.eslintrc.js index f5154b4f16..80fa312d44 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -141,6 +141,7 @@ module.exports = { ], extensions: ['.js', '.jpg'], }, + node: true, }, }, globals: { diff --git a/bin/launch-fp.sh b/bin/launch-fp.sh index 4d6592a294..5e3d08dc50 100755 --- a/bin/launch-fp.sh +++ b/bin/launch-fp.sh @@ -10,4 +10,4 @@ SCRIPTPATH=$(realpath "$0") SCRIPTDIR=$(dirname "$SCRIPTPATH") -NODE_ENV=development node "$SCRIPTDIR/../server.js" "$@" +node "$SCRIPTDIR/../launch-fp.mjs" "$@" diff --git a/launch-fp.mjs b/launch-fp.mjs new file mode 100644 index 0000000000..37927971e7 --- /dev/null +++ b/launch-fp.mjs @@ -0,0 +1,209 @@ +/* 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 + +import cp from 'child_process'; +import fs from 'fs'; +import fsPromises from 'fs/promises'; +import http from 'node:http'; +import open from 'open'; +import os from 'os'; +import path from 'path'; +import readline from 'readline'; +import url from 'url'; +import yargs from 'yargs'; +import { hideBin } from 'yargs/helpers'; +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); + +const argv = yargs(hideBin(process.argv)) + .command('* [profile]', 'Open Firefox Profiler, on [profile] if included.') + .option('c', { + alias: 'config', + describe: 'Path to local webpack config', + type: 'string', + }) + // Disabled --version flag since no version number in package.json. + .version(false) + .strict() + .parseSync(); + +// Determine if there is a local config to be processed. +const defaultLocalConfigPath = path.join( + __dirname, + './webpack.local-config.js' +); +let localConfigPath; +if (argv.config) { + localConfigPath = path.resolve(argv.config); +} else if (fs.existsSync(defaultLocalConfigPath)) { + localConfigPath = defaultLocalConfigPath; +} + +let localConfigTempDir; +if (argv.profile) { + // Need to prevent webpack from opening a browser even if requested in the + // local config file since this code does that for the user so a temporary + // overriding config file is created here. + try { + localConfigTempDir = await fsPromises.mkdtemp( + path.join(os.tmpdir(), 'launch-fp-') + ); + } catch (error) { + console.error('Unable to create launch-fp temporary directory'); + console.error(error); + process.exit(1); + } + + // This is the main function in that temporary config file. + const localConfig = function (config, serverConfig) { + // Read from original local config file. + const localConfigPath = '<>'; + const configRequirePath = `./${path.relative(__dirname, localConfigPath)}`; + try { + require(configRequirePath)(config, serverConfig); + } catch (error) { + console.error( + `Unable to load and apply settings from ${configRequirePath}` + ); + console.error(error); + } + + // Delete "open" target (if any) in serverConfig. + if ( + typeof serverConfig.open === 'object' && + !Array.isArray(serverConfig.open) && + serverConfig.open !== null + ) { + delete serverConfig.open.target; + } else { + delete serverConfig.open; + } + + // Save and delete "open" property from serverConfig so that + // webpack-dev-server doesn't open anything in tandem. + const openOptions = serverConfig.open; + delete serverConfig.open; + + return { origConfigPath: localConfigPath, openOptions }; + }; + + // This is the prologue code for the temporary config file. + const localConfigText = ` /* 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 path = require('path'); + + module.exports = ${localConfig + .toString() + .replace('<>', localConfigPath)} + `; + + if (localConfigTempDir) { + // Write to the temporary config file. + try { + await fsPromises.writeFile( + `${localConfigTempDir}/launch-fp-webpack.local-config.js`, + localConfigText + ); + } catch (error) { + console.error(`Unable to write to ${localConfigTempDir}`); + console.log(error); + } + + // Cleanup on CTRL-C. + const rmTempConfigDir = () => { + fs.rm( + localConfigTempDir, + { maxRetries: 10, recursive: true }, + (error) => { + if (error) { + console.error(`Unable to remove ${localConfigTempDir}`); + console.error(error); + } + } + ); + }; + process.on('SIGINT', rmTempConfigDir); + process.on('SIGTERM', rmTempConfigDir); + } +} + +// Spawn the profiler server. +let configFlagVal; +if (argv.profile) { + configFlagVal = `${localConfigTempDir}/launch-fp-webpack.local-config.js`; +} else if (argv.config) { + configFlagVal = argv.config; +} +const fpServer = cp.spawn( + process.argv[0], + [ + path.join(__dirname, './server.js'), + ...(configFlagVal ? ['--config', configFlagVal] : []), + ], + { + env: { ...process.env, NODE_ENV: 'development' }, + stdio: ['inherit', 'pipe', 'inherit'], + } +); + +// Wait until the profiler server is up before connecting to it, for better UX. +const fpServerStdout = readline.createInterface({ input: fpServer.stdout }); +// Assumption: once there is output from the fpServer process, the server is up. +await new Promise((resolve) => { + fpServerStdout.on('line', (line) => { + console.log(line); + resolve(); + }); +}); + +if (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. + const closeProfileServer = () => { + profileServer.close(); + // For quick profile server closing. + profileServer.closeAllConnections(); + }; + process.on('SIGINT', closeProfileServer); + process.on('SIGTERM', closeProfileServer); + + // Read local launch-fp webpack config. + let configDetails; + if (localConfigPath) { + const config = {}; + const serverConfig = {}; + const configImportPath = `${localConfigTempDir}/launch-fp-webpack.local-config.js`; + try { + configDetails = (await import(configImportPath)).default( + config, + serverConfig + ); + } catch (error) { + console.error(`Unable to load settings from ${configImportPath}`); + console.error(error); + } + } + + // Open on profile. + const port = process.env.FX_PROFILER_PORT || 4242; + const host = process.env.FX_PROFILER_HOST || 'localhost'; + const profilerUrl = `http://${host}:${port}`; + profileServer.listen(0, host, () => { + const profileFromUrl = `${profilerUrl}/from-url/${encodeURIComponent( + `http://${host}:${profileServer.address().port}/${encodeURIComponent( + path.basename(argv.profile) + )}` + )}`; + open(profileFromUrl, configDetails.openOptions); + }); +} diff --git a/server.js b/server.js index 44c21ce04e..f7bc4eb67e 100644 --- a/server.js +++ b/server.js @@ -4,7 +4,6 @@ // @noflow const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); -const http = require('node:http'); const config = require('./webpack.config'); const { oneLine, stripIndent } = require('common-tags'); const port = process.env.FX_PROFILER_PORT || 4242; @@ -15,7 +14,6 @@ const yargs = require('yargs'); const { hideBin } = require('yargs/helpers'); const argv = yargs(hideBin(process.argv)) - .command('* [profile]', 'Open Firefox Profiler, on [profile] if included.') .option('c', { alias: 'config', describe: 'Path to local webpack config', @@ -83,8 +81,10 @@ const defaultLocalConfigPath = path.join( const readConfig = (localConfigPath) => { const configRequirePath = `./${path.relative(__dirname, localConfigPath)}`; try { - require(configRequirePath)(config, serverConfig); - localConfigFile = path.basename(configRequirePath); + const configDetails = require(configRequirePath)(config, serverConfig); + localConfigFile = path.basename( + configDetails?.origConfigPath ?? configRequirePath + ); } catch (error) { console.error( `Unable to load and apply settings from ${configRequirePath}` @@ -98,49 +98,6 @@ if (argv.config) { readConfig(defaultLocalConfigPath); } -const profilerUrl = `http://${host}:${port}`; -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' && - !Array.isArray(serverConfig.open) && - serverConfig.open !== null - ) { - delete serverConfig.open.target; - } else { - delete serverConfig.open; - } - - // Save and delete "open" property from serverConfig so that - // webpack-dev-server doesn't open anything in tandem. - 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)); - }); -} - process.chdir(__dirname); // Allow server.js to be run from anywhere. const server = new WebpackDevServer(serverConfig, webpack(config)); server @@ -150,7 +107,7 @@ server '------------------------------------------------------------------------------------------'; console.log(barAscii); - console.log(`> Firefox Profiler is listening at: ${profilerUrl}\n`); + console.log(`> Firefox Profiler is listening at: http://${host}:${port}\n`); if (port === 4242) { console.log( '> You can change this default port with the environment variable FX_PROFILER_PORT.\n' From d2cf68547c5e5e3567dc31459dbd79e6a4d60aa2 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Tue, 19 Sep 2023 19:12:21 +0800 Subject: [PATCH 2/4] Revert "server.js: Open local `from-url` URL in separate script" This reverts commit 91b485c464eff46d7191aaa93dc0852b8107f83c. --- .eslintrc.js | 1 - bin/launch-fp.sh | 2 +- launch-fp.mjs | 209 ----------------------------------------------- server.js | 53 ++++++++++-- 4 files changed, 49 insertions(+), 216 deletions(-) delete mode 100644 launch-fp.mjs diff --git a/.eslintrc.js b/.eslintrc.js index 80fa312d44..f5154b4f16 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -141,7 +141,6 @@ module.exports = { ], extensions: ['.js', '.jpg'], }, - node: true, }, }, globals: { diff --git a/bin/launch-fp.sh b/bin/launch-fp.sh index 5e3d08dc50..4d6592a294 100755 --- a/bin/launch-fp.sh +++ b/bin/launch-fp.sh @@ -10,4 +10,4 @@ SCRIPTPATH=$(realpath "$0") SCRIPTDIR=$(dirname "$SCRIPTPATH") -node "$SCRIPTDIR/../launch-fp.mjs" "$@" +NODE_ENV=development node "$SCRIPTDIR/../server.js" "$@" diff --git a/launch-fp.mjs b/launch-fp.mjs deleted file mode 100644 index 37927971e7..0000000000 --- a/launch-fp.mjs +++ /dev/null @@ -1,209 +0,0 @@ -/* 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 - -import cp from 'child_process'; -import fs from 'fs'; -import fsPromises from 'fs/promises'; -import http from 'node:http'; -import open from 'open'; -import os from 'os'; -import path from 'path'; -import readline from 'readline'; -import url from 'url'; -import yargs from 'yargs'; -import { hideBin } from 'yargs/helpers'; -const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); - -const argv = yargs(hideBin(process.argv)) - .command('* [profile]', 'Open Firefox Profiler, on [profile] if included.') - .option('c', { - alias: 'config', - describe: 'Path to local webpack config', - type: 'string', - }) - // Disabled --version flag since no version number in package.json. - .version(false) - .strict() - .parseSync(); - -// Determine if there is a local config to be processed. -const defaultLocalConfigPath = path.join( - __dirname, - './webpack.local-config.js' -); -let localConfigPath; -if (argv.config) { - localConfigPath = path.resolve(argv.config); -} else if (fs.existsSync(defaultLocalConfigPath)) { - localConfigPath = defaultLocalConfigPath; -} - -let localConfigTempDir; -if (argv.profile) { - // Need to prevent webpack from opening a browser even if requested in the - // local config file since this code does that for the user so a temporary - // overriding config file is created here. - try { - localConfigTempDir = await fsPromises.mkdtemp( - path.join(os.tmpdir(), 'launch-fp-') - ); - } catch (error) { - console.error('Unable to create launch-fp temporary directory'); - console.error(error); - process.exit(1); - } - - // This is the main function in that temporary config file. - const localConfig = function (config, serverConfig) { - // Read from original local config file. - const localConfigPath = '<>'; - const configRequirePath = `./${path.relative(__dirname, localConfigPath)}`; - try { - require(configRequirePath)(config, serverConfig); - } catch (error) { - console.error( - `Unable to load and apply settings from ${configRequirePath}` - ); - console.error(error); - } - - // Delete "open" target (if any) in serverConfig. - if ( - typeof serverConfig.open === 'object' && - !Array.isArray(serverConfig.open) && - serverConfig.open !== null - ) { - delete serverConfig.open.target; - } else { - delete serverConfig.open; - } - - // Save and delete "open" property from serverConfig so that - // webpack-dev-server doesn't open anything in tandem. - const openOptions = serverConfig.open; - delete serverConfig.open; - - return { origConfigPath: localConfigPath, openOptions }; - }; - - // This is the prologue code for the temporary config file. - const localConfigText = ` /* 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 path = require('path'); - - module.exports = ${localConfig - .toString() - .replace('<>', localConfigPath)} - `; - - if (localConfigTempDir) { - // Write to the temporary config file. - try { - await fsPromises.writeFile( - `${localConfigTempDir}/launch-fp-webpack.local-config.js`, - localConfigText - ); - } catch (error) { - console.error(`Unable to write to ${localConfigTempDir}`); - console.log(error); - } - - // Cleanup on CTRL-C. - const rmTempConfigDir = () => { - fs.rm( - localConfigTempDir, - { maxRetries: 10, recursive: true }, - (error) => { - if (error) { - console.error(`Unable to remove ${localConfigTempDir}`); - console.error(error); - } - } - ); - }; - process.on('SIGINT', rmTempConfigDir); - process.on('SIGTERM', rmTempConfigDir); - } -} - -// Spawn the profiler server. -let configFlagVal; -if (argv.profile) { - configFlagVal = `${localConfigTempDir}/launch-fp-webpack.local-config.js`; -} else if (argv.config) { - configFlagVal = argv.config; -} -const fpServer = cp.spawn( - process.argv[0], - [ - path.join(__dirname, './server.js'), - ...(configFlagVal ? ['--config', configFlagVal] : []), - ], - { - env: { ...process.env, NODE_ENV: 'development' }, - stdio: ['inherit', 'pipe', 'inherit'], - } -); - -// Wait until the profiler server is up before connecting to it, for better UX. -const fpServerStdout = readline.createInterface({ input: fpServer.stdout }); -// Assumption: once there is output from the fpServer process, the server is up. -await new Promise((resolve) => { - fpServerStdout.on('line', (line) => { - console.log(line); - resolve(); - }); -}); - -if (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. - const closeProfileServer = () => { - profileServer.close(); - // For quick profile server closing. - profileServer.closeAllConnections(); - }; - process.on('SIGINT', closeProfileServer); - process.on('SIGTERM', closeProfileServer); - - // Read local launch-fp webpack config. - let configDetails; - if (localConfigPath) { - const config = {}; - const serverConfig = {}; - const configImportPath = `${localConfigTempDir}/launch-fp-webpack.local-config.js`; - try { - configDetails = (await import(configImportPath)).default( - config, - serverConfig - ); - } catch (error) { - console.error(`Unable to load settings from ${configImportPath}`); - console.error(error); - } - } - - // Open on profile. - const port = process.env.FX_PROFILER_PORT || 4242; - const host = process.env.FX_PROFILER_HOST || 'localhost'; - const profilerUrl = `http://${host}:${port}`; - profileServer.listen(0, host, () => { - const profileFromUrl = `${profilerUrl}/from-url/${encodeURIComponent( - `http://${host}:${profileServer.address().port}/${encodeURIComponent( - path.basename(argv.profile) - )}` - )}`; - open(profileFromUrl, configDetails.openOptions); - }); -} diff --git a/server.js b/server.js index f7bc4eb67e..44c21ce04e 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,7 @@ // @noflow const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); +const http = require('node:http'); const config = require('./webpack.config'); const { oneLine, stripIndent } = require('common-tags'); const port = process.env.FX_PROFILER_PORT || 4242; @@ -14,6 +15,7 @@ const yargs = require('yargs'); const { hideBin } = require('yargs/helpers'); const argv = yargs(hideBin(process.argv)) + .command('* [profile]', 'Open Firefox Profiler, on [profile] if included.') .option('c', { alias: 'config', describe: 'Path to local webpack config', @@ -81,10 +83,8 @@ const defaultLocalConfigPath = path.join( const readConfig = (localConfigPath) => { const configRequirePath = `./${path.relative(__dirname, localConfigPath)}`; try { - const configDetails = require(configRequirePath)(config, serverConfig); - localConfigFile = path.basename( - configDetails?.origConfigPath ?? configRequirePath - ); + require(configRequirePath)(config, serverConfig); + localConfigFile = path.basename(configRequirePath); } catch (error) { console.error( `Unable to load and apply settings from ${configRequirePath}` @@ -98,6 +98,49 @@ if (argv.config) { readConfig(defaultLocalConfigPath); } +const profilerUrl = `http://${host}:${port}`; +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' && + !Array.isArray(serverConfig.open) && + serverConfig.open !== null + ) { + delete serverConfig.open.target; + } else { + delete serverConfig.open; + } + + // Save and delete "open" property from serverConfig so that + // webpack-dev-server doesn't open anything in tandem. + 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)); + }); +} + process.chdir(__dirname); // Allow server.js to be run from anywhere. const server = new WebpackDevServer(serverConfig, webpack(config)); server @@ -107,7 +150,7 @@ server '------------------------------------------------------------------------------------------'; console.log(barAscii); - console.log(`> Firefox Profiler is listening at: http://${host}:${port}\n`); + console.log(`> Firefox Profiler is listening at: ${profilerUrl}\n`); if (port === 4242) { console.log( '> You can change this default port with the environment variable FX_PROFILER_PORT.\n' From 24c73d5b260906dab014087a046ca782b6d91531 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Tue, 19 Sep 2023 20:56:45 +0800 Subject: [PATCH 3/4] Refactor profile server code into own file --- profile-server.js | 34 ++++++++++++++++++++++++++++++++++ server.js | 22 +++------------------- 2 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 profile-server.js diff --git a/profile-server.js b/profile-server.js new file mode 100644 index 0000000000..53364a0d5b --- /dev/null +++ b/profile-server.js @@ -0,0 +1,34 @@ +/* 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 = { + start(host, profilerUrl, profilePath, callback) { + // 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) + )}` + )}`; + if (callback) { + callback(profileFromUrl); + } + }); + }, +}; diff --git a/server.js b/server.js index 718a3dec6a..15b4d89de0 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,13 +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) - )}` - )}`; + // Start profile server and open on profile. + profileServer.start(host, profilerUrl, argv.profile, (profileFromUrl) => { import('open').then((open) => open.default(profileFromUrl, openOptions)); }); } From f9ce3fde35709fd6d49eaff9fadcdc267af8a04e Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Wed, 27 Sep 2023 20:09:36 +0800 Subject: [PATCH 4/4] Move URL opening to utility function --- profile-server.js | 6 ++---- server.js | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/profile-server.js b/profile-server.js index 53364a0d5b..65d9c0d512 100644 --- a/profile-server.js +++ b/profile-server.js @@ -7,7 +7,7 @@ const fs = require('fs'); const path = require('path'); module.exports = { - start(host, profilerUrl, profilePath, callback) { + 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); @@ -26,9 +26,7 @@ module.exports = { path.basename(profilePath) )}` )}`; - if (callback) { - callback(profileFromUrl); - } + import('open').then((open) => open.default(profileFromUrl, openOptions)); }); }, }; diff --git a/server.js b/server.js index 15b4d89de0..8fd72da838 100644 --- a/server.js +++ b/server.js @@ -119,9 +119,7 @@ if (argv.profile) { delete serverConfig.open; // Start profile server and open on profile. - profileServer.start(host, profilerUrl, argv.profile, (profileFromUrl) => { - import('open').then((open) => open.default(profileFromUrl, openOptions)); - }); + profileServer.serveAndOpen(host, profilerUrl, argv.profile, openOptions); } process.chdir(__dirname); // Allow server.js to be run from anywhere.