Skip to content

Instantly share code, notes, and snippets.

View cjthompson's full-sized avatar

Chris Thompson cjthompson

  • Osiris Development
  • Colorado, USA
View GitHub Profile
@cjthompson
cjthompson / findschema.js
Last active September 28, 2021 20:05
Find all possible fields and their types from a MongoDB collection
var obj = {};
function reduce(accum, doc) {
for (var key in doc) {
if (doc.hasOwnProperty(key) && typeof doc[key] !== 'function') {
const type = typeof doc[key]
if (type === 'object') {
const aKey = (accum[key] && typeof accum[key] === 'string') ? key + '_obj' : key
accum[aKey] = accum[aKey] ? accum[aKey] : {}
reduce(accum[aKey], doc[key])
@cjthompson
cjthompson / gist:80cce78894edcbc04b8b
Created February 24, 2016 08:20
Lincastor - Two Browsers
(*
the 'args' dictionary contains following variables:
URL => my-http://myhost.domain.com:8080/mysite/a.html?search=blah#myanchor
URL_SCHEME => my-http
URL_HOST => myhost.domain.com
URL_PORT => 8080
URL_PATH => /mysite/a.html
URL_QUERY => ?search=blah
URL_FRAGMENT => #myanchor
URL_VALUE => everything that comes after the 'scheme:'
@cjthompson
cjthompson / strace55.txt
Created February 10, 2016 21:31
Node 5.5.0 with /etc/nsswitch.conf permissions set to 600
execve("./node", ["./node"], [/* 32 vars */]) = 0
brk(0) = 0x288c000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd8ca824000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26694, ...}) = 0
mmap(NULL, 26694, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd8ca81d000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
@cjthompson
cjthompson / strace.txt
Created February 10, 2016 21:28
Node 5.6.0 with /etc/nsswitch.conf permissions set to 644
execve("./node56", ["./node56"], [/* 17 vars */]) = 0
brk(0) = 0x340d000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64b6c1b000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=30969, ...}) = 0
mmap(NULL, 30969, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f64b6c13000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
@cjthompson
cjthompson / node56-nsswitch.conf
Created February 10, 2016 21:05
Node 5.6.0 crash with nsswitch.conf permissions set to 600
# /etc/nsswitch.conf is set to 600
strace ./node
execve("./node", ["./node"], [/* 32 vars */]) = 0
brk(0) = 0x2f42000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f01cb5de000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26694, ...}) = 0
@cjthompson
cjthompson / randomString.js
Created June 16, 2015 18:35
JavaScript: random string
var _ = require('lodash');
var randomString = function (length) {
return _(length).range().map(_.partial(_.random, 33, 126, false)).map(_.ary(String.fromCharCode)).join('');
};
@cjthompson
cjthompson / readableNumber.js
Created February 21, 2014 18:29
Convert a number into a human readable number with 3 significant digits followed by 'K' (thousand), 'M' (million), or 'B' (billion)
function readableNumber(num) {
var s = ['', 'K', 'M', 'B'];
var e = Math.floor(Math.log(num) / Math.log(1000));
return (num / Math.pow(1000, e)).toPrecision(3) + s[e];
}
@cjthompson
cjthompson / RobustPDO.php
Created February 3, 2014 19:52
Extended PDO class that detects dropped connections and reconnects
<?php
class RobustPDO extends PDO
{
/** Call setAttribute to set the session wait_timeout value */
const ATTR_MYSQL_TIMEOUT = 100;
/** @var array */
protected $config = [];
/** @var bool For lazy connection tracking */
@cjthompson
cjthompson / arrayDifference.php
Last active April 7, 2024 00:30
PHP: Compare two arrays and find the differences between them. Supports items that are arrays by serializing them for comparison. Returns an array of insertions (only in the second array) and deletions (only in the first array).
<?php
/**
* Compare two arrays and return a list of items only in array1 (deletions) and only in array2 (insertions)
*
* @param array $array1 The 'original' array, for comparison. Items that exist here only are considered to be deleted (deletions).
* @param array $array2 The 'new' array. Items that exist here only are considered to be new items (insertions).
* @param array $keysToCompare A list of array key names that should be used for comparison of arrays (ignore all other keys)
* @return array[] array with keys 'insertions' and 'deletions'
*/
public static function arrayDifference(array $array1, array $array2, array $keysToCompare = null) {
#!/bin/bash
USER="user"
PASSWORD="password"
HOST="localhost"
MYSQL_OPTS="--compact --single-transaction --skip-opt --quick --no-create-info --skip-triggers"
if [ "$1" == "" ]; then
echo "Usage: $0 database-name [output-dir]"
exit 1
fi