Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const http = require('http');
const https = require('https');
const { URL } = require('url');

module.exports = {
command: 'appify <url>',
getTitle, // exported for testing
register: (command, modules) => {
command
.option('-t,--title [title]')
Expand All @@ -27,22 +30,36 @@ module.exports = {


function getTitle(url) {
const TITLE_REGEX = /<title>(.*)<\/title>/im;

return new Promise((resolve, reject)=>{
https.get(url, (res) => {
let body = '', title = '';
res.on('data', (chunk) => {
body += chunk;
if(!title && TITLE_REGEX.test(body)) {
title = body.match(TITLE_REGEX)[1];
const TITLE_REGEX = /<title>(.*)<\/title>/im;

let protocol;
try {
const parsed = new URL(url);
if (parsed.protocol === 'https:') {
protocol = https;
} else if (parsed.protocol === 'http:') {
protocol = http;
} else {
return Promise.reject(new Error(`Unsupported protocol: ${parsed.protocol}. Only http: and https: are supported.`));
}
});
res.on('end', () => {
resolve(title);
});
}).on('error', (err) => {
reject('');
} catch (e) {
return Promise.reject(new Error('Invalid URL'));
}

return new Promise((resolve, reject) => {
protocol.get(url, (res) => {
let body = '', title = '';
res.on('data', (chunk) => {
body += chunk;
if (!title && TITLE_REGEX.test(body)) {
title = body.match(TITLE_REGEX)[1];
}
});
res.on('end', () => {
resolve(title);
});
}).on('error', (err) => {
reject(err);
});
});
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Convert any SPA to a lightweight desktop app - Appify plugin for Neutralinojs CLI",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"test:getTitle": "node scripts/test-getTitle.js"
},
"repository": {
"type": "git",
Expand Down