Skip to content

Instantly share code, notes, and snippets.

@thebearingedge
thebearingedge / t2_micro_swap.sh
Created December 2, 2022 01:58
Resize swap memory on t2.micro EC2 instance (Ubuntu)
#!/bin/sh
swap_file=/var/swap.img
if [ ! -f $swap_file ]; then
echo 'creating 1gb swap file...'
touch $swap_file
dd if=/dev/zero of=$swap_file bs=1024k count=1000
chmod 600 $swap_file
mkswap $swap_file
@thebearingedge
thebearingedge / real-array.js
Last active July 23, 2022 22:16
A "real" array in JS. Not really.
class RealArray extends Array {
constructor(length) {
if (!Number.isInteger(length) || length < 0) {
throw new Error('RealArray requires a positive integer length')
}
function boundsCheck(index) {
if (index in Array.prototype) {
throw new RangeError('RealArray does not have ' + index)
}
if (parseInt(index, 10) !== parseFloat(index, 10)) {
@thebearingedge
thebearingedge / typescript-result.ts
Last active July 5, 2022 19:29
Result type for Typescript, sorta like rust.
type Result<Ok, Err extends Error = Error> =
| { kind: 'ok', value: Ok }
| { kind: 'err', err: Err }
const Ok = <T>(value: T): Result<T> => {
return {
kind: 'ok',
value
}
}
@thebearingedge
thebearingedge / comment-threads.sql
Last active May 30, 2022 16:00
recursive common table expression comment threads
create table "users" (
"userId" serial,
"username" text not null,
primary key ("userId")
);
create table "comments" (
"commentId" serial,
"userId" integer not null,
"content" text not null,
@thebearingedge
thebearingedge / create-element.js
Created May 3, 2022 00:01
DOM creation helper function.
function h(tagName, attributes, ...children) {
if (arguments.length === 2) {
if (Array.isArray(attributes) || typeof attributes !== 'object') {
children = [attributes]
attributes = null
}
}
const $element = document.createElement(tagName)
for (const name in attributes) {
$element.setAttribute(name, attributes[name])
@thebearingedge
thebearingedge / decode-jwt.js
Created April 28, 2022 04:44
Decode a JSON Web Token in the browser.
function decode(token) {
const [, payload] = token.split('.')
try { return JSON.parse(atob(payload)) } catch (err) {}
const unsafe = payload.replace(/-/g, '+').replace(/_/g, '/')
let padded = unsafe + '='.repeat(4 - unsafe.length % 4)
const uriComponent = atob(padded).replace(/(.)/g, (_, c) => {
const hex = c.charCodeAt(0).toString(16).toUpperCase()
return '%' + (hex.length < 2 ? '0' : '') + hex
})
return JSON.parse(decodeURIComponent(uriComponent))
@thebearingedge
thebearingedge / diy-react-router.js
Last active April 28, 2022 14:00
Quick DIY React Router DOM library (framework 😏)
import React from 'react'
const RouterContext = React.createContext({
url: new URL('/', window.location.href),
navigate: () => {},
redirect: () => {}
})
export function Router({ children }) {
const [url, setUrl] = React.useState(() => new URL(window.location.href))
@thebearingedge
thebearingedge / repro.ts
Created May 19, 2021 21:47
Server-side socket has to wait for data before its client counterpart can be reliably included in a tunnel stream.
import assert from 'assert'
import { pipeline } from 'stream'
import { Socket, connect, AddressInfo } from 'net'
import { request, Server, IncomingMessage, ServerResponse } from 'http'
type Servers = {
proxyServer: Server
localServer: Server
}