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

@andrew_l/service-actor

v0.3.3

Published

Forget about passing data like trace IDs between functions.

Readme

Service Actor Toolkit

npm version license

The package is designed to help manage contextual data, such as trace IDs, across functions without explicitly passing them around. This allows you to easily track and manage contexts (like user actions or requests) while keeping your code clean and decoupled from context-passing logic.

Documentation

✨ Features

  • Contextual Data Management: Allows you to manage context (such as trace IDs or user info) in a central "actor" object.
  • No Context Passing: Eliminates the need to manually pass context data between functions.
  • Custom Methods: Actor objects can contain custom methods without affecting the data or iteration logic.
  • Compatible with Databases: Actor objects are simple JavaScript objects and can be safely used in database queries.

🚀 Example Usage

Setting up Actor Context

import { serviceActor } from '@andrew_l/service-actor';

let traceIdSec = 0;

const { with: withServiceActor, inject: injectServiceActor } = serviceActor(
  () => ({
    traceId: `trace-${++traceIdSec}`,
    ipAddress: '0.0.0.0',
  }),
);

// Bind actor context to the request in your middleware
app.use((ctx, next) => {
  return withServiceActor(
    {
      traceId: ctx.headers['x-request-id'],
      ipAddress: ctx.headers['x-forwarded-for'],
    },
    next,
  );
});

app.patch('/users/:id', async ctx => {
  const actor = injectServiceActor(); // Inject service actor

  console.log('Updating user with', actor);

  await UserService.updateById(ctx.params.id, ctx.request.body);
  ctx.body = 'ok';
});

In the above example, the traceId and ipAddress are automatically associated with the current context (request), eliminating the need to manually pass them through function calls.

Using Service Actor in a Service

class UserService {
  static async updateById(targetId, data) {
    // Get the service actor from the context or create a new one if none exists
    const actor = useServiceActor({ actorType: 'service' });

    console.info('Updating user by id', { targetId, actor });

    await db.users.updateOne({ _id: targetId }, data);
  }
}

In this example, the UserService.updateById method retrieves the actor object, which contains contextual information, such as the trace ID, without explicitly passing it as an argument.

🤔 Why Use This Package?

  1. No Context Passing: Automatically manages contextual information (e.g., trace IDs, user info) across different parts of your application.
  2. Cleaner Code: Avoids cluttering your function signatures with unnecessary context arguments.
  3. Flexible and Extendable: Actor objects can be extended with custom methods to suit your application's needs without interfering with the data.
  4. Database Safe: Since the actor is just a plain object, it can be safely stored and used in database queries.