已发布 上游基线 bf46254 原文 ↗ 在 GitHub 编辑

Effect Platform 简介

使用 @effect/platform 的统一抽象,为 Node.js、Deno、Bun 和浏览器构建跨平台应用。

@effect/platform 是一个用于在 Node.js、Deno、Bun 和浏览器等环境中构建与平台无关的抽象的库。

借助 @effect/platform,你可以把 FileSystemTerminal 这类抽象服务集成到你的程序中。 在组装最终应用时,你可以使用对应的包,为目标平台提供具体的 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/platform
pnpm
pnpm add @effect/platform
Yarn
yarn add @effect/platform
Bun
bun add @effect/platform
Deno
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-node
pnpm
pnpm add @effect/platform-node
Yarn
yarn add @effect/platform-node
Deno
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.txt
pnpm
pnpm dlx tsx index.ts
# Output: tmp/file.txt
Yarn
yarn dlx tsx index.ts
# Output: tmp/file.txt
Deno
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