-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (103 loc) · 3.18 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var https = require('https');
var url = require('url');
// Note that bad hash, e.g. https://github.com/user/repo#readme1, cann't be detected!
function checkGitHubURLValidity(githubUrl, callback, needValidation) {
if (needValidation) {
var parsedURL = url.parse(githubUrl);
var options = {
hostname: parsedURL.hostname,
port: 443,
path: parsedURL.pathname,
method: 'HEAD'
};
var req = https.request(options, function(res) {
if (res.statusCode === 404) {
console.log('\033[31mWarning: \033[39m' + '\033[33m' + githubUrl + '\033[39m' + ' is not reachable!');
} else {
// 200 or other status are not interested
}
callback();
});
req.end();
} else {
callback();
}
}
function shorten(rawURL, callback, check) {
var needValidation = (check === undefined) ? false : check;
var encodedURL = 'url=' + encodeURIComponent(rawURL);
var options = {
hostname: 'git.io',
port: 443,
path: '/create',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': encodedURL.length
}
};
var req = https.request(options, function(res) {
if (res.statusCode === 200) {
checkGitHubURLValidity(rawURL, function() {
var result = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function() {
result = 'https://git.io/'+result;
if (typeof callback === 'undefined') {
console.log(result);
} else {
callback(result);
}
});
}, needValidation);
} else {
console.log('Must be a GitHub.com URL.');
}
});
req.write(encodedURL);
req.end();
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
}
function unshorten(gitDotIoURL, callback) {
var parsedURL = url.parse(gitDotIoURL);
var options = {
hostname: parsedURL.hostname,
port: 443,
path: parsedURL.pathname,
method: 'HEAD'
};
var result;
var req = https.request(options, function(res) {
// Success 302
if (res.statusCode === 302) {
result = res.headers['location'];
} else {
// Failure 404, 200 or other status
console.error('\n' +
'Notice: Woops! We can\'t seem to unshorten that URL, this could be for a few reasons:\n\n' +
'1. it may not be a short URL in the first place;\n' +
'2. it may not be a real URL or could no longer be active;\n' +
'3. it may not be a short URL compatible with git.io!');
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.on('close', function() {
if (typeof callback === 'undefined') {
result === undefined ? undefined : console.log(result);
} else {
callback(result);
}
});
req.end();
}
module.exports = {
shorten: shorten,
unshorten: unshorten
}