@triggerlink/sdk 0.4.9 → 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/dist/function.d.ts +26 -2
- package/dist/function.js +3 -2
- package/dist/index.d.ts +1 -1
- package/dist/serve.js +24 -2
- package/dist/step.d.ts +2 -0
- package/dist/step.js +2 -2
- package/package.json +1 -1
package/dist/function.d.ts
CHANGED
|
@@ -22,15 +22,39 @@ export interface HandlerContext<T = unknown> {
|
|
|
22
22
|
runId: string;
|
|
23
23
|
attempt: number;
|
|
24
24
|
}
|
|
25
|
+
/** triggerlink/run.failed 内部事件的 data(FR-2.11),即 onFailure handler 的 event.data。 */
|
|
26
|
+
export interface RunFailedEventData {
|
|
27
|
+
run_id: string;
|
|
28
|
+
function_id: string;
|
|
29
|
+
error: string;
|
|
30
|
+
/** 触发该 run 的原始事件 */
|
|
31
|
+
event: EventPayload;
|
|
32
|
+
}
|
|
25
33
|
export interface FunctionOpts {
|
|
26
34
|
/** 稳定标识,改名会丢历史 memo 关联 */
|
|
27
35
|
id: string;
|
|
28
|
-
/**
|
|
29
|
-
event
|
|
36
|
+
/** 订阅的事件名;纯 cron 函数可省略(event/cron 至少其一) */
|
|
37
|
+
event?: string;
|
|
38
|
+
/** 事件触发 match 表达式(FR-3.1),expr 语法,环境只含 data(= 事件 data) */
|
|
39
|
+
match?: string;
|
|
40
|
+
/** 标准 5 字段 cron(分 时 日 月 周),UTC */
|
|
41
|
+
cron?: string;
|
|
30
42
|
/** 重试上限;0/缺省 = 平台默认(4) */
|
|
31
43
|
retries?: number;
|
|
32
44
|
/** 取消规则,随 manifest 上报为 cancel_on */
|
|
33
45
|
cancelOn?: CancelOnRule[];
|
|
46
|
+
/**
|
|
47
|
+
* run 级超时(FR-4.3),随 manifest 上报为 timeout(Go duration 字符串或毫秒数,
|
|
48
|
+
* 如 "5m" 或 300000)。run 从创建起超过该时长仍在 Queued/Running,平台置 Failed
|
|
49
|
+
* 并发 triggerlink/run.failed 事件——配置了 onFailure 的函数会因此触发。缺省 = 不限时。
|
|
50
|
+
*/
|
|
51
|
+
timeout?: string | number;
|
|
52
|
+
/**
|
|
53
|
+
* run 进入 Failed 终态时的处理器(FR-2.11)。serve 时注册隐式函数 <id>/on-failure,
|
|
54
|
+
* 订阅内部事件 triggerlink/run.failed(match 按 function_id 过滤),handler 内可用全部 step 原语。
|
|
55
|
+
* onFailure 自身失败不会递归触发。
|
|
56
|
+
*/
|
|
57
|
+
onFailure?: (ctx: HandlerContext<RunFailedEventData>) => Promise<unknown>;
|
|
34
58
|
}
|
|
35
59
|
export interface TriggerFunction<T = unknown> {
|
|
36
60
|
readonly opts: FunctionOpts & {
|
package/dist/function.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
export function createFunction(opts, handler) {
|
|
3
3
|
if (!opts.id)
|
|
4
4
|
throw new Error("createFunction: id is required");
|
|
5
|
-
if (!opts.event)
|
|
6
|
-
throw new Error("createFunction: event is required");
|
|
5
|
+
if (!opts.event && !opts.cron) {
|
|
6
|
+
throw new Error("createFunction: event or cron is required"); // 纯 cron 函数无事件订阅
|
|
7
|
+
}
|
|
7
8
|
return { opts: { retries: 0, ...opts }, handler };
|
|
8
9
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createClient, type Client, type ClientOptions, type SendEventInput, type SendResult, type RegisterOptions, } from "./client.js";
|
|
2
|
-
export { createFunction, type TriggerFunction, type FunctionOpts, type HandlerContext, type EventPayload, type TriggerLinkEvent, type CancelOnRule, } from "./function.js";
|
|
2
|
+
export { createFunction, type TriggerFunction, type FunctionOpts, type HandlerContext, type EventPayload, type TriggerLinkEvent, type CancelOnRule, type RunFailedEventData, } from "./function.js";
|
|
3
3
|
export { serve, sdkVersion, type ServeOptions } from "./serve.js";
|
|
4
4
|
export { StepInterrupt, type Opcode, type StepState, type OutgoingEvent } from "./execx.js";
|
|
5
5
|
export type { StepTool, WaitForEventOpts } from "./step.js";
|
package/dist/serve.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExecCtx, StepInterrupt } from "./execx.js";
|
|
2
|
-
import { createStepTool, errMessage } from "./step.js";
|
|
2
|
+
import { createStepTool, errMessage, msToGoDuration } from "./step.js";
|
|
3
3
|
import { verifySignature } from "./sign.js";
|
|
4
4
|
export const sdkVersion = "triggerlink-ts/0.4.6";
|
|
5
5
|
export const SIGNATURE_HEADER = "x-triggerlink-signature";
|
|
@@ -9,8 +9,21 @@ function json(data, status = 200) {
|
|
|
9
9
|
}
|
|
10
10
|
export function serve(opts) {
|
|
11
11
|
const byID = new Map();
|
|
12
|
-
for (const fn of opts.functions)
|
|
12
|
+
for (const fn of opts.functions) {
|
|
13
13
|
byID.set(fn.opts.id, fn);
|
|
14
|
+
if (fn.opts.onFailure) {
|
|
15
|
+
// FR-2.11:注册隐式函数,订阅 run 失败内部事件并按 function_id 过滤
|
|
16
|
+
byID.set(`${fn.opts.id}/on-failure`, {
|
|
17
|
+
opts: {
|
|
18
|
+
id: `${fn.opts.id}/on-failure`,
|
|
19
|
+
event: "triggerlink/run.failed",
|
|
20
|
+
match: `data.function_id == '${fn.opts.id}'`,
|
|
21
|
+
retries: 0,
|
|
22
|
+
},
|
|
23
|
+
handler: fn.opts.onFailure,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
14
27
|
async function GET(req) {
|
|
15
28
|
if (!verifySignature(opts.client.signingKey, req.headers.get(SIGNATURE_HEADER), new Uint8Array(0))) {
|
|
16
29
|
return json({ error: "unauthorized" }, 401);
|
|
@@ -21,10 +34,19 @@ export function serve(opts) {
|
|
|
21
34
|
functions: [...byID.values()].map((f) => ({
|
|
22
35
|
id: f.opts.id,
|
|
23
36
|
event: f.opts.event,
|
|
37
|
+
...(f.opts.match ? { match: f.opts.match } : {}),
|
|
38
|
+
...(f.opts.cron ? { cron: f.opts.cron } : {}),
|
|
24
39
|
retries: f.opts.retries,
|
|
25
40
|
...(f.opts.cancelOn?.length
|
|
26
41
|
? { cancel_on: f.opts.cancelOn.map((c) => ({ event: c.event, match: c.match })) }
|
|
27
42
|
: {}),
|
|
43
|
+
...(f.opts.timeout
|
|
44
|
+
? {
|
|
45
|
+
timeout: typeof f.opts.timeout === "number"
|
|
46
|
+
? msToGoDuration(f.opts.timeout, "createFunction")
|
|
47
|
+
: f.opts.timeout,
|
|
48
|
+
}
|
|
49
|
+
: {}),
|
|
28
50
|
})),
|
|
29
51
|
});
|
|
30
52
|
}
|
package/dist/step.d.ts
CHANGED
|
@@ -39,4 +39,6 @@ export interface StepTool {
|
|
|
39
39
|
}
|
|
40
40
|
/** 为一次函数调用构造 step 工具。 */
|
|
41
41
|
export declare function createStepTool(ec: ExecCtx): StepTool;
|
|
42
|
+
/** 毫秒 → Go duration 字符串(协议要求 timeout 为 Go duration,如 1500 → "1.5s")。 */
|
|
43
|
+
export declare function msToGoDuration(ms: number, what?: string): string;
|
|
42
44
|
export declare function errMessage(err: unknown): string;
|
package/dist/step.js
CHANGED
|
@@ -67,9 +67,9 @@ export function createStepTool(ec) {
|
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
/** 毫秒 → Go duration 字符串(协议要求 timeout 为 Go duration,如 1500 → "1.5s")。 */
|
|
70
|
-
function msToGoDuration(ms) {
|
|
70
|
+
export function msToGoDuration(ms, what = "step.waitForEvent") {
|
|
71
71
|
if (!Number.isFinite(ms) || ms <= 0) {
|
|
72
|
-
throw new Error(
|
|
72
|
+
throw new Error(`${what}: invalid timeout ${ms}`);
|
|
73
73
|
}
|
|
74
74
|
return `${ms / 1000}s`;
|
|
75
75
|
}
|