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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lambda-deadline-middleware

v1.1.0

Published

AWS SDK v3 middleware for automatic Lambda deadline propagation via AbortController-based timeouts

Readme

lambda-deadline-middleware

Zero-dependency AWS SDK v3 middleware that automatically propagates Lambda execution deadlines to outgoing SDK calls via AbortController-based timeouts.

When an AWS SDK call hangs inside a Lambda function, the runtime terminates the process at the configured timeout without throwing an error or giving your code a chance to react. This library prevents that by computing per-request deadlines from the Lambda's remaining execution time and aborting requests before the hard timeout fires.

Features

  • Automatic deadline propagation, no manual timeout configuration per call
  • Fresh deadline per retry: each SDK retry attempt uses current remaining time
  • Signal composition: preserves caller-provided AbortSignal via AbortSignal.any()
  • Zero runtime dependencies (@smithy/types is compile-time only)
  • Complete no-op when no Lambda context is available
  • Branded types prevent millisecond/buffer interchange at compile time

Requirements

  • Node.js ≥ 24
  • AWS SDK v3 (built against @smithy/types ≥ 3.0.0)

Installation

pnpm add lambda-deadline-middleware

Usage

Setup requires two pieces:

  1. Wrap your handler with withLambdaDeadline. This stores the Lambda context (specifically getRemainingTimeInMillis()) in AsyncLocalStorage so the SDK middleware can read it. The SDK middleware stack has no access to the Lambda context on its own.

  2. Register the middleware on each SDK client via the standard middlewareStack.use() pattern.

import { withLambdaDeadline, deadlineMiddleware } from "lambda-deadline-middleware";
import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";

const dynamodb = new DynamoDBClient({});
dynamodb.middlewareStack.use(deadlineMiddleware());

export const handler = withLambdaDeadline(async (event, context) => {
  const result = await dynamodb.send(
    new GetItemCommand({
      /* ... */
    }),
  );
  return { statusCode: 200, body: JSON.stringify(result) };
});

Every SDK call through dynamodb now receives a timeout derived from the Lambda's remaining execution time minus a configurable flush buffer (default: 1000ms).

How It Works

flowchart LR
    subgraph Lambda Invocation
        direction LR
        A[Lambda Runtime] --> B[withLambdaDeadline]
        B --> C[Your Handler]
        C --> D[SDK .send]
    end

    subgraph Per Attempt
        direction LR
        D --> E[Deadline Middleware]
        E -->|getRemainingTimeInMillis\nminus flush buffer| F[AbortController\n+ setTimeout]
        F --> G[HTTP Request]
    end

    style E fill:#f9a825,stroke:#f57f17
    style B fill:#66bb6a,stroke:#2e7d32

withLambdaDeadline stores the Lambda context in AsyncLocalStorage. The deadline middleware reads it on every attempt (including retries), computes a fresh timeout, and attaches an AbortSignal to the outgoing HTTP request.

Configuration

Flush Buffer

The flush buffer is subtracted from the remaining Lambda time to leave room for graceful shutdown and error handling:

// Default: 1000ms
dynamodb.middlewareStack.use(deadlineMiddleware());

// Custom: 500ms
dynamodb.middlewareStack.use(deadlineMiddleware({ flushBufferMs: 500 }));

Error Handling

When remaining time is less than or equal to the flush buffer, the middleware throws DeadlineExceededError immediately without dispatching an HTTP request.

import { isDeadlineExceeded } from "lambda-deadline-middleware";

try {
  await dynamodb.send(
    new GetItemCommand({
      /* ... */
    }),
  );
} catch (error) {
  if (isDeadlineExceeded(error)) {
    console.log(`Deadline exceeded: ${error.deadlineMs}ms`);
    console.log(`Remaining time was: ${error.remainingMs}ms`);
  }
  throw error;
}

Signal Composition

If you pass an AbortSignal to a request, the middleware composes both signals:

const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);

await dynamodb.send(
  new GetItemCommand({
    /* ... */
  }),
  {
    abortSignal: controller.signal,
  },
);

API Reference

withLambdaDeadline(handler)

Wraps a Lambda handler to store the Lambda context in AsyncLocalStorage. Required for the middleware to access getRemainingTimeInMillis().

function withLambdaDeadline<TEvent, TResult>(
  handler: (event: TEvent, context: LambdaContextLike) => Promise<TResult>,
): (event: TEvent, context: LambdaContextLike) => Promise<TResult>;

deadlineMiddleware(options?)

Returns a Pluggable for client.middlewareStack.use().

function deadlineMiddleware(options?: DeadlineOptions): Pluggable<object, object>;

getRemainingTimeInMillis()

Accessor for the current Lambda's remaining execution time. Returns undefined outside a Lambda context.

function getRemainingTimeInMillis(): number | undefined;

isDeadlineExceeded(error)

Type guard for deadline-triggered abort errors.

function isDeadlineExceeded(error: unknown): error is DeadlineExceededError;

DeadlineExceededError

class DeadlineExceededError extends Error {
  readonly name: "DeadlineExceededError";
  readonly deadlineMs: Milliseconds;
  readonly flushBufferMs: Milliseconds;
  readonly remainingMs: Milliseconds;
}

DeadlineOptions

interface DeadlineOptions {
  readonly flushBufferMs?: number; // Default: 1000
}

Types

| Type | Description | | ------------------- | ------------------------------------------------------------ | | Milliseconds | Branded number representing a duration in ms | | LambdaContextLike | Minimal interface: { getRemainingTimeInMillis?(): number } |

Reporting Bugs

Found a bug? Please open a GitHub Issue with:

  • Your Node.js version and AWS SDK version
  • A minimal code snippet reproducing the problem
  • Expected vs actual behavior

For security vulnerabilities, see SECURITY.md instead.

License

MIT