Skip to content

Instantly share code, notes, and snippets.

@tsmx
tsmx / git-build-nodejs-multi-version-coveralls.yml
Last active Jun 28, 2021
Building a NodeJS app for multiple node versions with GitHub actions and report test results to Coveralls.
View git-build-nodejs-multi-version-coveralls.yml
# save as ./github/workflows/git-ci-build.yml
# make sure that 'test-coverage' generates the coverage reports (lcov)
name: git-ci-build
on:
[push]
jobs:
build:
@tsmx
tsmx / sorted-iteratable-map.js
Last active Aug 19, 2021
sorted-iteratable-map: iterate with a for-loop over a JavaScript Map sorted by value attributes with a custom iterator using function* and yield*
View sorted-iteratable-map.js
// In the example: sort descending by 'activeUsers' attribute of the value object
var countries = new Map();
countries.set('DE', { name: 'Germany', activeUsers: 15000 });
countries.set('PL', { name: 'Poland', activeUsers: 13900 });
countries.set('UK', { name: 'United Kingdom', activeUsers: 14500 });
console.log('without iterator:');
for (let [key, info] of countries) {
@tsmx
tsmx / express-param-middleware.js
Last active Mar 26, 2021
NodeJS Express - middleware function with custom parameter
View express-param-middleware.js
var express = require("express");
var app = express();
const setMyInfo = (text) => {
return (req, res, next) => {
req.myInfo = text;
next();
};
};
@tsmx
tsmx / fedora-fix-grub2-after-kernel-update.md
Last active Mar 26, 2021
Fedora - fix grub2 after kernel update with invalid environment block error
View fedora-fix-grub2-after-kernel-update.md

Sometimes an update of the kernel breaks your current grub2. You will see an error like /usr/bin/grub2-editenv: error: invalid environment block during execution of the update procedure.

In that case don't restart your machine because it is most likely that this will fail and then you'll have to fix it using chroot from a Live Media. Instead, immediately after the update showing up the error, fix it by executing the following as root:

dnf reinstall grub2-efi-x64 shim-x64
grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
@tsmx
tsmx / nginx-cors-dev-proxy.md
Last active Sep 24, 2021
nginx as local proxy to avoid CORS during development
View nginx-cors-dev-proxy.md

nginx as a proxy to avoid CORS during development

A simple setup for using nginx to avoid CORS errors during local development without the need of code changes.

Local dev situation

Let's assume you are developing a frontend and a backend service belonging together. It's a good practice to have those two projects separated to decouple development, deployment, patching etc.

At the end, both parts mostly would run under one domain but in different contexts, e.g.:

@tsmx
tsmx / react-counter-func.md
Last active May 2, 2021
CounterFunc: functional React component for animated counting up using standard hooks
View react-counter-func.md

CounterFunc: functional React component for animated counting up using standard hooks

Small functional React component for counting numbers up to a certain value in a specified duration in ms. Useful for creating animated dashboards etc.

Uses React hooks useEffect, useState and useRef. Good example on how to pass props to a useEffect hook without declaring them as dependencies and how to deal with setInterval in functional components.

Usage

<CounterFunc countFrom={0} countTo={123} durationMs={400} />
@tsmx
tsmx / react-counter.md
Last active May 2, 2021
Counter: a simple React component for animated counting up
View react-counter.md

Counter: React component for animated counting up

Small React component for counting numbers up to a certain value in a specified duration in ms. Useful for creating animated dashboards etc.

Usage

<Counter countFrom={0} countTo={123} durationMs={400}/>
@tsmx
tsmx / gcp_instance_template_multi_nic.md
Last active Aug 5, 2021
GCP: instance template with multiple NIC's
View gcp_instance_template_multi_nic.md

GCP Compute Engine instance template creation with multiple NIC's and health-check FW rule

An instance template with more than one NIC obviously can't be created in the GCP web console. But it could easily be achieved using the gcloud CLI with consecutive --network-interface options.

gcloud compute --project=YOUR_PROJECT instance-templates create multi-nic-vm-template --machine-type=e2-micro --network-interface=subnet=projects/YOUR_PROJECT/regions/europe-west3/subnetworks/my-subnet-1,no-address --network-interface=subnet=projects/YOUR_PROJECT/regions/europe-west3/subnetworks/my-subnet-2,no-address --maintenance-policy=MIGRATE --image=my-image-1 --image-project=YOUR_PROJECT --boot-disk-size=10GB --boot-disk-type=pd-standard --boot-disk-device-name=instance-template-1 --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any --tags=allow-health-check

In this example I specified two subnets that belong to different VPC's.

@tsmx
tsmx / git-build-nodejs-coveralls.yml
Last active Nov 22, 2020
Building a NodeJS app with GitHub actions and report test results to Coveralls.
View git-build-nodejs-coveralls.yml
# save as ./github/workflows/git-ci-build.yml
# make sure that 'test-coverage' generates the coverage reports (lcov)
name: git-ci-build
on:
[push]
jobs:
build:
@tsmx
tsmx / nodejs-promise-basic-example.js
Last active Nov 11, 2020
NodeJS Promise example showing basic usage and control flow with: resolve, reject, then, catch & throw.
View nodejs-promise-basic-example.js
function isOdd(x) {
return new Promise((resolve, reject) => {
if (x == 11) throw new Error('11 is not allowed!');
if (x % 2) {
resolve(x.toString() + ' is odd')
}
else {
reject(new Error(x.toString() + ' is even'));
}
});