-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server.js: Refactor the "opening a profile file" logic in the start s…
…erver code (PR #4656)
- Loading branch information
Showing
2 changed files
with
35 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters