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
* 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

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)
### 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

View File

@@ -25,15 +25,17 @@ var opt = {
subdomain: argv.subdomain,
}
var client = lt_client.connect(opt);
// only emitted when the url changes
client.on('url', function(url) {
console.log('your url is: %s', url);
});
client.on('error', function(err) {
lt_client(opt.port, opt, function(err, tunnel) {
if (err) {
console.error(err);
return process.exit(1);
}
console.log('your url is: %s', tunnel.url);
tunnel.on('error', function(err) {
console.error(err);
});
});
// 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;
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);
});
};

View File

@@ -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);
});
});