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

Effect Platform 简介

使用 Effect 内置的 platform 模块,以统一的抽象为 Node.js、Deno、Bun 和浏览器构建跨平台应用。

effect 包内置了 platform 模块,用于在 Node.js、Deno、Bun 和浏览器等环境中构建与平台无关的抽象。

借助这些模块,你可以把 FileSystemTerminal 这类抽象服务集成到程序中。 在组装最终应用时,你可以使用对应的包,为目标平台提供具体的 layers

  • @effect/platform-node,用于 Node.js 或 Deno
  • @effect/platform-bun,用于 Bun
  • @effect/platform-browser,用于浏览器

稳定模块

以下模块已经稳定,它们的文档可以在本站查阅:

ModuleDescriptionStatus
FileSystem一套用于文件系统操作的模块。Stable
Path处理文件路径的工具。Stable
PlatformLogger使用 FileSystem API 把日志消息写入文件。Stable
Runtime以内置的错误处理与日志功能运行你的程序。Stable
Terminal用于终端交互的工具。Stable

安装

platform 模块已包含在 effect 包中,因此无需额外安装。关于如何安装 effect 本身,请参阅安装

@effect/platform-node 这类特定于平台的包,只有在具体平台上运行程序时才需要,如下文各节所示。

跨平台编程入门

下面是一个基础示例,使用 Path 模块创建一个文件路径,它可以在不同环境中运行:

示例(跨平台路径处理)

import { Effect, Path } 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)
  return mypath
})

Effect.runSync(program.pipe(Effect.provide(Path.layer))) // => "tmp/file.txt"

在 Node.js 或 Deno 中运行程序

首先,安装 Node.js 专用的包:

npm
npm install @effect/platform-node@rc
pnpm
pnpm add @effect/platform-node@rc
Yarn
yarn add @effect/platform-node@rc
Deno
deno add npm:@effect/platform-node@rc

更新程序,让它加载 Node.js 专用的 context:

示例(提供 Node.js context)

import { Effect, Path } from "effect"
import { NodeServices, 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(NodeServices.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@rc

更新程序,让它使用 Bun 专用的 context:

示例(提供 Bun context)

import { Effect, Path } from "effect"
import { BunServices, 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(BunServices.layer)))

在 Bun 中运行程序:

bun index.ts
tmp/file.txt