Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
marcogrcr / diff-between-two-dates.js
Last active June 26, 2024 13:21
Gets the difference between two dates and a timeline of a series of dates
export function diffBetweenTwoDates(start, end) {
const startTime = new Date(start).valueOf();
const endTime = new Date(end).valueOf();
if (Number.isNaN(startTime)) throw new Error("Invalid start date");
if (Number.isNaN(endTime)) throw new Error("Invalid end date");
const fmt = (value, len = 2) => value.toString().padStart(len, "0");
const diff = new Date(endTime - startTime);
const diffNoTime = `${fmt(diff.getUTCFullYear(), 4)}-${fmt(diff.getUTCMonth() + 1)}-${fmt(diff.getUTCDate())}T00:00:00Z`;
@marcogrcr
marcogrcr / print-element-attributes.js
Created May 28, 2024 18:25
Print element attributes
((selector) => Array
.from(document.querySelectorAll(selector))
.map(x => Array
.from(Array(x.attributes.length).keys())
.reduce((o, i) => ({ [x.attributes.item(i).name]: x.attributes.item(i).value, ...o }), {})
)
)('meta')
@marcogrcr
marcogrcr / echo-server.ts
Last active April 3, 2025 22:46
A simple node.js HTTP/1.1 server that echoes back the request
import { createServer } from "node:http";
export interface CreateEchoServerInput {
/**
* Indicates the echo mode.
* - `"body"` creates a response controlled by the request body with an {@link EchoBody} shape.
* - `"request"` echoes the request headers and body.
* - `"request-in-body"` echo the request in a JSON object in the response body.
* @default "request"
*/
@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 October 25, 2024 01:01
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 [email protected]
@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"/>