Files
react-native/local-cli/server/middleware/heapCaptureMiddleware.js
T
Charles Dick 54f867f0d6 Process heap capture into trace html
Reviewed By: bestander

Differential Revision: D3642188

fbshipit-source-id: c9a4699b2a0d60eb5961333dec45941085e19324
2016-08-08 04:28:32 -07:00

46 lines
1.5 KiB
JavaScript

/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
/*eslint no-console-disallow: "off"*/
const spawn = require('child_process').spawn;
const fs = require('fs');
const path = require('path');
module.exports = function(req, res, next) {
if (req.url !== '/jscheapcaptureupload') {
next();
return;
}
console.log('Downloading Heap Capture');
var preload = path.join(__dirname, 'heapCapture/preLoadedCapture.js');
fs.writeFileSync(preload, 'var preLoadedCapture = ');
fs.appendFileSync(preload, req.rawBody);
fs.appendFileSync(preload, ';');
res.end();
console.log('Packaging Trace');
var captureHtml = path.join(__dirname, 'heapCapture/captures/capture_' + Date.now() + '.html');
var capture = fs.createWriteStream(captureHtml);
var inliner = spawn(
'inliner',
['--nocompress', 'heapCapture.html'],
{ cwd: path.join(__dirname, '/heapCapture/'),
stdio: [ process.stdin, 'pipe', process.stderr ],
});
inliner.stdout.pipe(capture);
inliner.on('exit', (code, signal) => {
if (code === 0) {
console.log('Heap capture written to: ' + captureHtml);
} else {
console.error('Error processing heap capture, inliner returned code: ' + code);
}
});
};