Skip to content

Instantly share code, notes, and snippets.

View cyberinferno's full-sized avatar
🎯
Focusing

Karthik Panjaje cyberinferno

🎯
Focusing
View GitHub Profile
@cyberinferno
cyberinferno / cast.ts
Created March 21, 2023 15:38
Typescript type casting function generated by ChatGPT
function cast<T>(value: unknown, type: { new (): T }): T {
if (value instanceof type) {
return value as T;
}
if (typeof value === "object" && value !== null && "toJSON" in value) {
return cast(value.toJSON(), type);
}
const coercedValue = type(value);
import axios from "axios";
import { isDevMode } from "utils/core";
/**
* Returns an identical console object that can send logs to a log server
* if URL is specified
*
* @param remoteUrl? URL to send logs to
* @returns {{debug: () => void, error: () => void, info: () => void, log: () => void, warn: () => void}} Remote console object
*/
@cyberinferno
cyberinferno / logServer.js
Created September 22, 2022 04:58
A simple JSON log server
const http = require("http");
const fs = require("fs");
const { networkInterfaces } = require("os");
const path = require("path");
const PORT = process.argv.length > 2 ? process.argv[2] : 8081;
const LOG_PATH = process.argv.length > 3 ? process.argv[3] : "logs";
if (!fs.existsSync(LOG_PATH)) {
fs.mkdirSync(LOG_PATH);
}
#!/bin/bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
###################################
# BREW uninstall script for osx_bootstrap.sh script
###################################
SUDO_USER=$(whoami)
#!/bin/bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
# inspired by
# https://gist.github.com/codeinthehole/26b37efa67041e1307db
# https://github.com/why-jay/osx-init/blob/master/install.sh
# https://github.com/timsutton/osx-vm-templates/blob/master/scripts/xcode-cli-tools.sh
@cyberinferno
cyberinferno / Dockerfile
Created January 13, 2022 09:39
Working NextJS Dockerfile
# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
@cyberinferno
cyberinferno / boost_tcp.cpp
Created November 25, 2021 04:54
Boost Asio TCP server and client
#include <boost/asio.hpp>
class TcpServer
{
public:
TcpServer(boost::asio::io_service& io_service, short port)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
StartAccept();
}
/**
* set interval react hook
*/
import React, { useState, useEffect, useRef } from 'react';
function useInterval(callback, delay) {
const savedCallback = useRef();
// Remember the latest callback.
useEffect(() => {
/**
* Example local storage custom hook
*
* Usage:
* Same as useState hook
*/
import { useState, useEffect } from 'react';
function getSavedValue(key, initialValue) {
const savedValue = localStorage.getItem(key);
@cyberinferno
cyberinferno / CustomContext.js
Created September 26, 2020 15:35
Custom context react
/**
* Usage:
*
* export default function MyComponent() {
* const customVariable = useCustom();
* const changeCustomVarible = useCustomUpdate();
* // Call change custom variable function to change value to reflect to all other places
* }
* <CustomProvider><MyComponent /></CustomProvider>
*/