Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revert debug options && add debug proxy support #73

Merged
merged 5 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ coverage/
!test/fixtures/demo-app/node_modules/aliyun-egg/node_modules/
!test/fixtures/test-files-glob/**
!test/fixtures/test-files-stack/node_modules/
!test/fixtures/example/node_modules/
**/run/*.json
.tmp
.vscode
*.log
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ Debug egg app with [V8 Inspector Integration](https://nodejs.org/api/debugger.ht

automatically detect the protocol used by the targeted runtime, 8.0+ the new 'inspector' protocol is used.

use [inspector-proxy](https://github.com/whxaxes/inspector-proxy) to proxy worker debug, so you don't need to worry about reload.

```bash
$ egg-bin debug
$ egg-bin debug --debug-port=9229 --proxy=9999
```

##### options

- all `egg-bin dev` options is accepted.
- `--debug-port=6666` worker debug port, default to 9229(inspect) or 5858(debug), also has `--inspect` alias.
- `--debug-brk` whether stop at the top of worker initial script, also has `--brk` alias.
- `--debug-agent=7777` whether debug agent, could pass Number as debugPort, default to 9227(inspect) or 5856(debug), also has `--agent` alias.
- `--debug-agent-brk` whether stop at the top of agent initial script.
- `--proxy=9999` worker debug proxy port.


### test

Expand Down
89 changes: 51 additions & 38 deletions lib/cmd/debug.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
'use strict';

const cp = require('child_process');
const chalk = require('chalk');
const InspectorProxy = require('inspector-proxy');
const debug = require('debug')('egg-bin');
const semver = require('semver');
const Command = require('./dev');
const newDebugger = semver.gte(process.version, '8.0.0');

class DebugCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
// egg-bin debug --debug-port=6666 --agent=5555 --brk --agent-brk
this.usage = 'Usage: egg-bin debug [dir] [options]';
this.options = {
debug: {
alias: 'inspect',
description: 'auto detect the protocol used by the targeted runtime, use inspect at 8.x+',
default: true,
},
'debug-port': {
description: 'worker debug port, default to 9229(inspect) or 5858(debug)',
// set default to empty so `--inspect` will always pass to fork
inspect: {
description: 'V8 Inspector port',
default() {
/* istanbul ignore next */
return newDebugger ? '' : undefined;
},
},
'debug-brk': {
alias: 'brk',
description: 'whether stop at the top of worker initial script',
'inspect-brk': {
description: 'whether break at start',
},
'debug-agent': {
alias: 'agent',
description: 'whether debug agent, could pass Number as debugPort, default to 9227(inspect) or 5856(debug)',

debug: {
description: 'legacy debugger',
default() {
/* istanbul ignore next */
return newDebugger ? undefined : '';
},
},
'debug-agent-brk': {
description: 'whether stop at the top of agent initial script',

proxy: {
description: 'worker debug proxy port',
default: 9999,
},
};
process.env.EGG_DEBUG = 'true';
Expand All @@ -35,32 +45,35 @@ class DebugCommand extends Command {
return 'Start server at local debug mode';
}

get context() {
const context = super.context;
const { argv, execArgvObj, debugOptions, debugPort } = context;

// use debugPort extract from `--inspect=9999 / --debug-port=1111` etc, if not provide just pass true
argv.debug = debugPort || true;
* run(context) {
const proxyPort = context.argv.proxy;
context.argv.proxy = undefined;

if (debugOptions['inspect-brk'] || debugOptions['debug-brk']) {
argv.debugBrk = true;
}

// remove unused
argv['debug-port'] = undefined;
argv['debug-brk'] = undefined;
argv['debug-agent'] = undefined;
argv['debug-agent-brk'] = undefined;
const eggArgs = yield this.formatArgs(context);
const options = {
execArgv: context.execArgv,
env: Object.assign({ NODE_ENV: 'development', EGG_DEBUG: true }, context.env),
};
debug('%s %j %j, %j', this.serverBin, eggArgs, options.execArgv, options.env.NODE_ENV);

// remove all debug options from execArgv
for (const key of Object.keys(debugOptions)) {
execArgvObj[key] = undefined;
}
// start egg
const child = cp.fork(this.serverBin, eggArgs, options);

// recreate execArgv array
context.execArgv = this.helper.unparseArgv(execArgvObj);
// start debug proxy
const proxy = new InspectorProxy({ port: proxyPort });
// proxy to new worker
child.on('message', msg => {
if (msg && msg.action === 'debug' && msg.from === 'app') {
const { debugPort, pid } = msg.data;
debug(`recieve new worker#${pid} debugPort: ${debugPort}`);
proxy.start({ debugPort }).then(() => {
console.log(chalk.yellow(`Debug Proxy online, now you could attach to ${proxyPort} without worry about reload.`));
if (newDebugger) console.log(chalk.yellow(`DevTools → ${proxy.url}`));
});
}
});

return context;
child.on('exit', () => proxy.end());
}
}

Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
},
"dependencies": {
"autod": "^2.9.0",
"chalk": "^2.1.0",
"co-mocha": "^1.2.0",
"common-bin": "^2.7.0",
"debug": "^3.0.1",
"detect-port": "^1.2.1",
"egg-utils": "^2.2.0",
"globby": "^6.1.0",
"inspector-proxy": "^1.0.0",
"intelli-espower-loader": "^1.0.1",
"mocha": "^3.5.0",
"mocha": "^3.5.3",
"mz-modules": "^2.0.0",
"nyc": "^11.2.0",
"nyc": "^11.2.1",
"power-assert": "^1.4.4",
"semver": "^5.4.1",
"test-exclude": "^4.1.1",
"ypkgfiles": "^1.4.0"
},
Expand All @@ -29,12 +32,14 @@
"babel-register": "^6.4.3",
"coffee": "^4.1.0",
"cross-env": "^3.1.3",
"egg": "^1.8.0",
"egg-ci": "^1.8.0",
"egg-mock": "^3.12.0",
"enzyme": "^2.0.0",
"eslint": "^4.6.1",
"eslint-config-egg": "^5.1.1",
"jsdom": "^8.0.1",
"mm": "^2.1.0",
"mm": "^2.2.0",
"mz": "^2.6.0",
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
Expand Down Expand Up @@ -71,4 +76,4 @@
"ci": {
"version": "6, 8"
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/example/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
this.body = 'hi, egg';
});
};
3 changes: 3 additions & 0 deletions test/fixtures/example/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.key = '12345';
8 changes: 8 additions & 0 deletions test/fixtures/example/node_modules/egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/example/node_modules/egg/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example"
}
100 changes: 35 additions & 65 deletions test/lib/cmd/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const path = require('path');
const coffee = require('coffee');
const mm = require('mm');
const mm = require('egg-mock');
const net = require('net');
const semver = require('semver');

describe('test/lib/cmd/debug.test.js', () => {
const eggBin = require.resolve('../../../bin/egg-bin.js');
Expand All @@ -13,73 +14,19 @@ describe('test/lib/cmd/debug.test.js', () => {

it('should startCluster success', () => {
return coffee.fork(eggBin, [ 'debug' ], { cwd })
.debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":true/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--debug-port=7777', () => {
return coffee.fork(eggBin, [ 'debug', '--debug-port=7777' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":7777/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--inspect=7777', () => {
return coffee.fork(eggBin, [ 'debug', '--inspect=7777' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":7777/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--debug-brk --debug-agent --debug-agent-brk --inspect-brk', () => {
return coffee.fork(eggBin, [ 'debug', '--debug-brk', '--debug-agent', '--debug-agent-brk', '--inspect-brk' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debugBrk":true,"debugAgent":true,"debugAgentBrk":true,"debug":true/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--brk --agent', () => {
return coffee.fork(eggBin, [ 'debug', '--brk', '--agent' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debugBrk":true,"debugAgent":true,"debug":true/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--agent=6666', () => {
return coffee.fork(eggBin, [ 'debug', '--agent=6666' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {"debugAgent":6666,"debug":true/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('stderr', /Debugger listening/)
// node 8 missing "chrome-devtools" url
// .expect('stderr', /chrome-devtools:/)
.expect('stdout', /"workers":1/)
.expect('code', 0)
.end();
});

it('should startCluster with port', () => {
return coffee.fork(eggBin, [ 'debug', '--port', '6001' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":true/)
.expect('stderr', /Debugger listening/)
.expect('stdout', /"port":6001/)
.expect('stdout', /"workers":1/)
.expect('stdout', /"baseDir":".*?demo-app"/)
Expand All @@ -89,12 +36,10 @@ describe('test/lib/cmd/debug.test.js', () => {
});

it('should debug with $NODE_DEBUG_OPTION', () => {
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect-brk=6666' });
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect=5555' });
return coffee.fork(eggBin, [ 'debug' ], { cwd, env })
.debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":6666/)
.expect('stdout', /options: {.*"debugBrk":true/)
// .debug()
.expect('stderr', /Debugger listening.*5555/)
.expect('stdout', /"workers":1/)
.expect('code', 0)
.end();
Expand All @@ -118,4 +63,29 @@ describe('test/lib/cmd/debug.test.js', () => {
.end();
});
});

describe('real egg', () => {
const cwd = path.join(__dirname, '../../fixtures/example');
const newDebugger = semver.gte(process.version, '8.0.0');

it('should proxy', function* () {
const app = coffee.fork(eggBin, [ 'debug' ], { cwd });
// app.debug();
if (newDebugger) app.expect('stdout', /DevTools → chrome-devtools:.*:9999/);
yield app.expect('stderr', /Debugger listening/)
.expect('stdout', /Debug Proxy online, now you could attach to 9999/)
.expect('code', 0)
.end();
});

it('should proxy with port', function* () {
const app = coffee.fork(eggBin, [ 'debug', '--proxy=6666' ], { cwd });
// app.debug();
if (newDebugger) app.expect('stdout', /DevTools → chrome-devtools:.*:6666/);
yield app.expect('stderr', /Debugger listening/)
.expect('stdout', /Debug Proxy online, now you could attach to 6666/)
.expect('code', 0)
.end();
});
});
});
8 changes: 0 additions & 8 deletions test/lib/cmd/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ describe('test/lib/cmd/dev.test.js', () => {
});
});

it.skip('should startCluster with execArgv --debug', done => {
coffee.fork(eggBin, [ 'dev', '--debug=7000' ], { cwd })
.debug()
.expect('stderr', /Debugger listening on .*7000/)
.expect('code', 0)
.end(done);
});

it('should startCluster with execArgv --inspect', done => {
coffee.fork(eggBin, [ 'dev', '--inspect=7000' ], { cwd })
// .debug()
Expand Down