forked from strongloop/strong-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (42 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: strong-tunnel
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
var tunnel = require('./lib/tunnel');
var libUrl = require('url');
module.exports = fromUrl;
function fromUrl(url, opts, callback) {
if (callback === undefined && typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = opts || {};
var obj = JSON.parse(JSON.stringify(url));
if (typeof obj === 'string') {
obj = parseUrl(obj);
}
obj.protocol = ':'
// we've replaced the port, so delete host so URL is recomposed using
// $hostname:$port for $host
delete obj.host;
tunnel(obj, opts, function(err, urlObj) {
if (err) {
return callback(err);
}
formatUrl(urlObj, callback);
});
}
function libUrlWrapper(fn, url, callback) {
try {
var convertedUrl = fn(url);
return callback ? callback(null, convertedUrl) : convertedUrl;
} catch (error) {
return callback ? callback(error) : error;
}
}
function formatUrl(url, callback) {
return libUrlWrapper(libUrl.format, url, callback);
}
function parseUrl(url, callback) {
return libUrlWrapper(libUrl.parse, url, callback);
}