Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 99a22de

Browse files
committed
2.0-alpha
1 parent 912c055 commit 99a22de

8 files changed

Lines changed: 153 additions & 113 deletions

File tree

.replit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
hidden = [".config", "package-lock.json"]
3-
run = "node index"
3+
run = "ts-node index"
44

55
[[hints]]
66
regex = "Error \\[ERR_REQUIRE_ESM\\]"
@@ -62,7 +62,7 @@ support = true
6262
cwd = "."
6363
environment = []
6464
pauseForSourceMap = false
65-
program = "./index.js"
65+
program = "./index.ts"
6666
request = "launch"
6767
sourceMaps = true
6868
stopOnEntry = false

cache.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

cache.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const conf = require("./config");
2+
const logger = (require("log4js")).getLogger("Cache");
3+
4+
logger.level = "debug";
5+
6+
7+
class Cache {
8+
protected caches: object;
9+
public expiration: number;
10+
constructor(expiration: number) {
11+
this.caches = {};
12+
this.expiration = expiration;
13+
this.uptime();
14+
}
15+
16+
get(key: string): undefined | any {
17+
const value = this.caches[key];
18+
if (this.exist(key)) {
19+
return JSON.parse(value.value);
20+
}
21+
}
22+
23+
set(key: string, value: any): void {
24+
this.caches[key] = {
25+
value: JSON.stringify(value),
26+
expiration: (new Date().getTime() / 1000) + this.expiration,
27+
}
28+
}
29+
30+
exist(key: string): boolean {
31+
const value = this.caches[key];
32+
return (!!value) && (value.expiration > (new Date().getTime() / 1000));
33+
}
34+
35+
remove(key: string): boolean {
36+
return delete this.caches[key];
37+
}
38+
39+
uptime(): void {
40+
const _this = this;
41+
setInterval(function (){
42+
let n: number = 0;
43+
for (const key in _this.caches) {
44+
if (_this.caches[key].expiration < (new Date().getTime() / 1000)) {
45+
_this.remove(key); n++;
46+
}
47+
if (n > 0) logger.debug(`Clean ${n} Caches`);
48+
}
49+
}, 1800);
50+
}
51+
52+
wrap(func: (params: any[]) => Promise<any>): (...params: any[]) => Promise<any> {
53+
/**
54+
* Async Function Cache.
55+
*/
56+
57+
const _this = this;
58+
return async function (...params : any[]) {
59+
const key: string = func.name + JSON.stringify(params);
60+
if (_this.exist(key)) {
61+
logger.debug(`Hit Cache <${func.name}>`)
62+
return _this.get(key);
63+
} else {
64+
logger.info(`Cache Response ${func.name}`); // @ts-ignore
65+
const response: any = await func(...params);
66+
_this.set(key, response);
67+
return response;
68+
}
69+
}
70+
}
71+
}
72+
73+
export const cache = new Cache(conf.expiration);

config.js renamed to config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
token: process.env.CODE_STATISTIC, // GitHub Access Token (Minimum permissions). Increase QPS of GitHub APIs.
33
expiration: 3600, // expiration second
44
requires: ["*"], // CODE STATISTIC can only be parsed for allowed users. ( * indicates that all users are allowed )
File renamed without changes.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@
2525
"ejs-locals": "^1.0.2",
2626
"express": "^4.18.2",
2727
"log4js": "^6.7.1"
28+
},
29+
"devDependencies": {
30+
"@types/node": "^18.14.6"
2831
}
2932
}

stats.js

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
const cache = new (require('./cache').ApiCache)();
1+
const cache = require('./cache').cache;
22

3-
const store_units = ["b", "KiB", "MiB", "GiB", "TiB", "PiB"];
4-
const dec_units = ["", "k", "m"];
5-
6-
const lang_colors = { /** thanks, @anuraghazra's github-readme-stats **/
3+
const colors = { /** thanks, @anuraghazra's github-readme-stats **/
74
"1C Enterprise": "#814CCC",
85
"2-Dimensional Array": "#38761D",
96
"4D": "#004289",
@@ -552,45 +549,6 @@ const lang_colors = { /** thanks, @anuraghazra's github-readme-stats **/
552549
"xBase": "#403a40"
553550
}
554551

555-
function sort(arr) {
556-
const len = arr.length - 1;
557-
for (let i = 0; i <= len; i++) {for (let j = 0; j < len - i; j++) {if (arr[j] > arr[j + 1]) {[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]}}}
558-
return arr
559-
}
560-
561-
function storeConvert(size, idx=0) {
562-
if (size <= 0) {
563-
return "0";
564-
}
565-
while (idx < (store_units.length - 1) && size > 1024) {
566-
size /= 1024;
567-
idx ++;
568-
}
569-
return `${size.toFixed(1)} ${store_units[idx]}`;
570-
}
571-
572-
function decConvert(n, allowed_pre=true) {
573-
let idx = 0;
574-
let condition = allowed_pre ? 100 : 1000;
575-
while (idx < (dec_units.length - 1) && n > condition) { n /= 1000 ; idx ++ }
576-
return idx === 0 ? n : n.toFixed(1) + dec_units[idx];
577-
}
578-
579-
function sum(arr) {
580-
switch (arr.length) {
581-
case 0 :
582-
return 0;
583-
case 1:
584-
return arr[0];
585-
default:
586-
return arr.reduce((a, b) => a + b);
587-
}
588-
}
589-
590-
async function getLanguage(user, repo) {
591-
return await cache.requestWithCache(`/repos/${user}/${repo}/languages`);
592-
}
593-
594552
async function langStatistics(queue) {
595553
const res = {};
596554
for (const idx in queue) {
@@ -622,7 +580,7 @@ function langHandler(langs) {
622580
cursor += ratio;
623581
return {
624582
name: lang,
625-
color: lang_colors[lang],
583+
color: colors[lang],
626584
cursor: cursor - ratio,
627585
ratio: ratio,
628586
text: `${lang} ${(ratio * 100).toFixed(0)}% (${decConvert(key, false)})`,
@@ -662,7 +620,7 @@ async function getRepository(username, repo, dark=false) {
662620
stars: decConvert(res['stargazers_count']),
663621
watchers: decConvert(res['watchers_count']),
664622
license: license ? license['spdx_id'] : "Empty",
665-
color: lang_colors[res['language']],
623+
color: colors[res['language']],
666624
langs: langHandler(await getLanguage(username, repo)),
667625
};
668626
}

utils.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const axios = require("axios");
2+
const conf = require("./config");
3+
axios.defaults.baseURL = "https://api.github.com";
4+
5+
export function sort(arr: number[]): number[] {
6+
const len = arr.length - 1;
7+
for (let i = 0; i <= len; i++) {for (let j = 0; j < len - i; j++) {if (arr[j] > arr[j + 1]) {[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]}}}
8+
return arr
9+
}
10+
11+
const store_units = ["b", "KiB", "MiB", "GiB", "TiB", "PiB"];
12+
export function storeConvert(size: number, idx: number = 0): string {
13+
if (size <= 0) {
14+
return "0";
15+
}
16+
while (idx < (store_units.length - 1) && size > 1024) {
17+
size /= 1024;
18+
idx ++;
19+
}
20+
return `${size.toFixed(1)} ${store_units[idx]}`;
21+
}
22+
23+
const dec_units = ["", "k", "m"];
24+
export function decConvert(n: number, allowed_pre: boolean = true): string {
25+
let idx = 0;
26+
let condition = allowed_pre ? 100 : 1000;
27+
while (idx < (dec_units.length - 1) && n > condition) { n /= 1000 ; idx ++ }
28+
return idx === 0 ? n.toString() : n.toFixed(1) + dec_units[idx];
29+
}
30+
31+
export function sum(arr: number[]): number {
32+
switch (arr.length) {
33+
case 0 : return 0;
34+
case 1: return arr[0];
35+
default: return arr.reduce((a, b) => a + b);
36+
}
37+
}
38+
39+
export async function request(url: string): Promise<any> {
40+
const response = await axios.get(url, {
41+
headers: {
42+
Accept: "application/json",
43+
Authorization: `Bearer ${this.token}`,
44+
},
45+
});
46+
return response.data;
47+
}
48+
49+
export async function requestUser(user): Promise<any> {
50+
return await request(`/users/${user}`);
51+
}
52+
53+
export async function listRepos(user): Promise<object> {
54+
return Object.values(await request(`/users/${user}/repos`)).filter(repo => !repo['fork']);
55+
}
56+
57+
export async function requestRepo(user, repo): Promise<any> {
58+
return await request(`/repos/${user}/${repo}`);
59+
}
60+
61+
export async function requestLanguage(user, repo): Promise<any> {
62+
return await request(`/repos/${user}/${repo}/languages`);
63+
}
64+
65+
export async function isAuthenticated(user): Promise<boolean> {
66+
user = user.trim();
67+
return (!! user.length) &&
68+
(conf.requires.includes("*") || conf.requires.includes(user)) &&
69+
((await requestUser(user))['message'] !== "Not Found");
70+
}

0 commit comments

Comments
 (0)