npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dcm-lambda-utils

v1.1.4

Published

Lambda utils by David CM

Downloads

7

Readme

AWS Lambda utils

Over the space of some time I've built quite a few lambdas for more or less complex applications. I've been extracting some of that into a package and will be using it on my own projects this way so I can share it with others.

Responses

There's two fundamental responses a Lambda will always send, things either went well or not. In the case they go well, you always want a 200 response. If it doesn't go as well then there's some different options.

Returning OK for an operation

const { ResponseUtil } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    return ResponseUtil.OK({message: 'This doesnt do much!'});
}

Returning an Error for an operation

const { ResponseUtil } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    return ResponseUtil.Error(401, 'I know this doesnt do much, but you dont even get that ;)');
}

Logging operations

In order to use the full benefits of structured logging, all that is required is to use the logger that comes bundled with the library.

By getting a logger, we will create an instance of WinstonJS and the returned object will be a standard Winston logger object. For more docs on how to get started with Winston you can use the official guide.

Using it is very simple:

const { BodyParser, getLogger } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    const parsedBody = BodyParser(event);
    const logger = getLogger();

    if (parsedBody === null) {
        logger.error("There was an attempt to parse the body of the function and it returned null");
        return ResponseUtil.Error(400, 'Could not parse the body of the request');
    }
    
    return ResponseUtil.OK({message: 'Parsed!', body: parsedBody});
}

Parsing requests

Parsing a request is pretty simple:

const { ResponseUtil, BodyParser } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    const parsedBody = BodyParser(event);

    if (parsedBody === null) {
        return ResponseUtil.Error(400, 'Could not parse the body of the request');
    }
    
    return ResponseUtil.OK({message: 'Parsed!', body: parsedBody});
}

Decoding JWT Tokens with Cognito

To check a jwt token issued by Cognito, you will need to know which Cognito pool your application is using and on which region is hosted. Once you have that you can check and decode the object very simply:

const { Security, ResponseUtil, BodyParser } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {

    //First make sure you can get the token
    const parsedBody = BodyParser(event);

    if (parsedBody === null) {
        return ResponseUtil.Error(400, 'Could not parse the body of the request');
    }

    try {
        let validatedUser = await Security.decodeJWTToken(parsedBody.token, process.env.cognitoPoolId, process.env.AWS_REGION);

        if (validatedUser === null) {
            return ResponseUtil.Error(400, 'Could not validate your token');
        }

        const userName = validatedUser.username;

        if (userName === null) 
            userName = validatedUser['cognito:username'];
        
        return validatedUser.username;

    } catch(error) {
        console.log(error.Message)
        return null;
    }
    
    return ResponseUtil.OK({message: 'Parsed!', body: parsedBody});
}