Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
melbourne2991 / type-utils.ts
Last active July 4, 2023 09:40
Typescript utils for safe code
export type ILeft<L> = {
tag: 'left';
value: L;
};
export type IRight<R> = {
tag: 'right';
value: R;
};
@melbourne2991
melbourne2991 / rpc.ts
Last active February 11, 2023 06:20
typed rpc
export type ServiceDefinition = Record<string, (...args: any[]) => Promise<any>>
export interface Client<T extends ServiceDefinition> {
api: T;
handler: (e: MessageEvent) => void;
}
export type Send = (payload: any) => void;
export function Bridge<T extends ServiceDefinition>(serviceDefinition: T) {
@melbourne2991
melbourne2991 / events.ts
Created May 10, 2022 05:37
Typed event emitter
export type IEventListener<P> = (payload: P) => void;
export class Events<T1 extends Record<string, any>> {
private listeners: Map<string, IEventListener<any>[]> = new Map();
readonly addListener = <T2 extends keyof T1>(
eventType: T2,
listener: IEventListener<T1[T2]>
) => {
let arr = this.listeners.get(eventType as string);
import {promises as fs} from 'fs';
import path from 'path';
const JsonStore = <T>(filename: string, initial: T) => {
const filePath = path.resolve(__dirname, filename);
const api = {
read: async (): Promise<T> => {
if((await fs.stat(filePath)).isFile()) {
return JSON.parse(await fs.readFile(filePath, 'utf8'));
@melbourne2991
melbourne2991 / handler.ts
Last active June 6, 2021 06:51
NextJS API Util
import { NextApiRequest, NextApiResponse } from "next";
import { StatusCodes, getReasonPhrase } from "http-status-codes";
type NextHandler<R> = (req: NextApiRequest, res: NextApiResponse<R>) => void;
type ResponseEnhanced<R> = {
status?: StatusCodes | number;
headers?: Record<string, string>;
body?: R;
};
@melbourne2991
melbourne2991 / makeStateContext.tsx
Last active April 7, 2021 22:10
React state context
import React, { Dispatch, createContext, useMemo, useContext } from "react";
type ProviderProps<S, A> = React.PropsWithChildren<{
state: S;
dispatch: Dispatch<A>;
}>;
type MakeStateContextReturn<S, A> = [
ContextProvider: ({ state, dispatch }: ProviderProps<S, A>) => JSX.Element,
useContext: () => [S, Dispatch<A>]

Create Root CA (Done once)

Create Root Key

Attention: this is the key used to sign the certificate requests, anyone holding this can sign certificates on your behalf. So keep it in a safe place!

openssl genrsa -des3 -out rootCA.key 4096

Create Root CA (Done once)

Create Root Key

Attention: this is the key used to sign the certificate requests, anyone holding this can sign certificates on your behalf. So keep it in a safe place!

openssl genrsa -des3 -out rootCA.key 4096
#!/usr/bin/env node
const readline = require('readline')
const colors = require('colors/safe')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
@melbourne2991
melbourne2991 / gist:07224c22ccf071a1df2e920970cfce95
Created May 24, 2020 16:32
map union record types from js
type ofCst;
type unknownCstElement;
type cstChildrenDict = Js.Dict.t(list(unknownCstElement));
type cstNode = {
name: string,
children: cstChildrenDict,
};
type token;