Socket
Book a DemoInstallSign in
Socket

@lamware/memoize

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lamware/memoize

Lamware Middleware for memoizing outside the Lambda handler

latest
Source
npmnpm
Version
3.0.1
Version published
Maintainers
1
Created
Source
NPM Discord Apache-2.0

Lamware - Variable Memoize

This Lamware Middleware allows you to memoize variables and instances outside of the Lambda Handler with ease.

Why?

A lesser known feature of Lambda is that variables declared outside of the handler persist through the handler lifecycle. Since a Lambda instance, once cold-started, stays alive for 5-15 minutes (varies based on activity and the type of Lambda function), this provides a clear performance gain over having all logic instantiated inside the handler itself, since it'll only execute once (on the first request/invocation).

Currently, Lambda doesn't support top-level async/await, meaning set-up that involves non-synchronous operations isn't as simple as placing it outside the handler. By allowing you to supply a closure, and Lamware having support for asynchronous initialization, this middleware allows you to asynchronously memoize/load outside of the handler.

Installation

This package is available via NPM:

yarn add @lamware/memoize

# or

npm install @lamware/memoize

Usage

import type { APIGatewayProxyHandlerV2 } from 'aws-lambda';
import { memoize } from '@lamware/memoize';
import { lamware } from '@lamware/core';

interface MemoizePayload {
  count: number;
}

const { handler } = lamware<APIGatewayProxyHandlerV2<any>>()
  .use(memoize<MemoizePayload>(async () => {
    return { count: 1 };
  }, {
    // [optional] Whether to throw an `Error` if the memoize closure fails [default: true]
    throwOnError: false,
  }))
  .execute(async ({ state }) => {
    return {
      statusCode: 200,
      message: 'count should always be `1` since it is memoized!',
      count: state.count,
    };
  });

export { handler };

Keywords

lambda

FAQs

Package last updated on 21 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts