Skip to content

Instantly share code, notes, and snippets.

@jrc03c
jrc03c / server.js
Created January 7, 2023 16:43
Basic HTTP / HTTPS server in Node w/ Express
const express = require("express")
const fs = require("fs")
const http = require("http")
const https = require("https")
// create a basic HTTP app that just redirects requests from HTTP to HTTPS
const httpApp = express()
httpApp.use((request, response) => {
return response.redirect("https://" + request.hostname + request.url)
@jrc03c
jrc03c / vite.config.js
Created September 2, 2022 15:39
Create a module resolution alias in (e.g.) Vite
// Note that this uses the __dirname definition from here:
// https://gist.github.com/jrc03c/6fbccd5af3a4d1b61f12e1e620e1c01b
import { defineConfig } from "vite"
import path from "path"
const __dirname = path.resolve(
import.meta.url.replace("file://", "").split("/").slice(0, -1).join("/")
)
@jrc03c
jrc03c / __dirname.js
Last active September 2, 2022 15:34
Create `__dirname` variable when using `import` statements in Node
const __dirname = path.resolve(
import.meta.url.replace("file://", "").split("/").slice(0, -1).join("/")
)
@jrc03c
jrc03c / keybase.md
Created June 10, 2022 13:46
keybase.md

Keybase proof

I hereby claim:

  • I am jrc03c on github.
  • I am jrc03c (https://keybase.io/jrc03c) on keybase.
  • I have a public key whose fingerprint is 1033 6973 7373 5FD5 7AE9 5114 ED23 C1F4 B19A C886

To claim this, I am signing this object:

@jrc03c
jrc03c / rsync-alt-port.sh
Created June 1, 2022 12:47
Use `rsync` with alternate port
# from: https://stackoverflow.com/a/4630407
rsync -rvz -e "ssh -p <PORT>" --progress /source/path user@host:/target/path
@jrc03c
jrc03c / working-with-luks.sh
Created May 10, 2022 17:14
working with LUKS
# assuming a partition already exists, apply LUKS formatting to it
sudo cryptsetup luksFormat /dev/whatever
# open it
sudo cryptsetup luksOpen /dev/whatever mapping_name
# format it to ext4 (or whatever)
sudo mkfs.ext4 /dev/mapper/mapping_name
# mount it

Modifiers

Description Code
Reset \x1b[0m
Bright \x1b[1m
Dim \x1b[2m
Underscore \x1b[4m
Blink \x1b[5m
Reverse \x1b[7m
@jrc03c
jrc03c / get-scrollbar-width.js
Created July 31, 2021 21:03
Get the width of a browser scrollbar
// from: https://davidwalsh.name/detect-scrollbar-width
function getScrollbarWidth() {
const div = document.createElement("div")
div.style = `
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
@jrc03c
jrc03c / copy-text.js
Last active April 19, 2023 20:00
Copy arbitrary text to the clipboard in the browser
// 🚨 🚨 🚨
// NOTE: According to MDN, `document.execCommand()` is deprecated. See:
// https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand
// Instead, use the Clipboard API:
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
// 🚨 🚨 🚨
function copy(text) {
const input = document.createElement("input")
input.type = "text"
@jrc03c
jrc03c / high-dpi-canvas.js
Last active March 7, 2021 23:51
Create a high-DPI-compatible canvas element
function createHighDPICanvas(width, height){
let dpi = window.devicePixelRatio || 1
let canvas = document.createElement("canvas")
canvas.width = width * dpi
canvas.height = height * dpi
canvas.style.width = width + "px"
canvas.style.height = height + "px"
canvas.getContext("2d").scale(dpi, dpi)
return canvas
}