Skip to content

Instantly share code, notes, and snippets.

@ritwickdey
ritwickdey / .env2envrc.mk
Last active March 20, 2023 15:03
Quickly convert .env (non exported variable) file to .envrc (exported variable)
# To convert .env file to .envrc
# usages: make [.env_file]2envrc
# e.g. "make .env2envrc" or "make .env.production2envrc"
%2envrc:
$(eval file := $*)
@(cat $(file) | sed 's/^/export /' > .envrc)
@echo ".envrc generated from $(file)"
# example
@ritwickdey
ritwickdey / go-webrtc-sample-example.go
Created February 20, 2022 09:59
go webrtc sample example
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/pion/webrtc/v3"
)
@ritwickdey
ritwickdey / infinite-loop-detection.js
Last active November 21, 2023 12:54
Infinite loop detection via AST transformation
export default function (babel) {
const { types: t } = babel;
const infiniteLoopDetector = babel.parse(`
function __infiniteLoopDetector() {
loopdetector.count += 1;
if(loopdetector.count > 10000) {
throw new Error("Infinte loop detected.")
//https://astexplorer.net/#/gist/6fa8fbfbc299ba7dcf27a83d50d89c26/3af7743eac480fd64e4b9013c9e0969097470959
export default function (babel) {
const { types: t } = babel;
return {
name: "ast-transform", // not required
visitor: {
ImportDeclaration(importPath) {
if (importPath.node.source.value !== "lodash") return;
/***
*
* @author Ritwick Dey (https://github.com/ritwickdey)
* @license MIT
*
* What is the hell?
* - This is a automated script to do n numbers of facebook comments.
*
* Perpose of the hell?
* You may seen such kind of post
@ritwickdey
ritwickdey / useLocalStorage.js
Created June 27, 2019 07:29
useLocalStorage React Hook
import { useState, useEffect } from 'react';
export const useLocalStorage = (storageKey, initialValue) => {
const [storageVal, setStorageVal] = useState(getFromLocalStorage(storageKey));
useEffect(() => {
setToLocalStorage(storageKey, storageVal);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [storageVal]);
@ritwickdey
ritwickdey / github-auto-close.js
Last active April 12, 2019 02:57
Auto Close Github issue by issue name (e.g. "Extension causes high cpu load" )
const comment = `Hello. few questions!!,
When you're getting this issue? after load of vscode or after clicking to Go Live ?
Is this happen every-time ? or random ?
Please reply here https://github.com/ritwickdey/vscode-live-server/issues/278
Duplicate of https://github.com/ritwickdey/vscode-live-server/issues/278
@ritwickdey
ritwickdey / MakeUnique.ts
Created January 11, 2019 09:34
MakeUnique
function makeUnique<T>(arr: T[], idSelector: (e: T) => string | number) {
const obj = {};
arr.forEach(e => {
obj[idSelector(e)] = e;
});
return Object.keys(obj).map(key => obj[key]) as T[];
}
@ritwickdey
ritwickdey / withCORS.js
Last active December 27, 2018 06:21
CORS Middleware (Higher Order Function) for firebase function // Just Nice Syntax
//For firebase funtion.
/*
Usage:
exports.foo = functions.https.onRequest(withCORS((req, res) => {
res.send("Hello World");
});
*/
@ritwickdey
ritwickdey / cors.js
Last active November 21, 2018 06:14
CORS Setup for Node.js
const express = require('express');
const app = express();
//CORS Setup
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*'); // Allowed Origins.
res.header('Access-Control-Allow-Headers', '*'); // Allowed Headers.
res.header('Access-Control-Expose-Headers', 'token'); // Exposed Headers - means client only can access those headers.
if (req.method === 'OPTIONS') {