Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
marcogrcr / echo-server.js
Created May 22, 2024 20:41
A simple node.js HTTP/1.1 server that echoes back a request as a JSON object
import { createServer } from "http";
const server = createServer((req, res) => {
const { method, url: path, headersDistinct: headers } = req;
let data = [];
req.on("data", (chunk) => data.push(chunk));
req.on("end", (chunk) => {
data.push(chunk);
data = data.join("");
@marcogrcr
marcogrcr / transform-contentful-type-files.md
Last active May 2, 2024 00:14
Transforms files generated by contentful-typescript-codegen

This script transforms files generated by [contentful-typescript-codegen] to ensure the sys property is in sync with the [contentful] package.

For example, the following auto-generated file:

// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Asset, Entry } from 'contentful';
import { Document } from '@contentful/rich-text-types';
@marcogrcr
marcogrcr / AwsSdkV2AndVirtualThreads.java
Last active March 21, 2024 17:35
Java AWS SDK v2 and virtual threads
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@marcogrcr
marcogrcr / extract-jar-files.sh
Created March 13, 2024 00:26
Extract all nested jar files
#!/bin/sh
find . -name '*.jar' | while read -r jar_path; do
jar=$(basename "$jar_path")
folder=$(dirname "$jar_path")
cd "$folder" || exit 1
unzip -o "$jar"
cd - || exit 1
done
echo 'Done!'
@marcogrcr
marcogrcr / recommended-flags.sh
Created March 11, 2024 19:41
Recommended flags for shell scripts
@marcogrcr
marcogrcr / parse-args.sh
Created March 11, 2024 19:23
POSIX compliant argument parsing example
#!/bin/sh
# Example to parse arguments in a POSIX shell file
# Inspired by: https://stackoverflow.com/questions/2875424/correct-way-to-check-for-a-command-line-flag-in-bash
# error codes
invalid_args=-1
# arg defaults
value=""
@marcogrcr
marcogrcr / rtx-in-ubuntu.md
Created December 2, 2023 09:15
rtx-in-ubuntu.md

When using [rtx] in Ubuntu 22.04, take the following into account:

dotnet

Installation works out of the box via the [asdf-dotnet] plugin.

rtx install dotnet@8.0.100
@marcogrcr
marcogrcr / child-src-data-proof-of-concept.mjs
Last active October 13, 2023 17:49
Content-Security-Policy: `child-src data:` for allowing `data:` URLs in workers but mitigate malicious `<iframe>`/`<frame>` elements
import { createServer } from "node:http";
const CSP =
"default-src 'none'; child-src data:; frame-src https://www.example.com; script-src 'nonce-abc'; worker-src data:";
const INDEX_PAGE = `
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
@marcogrcr
marcogrcr / aws-mfa-credentials.py
Created October 2, 2023 03:55
Invokes AWS STS GetSessionToken using the AWS CLI and stores the temporary credentials in ~/.aws/credentials
#!/usr/bin/env python
import json, os, subprocess
from argparse import ArgumentParser
from configparser import ConfigParser
from pathlib import Path
# 1. parse args
parser = ArgumentParser(
prog='mfa-aws-credentials',
description='Invokes AWS STS GetSessionToken using the AWS CLI and stores the temporary credentials in ~/.aws/credentials'
@marcogrcr
marcogrcr / AmbiguousFunctionalArgumentOverload.kt
Last active July 18, 2023 20:44
Ambiguous functional argument overload
import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Supplier
// Take these ambiguous overloads:
fun fn(consumerLike: (String) -> Unit) = consumerLike("").also { println("consumer-like") }
fun fn(consumer: Consumer<String>) = consumer.accept("").also { println("consumer") }