Skip to content

Instantly share code, notes, and snippets.

View pourdaavar's full-sized avatar
🎲

Hossein Pourdavar pourdaavar

🎲
View GitHub Profile
@pourdaavar
pourdaavar / storyBookRouter.tsx
Created July 4, 2025 08:03 — forked from jaens/storyBookRouter.tsx
Storybook fake Tanstack router
/* eslint-disable react-refresh/only-export-components */
import {
createMemoryHistory,
createRootRoute,
createRoute,
createRouter,
useRouterState,
type NotFoundRouteProps,
} from "@tanstack/react-router";
import { createContext, useContext, type ReactNode } from "react";
@pourdaavar
pourdaavar / user.controller.ts
Created June 4, 2025 14:31 — forked from animir/user.controller.ts
Nest.js prevent brute-force against authorisation example
import { Request, Response } from 'express';
import { Body, Controller, Post, Req, Res } from '@nestjs/common';
import { UserService } from './user.service';
import * as Redis from 'ioredis';
import { RateLimiterRedis } from 'rate-limiter-flexible';
const redisClient = new Redis({enableOfflineQueue: false});
const maxWrongAttemptsByIPperDay = 100;
const maxConsecutiveFailsByUsernameAndIP = 5;
All code you write MUST be fully optimized.“Fully optimized” includes:
• Maximizing algorithmic big-O efficiency for memory and runtime (e.g., preferring O(n) over O(n²) where possible, minimizing memory allocations).
• Using parallelization and vectorization where appropriate (e.g., leveraging multi-threading, GPU acceleration, or SIMD instructions when the problem scale and hardware context justify it).
• Following proper style conventions for the code language (e.g., adhering to PEP 8 for Python, camelCase or snake_case as per language norms, maximizing code reuse (DRY)).
• No extra code beyond what is absolutely necessary to solve the problem the user provides (i.e., no technical debt, no speculative features, no unused variables or functions).
• Ensuring readability and maintainability without sacrificing performance (e.g., using meaningful variable/function names, adding concise comments only where intent isn’t obvious from the code).
• Prioritizing language-specific best practices and idiomatic
@pourdaavar
pourdaavar / retryDynamicImport.ts
Created October 13, 2024 13:02 — forked from mberneti/retryDynamicImport.ts
This utility function retryDynamicImport enhances React’s lazy loading mechanism by adding retry logic with a versioned query parameter. It retries importing a component multiple times in case of failure, which can be useful for bypassing browser cache or dealing with intermittent network issues. It can be used as a drop-in replacement for React…
// Usage:
// Replace React.lazy(() => import('x'));
// with retryDynamicImport(() => import('x'));
import { ComponentType, lazy } from 'react';
const MAX_RETRY_COUNT = 15;
const RETRY_DELAY_MS = 500;
// Regex to extract the module URL from the import statement
@pourdaavar
pourdaavar / object-group-by.js
Created September 18, 2024 02:57 — forked from gtrabanco/object-group-by.js
Object.groupBy polyfill
const hasGroup = typeof Object.groupBy === typeof undefined || typeof Array.groupToMap === typeof undefined || typeof Array.group === typeof undefined;
if (!hasGroup) {
const groupBy = (arr, callback) => {
return arr.reduce((acc = {}, ...args) => {
const key = callback(...args);
acc[key] ??= []
acc[key].push(args[0]);
return acc;
}, {});
};
docker ps -f status=exited | grep "\-cache-" | awk '{print $1}' | xargs docker rm
@pourdaavar
pourdaavar / Virtual IP Address Failover Using Keepalived.guide
Created July 19, 2024 19:33 — forked from loinguyenduc101/Virtual IP Address Failover Using Keepalived.guide
Configuring Simple Virtual IP Address Failover Using Keepalived
#### https://docs.oracle.com/cd/E37670_01/E41138/html/section_uxg_lzh_nr.html
Enable IP forwarding:
# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
# sysctl -p
net.ipv4.ip_forward = 1
Add firewall rules to allow VRRP communication using the multicast IP address 224.0.0.18 and the VRRP protocol (112) on each network interface that Keepalived will control, for example:
# iptables -I INPUT -i eth0 -d 224.0.0.0/8 -p vrrp -j ACCEPT
# iptables -I OUTPUT -o eth0 -d 224.0.0.0/8 -p vrrp -j ACCEPT
# service iptables save
@pourdaavar
pourdaavar / nginx-webp-sample.conf
Created May 25, 2024 17:02 — forked from uhop/nginx-webp-sample.conf
Serving WEBP with nginx conditionally.
user www-data;
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
@pourdaavar
pourdaavar / nginx.conf
Created May 17, 2024 15:50 — forked from weirongxu/nginx.conf
nginx image filter
server {
# Internal image resizing server.
server_name localhost;
listen 8888;
location /resize {
alias /var/www/app/current/public/imgs;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
allow 127.0.0.0/8;
@pourdaavar
pourdaavar / previous-git-tag.sh
Created May 17, 2024 10:45 — forked from kjantzer/previous-git-tag.sh
Get Previous Git Tag (the one before the latest tag)
# http://stackoverflow.com/a/28818420/484780
git describe --abbrev=0 --tags `git rev-list --tags --skip=1 --max-count=1`