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: add anyproxy rule #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var readFileSync = fs.readFileSync;
program
.version(require('./package').version, '-v, --version')
.option('-p, --port <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')
Expand Down
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,37 @@ function Server(compiler, opts) {

!anyproxy.isRootCAFileExists() && anyproxy.generateRootCA();

new anyproxy.proxyServer({

var anyproxy_opts = {
type: 'http',
port: opts.anyproxyPort || 8989,
hostname: 'localhost',
rule: require('./rule')({
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 支持
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/anyproxy/README.md
Original file line number Diff line number Diff line change
@@ -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');`
1 change: 1 addition & 0 deletions test/fixtures/anyproxy/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('a.js');
7 changes: 7 additions & 0 deletions test/fixtures/anyproxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"spm": {
"output": [
"a.js"
]
}
}
11 changes: 11 additions & 0 deletions test/fixtures/anyproxy/rule__anyproxy.js
Original file line number Diff line number Diff line change
@@ -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);
}
};