Skip to content

Commit

Permalink
feat(tunnel): Switch to ngrok - re: #192
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Feb 5, 2015
1 parent 29c9bd3 commit 7359435
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 42 deletions.
10 changes: 6 additions & 4 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,23 @@ module.exports = {
* @param {Function} done
*/
startTunnel: function (bs, done) {

if (bs.options.get("tunnel") && bs.options.get("online")) {
var localTunnel = require("./tunnel");
localTunnel(bs, function (err, tunnel) {

var ngrok = require("./tunnel")(bs, function (err, tunnel) {

if (err) {
return done(err);
} else {
return done(null, {
optionsIn: [
{
path: ["urls", "tunnel"],
value: tunnel.url
value: tunnel
}
],
instance: {
tunnel: tunnel
tunnel: ngrok
}
});
}
Expand Down
16 changes: 16 additions & 0 deletions lib/cli/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ opts.callbacks = {

return Immutable.Map(obj);
},
/**
* Allow tunnel arg to be `true`, or an object.
* https://www.npmjs.com/package/ngrok
* @param {Boolean|Object} value
* @returns {Map}
*/
tunnel: function (value) {
if (!value) {
return false;
}
var opts = Immutable.Map({});
if (value === true) {
return opts;
}
return opts.merge(value);
},
/**
* @param value
* @param argv
Expand Down
16 changes: 6 additions & 10 deletions lib/tunnel.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
"use strict";

var _ = require("lodash");
var localTunnel = require("localtunnel");
var utils = require("util");
var ngrok = require("ngrok");
var utils = require("util");

/**
* @param {BrowserSync} bs
* @param {Function} cb
*/
module.exports = function (bs, cb) {

var opts = {};
var options = bs.options;
var port = options.get("port");

if (_.isString(options.get("tunnel"))) {
opts.subdomain = options.get("tunnel");
}
var opts = options.get("tunnel");

bs.debug("Requesting a tunnel connection on port: {magenta:%s}", port);
bs.debug("Requesting a tunnel connection with options: {magenta:%s}", utils.inspect(opts));

localTunnel(port, opts, function (err, tunnel) {
ngrok.connect(opts.set("port", port).toJS(), function (err, url) {
if (err) {
return cb(err);
}
return cb(null, tunnel);
return cb(null, url);
});
return ngrok;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
"foxy": "^9.0.0",
"glob-watcher": "^0.0.7",
"immutable": "^3.4.1",
"localtunnel": "^1.3.0",
"lodash": "^3.0.1",
"meow": "^3.0.0",
"ngrok": "^0.1.98",
"opn": "^1.0.0",
"portscanner": "^1.0.0",
"resp-modifier": "^2.0.1",
Expand Down
42 changes: 42 additions & 0 deletions test/specs/e2e/cli/e2e.cli.tunnel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

var path = require("path");
var assert = require("chai").assert;
var browserSync = require(path.resolve("./"));

var pkg = require(path.resolve("package.json"));
var cli = require(path.resolve(pkg.bin));

describe("E2E CLI server + tunnel test", function () {

var bs;

before(function (done) {

browserSync.reset();

cli({
cli: {
input: ["start"],
flags: {
server: "test/fixtures",
open: false,
online: true,
logLevel: "silent",
tunnel: true
}
},
cb: function (err, out) {
bs = out;
done();
}
});
});
after(function () {
bs.cleanup();
bs.tunnel.disconnect();
});
it("should call init on the tunnel", function () {
assert.include(bs.getOptionIn(["urls", "tunnel"]), "ngrok.com");
});
});
134 changes: 107 additions & 27 deletions test/specs/e2e/server/e2e.server.tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,113 @@
var browserSync = require("../../../../index");

var assert = require("chai").assert;
var sinon = require("sinon");
var ngrok = require("ngrok");

describe.skip("Tunnel e2e tests", function () {

describe("E2E server test with tunnel", function () {

var instance;

before(function (done) {
browserSync.reset();
var config = {
server: {
baseDir: "test/fixtures"
},
logLevel: "silent",
open: false,
tunnel: true,
online: true
};
instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("should call init on the tunnel", function () {
assert.include(instance.options.getIn(["urls", "tunnel"]), "localtunnel.me");
});
describe("Tunnel e2e tests", function () {

var bs;

before(function (done) {

browserSync.reset();

var config = {
server: {
baseDir: "test/fixtures"
},
logLevel: "silent",
open: false,
tunnel: true,
online: true
};
bs = browserSync(config, done).instance;
});

after(function () {
bs.cleanup();
bs.tunnel.disconnect();
});

it("should call init on the tunnel", function () {
assert.include(bs.options.getIn(["urls", "tunnel"]), "ngrok");
});
});

describe("Tunnel e2e tests passing config", function () {

var bs, stub;

before(function (done) {

browserSync.reset();

stub = sinon
.stub(ngrok, "connect")
.yields(null, "http://shane.com");

var config = {
server: {
baseDir: "test/fixtures"
},
logLevel: "silent",
open: false,
online: true,
tunnel: {
authtoken: "shakyshane",
somevar: 9000
}
};
bs = browserSync(config, done).instance;
});

after(function () {
bs.cleanup();
ngrok.connect.restore();
});

it("should call init on the tunnel", function () {
assert.equal(bs.options.getIn(["tunnel", "authtoken"]), "shakyshane");
assert.equal(bs.options.getIn(["tunnel", "somevar"]), 9000);
assert.equal(bs.options.getIn(["urls", "tunnel"]), "http://shane.com");
});
});

describe("Tunnel e2e tests - handling errors", function () {

var bs, stub;

before(function (done) {

browserSync.reset();

stub = sinon
.stub(ngrok, "connect")
.yields(new Error("Cannot connect"));

var config = {
server: {
baseDir: "test/fixtures"
},
logLevel: "silent",
open: false,
online: true,
tunnel: {
authtoken: "shakyshane",
somevar: 9000
}
};

bs = browserSync(config, done).instance;
});

after(function () {
bs.cleanup();
ngrok.connect.restore();
});

it("should call init on the tunnel", function () {
assert.isUndefined(bs.getOptionIn(["urls", "tunnel"]));
assert.isUndefined(bs.tunnel);
});
});

0 comments on commit 7359435

Please sign in to comment.