# @hlang-tech/runtime 中文文档

规范地址：https://npmcn.com/package/%40hlang-tech/runtime/

版本：1\.0\.7
npm 元数据更新时间：2026-11-01T05:50:49.053Z
中文译文更新时间：2026-11-01T08:53:32.373Z
软件包许可：MIT

本文由上游 README 机器翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40hlang-tech/runtime
原文对照：https://npmcn.com/package/%40hlang-tech/runtime/?lang=original
项目主页：https://github.com/hlang-tech/hlang/tree/main/packages/runtime#readme
源码仓库：https://github.com/hlang-tech/hlang

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：71.3 kB，33 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18
- 运行依赖：2 个
- 首次发布：2026-11-01T04:18:39.833Z
- 最新版本发布：2026-11-01T05:50:48.895Z
- 已发布版本数：3（近 12 个月 3 个）

## 中文简介

一种受fbp启发的通用应用程序语言。

## 安装

```sh
npm install @hlang-tech/runtime
```

也可以使用：`pnpm add @hlang-tech/runtime`、`yarn add @hlang-tech/runtime`、`bun add @hlang-tech/runtime`

## README

![hlang runtime](https://raw.githubusercontent.com/hlang-tech/hlang/main/media/brand/packages/runtime.png)

# @hlang-tech /运行时

hlang运行时：基于流的RxJS流编程。从通过指定端口连接的可读、转换和可写节点构建程序。

[![npm](https://img.shields.io/npm/v/@hlang-tech/runtime?color=c8ccd4&labelColor=0f1114)](https://www.npmjs.com/package/@hlang-tech/runtime) [![license](https://img.shields.io/npm/l/@hlang-tech/runtime?color=c8ccd4&labelColor=0f1114)](https://github.com/hlang-tech/hlang/blob/main/LICENSE)

## 安装

```sh
npm install @hlang-tech/runtime
```

相同的构建发布为 `@hlang-org/runtime`。包是带有类型声明的CommonJS。ES模块可以导入其命名的导出。

## 例子

```js
const { Flow, ReadableNode, TransformNode, WriteableNode, Port } = require('@hlang-tech/runtime');

class Numbers extends ReadableNode {
  constructor(opts) {
    super(opts);
    Port.O('out').attach(this);
  }
  _read($o) {
    [1, 2, 3].forEach((n) => $o('out').send(n));
  }
}

class Double extends TransformNode {
  constructor(opts) {
    super(opts);
    Port.I('in').attach(this);
    Port.O('out').attach(this);
  }
  _transform($i, $o) {
    $i('in').receive((n) => $o('out').send(n * 2));
  }
}

class Log extends WriteableNode {
  constructor(opts) {
    super(opts);
    Port.I('in').attach(this);
  }
  _write($i) {
    $i('in').receive((n) => console.log(n));
  }
}

const numbers = new Numbers();
const double = new Double();
const log = new Log();

numbers.O('out').connect(double.I('in'));
double.O('out').connect(log.I('in'));

new Flow().run(numbers); // 2, 4, 6
```

## 节点

| 类 | 种类 | 港口 | 实现 |
| --- | --- | --- | --- |
| `ReadableNode` | R是源 | 输出,加上 `@node/error` | `_read($o)` |
| `TransformNode` | T | 输入和输出，加上 `@node/error` | `_transform($i, $o)` |
| `WriteableNode` | W，水槽 | 只有输入，没有错误端口 | `_write($i)` |

在构造函数中创建端口 `Port.I(name).attach(this)` 和 `Port.O(name).attach(this)`。一个节点可以有任意数量的命名端口。构造函数参数存储为 `this.opts`。解释器和编译器通过 `{ nodeId, flowId, env, middleware, config, signal }`，节点参数为 `this.opts.config.params`.

运行时调用 `_read`， `_transform`，或者 `_write` 每个流一次。 `$i(name)` 和 `$o(name)` 返回节点的输入和输出端口。在节点内部， `this.I(name)`， `this.O(name)`, `this.E()` （错误端口）返回相同的端口。

可读和转换节点 `throw(error)`，它发送 `error` 在 `@node/error`.

## 港口

一个端口封装了一个RxJS `Subject`.

| 方法 | 效果 |
| --- | --- |
| `send(message)` | 发出 `message` （`next`）。 |
| `receive(listener)` | 订阅 `listener`。返回RxJS `Subscription`. |
| `pipe(...operators)` | 对端口发出的内容应用RxJS操作符。返回 `{ receive }`. |
| `connect(input)` | 将此输出端口连接到输入端口。抛出，除非方向是输出到输入。 |

端口是多播的：一个输出可以提供多个输入，一个输入可以从多个输出接收。

## 流

`new Flow().run(readable)` 收集可读根通过其连接可访问的节点。它首先启动可写节点，然后是转换节点，然后是可读根，因此在发送数据之前每个订阅者都存在。运行一个图的每个可读根。 `run` 如果其参数不是可读节点则抛出。

变换节点可以形成循环，因此图可以包含反馈循环。绑定一个反馈循环，例如用 `take` 或者计数器，这样就结束了。

## 相关的包

-   [`@hlang-tech/interpreter`](https://www.npmjs.com/package/@hlang-tech/interpreter) 运行一个JSON图(`IGraph`)与这些节点。
-   [`@hlang-tech/compiler`](https://www.npmjs.com/package/@hlang-tech/compiler) 编译一个JSON图形到一个ES模块。

来源、问题和文档： [github.com/hlang-tech/hlang](https://github.com/hlang-tech/hlang).

## 许可证

麻省理工学院

## 常见问题

### @hlang-tech/runtime 是什么？

一种受fbp启发的通用应用程序语言。

### 如何安装 @hlang-tech/runtime？

在项目目录运行 npm install @hlang-tech/runtime，或使用 pnpm add / yarn add / bun add。

### @hlang-tech/runtime 支持 TypeScript 吗？

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

### @hlang-tech/runtime 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @hlang-tech/runtime 需要什么版本的 Node\.js？

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

### @hlang-tech/runtime 可以商用吗？

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

### @hlang-tech/runtime 还在维护吗？

最新版本 1\.0\.7 发布于 2026/11/01（2 天前）；至今共发布 3 个版本，其中过去 12 个月发布了 3 个；首次发布于 2026/11/01。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

