Skip to content

Instantly share code, notes, and snippets.

@dillonstreator
dillonstreator / context-aware-io-copy.md
Last active September 27, 2025 10:57
Golang context aware `io.Copy`
type readerFunc func(p []byte) (n int, err error)

func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }

func Copy(ctx context.Context, dst io.Writer, src io.Reader) error {
	_, err := io.Copy(dst, readerFunc(func(p []byte) (int, error) {
		select {
		case <-ctx.Done():
 return 0, ctx.Err()
@dillonstreator
dillonstreator / golang-arbitrary-image-count-upload-with-browser-compression.md
Created August 21, 2023 00:41
Golang http server arbitrary image count upload with `browser-image-compression` and concurrent streaming
package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"strings"
@dillonstreator
dillonstreator / github-action-validate-k8s-yaml.yaml
Last active September 19, 2023 21:18
Github Action to validate Kubernetes yaml in a directory (`k8s`) with `kubeconform` including popular CRD's
name: Validate Kubernetes YAML
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
@dillonstreator
dillonstreator / fetchTimeout.ts
Last active April 13, 2024 20:36
`fetch` wrapper with timeout
class TimeoutError extends Error {
error: unknown;
constructor(timeoutMs: number, error: unknown) {
super(`Operation timed out after ${timeoutMs}ms`);
this.name = 'TimeoutError';
this.error = error;
}
}