Skip to content

Instantly share code, notes, and snippets.

View nkcmr's full-sized avatar

nick comer nkcmr

View GitHub Profile
package main
import (
"encoding/base64"
"fmt"
"io"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
package chanutil
import (
"context"
"sync"
)
func fanIn[T any](ctx context.Context, size int, chans ...<-chan T) <-chan T {
var wg sync.WaitGroup
copy := func(i <-chan T, o chan<- T) {
@nkcmr
nkcmr / webfinger-cf-worker.js
Created December 20, 2022 12:32
A Cloudflare worker that allows you to make your domains or email addresses aliases to your Fediverse account
// add your domain to CF and assign this worker to a route: example.com/.well-known/webfinger*
export default {
async fetch(request, env) {
return await handleRequest(request);
},
};
const lookupmap = {
"acct:me@example.com": {
#!/bin/bash
# should rename it just "git-newbranch" and put it in your path somewhere.
# once that is done it should be invokable as "git newbranch JIRA-123"
set -euo pipefail
JIRA_NUMBER="${1:-}"
if ! [[ "$JIRA_NUMBER" =~ ^[A-Z]{2,}-[0-9]+$ ]] ; then
echo "$(basename $0): error: invalid Jira number"
@nkcmr
nkcmr / example.ts
Last active November 16, 2022 15:02
Dynamic HTTP Request Router for Cloudflare Workers
import { Router } from './router.ts'
export interface Env {
// holds things like KV namespaces and Durable Objects
}
const r = Router.create<Env>((handle) => {
handle('GET', '/users/:username', (request, env, ctx, { username }) => {
return new Response(`hello, ${username}!`);
});
<?php
date_default_timezone_set('UTC');
$foo = '{"bar":{"$date":1358205756553},"pop":{"goes":{"err":{"$binary":"c3VyZS4="},"the":{"weasel":{"$date":1358205756553,"$tz":"America/New_York"}}}}}';
class EJSON {
protected static $customTypes = [];
public static function type($marker, $decoder) {
if (!is_callable($decoder)) {
@nkcmr
nkcmr / matrix.go
Last active August 24, 2021 12:15
package main
import (
"errors"
)
// stringMatrix will iterate through every combination of each row of the matrix
// it is initialized with. while most of the time, there is a static number of
// rows in a particular matrix and it is trivial to just write something like
// this:
@nkcmr
nkcmr / hsl2hex.php
Created December 11, 2012 14:51
HSL to HEX triplet algorithm
<?php
function HSL2HEX($h,$s,$l){
function hue2rgb($v1, $v2, $vH){
if($vH<0) $vH += 1;
if($vH>1) $vH -= 1;
if((6*$vH)<1) return $v1+($v2-$v1)*6*$vH;
if((2*$vH)<1) return $v2;
if((3*$vH)<2) return $v1+($v2-$v1)*((2/3)-$vH)*6;
return $v1;
@nkcmr
nkcmr / proposal.md
Last active July 17, 2019 14:15
Informal Golang Proposal: `catch` block as an alternative to `if err != nil`

Informal Golang Proposal: catch block as an alternative to if err != nil

PLEASE NOTE: I am not a language designer or have any experience in implementing or mainting a language or compiler. So, I would love to write out the EBNF syntax for what I am about to propose, but alas, I wouldn't know where to start. So, please forgive the informal nature of the proposal and feel free to ask questions; hopefully I'll be able to keep up with actual experts in this field and if not I apologize for my naivety in advance!

When the try() proposal was closed, I was relieved. There is a problem to be solved here, but try, to me at least, created more problems while trying to solve one.

The main problems, I think it created were the following:

  • try reduced the visiblility of failures. For example:
@nkcmr
nkcmr / torrent_file_processing.js
Created October 10, 2013 12:55
It took 2 days for me to figure out how to process torrent files in JavaScript. So I am publishing my findings. Fully commented and helpful links are dropped in. I hope if you find this it saves you a few hours of hair-pulling and cursing at your screen. Spoiler alert: If you don't know a whole lot about encodings, you're gonna have a bad time ;-;
var debug = require("debug")("torrent:read"); //npm install debug
var bencode = require("bencode"); //npm install bencode - https://en.wikipedia.org/wiki/Bencode
var fs = require("fs");
var crypto = require("crypto");
var _ = require("underscore"); //npm install underscore
var percent_encoding = {
encode: function(buffer) {
var ret = "", a2z, AtoZ, zero2nine, other_valid_symbols, all_unreserved_symbols;