Updated components build.

This commit is contained in:
Mike Cao 2025-09-01 15:59:06 -07:00
parent 5f27ba149b
commit 56af91950a
53 changed files with 942 additions and 333 deletions

View file

@ -2,7 +2,8 @@ import bcrypt from 'bcryptjs';
import redis from '@/lib/redis';
import debug from 'debug';
import { ROLE_PERMISSIONS, ROLES, SHARE_TOKEN_HEADER } from '@/lib/constants';
import { secret, getRandomChars } from '@/lib/crypto';
import { secret } from '@/lib/crypto';
import { getRandomChars } from '@/lib/generate';
import { createSecureToken, parseSecureToken, parseToken } from '@/lib/jwt';
import { ensureArray } from '@/lib/utils';
import { getUser } from '@/queries';
@ -36,12 +37,10 @@ export async function checkAuth(request: Request) {
}
}
if (process.env.NODE_ENV === 'development') {
log('checkAuth:', { token, shareToken, payload, user, grant });
}
log({ token, shareToken, payload, user, grant });
if (!user?.id && !shareToken) {
log('checkAuth: User not authorized');
log('User not authorized');
return null;
}

View file

@ -1,5 +1,4 @@
import crypto from 'crypto';
import prand from 'pure-rand';
import { v4, v5 } from 'uuid';
const ALGORITHM = 'aes-256-gcm';
@ -12,25 +11,6 @@ const ENC_POSITION = TAG_POSITION + TAG_LENGTH;
const HASH_ALGO = 'sha512';
const HASH_ENCODING = 'hex';
const seed = Date.now() ^ (Math.random() * 0x100000000);
const rng = prand.xoroshiro128plus(seed);
export function random(min: number, max: number) {
return prand.unsafeUniformIntDistribution(min, max, rng);
}
export function getRandomChars(
n: number,
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
) {
const arr = chars.split('');
let s = '';
for (let i = 0; i < n; i++) {
s += arr[random(0, arr.length - 1)];
}
return s;
}
const getKey = (password: string, salt: Buffer) =>
crypto.pbkdf2Sync(password, salt, 10000, 32, 'sha512');

View file

@ -1,51 +1,3 @@
export const urlFilter = (data: any[]) => {
const map = data.reduce((obj, { x, y }) => {
if (x) {
if (!obj[x]) {
obj[x] = y;
} else {
obj[x] += y;
}
}
return obj;
}, {});
return Object.keys(map).map(key => ({ x: key, y: map[key] }));
};
export const refFilter = (data: any[]) => {
const links = {};
const map = data.reduce((obj, { x, y }) => {
let id;
try {
const url = new URL(x);
id = url.hostname.replace(/www\./, '') || url.href;
} catch {
id = '';
}
links[id] = x;
if (!obj[id]) {
obj[id] = y;
} else {
obj[id] += y;
}
return obj;
}, {});
return Object.keys(map).map(key => ({ x: key, y: map[key], w: links[key] }));
};
export const emptyFilter = (data: any[]) => {
return data.map(item => (item.x ? item : null)).filter(n => n);
};
export const percentFilter = (data: any[]) => {
if (!data) return [];
const total = data.reduce((n, { y }) => n + y, 0);

20
src/lib/generate.ts Normal file
View file

@ -0,0 +1,20 @@
import prand from 'pure-rand';
const seed = Date.now() ^ (Math.random() * 0x100000000);
const rng = prand.xoroshiro128plus(seed);
export function random(min: number, max: number) {
return prand.unsafeUniformIntDistribution(min, max, rng);
}
export function getRandomChars(
n: number,
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
) {
const arr = chars.split('');
let s = '';
for (let i = 0; i < n; i++) {
s += arr[random(0, arr.length - 1)];
}
return s;
}