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

@karmaniverous/serverless-lodash-plugin

v0.1.12

Published

Exposes the full Lodash library for use inside serverless.yml.

Downloads

9

Readme

serverless-lodash-plugin

This module was born out of sheer frustration: while building a Serverless Framework project, I just wanted a convenient way to set provisionedConcurrency conditionally to 1 or 0.

Easy, right?

Unfortunately...

  • I needed to deliver that value as an environment variable.
  • Environment variables are ONLY delivered into serverless.yml as strings.
  • provisionedConcurrency HAS to be a number.

Bollocks. And while serverless.yml DOES support a few inline functions (like strToBool), it won't parse an integer from a string.

Double bollocks. You can see other devs crying about the same problem here and here.

Hence this plugin, which exposes the entire lodash library (plus some other goodies) as variables in serverless.yml.

I mean why use a fly swatter when you can use a bazooka, right? 🤓

P.S. FWIW strToBool doesn't seem to work very well. 🙄 Use boolean as described below instead!

Why is this even an issue?

If you think about it, the Serverless Framework applies two independent processing layers to serverless.yml:

  1. It parses the file into JSON, applies Serverless-specific variable expansion, and validates the result against its config schema.

  2. It passes the result to provider-specific packaging logic (like AWS CloudFormation), which does its own provider-specific thing.

So if you try to use an AWS !If function to deal with the provisionedConcurrency-as-string issue, you're going to fail, because the environment variable containing the number will parse as a string and fail the first schema-validation step BEFORE your !If function gets a chance at it!

This plugin works because its functions run during the initial variable-expansion phase, BEFORE your config is validated against the Serverless schema. So as long as your lodash functions resolve to a valid serverless.yml config, you're good to go!

Sign me up!

Thought so. 🤣 Install the plugin with:

npm i -D @karmaniverous/serverless-lodash-plugin

In general, use it like this in your serverless.yml file:

plugins:
  - '@karmaniverous/serverless-lodash-plugin'

someKey: ${lodash(<param1>, <param2>, ...):<functionName>}
# Equivalent to _.<functionName>(<param1>, <param2>, ...)

<functionName> can be:

  • boolean — convert lots of things to a boolean
  • ifelse — ternary function with params (condition, trueValue, falseValue)
  • params — converts params to an array
  • Any lodash function.

<param1>, <param2>, etc. can be just about anything, including a reference to a lodash function like _.parseInt. Raise an issue if you figure out how to break it!

Your output must be a primitive value, not an object or an array, or Serverless will throw an error. This is probably a Serverless limitation, but if you have the use case raise an issue and I'll see what I can do.

Any undefined output will be converted to NULL to avoid breaking Serverless.

Yes, putting the function name after the params is a little weird. But Serverless parses the stuff in the parentheses as an array, so internally it makes sense. And all the usual rules apply with respect to Serverless variable parsing.

Some examples

plugins:
  - '@karmaniverous/serverless-lodash-plugin'

iWantANumber: ${lodash(1, 2, 3):sum} # 6
# Equivalent to _.sum([1, 2, 3])

# Assuming env var THREE = '3'
meToo: ${lodash(1, 2, ${lodash(${env:THREE}):parseInt}):sum} # 6
# Equivalent to _.sum([1, 2, _.parseInt(process.env.THREE)])

# The 'params' function converts the params into an array.
# You can pass a lodash function reference like '_.multiply' as a param.
# Each expression & sub-expression has to returns a VALUE, not a FUNCTION.
iWantAnArray: ${lodash(${lodash(1, 2, 3):params}, _.multiply):map} # [0, 2, 6]
# Equivalent to _.map([1, 2, 3], _.multiply)

# AT LAST!
# boolean converts lots of things to a boolean.
# ifelse is a ternary function.
provisionedConcurrency: ${lodash(${lodash(${env:USE_PROVISIONED_CONCURRENCY}):boolean}, 1, 0):ifelse, 0}
# Equivalent to boolean(process.env.USE_PROVISIONED_CONCURRENCY) ? 1 : 0
# SEE THE WEIRDNESS ALERT BELOW!

Weirdness Alert!

Serverless—NOT this plugin!—reads the 0 in ${lodash(${env:USE_PROVISIONED_CONCURRENCY}):boolean}, 1, 0) as a NULL rather than a 0.

I know. 🙄

To prove it to yourself:

  1. Clone this repo

  2. Run npx mocha -g "lodash pc off weirdness"

  3. Watch the console.

DEBUG serverless-lodash-plugin {
  address: 'ifelse',
  params: [
    false,
    1,
    null <-- COMING STRAIGHT FROM SERVERLESS & SHOULD BE 0!
  ],
  value: null
}

If you were paying atention above, the provisionedConcurrency setting SHOULD look like:

provisionedConcurrency: ${lodash(${lodash(${env:USE_PROVISIONED_CONCURRENCY}):boolean}, 1, 0):ifelse}

But when we do that and USE_PROVISIONED_CONCURRENCY is false, Serverless reads the 0 as a NULL and the ifelse function returns NULL, which is an invalid provisionedConcurrency value, and serverless package throws an error.

So instead we are using:

provisionedConcurrency: ${lodash(${lodash(${env:USE_PROVISIONED_CONCURRENCY}):boolean}, 1, 0):ifelse, 0}

... which coalesces the NULL returned by ifelse function back to a 0, and everything works.

BUT... we could also just embrace the weirdness and declaratively pass the NULL by leaving off the , 0, like this:

provisionedConcurrency: ${lodash(${lodash(${env:USE_PROVISIONED_CONCURRENCY}):boolean}, 1):ifelse, 0}

That's defensive enough (it'll still work when Serverless fixes the issue) and just a tad more readable. It's the version I use in my projects.

If you run into odd failures of your lodash logic, try running serverless package with LOG_LEVEL=debug set in your environment. It'll show you the same lodash function logging as pictured above and help you understand what's happening.

If you learn anything new, please raise an issue and I'll document it here!


See more great templates and other tools on my GitHub Profile!