u2a 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const axios = require('axios');
4
+ const { APPS_DIR } = require('./config');
5
+ const { normalizeUrl, getDomainName } = require('./url');
6
+ const Logger = require('./logger');
7
+
8
+ const logger = new Logger('favicon');
9
+
10
+ async function getFavicon(url) {
11
+ logger.info(`Getting the icon path for ${url}`);
12
+ const defaultIconPath = path.join(__dirname, 'favicon.ico');
13
+
14
+ try {
15
+ const domain = getDomainName(url);
16
+ const normalizedUrl = normalizeUrl(url);
17
+
18
+ const faviconUrl = `${normalizedUrl}/favicon.ico`;
19
+ const iconResponse = await axios.get(faviconUrl, { responseType: 'arraybuffer' });
20
+
21
+ const contentType = iconResponse.headers['content-type'];
22
+ let fileExtension = '.ico'; // Default extension
23
+ if (contentType.includes('png')) {
24
+ fileExtension = '.png';
25
+ } else if (contentType.includes('jpeg') || contentType.includes('jpg')) {
26
+ fileExtension = '.jpg';
27
+ }
28
+
29
+ const iconPathInAppDir = path.join(APPS_DIR, `${domain}${fileExtension}`);
30
+
31
+ fs.writeFileSync(iconPathInAppDir, iconResponse.data);
32
+ logger.success(`Site icon downloaded and saved for ${domain}`);
33
+ return iconPathInAppDir;
34
+
35
+ } catch (error) {
36
+ logger.warn(`Error downloading the site icon, using the default icon`);
37
+
38
+ if (fs.existsSync(defaultIconPath)) {
39
+ return defaultIconPath;
40
+ } else {
41
+ logger.error(`No icon found`);
42
+ return null;
43
+ }
44
+ }
45
+ }
46
+
47
+ module.exports = {
48
+ getFavicon
49
+ };
@@ -0,0 +1,62 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const chalk = require('chalk');
4
+ const { LOGS_DIR } = require('./config');
5
+
6
+ class Logger {
7
+ constructor(component) {
8
+ this.component = component;
9
+ this.logFile = path.join(LOGS_DIR, `${component}-${new Date().toISOString().split('T')[0]}.log`);
10
+ }
11
+
12
+ _format(status, message) {
13
+ const timestamp = new Date().toLocaleTimeString('en-US', {
14
+ hour: '2-digit',
15
+ minute: '2-digit',
16
+ second: '2-digit'
17
+ });
18
+ return `[${timestamp}] - ${status} | ${message}`;
19
+ }
20
+
21
+ _writeToFile(formattedMessage) {
22
+ fs.appendFileSync(this.logFile, formattedMessage + '\n');
23
+ }
24
+
25
+ info(message) {
26
+ const formattedMessage = this._format('INFO', message);
27
+ console.log(chalk.blue(formattedMessage));
28
+ this._writeToFile(formattedMessage);
29
+ }
30
+
31
+ success(message) {
32
+ const formattedMessage = this._format('SUCCESS', message);
33
+ console.log(chalk.green(formattedMessage));
34
+ this._writeToFile(formattedMessage);
35
+ }
36
+
37
+ warn(message) {
38
+ const formattedMessage = this._format('WARN', message);
39
+ console.log(chalk.yellow(formattedMessage));
40
+ this._writeToFile(formattedMessage);
41
+ }
42
+
43
+ error(message, error = null) {
44
+ const formattedMessage = this._format('ERROR', message);
45
+ console.log(chalk.red(formattedMessage));
46
+ this._writeToFile(formattedMessage);
47
+
48
+ if (error && error.stack) {
49
+ this._writeToFile(`Stack trace: ${error.stack}`);
50
+ }
51
+ }
52
+
53
+ debug(message) {
54
+ const formattedMessage = this._format('DEBUG', message);
55
+ if (process.env.DEBUG) {
56
+ console.log(chalk.gray(formattedMessage));
57
+ }
58
+ this._writeToFile(formattedMessage);
59
+ }
60
+ }
61
+
62
+ module.exports = Logger;
@@ -0,0 +1,20 @@
1
+ function normalizeUrl(url) {
2
+ if (!url.startsWith('http://') && !url.startsWith('https://')) {
3
+ url = 'https://' + url;
4
+ }
5
+ return url;
6
+ }
7
+
8
+ function getDomainName(url) {
9
+ try {
10
+ const { hostname } = new URL(url);
11
+ return hostname.replace(/^www\./, '');
12
+ } catch (error) {
13
+ return url;
14
+ }
15
+ }
16
+
17
+ module.exports = {
18
+ normalizeUrl,
19
+ getDomainName
20
+ };