change main export signature to localtunnel(port, opt, fn)

Makes for a simpler hello world app
This commit is contained in:
Roman Shtylman
2014-02-14 00:32:43 -05:00
parent 7217a08a05
commit 2f6f9459ad
5 changed files with 67 additions and 52 deletions

View File

@@ -2,5 +2,6 @@
* default to localltunnel.me for host * default to localltunnel.me for host
* remove exported `connect` method (just export one function that does the same thing) * 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 # 0.2.2 / 2014-01-09

View File

@@ -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) 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 ```javascript
var localtunnel = require('localtunnel'); var localtunnel = require('localtunnel');
var client = localtunnel({ localtunnel(port, function(err, tunnel) {
// the localtunnel server to proxy through if (err) ...
// default is localtunnel.me
//host: 'http://localtunnel.me',
// your local application port // the assigned public url for your tunnel
port: 12345 // i.e. https://abcdefgjhij.localtunnel.me
}); tunnel.url;
// 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!
}); });
``` ```
### 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 ## ## other clients ##
Clients in other languages Clients in other languages
@@ -59,7 +71,7 @@ Clients in other languages
## server ## ## 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 ## ## License ##
MIT MIT

View File

@@ -25,15 +25,17 @@ var opt = {
subdomain: argv.subdomain, 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 console.log('your url is: %s', tunnel.url);
client.on('url', function(url) {
console.log('your url is: %s', url);
});
client.on('error', function(err) { tunnel.on('error', function(err) {
console.error(err); console.error(err);
});
}); });
// vim: ft=javascript // vim: ft=javascript

View File

@@ -222,15 +222,17 @@ Tunnel.prototype._establish = function(info) {
} }
}; };
Tunnel.prototype.open = function() { Tunnel.prototype.open = function(cb) {
var self = this; var self = this;
self._init(function(err, info) { self._init(function(err, info) {
if (err) { if (err) {
return self.emit('error', err); return cb(err);
} }
self.url = info.url;
self._establish(info); self._establish(info);
cb();
}); });
}; };
@@ -242,8 +244,21 @@ Tunnel.prototype.close = function() {
self.emit('close'); 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); var client = Tunnel(opt);
client.open(); client.open(function(err) {
return client; if (err) {
return fn(err);
}
fn(null, client);
});
}; };

View File

@@ -21,19 +21,12 @@ test('setup local http server', function(done) {
}); });
test('setup localtunnel client', function(done) { test('setup localtunnel client', function(done) {
var client = localtunnel({ localtunnel(test._fake_port, function(err, tunnel) {
port: test._fake_port assert.ifError(err);
}); assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(tunnel.url));
test._fake_url = tunnel.url;
client.on('url', function(url) {
assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(url));
test._fake_url = url;
done(); done();
}); });
client.on('error', function(err) {
done(err);
});
}); });
test('query localtunnel server w/ ident', function(done) { 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) { test('request specific domain', function(done) {
var client = localtunnel({ localtunnel(test._fake_port, { subdomain: 'abcd' }, function(err, tunnel) {
port: test._fake_port, assert.ifError(err);
subdomain: 'abcd' assert.ok(new RegExp('^https:\/\/abcd.localtunnel.me' + '$').test(tunnel.url));
}); tunnel.close();
client.on('url', function(url) {
assert.ok(new RegExp('^https:\/\/abcd.localtunnel.me' + '$').test(url));
client.close();
done(); done();
}); });
client.on('error', function(err) {
console.error(err);
});
}); });