Skip to content

Instantly share code, notes, and snippets.

@bminer
bminer / express_sessions_socket.io.js
Created September 13, 2011 02:51
Accessing/Manipulating Session Data in Socket.IO (Express + Sessions + Socket.IO)
var express = require('express');
var RedisStore = require('connect-redis')(express);
express.socketio = require('socket.io');
var app = express.createServer();
app.configure(function() {
app.use(express.cookieParser() );
app.sessionStore = new express.session.MemoryStore(); //Not recommended for production use
app.use(express.session({
@bminer
bminer / ebs-backup.php
Created March 8, 2012 15:48
Simple PHP script to backup EC2 EBS volumes. Nice to include as a daily Cron job.
#! /usr/bin/env php
<?php
//Don't forget to setup credentials for the AWS API.
require_once('/usr/lib/aws-php-sdk-1.3.1/sdk.class.php');
$ec2 = new AmazonEC2();
$volumes = array(
'vol-xxxxxxxx' => '-- Your snapshot description here --',
'vol-xxxxxxxx' => '-- Your snapshot description here --',
'vol-xxxxxxxx' => '-- Your snapshot description here --'
@bminer
bminer / exportDatabaseToCSV.php
Created June 20, 2012 17:35
Quick code to export an entire database schema to RFC4180-compliant CSV files and wrap them in a Gzip-compressed tarball.
<?php
/* exportDatabaseToCSV
Author: Blake C. Miner
$db - must be a PDO connection
$database - string, the name of the schema to be backed up
$filename - string, the name of the *.tar file to be generated
*/
function exportDatabaseToCSV($db, $database, $filename) {
//Delete files
if(file_exists($filename) )
@bminer
bminer / changeTypeAttr.js
Created August 31, 2012 21:30
Cross-browser solution for changing the 'type' attribute of an `<input/>` tag.
/* x is the <input/> element
type is the type you want to change it to.
jQuery is required and assumed to be the "$" variable */
function changeType(x, type) {
if(x.prop('type') == type)
return x; //That was easy.
try {
return x.prop('type', type); //Stupid IE security will not allow this
} catch(e) {
//Try re-creating the element (yep... this sucks)
@bminer
bminer / variableHash.js
Created January 23, 2013 00:30
Generate a variable-length hash of `data` in Node.js
var crypto = require("crypto");
/* variableHash - Generate a variable-length hash of `data`.
Similar to the answer here: http://crypto.stackexchange.com/a/3559/4829
If you want a b-bit hash of the message m, then use the first b bits of AES-CTR(SHA256(m)).
Rather than using the suggested algorithm in the stackexchange answer above, I developed
my own.
I decided to derive AES256 initialization vector and key from the output of SHA256(data).
@bminer
bminer / range_sensor.py
Created May 21, 2018 15:25
Python Script for Raspberry Pi + HC-SR04 sensor to measure water level in a sump pit
#!/usr/bin/python
# Measure distance to water level in sump pit using
# HC-SR04 ultrasonic ranging module
# Sensor info:
# 2cm - 4m. range with ~3mm. accuracy, 15 degree measuring angle
# 5VDC, 15mA working current
# Suggestion in spec to connect ground before 5VDC line when hot plugging
# Measure surface area no less than 0.5 m^2 (really?)
@bminer
bminer / reactHandlerUtils.js
Last active October 11, 2018 20:17
Utility to make React `onChange` event handlers more enjoyable to write
/* Returns a function `(value, merge) => {...}` that calls
`component.setState(...)` to update the component's state with the new value
for `key`.
A simple example:
const func = updateKey(component, "counter");
func(23);
which is equivalent to:
package main
import (
"fmt"
"strings"
)
// odds computes the square root by adding odd numbers together until n is
// reached.
// Cost: 4 commands per loop; O(n) loops