Skip to content

Instantly share code, notes, and snippets.

View Eddie2111's full-sized avatar

ASM Tareq Mahmood Eddie2111

View GitHub Profile
@Eddie2111
Eddie2111 / gist:e0df618cff4a29b5db98f7e135becf3c
Last active April 23, 2025 04:45
Rate-limiting-util_for_next-auth.ts
// !Important: DO NOT IMPLEMENT THIS ON large scale app
// this is an implementation for token bucket algorithm
const tokenBuckets = new Map();
const RATE_LIMIT = 3;
const INTERVAL = 3 * 60 * 1000;
export function allowRequest(userId: string) {
const now = Date.now();
if (!tokenBuckets.has(userId)) {
@Eddie2111
Eddie2111 / sanity-image-upload-and-get_route.ts
Created April 22, 2025 18:06
Uploading images to Sanity CMS from NextJS App Router
import sharp from "sharp";
// example rate limiter for getting images
import RateLimiter_Middleware from "@/lib/rate-limiter.middleware";
function generateUniqueFilename() {
const timestamp = new Date().toISOString().replace(/[:.-]/g, "_");
const randomString = Math.random().toString(36).substring(2, 8);
return `${timestamp}_${randomString}.webp`;
}
var validateEmail = (email)=> {
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
return re.test(email)
};
var validateNumber = (number) =>{
var re = /(^(\+88|0088)?(01){1}[3456789]{1}(\d){8})$/;
return re.test(number);
};