Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
marcogrcr / get-file-sha256-base64.sh
Created April 18, 2025 12:05
Get file SHA256 in Base64
shasum -a 256 lambda/lambda.zip | cut -f 1 -d ' ' | xxd -r -p | base64
@marcogrcr
marcogrcr / 1-stream-json-objects.ts
Last active May 7, 2025 20:35
Streams JSON objects from a Readable in a memory-efficient manner
import type { Readable } from "node:stream";
export interface StreamJsonObjectsInput {
/** The {@link Readable} to extract JSON objects from. */
readonly readable: Readable;
}
export interface StreamJsonObjectsOutput<T> {
/** The parsed JSON object. */
readonly object: T;
@marcogrcr
marcogrcr / cli-confirm.mjs
Last active April 10, 2025 14:49
Confirms a y/n in a CLI
import { createInterface } from "node:readline";
const rl = createInterface({ input: process.stdin, terminal: false });
process.stdout.write("Please confirm with y/n: ");
for await (const line of rl) {
if (line === "y") {
break;
}
if (line === "n") {
@marcogrcr
marcogrcr / yarn-lock-duplicates.mjs
Created March 10, 2025 14:12
Gets duplicate packages in a yarn.lock file
import("node:fs")
.then(({ readFileSync, writeFileSync }) => {
const packages = new Map();
const output = [];
readFileSync("yarn.lock")
.toString()
.split("\n")
.forEach((l) => {
if (l[0] === '"') {
const [pkg] = l.split("@npm:");
@marcogrcr
marcogrcr / foreach-line.sh
Created December 17, 2024 15:59
Executes code for each line of a file
#!/bin/sh
# we use a heredoc for inliling the lines in the script, but they can be obtained from a file as well
# IMPORTANT: heredocs MUST use tabs, they cannot use space indentation
LINES=$(
cat <<- EOF
line1
line2
line3
EOF
@marcogrcr
marcogrcr / my-module.test.ts
Created December 4, 2024 16:19
Vitest mocking basics
import {
afterEach,
beforeEach,
expect,
it,
MockedClass,
MockedFunction,
vi,
} from "vitest";
@marcogrcr
marcogrcr / 1-aws-lambda-http-event-comparison.md
Last active November 28, 2024 02:10
AWS Lambda HTTP event comparison

AWS Lambda HTTP event comparison

AWS Lambda allows to create HTTP APIs fronted by API Gateway ([REST] and [HTTP]) and [ALB] (Application Load Balancer) endpoints. However, the input events vary depending on the endpoint. In order to write code that is compatible with all endpoint, certain differences need to be accounted for.

Methodology

The following endpoint configuration was applied to each endpoint type:

@marcogrcr
marcogrcr / localstack-ddb-table.sh
Created November 7, 2024 13:35
Create a DynamoDB table using localstack
#!/bin/sh
set -eu
echo 'Initializing test DynamoDB table...'
# https://docs.localstack.cloud/getting-started/installation/#docker
IMAGE=$(docker run \
--rm -d \
-p 127.0.0.1:4566:4566 \
@marcogrcr
marcogrcr / 1-string-builder.ts
Last active April 14, 2025 21:00
TypeScript StringBuilder
export interface StringBuilderOptions {
/**
* The initial size of the underlying buffer in bytes.
* @default 1048576
*/
readonly initialSize?: number;
/**
* The growth factor of the underlying buffer.
* @default 20
@marcogrcr
marcogrcr / recursive-for-each-file.sh
Created July 19, 2024 15:31
A POSIX-compatible recursive iteration over each file
#!/bin/sh
folder_path='...'
file_types='*.json'
# See https://www.shellcheck.net/wiki/SC2044 for why we loop this way
find "$folder_path" ! -name "$(printf "*\n*")" -name "$file_types" | while IFS= read -r file
do
echo "Processing $file..."
# process $file