Skip to content

Instantly share code, notes, and snippets.

View pmuellr's full-sized avatar
🌀
Software developer @ Elastic working on Kibana. Previously @ NodeSource, IBM.

Patrick Mueller pmuellr

🌀
Software developer @ Elastic working on Kibana. Previously @ NodeSource, IBM.
View GitHub Profile
@pmuellr
pmuellr / package_info2dot.js
Last active May 6, 2016 12:00
Generate a diagram of package deps for running N|Solid instances, in graphviz dot notation, using the N|Solid agent `package_info` command. For more info on N|Solid, head over to https://nodesource.com/products/nsolid
#!/usr/bin/env node
// Generate a graphviz (graphviz.org) .dot file for the package relationships
// of the currently running N|Solid instances, using the N|Solid 1.3
// package_info command.
'use strict'
var fs = require('fs')
var path = require('path')
@pmuellr
pmuellr / 500-server.js
Created April 28, 2016 20:22
http server that always returns 500 status
'use strict'
const http = require('http')
const server = http.createServer( (req, res) => {
res.writeHead(500, {'Content-Type': 'text/plain'})
res.end('500')
})
const PORT = process.env.PORT || "3000"
@pmuellr
pmuellr / microseconds-stuff.js
Created April 21, 2016 03:13
how many microseconds fit in a safe JS integer?
'use strict'
const max = Number.MAX_SAFE_INTEGER // 9007199254740991
console.log('Number.MAX_SAFE_INTEGER: ', nice(max))
console.log('')
// Number.MAX_SAFE_INTEGER: 9,007,199,254,740,991
console.log('how many time units fit in', nice(max), 'microseconds')
@pmuellr
pmuellr / cpu-percent.js
Created April 12, 2016 14:59
get CPU usage percent for a process in node, using proposed `process.cpuUsage()` function
'use strict'
// see: https://github.com/nodejs/node/pull/6157
var startTime = process.hrtime()
var startUsage = process.cpuUsage()
// spin the CPU for 500 milliseconds
var now = Date.now()
while (Date.now() - now < 500)
@pmuellr
pmuellr / req-mapping.json
Created March 9, 2016 05:23
aws api gateway integration request mapping template for aws lambda
{
"method": "$context.httpMethod",
"resourcePath": "$context.resourcePath",
"querystring": {
#foreach($key in $input.params().querystring.keySet())
"$key": "$input.params().querystring.get($key)"#if($foreach.hasNext),#end
#end
},
"path": {
#foreach($key in $input.params().path.keySet())
@pmuellr
pmuellr / yield-callback.js
Last active February 26, 2016 13:24
Call functions that take async callbacks, in a synchronous style, in a generator, using yield.
'use strict'
module.exports = yieldCallback
// Call functions that use async callbacks, in an "synchronous" type style from
// within a generator.
if (require.main === module) test()
// example invoking a yielding callback generator
@pmuellr
pmuellr / add-code-launch.js
Created February 24, 2016 16:11
Handy little script to add a launch for a particular node script to the VS Code launch configuration, so you don't have to do it in Code itself. Should be run from the project's base directory.
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const LaunchFile = '.vscode/launch.json'
const LaunchDir = path.dirname(LaunchFile)
@pmuellr
pmuellr / read-file-promised.js
Last active February 18, 2016 03:44
read some bytes from a file with promises
'use strict'
const fs = require('fs')
// promise-style fs subset (at bottom of this file)
const fsp = requirePromised_fs()
// read 12 bytes of this file, print them, or print error if that happens
readFile(__filename, 12)
.then(s => console.log('read:', s.toString()))
@pmuellr
pmuellr / nsolid-install-dynamic.sh
Created February 11, 2016 04:51
like https://gist.github.com/pmuellr/3854a1a7f17159b432e6 , but gets the N|Solid release versions dynamically
#!/bin/bash
#-------------------------------------------------------------------------------
# download/unpack componentry for N|Solid into ~/nsolid
#-------------------------------------------------------------------------------
# directories
DIR_DOWN=~/Downloads
DIR_INST=~/nsolid
@pmuellr
pmuellr / stepper.js
Created February 5, 2016 07:16
a class to help deal with multiple async fn's
'use strict'
const fs = require('fs')
class Steps {
constructor (tx) {
this.tx = tx
this.steps = []
this.runningSeries = false
this.runningParallel = false