Skip to content

Instantly share code, notes, and snippets.

@mdlavin
mdlavin / null-payload-article-beforeExit-listener-line.js
Created June 29, 2018 21:07
The beforeExit listener registered by the AWS Lambda runtime
process.on('beforeExit', () => invokeManager.finish(null, null, false));
@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)];
}
}
@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-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-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-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 / lambda-function-xray-enablement.tf
Last active March 18, 2024 14:38
Terraform configuration to enable X-Ray for a Lambda function
resource "aws_lambda_function" "service" {
# Your usual aws_lambda_function configuration settings here
tracing_config {
mode = "Active"
}
}
@mdlavin
mdlavin / enable-graphql-resolvers-xray-tracing.js
Last active October 2, 2019 11:32
Enabling graphql-resolvers-xray-tracing integration
const traceResolvers = require('@lifeomic/graphql-resolvers-xray-tracing');
const schema = makeExecutableSchema( ... );
traceResolvers(schema);
@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 / 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);