forked from tenderlove/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile-server.js
32 lines (29 loc) · 1.18 KB
/
profile-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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));
});
},
};