Skip to content

Instantly share code, notes, and snippets.

View Silent-Watcher's full-sized avatar
🛸
loading ...

Ali nazari Silent-Watcher

🛸
loading ...
View GitHub Profile
@Silent-Watcher
Silent-Watcher / docker-compose.dev.yml
Created October 7, 2025 18:52
Kafka docker local service
version: "3.8"
services:
kafka:
image: apache/kafka:4.0.1-rc2
container_name: kafka
ports:
- "9092:9092"
environment:
CLUSTER_ID: mkqv8X5vRla7PzR8bKqfHk
@Silent-Watcher
Silent-Watcher / docker-compose.dev.yml
Created October 7, 2025 16:45
local ElasticSearch and Kibana setup with docker compose
version: '3.8'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:9.0.3
container_name: elasticsearch
environment:
- node.name=es-node
- discovery.type=single-node
- cluster.name=es-cluster
@Silent-Watcher
Silent-Watcher / process.ts
Created June 30, 2025 20:13
process handler in node.js
export function setupProcessHandlers(options?: {
exitOnError?: boolean;
onError?: (type: 'uncaughtException' | 'unhandledRejection', error: any) => void;
}) {
const exit = options?.exitOnError ?? true;
const onError = options?.onError;
process.on('uncaughtException', (err) => {
console.error('[🔥 Uncaught Exception]:', err);
onError?.('uncaughtException', err);
@Silent-Watcher
Silent-Watcher / jose.md
Created May 30, 2025 10:04
jose vs jsonwebtoken
Feature jose jsonwebtoken
TypeScript Support ✅ Native ☑️ Partial
ESM Support ✅ Yes ❌ No
Browser Compatibility ✅ Yes ❌ No
Algorithm Support ✅ Broad (EdDSA, ES, PS, etc.) ⚠️ Limited
Maintenance ✅ Active (Panva) 💤 Slow
Crypto Security ✅ Uses native Web Crypto API ⚠️ Custom crypto
API Style ✅ Promise-based, functional ❌ Callback/Sync
@Silent-Watcher
Silent-Watcher / recaptchaFraud.ts
Created May 18, 2025 09:15
reCaptcha fraud detector
import { safeIncrWithTTL } from "#app/common/helpers/redis";
import { unwrap } from "#app/config/db/global";
import type { MultiExecResult } from "#app/config/db/redis.config";
import { redis } from "#app/config/db/redis.config";
import { logger } from "../logger.util";
const FAILURE_PREFIX = "recaptcha:fail:ip-";
const THRESHOLD = 5; // max allowed failures
const WINDOW_SEC = 60 * 60; // rolling window for failures (1h)
const BLOCK_PREFIX = "recaptcha:block:ip-";
@Silent-Watcher
Silent-Watcher / mongo.config.ts
Created May 8, 2025 16:24
mongo client connection with mongoose
import mongoose from 'mongoose';
import type { Mongoose } from 'mongoose';
import { CONFIG } from '#app/config';
import CircuitBreaker from 'opossum';
import { logger } from '#app/common/utils/logger.util';
export const MONGO_STATE_MAP: Record<number, string> = {
0: 'DISCONNECTED',
1: 'CONNECTED',
@Silent-Watcher
Silent-Watcher / redist.config.ts
Last active May 8, 2025 16:23
redis client connection with ioredis
import Redis from 'ioredis';
import CircuitBreaker from 'opossum';
import { logger } from '#app/common/utils/logger.util';
import { CONFIG } from '..';
export const REDIS_STATE_MAP: Record<number, string> = {
0: 'DISCONNECTED',
1: 'CONNECTED',
} as const;
Criterion csurf HMAC Double-Submit (Ours)
State Stateful (session store needed) Stateless (no DB/cache)
Testability Harder to test middleware + sessions Pure functions + small middlewares
Microservices-ready Requires shared session backend Easy to drop into any service
Framework lock-in Express-only Copy-paste core logic anywhere
@Silent-Watcher
Silent-Watcher / table.md
Last active May 2, 2025 18:10
API Header versioning strategies
Strategy How It Works Pros Cons
Accept Header Accept: application/vnd.myapp.v1+json Follows HTTP spec; cache-friendly Clients must set correct MIME type
Custom Header X-API-Version: 2 Explicit, easy to read Non-standard header
@Silent-Watcher
Silent-Watcher / compression.js
Last active February 3, 2025 14:04
HTTP response compression middleware
import { createGzip, createDeflate, createBrotliCompress } from 'zlib';
function getKeyWithHighestQ(obj) {
let [highestKey , highestQ ] = [null , -1]
for (const [encoding, qualiltyString] of Object.entries(obj)) {
if(!qualiltyString || qualiltyString.trim().length == 0) continue;
let [qualityKeyName , qualityValue] = qualiltyString.split('=');