This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
83 lines (78 loc) · 2.95 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { env } from "bun";
import { Database } from 'bun:sqlite'
const DETA = env.DETA == null ? null : (await import('deta')).Deta(env.DETA).Drive(env.DETA_DRIVE ?? 'dfonline');
async function load() {
if(DETA) {
const res = await DETA.get(env.DETA_PATH ?? 'database.db');
return res == null ? new Database(env.DB_PATH, { create: true }) : Database.deserialize(new Uint8Array(await res.arrayBuffer()));
// oh and btw, if the deserialize call is returning a number, I fixed it by deleting node_modules, bun.lockb, and running bun i.
}
else {
return new Database(env.DB_PATH, { create: true });
}
}
async function save() {
if(DETA) {
const res = await DETA.put(env.DETA_PATH ?? 'database.db', { data: db.serialize() });
}
else {
// tmk it saves anyway
// :memory: enjoyers
// time to for universe to collapse when someone uses my code in production in a critical scenario and have important data lost to this
// "sir, what happened to the database" "well, have a look at this"
// "what the f--"
}
}
const db = await load();
db.run(`CREATE TABLE IF NOT EXISTS users (
uuid UUID PRIMARY KEY,
username VARCHAR(16),
token TEXT
);`)
function hash(key: string) {
const hasher = new Bun.CryptoHasher("sha256");
hasher.update(key);
return hasher.digest('hex').toUpperCase();
}
export interface User {
uuid: string,
username: string,
key: string,
}
/**
* Securely saves a user. Will hash the key, so don't hash it before this.
* This will overwrite any user with the same uuid.
* @param uuid Minecraft UUID
* @param username Minecraft Name
* @param key Non-Hashed pure access key.
*/
export function setUser(uuid: string, username: string, key: string) {
if(!uuid.match(/^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/)) throw new TypeError("Couldn't validate UUID");
if(!username.match(/^\w{3,16}$/)) throw new TypeError("Couldn't validate username");
db.query(`DELETE FROM users WHERE uuid = ?1`).run(uuid);
db.query(`INSERT INTO users (uuid, username, token) VALUES (?1, ?2, ?3);`).run(uuid,username,hash(key));
save();
}
/**
* Delete a user.
* @param user Any user value, such as uuid, name, hashed token.
*/
export function deleteUser(user : string) {
db.query(`DELETE FROM users WHERE uuid = ?1 OR username = ?1 OR token = ?1`).run(user);
save();
}
/**
* Gets user data.
* @param user Any user value, such as uuid, name, token hashed or not.
*/
export function getUser(user : string) : User | null {
return db.query(`SELECT * FROM users WHERE uuid = ?1 OR username = ?1 OR token = ?1`).get(user) ?? authUser(user) as any;
}
/**
* Safely get a user which needs authorization.
* @param key Non-Hashed token
* @returns The user authorized by token.
*/
export function authUser(key: string) : User | null {
return db.query(`SELECT * FROM users WHERE token = ?`).get(hash(key)) as any;
}