diff --git a/icons/content.png b/icons/content.png new file mode 100644 index 0000000..d1d514c Binary files /dev/null and b/icons/content.png differ diff --git a/icons/dns.png b/icons/dns.png new file mode 100644 index 0000000..f3d8118 Binary files /dev/null and b/icons/dns.png differ diff --git a/icons/header.png b/icons/header.png new file mode 100644 index 0000000..df05cd4 Binary files /dev/null and b/icons/header.png differ diff --git a/icons/server.png b/icons/server.png new file mode 100644 index 0000000..75d134d Binary files /dev/null and b/icons/server.png differ diff --git a/icons/ssl.png b/icons/ssl.png new file mode 100644 index 0000000..03cc528 Binary files /dev/null and b/icons/ssl.png differ diff --git a/icons/tcp.png b/icons/tcp.png new file mode 100644 index 0000000..7cc3ed8 Binary files /dev/null and b/icons/tcp.png differ diff --git a/icons/total.png b/icons/total.png new file mode 100644 index 0000000..83c9477 Binary files /dev/null and b/icons/total.png differ diff --git a/index.js b/index.js index 743059c..bb279e0 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict'; +const util = require('util'); const isUrl = require('is-url'); const alfy = require('alfy'); const http = require('http'); @@ -6,9 +7,18 @@ const https = require('https'); const parse = require('url').parse; const dns = require('dns'); -if (isUrl(alfy.input)) { - let url = parse(alfy.input); - let protocol = url.protocol === 'https:' ? https : http; +function getDnslookup(host) { + return new Promise((resolve, reject) => { + dns.lookup(host, (err, address, family) => { + resolve(address); + if (err) { + reject(err); + } + }); + }); +} + +function httpOutput(url, ip) { let begin = Date.now(); let onLookup = begin; // diff begin - dns resolve let onConnect = begin; // diff dns resolve - connect @@ -16,12 +26,7 @@ if (isUrl(alfy.input)) { let onTransfer = begin; // diff connect - transfer let onTotal = begin; // diff begin - end let body = ''; - let ip = ''; - - dns.lookup(url.host, (err, address, family) => { - ip = address; - }); - const req = protocol.request(url, res => { + const req = http.request(url, res => { res.once('readable', () => { onTransfer = Date.now(); }); @@ -31,65 +36,140 @@ if (isUrl(alfy.input)) { res.on('end', () => { onTotal = Date.now(); res.body = body; - if (url.protocol === 'https:') { - alfy.output([ - { - title: 'DNS Lookup', - subtitle: onLookup - begin + 'ms' + ` IP: ${ip}` - }, - { - title: 'TCP Connection', - subtitle: onConnect - onLookup + 'ms' - }, - { - title: 'SSL Handshake', - subtitle: onSecureConnect - onConnect + 'ms' - }, - { - title: 'Server Processing', - subtitle: onTransfer - onSecureConnect + 'ms' - }, - { - title: 'Content Transfer', - subtitle: onTotal - onTransfer + 'ms' - }, - { - title: 'Total', - subtitle: onTotal - begin + 'ms' - }, - { - title: 'Headers', - subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}` + alfy.output([ + { + title: 'DNS Lookup', + subtitle: onLookup - begin + 'ms' + ` IP: ${ip}`, + icon: { + path: 'icons/dns.png' } - ]); - } else if (url.protocol === 'http:') { - alfy.output([ - { - title: 'DNS Lookup', - subtitle: onLookup - begin + 'ms' + ` IP: ${ip}` - }, - { - title: 'TCP Connection', - subtitle: onConnect - onLookup + 'ms' - }, - { - title: 'Server Processing', - subtitle: onTransfer - onConnect + 'ms' - }, - { - title: 'Content Transfer', - subtitle: onTotal - onTransfer + 'ms' - }, - { - title: 'Total', - subtitle: onTotal - begin + 'ms' - }, - { - title: 'Headers', - subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}` + }, + { + title: 'TCP Connection', + subtitle: onConnect - onLookup + 'ms', + icon: { + path: 'icons/tcp.png' } - ]); + }, + { + title: 'Server Processing', + subtitle: onTransfer - onConnect + 'ms', + icon: { + path: 'icons/server.png' + } + }, + { + title: 'Content Transfer', + subtitle: onTotal - onTransfer + 'ms', + icon: { + path: 'icons/content.png' + } + }, + { + title: 'Total', + subtitle: onTotal - begin + 'ms', + icon: { + path: 'icons/total.png' + } + }, + { + title: 'Headers', + subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}`, + icon: { + path: 'icons/header.png' + } + } + ]); + }); + }); + req.on('socket', socket => { + socket.on('lookup', () => { + onLookup = Date.now(); + }); + socket.on('connect', () => { + onConnect = Date.now(); + }); + }); + req.on('error', reject => { + alfy.output([ + { + title: 'Resolve Error', + subtitle: 'Please Check the URL' } + ]); + }); + req.end(); +} + +function httpsOutput(url, ip) { + let begin = Date.now(); + let onLookup = begin; // diff begin - dns resolve + let onConnect = begin; // diff dns resolve - connect + let onSecureConnect = begin; // diff connect - secureConnect + let onTransfer = begin; // diff connect - transfer + let onTotal = begin; // diff begin - end + let body = ''; + const req = https.request(url, res => { + res.once('readable', () => { + onTransfer = Date.now(); + }); + res.on('data', chunk => { + body += chunk; + }); + res.on('end', () => { + onTotal = Date.now(); + res.body = body; + alfy.output([ + { + title: 'DNS Lookup', + subtitle: onLookup - begin + 'ms' + ` IP: ${ip}`, + icon: { + path: 'icons/dns.png' + } + }, + { + title: 'TCP Connection', + subtitle: onConnect - onLookup + 'ms', + icon: { + path: 'icons/tcp.png' + } + }, + { + title: 'SSL Handshake', + subtitle: onSecureConnect - onConnect + 'ms', + icon: { + path: 'icons/ssl.png' + } + }, + { + title: 'Server Processing', + subtitle: onTransfer - onSecureConnect + 'ms', + icon: { + path: 'icons/server.png' + } + }, + { + title: 'Content Transfer', + subtitle: onTotal - onTransfer + 'ms', + icon: { + path: 'icons/content.png' + } + }, + { + title: 'Total', + subtitle: onTotal - begin + 'ms', + icon: { + path: 'icons/total.png' + } + }, + { + title: 'Headers', + subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}`, + icon: { + path: 'icons/header.png' + } + } + ]); }); }); req.on('socket', socket => { @@ -113,6 +193,37 @@ if (isUrl(alfy.input)) { ]); }); req.end(); +} + +if (isUrl(alfy.input)) { + let url = parse(alfy.input); + if (url.protocol === 'http:') { + getDnslookup(url.host) + .then(ip => { + httpOutput(url, ip); + }) + .catch(() => { + alfy.output([ + { + title: 'Parse URL Formate Error', + subtitle: 'Please check the URL formate' + } + ]); + }); + } else if (url.protocol === 'https:') { + getDnslookup(url.host) + .then(ip => { + httpsOutput(url, ip); + }) + .catch(() => { + alfy.output([ + { + title: 'Parse URL Formate Error', + subtitle: 'Please check the URL formate' + } + ]); + }); + } } else { alfy.output([ {