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

stepfunctions

v0.1.0

Published

AWS Step Functions implementation in Node, so you can run your Node.js lambda handlers in your test environments. Made to support Serverless JS testing.

Downloads

76

Readme

stepfunctions

Stepfunctions codecov

AWS Step Functions implementation in Node, so you can run your Node.js lambda handlers in your test environments. Made to support Serverless JS testing.

Installation

npm i -D stepfunctions

or if you're using yarn like me:

yarn add -D stepfunctions

Motivation

I was working on getting step functions orchestrated using Serverless, Lambda, and Step functions and there was no way to run through the statemachine in Jest. So I made the spec, or parts of it, work in JS so that I can spy and mock the statemachine.

I am perfectly aware of the existence of step-functions-offline and local-stepfunctions, but none of those can be orchestrated natively in a testing context.

Usecase

If you are:

  • using Node, Lambda, AWS Step Functions
  • using Serverless
  • writing Integration tests with AWS Step Functions
  • trying to see how the statemachine runs before creating it in AWS

Usage

Include it in your test files, tested with Jest so far.

const Sfn = require('stepfunctions');

const sm = new Sfn({
  StateMachine: {
    StartAt: 'Test',
    States: {
      Test: {
        Type: 'Task',
        Resource: 'arn:aws:lambda:ap-southeast-1:123456789012:function:test',
        End: true,
      },
    },
  },
});

describe('StateMachine Test', () => {
  it('Check if a task was run', async () => {
    const mockfn = jest.fn((input) => input.test === 1);
    sm.bindTaskResource('Test', mockfn);
    await sm.startExecution({ test: 1 });
    expect(mockfn).toHaveBeenCalled();
  });
});

You can see more examples in /test/stepfunctions.test.js.

API

startExecution(Input, Options);

sm.startExecution(input, {
  respectTime: false,
  maxWaitTime: 30,
  maxConcurrency: 10,
});
  • respectTime - will ensure that the time used in Wait steps will be respected and not use the maximum wait time in the library. defaults to false.
  • maxWaitTime - the maximum amount of time a wait step can function. defaults to 30s.
  • maxConcurrency - allows the amount of parallel tasks to be ran concurrently. defaults to 10.

bindTaskResource(Task, Callback)

const sm = new Sfn({
  StateMachine: {
    StartAt: 'HelloWorld',
    States: {
      HelloWorld: {
        Type: 'Task',
        Resource: 'arn:aws:lambda:ap-southeast-1:123456789012:function:test',
        End: true,
      },
    },
  },
});
sm.bindTaskResource('HelloWorld', (input) => `hello ${input}`);
await sm.startExecution('world');
// will output `hello world`

Must be called before startExecution, binds to Tasks and replaces their handler to the provided Callback parameter.

getExecutionResult()

Must be called after startExecution. This function returns the absolute result from the statemachine if it has finished.

getReport()

Use console.table to list down the transitions that occured.

Task.abort()

abort is made available within the replaced Task handlers made with bindTaskResource. this allows you to abort a call from within a handler itself.

Support

The spec implemented in https://states-language.net/spec.html is fully supported by this library besides the ones below:

Experimental

  • Retry
  • Catch

The above features are labeled experimental because it cannot be fully spec compliant(yet) due to AWS specific cases.

Caveats

  1. Wait will wait for at most 30 seconds. This is because it's expected that this library will be used within a testing context. You can override this behaviour by adding the respectTime option to true in the startExecution method.
  2. No support for Handling States.Permissions as the library will not have context on AWS related permissions.

Future

PR's are welcome to help finish the ones below :)

  • [ ] Change arn in bindTaskResource instead of the State name
  • [ ] Run sls invoke local instead of binding resolvers
  • [ ] Typescript typings
  • [ ] Run via CLI
  • [ ] Remove the "experimental" label on retry and catch
  • [ ] More accurate timing mechanism
  • [ ] use jest.fakeTimers() in the test
  • [ ] Walk through states ala "generator" style. e.g, yield sm.next()

License

stepfunctions is MIT Licensed