Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am mdlavin on github.
  • I am mdlavin (https://keybase.io/mdlavin) on keybase.
  • I have a public key whose fingerprint is CBA7 40DB B828 D263 1049 22DF 08A6 8D0C 1C04 C39A

To claim this, I am signing this object:

@mdlavin
mdlavin / request-agent-pool-leak-test.js
Created May 20, 2016 15:43
A testcase that demonstrates callback function leaks from request.js library
var request = require('./index');
const http = require('http');
const Promise = require('bluebird');
const v8 = require('v8');
const memwatch = require('memwatch-next');
const _ = require('lodash');
var triggerResponseSend;
const readyToRespond = new Promise( (resolve) => {
triggerResponseSend = resolve;
@mdlavin
mdlavin / zlib-buffer-closure-leak.js
Created May 24, 2016 16:55
An example application to demonstrate a small buffer/closure leak from zlib
const memwatch = require('memwatch-next');
const _ = require('lodash');
const zlib = require('./lib/zlib');
function writeLeakDiff(diff) {
const closures = _.filter(diff.change.details, {what: 'Closure'});
console.log('Closure changes', closures);
const buffers = _.filter(diff.change.details, {what: 'Uint8Array'});
console.log('Uint8Array changes', buffers);
@mdlavin
mdlavin / build-ssh-hosts-from-aws-subnets.sh
Last active October 24, 2017 18:41
Build SSH config Hosts line from AWS subnet query
#!/bin/bash
# If your AWS account has a lot of subnets and you want to have an SSH config entry for all of the subnets, then
# building the Hosts line by hand can be quite cumbersome. This script will automatically fetch the subnets
# from AWS for your account and build a Hosts line with wildcards to cover all of the IP ranges
ALL=""
for CIDR in `aws ec2 describe-subnets --query Subnets[].CidrBlock --output text`; do
#echo $CIDR
HOST_MIN=$(ipcalc $CIDR | ggrep -Po '(?<=HostMin:)[ ]*([^ ]*)' | sed 's/ //g')
@mdlavin
mdlavin / aws-xray-initialize.js
Created June 10, 2018 22:41
Node.js AWS X-Ray instrumentation
const xray = require('aws-xray-sdk-core');
// Allow X-Ray to track execution through API API calls
xray.captureAWS(require('aws-sdk'));
// Allow X-Ray to track execution through external API calls
xray.captureHTTPsGlobal(require('http'));
// Allow X-Ray to track execution when Promises are used
xray.capturePromise();
@mdlavin
mdlavin / null-payload-article-return-null.js
Created June 15, 2018 16:46
A simple lambda that returns a null payload
exports.returnNull = function (event, context, callback) {
callback(null, null);
}
@mdlavin
mdlavin / null-payload-article-lambda-invoke-example.js
Created June 15, 2018 17:03
Example code that was invoking the lambda
const result = await lambda.invoke(request).promise();
const payload = JSON.parse(result.Payload);
const body = payload.body;
@mdlavin
mdlavin / null-payload-article-serverless-pseudocode.js
Created June 15, 2018 17:45
Pseudocode for serverless-http request handling
export.handler = function (evt, ctx, callback) {
Promise.resolve()
.then(() => {
const koaRequest = convertToKoaRequest()
return myAppResponseHandler(koaRequest);
})
.then(koaResponse => {
callback(null, {
statusCode: koaResponse.statusCode
headers: getHeaders(koaResponse)
@mdlavin
mdlavin / null-payload-article-patched-serverless-pseudocode.js
Last active June 15, 2018 18:08
A patched version of serverless-http to log falsy errors
export.handler = function (evt, ctx, callback) {
Promise.resolve()
.then(() => {
// Unmodified handler execution
})
.then(koaResponse => {
// Unmodified success handling
})
.catch(e => {
if (!e) {
@mdlavin
mdlavin / null-payload-article-finish-pseudocode.js
Created June 15, 2018 19:25
Pseudocode for the AWS Lambda InvokeManager.finish function
class InvokeManager {
finish(err, data, waitToFinish) {
if (this._result === undefined) {
if (err == null) {
this._result = [null, JSON.stringify(isUndefined(data) ? null : data)];
} else {
let errType = this._faulted ? 'unhandled' : 'handled';
this._result = [errType, stringifyError(errType, err)];
}
}