@effectionx/node

中文文档
0.2.5发布于 2026/09/03MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 9.9万

Node.js stream and event emitter adapters for Effection

原始使用文档
文档目录9 节
  1. Installation
  2. Modules
  3. Stream Utilities
  4. fromReadable()
  5. Event Utilities
  6. on()
  7. once()
  8. TypeScript Support
  9. Interfaces

Node

Node.js-specific utilities for Effection programs. This package provides adapters for working with Node.js streams and event emitters using structured concurrency.


Installation

npm install @effectionx/node

Modules

This package provides two sub-modules:

  • @effectionx/node/stream - Stream utilities for Node.js
  • @effectionx/node/events - Event utilities for Node.js EventEmitters

You can also import everything from the main module:

import { fromReadable, on, once } from "@effectionx/node";

Stream Utilities

fromReadable()

Convert a Node.js Readable stream to an Effection Stream.

import fs from "node:fs";
import { each, main } from "effection";
import { fromReadable } from "@effectionx/node/stream";

await main(function* () {
  const fileStream = fs.createReadStream("./data.txt");

  for (const chunk of yield* each(fromReadable(fileStream))) {
    console.log(new TextDecoder().decode(chunk));
    yield* each.next();
  }
});

The returned stream emits Uint8Array chunks and automatically cleans up event listeners when the stream is closed or the operation is shut down.

Event Utilities

on()

Create a Stream of events from any EventEmitter or EventTarget-like object.

This works with:

  • Node.js EventEmitters (using on/off)
  • DOM EventTargets (using addEventListener/removeEventListener)
  • Web Worker's global self object
import { each, main } from "effection";
import { on } from "@effectionx/node/events";

await main(function* () {
  // With Node.js EventEmitter
  for (const [chunk] of yield* each(on(stream, "data"))) {
    console.log("data:", chunk);
    yield* each.next();
  }

  // In a worker thread (EventTarget style)
  for (const [event] of yield* each(on(self, "message"))) {
    console.log("received:", event.data);
    yield* each.next();
  }
});

For EventEmitters, events are emitted as arrays of arguments. For EventTargets, events are emitted as single-element arrays containing the event object.

once()

Create an Operation that yields the next event to be emitted by an EventEmitter or EventTarget-like object.

import { main } from "effection";
import { once } from "@effectionx/node/events";

await main(function* () {
  // Wait for a single message (EventTarget style)
  const [event] = yield* once(self, "message");
  console.log(event.data);

  // Wait for a single event (EventEmitter style)
  const [code] = yield* once(process, "exit");
  console.log("Process exited with code:", code);
});

TypeScript Support

All exports include TypeScript type definitions. The event functions support generic type parameters for type-safe event handling:

import { once, on } from "@effectionx/node/events";

// Type the event arguments
const [code] = yield* once<[number]>(process, "exit");

// Type the stream events
for (const [data] of yield* each(on<[Buffer]>(stream, "data"))) {
  // data is typed as Buffer
  yield* each.next();
}

Interfaces

The event utilities work with any object that implements these interfaces:

// Node.js EventEmitter style
interface EventEmitterLike {
  on(event: string, listener: (...args: unknown[]) => void): void;
  off(event: string, listener: (...args: unknown[]) => void): void;
}

// DOM EventTarget style
interface EventTargetLike {
  addEventListener(event: string, listener: (event: unknown) => void): void;
  removeEventListener(event: string, listener: (event: unknown) => void): void;
}

版本与发布记录

@effectionx/node 共发布 6 个版本,首次发布于 2026/01/19;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.5latest2026/09/032 个月前
0.2.42026/03/237 个月前
0.2.32026/03/098 个月前
0.2.22026/02/248 个月前
0.2.12026/02/129 个月前
0.2.02026/01/199 个月前

关于 @effectionx/node 的常见问题

@effectionx/node 是什么?

Node.js stream and event emitter adapters for Effection

如何安装 @effectionx/node?

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

@effectionx/node 支持 TypeScript 吗?

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

@effectionx/node 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@effectionx/node 可以商用吗?

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

@effectionx/node 的下载量有多少?

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

@effectionx/node 还在维护吗?

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