Skip to content

Instantly share code, notes, and snippets.

View lmammino's full-sized avatar
🦁
Roar!

Luciano Mammino lmammino

🦁
Roar!
View GitHub Profile
@lmammino
lmammino / Cargo.toml
Last active February 22, 2022 19:37
Decode an EU DGC (greenpass) in Rust
[package]
name = "dgc-decode"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
base45 = "3.0.0"
ciborium = "0.2.0"
@lmammino
lmammino / neighbours.rs
Created September 2, 2021 08:13
relative neighbour pixels in generic multidimensional spaces using const generics
fn neighbour_at<const D: usize>(i: u32) -> [i32; D] {
let mut el = [0_i32; D];
let mut n = i;
for v in el.iter_mut().rev() {
*v = (n as i32 % 3) - 1; // -1 is needed because we offset the digits so that we are in range -1..1
n /= 3;
}
el
@lmammino
lmammino / db.js
Created August 14, 2021 15:23
Sample Fastify Plugin to connect to MariaDB/MySql
'use strict'
const fp = require('fastify-plugin')
const mysql = require('mysql2/promise')
const CONNECTION_STRING = process.env.DB_CONN_STRING
module.exports = fp(async function (fastify, opts) {
const db = await mysql.createConnection(CONNECTION_STRING)
@lmammino
lmammino / example.ts
Created August 3, 2021 19:03
Get your public IP in TypeScript
import { getMyPublicIp } from './utils.ts'
getMyPublicIp
.then(console.log)
.catch(console.error)
@lmammino
lmammino / dependant.js
Created June 29, 2021 17:32
Modulino pattern in Node.js
const x = require('./index.js')
@lmammino
lmammino / server.js
Created June 22, 2021 13:24
Node.js debug server
const { createServer } = require('http');
createServer((req, res) => {
console.log('--------');
console.log(JSON.stringify(req.headers, null, 2));
req.pipe(process.stdout);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ok:true}));
})
.listen(8000);
@lmammino
lmammino / event_listener.py
Last active June 18, 2021 08:24
Gzipping payload for custom metrics to cloudwatch with boto3
import boto3
import gzip
cw_client = boto3.client('cloudwatch')
event_system = cw_client.meta.events
def gzip_request_body(request, **kwargs):
gzipped_body = gzip.compress(request.body)
request.headers['Content-Encoding'] = 'gzip'
request.data = gzipped_body
@lmammino
lmammino / lib.rs
Last active May 12, 2021 14:14
Ex 11, year 2020 part 1 & part 2
use std::fmt;
use std::str::FromStr;
// [x - 1, y - 1] [ x , y - 1] [x + 1, y - 1]
// [x - 1, y ] (current) [x + 1, y ]
// [x - 1, y + 1] [ x , y + 1] [x + 1, y + 1]
const DIRECTIONS: [(i8, i8); 8] = [
(-1, -1),
(0, -1),
(1, -1),
@lmammino
lmammino / listNestedFiles.js
Created March 14, 2021 17:00
A small example for a script walking through a path to find all files in the folder and nested subfolders. Implemented only using callbacks
import { readdir, stat } from 'fs'
import { join } from 'path'
function listNestedFilesRec (path, state, cb) {
state.ops++
readdir(path, (err, files) => {
state.ops--
if (err) {
return cb(err)
@lmammino
lmammino / sample-upload-with-passthrough.js
Created July 10, 2020 19:13
sample upload with PassThrough stream
function createUploadStream (filename) {
const connector = new PassThrough()
upload(filename, connector)
return connector
}