UNPKG

3 kBJavaScriptView Raw
1var semver = require('semver');
2
3var injection = require.resolve('./Injections/ProfilerAgent');
4/**
5 * @param {{inject}} config
6 * @param {DebuggerClient} debuggerClient
7 * @param {InjectorClient} injectorClient
8 * @param {FrontendClient} frontendClient
9*/
10function ProfilerAgent(config, session) {
11 try {
12 this._noInject = config.inject === false || config.inject.profiles === false;
13 } catch (e) {
14 this._noInject = false;
15 }
16
17 this._injected = false;
18 this._debuggerClient = session.debuggerClient;
19 this._injectorClient = session.injectorClient;
20 this._frontendClient = session.frontendClient;
21
22 this._translateCommandToInjection(
23 'start',
24 'stop'
25 );
26
27 if (!this._noInject) this._injectorClient.on('inject', this._inject.bind(this));
28}
29
30ProfilerAgent.prototype._inject = function(injected) {
31 this._translateEventToFrontend(
32 'consoleProfileStarted',
33 'consoleProfileFinished'
34 );
35
36 this._injectorClient.injection(
37 function(require, debug, options) {
38 require(options.injection)(require, debug, options);
39 },
40 {
41 injection: injection,
42 'v8-profiler': require.resolve('v8-profiler')
43 },
44 function(error, result) {
45 this._injected = !error;
46
47 if (error) return this._frontendClient.sendLogToConsole('error', error.message || error);
48 }.bind(this)
49 );
50};
51
52/**
53 * @param {...string} eventNames
54*/
55ProfilerAgent.prototype._translateEventToFrontend = function(eventNames) {
56 Array.prototype.forEach.call(arguments, function(event) {
57 event = 'Profiler.' + event;
58 this._debuggerClient.on(event, function(message) {
59 this._frontendClient.sendEvent(event, message);
60 }.bind(this));
61 }, this);
62};
63
64/**
65 * @param {...string} commandNames
66*/
67ProfilerAgent.prototype._translateCommandToInjection = function(commandNames) {
68 Array.prototype.forEach.call(arguments, function(command) {
69 this[command] = function(params, done) {
70 this._debuggerClient.request('Profiler.' + command, params, done);
71 };
72 }, this);
73};
74
75ProfilerAgent.prototype.enable = function(params, done) {
76 done();
77
78 if (this._debuggerClient.isReady) {
79 this._checkCompatibility();
80 } else {
81 this._debuggerClient.on('connect', this._checkCompatibility.bind(this));
82 }
83};
84
85ProfilerAgent.prototype._checkCompatibility = function() {
86 var version = this._debuggerClient.target.nodeVersion;
87 var isCompatible = ProfilerAgent.nodeVersionIsCompatible(version);
88 if (!isCompatible) {
89 this._frontendClient.sendLogToConsole(
90 'warning',
91 'Your Node version (' + version + ') has a partial support of profiler.\n' +
92 'The stack frames tree doesn\'t show all stack frames due to low sampling rate.\n' +
93 'The profiling data is incomplete and may show misleading results.\n' +
94 'Update Node to v0.11.13 or newer to get full support.'
95 );
96 }
97};
98
99ProfilerAgent.nodeVersionIsCompatible = function(version) {
100 return semver.satisfies(version, '>=0.11.13');
101};
102
103exports.ProfilerAgent = ProfilerAgent;