-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
112 lines (96 loc) · 2.77 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: strong-debugger
// US Government Users Restricted Rights - Use, duplication or disclosure
// restricted by GSA ADP Schedule Contract with IBM Corp.
'use strict';
var path = require('path');
var bindings = require('bindings');
var dbg = bindings('debugger');
var debuglogEnabled = require('debug')('strong-debugger').enabled;
var GATHER_CODE_COVERAGE = process.env.NYC_CWD; // jscs:disable
var COVERAGE_REPORT_INDEX = 0;
var COVERAGE_REPORT_BASE = path.join(__dirname, '.nyc_output',
process.pid + '-backend-');
var SCRIPT_ROOT = [
path.dirname(require.resolve('./backend/context.js')),
GATHER_CODE_COVERAGE ? '.cov' : '',
path.sep
].join('');
var startCallbacks = [];
var stopCallbacks = [];
var listeningOnPort = -1;
/**
* Start the background thread providing TCP server for DevTools protocol.
* @param {Number} port
* @param {Function<Error=,Number>} callback The first argument is the error
* (if any), the second argument is the actual port number where the debugger
* is listening on.
*/
exports.start = function(port, cb) {
if (!cb) throw new Error('You must supply a callback argument.');
if (listeningOnPort > 0) {
// The background thread is already running.
return process.nextTick(function() {
cb(null, listeningOnPort);
});
}
startCallbacks.push(cb);
if (startCallbacks.length > 1) return;
try {
dbg.start({
port: port,
scriptRoot: SCRIPT_ROOT,
debuglogEnabled: debuglogEnabled,
codeCoverageReport: GATHER_CODE_COVERAGE ?
COVERAGE_REPORT_BASE + COVERAGE_REPORT_INDEX++ + '.json' : undefined
}, onStarted);
} catch (err) {
startCallbacks = [];
throw err;
}
function onStarted(err, port) {
if (!err && port !== undefined) listeningOnPort = port;
var list = startCallbacks;
startCallbacks = [];
list.forEach(function(cb) {
cb(err, port);
});
}
};
/**
* Stop the background thread.
* @param {Function<Error=>} callback
*/
exports.stop = function(cb) {
if (listeningOnPort < 0) {
// The background thread is not running.
return process.nextTick(cb);
}
stopCallbacks.push(cb);
if (stopCallbacks.length > 1) return;
try {
dbg.stop(onStopped);
} catch (err) {
stopCallbacks = [];
throw err;
}
function onStopped(err) {
listeningOnPort = -1;
var list = stopCallbacks;
stopCallbacks = [];
list.forEach(function(cb) {
cb(err);
});
}
};
/**
* Get the status of the debugger thread (sync).
* @return {Object} Status object with the following properties: running, port.
*/
exports.status = function() {
var running = listeningOnPort > 0;
return {
running: running,
port: running ? listeningOnPort : null
};
};