Effect Platform 简介
使用 @effect/platform 的统一抽象,为 Node.js、Deno、Bun 和浏览器构建跨平台应用。
@effect/platform 是一个用于在 Node.js、Deno、Bun 和浏览器等环境中构建与平台无关的抽象的库。
借助 @effect/platform,你可以把 FileSystem 或 Terminal 这类抽象服务集成到你的程序中。
在组装最终应用时,你可以使用对应的包,为目标平台提供具体的 layers:
@effect/platform-node,用于 Node.js 或 Deno@effect/platform-bun,用于 Bun@effect/platform-browser,用于浏览器
稳定模块
以下模块已经稳定,它们的文档可以在本站查阅:
| 模块 | 说明 | 状态 |
|---|---|---|
| Command | 提供与命令行交互的方式。 | Stable |
| FileSystem | 一套用于文件系统操作的模块。 | Stable |
| KeyValueStore | 管理用于数据存储的键值对。 | Stable |
| Path | 处理文件路径的工具。 | Stable |
| PlatformLogger | 使用 FileSystem API 把日志消息写入文件。 | Stable |
| Runtime | 以内置的错误处理与日志功能运行你的程序。 | Stable |
| Terminal | 用于终端交互的工具。 | Stable |
不稳定模块
@effect/platform 中还有一些模块仍在开发中,或被标记为实验性。
这些特性可能会发生变化。
| 模块 | 说明 | 状态 |
|---|---|---|
| Http API | 以声明式的方式定义 HTTP API。 | Unstable |
| Http Client | 用于发起 HTTP 请求的客户端。 | Unstable |
| Http Server | 用于处理 HTTP 请求的服务端。 | Unstable |
| Socket | 一套基于 socket 进行通信的模块。 | Unstable |
| Worker | 用于在独立 worker 中运行任务的模块。 | Unstable |
有关最新文档与详细信息,请参阅该包官方的 README。
安装
安装 beta 版本:
npm
npm install @effect/platformpnpm
pnpm add @effect/platformYarn
yarn add @effect/platformBun
bun add @effect/platformDeno
deno add npm:@effect/platform跨平台编程入门
下面是一个基础示例,使用 Path 模块创建一个文件路径,它可以在不同环境中运行:
示例(跨平台路径处理)
import { Path } from "@effect/platform"
import { Effect } from "effect"
const program = Effect.gen(function* () {
// Access the Path service
const path = yield* Path.Path
// Join parts of a path to create a complete file path
const mypath = path.join("tmp", "file.txt")
console.log(mypath)
})
在 Node.js 或 Deno 中运行程序
首先,安装 Node.js 专用的包:
npm
npm install @effect/platform-nodepnpm
pnpm add @effect/platform-nodeYarn
yarn add @effect/platform-nodeDeno
deno add npm:@effect/platform-node更新程序,让它加载 Node.js 专用的 context:
示例(提供 Node.js context)
import { Path } from "@effect/platform"
import { Effect } from "effect"
import { NodeContext, NodeRuntime } from "@effect/platform-node"
const program = Effect.gen(function* () {
// Access the Path service
const path = yield* Path.Path
// Join parts of a path to create a complete file path
const mypath = path.join("tmp", "file.txt")
console.log(mypath)
})
NodeRuntime.runMain(program.pipe(Effect.provide(NodeContext.layer)))
最后,使用 tsx 在 Node.js 中运行程序,或直接在 Deno 中运行:
npm
npx tsx index.ts
# Output: tmp/file.txtpnpm
pnpm dlx tsx index.ts
# Output: tmp/file.txtYarn
yarn dlx tsx index.ts
# Output: tmp/file.txtDeno
deno run index.ts
# Output: tmp/file.txt
# or
deno run -RE index.ts
# Output: tmp/file.txt
# (granting required Read and Environment permissions without being prompted)在 Bun 中运行程序
要在 Bun 中运行同一个程序,首先安装 Bun 专用的包:
bun add @effect/platform-bun
更新程序,让它使用 Bun 专用的 context:
示例(提供 Bun context)
import { Path } from "@effect/platform"
import { Effect } from "effect"
import { BunContext, BunRuntime } from "@effect/platform-bun"
const program = Effect.gen(function* () {
// Access the Path service
const path = yield* Path.Path
// Join parts of a path to create a complete file path
const mypath = path.join("tmp", "file.txt")
console.log(mypath)
})
BunRuntime.runMain(program.pipe(Effect.provide(BunContext.layer)))
在 Bun 中运行程序:
bun index.ts
tmp/file.txt