Skip to content

Instantly share code, notes, and snippets.

View bitsmanent's full-sized avatar

Claudio Alessi bitsmanent

View GitHub Profile
@bitsmanent
bitsmanent / timers.js
Last active May 13, 2024 15:26
Set and cancel timers which can be paused and resumed
(() => {
"use strict";
/* Why don't overwrite setTimeout()? It's doable but...
* https://www.adequatelygood.com/Replacing-setTimeout-Globally.html
* Also we may want to keep running high priority timers one day. */
const timers = [];
const state = {
paused: false,
@bitsmanent
bitsmanent / hash.js
Last active May 2, 2024 20:20 — forked from zazapeta/auth.js
Hashing and verifying in Node.js using pbkdf2
const crypto = require("crypto");
const config = {
hashBytes: 32,
saltBytes: 16,
iterations: 300000,
digest: "sha512"
};
function hashPassword(pwd) {
#!/bin/sh
# Gist: https://gist.github.com/bitsmanent/6ce604a5f09bd77b59fbbdf9dab818d1
# https://www.pexels.com/it-it/api/documentation
# include resource file
. ~/.pexelsrc
# kill another instance if running
mypid=$$
/* quick-and-dirty */
const Types = {
string: x => typeof x == "string",
number: x => typeof x == "number"
};
const Interface = model => {
const check = scope => {
let fails = 0;
/* core chaining and context builder */
(() => {
"use strict";
const plugins = {};
const ready = {
callbacks: [],
installed: false
};
let oref = null;
@bitsmanent
bitsmanent / msleep.c
Last active December 21, 2022 11:38
Sleep for a specified number of milliseconds
int
msleep(int ms) {
struct timespec req = {0}, rem;
int r = ms / 1000;
if(r >= 1) {
req.tv_sec = r;
ms -= r * 1000;
}
if(ms)
@bitsmanent
bitsmanent / xstrftime.c
Created August 27, 2022 20:31
strftime(3) with proper return values
int
xstrftime(char *s, size_t max, const char *format, const struct tm *tm) {
char *fmt;
int slen, flen;
flen = strlen(format);
if(!(fmt = malloc(flen + 1)))
return -1;
fmt[0] = 'x';
memcpy(&fmt[1], format, flen);
@bitsmanent
bitsmanent / smtpd.conf
Last active March 9, 2023 10:30
OpenSMTPD configuration
# opensmtpd smtpd.conf
#
# Current version:
# https://github.com/OpenSMTPD/OpenSMTPD/issues/1171#issuecomment-1139450955
#
# Installed additional packages:
# debhelper libdb-dev libpam0g-dev libevent-dev bison
#
# Also note: /etc/smtpd created by hand.
@bitsmanent
bitsmanent / domapply.js
Last active March 21, 2022 16:36
Render only the differences between nodes of a given target
function domapply(target, nodes) {
const children = target.childNodes;
var child, n, has;
nodes.forEach((node,i) => {
/* skip comment node */
if(node.nodeType == Node.COMMENT_NODE)
return;
child = children[i];
/* add missing nodes */
@bitsmanent
bitsmanent / react-native-sprite-sheets.js
Last active September 25, 2021 17:18
Simple sprite sheets for React Native
import React from "react";
import {ImageBackground} from "react-native";
export function Sprite(props) {
const [w, h] = [props.geometry.width, props.geometry.height];
const r = Math.min(w / (props.width / props.cols), h / (props.height / props.rows));
return (
<ImageBackground
source={props.source}