Skip to content

Instantly share code, notes, and snippets.

View maxktz's full-sized avatar

Max Katz maxktz

View GitHub Profile
@maxktz
maxktz / env.ts
Last active March 20, 2025 01:59
Env `pnpm install dotenv zod`
import dotenv from "dotenv";
import { z } from "zod";
dotenv.config();
const envSchema = z.object({
RABBITMQ_URL: z.string().url(),
RABBITMQ_QUEUE: z.string().min(1),
});
@maxktz
maxktz / global-singleton.ts
Created February 7, 2025 03:13
Global singleton utility function for nextjs. to avoid re-initialization hot reloads
export function globalSingleton<T>(key: string, createInstance: () => T): T {
const globalStore = globalThis as unknown as Record<string, T | undefined>;
if (!globalStore[key]) {
globalStore[key] = createInstance();
}
return globalStore[key];
}
@maxktz
maxktz / map-telegram-proxy-string.ts
Created February 4, 2025 00:06
Map Proxy String to GramJS ProxyInterface
import type { ProxyInterface } from "telegram/network/connection/TCPMTProxy";
export function mapTelegramProxyString(
proxy?: string
): ProxyInterface | undefined {
const { protocol, hostname, port, username, password } = proxy
? new URL(proxy!)
: {};
if (proxy) {
@maxktz
maxktz / ranged_number.ts
Last active September 17, 2024 13:30
RangedNumber, utility class used to store range of numbers and get random, or mid value easily
/**
* @example
* // Example usage:
* let rn = RangedNumber.fromString("1");
* console.log(rn); // RangedNumber { min: 1, mid: 1, max: 1 }
*
* let rn = RangedNumber.fromString("1-2");
* console.log(rn); // RangedNumber { min: 1, mid: 1.5, max: 2 }
*
* let randomNumber = rn.get();
@maxktz
maxktz / aes_password_string_encryption.ts
Last active September 12, 2024 09:04
JS TS module to encrypt and decrypt string with password, using AES, 256bit salt, and 128 bit init vector.
import crypto from "crypto"; // npm i crypto
/** Helper function to generate a random salt */
export function generateSalt(length: number = 32): string {
return crypto.randomBytes(length).toString("hex");
}
/** Derive a key from a password and salt */
export function deriveKey(password: string, salt: string): Buffer {
return crypto.pbkdf2Sync(password, salt, 100000, 32, "sha256");
@maxktz
maxktz / gist:1be6d7dcc09560266ea44aba14d5d1a8
Created September 12, 2024 08:57
JS/TS encrypt string with password using AES and decrypt methods
import crypto from "crypto";
/** Helper function to generate a random salt */
export function generateSalt(length: number = 32): string {
return crypto.randomBytes(length).toString("hex");
}
/** Derive a key from a password and salt */
export function deriveKey(password: string, salt: string): Buffer {
return crypto.pbkdf2Sync(password, salt, 100000, 32, "sha256");