-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.js
178 lines (123 loc) · 3.93 KB
/
ping.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const URL = require('url').URL;
const https = require('https');
const http = require('http');
const querystring = require('querystring');
const log = require('inspc');
const th = msg => new Error(`request: ${String(msg)}`);
function request (url, opt = {}) {
let {
method = 'GET',
timeout = 30000,
get = {},
} = opt;
if ( typeof method !== 'string' ) {
throw th(`method is not a string`);
}
method = method.toLowerCase();
return new Promise((resolve, reject) => {
const uri = new URL(url);
const lib = (uri.protocol === 'https:') ? https : http;
const query = querystring.stringify(get)
const rq = {
hostname : uri.hostname,
port : uri.port || ( (uri.protocol === 'https:') ? '443' : '80'),
path : uri.pathname + uri.search + (query ? (uri.search.includes('?') ? '&' : '?') + query : ''),
method,
headers : {
'Content-Type': 'application/json; charset=utf-8',
Accept: `text/html; charset=utf-8`,
},
};
var req = lib.request(rq, res => {
const isHtml = (res.headers['content-type'] || '').toLowerCase().indexOf('text/html') === 0;
if (isHtml) {
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => {
body += chunk
});
res.on('end', () => {
// log.dump({
// status: res.statusCode,
// headers: res.headers,
// isHtml,
// uri,
// body,
// rq,
// }, 5)
resolve({
status: res.statusCode,
headers: res.headers,
body,
})
});
}
else {
// log.dump({
// status: res.statusCode,
// headers: res.headers,
// isHtml,
// uri,
// rq,
// }, 5)
resolve({
status: res.statusCode,
headers: res.headers,
})
}
});
req.on('error', e => { // wothout this it will crash the main node server
log.dump({
request: rq,
ping_error: e,
})
});
req.on('socket', function (socket) { // uncomment this to have timeout
socket.setTimeout(timeout);
socket.on('timeout', () => { // https://stackoverflow.com/a/9910413/5560682
req.abort();
reject({
type: 'timeout',
})
});
});
req.on('error', e => reject({
type: 'error',
error: e,
}));
if ( typeof opt.json !== 'undefined' ) {
if (opt.method === 'get') {
throw th(`opt.json is given but method is still get`);
}
log.dump({
sendjson: opt.json,
}, 5)
req.write(JSON.stringify(opt.json));
}
req.end();
});
}
const url = process.argv[2];
if ( typeof url !== 'string' ) {
throw th(`url param is not a string`);
}
if ( ! /^https?:\/\//.test(url) ) {
throw th(`url don't match https?:\/\/`);
}
;(async function () {
try {
const data = await request(url, {
timeout: 3000,
});
// log.dump({
// data,
// }, 3)
process.exit(data.status);
}
catch (e) {
log.dump({
error: e
})
process.exit(504);
}
}());