core&utils: remove outdated code

This commit is contained in:
undefined committed 2022-08-24 14:35:45 +08:00
1 parent 5ae8258b8a
commit 7903d012da
23 files changed
+61 -144

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
import { md5 } from './crypto';
import { md5 } from '../utils';
type AvatarProvider = (src: string, size: number) => string;
-13
View File
@@ -1,13 +0,0 @@
import crypto from 'crypto';
const encrypt = (algorithm, content) => {
const hash = crypto.createHash(algorithm);
hash.update(content);
return hash.digest('hex');
};
export const sha1 = (content: string) => encrypt('sha1', content);
export const md5 = (content: string) => encrypt('md5', content);
global.Hydro.lib.md5 = md5;
global.Hydro.lib.sha1 = sha1;
-22
View File
@@ -1,22 +0,0 @@
import fs from 'fs';
import superagent, { SuperAgentRequest } from 'superagent';
async function _download(url: string, path: string, retry: number) {
const w = fs.createWriteStream(path);
superagent.get(url).retry(retry).pipe(w);
await new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
});
return path;
}
function download(url: string): SuperAgentRequest;
function download(url: string, path?: string, retry = 3) {
if (path) return _download(url, path, retry);
return superagent.get(url).retry(retry);
}
export = download;
global.Hydro.lib.download = download;
-3
View File
@@ -1,9 +1,6 @@
import './jwt';
import './download';
import './i18n';
import './mail';
import './useragent';
import './crypto';
import './paginate';
import './hash.hydro';
import './rank';
-35
View File
@@ -1,35 +0,0 @@
function decodeBase64WithUriEncoding(encodedText: string) {
return Buffer.from(encodedText, 'base64').toString('utf8');
}
function unescapedString(escapedString: string) {
escapedString += new Array(5 - (escapedString.length % 4)).join('=');
return escapedString.replace(/-/g, '+').replace(/_/g, '/');
}
function decodeJWT(idToken: string) {
const token = idToken.split('.');
if (token.length !== 3) {
throw new Error('Invalid idToken');
}
try {
const headerSegment = JSON.parse(decodeBase64WithUriEncoding(token[0]));
const payloadSegment = JSON.parse(decodeBase64WithUriEncoding(token[1]));
const signature = unescapedString(token[2]);
return {
dataToSign: [token[0], token[1]].join('.'),
header: headerSegment,
payload: payloadSegment,
signature,
};
} catch (e) {
throw new Error('Invalid payload');
}
}
export function decode(idToken: string) {
const decodedJWT = decodeJWT(idToken);
return decodedJWT.payload;
}
global.Hydro.lib.jwt = { decode };
+12 -23
View File
@@ -1,33 +1,22 @@
import { Dictionary, isNull } from 'lodash';
import { PERM, PRIV } from '../model/builtin';
const trueChecker = () => true;
const Checker = (perm: bigint, priv: number, checker: Function = trueChecker) => (handler) => (
const Checker = (perm: bigint | bigint[], priv: number | number[], checker: Function = trueChecker) => (handler) => (
checker(handler)
&& (perm ? handler.user.hasPerm(perm) : true)
&& (priv ? handler.user.hasPriv(priv) : true)
);
const buildChecker = (...permPrivChecker: Array<number | bigint | Function>) => {
let _priv: number;
let _perm: bigint;
const buildChecker = (...permPrivChecker: Array<number | bigint | Function | number[] | bigint[]>) => {
let _priv: number | number[];
let _perm: bigint | bigint[];
let checker: Function = trueChecker;
for (const item of permPrivChecker) {
if (item instanceof Object && !isNull(item)) {
if (item instanceof Array) {
if (typeof item[0] === 'number') {
// @ts-ignore
_priv = item;
} else if (typeof item[0] === 'bigint') {
// @ts-ignore
_perm = item;
}
} else if (typeof item.call !== 'undefined') {
checker = item;
}
} else if (typeof item === 'number') {
_priv = item;
} else if (typeof item === 'bigint') {
_perm = item;
if (typeof item === 'function') checker = item;
else if (typeof item === 'number') _priv = item;
else if (typeof item === 'bigint') _perm = item;
else if (item instanceof Array) {
if (typeof item[0] === 'number') _priv = item as number[];
else _perm = item as bigint[];
}
}
return Checker(_perm, _priv, checker);
@@ -51,7 +40,7 @@ export const Nav = (
};
export const ProblemAdd = (
name: string, args: Dictionary<any> = {}, icon = 'add', text = 'Create Problem',
name: string, args: Record<string, any> = {}, icon = 'add', text = 'Create Problem',
) => {
global.Hydro.ui.nodes.problem_add.push({
name, args, icon, text,
@@ -59,7 +48,7 @@ export const ProblemAdd = (
};
export const UserDropdown = (
name: string, args: Dictionary<any> = {}, ...permPrivChecker: Array<number | bigint | Function>
name: string, args: Record<string, any> = {}, ...permPrivChecker: Array<number | bigint | Function>
) => {
global.Hydro.ui.nodes.user_dropdown.push({
name, args: args || {}, checker: buildChecker(...permPrivChecker),