Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / eventemitter.js
Last active April 23, 2024 18:46
A very simple EventEmitter in pure JavaScript (suitable for both node.js and browsers).
/* Polyfill indexOf. */
var indexOf;
if (typeof Array.prototype.indexOf === 'function') {
indexOf = function (haystack, needle) {
return haystack.indexOf(needle);
};
} else {
indexOf = function (haystack, needle) {
var i = 0, length = haystack.length, idx = -1, found = false;
@mudge
mudge / blocklist.sh
Last active April 9, 2024 16:41
A Bash script to generate an Unbound configuration to block all domains on The Firebog's "The Big Blocklist Collection" and allow all domains on Anudeep's list
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# Download all ticked blocklists from The Firebog's "The Big Blocklist
# Collection" [0] and block access to them with Unbound by redirecting traffic
# to 0.0.0.0.
#
# [0]: https://firebog.net
(
@mudge
mudge / production.rb
Last active November 21, 2023 14:06
How to configure Rails and Rack::Attack to use the real client IP when running behind Cloudflare
Rails.application.configure do
# Add Cloudflare's IPs to the trusted proxy list so they are ignored when
# determining the true client IP.
#
# See https://www.cloudflare.com/ips-v4/ and https://www.cloudflare.com/ips-v6/
config.action_dispatch.trusted_proxies = ActionDispatch::RemoteIp::TRUSTED_PROXIES + %w[
173.245.48.0/20
103.21.244.0/22
103.22.200.0/22
103.31.4.0/22
@mudge
mudge / new.html.erb
Last active November 9, 2023 14:51
Using a custom direct upload controller with Rails Active Storage to limit file size before any data is uploaded
<%= form_with model: @post, data: { controller: 'upload', action: 'direct-upload:error@window->upload#error' } do |f| %>
<%= f.file_field(:image, data: { 'direct-upload-url' => uploads_url }) %>
<%= f.submit %>
<% end %>
@mudge
mudge / webcam-share-ended
Last active October 30, 2023 11:49
A Tuple Trigger to turn an Elgato Key light on and off when your webcam is shared
#!/bin/bash
if [[ "$TUPLE_TRIGGER_IS_SELF" == "true" ]]
then
curl --silent --request PUT --json '{"lights":[{"on":0}]}' http://elgato-key-light-air-2282.local:9123/elgato/lights
fi
@mudge
mudge / en.rb
Last active September 5, 2023 09:32
Using date ordinals in Rails through an I18n time format.
# This goes in config/locales/en.rb (*not* en.yml)
{
:en => {
:time => {
:formats => {
:full => lambda { |time, _| "%H:%M | %A, #{time.day.ordinalize} %B %Y" }
}
}
}
}
@mudge
mudge / api_controller.rb
Last active March 24, 2023 05:27
Instruct Rails to respect the Accept header even if it contains a wildcard
class ApiController < ApplicationController
before_action :only_respect_accept_header
private
# By default, Rails will ignore the Accept header if it contains a wildcard
# and assume the client wants HTML (or JS if using XMLHttpRequest). See
# https://github.com/rails/rails/blob/a807a4f4f95798616a2a85856f77fdfc48da4832/actionpack/lib/action_dispatch/http/mime_negotiation.rb#L171-L173
#
# If you don't expect your clients to be browsers, we want to override this
@mudge
mudge / nginx.conf
Created September 20, 2010 12:29
Nginx configuration to redirect requests for non-existent files to an external URI without using if.
# Redirect all requests for non-existent files to another web
# site, used on my own site to redirect people to my GitHub
# account page.
server {
server_name example.com;
location / {
try_files $uri $uri/index.html @external;
}
location @external {
rewrite ^ $scheme://anotherexample.com$request_uri redirect;
@mudge
mudge / data_uri.rb
Last active December 16, 2022 12:10
A Ruby regular expression to parse data URIs based on RFC 2397.
require 'base64'
class DataUri
REGEXP = %r{
data:
(?<mediatype>
(?<mimetype> .+? / .+? )?
(?<parameters> (?: ; .+? = .+? )* )
)?
(?<extension>;base64)?
@mudge
mudge / use-debounce.js
Last active September 22, 2022 10:14
A custom React Hook for a debounced click handler with a given callback and delay.
/*
* Inspired by Dan Abramov's "Making setInterval Declarative with React Hooks",
* this is a custom hook for debouncing a callback (e.g. for click handlers) such
* that a callback will not be fired until some delay has passed since the last click.
* The callback will automatically be updated with the latest props and state on every
* render meaning that users don't need to worry about stale information being used.
*
* See https://overreacted.io/making-setinterval-declarative-with-react-hooks/ for the
* original inspiration.
*/