Skip to content

Instantly share code, notes, and snippets.

Avatar

Geoff Cox redgeoff

View GitHub Profile
@redgeoff
redgeoff / lambda.js
Last active Aug 6, 2021
lambdaToExpress - lambda.js
View lambda.js
import leadService from './lead-service';
// Define a handler for `PUT /lead/:leadId` that accepts the request body payload
// `{ name, email }`
const handler = async (event) => {
// Parse the request parameters
const { name, email } = JSON.parse(event.body);
const { leadId } = event.pathParameters;
// Assume this is the service-layer code that actually performs the updating of
@redgeoff
redgeoff / lamdbaToExpress.js
Last active Aug 5, 2021
lamdbaToExpress - lamdbaToExpress.js
View lamdbaToExpress.js
// Convert from a Lambda to an ExpressJS handler
const lambdaToExpress = async (handler) => {
return async (req, res) => {
// Repackage the express parameters as an event object for the Lambda
const event = {
body: JSON.stringify(req.body),
headers: req.headers,
queryStringParameters: req.query,
pathParameters: req.params
};
@redgeoff
redgeoff / express.js
Created Aug 5, 2021
lambdaToExpress - express.js
View express.js
import express from 'express';
import cors from 'cors';
import handler from './lambda';
import lambdaToExpress from './lambda-to-express';
const PORT = 3000;
const app = express();
app.use(cors());
app.use(express.json()); // Parse JSON bodies
@redgeoff
redgeoff / express.js
Last active Jul 27, 2021
Basic Lambda to serve POST request
View express.js
import express from 'express';
import cors from 'cors';
import handler from './lambda';
import lambdaToExpress from './lambda-to-express';
const PORT = 3000;
const app = express();
app.use(cors());
app.use(express.json()); // Parse JSON bodies
@redgeoff
redgeoff / first-key.js
Last active Jan 24, 2021
Get first key speed test
View first-key.js
import each from 'lodash/each';
const getFirstKeyUsingEach = (obj) => {
let firstKey = undefined;
each(obj, (value, key) => {
firstKey = key;
return false; // Exit loop
});
return firstKey;
};
@redgeoff
redgeoff / each-deep.js
Created Dec 31, 2020
eachDeep - Recursively traverse object
View each-deep.js
import each from 'lodash/each';
const eachDeepInner = (collection, iteratee, key) => {
if (key !== undefined) {
iteratee(collection, key)
}
if (collection && typeof collection === 'object') {
each(collection, (value, key) => {
eachDeepInner(value, iteratee, key);
@redgeoff
redgeoff / each-leaf.js
Last active Dec 31, 2020
eachLeaf - Recursively process each leaf in JavaScript object
View each-leaf.js
import each from 'lodash/each';
const eachLeafInner = (collection, iteratee, key) => {
if (collection && typeof collection === 'object') {
each(collection, (value, key) => {
eachLeafInner(value, iteratee, key);
})
} else if (key !== undefined) {
iteratee(collection, key)
}
@redgeoff
redgeoff / new-function-no-local-scope.js
Last active Dec 29, 2020
JavaScript New Function Only Has Access to Global Scope
View new-function-no-local-scope.js
// The following code proves that functions created in `new Function()` cannot access local scope
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function for more details
let userDefinedFunction = function () {
const foo = 'bar';
const fn = () => { console.log(foo) };
fn(); // This succeeds and prints 'bar'
return new Function('props', `const f = ${fn.toString()}; return f(props);`)
@redgeoff
redgeoff / generate-password-hash.js
Last active Dec 15, 2021
Encode/decode a password in JS in a way that is compatible with Python's werkzeug password hashing
View generate-password-hash.js
// Encode/decode a password in JS in a way that is compatible with Python's werkzeug password hashing
const crypto = require('crypto');
const util = require('util')
// Hashed value of clearTextPassword='secret'
const EXAMPLE_PYTHON_PASSWORD = 'pbkdf2:sha256:50000$GPJFgPGqrbYqxiM1DITq919diw3sRb5k$970485eef0aa61bf39dba3468b913ada19937eec00fec74b3077a7aec905f9c9'
const pbkdf2 = util.promisify(crypto.pbkdf2)
View open-api.yml
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing