rura 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +139 -0
- package/dist/index.cjs +48 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +44 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yiming Liao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
<h1 align="center">Rura</h1>
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
A minimal **pipeline engine** for every modern workflow.
|
|
6
|
+
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
|
|
11
|
+
[](https://www.npmjs.com/package/rura)
|
|
12
|
+
[](https://bundlephobia.com/package/rura)
|
|
13
|
+
[](https://coveralls.io/github/yiming-liao/rura?branch=main)
|
|
14
|
+
[](https://www.typescriptlang.org/)
|
|
15
|
+
[](LICENSE)
|
|
16
|
+
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
> Clarity meets stability.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- 🎯 **Deterministic** – Ordered hooks with effortless early exit.
|
|
24
|
+
- 🧱 **Typed** – Inferred context and output with zero boilerplate.
|
|
25
|
+
- 🌏 **Universal** – Tiny, framework-agnostic, and ready for any flow.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# npm
|
|
31
|
+
npm install rura
|
|
32
|
+
# yarn
|
|
33
|
+
yarn add rura
|
|
34
|
+
# pnpm
|
|
35
|
+
pnpm add rura
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
#### 1. Define the initial context
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
const ctx = { count: 1 };
|
|
44
|
+
type Ctx = typeof ctx;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### 2. Create your hooks
|
|
48
|
+
|
|
49
|
+
- Hooks can be defined with `createHook` or manually via the `Hook` type.
|
|
50
|
+
- Hooks may specify an `order`; omitted values default to `0`.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// With createHook (recommended for convenience)
|
|
54
|
+
const addOne = createHook<Ctx>("add-one", (ctx) => {
|
|
55
|
+
ctx.count += 1;
|
|
56
|
+
}); // order: 0
|
|
57
|
+
|
|
58
|
+
// Manual hook definition (full control)
|
|
59
|
+
const stopIfEven: Hook<Ctx, number> = {
|
|
60
|
+
name: "stop-if-even",
|
|
61
|
+
run: (ctx) => {
|
|
62
|
+
if (ctx.count % 2 === 0) {
|
|
63
|
+
return { done: true, output: ctx.count };
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
order: 2, // order: 2
|
|
67
|
+
};
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
> Rura executes hooks in order and exits early when a hook returns `{ done: true }`.
|
|
71
|
+
|
|
72
|
+
#### 3. Run the pipeline
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const result = await runRura(ctx, [addOne, stopIfEven]);
|
|
76
|
+
|
|
77
|
+
console.log(result); // -> 2 (early exit triggered)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Pipeline Builder
|
|
83
|
+
|
|
84
|
+
**createRura()** - Creates a lightweight, composable pipeline instance.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const rura = createRura<Context, Output>();
|
|
88
|
+
|
|
89
|
+
// Add hooks
|
|
90
|
+
rura
|
|
91
|
+
.use(hookA) // chainable
|
|
92
|
+
.use(hookB)
|
|
93
|
+
.use(hookC);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
example:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { createRura } from "rura";
|
|
100
|
+
|
|
101
|
+
type Ctx = { value: number };
|
|
102
|
+
|
|
103
|
+
// Create a pipeline
|
|
104
|
+
const pipelineA = createRura<Ctx>();
|
|
105
|
+
|
|
106
|
+
pipelineA.use({
|
|
107
|
+
name: "add-two",
|
|
108
|
+
run: (ctx) => {
|
|
109
|
+
ctx.value += 2;
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Create another pipeline
|
|
114
|
+
const pipelineB = createRura<Ctx>();
|
|
115
|
+
|
|
116
|
+
pipelineB.use({
|
|
117
|
+
name: "multiply-three",
|
|
118
|
+
run: (ctx) => {
|
|
119
|
+
ctx.value *= 3;
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Merge pipelines
|
|
124
|
+
const pipeline = pipelineA.merge(pipelineB);
|
|
125
|
+
|
|
126
|
+
// Run it
|
|
127
|
+
const result = await pipeline.run({ value: 1 });
|
|
128
|
+
|
|
129
|
+
console.log(result); // -> { value: 9 }
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### Pipeline Instance Methods
|
|
133
|
+
|
|
134
|
+
| Method | Description | Parameters | Returns |
|
|
135
|
+
| ---------------- | ---------------------------------------------------- | ---------- | ---------------------------- |
|
|
136
|
+
| **use(hook)** | Adds a hook to the pipeline. | `hook` | `this` (chainable) |
|
|
137
|
+
| **merge(other)** | Merges hooks from another pipeline instance. | `other` | `this` (chainable) |
|
|
138
|
+
| **getHooks()** | Returns all registered hooks. | – | `Hook[]` |
|
|
139
|
+
| **run(ctx)** | Executes the pipeline **_(equivalent to runRura)_**. | `ctx` | `Promise<Output \| Context>` |
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/is-done.ts
|
|
4
|
+
function isDone(r) {
|
|
5
|
+
return typeof r === "object" && r !== null && "done" in r;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/run-rura.ts
|
|
9
|
+
async function runRura(ctx, hooks) {
|
|
10
|
+
hooks.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
11
|
+
for (const hook of hooks) {
|
|
12
|
+
const result = await hook.run(ctx);
|
|
13
|
+
if (isDone(result)) {
|
|
14
|
+
return result.output;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return ctx;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/create-rura.ts
|
|
21
|
+
function createRura() {
|
|
22
|
+
const hooks = [];
|
|
23
|
+
function use(hook) {
|
|
24
|
+
hooks.push(hook);
|
|
25
|
+
return api;
|
|
26
|
+
}
|
|
27
|
+
function merge(other) {
|
|
28
|
+
other.getHooks().forEach((h) => hooks.push(h));
|
|
29
|
+
return api;
|
|
30
|
+
}
|
|
31
|
+
function getHooks() {
|
|
32
|
+
return hooks;
|
|
33
|
+
}
|
|
34
|
+
async function run(ctx) {
|
|
35
|
+
return runRura(ctx, hooks);
|
|
36
|
+
}
|
|
37
|
+
const api = { use, merge, getHooks, run };
|
|
38
|
+
return api;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/create-hook.ts
|
|
42
|
+
function createHook(name, run, order) {
|
|
43
|
+
return { name, run, order };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
exports.createHook = createHook;
|
|
47
|
+
exports.createRura = createRura;
|
|
48
|
+
exports.runRura = runRura;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
2
|
+
interface Done<Output> {
|
|
3
|
+
done: true;
|
|
4
|
+
output: Output;
|
|
5
|
+
}
|
|
6
|
+
interface Hook<Context, Output> {
|
|
7
|
+
name: string;
|
|
8
|
+
order?: number;
|
|
9
|
+
run(ctx: Context): MaybePromise<void | Done<Output>>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare function runRura<Context = unknown, Output = unknown>(ctx: Context, hooks: Hook<Context, Output>[]): Promise<Output | Context>;
|
|
13
|
+
|
|
14
|
+
declare function createRura<Context = unknown, Output = unknown>(): {
|
|
15
|
+
use: (hook: Hook<Context, Output>) => /*elided*/ any;
|
|
16
|
+
merge: (other: ReturnType<typeof createRura<Context, Output>>) => /*elided*/ any;
|
|
17
|
+
getHooks: () => Hook<Context, Output>[];
|
|
18
|
+
run: (ctx: Context) => Promise<Context | Output>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare function createHook<Context = unknown, Output = unknown>(name: string, run: Hook<Context, Output>["run"], order?: number): Hook<Context, Output>;
|
|
22
|
+
|
|
23
|
+
export { type Hook, createHook, createRura, runRura };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
2
|
+
interface Done<Output> {
|
|
3
|
+
done: true;
|
|
4
|
+
output: Output;
|
|
5
|
+
}
|
|
6
|
+
interface Hook<Context, Output> {
|
|
7
|
+
name: string;
|
|
8
|
+
order?: number;
|
|
9
|
+
run(ctx: Context): MaybePromise<void | Done<Output>>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare function runRura<Context = unknown, Output = unknown>(ctx: Context, hooks: Hook<Context, Output>[]): Promise<Output | Context>;
|
|
13
|
+
|
|
14
|
+
declare function createRura<Context = unknown, Output = unknown>(): {
|
|
15
|
+
use: (hook: Hook<Context, Output>) => /*elided*/ any;
|
|
16
|
+
merge: (other: ReturnType<typeof createRura<Context, Output>>) => /*elided*/ any;
|
|
17
|
+
getHooks: () => Hook<Context, Output>[];
|
|
18
|
+
run: (ctx: Context) => Promise<Context | Output>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare function createHook<Context = unknown, Output = unknown>(name: string, run: Hook<Context, Output>["run"], order?: number): Hook<Context, Output>;
|
|
22
|
+
|
|
23
|
+
export { type Hook, createHook, createRura, runRura };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/is-done.ts
|
|
2
|
+
function isDone(r) {
|
|
3
|
+
return typeof r === "object" && r !== null && "done" in r;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/run-rura.ts
|
|
7
|
+
async function runRura(ctx, hooks) {
|
|
8
|
+
hooks.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
9
|
+
for (const hook of hooks) {
|
|
10
|
+
const result = await hook.run(ctx);
|
|
11
|
+
if (isDone(result)) {
|
|
12
|
+
return result.output;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return ctx;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/create-rura.ts
|
|
19
|
+
function createRura() {
|
|
20
|
+
const hooks = [];
|
|
21
|
+
function use(hook) {
|
|
22
|
+
hooks.push(hook);
|
|
23
|
+
return api;
|
|
24
|
+
}
|
|
25
|
+
function merge(other) {
|
|
26
|
+
other.getHooks().forEach((h) => hooks.push(h));
|
|
27
|
+
return api;
|
|
28
|
+
}
|
|
29
|
+
function getHooks() {
|
|
30
|
+
return hooks;
|
|
31
|
+
}
|
|
32
|
+
async function run(ctx) {
|
|
33
|
+
return runRura(ctx, hooks);
|
|
34
|
+
}
|
|
35
|
+
const api = { use, merge, getHooks, run };
|
|
36
|
+
return api;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/create-hook.ts
|
|
40
|
+
function createHook(name, run, order) {
|
|
41
|
+
return { name, run, order };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { createHook, createRura, runRura };
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rura",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A minimal pipeline engine for every modern workflow.",
|
|
5
|
+
"author": "Yiming Liao",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/yiming-liao/rura#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/yiming-liao/rura.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/yiming-liao/rura/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"type": "module",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup",
|
|
34
|
+
"prepublishOnly": "yarn build",
|
|
35
|
+
"test": "vitest",
|
|
36
|
+
"type": "tsc --noEmit",
|
|
37
|
+
"lint": "eslint",
|
|
38
|
+
"lint:debug": "eslint --debug",
|
|
39
|
+
"knip": "knip --config .config/knip.config.ts"
|
|
40
|
+
},
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=16.0.0"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^24.10.1",
|
|
48
|
+
"@vitest/coverage-v8": "4.0.15",
|
|
49
|
+
"eslint": "^9.39.1",
|
|
50
|
+
"eslint-config-prettier": "^10.1.8",
|
|
51
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
52
|
+
"eslint-plugin-import": "^2.32.0",
|
|
53
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
54
|
+
"eslint-plugin-unicorn": "^62.0.0",
|
|
55
|
+
"eslint-plugin-unused-imports": "^4.3.0",
|
|
56
|
+
"knip": "^5.69.1",
|
|
57
|
+
"prettier": "^3.6.2",
|
|
58
|
+
"ts-node": "^10.9.2",
|
|
59
|
+
"tsup": "^8.4.0",
|
|
60
|
+
"typescript": "^5.8.3",
|
|
61
|
+
"typescript-eslint": "^8.46.4",
|
|
62
|
+
"vitest": "^4.0.9"
|
|
63
|
+
}
|
|
64
|
+
}
|