Skip to content

Instantly share code, notes, and snippets.

Starbeamrainbowlabs sbrl

View GitHub Profile
@sbrl
sbrl / ensure-network.service
Last active Jun 15, 2020
Bash script to automatically reboot if the network connection goes down
View ensure-network.service
[Unit]
Description=Reboot if the network connection is down
After=network.target
[Service]
Type=simple
# Because it needs to be able to reboot
User=root
Group=root
EnvironmentFile=-/etc/default/ensure-network
@sbrl
sbrl / main.js
Created May 26, 2020
Multi-process line-by-line reading from a single pipe in Node.js
View main.js
"use strict";
import EventEmitter from 'events';
import child_process from 'child_process';
/**
* Helper method that waits for an event to be fired on a given object.
* @param {EventEmitter} obj The object that will fire the event - must inherit from EventEmitter
* @param {string} event_name The name of the event to wait for.
* @return {Promise} A promise that resolves when the specified event is fired on the given object.
@sbrl
sbrl / ErrorWrapper.mjs
Last active Feb 13, 2020
[ErrorWrapper.mjs] Error wrapper class - wrap errors like C♯! #error-handling
View ErrorWrapper.mjs
class ErrorWrapper extends Error {
constructor(message, inner_exception) {
super(message);
this.inner_exception = inner_exception;
}
toString() {
return `${super.toString()}\n***Inner Exception ***\n${this.inner_exception}`;
}
}
@sbrl
sbrl / Retry.mjs
Created Feb 13, 2020
[Retry.mjs] Retry a function automatically a specified number of times #utility
View Retry.mjs
"use strict";
import l from './Log.mjs';
import { ErrorWrapper } from './Errors.mjs';
/**
* Tries to execute a given function, and retried it a specified number of
* times if it throws an error.
* @param {Number} times The number of times to retry before giving up and throwing an error ourselves.
* @param {Function} func The function to execute.
@sbrl
sbrl / CliParser.mjs
Last active Mar 9, 2020
[CliParser.mjs] Minimal CLI parser for Node.js - uses Ansi.mjs #library
View CliParser.mjs
"use strict";
import fs from 'fs';
import a from './Ansi.mjs';
/**
* Represents a program and all it's arguments and subcommands.
* TODO: Publish on npm under the name "applause-cli", since it's inspired by clap?
* @license MPL-2.0
@sbrl
sbrl / Stopwatch.mjs
Created Dec 22, 2019
[Stopwatch.mjs] C#'s System.Diagnostics.Stopwatch (roughly) implemented in C#.
View Stopwatch.mjs
"use strict";
/**
* Stopwatch class for timing things.
* Based on System.Diagnostics.Stopwatch from .NET: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch
*/
class Stopwatch {
/**
* Creates a new (stopped) stopwatch.
*/
@sbrl
sbrl / Log.mjs
Last active Jan 12, 2020
[Log.mjs] Logging utility class
View Log.mjs
"use strict";
import a from './Ansi.mjs';
const LOG_LEVELS = {
DEBUG: 0,
INFO: 1,
LOG: 2,
WARN: 4,
ERROR: 8,
@sbrl
sbrl / errorimage.php
Created Oct 22, 2019
[errorimage.php] Generate error images on the fly in portable PHP
View errorimage.php
/**
* Creates an images containing the specified text.
* Useful for sending errors back to the client.
* @package core
* @param string $text The text to include in the image.
* @param int $target_size The target width to aim for when creating
* the image. Not not specified, a value is
* determined automatically.
* @return resource The handle to the generated GD image.
*/
@sbrl
sbrl / JsonStorageBox.php
Last active Sep 21, 2019
[JsonStorageBox.php]
View JsonStorageBox.php
<?php
/*
███████ ████████ ██████ ██████ █████ ██████ ███████ ██████ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██████ ███████ ██ ███ █████ ██████ ██ ██ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██████ ██ ██ ██ ██ ██████ ███████ ██████ ██████ ██ ██
*/
@sbrl
sbrl / StorageBox.php
Last active Sep 21, 2019
[StorageBox.php] A key-value data store, backed by SQLite3.
View StorageBox.php
<?php
/*
███████ ████████ ██████ ██████ █████ ██████ ███████ ██████ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██████ ███████ ██ ███ █████ ██████ ██ ██ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██████ ██ ██ ██ ██ ██████ ███████ ██████ ██████ ██ ██
*/
You can’t perform that action at this time.