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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@alt-stack/workers-core

v0.6.1

Published

Framework-agnostic core for type-safe background workers with Zod validation

Readme

@alt-stack/workers-core

Framework-agnostic core for type-safe background workers with Zod validation. Follows a tRPC-style pattern for defining and organizing background jobs.

Installation

pnpm add @alt-stack/workers-core zod

Usage

import { init, ok, err } from "@alt-stack/workers-core";
import { z } from "zod";

// Define your context type
interface AppContext {
  db: Database;
  logger: Logger;
}

// Initialize with your context
const { router, procedure } = init<AppContext>();

// Define jobs
const emailRouter = router({
  // On-demand task with Result-based error handling
  "send-welcome-email": procedure
    .input({ payload: z.object({ userId: z.string(), email: z.string() }) })
    .errors({
      INVALID_EMAIL: z.object({ code: z.literal("INVALID_EMAIL"), message: z.string() }),
    })
    .task(async ({ input, ctx }) => {
      if (!isValidEmail(input.email)) {
        return err({ data: { code: "INVALID_EMAIL" as const, message: "Invalid email format" } });
      }
      ctx.logger.info(`Sending welcome email to ${input.email}`);
      await sendEmail(input.email, "Welcome!");
      return ok();
    }),

  // Scheduled cron job
  "daily-digest": procedure
    .cron("0 9 * * *", async ({ ctx }) => {
      ctx.logger.info("Running daily digest");
      await generateDailyDigest();
      return ok();
    }),

  // Queue-based job
  "process-upload": procedure
    .input({ payload: z.object({ fileId: z.string() }) })
    .queue("uploads", async ({ input, ctx }) => {
      await processFile(input.fileId);
      return ok();
    }),
});

export { emailRouter };

See @alt-stack/result for full Result type documentation.

Middleware

import { createMiddleware } from "@alt-stack/workers-core";

const middleware = createMiddleware<AppContext>();

const loggingMiddleware = middleware(async ({ ctx, next }) => {
  console.log(`Starting job: ${ctx.jobName}`);
  const start = Date.now();
  const result = await next();
  console.log(`Finished job: ${ctx.jobName} in ${Date.now() - start}ms`);
  return result;
});

// Use in procedure
const protectedProcedure = procedure.use(loggingMiddleware);

AsyncAPI Spec Generation

Generate an AsyncAPI specification from your router for SDK generation:

import { generateAsyncAPISpec } from "@alt-stack/workers-core";
import { writeFileSync } from "node:fs";

const spec = generateAsyncAPISpec(emailRouter, {
  title: "Workers API",
  version: "1.0.0",
});

writeFileSync("asyncapi.json", JSON.stringify(spec, null, 2));

Then generate a TypeScript SDK:

npx asyncapi-to-zod asyncapi.json -o ./sdk/index.ts

Use the generated SDK with worker clients:

import { Topics } from "./sdk";
import { createTriggerClient } from "@alt-stack/workers-client-trigger";

const client = createTriggerClient({ jobs: Topics });
await client.trigger("send-welcome-email", { userId: "123", email: "[email protected]" });

Provider Bindings

This is the core package. To actually run workers, you need a provider binding:

  • @alt-stack/workers-trigger - Trigger.dev integration
  • @alt-stack/workers-warpstream - WarpStream/Kafka integration

Client Packages

To trigger workers from generated SDKs (without importing router definitions):

  • @alt-stack/workers-client-trigger - Trigger.dev client
  • @alt-stack/workers-client-warpstream - WarpStream/Kafka client

API Reference

init<TContext>()

Initialize the workers framework with a custom context type.

procedure

The procedure builder for defining jobs:

  • .input({ payload: schema }) - Define input validation
  • .output(schema) - Define output validation
  • .use(middleware) - Add middleware
  • .task(handler) - Create an on-demand task
  • .cron(schedule, handler) - Create a scheduled job
  • .queue(name, handler) - Create a queue-based job

router(config)

Create a router with job definitions.

mergeRouters(...routers)

Combine multiple routers into one.