internal changes only

- remade config module
- renamed loc to i18n because that's what all developers do
- moved code to src to make repo look cleaner
- fixed some i18n strings
This commit is contained in:
wukko
2022-07-17 17:08:49 +06:00
parent 227dc8436c
commit 67223b3acd
62 changed files with 50 additions and 55 deletions

View File

@@ -0,0 +1,51 @@
import { createStream } from "../stream/manage.js";
export function apiJSON(type, obj) {
try {
switch (type) {
case 0:
return { status: 400, body: { status: "error", text: obj.t } };
case 1:
return { status: 200, body: { status: "redirect", url: obj.u } };
case 2:
return { status: 200, body: { status: "stream", url: createStream(obj) } };
case 3:
return { status: 200, body: { status: "success", text: obj.t } };
case 4:
return { status: 429, body: { status: "rate-limit", text: obj.t } };
default:
return { status: 400, body: { status: "error", text: "Bad Request" } };
}
} catch (e) {
return { status: 500, body: { status: "error", text: "Internal Server Error" } };
}
}
export function msToTime(d) {
let milliseconds = parseInt((d % 1000) / 100),
seconds = parseInt((d / 1000) % 60),
minutes = parseInt((d / (1000 * 60)) % 60),
hours = parseInt((d / (1000 * 60 * 60)) % 24),
r;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
r = hours + ":" + minutes + ":" + seconds;
milliseconds ? r += "." + milliseconds : r += "";
return r;
}
export function cleanURL(url, host) {
url = url.replace('}', '').replace('{', '').replace(')', '').replace('(', '').replace(' ', '');
if (url.includes('youtube.com/shorts/')) {
url = url.split('?')[0].replace('shorts/', 'watch?v=');
}
if (host == "youtube") {
url = url.split('&')[0];
} else {
url = url.split('?')[0];
if (url.substring(url.length - 1) == "/") {
url = url.substring(0, url.length - 1);
}
}
return url
}

View File

@@ -0,0 +1,49 @@
export function t(color, tt) {
return color + tt + "\x1b[0m"
}
export function Reset(tt) {
return "\x1b[0m" + tt
}
export function Bright(tt) {
return t("\x1b[1m", tt)
}
export function Dim(tt) {
return t("\x1b[2m", tt)
}
export function Underscore(tt) {
return t("\x1b[4m", tt)
}
export function Blink(tt) {
return t("\x1b[5m", tt)
}
export function Reverse(tt) {
return t("\x1b[7m", tt)
}
export function Hidden(tt) {
return t("\x1b[8m", tt)
}
export function Black(tt) {
return t("\x1b[30m", tt)
}
export function Red(tt) {
return t("\x1b[31m", tt)
}
export function Green(tt) {
return t("\x1b[32m", tt)
}
export function Yellow(tt) {
return t("\x1b[33m", tt)
}
export function Blue(tt) {
return t("\x1b[34m", tt)
}
export function Magenta(tt) {
return t("\x1b[35m", tt)
}
export function Cyan(tt) {
return t("\x1b[36m", tt)
}
export function White(tt) {
return t("\x1b[37m", tt)
}

11
src/modules/sub/crypto.js Normal file
View File

@@ -0,0 +1,11 @@
import { createHmac, createHash, randomUUID } from "crypto";
export function encrypt(str, salt) {
return createHmac("sha256", salt).update(str).digest("hex");
}
export function md5(string) {
return createHash('md5').update(string).digest('hex');
}
export function UUID() {
return randomUUID();
}

View File

@@ -0,0 +1,10 @@
import { execSync } from "child_process";
export function shortCommit() {
return execSync('git rev-parse --short HEAD').toString().trim()
}
export function getCommitInfo() {
let d = execSync(`git show -s --format='%s;;;%B'`).toString().trim().replace(/[\r\n]/gm, '\n').split(';;;')
d[1] = d[1].replace(d[0], '').trim().toString().replace(/[\r\n]/gm, '<br>')
return d
}

11
src/modules/sub/errors.js Normal file
View File

@@ -0,0 +1,11 @@
import loc from "./i18n.js";
export function internalError(res) {
res.status(501).json({ status: "error", text: "Internal Server Error" });
}
export function errorUnsupported(lang) {
return loc(lang, 'apiError', 'notSupported') + loc(lang, 'apiError', 'letMeKnow');
}
export function genericError(lang, host) {
return loc(lang, 'apiError', 'brokenLink', host) + loc(lang, 'apiError', 'letMeKnow');
}

22
src/modules/sub/i18n.js Normal file
View File

@@ -0,0 +1,22 @@
import { supportedLanguages, appName, repo } from "../config.js";
import loadJson from "./load-json.js";
export default function(lang, cat, string, replacement) {
if (!supportedLanguages.includes(lang)) {
lang = 'en'
}
try {
let str = loadJson(`./src/i18n/${lang}/${cat}.json`);
if (str && str[string]) {
let s = str[string].replace(/\n/g, '<br/>').replace(/{appName}/g, appName).replace(/{repo}/g, repo)
if (replacement) {
s = s.replace(/{s}/g, replacement)
}
return s + ' '
} else {
return string
}
} catch (e) {
return string
}
}

View File

@@ -0,0 +1,9 @@
import * as fs from "fs";
export default function(path) {
try {
return JSON.parse(fs.readFileSync(path, 'utf-8'))
} catch(e) {
return false
}
}