@lark-base-open/base-database-app-sdk 0.5.0-durable.0 → 0.5.1
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/CHANGELOG.md +25 -73
- package/README.md +4 -40
- package/capabilities.json +7 -1
- package/dist/chunk-OF7TEKK6.js +1 -0
- package/dist/components.js +1 -1
- package/dist/page.js +1 -1
- package/dist/server.cjs +1 -1
- package/dist/server.js +1 -1
- package/dist/testing.d.ts +1 -2
- package/dist/theme.css +129 -8
- package/docs/README.md +1 -1
- package/docs/capabilities.md +3 -2
- package/docs/codegen.md +3 -3
- package/docs/compat.md +2 -2
- package/docs/components.md +2 -2
- package/docs/exports.md +2 -14
- package/docs/page.md +4 -4
- package/docs/server.md +4 -4
- package/docs/shared.md +1 -1
- package/docs/start-base-app.md +6 -8
- package/docs/testing.md +4 -24
- package/docs/upgrade.md +73 -0
- package/examples/upgrade.ts +1 -1
- package/package.json +3 -63
- package/dist/chunk-KJQ3P646.js +0 -1
- package/dist/durable/activity.cjs +0 -25
- package/dist/durable/activity.d.cts +0 -6
- package/dist/durable/activity.d.ts +0 -6
- package/dist/durable/workflow.cjs +0 -107
- package/dist/durable/workflow.d.cts +0 -15
- package/dist/durable/workflow.d.ts +0 -15
- package/docs/durable.md +0 -54
- package/examples/durable-activities.ts +0 -3
- package/examples/durable-workflow.ts +0 -8
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function getAttempt(): number;
|
|
2
|
-
export declare function cancellationSignal(): AbortSignal;
|
|
3
|
-
/** input 与普通 Site Function 的请求体一致,例如 { params: { city: '北京' } }。 */
|
|
4
|
-
export declare function invokeFunction<T = unknown>(functionId: string, input: Record<string, unknown>, options: {
|
|
5
|
-
key: string;
|
|
6
|
-
}): Promise<T>;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function getAttempt(): number;
|
|
2
|
-
export declare function cancellationSignal(): AbortSignal;
|
|
3
|
-
/** input 与普通 Site Function 的请求体一致,例如 { params: { city: '北京' } }。 */
|
|
4
|
-
export declare function invokeFunction<T = unknown>(functionId: string, input: Record<string, unknown>, options: {
|
|
5
|
-
key: string;
|
|
6
|
-
}): Promise<T>;
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runDefinition = runDefinition;
|
|
4
|
-
exports.callStep = callStep;
|
|
5
|
-
exports.sleep = sleep;
|
|
6
|
-
exports.reportProgress = reportProgress;
|
|
7
|
-
exports.waitForEvent = waitForEvent;
|
|
8
|
-
const workflow_1 = require("@temporalio/workflow");
|
|
9
|
-
let state;
|
|
10
|
-
let definition;
|
|
11
|
-
let keys;
|
|
12
|
-
let events;
|
|
13
|
-
let receivedEvents;
|
|
14
|
-
let eventOverflow;
|
|
15
|
-
function receiveEvent(name, data, id) {
|
|
16
|
-
if (!(definition.events ?? ['approval']).includes(name))
|
|
17
|
-
return;
|
|
18
|
-
const key = id === undefined ? undefined : name + ':' + id;
|
|
19
|
-
if (key && receivedEvents.has(key))
|
|
20
|
-
return;
|
|
21
|
-
if (receivedEvents.size >= 1000 || [...events.values()].reduce((n, items) => n + items.length, 0) >= 1000) {
|
|
22
|
-
eventOverflow = true;
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
if (key)
|
|
26
|
-
receivedEvents.add(key);
|
|
27
|
-
const pending = events.get(name) ?? [];
|
|
28
|
-
pending.push(data);
|
|
29
|
-
events.set(name, pending);
|
|
30
|
-
}
|
|
31
|
-
/** @internal */
|
|
32
|
-
async function runDefinition(meta, fn, input) {
|
|
33
|
-
definition = meta;
|
|
34
|
-
keys = new Set();
|
|
35
|
-
events = new Map();
|
|
36
|
-
receivedEvents = new Set();
|
|
37
|
-
eventOverflow = false;
|
|
38
|
-
state = { stage: 'starting', processed: 0, total: 0, paused: false, approved: false, results: [] };
|
|
39
|
-
(0, workflow_1.setHandler)((0, workflow_1.defineQuery)('progress'), () => ({ ...state, results: [...state.results] }));
|
|
40
|
-
(0, workflow_1.setHandler)((0, workflow_1.defineSignal)('pause'), () => { state.paused = true; });
|
|
41
|
-
(0, workflow_1.setHandler)((0, workflow_1.defineSignal)('resume'), () => { state.paused = false; });
|
|
42
|
-
(0, workflow_1.setHandler)((0, workflow_1.defineSignal)('approve'), () => { state.approved = true; receiveEvent('approval', { approved: true }); });
|
|
43
|
-
(0, workflow_1.setHandler)((0, workflow_1.defineSignal)('event'), (event) => {
|
|
44
|
-
if (!event || typeof event.name !== 'string' || typeof event.id !== 'string' || event.id.length < 8 || event.id.length > 128)
|
|
45
|
-
return;
|
|
46
|
-
receiveEvent(event.name, event.data, event.id);
|
|
47
|
-
});
|
|
48
|
-
try {
|
|
49
|
-
const result = await fn(input);
|
|
50
|
-
await boundary();
|
|
51
|
-
state.stage = 'completed';
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
if (e instanceof TypeError || e instanceof RangeError)
|
|
56
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Durable Function definition error', 'DefinitionError');
|
|
57
|
-
throw e;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async function boundary() {
|
|
61
|
-
if (eventOverflow)
|
|
62
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Durable event limit exceeded', 'EventLimitExceeded');
|
|
63
|
-
if (state.paused) {
|
|
64
|
-
state.stage = 'paused';
|
|
65
|
-
await (0, workflow_1.condition)(() => !state.paused || eventOverflow);
|
|
66
|
-
}
|
|
67
|
-
if (eventOverflow)
|
|
68
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Durable event limit exceeded', 'EventLimitExceeded');
|
|
69
|
-
}
|
|
70
|
-
async function callStep(name, args, options) {
|
|
71
|
-
await boundary();
|
|
72
|
-
if (!definition.activities[name] || !options.key || keys.has(options.key) || keys.size >= 100)
|
|
73
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Invalid or repeated step key', 'DefinitionError');
|
|
74
|
-
keys.add(options.key);
|
|
75
|
-
const policy = definition.activities[name];
|
|
76
|
-
state.stage = 'processing';
|
|
77
|
-
const activity = definition.id + '__' + name;
|
|
78
|
-
const proxy = (0, workflow_1.proxyActivities)({
|
|
79
|
-
activityId: options.key, startToCloseTimeout: policy.timeout, scheduleToCloseTimeout: '2 minutes',
|
|
80
|
-
retry: { initialInterval: '1 second', maximumInterval: '5 seconds', maximumAttempts: policy.retry.maximumAttempts },
|
|
81
|
-
});
|
|
82
|
-
return proxy[activity](args);
|
|
83
|
-
}
|
|
84
|
-
async function sleep(milliseconds) {
|
|
85
|
-
await boundary();
|
|
86
|
-
state.stage = 'durable_wait';
|
|
87
|
-
await (0, workflow_1.sleep)(milliseconds);
|
|
88
|
-
await boundary();
|
|
89
|
-
}
|
|
90
|
-
function reportProgress(progress) {
|
|
91
|
-
Object.assign(state, progress);
|
|
92
|
-
}
|
|
93
|
-
async function waitForEvent(name, options) {
|
|
94
|
-
if (!(definition.events ?? ['approval']).includes(name))
|
|
95
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Undeclared event', 'DefinitionError');
|
|
96
|
-
if (!Number.isFinite(options.timeout) || options.timeout <= 0)
|
|
97
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Invalid event timeout', 'DefinitionError');
|
|
98
|
-
await boundary();
|
|
99
|
-
state.stage = 'waiting_' + name;
|
|
100
|
-
if (!await (0, workflow_1.condition)(() => (events.get(name)?.length ?? 0) > 0 || eventOverflow, options.timeout))
|
|
101
|
-
throw workflow_1.ApplicationFailure.nonRetryable('Event wait timed out', 'EventTimeout');
|
|
102
|
-
await boundary();
|
|
103
|
-
const result = events.get(name).shift();
|
|
104
|
-
if (name === 'approval')
|
|
105
|
-
state.approved = false;
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
type Json = any;
|
|
2
|
-
export declare function callStep<T>(name: string, args: Json, options: {
|
|
3
|
-
key: string;
|
|
4
|
-
}): Promise<T>;
|
|
5
|
-
export declare function sleep(milliseconds: number): Promise<void>;
|
|
6
|
-
export declare function reportProgress(progress: {
|
|
7
|
-
stage?: string;
|
|
8
|
-
processed: number;
|
|
9
|
-
total: number;
|
|
10
|
-
results?: Json[];
|
|
11
|
-
}): void;
|
|
12
|
-
export declare function waitForEvent<T = Json>(name: string, options: {
|
|
13
|
-
timeout: number;
|
|
14
|
-
}): Promise<T>;
|
|
15
|
-
export {};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
type Json = any;
|
|
2
|
-
export declare function callStep<T>(name: string, args: Json, options: {
|
|
3
|
-
key: string;
|
|
4
|
-
}): Promise<T>;
|
|
5
|
-
export declare function sleep(milliseconds: number): Promise<void>;
|
|
6
|
-
export declare function reportProgress(progress: {
|
|
7
|
-
stage?: string;
|
|
8
|
-
processed: number;
|
|
9
|
-
total: number;
|
|
10
|
-
results?: Json[];
|
|
11
|
-
}): void;
|
|
12
|
-
export declare function waitForEvent<T = Json>(name: string, options: {
|
|
13
|
-
timeout: number;
|
|
14
|
-
}): Promise<T>;
|
|
15
|
-
export {};
|
package/docs/durable.md
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# Base Site Durable SDK
|
|
2
|
-
|
|
3
|
-
供 Base Site 的 Durable Function 源码使用。Workflow 定义执行顺序和等待,Activity 执行网络请求、站点 Function 调用等业务操作。执行由 DevCenter 管理的 Worker 承载。
|
|
4
|
-
|
|
5
|
-
Durable 是 App SDK 的两个子入口。预发布版本 0.5.0-durable.0 使用 npm 的 durable 标签;0.4.0 不包含这些入口。使用前需确认目标平台已部署匹配的 Durable 运行时。
|
|
6
|
-
|
|
7
|
-
## 构建与安装
|
|
8
|
-
|
|
9
|
-
安装此预发布版本:`npm install @lark-base-open/base-database-app-sdk@0.5.0-durable.0`。
|
|
10
|
-
|
|
11
|
-
本地开发 SDK 时,在 SDK 根目录执行 `npm ci`、`npm run build`、`npm pack`。构建完成类型检查、JS 编译和公开声明生成。在使用项目中通过 `npm install <tarball-path>` 安装。Temporal 是可选 peer。开发时若直接加载 Durable 模块,需要安装 `@temporalio/workflow@1.24.0` 与 `@temporalio/activity@1.24.0`;只做公开类型检查不需要加载它们。托管 Worker 负责提供匹配版本,当前固定为 1.24.0。
|
|
12
|
-
|
|
13
|
-
Durable 使用 `/durable/workflow` 和 `/durable/activity` 两个入口。Workflow API 在 Temporal Workflow 执行上下文内使用,Activity API 在托管 Worker 的 Activity 上下文内使用;普通 Node 进程中的 import 成功不代表任务可以执行。
|
|
14
|
-
|
|
15
|
-
## 编排与步骤
|
|
16
|
-
|
|
17
|
-
`workflow.ts`:
|
|
18
|
-
|
|
19
|
-
<!-- example: durable-workflow.ts -->
|
|
20
|
-
```ts
|
|
21
|
-
import { callStep, reportProgress, sleep } from '@lark-base-open/base-database-app-sdk/durable/workflow';
|
|
22
|
-
|
|
23
|
-
export default async function task(input: { value: number }) {
|
|
24
|
-
const result = await callStep<number>('double', input, { key: 'double-input' });
|
|
25
|
-
reportProgress({ processed: 1, total: 1 });
|
|
26
|
-
await sleep(1000);
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
`activities.ts`:
|
|
32
|
-
|
|
33
|
-
<!-- example: durable-activities.ts -->
|
|
34
|
-
```ts
|
|
35
|
-
export async function double(input: { value: number }): Promise<number> {
|
|
36
|
-
return input.value * 2;
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
在对应 Function 的 `meta.json` 中声明 `double` Activity 的 timeout 和 retry.maximumAttempts。输入 Schema、发布方式和任务调用接口由 Site/DevCenter 契约管理。
|
|
41
|
-
|
|
42
|
-
`callStep` 的 key 在同一次 Workflow 中必须唯一;等待使用 SDK 的 `sleep` 或 `waitForEvent`,外部 I/O 放入 Activity。暂停在 SDK 边界生效,不中断已经开始执行的 Activity。
|
|
43
|
-
|
|
44
|
-
## 站点能力
|
|
45
|
-
|
|
46
|
-
Activity 通过 `invokeFunction(functionId, input, { key })` 调用同一站点的普通 Function,input 与普通调用请求体一致,例如 `{ params: { city: '北京' } }`。key 表示同一个业务调用,Activity 重试时保持不变。平台注入调用通道和执行凭证,业务源码不传 token。
|
|
47
|
-
|
|
48
|
-
请求结果未知时 Activity 可能重试。稳定 key 不等于业务端已经去重,写操作仍须由 Function 实现幂等处理。
|
|
49
|
-
|
|
50
|
-
## 类型维护
|
|
51
|
-
|
|
52
|
-
公开声明由 TypeScript 源码生成,内部启动器接口不出现在公开声明中。修改源码后执行 `npm run build`。平台私有传输实现不进入 SDK tarball。
|
|
53
|
-
|
|
54
|
-
SDK 的构建、声明与打包测试由本目录独立维护。执行端消费本包的 tarball;平台凭证传输和 Worker 管理不进入此包。
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { callStep, reportProgress, sleep } from '@lark-base-open/base-database-app-sdk/durable/workflow';
|
|
2
|
-
|
|
3
|
-
export default async function task(input: { value: number }) {
|
|
4
|
-
const result = await callStep<number>('double', input, { key: 'double-input' });
|
|
5
|
-
reportProgress({ processed: 1, total: 1 });
|
|
6
|
-
await sleep(1000);
|
|
7
|
-
return result;
|
|
8
|
-
}
|