@collidor/command

中文文档
7.3.4发布于 2026/10/09MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7951

A small library to create, register and execute commands.

原始使用文档
文档目录21 节
  1. Installation
  2. Features
  3. Basic Usage
  4. Using Class Inheritance
  5. Using createCommand (Bundler / Minification Safe)
  6. Async Stream Methods
  7. Registering an Async Stream Handler
  8. Consuming Async Streams
  9. Plugin System
  10. Async Operations
  11. Stream Processing
  12. API Documentation
  13. Constructor
  14. Methods
  15. Advanced Usage
  16. Custom Context
  17. Error Handling Plugin
  18. Plugins
  19. HTTP Plugin
  20. PortChannel Plugin
  21. Type Transformations

Codecov npm version License: MIT

Command

A lightweight, type-safe command pattern implementation with plugin support:

  • 🚀 Zero dependencies
  • 🔌 Extensible plugin system
  • 🔄 Async/Stream/Generator support
  • 🛠️ Full TypeScript type inference
  • 🌐 Context-aware execution

Installation

npm install @collidor/command

Features

  • Type-safe command execution - Return types automatically match handlers
  • Plugin architecture - Transform outputs to Promises, Streams, or custom types
  • Flexible context - Carry execution state through context object
  • Simple API - Only two methods: register and execute
  • Iterator support - Built-in handling of generators and async streams

Basic Usage

Using Class Inheritance

import { Command, CommandBus } from "@collidor/command";

// 1. Define command
class CreateUser extends Command<{name: string}, { id: string }> {}

// 2. Create bus
const bus = new CommandBus();

// 3. Register handler
bus.register(CreateUser, (command, context) => ({
  id: Math.random().toString(36).substr(2, 9),
}));

// 4. Execute (type inferred as { id: string })
const user = bus.execute(new CreateUser({ name: "Alice" }));

Using createCommand (Bundler / Minification Safe)

When bundling for production, JavaScript minifiers often mangle class names (e.g. class CreateUser becomes class a). Since the bus, DI provider, and networking plugins (httpPlugin, portChannelPlugin, windowCustomEventPlugin) route commands using their constructor name, createCommand provides a type-safe way to define commands with a fixed, unminifiable name:

import { createCommand, CommandBus } from "@collidor/command";

// Define command with explicit types and name: createCommand<PayloadType, ReturnType>("CommandName")
export const CreateUser = createCommand<{ name: string }, { id: string }>("CreateUser");

// Void payload command
export const PingCommand = createCommand<void, string>("PingCommand");

const bus = new CommandBus();

bus.register(CreateUser, (command) => ({
  id: "user_123",
}));

bus.register(PingCommand, () => "pong");

const user = bus.execute(new CreateUser({ name: "Alice" }));
const pong = bus.execute(new PingCommand());

Async Stream Methods

For scenarios where a command handler produces multiple asynchronous events, you can now register an async stream handler that returns an AsyncIterable. This lets you consume command streams using a natural for await … of loop.

Registering an Async Stream Handler

Use registerStreamAsync to register a handler that returns an async iterator. This handler can perform asynchronous work and yield events over time.

import { CommandBus, Command } from "@collidor/command";

// Define a command
class UpdateUser extends Command<number, { status: string }> {}

// Create a CommandBus instance
const commandBus = new CommandBus();

// Register an async stream handler
commandBus.registerStreamAsync(UpdateUser, async function* (command, context) {
  for (let i = 0; i < command.data; i++) {
    // Simulate asynchronous delay
    await new Promise((resolve) => setTimeout(resolve, 100));
    yield { status: `update-${i}` };
  }
});

Consuming Async Streams

The streamAsync method returns an async iterator. You can easily consume the stream with a for await … of loop:

(async () => {
  // Execute the command and consume the async stream
  for await (const result of commandBus.streamAsync(new UpdateUser(5))) {
    console.log(result);
  }
  // Expected output:
  // { status: 'update-0' }
  // { status: 'update-1' }
  // { status: 'update-2' }
  // { status: 'update-3' }
  // { status: 'update-4' }
})();

Plugin System

Async Operations

const asyncBus = new CommandBus({
  plugin: (command, ctx, handler) => {
    return Promise.resolve(handler?.(command, ctx));
  }
});

// Returns Promise<{ id: string }>
const futureUser = asyncBus.execute(new CreateUser());

Stream Processing

const streamBus = new CommandBus({
  plugin: async function* (command, ctx, handler) {
    yield await handler?.(command, ctx);
    yield await handler?.(command, ctx);
  }
});

// Returns AsyncIterable<{ id: string }>
for await (const result of streamBus.execute(new CreateUser())) {
  console.log(result);
}

API Documentation

CommandBus<TContext, TPlugin>

Constructor

Methods

MethodDescription
register<C>Register command handler
execute<C>Execute command with type inference
registerStream<C>Register a stream command handler
stream<C>Execute a command with the result calling the callback overtime
registerStreamAsync<C>Register an async stream handler that returns an AsyncIterable
streamAsync<C>Execute a command and return an async iterator for streaming events.

Advanced Usage

Custom Context

interface AppContext {
  requestId: string;
  user: { id: string };
}

const bus = new CommandBus<AppContext>({
  context: {
    requestId: "123",
    user: { id: "system" }
  }
});

bus.register(CreateUser, (cmd, ctx) => {
  console.log(ctx.user.id); // "system"
  return { id: ctx.requestId };
});

Error Handling Plugin


const errorHandlingPlugin: CommandBusPlugin<
    Command,
    typeof context,
    Command[COMMAND_RETURN]
  > = {
    handler: (command, ctx, handler) => {
        try {
            return {
                success: true,
                value: handler?.(command, ctx)
            };
        } catch (error) {
            console.error("Command failed:", command);
            return {
                success: false,
                error,
            }
        }
    },
  };

const safeCommandBus = new CommandBus({ plugin: errorHandlingPlugin });

Plugins

HTTP Plugin

The HTTP plugin integrates your CommandBus with HTTP endpoints—on both the client and server side—while preserving full type safety and leveraging the plugin system.

HTTP Client Plugin

The httpClientPlugin intercepts command execution and makes an HTTP POST request to your API endpoint. It supports:

  • Dynamic routes: Provide a function to generate the URL based on the command and context.
  • Custom headers: Supply static headers or a function that returns headers.
  • Custom serializers: Override the default JSON serializer for different content types.

Example Usage:

import { CommandBus, Command, httpClientPlugin } from "@collidor/command";

// Define a command
class CreateUser extends Command<{ name: string }, { id: string }> {}

// Create the HTTP client plugin with custom headers
const clientPlugin = httpClientPlugin("http://api.example.com/createUser", {
  headers: () => ({
    "X-API-Key": "my-secret-key",
  }),
});

// Create the CommandBus with the HTTP client plugin installed.
// If a local handler is registered, it will take precedence.
const bus = new CommandBus({ plugin: clientPlugin });

// Execute the command.
// makes an HTTP POST.
const user = await bus.execute(new CreateUser({ name: "Alice" }));
console.log(user); // e.g. { id: "generated-id-xxx" }

Notes:

If a local handler is provided, it will be used; otherwise, the HTTP client plugin sends a POST request. The default serializer converts data to JSON and expects JSON responses, but you can supply your own.

HTTP Server Plugin

The httpServerPlugin enables you to expose your CommandBus as an HTTP endpoint. It provides helper methods to:

  • Extract a command from a request: getCommandFromRequest
  • Handle a request: handleRequest

This plugin deserializes the incoming request into a command, executes it on the bus, and returns the result.

Example Usage:

import { CommandBus, Command, httpServerPlugin } from "@collidor/command";

// Define a command
class CreateUser extends Command<{ name: string }, { id: string }> {}

// Create an HTTP server plugin instance
const serverPlugin = httpServerPlugin();

// Create the CommandBus with the server plugin installed
const bus = new CommandBus({ plugin: serverPlugin });

// Register a command handler
bus.register(CreateUser, (command) => ({
  id: "generated-id-" + Math.random().toString(36).substring(2, 9),
}));

// Simulate an HTTP POST request (e.g., within your web framework)
const request = new Request("http://api.example.com", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    data: { name: "Alice" },
    name: CreateUser.name,
  }),
});

// Handle the request using the plugin
serverPlugin.handleRequest(request).then((result) => {
  console.log(result);
  // Expected output: { id: "generated-id-xxx" }
});

Notes:

Ensure that the plugin is installed via the CommandBus so that getCommandFromRequest can access the command constructors. The server plugin uses the same serializer logic as the client plugin by default. Customize it if needed.

Customizing Serialization

Both HTTP plugins allow you to provide a custom serializer if you need to work with different data formats or content types.

Example:

import { Serializer } from "@collidor/command/plugins/httpPlugin";

const customSerializer: Serializer<Blob> = {
  serialize: (data, headers) => {
    headers.set("Content-Type", "application/octet-stream");
    return new Blob([JSON.stringify(data)], { type: "application/octet-stream" });
  },
  deserializeResponse: async (response) => {
    if (!response.ok) {
      throw new Error(`HTTP error: ${response.status}`);
    }
    const text = await response.text();
    return JSON.parse(text);
  },
  deserializeRequest: async (request) => {
    const text = await request.text();
    return JSON.parse(text);
  },
};

const clientPlugin = httpClientPlugin("http://api.example.com", {
  serializer: customSerializer,
});

By integrating the HTTP client and server plugins, you can easily connect your command handlers with remote HTTP services while still enjoying all the benefits of type safety and plugin extensibility offered by the Command library.

PortChannel Plugin

The new PortChannel Plugin leverages the underlying PortChannel from @collidor/event to bridge command execution across different environments (for example, between a server and a client). It provides:

  • Buffering of Events:

    If an event is published before any subscriber connects, the plugin buffers the event and flushes it immediately when a subscriber appears.

  • Maximum Buffer Timeout:

    Buffered events are automatically discarded after a configurable timeout (defaulting to 5000 ms) to prevent memory leaks.

  • Inter-Process Communication:

    Allows commands to be dispatched and responses received via a MessagePort, making it ideal for scenarios like web workers, iframes, or other multi-context environments.

Example Usage:

import { CommandBus, Command, PortChannelPlugin } from "@collidor/command";

// Define a command
class CreateUser extends Command<{ name: string }, { id: string }> {}

// Create an instance of the PortChannel Plugin.
// Optionally, configure the buffer timeout (in milliseconds)
const portPlugin = new PortChannelPlugin({ bufferTimeout: 5000 });

// Create a CommandBus with the PortChannel Plugin installed.
const bus = new CommandBus({ plugin: portPlugin });

// Register a command handler on the bus.
bus.register(CreateUser, (command, context) => ({
  id: "user-" + Math.random().toString(36).slice(2),
}));

// Dispatch a command via the PortChannel Plugin.
// The plugin buffers events until a subscriber (e.g. a connected MessagePort) attaches.
const user = await bus.execute(new CreateUser({ name: "Alice" }));
console.log(user); // e.g. { id: "user-..." }

Notes:

  • Buffering Behavior:

    Commands or events dispatched before a subscriber connects will be stored in a buffer and then flushed as soon as a subscriber (via addPortSubscription) attaches.

  • Timeout Control:

    The maximum time an event remains buffered is controlled by the bufferTimeout option.

  • Interoperability:

    This plugin is particularly useful in scenarios where the server and client reside in separate execution contexts (e.g., web workers or iframes), ensuring that events are not lost during initial connection delays.

Type Transformations

Automatic return type wrapping based on plugin:

// Given this plugin:
  const plugin: CommandBusPlugin<
    Command,
    typeof context,
    Command[COMMAND_RETURN][]
  > = {
    handler: (command, c, h) => [(h?.(command, c))],
  };

// Return type becomes Array<{ id: string }>
const result = bus.execute(new CreateUser());

Contribution

  1. Fork repository
  2. Create feature branch (git checkout -b feature/fooBar)
  3. Commit changes (git commit -am 'Add some fooBar')
  4. Push to branch (git push origin feature/fooBar)
  5. Create new Pull Request

License

MIT © Alykam Burdzaki

版本与发布记录

@collidor/command 共发布 87 个版本,首次发布于 2021/10/14;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.3.4latest2026/10/091 个月前
7.3.32026/10/091 个月前
7.3.22026/10/081 个月前
7.3.12026/10/081 个月前
7.3.02026/10/021 个月前
7.2.12026/09/261 个月前

关于 @collidor/command 的常见问题

@collidor/command 是什么?

A small library to create, register and execute commands.

如何安装 @collidor/command?

在项目目录运行 npm install @collidor/command,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@collidor/event(^4.7.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@collidor/command 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@collidor/command 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@collidor/command 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@collidor/command 可以商用吗?

@collidor/command 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@collidor/command 的下载量有多少?

根据 npm 官方下载统计,@collidor/command 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,951 次,日均约 265 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@collidor/command 还在维护吗?

最新版本 7.3.4 发布于 2026/10/09(1 个月前);至今共发布 87 个版本,其中过去 12 个月发布了 14 个;首次发布于 2021/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。