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

@aerofer/utils

v0.4.4

Published

Functional utilities for the entire Aerofer Application stack

Downloads

42

Readme

Cloud Utilities

A collection of helpful Node.JS utility functions which are used across Lambda (and other) serverless functions.

Secrets

Wraps function execution with encrypted secrets from AWS SSM Parameter Store using a specific key, provided that the executing IAM Role has sufficient privileges to do so.

import { withParameters } from '@aerofer/utils';

const SECRET_KEY = '/path/to/ssm/encrypted/secret';

const ALIAS = 'SUPER_SECRET_KEY';

const myFn = async ({ [ALIAS]: key }, a, b) => {
  return thirdPartyApi(key).someOperation(a, b);
};

export default withParameters({ [ALIAS]: SECRET_KEY })(myFn);

Elsewhere in the application, this function can be consumed as:

import myFn from './some-file';

const handler = async (a, b, ...other) => {
  const { result } = await myFn(a, b);
  return result;
};

Tracing

Wraps modules & functions, providing AWS XRay tracing for trace execution. You may also provide a recognizable name for the module to be traced, in addition to custom metadata, annotations and captured functions as subsegments.

Note, functions wrapped with traceAsyncFunc or traceFunc should have a withTracing higher in their call stack; otherwise XRay will throw an error, as no namespace, segment or context has been configured.

Note: In-order to dramatically reduce the bundled size (-350kb) of the AWS XRay library, debugging/logging has been disabled.

Async module tracing with
import { traceAsyncFunc } from '@aerofer/utils/tools/tracing';
import someRemoteOperation from 'some-other-module';

const OPERATION = 'Some Remote Operation';

const metadata = {
  version: '0.0.1',
};

// capture arguments and add as annotations
const capture = (a, b) => ({ a, b });

const handler = async (a, b) => {
  const { body } = await someRemoteOperation(a, b);
  return body;
}

export default traceAsyncFunc(OPERATION, { metadata, capture })(handler);
Runtime sync tracing with annotations
import { traceFunc } from '@aerofer/utils/tools/tracing';
import captureSync from 'some-other-module';

const CAPTURE = 'Capture';

const handler = (customerId, amount) => {
  const annotations = { customerId };
  const capture = traceFunc(CAPTURE, { annotations })(captureSync);
  return capture(customerId, amount);
};

export default handler;

Errors

Provides a helpful utility for optionally wrapping low-level, native error objects from libraries with application errors which can be:

  • Serialized and surfaced to consumer applications.
  • Filtered inside logs using JSONPath to provide custom application error metrics.
import { withProperties } from '@aerofer/utils/tools/errors';

export const FOOBAR_ERROR = 'FoobarError';

const errors = {
  ...(withProperties(FOOBAR_ERROR, { statusCode: 400, message: 'A foobar error occurred' })),
};

export default errors;

When you need to throw an error of this type inside your application, you can instantiate it like so:

import Errors, { FOOBAR_ERROR } from './errors';

const { [FOOBAR_ERROR]: LogicalError } = Errors;

const fn = (cb) => {
  someOperation((err, data) => {
    if (err) {
      throw new LogicalError('overrides default message', err);
    } else {
      cb(data);
    }
  })
};

Logger

Simple logger for serializing objects and application errors into JSON lines for inspection and JSONPath filtering.

import Logger from '@aerofer/utils/tools/logger';

const TIME_TAKEN = 'ExecutionTimeTaken';

const reporter = (time) => ({ time, type: TIME_TAKEN });

const handler = async () => {
  const start = +new Date();
  const data = await processor();
  const end = +new Date();
  Logger.info(reporter(end - start));
  return data;
};

There are two environment variables you can set, in-order to control the logger:

  • LOG_NAME - The name of the application generating logs.
  • LOG_LEVEL - The level of logging to produce output for.

Responders

HTTP Responders wrap response objects from Lambda Proxy functions (or any other mechanism), enabling the easy serialization of Javascript objects whether it is a success or failure.

import { success, failure } from '@aerofer/utils/tools/responders';

// Object containing security HTTP headers & CORS
import headers from './headers';

export const handler = async (event, context, cb) => {
  try {
    const result = await processor(event);
    const response = success({ code: 201, headers, body: result });
    cb(null, response);
  } catch (err) {
    if (err.statusCode) {
      // application error
      cb(null, failure(event, err));
    } else {
      // native error
      cb(err);
    }
  }
};