@elysiumjs/ariadne

中文文档
0.4.1发布于 2026/11/12Apache-2.0TypeScriptES Module近 30 天下载 2809

Multi-step flows for the Elysium.js framework, with replayable steps and server-side state.

此软件包已被标记为弃用

has moved into @elysiumjs/olympus: import from @elysiumjs/olympus/ariadne.

原始使用文档
文档目录4 节
  1. Installation
  2. Declaring a flow
  3. Serving flows
  4. Stores

@elysiumjs/ariadne

Multi-step flows for Elysium.js apps, such as a sign-up with a code check or a checkout. The state of a flow stays on the server between steps, and each step runs once: a repeated step id replays the stored answer.

Installation

bun add @elysiumjs/ariadne

Declaring a flow

A flow is a controller. Each step is a POST route of it, so the @Http.*() parameter decorators work on a step like on any route.

import type { Static } from '@elysiumjs/core';

import { Flow } from '@elysiumjs/ariadne';
import { BadRequestError, Http, t } from '@elysiumjs/core';

type SignUp = { email?: string; attempts: number };

const EmailBody = t.Object({ email: t.String({ format: 'email' }) });
const CodeBody = t.Object({ code: t.String() });

@Flow.controller('sign-up', { ttl: 900, owner: 'guest' })
export class SignUpFlow {
    constructor(
        private readonly codes: CodeService,
        private readonly users: UserService
    ) {}

    @Flow.initial()
    public initial(): SignUp {
        return { attempts: 0 };
    }

    @Flow.step({ next: ['code'], back: ['email'] })
    public async email(
        @Http.body(EmailBody) { email }: Static<typeof EmailBody>,
        @Flow.state() state: SignUp
    ) {
        await this.codes.send(email);
        return Flow.advance({ ...state, email }, { sentTo: email });
    }

    @Flow.step()
    public async code(
        @Http.body(CodeBody) { code }: Static<typeof CodeBody>,
        @Flow.state() state: SignUp
    ) {
        if (!(await this.codes.check(state.email!, code))) {
            const attempts = state.attempts + 1;
            return Flow.refuse({ ...state, attempts }, new BadRequestError('Wrong code.'), {
                end: attempts >= 3
            });
        }
        return { userId: await this.users.create(state.email!) };
    }
}
  • next lists the steps that may follow the step, or is a function of the state the step produced. The flow ends when it gives no step, which is the default.
  • back lists the earlier steps the client may submit again from there, and onRewind(state, ctx) runs on the state of a step submitted again.
  • ttl is how long the flow lives after its last step, in seconds. It may be a function that reads it from the configuration, called each time a flow starts or a step is committed.
  • start lists the steps accepted first, and defaults to the first declared step. name names a step after something other than its method.
  • @Flow.initial() marks the method that builds the state before the first step. Without one, the state starts as {}.
  • @Flow.state() injects the state, and @Flow.context() the flow, its key, the step id and the owner.
  • Flow.advance(state, answer) moves the flow forward. A step that returns anything else answers it and keeps the state.
  • A step may return a Result (or an AsyncResult): a passed one counts as its value, a failed one refuses the step with its error, like a returned or thrown DomainError.
  • Flow.refuse(state, error) refuses the step but keeps its state, such as a counted attempt. With { end: true }, the flow is deleted.
  • A step that throws commits nothing, and the client may retry the same step id.
  • A submission holds its step for lease seconds (30 by default) while it runs, and the step runs on the state the store holds at that moment, so attempts that race see each other's refusals. A retry of a submission that outlives its lease runs the step again, so a step that may run longer sets a longer lease.

The other options of @Http.controller() apply to @Flow.controller(), and those of the route decorators to @Flow.step(). Guards, rate limits, interceptors and filters decorate a flow like any controller.

Serving flows

import { AriadneModule } from '@elysiumjs/ariadne';
import { RedisFlowStore } from '@elysiumjs/ariadne-redis';
import { Application, Module } from '@elysiumjs/core';

@Module.register({ controllers: [SignUpFlow, ChangeEmailFlow] })
class AccountModule extends Module {}

@Application.register({
    modules: [
        AriadneModule.with({ store: new RedisFlowStore({ connection: () => Redis.connection() }) }),
        AccountModule
    ]
})
class App extends Application {}

A flow answers under path, which defaults to /flows/{name}:

RouteHeadersAnswer
POST /flows/{flow}201 { flowKey, owner?, nextSteps, backSteps }
GET /flows/{flow}x-flow-key{ flowKey, step, nextSteps, backSteps, answer }
POST /flows/{flow}/{step}x-flow-key, x-flow-step{ answer, nextSteps, backSteps }
  • x-flow-step is the id of the submission, which the client picks and sends again to retry it.
  • A guest flow belongs to the owner token answered at start. Later calls send it in the x-flow-owner header.
  • A user flow belongs to the signed-in user, so its controller needs a guard, such as @Auth() from themis. user in the module options reads the user id from the context. By default it reads currentUser, which @Auth() sets: the user itself when it is a string (the session guard without a loader), else its id, else its sub (JWT claims).

The routes answer problems:

  • 410 when the flow expired, or is finished;
  • 409 for a step the flow does not accept now (with nextSteps, backSteps and the last answer), and for a step submitted while another submission of the flow runs (flow-step-in-progress);
  • 403 when the caller does not own the flow, and 401 for a user flow without a user;
  • 422 for a body that does not match the step's schema.
  • 400 for a request without x-flow-key or x-flow-step.

Stores

  • MemoryFlowStore, for tests and single-process apps.
  • RedisFlowStore of @elysiumjs/ariadne-redis, whose claims and commits are Lua scripts run with EVAL, which Upstash runs too.
  • durableFlowStore of @elysiumjs/ariadne-workers, a Durable Object per flow on Cloudflare Workers.

A finished flow keeps its last answer replayable for up to 10 minutes. Then only its owner and end time stay, for its TTL, so that a late call learns that the flow is finished. A custom store implements FlowStore, and can follow FlowRules to answer like the others.

版本与发布记录

@elysiumjs/ariadne 共发布 15 个版本,首次发布于 2026/11/07;过去 12 个月发布了 15 个版本。

版本发布日期距今
0.4.1latest2026/11/122 天前
0.4.02026/11/112 天前
0.3.32026/11/113 天前
0.3.22026/11/113 天前
0.3.12026/11/103 天前
0.3.02026/11/094 天前

关于 @elysiumjs/ariadne 的常见问题

@elysiumjs/ariadne 是什么?

Multi-step flows for the Elysium.js framework, with replayable steps and server-side state.注意:该包已被作者标记为弃用——has moved into @elysiumjs/olympus: import from @elysiumjs/olympus/ariadne.

如何安装 @elysiumjs/ariadne?

在项目目录运行 npm install @elysiumjs/ariadne,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@elysiumjs/core(^0.24.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@elysiumjs/ariadne 支持 TypeScript 吗?

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

@elysiumjs/ariadne 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@elysiumjs/ariadne 可以商用吗?

@elysiumjs/ariadne 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@elysiumjs/ariadne 的下载量有多少?

根据 npm 官方下载统计,@elysiumjs/ariadne 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,809 次,日均约 94 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elysiumjs/ariadne 还在维护吗?

最新版本 0.4.1 发布于 2026/11/12(2 天前);至今共发布 15 个版本,其中过去 12 个月发布了 15 个;首次发布于 2026/11/07。作者已将其标记为弃用,新项目建议寻找替代方案。