Skip to content

Commit

Permalink
Merge 6cc4191 into 760df08
Browse files Browse the repository at this point in the history
  • Loading branch information
rmg committed May 8, 2016
2 parents 760df08 + 6cc4191 commit a2b1bfb
Show file tree
Hide file tree
Showing 52 changed files with 952 additions and 311 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ checkstyle.xml
tests.tap
.travis.yml
appveyor.yml
.nyc_output
sandbox
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
coverage
.nyc_output
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
language: node_js
node_js:
- "6"
- "5"
- "4"
- "0.12"
- "0.10"
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
env:
global:
- COVERALLS_PARALLEL=true
- secure: rxAxzXc6ff4UQIMYD46SZQP37fIDdZ2IlMtPx34dp61sIQj9dKAYmjtwr4J7Ra+qiisdia+szvfNLaPa6ThCWj6OOhdwNMd/vRsoGQGHHYMf2qU+uMwA+8TP9WzjLlEVtu3TiU+gNb8YEfMW6TzTRKSHGwC1aNYOa14PhhAygYY=
12 changes: 9 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
environment:
COVERALLS_REPO_TOKEN:
secure: sxoCWFzAUW5WrH5KKKY38kZo1oB3Bu+WWJDshymilkDq/rKR4j9qXmrf6Ae/ekt8
matrix:
- nodejs_version: '6'
- nodejs_version: '5'
Expand All @@ -7,15 +9,19 @@ environment:
- nodejs_version: '0.10'
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
- npm -g install npm@latest
- set PATH=%APPDATA%\npm;%PATH%
- npm install
matrix:
fast_finish: true
branches:
only:
- master
build: off
version: '{build}'
shallow_clone: true
clone_depth: 1
clone_depth: 10
test_script:
- npm test -- -Rclassic --no-coverage --timeout=3600
- set CI=true
- set COVERALLS_PARALLEL=true
- npm test -- --reporter=tap
2 changes: 1 addition & 1 deletion demo/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"port": 80,
"port": 8000,
"bind": "0.0.0.0",
"mysql":{
"host":"localhost",
Expand Down
4 changes: 3 additions & 1 deletion forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function startForward(proxy_port, proxy_host) {
res.end();
});

httpServer.listen(proxy_port);
httpServer.listen(proxy_port, '127.0.0.1', function() {
process.send({http: this.address().port});
});
}

startForward(process.env.PROXY_PORT, process.env.PROXY_HOST);
20 changes: 18 additions & 2 deletions lib/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ function startForward(port, hostname, emitter) {
} else {
cons.Alert('Intercepting ALL requests through forward proxy');
}
proc.on('message', function(msg) {
if ('http' in msg) {
port = msg.http;
cons.Alert('Forward Proxy Listening On Port %d', port);
emitter.emit('http', msg.http);
}
});
emitter.once('killall', function(signal) {
cons.Done('Killing Forward Proxy Server on Port %d',port);
proc.kill(signal);
cons.Done('Killing Forward Proxy Server on Port %d', port);
try {
proc.kill(signal);
} catch(_e) {
cons.Warn('Proxy server already dead');
}
});

proc.on('exit', function(code, signal) {
emitter.emit('exit', code, signal);
emitter.emit('killall', 'SIGINT');
});
}

Expand Down
6 changes: 5 additions & 1 deletion lib/proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ function run(key, proc, emitter) {
});

emitter.on('killall', function(signal) {
child.kill(signal);
try {
child.kill(signal);
} catch(_e) {
cons.Warn('Proxy server already dead');
}
});

}
Expand Down
13 changes: 7 additions & 6 deletions lib/procfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ function procs(procdata){

var processes = {};

procdata.toString().split(/\n/).forEach(function(line) {
procdata.toString().split(/\n/).forEach(function(line, lineNo) {
if(!line || line[0] === '#') { return; }
lineNo += 1;

var tuple = /^([A-Za-z0-9_-]+):\s*(.+)$/m.exec(line);
var tuple = /^([^:]*):(.*)$/m.exec(line);

var prockey = tuple[1].trim();
var command = tuple[2].trim();
var prockey = tuple && tuple[1].trim();
var command = tuple && tuple[2].trim();

if(!prockey) {
throw new Error('Syntax Error in Procfile, Line %d: No Prockey Found');
throw new Error('Syntax Error in Procfile, Line ' + lineNo + ': No Prockey Found');
}

if(!command) {
throw new Error('Syntax Error in Procfile, Line %d: No Command Found');
throw new Error('Syntax Error in Procfile, Line ' + lineNo + ': No Command Found');
}

processes[prockey] = command;
Expand Down
32 changes: 24 additions & 8 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function f(key, j, ports, proc, reqs, portargs, localhost, emitter, ssl) {
}

var upstream_size = reqs[key];
var upstream_port = parseInt(portargs) + j * 100;
var upstream_port = portargs[j];

var proxy = prog.fork(require.resolve('../proxy'), [], {
env: {
Expand All @@ -50,27 +50,31 @@ function f(key, j, ports, proc, reqs, portargs, localhost, emitter, ssl) {
port_targets = util.format('(%d-%d)', upstream_port, upstream_port + upstream_size - 1);
}

cons.Alert('Starting Proxy Server [%s] %s -> %s', key, port, port_targets);
if (ssl.cert && ssl.key) {
cons.Alert('Starting Secure Proxy Server [%s] %s -> %s', key, ssl_port, port_targets);
}

proxy.on('message', function(msg) {
if ('http' in msg) {
port = ports[j] = msg.http;
cons.Alert('Started Proxy Server [%s] %s -> %s', key, port, port_targets);
emitter.emit('http', msg.http);
}
if ('https' in msg) {
port = ports[j] = msg.https;
cons.Alert('Started Secure Proxy Server [%s] %s -> %s', key, port, port_targets);
emitter.emit('https', msg.https);
}
});

emitter.once('killall', function(signal) {
cons.Done('Killing Proxy Server on Port %s', port);
proxy.kill(signal);
try {
proxy.kill(signal);
} catch(_e) {
cons.Warn('Proxy server already dead');
}
});

proxy.on('exit', function(code, signal) {
emitter.emit('killall', signal);
emitter.emit('exit', code, signal);
emitter.emit('killall', 'SIGINT');
});

}
Expand All @@ -80,6 +84,18 @@ function startProxies(reqs, proc, command, emitter, portargs) {
if ('proxy' in command) {

var localhost = 'localhost';
if (typeof portargs === 'string') {
portargs = portargs.split(',');
} else if (typeof portargs === 'number') {
portargs = [portargs];
}
Object.keys(reqs).forEach(function(k, j) {
if (portargs[j]) {
portargs[j] = parseInt(portargs[j]);
} else {
portargs[j] = parseInt(portargs[0]) + j * 100;
}
});

var ports = command.proxy.split(',');

Expand Down
2 changes: 1 addition & 1 deletion lib/smf/foreman-APP.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@

</service>

</service_bundle>
</service_bundle>
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
],
"scripts": {
"pretest": "jshint .",
"test": "tap test/*.test.*"
"test": "tap --coverage test/*.test.*"
},
"dependencies": {
"commander": "~2.9.0",
"http-proxy": "~1.11.1",
"commander": "^2.9.0",
"http-proxy": "^1.13.2",
"mustache": "^2.2.1",
"shell-quote": "~1.4.2"
"shell-quote": "^1.6.0"
},
"repository": {
"type": "git",
Expand All @@ -44,9 +44,8 @@
},
"preferGlobal": true,
"devDependencies": {
"chai": "~1.9.1",
"jshint": "^2.6.3",
"rimraf": "~2.2.8",
"tap": "^0.7.1"
"rimraf": "^2.5.2",
"tap": "^5.7.1"
}
}
128 changes: 128 additions & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/bin/bash
fails=0
tests=0
skips=0

trap assert_report EXIT

function fail() {
local report=$1
local output=$2
fails=$((fails+1))
tests=$((tests+1))
echo "not ok $tests # $report" >&3
echo "# $report" >&2
echo "$output" >&2
}

function ok() {
cmd=$1
tests=$((tests+1))
echo "ok $tests # $cmd" >&3
}

function skip() {
comment=$1
tests=$((tests+1))
echo "ok $tests # SKIP $comment" >&3
}

function comment() {
echo "# $*" >&3
}

function assert_exit() {
expected="$1"
case $expected in
''|*[!0-9]*)
expected=0
cmd="$*"
;;
*)
shift
cmd="$*"
;;
esac
output=`$cmd 2>&1`
result=$?
report="exit $result should be $expected: '$cmd'"
if test $result -ne $expected; then
fail "$report" "$output"
else
ok "$report"
fi
}

function assert_file() {
local fname=$1
if test $# -gt 1; then
shift
found=$(grep -F -e "$*" $fname)
result=$?
if test $result -eq 0; then
ok "needle: '$*' in $fname"
else
fail "needle: '$*' NOT in $fname"
fi
else
report="file exists: $fname"
if test -f $fname; then
ok "$report"
else
fail "$report"
fi
fi
}

function assert_not_file() {
local fname=$1
if test $# -gt 1; then
shift
found=$(grep -F -e "$*" $fname)
result=$?
if test $result -eq 0; then
fail "needle: '$*' in $fname"
else
ok "needle: '$*' NOT in $fname"
fi
else
report="file exists: $fname"
if test -f $fname; then
fail "$report"
else
ok "$report"
fi
fi
}

function bailout() {
fail "$1"
echo "Bail out! $1" >&1
echo "Bail out! $1" >&2
echo "Bail out! $1" >&3
# exit 1
assert_report
}

function assert_report() {
if test -z $reported; then
echo "1..$tests" >&3
reported=1
fi
exit $fails
}

echo 'TAP version 13'

# make fd 3 the new fd 1 and redirect original fd 1 to fd 2
# this allows us to completely isolate the TAP output from these helpers
# to their own stream
if test -z "$DEBUG"; then
exec 3>&1 2>/dev/null 1>&2
comment "suppressing stderr, set DEBUG to restore"
else
exec 3>&1 1>&2
fi

comment "using npm registry $(npm config get registry)"
comment "using $(basename $(node -p process.execPath))-$(node -p process.version)"
Loading

0 comments on commit a2b1bfb

Please sign in to comment.