From e007664cf1d7ceb7c506aab0a0ddd65c767d9626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=92=B0?= Date: Mon, 21 Dec 2015 20:51:59 +0800 Subject: [PATCH] feat: add anyproxy rule --- cli.js | 2 +- index.js | 26 ++++++++++++++++++++++-- test/fixtures/anyproxy/README.md | 15 ++++++++++++++ test/fixtures/anyproxy/a.js | 1 + test/fixtures/anyproxy/package.json | 7 +++++++ test/fixtures/anyproxy/rule__anyproxy.js | 11 ++++++++++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/anyproxy/README.md create mode 100644 test/fixtures/anyproxy/a.js create mode 100644 test/fixtures/anyproxy/package.json create mode 100644 test/fixtures/anyproxy/rule__anyproxy.js diff --git a/cli.js b/cli.js index 509ba20..418bb5c 100755 --- a/cli.js +++ b/cli.js @@ -16,7 +16,7 @@ var readFileSync = fs.readFileSync; program .version(require('./package').version, '-v, --version') .option('-p, --port ', 'port') - .option('--proxy', 'proxy with anyproxy') + .option('--proxy [rule]', 'proxy with anyproxy. rule is anyproxy rule file.') .option('--livereload', 'livereload') .option('--compress', 'build files with compress') .option('--weinre', 'weinre') diff --git a/index.js b/index.js index 91ac598..4cc8db4 100644 --- a/index.js +++ b/index.js @@ -188,7 +188,8 @@ function Server(compiler, opts) { !anyproxy.isRootCAFileExists() && anyproxy.generateRootCA(); - new anyproxy.proxyServer({ + + var anyproxy_opts = { type: 'http', port: opts.anyproxyPort || 8989, hostname: 'localhost', @@ -196,7 +197,28 @@ function Server(compiler, opts) { port: opts.port, hostname: ip }) - }); + }; + + if (typeof opts.proxy == 'string') { + var rule_path = path.join(process.cwd(), opts.proxy); + + if (fs.existsSync(rule_path) && fs.statSync(rule_path).isFile()) { + try { + var rule = require(rule_path); + } catch (e) { + log.error('error', rule_path + ' is not a correct anyproxy rule file'); + process.exit(1); + } + + anyproxy_opts = { + rule: rule + }; + } else { + log.error('error', rule_path + ' is not a correct anyproxy rule file. Use default anyproxy options'); + } + } + + new anyproxy.proxyServer(anyproxy_opts); } // https 支持 diff --git a/test/fixtures/anyproxy/README.md b/test/fixtures/anyproxy/README.md new file mode 100644 index 0000000..e2ae843 --- /dev/null +++ b/test/fixtures/anyproxy/README.md @@ -0,0 +1,15 @@ +> anyproxy的测试用例不会写~ + +测试方法: + +``` +cd test/fixtures/anyproxy + +../../../cli.js --proxy rule__anyproxy.js +``` + +浏览器中配置anyproxy的HTTP代理:127.0.0.1 端口8001 + +访问:http://jianyu.alipay.net:8000/a.js + +会发现原来的`console.log('a.js');`变成了`console.log('b.js');` diff --git a/test/fixtures/anyproxy/a.js b/test/fixtures/anyproxy/a.js new file mode 100644 index 0000000..281c440 --- /dev/null +++ b/test/fixtures/anyproxy/a.js @@ -0,0 +1 @@ +console.log('a.js'); diff --git a/test/fixtures/anyproxy/package.json b/test/fixtures/anyproxy/package.json new file mode 100644 index 0000000..9cc16fd --- /dev/null +++ b/test/fixtures/anyproxy/package.json @@ -0,0 +1,7 @@ +{ + "spm": { + "output": [ + "a.js" + ] + } +} diff --git a/test/fixtures/anyproxy/rule__anyproxy.js b/test/fixtures/anyproxy/rule__anyproxy.js new file mode 100644 index 0000000..6488f10 --- /dev/null +++ b/test/fixtures/anyproxy/rule__anyproxy.js @@ -0,0 +1,11 @@ +module.exports = { + summary: function () { + return 'my anyproxy'; + }, + + replaceServerResDataAsync: function (req, res, serverResData, callback) { + serverResData = serverResData.toString().replace('a.js', 'b.js'); + callback(serverResData); + } +}; +