diff --git a/History.md b/History.md index a9e676f..7f3e39f 100644 --- a/History.md +++ b/History.md @@ -2,5 +2,6 @@ * default to localltunnel.me for host * remove exported `connect` method (just export one function that does the same thing) +* change localtunnel signature to (port, opt, fn) # 0.2.2 / 2014-01-09 diff --git a/README.md b/README.md index fb05869..b5fe7cc 100644 --- a/README.md +++ b/README.md @@ -28,29 +28,41 @@ You can restart your local server all you want, ```lt``` is smart enough to dete The localtunnel client is also usable through an API (for test integration, automation, etc) +### localtunnel(port [,opts], fn) + +Creates a new localtunnel to the specified local `port`. `fn` will be called once you have been assigned a public localtunnel url. `opts` can be used to request a specific `subdomain`. + ```javascript var localtunnel = require('localtunnel'); -var client = localtunnel({ - // the localtunnel server to proxy through - // default is localtunnel.me - //host: 'http://localtunnel.me', +localtunnel(port, function(err, tunnel) { + if (err) ... - // your local application port - port: 12345 -}); - -// when your are assigned a url -client.on('url', function(url) { - // you can now make http requests to the url - // they will be proxied to your local server on port [12345] -}); - -client.on('error', function(err) { - // uh oh! + // the assigned public url for your tunnel + // i.e. https://abcdefgjhij.localtunnel.me + tunnel.url; }); ``` +### opts + +* `subdomain` A *string* value requesting a specific subdomain on the proxy server. **Note** You may not actually receive this name depending on availablily. + +### Tunnel + +The `tunnel` instance returned to your callback emits the following events + +|event|args|description| +|:|:|:| +|error|err|fires when an error happens on the tunnel| +|close||fires when the tunnel has closed| + +The `tunnel instance has the following methods + +|method|args|description| +|:|:|:| +|close||close the tunnel| + ## other clients ## Clients in other languages @@ -59,7 +71,7 @@ Clients in other languages ## server ## -See shtylman/localtunnel-server for details on the server that powers localtunnel. +See defunctzombie/localtunnel-server for details on the server that powers localtunnel. ## License ## MIT diff --git a/bin/client b/bin/client index 4b4a9bd..2a5888a 100755 --- a/bin/client +++ b/bin/client @@ -25,15 +25,17 @@ var opt = { subdomain: argv.subdomain, } -var client = lt_client.connect(opt); +lt_client(opt.port, opt, function(err, tunnel) { + if (err) { + console.error(err); + return process.exit(1); + } -// only emitted when the url changes -client.on('url', function(url) { - console.log('your url is: %s', url); -}); + console.log('your url is: %s', tunnel.url); -client.on('error', function(err) { - console.error(err); + tunnel.on('error', function(err) { + console.error(err); + }); }); // vim: ft=javascript diff --git a/client.js b/client.js index 991dba3..d0a84cb 100644 --- a/client.js +++ b/client.js @@ -222,15 +222,17 @@ Tunnel.prototype._establish = function(info) { } }; -Tunnel.prototype.open = function() { +Tunnel.prototype.open = function(cb) { var self = this; self._init(function(err, info) { if (err) { - return self.emit('error', err); + return cb(err); } + self.url = info.url; self._establish(info); + cb(); }); }; @@ -242,8 +244,21 @@ Tunnel.prototype.close = function() { self.emit('close'); }; -module.exports = function localtunnel(opt) { +module.exports = function localtunnel(port, opt, fn) { + if (typeof opt === 'function') { + fn = opt; + opt = {}; + } + + opt = opt || {}; + opt.port = port; + var client = Tunnel(opt); - client.open(); - return client; + client.open(function(err) { + if (err) { + return fn(err); + } + + fn(null, client); + }); }; diff --git a/test/index.js b/test/index.js index 87bec19..0a39a0e 100644 --- a/test/index.js +++ b/test/index.js @@ -21,19 +21,12 @@ test('setup local http server', function(done) { }); test('setup localtunnel client', function(done) { - var client = localtunnel({ - port: test._fake_port - }); - - client.on('url', function(url) { - assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(url)); - test._fake_url = url; + localtunnel(test._fake_port, function(err, tunnel) { + assert.ifError(err); + assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(tunnel.url)); + test._fake_url = tunnel.url; done(); }); - - client.on('error', function(err) { - done(err); - }); }); test('query localtunnel server w/ ident', function(done) { @@ -67,18 +60,10 @@ test('query localtunnel server w/ ident', function(done) { }); test('request specific domain', function(done) { - var client = localtunnel({ - port: test._fake_port, - subdomain: 'abcd' - }); - - client.on('url', function(url) { - assert.ok(new RegExp('^https:\/\/abcd.localtunnel.me' + '$').test(url)); - client.close(); + localtunnel(test._fake_port, { subdomain: 'abcd' }, function(err, tunnel) { + assert.ifError(err); + assert.ok(new RegExp('^https:\/\/abcd.localtunnel.me' + '$').test(tunnel.url)); + tunnel.close(); done(); }); - - client.on('error', function(err) { - console.error(err); - }); });