持久性流式CLI
用于与可持久流交互的命令行工具 .
安装
从 npm
# Global installation
npm install -g @durable-streams/cli
# Or run directly with npx
npx @durable-streams/cli create my-stream
npx @durable-streams/cli read my-stream
从来源(促进发展)
# Clone the repository
git clone https://github.com/durable-streams/durable-streams.git
cd durable-streams
# Install dependencies
pnpm install
# Build the CLI
pnpm build
# Link globally for development (uses tsx, no rebuild needed)
cd packages/cli
pnpm link:dev
# Now you can use durable-stream-dev anywhere
durable-stream-dev create my-stream
快速启动
最容易启动的方法是运行本地开发服务器并使用 CLI :
第1终端:启动本地服务器
pnpm start:dev
这将启动持久流服务器 http://localhost:4437 现场重装.
终端2:使用 CLI
# Set the server URL (optional, defaults to http://localhost:4437/v1/stream)
export STREAM_URL=http://localhost:4437/v1/stream
# Create a stream
durable-stream-dev create my-stream
# Stream IDs may include slashes for hierarchical names
durable-stream-dev create account-123/chat/room-1
# Write to the stream
durable-stream-dev write my-stream "Hello, world!"
# Read from the stream (follows live)
durable-stream-dev read my-stream
使用量
环境变量
STREAM_URL- 流服务器的基础 URL( 默认 ):http://localhost:4437/v1/stream(中文(简体) )STREAM_AUTH- 授权标题值(例如,Bearer my-token(中文(简体) )
全球选项
--auth <value>- 授权页眉值(覆盖)STREAM_AUTH(原始内容存档于2019-09-21) (英语). env var
写入选项
--content-type <type>- 消息的内容类型( 默认 :application/octet-stream(中文(简体) )--json- 简指--content-type application/json
图标
创建流
durable-stream-dev create <stream_id>
写入到流中
# Write content as arguments
durable-stream-dev write <stream_id> "Hello, world!"
# Pipe content from stdin
echo "Hello from stdin" | durable-stream-dev write <stream_id>
cat file.txt | durable-stream-dev write <stream_id>
# Specify content type
durable-stream-dev write <stream_id> '{"key": "value"}' --content-type application/json
# Shorthand for JSON
durable-stream-dev write <stream_id> '{"key": "value"}' --json
JSON 模式阵列平滑
在 JSON 模式中 (--json 或者说 --content-type application/json),顶级数组被平整为单个消息:
| 投入 | 存储的信件 |
|---|---|
{} | 1条信息: {} |
[{}, {}] | 2个消息: {}, (中文(简体) ), {} |
[[{}, {}]] | 1条信息: [{}, {}] |
这符合协议的批量语义.
从流中读取
# Follows the stream and outputs new data to stdout
durable-stream-dev read <stream_id>
删除流
durable-stream-dev delete <stream_id>
认证
使用 --auth 旗帜或 STREAM_AUTH 要认证的环境变量 :
# Using environment variable
export STREAM_AUTH="Bearer my-token"
durable-stream-dev read my-stream
# Using --auth flag (overrides env var)
durable-stream-dev --auth "Bearer my-token" read my-stream
# Works with any auth scheme
durable-stream-dev --auth "Basic dXNlcjpwYXNz" read my-stream
durable-stream-dev --auth "ApiKey abc123" read my-stream
完成示例工作流程
# Terminal 1: Start the local development server
pnpm start:dev
# Terminal 2: Set up the stream
export STREAM_URL=http://localhost:4437/v1/stream
durable-stream-dev create test-stream
# Terminal 3: Start reading (will show data as it arrives)
export STREAM_URL=http://localhost:4437/v1/stream
durable-stream-dev read test-stream
# Back in Terminal 2: Write data and watch it appear in Terminal 3
durable-stream-dev write test-stream "First message"
durable-stream-dev write test-stream "Second message"
echo "Piped content!" | durable-stream-dev write test-stream
发展
# Start the example server with live reloading
pnpm start:dev
# Watch mode for CLI development (rebuilds dist/)
pnpm dev
# Build
pnpm build
# Link globally for development (uses tsx, no rebuild needed)
pnpm link:dev