Skip to content

Instantly share code, notes, and snippets.

View disintegrator's full-sized avatar

Georges Haidar disintegrator

View GitHub Profile
@disintegrator
disintegrator / Fetch-happened.ts
Created January 7, 2025 23:30
Cross-platform fetch error classification
/**
* Uses various heurisitics to determine if an error is a connection error.
*/
export function isConnectionError(err: unknown): boolean {
if (typeof err !== "object" || err == null) {
return false;
}
// Covers fetch in Deno as well
const isBrowserErr =
@disintegrator
disintegrator / ua-quirk.ts
Last active August 12, 2024 10:20
Setting User-Agent header on the browser has surprising results
// Chromium bug: https://issues.chromium.org/issues/40450316
const headers = new Headers();
headers.set("user-agent", "fancy/1.0");
console.log(headers.get("user-agent") == null);
// Prints: false
const req = new Request('');
req.headers.set("user-agent", "fancy/1.0");
console.log(req.headers.get("user-agent") == null);
@disintegrator
disintegrator / urls.ts
Created June 7, 2024 11:12
URL helpers
export function tryParseURL(input: unknown): URL | null {
if (input instanceof URL) {
return input;
}
if (typeof input !== "string") {
return null;
}
try {
return new URL(input);
@disintegrator
disintegrator / discard.go
Created March 4, 2024 10:11
DiscardHandler for log/slog in Go
package log
import (
"context"
"log/slog"
)
// A DiscardHandler discards all log records.
type DiscardHandler struct{}
@disintegrator
disintegrator / assert.go
Last active March 1, 2024 13:04
Assertions in Go
package main
import (
"errors"
"fmt"
"runtime"
)
func Assert(group string, pairs ...any) error {
if len(pairs)%2 != 0 {
@disintegrator
disintegrator / demo.ts
Last active November 10, 2023 15:13
console.group and the `using` keyword
function logGroup(label: string) {
console.group(label)
return {
[Symbol.dispose]: () => {
console.groupEnd()
}
}
}
function demo() {
package functional
import (
"context"
"errors"
"time"
"github.com/benthosdev/benthos/v4/public/service"
)
@disintegrator
disintegrator / sample.test.ts
Last active June 19, 2023 16:52
A sample test helper used to acquire resources and automatically tear them down with the upcoming `using` keyword.
// This is untested code. More of a proof of concept for what's to come...
async function $acquire<
Resource
>(fn: () => Promise<readonly [resource: Resource, teadown?: () => void]>) {
let [resource, teardown = () => {}] = await fn();
return {
resource,
[Symbol.dispose]: teardown,
}
@disintegrator
disintegrator / debounce.go
Created December 23, 2021 18:52
A debouncer that sends pulses on a channel (Golang, Go)
package debounce
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
@disintegrator
disintegrator / typescriptreact.json
Created December 18, 2020 10:11
VS Code snippets
{
"React function component": {
"prefix": "nrf",
"body": [
"import * as React from \"react\";",
"",
"export interface ${1:$TM_FILENAME_BASE}Props {}",
"",
"const ${1:$TM_FILENAME_BASE}: React.FC<${1:$TM_FILENAME_BASE}Props> = props => {",
" return <>{props.children}</>;",