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

@saws/function

v1.0.6

Published

<div align='center'>

Downloads

15

Readme

Function

Services and library for working with serverless functions in your application.

Table of Contents

Installation

From the command line run:

npm install @saws/function

Then add one of the included service (TypescriptFunctionService or ContainerFunctionService) to your saws.js file.

Development

In development function services enable you to develop serverless AWS Lambda functions. There are two flavors, a typescript function, and a container function.

Typescript functions will have an index.ts entrypoint and will be bundled into a javascript bundle. This is then run in the local Lambda server. Anytime a file is changed in the service's directory, it will re bundle the entrypoint and re-register it with the local Lambda server.

For Container functions, it will look for a Dockerfile in the service's directory and build it. These images should be based on one of the (AWS base images for Lambda)[https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#runtimes-images-lp]. It will then register it with the local Lambda server. Any time a file changes within the service's directory it will rebuild the docker image and re-register it with the local Lambda server.

Deployment

When you deploy a function service, it will do one of two things, depending on the type of function.

For typescript functions it will stand up and configure the following for you:

  • S3 bucket to host your function code in
  • Apply any layers you've configured for your function
  • Create an IAM Role for your function to attach other service permissions to
  • Create a Lambda function
  • (For scheduled lambdas) create an event rule with your cron schedule and an IAM permission for events to trigger your function.

For container functions it will stand up and configure the following for you:

  • An ECR repository to hold your Docker image
  • An IAM role for your function to attach other service permissions to
  • Create a Lambda function

Services

@saws/function includes 2 services, TypescriptFunctionService and ContainerFunctionService

TypescriptFunctionService

You can require the TypescriptFunctionService and use it in your saws.js file like so:

const { TypescriptFunctionService } = require('@saws/function/typescript-function-service')

// can be used as a dependency to another service to allow that service to invoke this function
// or exported as a service
const func = new TypescriptFunctionService({
  name: 'my-func',
})

The TypescriptFunctionService constructor accepts the following options:

name: string

The name of your service. This should be unique across all of your services.

dependencies: ServiceDefinition[]

An array of all of the other services this service depends on. This will ensure that permissions, environment variables, and execution order are all set up.

triggers?: { cron?: string }

Possible triggers for your function. Currently only cron schedules are supported.

externalPackages?: string[]

Allows you to provide a list of modules or files to not include in your code bundle.

include: string[]

A list of additional files you would like to include in your ZIP archive uploaded to S3 and used by Lambda. This is useful for files that you might need to read from disk. The file path in the zip archive will match the path from the root of your API service.

For example, given the following directory structure:

  • my-rest-api
    • index.ts
    • other-folder
      • include-this-file.txt
      • other-module.ts

The ZIP archive structure will look like:

  • my-rest-api
    • index.js
    • other-folder
      • include-this-file.txt

So make sure your path handling logic will still work when your code is bundled.

Your function entrypoint should be named index.ts inside of your service's directory. It should export a handler function that will be called whenever your function is invoked.

ContainerFunctionService

⚠️ This is one of the lesser used services in SAWS and thus may not be complete. ⚠️

You can require the ContainerFunctionService and use it in your saws.js file like so:

const { ContainerFunctionService } = require('@saws/function/container-function-service')

// can be used as a dependency to another service to allow that service to invoke this function
// or exported as a service
const func = new ContainerFunctionService({
  name: 'my-func',
})

The ContainerFunctionService constructor accepts the following options:

name: string

The name of your service. This should be unique across all of your services.

dependencies: ServiceDefinition[]

An array of all of the other services this service depends on. This will ensure that permissions, environment variables, and execution order are all set up.

port: number

The port in your container to expose.

When used as a dependency

When a function service is used as a dependency to other services there are no environment variables injected into those other services.

Libraries

@saws/function includes a library to make calling your functions from other services easier.

FunctionsClient

The FunctionsClient class contains methods for invoking other function services.

Example usage:

import { FunctionsClient } from '@saws/function/functions-client'

const client = new FunctionsClient()

In order for the FunctionsClient class to work in a service, that service must list your function service as a dependency.

async call<T extends any>(name: string, payload: any, config: { async: boolean } = { async: false }): Promise<T>

Calls a function in your application. The name parameter should be the name of your function service. The payload will be passed as the event to your function. And the optional config options allows you to tell the client to either wait for the response or not.